PluginProbe
Cool Timeline (Horizontal & Vertical Timeline) / trunk
Cool Timeline (Horizontal & Vertical Timeline) vtrunk
3.4.0 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 2.2.3 2.3.3 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.6.1 2.7.1 2.8.3 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 All 79 releases
cool-timeline / admin / codestar-framework / classes / comment-options.class.php

comment-options.class.php in Cool Timeline (Horizontal & Vertical Timeline) trunk, at admin/codestar-framework/classes/comment-options.class.php

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