-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtext-limiter.php
134 lines (114 loc) · 4.18 KB
/
text-limiter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* Plugin Name: MB Text Limiter
* Plugin URI: https://metabox.io/plugins/meta-box-text-limiter/
* Description: Limit number of characters or words entered for text, textarea, and wysiwyg fields.
* Version: 1.2.5
* Author: MetaBox.io
* Author URI: https://metabox.io
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright (C) 2010-2025 Tran Ngoc Tuan Anh. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if ( ! class_exists( 'MB_Text_Limiter' ) ) {
class MB_Text_Limiter {
/**
* List of supported fields.
*
* @var array
*/
protected $types = [ 'text', 'textarea', 'wysiwyg' ];
public function init() {
add_action( 'rwmb_before', [ $this, 'register' ] );
// Change the output of fields with limit.
add_filter( 'rwmb_get_value', [ $this, 'get_value' ], 10, 2 );
add_filter( 'rwmb_the_value', [ $this, 'get_value' ], 10, 2 );
add_action( 'rwmb_enqueue_scripts', [ $this, 'enqueue' ] );
}
/**
* Register hook to change the output of text/textarea fields.
*/
public function register() {
foreach ( $this->types as $type ) {
add_filter( "rwmb_{$type}_html", [ $this, 'show' ], 10, 2 );
}
}
/**
* Change the output of text/textarea fields.
*
* @param string $output HTML output of the field.
* @param array $field Field parameter.
*
* @return string
*/
public function show( $output, $field ) {
static $run = false;
if ( $run || ! isset( $field['limit'] ) || ! is_numeric( $field['limit'] ) || ! $field['limit'] > 0 ) {
return $output;
}
$type = isset( $field['limit_type'] ) ? $field['limit_type'] : 'character';
$text = 'word' === $type ? __( 'Word Count', 'text-limiter' ) : __( 'Character Count', 'text-limiter' );
$run = true;
return $output . '
<div class="text-limiter" data-limit-type="' . esc_attr( $type ) . '">
<span>' . esc_html( $text ) . ':
<span class="counter">0</span>/<span class="maximum">' . esc_html( $field['limit'] ) . '</span>
</span>
</div>';
}
/**
* Filters the value of a field
*
* @see rwmb_get_field() in meta-box/inc/functions.php for explenation
*
* @param string $value Field value.
* @param array $field Field parameters.
*
* @return string
*/
public function get_value( $value, $field ) {
if ( empty( $field ) ) {
return $value;
}
if ( ! in_array( $field['type'], $this->types, true ) || empty( $field['limit'] ) || ! is_numeric( $field['limit'] ) ) {
return $value;
}
if ( ! is_string( $value ) ) {
return $value;
}
// Don't truncate if $value contains HTML.
if ( str_contains( $value, '<' ) ) {
return $value;
}
$type = isset( $field['limit_type'] ) ? $field['limit_type'] : 'character';
if ( 'character' === $type ) {
return function_exists( 'mb_substr' ) ? mb_substr( $value, 0, $field['limit'] ) : substr( $value, 0, $field['limit'] );
}
$value = preg_split( '/\s+/', $value, -1, PREG_SPLIT_NO_EMPTY );
$value = implode( ' ', array_slice( $value, 0, $field['limit'] ) );
return $value;
}
public function enqueue() {
// Use helper function to get correct URL to current folder, which can be used in themes/plugins.
list( , $url ) = RWMB_Loader::get_path( __DIR__ );
wp_enqueue_style( 'text-limiter', $url . 'text-limiter.css', [], filemtime( __DIR__ . '/text-limiter.css' ) );
wp_enqueue_script( 'text-limiter', $url . 'text-limiter.js', [ 'jquery' ], filemtime( __DIR__ . '/text-limiter.js' ), true );
}
}
$text_limiter = new MB_Text_Limiter();
$text_limiter->init();
}