forked from Hube2/acf-input-counter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathacf-input-counter.php
More file actions
319 lines (265 loc) · 8.39 KB
/
acf-input-counter.php
File metadata and controls
319 lines (265 loc) · 8.39 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/*
Plugin Name: ACF Input Counter
Plugin URI: https://github.com/rowatt/acf-input-counter/
Description: Show character count for limited text and textarea fields
Version: 1.5.1
Author: John A. Huebner II, Mark Rowatt Anderson
Author URI: https://github.com/Hube2/
Text-domain: acf-counter
Domain-path: languages
GitHub Plugin URI: https://github.com/rowatt/acf-input-counter/
License: GPL
*/
// If this file is called directly, abort.
if (!defined('WPINC')) {die;}
new acf_input_counter();
class acf_input_counter {
private $version = '1.5.1';
/**
* Field types which have character limits
*
* @var array
*/
private $limited_field_types = [
'text',
'textarea',
'wysiwyg'
];
public function __construct() {
add_action('plugins_loaded', [ $this, '_acf_counter_load_plugin_textdomain' ] );
add_action('acf/input/admin_enqueue_scripts', [ $this, '_scripts' ] );
add_filter('jh_plugins_list', [ $this, '_meta_box_data' ] );
foreach ($this->limited_field_types as $type) {
//adds counter beneath fields
add_action('acf/render_field/type=' . $type, [ $this, '_render_field' ], 20, 1);
//validates field length
add_filter('acf/validate_value/type=' . $type, [ $this, '_validate_maxlength' ], 10, 4 );
}
//wysiwyg field specific hook
add_action('acf/render_field_settings/type=wysiwyg', [ $this, '_wysiwyg_field_settings' ], 10, 1);
}
public function _acf_counter_load_plugin_textdomain() {
load_plugin_textdomain( 'acf-counter', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
public function _meta_box_data( $plugins=[] ) {
$plugins[] = array(
'title' => 'ACF Input Counter',
'screens' => array('acf-field-group', 'edit-acf-field-group'),
'doc' => 'https://github.com/rowatt/acf-input-counter'
);
return $plugins;
}
/**
* Can we run?
*
* We cannot run on field group editor as it will
* add code to every ACF field in the editor
*
* @return bool
*/
private function run() {
$run = true;
global $post;
if ($post && $post->ID && get_post_type($post->ID) == 'acf-field-group') {
$run = false;
}
return $run;
}
/**
* Enqueue scripts and stylesheets
*/
public function _scripts() {
if (!$this->run()) {
return;
}
$handle = 'acf-input-counter';
$src = plugin_dir_url(__FILE__).'acf-input-counter.js';
$deps = array('acf-input');
$ver = $this->version;
$in_footer = false;
wp_enqueue_script($handle, $src, $deps, $ver, $in_footer);
$data = [
'init' => true
];
wp_localize_script( $handle, 'acf_input_counter_data', $data );
/**
* Turn off plugin CSS if you want to incorporate into site stylesheet
*
* @param bool
*/
if( apply_filters( 'acf-input-counter/load-css', true ) ) {
wp_enqueue_style('acf-counter', plugins_url( 'acf-counter.css' , __FILE__ ));
}
}
/**
* Add max length option to wysiwyg field
*
* @param $field
*/
public function _wysiwyg_field_settings( $field ) {
acf_render_field_setting( $field, array(
'label' => __('Character Limit','acf'),
'instructions' => __('Leave blank for no limit','acf'),
'type' => 'number',
'name' => 'maxlength',
));
}
/**
* Add character counter when rendering limited field
*
* @param array $field
*/
public function _render_field( $field ) {
//only run on field types we are limiting which have maxlength set
if ( ! $this->run() ||
! isset( $field[ 'maxlength' ] ) ||
! in_array( $field['type'], $this->limited_field_types ) ) {
return;
}
$len = $this->content_length( $field[ 'value' ] );
$max = $field[ 'maxlength' ] ?? 0;
$classes = apply_filters( 'acf-input-counter/classes', array() );
$ids = apply_filters( 'acf-input-counter/ids', array() );
$insert = TRUE;
if ( count( $classes ) || count( $ids ) ) {
$exist = [];
if ( $field[ 'wrapper' ][ 'class' ] ) {
$exist = explode( ' ', $field[ 'wrapper' ][ 'class' ] );
}
$insert = $this->does_allowed_exist( $classes, $exist );
if ( ! $insert && $field[ 'wrapper' ][ 'id' ] ) {
$exist = array();
if ( $field[ 'wrapper' ][ 'id' ] ) {
$exist = explode( ' ', $field[ 'wrapper' ][ 'id' ] );
}
$insert = $this->does_allowed_exist( $ids, $exist );
}
}
if ( ! $insert ) {
return;
}
//output wysiwyg maxlength - used by counter js
if( $max && 'wysiwyg' == $field['type'] ) {
printf( '<script>acf_input_counter_data.%s="%d";</script>', $field['key'], $max );
}
$display = sprintf(
__( 'chars: %1$s of %2$s', 'acf-counter' ),
'%%len%%',
'%%max%%'
);
/**
* Filter the text format of the character counter
*
* String should contain:
* %%len%% - replaced with number of characters
* %%max%% - replaced with max allowed number of characters
*
* @param string text to display
*/
$display = apply_filters( 'acf-input-counter/display', $display );
$display = str_replace( '%%len%%', '<span class="count">' . $len . '</span>', $display );
$display = str_replace( '%%max%%', $max, $display );
if ( isset( $field[ 'maxlength' ] ) && $field[ 'maxlength' ] > 0 ) {
printf( '<span class="char-count">%s</span>',$display );
}
}
/**
* Do any elements present in an array match at least
* one element in the allowed list?
*
* @param array $allow elements that are allowed
* @param array $exist elements that are present
*
* @return bool
*/
private function does_allowed_exist($allow, $exist) {
$intersect = array_intersect($allow, $exist);
if (count($intersect)) {
return true;
}
return false;
}
/**
* Make sure that fields can't be more than max length
*
* @param $valid
* @param $value
* @param $field
* @param $input
*
* @return string
*/
public function _validate_maxlength( $valid, $value, $field, $input ) {
$maxlength = $field['maxlength'] ?? 0;
if( $maxlength ) {
$content_length = $this->content_length( $value );
if ( $content_length > $maxlength ) {
$msg = __( 'Field is %d characters but must be no more than %d', 'acf-counter' );
return sprintf( $msg, $content_length, $maxlength );
}
}
return $valid;
}
/**
* Get length of content after stripping out HTML and other things
*
* post_content can include HTML tags, so make sure we strip those out, remove double spaces etc
* and convert any HTML entities to their single unicode character.
*
* @param $content
*
* @return int content length
*/
private function content_length( $content ) {
$content = strip_tags( $content );
//remove linebreaks
$content = str_replace( "\n", '', $content );
$content = str_replace( "\r", '', $content );
$content = preg_replace( '#[\s]{2,}#', ' ', $content );
$content = html_entity_decode( $content, ENT_HTML5 );
return mb_strlen( $content );
}
}
if (!function_exists('jh_plugins_list_meta_box')) {
function jh_plugins_list_meta_box() {
if (apply_filters('remove_hube2_nag', false)) {
return;
}
$plugins = apply_filters('jh_plugins_list', array());
$id = 'plugins-by-john-huebner';
$title = '<a style="text-decoration: none; font-size: 1em;" href="https://github.com/Hube2" target="_blank">Plugins by John Huebner</a>';
$callback = 'show_blunt_plugins_list_meta_box';
$screens = array();
foreach ($plugins as $plugin) {
$screens = array_merge($screens, $plugin['screens']);
}
$context = 'side';
$priority = 'low';
add_meta_box($id, $title, $callback, $screens, $context, $priority);
}
add_action('add_meta_boxes', 'jh_plugins_list_meta_box');
function show_blunt_plugins_list_meta_box() {
$plugins = apply_filters('jh_plugins_list', array());
?>
<p style="margin-bottom: 0;">Thank you for using my plugins</p>
<ul style="margin-top: 0; margin-left: 1em;">
<?php
foreach ($plugins as $plugin) {
?>
<li style="list-style-type: disc; list-style-position:">
<?php
echo $plugin['title'];
if ($plugin['doc']) {
?> <a href="<?php echo $plugin['doc']; ?>" target="_blank">Documentation</a><?php
}
?>
</li>
<?php
}
?>
</ul>
<p><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=hube02%40earthlink%2enet&lc=US&item_name=Donation%20for%20WP%20Plugins%20I%20Use&no_note=0&cn=Add%20special%20instructions%20to%20the%20seller%3a&no_shipping=1¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted" target="_blank">Please consider making a small donation.</a></p><?php
}
}
/* EOF */