forked from Hube2/acf-input-counter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acf-input-counter.php
188 lines (162 loc) · 5.87 KB
/
acf-input-counter.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
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
<?php
/*
Plugin Name: ACF Input Counter
Plugin URI: https://github.com/Hube2/acf-input-counter/
Description: Show character count for limited text and textarea fields
Version: 1.4.1
Author: John A. Huebner II
Author URI: https://github.com/Hube2/
Text-domain: acf-counter
Domain-path: languages
GitHub Plugin URI: https://github.com/Hube2/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.4.1';
public function __construct() {
add_action('plugins_loaded', array($this, 'acf_counter_load_plugin_textdomain'));
add_action('acf/render_field/type=text', array($this, 'render_field'), 20, 1);
add_action('acf/render_field/type=textarea', array($this, 'render_field'), 20, 1);
add_action('acf/input/admin_enqueue_scripts', array($this, 'scripts'));
add_filter('jh_plugins_list', array($this, 'meta_box_data'));
} // end public function __construct
public function acf_counter_load_plugin_textdomain() {
load_plugin_textdomain( 'acf-counter', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
function meta_box_data($plugins=array()) {
$plugins[] = array(
'title' => 'ACF Input Counter',
'screens' => array('acf-field-group', 'edit-acf-field-group'),
'doc' => 'https://github.com/Hube2/acf-input-counter'
);
return $plugins;
} // end function meta_box
private function run() {
// cannot run on field group editor or it will
// add code to every ACF field in the editor
$run = true;
global $post;
if ($post && $post->ID && get_post_type($post->ID) == 'acf-field-group') {
$run = false;
}
return $run;
} // end private function run
public function scripts() {
if (!$this->run()) {
return;
}
// wp_enqueue_script
$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);
wp_enqueue_style('acf-counter', plugins_url( 'acf-counter.css' , __FILE__ ));
} // end public function scripts
public function render_field($field) {
//echo '<pre>'; print_r($field); echo '</pre>';
if (!$this->run() ||
!$field['maxlength'] ||
($field['type'] != 'text' && $field['type'] != 'textarea')) {
// only run on text and text area fields when maxlength is set
return;
}
if (function_exists('mb_strlen')) {
$len = mb_strlen($field['value']);
} else {
$len = strlen($field['value']);
}
$max = $field['maxlength'];
$classes = apply_filters('acf-input-counter/classes', array());
$ids = apply_filters('acf-input-counter/ids', array());
$insert = true;
if (count($classes) || count($ids)) {
$insert = false;
$exist = array();
if ($field['wrapper']['class']) {
$exist = explode(' ', $field['wrapper']['class']);
}
$insert = $this->check($classes, $exist);
if (!$insert && $field['wrapper']['id']) {
$exist = array();
if ($field['wrapper']['id']) {
$exist = explode(' ', $field['wrapper']['id']);
}
$insert = $this->check($ids, $exist);
}
} // end if filter classes or ids
if (!$insert) {
return;
}
$display = sprintf(
__('chars: %1$s of %2$s', 'acf-counter'),
'%%len%%',
'%%max%%'
);
$display = apply_filters('acf-input-counter/display', $display);
$display = str_replace('%%len%%', '<span class="count">'.$len.'</span>', $display);
$display = str_replace('%%max%%', $max, $display);
?>
<span class="char-count">
<?php
echo $display;
?>
</span>
<?php
} // end public function render_field
private function check($allow, $exist) {
// if there is anything in $allow
// see if any of those values are in $exist
$intersect = array_intersect($allow, $exist);
if (count($intersect)) {
return true;
}
return false;
} // end private function check
} // end class acf_input_counter
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);
} // end function jh_plugins_list_meta_box
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
}
} // end if !function_exists
?>