PluginProbe
Smart Custom Fields / trunk
Smart Custom Fields vtrunk
1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.7.0 2.0.0 2.0.1 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 All 68 releases
smart-custom-fields / classes / controller / class.settings.php

class.settings.php in Smart Custom Fields trunk, at classes/controller/class.settings.php

541 lines 18.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package smart-custom-fields
4 * @author inc2734
5 * @license GPL-2.0+
6 */
7
8 /**
9 * Smart_Custom_Fields_Controller_Settings class.
10 */
11 class Smart_Custom_Fields_Controller_Settings {
12
13 /**
14 * Selectbox choices of the field selection.
15 *
16 * @var array
17 */
18 private $optgroups = array();
19
20 /**
21 * __construct
22 */
23 public function __construct() {
24 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
25 add_action( 'admin_head', array( $this, 'admin_inline_css' ) );
26 add_action( 'save_post', array( $this, 'save_post' ) );
27 add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
28
29 $this->optgroups = array(
30 'basic-fields' => array(
31 'label' => esc_attr__( 'Basic fields', 'smart-custom-fields' ),
32 'options' => array(),
33 ),
34 'select-fields' => array(
35 'label' => esc_attr__( 'Select fields', 'smart-custom-fields' ),
36 'options' => array(),
37 ),
38 'content-fields' => array(
39 'label' => esc_attr__( 'Content fields', 'smart-custom-fields' ),
40 'options' => array(),
41 ),
42 'other-fields' => array(
43 'label' => esc_attr__( 'Other fields', 'smart-custom-fields' ),
44 'options' => array(),
45 ),
46 );
47 }
48
49 /**
50 * Get Current Admin Color Scheme.
51 *
52 * @return object
53 */
54 protected function admin_color_scheme() {
55 global $_wp_admin_css_colors;
56
57 $user_admin_color_scheme = get_user_option( 'admin_color' );
58 $colors_obj = $_wp_admin_css_colors[ $user_admin_color_scheme ];
59
60 return $colors_obj;
61 }
62
63 /**
64 * Add Custom Inline CSS on Admin Dashboard.
65 */
66 public function admin_inline_css() {
67 $colors = $this->admin_color_scheme()->colors;
68 ?>
69 <style>
70 #smart-cf-meta-box-condition-post .selectivity-load-more.highlight,
71 #smart-cf-meta-box-condition-post .selectivity-result-item.highlight {
72 background-color: <?php echo esc_html( $colors[2] ); ?>;
73 }
74
75 .smart-cf-group .smart-cf-group-repeat label .ios-ui-select.checked,
76 #smart-cf-meta-box-condition-post .ios-ui-select.checked,
77 #smart-cf-meta-box-condition-profile .ios-ui-select.checked,
78 #smart-cf-meta-box-condition-taxonomy .ios-ui-select.checked,
79 #smart-cf-meta-box-condition-options-page .ios-ui-select.checked {
80 box-shadow: inset 0 0 0 36px <?php echo esc_html( $colors[2] ); ?>;
81 }
82 </style>
83 <?php
84 }
85
86 /**
87 * Loading resources.
88 */
89 public function admin_enqueue_scripts() {
90 do_action( SCF_Config::PREFIX . 'before-settings-enqueue-scripts' );
91
92 wp_enqueue_style(
93 SCF_Config::PREFIX . 'settings',
94 SMART_CUSTOM_FIELDS_URL . '/css/settings.css',
95 array(),
96 filemtime( SMART_CUSTOM_FIELDS_PATH . '/css/settings.css' )
97 );
98
99 wp_enqueue_style(
100 SCF_Config::PREFIX . 'selectivity',
101 SMART_CUSTOM_FIELDS_URL . '/libs/selectivity-3.1.0/selectivity-jquery.min.css',
102 array(),
103 filemtime( SMART_CUSTOM_FIELDS_PATH . '/libs/selectivity-3.1.0/selectivity-jquery.min.css' )
104 );
105
106 wp_enqueue_style(
107 SCF_Config::PREFIX . 'ios-checkbox',
108 SMART_CUSTOM_FIELDS_URL . '/libs/iosCheckbox/iosCheckbox.min.css',
109 array(),
110 filemtime( SMART_CUSTOM_FIELDS_PATH . '/libs/iosCheckbox/iosCheckbox.min.css' )
111 );
112
113 wp_enqueue_script(
114 SCF_Config::PREFIX . 'settings',
115 SMART_CUSTOM_FIELDS_URL . '/js/settings.js',
116 array( 'jquery' ),
117 filemtime( SMART_CUSTOM_FIELDS_PATH . '/js/settings.js' ),
118 true
119 );
120
121 wp_enqueue_script(
122 SCF_Config::PREFIX . 'selectivity',
123 SMART_CUSTOM_FIELDS_URL . '/libs/selectivity-3.1.0/selectivity-jquery.min.js',
124 array(),
125 filemtime( SMART_CUSTOM_FIELDS_PATH . '/libs/selectivity-3.1.0/selectivity-jquery.min.js' ),
126 true
127 );
128
129 wp_enqueue_script(
130 SCF_Config::PREFIX . 'ios-checkbox',
131 SMART_CUSTOM_FIELDS_URL . '/libs/iosCheckbox/iosCheckbox.min.js',
132 array(),
133 filemtime( SMART_CUSTOM_FIELDS_PATH . '/libs/iosCheckbox/iosCheckbox.min.js' ),
134 true
135 );
136
137 wp_localize_script(
138 SCF_Config::PREFIX . 'settings',
139 'smart_cf_settings',
140 array(
141 'duplicate_alert' => esc_html__( 'Same name exists!', 'smart-custom-fields' ),
142 'autocomplete_placeholder' => esc_html__( 'Type to search a post or page', 'smart-custom-fields' ),
143 'loading' => esc_html__( 'Loading...', 'smart-custom-fields' ),
144 'load_more' => esc_html__( 'Load more', 'smart-custom-fields' ),
145 'rest_api_url' => rest_url( SCF_Config::PREFIX . 'api/v2/search/posts' ),
146 'nonce' => wp_create_nonce( 'wp_rest' ),
147 )
148 );
149
150 wp_enqueue_script( 'jquery-ui-sortable' );
151 do_action( SCF_Config::PREFIX . 'after-settings-enqueue-scripts' );
152 }
153
154 /**
155 * Adding meta boxes.
156 */
157 public function add_meta_boxes() {
158 add_meta_box(
159 SCF_Config::PREFIX . 'meta-box',
160 __( 'Custom Fields', 'smart-custom-fields' ),
161 array( $this, 'display_meta_box' ),
162 SCF_Config::NAME
163 );
164 add_meta_box(
165 SCF_Config::PREFIX . 'meta-box-condition-post',
166 __( 'Display conditions ( Post )', 'smart-custom-fields' ),
167 array( $this, 'display_meta_box_condition_post' ),
168 SCF_Config::NAME,
169 'side'
170 );
171 add_meta_box(
172 SCF_Config::PREFIX . 'meta-box-condition-profile',
173 __( 'Display conditions ( Profile )', 'smart-custom-fields' ),
174 array( $this, 'display_meta_box_condition_profile' ),
175 SCF_Config::NAME,
176 'side'
177 );
178 add_meta_box(
179 SCF_Config::PREFIX . 'meta-box-condition-taxonomy',
180 __( 'Display conditions ( Taxonomy )', 'smart-custom-fields' ),
181 array( $this, 'display_meta_box_condition_taxonomy' ),
182 SCF_Config::NAME,
183 'side'
184 );
185 add_meta_box(
186 SCF_Config::PREFIX . 'meta-box-condition-options-page',
187 __( 'Display conditions ( Options page )', 'smart-custom-fields' ),
188 array( $this, 'display_meta_box_condition_options_page' ),
189 SCF_Config::NAME,
190 'side'
191 );
192 }
193
194 /**
195 * Displaying "hide" if $key isn't empty.
196 *
197 * @param string $key Key.
198 */
199 private function add_hide_class( $key ) {
200 if ( ! $key ) {
201 echo 'hide';
202 }
203 }
204
205 /**
206 * Displaying custom fields.
207 */
208 public function display_meta_box() {
209 $setting = SCF::add_setting( get_the_ID(), get_the_title() );
210 $setting->add_group_unshift();
211 $groups = $setting->get_groups();
212 ?>
213 <div class="<?php echo esc_attr( SCF_Config::PREFIX . 'fields-wrapper' ); ?>">
214 <div class="<?php echo esc_attr( SCF_Config::PREFIX . 'groups' ); ?>">
215 <?php foreach ( $groups as $group_key => $group ) : ?>
216 <?php
217 $fields = $group->get_fields();
218 array_unshift( $fields, SCF::get_form_field_instance( 'text' ) );
219 ?>
220 <div class="<?php echo esc_attr( SCF_Config::PREFIX . 'group' ); ?> <?php $this->add_hide_class( $group_key ); ?>">
221 <div class="btn-remove-group"><span class="dashicons dashicons-no-alt"></span></div>
222 <?php $group->display_options( $group_key ); ?>
223
224 <div class="<?php echo esc_attr( SCF_Config::PREFIX . 'fields' ); ?>">
225 <?php foreach ( $fields as $field_key => $field ) : ?>
226 <div class="<?php echo esc_attr( SCF_Config::PREFIX . 'field' ); ?> <?php $this->add_hide_class( $field_key ); ?>">
227 <?php
228 $field_name = $field->get( 'name' );
229 $field_label = $field->get( 'label' );
230 if ( ! $field_label ) {
231 $field_label = $field_name;
232 if ( ! $field_label ) {
233 $field_label = '&nbsp;';
234 }
235 }
236 ?>
237 <div class="<?php echo esc_attr( SCF_Config::PREFIX . 'icon-handle' ); ?>"></div>
238 <b class="btn-remove-field"><span class="dashicons dashicons-no-alt"></span></b>
239 <div class="field-label">
240 <?php echo esc_html( $field_label ); ?>
241 <?php if ( $field_name ) : ?>
242 <small>[ <?php echo esc_html( $field_name ); ?> ]</small>
243 <?php endif; ?>
244 </div>
245 <table class="<?php $this->add_hide_class( ! $field->get( 'name' ) ); ?>">
246 <tr>
247 <th><?php esc_html_e( 'Type', 'smart-custom-fields' ); ?><span class="<?php echo esc_attr( SCF_Config::PREFIX . 'require' ); ?>">*</span></th>
248 <td>
249 <select
250 name="<?php echo esc_attr( $field->get_field_name_in_setting( $group_key, $field_key, 'type' ) ); ?>"
251 class="<?php echo esc_attr( SCF_Config::PREFIX . 'field-select' ); ?>" />
252 <?php
253 foreach ( $this->optgroups as $optgroup_name => $optgroup_values ) {
254 $optgroup_fields = array();
255 $optgroup_values['options'] = apply_filters(
256 SCF_Config::PREFIX . 'field-select-' . $optgroup_name,
257 $optgroup_values['options']
258 );
259 foreach ( $optgroup_values['options'] as $option_key => $option ) {
260 $optgroup_fields[] = sprintf(
261 '<option value="%s" %s>%s</option>',
262 esc_attr( $option_key ),
263 selected( $field->get_attribute( 'type' ), $option_key, false ),
264 esc_html( $option )
265 );
266 }
267 printf(
268 '<optgroup label="%s">%s</optgroup>',
269 esc_attr( $optgroup_values['label'] ),
270 implode( '', $optgroup_fields ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
271 );
272 }
273 ?>
274 </select>
275 </td>
276 </tr>
277 <?php $field->display_options( $group_key, $field_key ); ?>
278 </table>
279 </div>
280 <?php endforeach; ?>
281 </div>
282 <div class="button btn-add-field <?php $this->add_hide_class( $group->is_repeatable() ); ?>"><?php esc_html_e( 'Add Sub field', 'smart-custom-fields' ); ?></div>
283 </div>
284 <?php endforeach; ?>
285 </div>
286 <div class="button button-primary btn-add-group"><?php esc_html_e( 'Add Field', 'smart-custom-fields' ); ?></div>
287 </div>
288 <?php wp_nonce_field( SCF_Config::NAME . '-settings', SCF_Config::PREFIX . 'settings-nonce' ); ?>
289 <?php
290 }
291
292 /**
293 * Displaying the meta box to set the display conditions for post edit page.
294 */
295 public function display_meta_box_condition_post() {
296 $post_types = get_post_types(
297 array(
298 'show_ui' => true,
299 ),
300 'objects'
301 );
302 unset( $post_types['attachment'] );
303 unset( $post_types['wp_block'] );
304 unset( $post_types[ SCF_Config::NAME ] );
305
306 $conditions = get_post_meta( get_the_ID(), SCF_Config::PREFIX . 'condition', true );
307 $post_type_field = '';
308 foreach ( $post_types as $post_type => $post_type_object ) {
309 $current = is_array( $conditions ) && in_array( $post_type, $conditions, true ) ? $post_type : false;
310 $post_type_field .= sprintf(
311 '<label><input type="checkbox" name="%s" value="%s" %s /> %s</label>',
312 esc_attr( SCF_Config::PREFIX . 'condition[]' ),
313 esc_attr( $post_type ),
314 checked( $current, $post_type, false ),
315 esc_attr( $post_type_object->labels->singular_name )
316 );
317 }
318 printf(
319 // Translators: %1$s: Label, %2$s: Post type field
320 '<p><b>%1$s</b>%2$s</p>',
321 esc_html__( 'Post Types', 'smart-custom-fields' ),
322 $post_type_field // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
323 );
324
325 $condition_post_ids = get_post_meta( get_the_ID(), SCF_Config::PREFIX . 'condition-post-ids', true );
326
327 // get all posts saved
328 $saved_posts = explode( ',', $condition_post_ids );
329
330 if ( $saved_posts ) {
331 $saved = array();
332
333 foreach ( $saved_posts as $k => $post_id ) {
334 if ( '' !== $post_id ) {
335 $saved[ $k ]['id'] = $post_id;
336 $saved[ $k ]['text'] = $post_id;
337 }
338 }
339 }
340
341 // create variable js with posting IDs to use in post search results
342 printf(
343 '<script type="text/javascript">smart_cf_saved_posts = %s;</script>',
344 wp_json_encode( array_values( $saved ) )
345 );
346
347 // create div to use with jquery plugin "selectivity"
348 // https://github.com/arendjr/selectivity
349 printf(
350 '<p><b>%s</b><div id="%s" class="selectivity-input"></div></p>',
351 esc_html__( 'Post or Page Ids', 'smart-custom-fields' ),
352 esc_attr( SCF_Config::PREFIX . 'autocomplete-condition-post' )
353 );
354
355 // create input hidden with the IDS of saved posts
356 printf(
357 // Translators: %1$s: name, %2$s: value
358 '<input type="hidden" name="%1$s" value="%2$s"/>',
359 esc_attr( SCF_Config::PREFIX . 'condition-post-ids' ),
360 esc_attr( $condition_post_ids )
361 );
362 }
363
364 /**
365 * Displaying the meta box to set the display conditions for profile edit page.
366 */
367 public function display_meta_box_condition_profile() {
368 $roles = get_editable_roles();
369 $conditions = get_post_meta( get_the_ID(), SCF_Config::PREFIX . 'roles', true );
370 $profile_field = '';
371 foreach ( $roles as $name => $role ) {
372 $current = is_array( $conditions ) && in_array( $name, $conditions, true ) ? $name : false;
373 $profile_field .= sprintf(
374 '<label><input type="checkbox" name="%s" value="%s" %s /> %s</label>',
375 esc_attr( SCF_Config::PREFIX . 'roles[]' ),
376 esc_attr( $name ),
377 checked( $current, $name, false ),
378 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
379 esc_html__( $role['name'], 'smart-custom-fields' )
380 );
381 }
382 printf(
383 // Translators: %1$s: Label, %2$s: Roles field
384 '<p><b>%1$s</b>%2$s</p>',
385 esc_html__( 'Roles', 'smart-custom-fields' ),
386 $profile_field // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
387 );
388 }
389
390 /**
391 * Displaying the meta box to set the display conditions for term edit page.
392 */
393 public function display_meta_box_condition_taxonomy() {
394 $taxonomies = get_taxonomies(
395 array(
396 'show_ui' => true,
397 ),
398 'objects'
399 );
400 $conditions = get_post_meta( get_the_ID(), SCF_Config::PREFIX . 'taxonomies', true );
401 $taxonomy_field = '';
402 foreach ( $taxonomies as $name => $taxonomy ) {
403 $current = is_array( $conditions ) && in_array( $name, $conditions, true ) ? $name : false;
404 $taxonomy_field .= sprintf(
405 '<label><input type="checkbox" name="%s" value="%s" %s /> %s</label>',
406 esc_attr( SCF_Config::PREFIX . 'taxonomies[]' ),
407 esc_attr( $name ),
408 checked( $current, $name, false ),
409 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
410 esc_html__( $taxonomy->label, 'smart-custom-fields' )
411 );
412 }
413 printf(
414 // Translators: %1$s: Label, %2$s: Taxonomy field
415 '<p><b>%1$s</b>%2$s</p>',
416 esc_html__( 'Taxonomies', 'smart-custom-fields' ),
417 $taxonomy_field // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
418 );
419 }
420
421 /**
422 * Displaying the meta box to set the display conditions for custom options page.
423 */
424 public function display_meta_box_condition_options_page() {
425 $optinos_pages = SCF::get_options_pages();
426 $conditions = get_post_meta( get_the_ID(), SCF_Config::PREFIX . 'options-pages', true );
427 $options_page_field = '';
428 foreach ( $optinos_pages as $name => $optinos_page ) {
429 $current = is_array( $conditions ) && in_array( $name, $conditions, true ) ? $name : false;
430 $options_page_field .= sprintf(
431 '<label><input type="checkbox" name="%s" value="%s" %s /> %s</label>',
432 esc_attr( SCF_Config::PREFIX . 'options-pages[]' ),
433 esc_attr( $name ),
434 checked( $current, $name, false ),
435 esc_html( $optinos_page )
436 );
437 }
438 printf(
439 // Translators: %1$s: Label, %2$s: Options page field
440 '<p><b>%1$s</b>%2$s</p>',
441 esc_html__( 'Options pages', 'smart-custom-fields' ),
442 $options_page_field // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
443 );
444 }
445
446 /**
447 * Saving settings.
448 *
449 * @param int $post_id The post id.
450 */
451 public function save_post( $post_id ) {
452 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
453 return;
454 }
455
456 $posted_data = filter_input( INPUT_POST, SCF_Config::NAME, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
457 if ( ! $posted_data ) {
458 return;
459 }
460
461 check_admin_referer(
462 SCF_Config::NAME . '-settings',
463 SCF_Config::PREFIX . 'settings-nonce'
464 );
465
466 $data = array();
467 foreach ( $posted_data as $group_key => $group_value ) {
468 // $group_key = 0 is hidden field so don't save
469 if ( 0 === $group_key ) {
470 continue;
471 }
472 if ( ! empty( $group_value['fields'] ) && count( $group_value['fields'] ) > 1 ) {
473 $fields = array();
474 foreach ( $group_value['fields'] as $field_key => $field_value ) {
475 // $field_key = 0 is hidden field so don't save
476 if ( 0 === $field_key ) {
477 continue;
478 }
479 if ( ! empty( $field_value['name'] ) ) {
480 $fields[] = $field_value;
481 }
482 }
483 if ( ! $fields ) {
484 continue;
485 }
486
487 if ( ! empty( $group_value['repeat'] ) && 'true' === $group_value['repeat'] ) {
488 $group_value['repeat'] = true;
489 } else {
490 $group_value['repeat'] = false;
491 }
492
493 // If "repeat" isn't true, empty name
494 // If "repeat" is true and name is empty, assign index
495 if ( ! isset( $group_value['repeat'] ) || true !== $group_value['repeat'] || empty( $group_value['group-name'] ) ) {
496 $group_value['group-name'] = $group_key;
497 }
498
499 $group_value['fields'] = $fields;
500 $data[] = $group_value;
501 }
502 }
503 update_post_meta( $post_id, SCF_Config::PREFIX . 'setting', $data );
504
505 $poste_condition = filter_input( INPUT_POST, SCF_Config::PREFIX . 'condition', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
506 if ( ! $poste_condition ) {
507 delete_post_meta( $post_id, SCF_Config::PREFIX . 'condition' );
508 } else {
509 update_post_meta( $post_id, SCF_Config::PREFIX . 'condition', $poste_condition );
510 }
511
512 $poste_condition_post_ids = filter_input( INPUT_POST, SCF_Config::PREFIX . 'condition-post-ids' );
513 if ( ! $poste_condition_post_ids ) {
514 delete_post_meta( $post_id, SCF_Config::PREFIX . 'condition-post-ids' );
515 } else {
516 update_post_meta( $post_id, SCF_Config::PREFIX . 'condition-post-ids', $poste_condition_post_ids );
517 }
518
519 $poste_roles = filter_input( INPUT_POST, SCF_Config::PREFIX . 'roles', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
520 if ( ! $poste_roles ) {
521 delete_post_meta( $post_id, SCF_Config::PREFIX . 'roles' );
522 } else {
523 update_post_meta( $post_id, SCF_Config::PREFIX . 'roles', $poste_roles );
524 }
525
526 $poste_taxonomies = filter_input( INPUT_POST, SCF_Config::PREFIX . 'taxonomies', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
527 if ( ! $poste_taxonomies ) {
528 delete_post_meta( $post_id, SCF_Config::PREFIX . 'taxonomies' );
529 } else {
530 update_post_meta( $post_id, SCF_Config::PREFIX . 'taxonomies', $poste_taxonomies );
531 }
532
533 $poste_options_pages = filter_input( INPUT_POST, SCF_Config::PREFIX . 'options-pages', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
534 if ( ! $poste_options_pages ) {
535 delete_post_meta( $post_id, SCF_Config::PREFIX . 'options-pages' );
536 } else {
537 update_post_meta( $post_id, SCF_Config::PREFIX . 'options-pages', $poste_options_pages );
538 }
539 }
540 }
541