PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 2.0.0
Adminify – White Label, Admin Menu Editor, Login Customizer v2.0.0
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / lib / adminify-framework / classes / comment-options.class.php

comment-options.class.php in Adminify – White Label, Admin Menu Editor, Login Customizer 2.0.0, at lib/adminify-framework/classes/comment-options.class.php

348 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
2 /**
3 *
4 * Comment Metabox Class
5 *
6 * @since 1.0.0
7 * @version 1.0.0
8 *
9 */
10 if ( ! class_exists( 'ADMINIFY_Comment_Metabox' ) ) {
11 class ADMINIFY_Comment_Metabox extends ADMINIFY_Abstract{
12
13 // constans
14 public $unique = '';
15 public $abstract = 'comment_metabox';
16 public $pre_fields = array();
17 public $sections = array();
18 public $args = array(
19 'title' => '',
20 'data_type' => 'serialize',
21 'priority' => 'default',
22 'show_reset' => false,
23 'show_restore' => false,
24 'nav' => 'normal',
25 'theme' => 'dark',
26 'class' => '',
27 'defaults' => array(),
28 );
29
30 // run comment metabox construct
31 public function __construct( $key, $params = array() ) {
32
33 $this->unique = $key;
34 $this->args = apply_filters( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
35 $this->sections = apply_filters( "adminify_{$this->unique}_sections", $params['sections'], $this );
36 $this->pre_fields = $this->pre_fields( $this->sections );
37
38 add_action( 'add_meta_boxes_comment', array( &$this, 'add_comment_meta_box' ) );
39 add_action( 'edit_comment', array( &$this, 'save_comment_meta_box' ) );
40
41 if ( ! empty( $this->args['class'] ) ) {
42 add_filter( 'postbox_classes_comment_'. $this->unique, array( &$this, 'add_comment_metabox_classes' ) );
43 }
44
45 }
46
47 // instance
48 public static function instance( $key, $params = array() ) {
49 return new self( $key, $params );
50 }
51
52 public function pre_fields( $sections ) {
53
54 $result = array();
55
56 foreach ( $sections as $key => $section ) {
57 if ( ! empty( $section['fields'] ) ) {
58 foreach ( $section['fields'] as $field ) {
59 $result[] = $field;
60 }
61 }
62 }
63
64 return $result;
65 }
66
67 public function add_comment_metabox_classes( $classes ) {
68
69 if ( ! empty( $this->args['class'] ) ) {
70 $classes[] = $this->args['class'];
71 }
72
73 return $classes;
74
75 }
76
77 // add comment metabox
78 public function add_comment_meta_box( $post_type ) {
79
80 add_meta_box( $this->unique, $this->args['title'], array( &$this, 'add_comment_meta_box_content' ), 'comment', 'normal', $this->args['priority'], $this->args );
81
82 }
83
84 // get default value
85 public function get_default( $field ) {
86
87 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
88 $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
89
90 return $default;
91
92 }
93
94 // get meta value
95 public function get_meta_value( $comment_id, $field ) {
96
97 $value = null;
98
99 if ( ! empty( $comment_id ) && ! empty( $field['id'] ) ) {
100
101 if ( $this->args['data_type'] !== 'serialize' ) {
102 $meta = get_comment_meta( $comment_id, $field['id'] );
103 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
104 } else {
105 $meta = get_comment_meta( $comment_id, $this->unique, true );
106 $value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
107 }
108
109 }
110
111 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
112 $value = ( isset( $value ) ) ? $value : $default;
113
114 return $value;
115
116 }
117
118 // add comment metabox content
119 public function add_comment_meta_box_content( $comment, $callback ) {
120
121 $has_nav = ( count( $this->sections ) > 1 ) ? true : false;
122 $show_all = ( ! $has_nav ) ? ' adminify-show-all' : '';
123 $errors = ( is_object ( $comment ) ) ? get_comment_meta( $comment->comment_ID, '_adminify_errors_'. $this->unique, true ) : array();
124 $errors = ( ! empty( $errors ) ) ? $errors : array();
125 $theme = ( $this->args['theme'] ) ? ' adminify-theme-'. $this->args['theme'] : '';
126 $nav_type = ( $this->args['nav'] === 'inline' ) ? 'inline' : 'normal';
127
128 if ( is_object( $comment ) && ! empty( $errors ) ) {
129 delete_comment_meta( $comment->comment_ID, '_adminify_errors_'. $this->unique );
130 }
131
132 wp_nonce_field( 'adminify_comment_metabox_nonce', 'adminify_comment_metabox_nonce'. $this->unique );
133
134 echo '<div class="adminify adminify-comment-metabox'. esc_attr( $theme ) .'">';
135
136 echo '<div class="adminify-wrapper'. esc_attr( $show_all ) .'">';
137
138 if ( $has_nav ) {
139
140 echo '<div class="adminify-nav adminify-nav-'. esc_attr( $nav_type ) .' adminify-nav-metabox">';
141
142 echo '<ul>';
143
144 $tab_key = 1;
145
146 foreach ( $this->sections as $section ) {
147
148 $tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-tab-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
149 $tab_error = ( ! empty( $errors['sections'][$tab_key] ) ) ? '<i class="adminify-label-error adminify-error">!</i>' : '';
150
151 echo '<li><a href="#">'. $tab_icon . $section['title'] . $tab_error .'</a></li>';
152
153 $tab_key++;
154
155 }
156
157 echo '</ul>';
158
159 echo '</div>';
160
161 }
162
163 echo '<div class="adminify-content">';
164
165 echo '<div class="adminify-sections">';
166
167 $section_key = 1;
168
169 foreach ( $this->sections as $section ) {
170
171 $section_onload = ( ! $has_nav ) ? ' adminify-onload' : '';
172 $section_class = ( ! empty( $section['class'] ) ) ? ' '. $section['class'] : '';
173 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
174 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
175
176 echo '<div class="adminify-section hidden'. esc_attr( $section_onload . $section_class ) .'">';
177
178 echo ( $section_title || $section_icon ) ? '<div class="adminify-section-title"><h3>'. $section_icon . $section_title .'</h3></div>' : '';
179 echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">'. $section['description'] .'</div>' : '';
180
181 if ( ! empty( $section['fields'] ) ) {
182
183 foreach ( $section['fields'] as $field ) {
184
185 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
186 $field['_error'] = $errors['fields'][$field['id']];
187 }
188
189 if ( ! empty( $field['id'] ) ) {
190 $field['default'] = $this->get_default( $field );
191 }
192
193 ADMINIFY::field( $field, $this->get_meta_value( $comment->comment_ID, $field ), $this->unique, 'comment_metabox' );
194
195 }
196
197 } else {
198
199 echo '<div class="adminify-no-option">'. esc_html__( 'No data available.', 'adminify' ) .'</div>';
200
201 }
202
203 echo '</div>';
204
205 $section_key++;
206
207 }
208
209 echo '</div>';
210
211 if ( ! empty( $this->args['show_restore'] ) || ! empty( $this->args['show_reset'] ) ) {
212
213 echo '<div class="adminify-sections-reset">';
214 echo '<label>';
215 echo '<input type="checkbox" name="'. esc_attr( $this->unique ) .'[_reset]" />';
216 echo '<span class="button adminify-button-reset">'. esc_html__( 'Reset', 'adminify' ) .'</span>';
217 echo '<span class="button adminify-button-cancel">'. sprintf( '<small>( %s )</small> %s', esc_html__( 'update post', 'adminify' ), esc_html__( 'Cancel', 'adminify' ) ) .'</span>';
218 echo '</label>';
219 echo '</div>';
220
221 }
222
223 echo '</div>';
224
225 echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="adminify-nav-background"></div>' : '';
226
227 echo '<div class="clear"></div>';
228
229 echo '</div>';
230
231 echo '</div>';
232
233 }
234
235 // save comment metabox
236 public function save_comment_meta_box( $comment_id ) {
237
238 $count = 1;
239 $data = array();
240 $errors = array();
241 $noncekey = 'adminify_comment_metabox_nonce'. $this->unique;
242 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
243
244 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'adminify_comment_metabox_nonce' ) ) {
245 return $comment_id;
246 }
247
248 // XSS ok.
249 // No worries, This "POST" requests is sanitizing in the below foreach.
250 $request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array();
251
252 if ( ! empty( $request ) ) {
253
254 foreach ( $this->sections as $section ) {
255
256 if ( ! empty( $section['fields'] ) ) {
257
258 foreach ( $section['fields'] as $field ) {
259
260 if ( ! empty( $field['id'] ) ) {
261
262 $field_id = $field['id'];
263 $field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
264
265 // Sanitize "post" request of field.
266 if ( ! isset( $field['sanitize'] ) ) {
267
268 if( is_array( $field_value ) ) {
269 $data[$field_id] = wp_kses_post_deep( $field_value );
270 } else {
271 $data[$field_id] = wp_kses_post( $field_value );
272 }
273
274 } else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
275
276 $data[$field_id] = call_user_func( $field['sanitize'], $field_value );
277
278 } else {
279
280 $data[$field_id] = $field_value;
281
282 }
283
284 // Validate "post" request of field.
285 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
286
287 $has_validated = call_user_func( $field['validate'], $field_value );
288
289 if ( ! empty( $has_validated ) ) {
290
291 $errors['sections'][$count] = true;
292 $errors['fields'][$field_id] = $has_validated;
293 $data[$field_id] = $this->get_meta_value( $comment_id, $field );
294
295 }
296
297 }
298
299 }
300
301 }
302
303 }
304
305 $count++;
306
307 }
308
309 }
310
311 $data = apply_filters( "adminify_{$this->unique}_save", $data, $comment_id, $this );
312
313 do_action( "adminify_{$this->unique}_save_before", $data, $comment_id, $this );
314
315 if ( empty( $data ) || ! empty( $request['_reset'] ) ) {
316
317 if ( $this->args['data_type'] !== 'serialize' ) {
318 foreach ( $data as $key => $value ) {
319 delete_comment_meta( $comment_id, $key );
320 }
321 } else {
322 delete_comment_meta( $comment_id, $this->unique );
323 }
324
325 } else {
326
327 if ( $this->args['data_type'] !== 'serialize' ) {
328 foreach ( $data as $key => $value ) {
329 update_comment_meta( $comment_id, $key, $value );
330 }
331 } else {
332 update_comment_meta( $comment_id, $this->unique, $data );
333 }
334
335 if ( ! empty( $errors ) ) {
336 update_comment_meta( $comment_id, '_adminify_errors_'. $this->unique, $errors );
337 }
338
339 }
340
341 do_action( "adminify_{$this->unique}_saved", $data, $comment_id, $this );
342
343 do_action( "adminify_{$this->unique}_save_after", $data, $comment_id, $this );
344
345 }
346 }
347 }
348