PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 3.1.6
Adminify – White Label, Admin Menu Editor, Login Customizer v3.1.6
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 / customize-options.class.php

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

271 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) {
2 die; } // Cannot access directly.
3 /**
4 *
5 * Customize Options Class
6 *
7 * @since 1.0.0
8 * @version 1.0.0
9 */
10 if ( ! class_exists( 'ADMINIFY_Customize_Options' ) ) {
11 class ADMINIFY_Customize_Options extends ADMINIFY_Abstract {
12
13 // constans
14 public $unique = '';
15 public $abstract = 'customize';
16 public $options = [];
17 public $sections = [];
18 public $pre_fields = [];
19 public $pre_tabs = [];
20 public $priority = 10;
21 public $args = [
22 'database' => 'option',
23 'transport' => 'refresh',
24 'capability' => 'manage_options',
25 'save_defaults' => true,
26 'enqueue_webfont' => true,
27 'async_webfont' => false,
28 'output_css' => true,
29 'defaults' => [],
30 ];
31
32 // run customize construct
33 public function __construct( $key, $params ) {
34 $this->unique = $key;
35 $this->args = apply_filters( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
36 $this->sections = apply_filters( "adminify_{$this->unique}_sections", $params['sections'], $this );
37 $this->pre_fields = $this->pre_fields( $this->sections );
38
39 $this->get_options();
40 $this->save_defaults();
41
42 add_action( 'customize_register', [ $this, 'add_customize_options' ] );
43 add_action( 'customize_save_after', [ $this, 'add_customize_save_after' ] );
44
45 // Get options for enqueue actions
46 if ( is_customize_preview() ) {
47 add_action( 'wp_enqueue_scripts', [ $this, 'get_options' ] );
48 }
49
50 // wp enqeueu for typography and output css
51 parent::__construct();
52 }
53
54 // instance
55 public static function instance( $key, $params = [] ) {
56 return new self( $key, $params );
57 }
58
59 public function add_customize_save_after( $wp_customize ) {
60 do_action( "adminify_{$this->unique}_save_before", $this->get_options(), $this, $wp_customize );
61 do_action( "adminify_{$this->unique}_saved", $this->get_options(), $this, $wp_customize );
62 do_action( "adminify_{$this->unique}_save_after", $this->get_options(), $this, $wp_customize );
63 }
64
65 // get default value
66 public function get_default( $field ) {
67 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
68 $default = ( isset( $this->args['defaults'][ $field['id'] ] ) ) ? $this->args['defaults'][ $field['id'] ] : $default;
69
70 return $default;
71 }
72
73 // get option
74 public function get_options() {
75 if ( $this->args['database'] === 'theme_mod' ) {
76 $this->options = get_theme_mod( $this->unique, [] );
77 } else {
78 $this->options = get_option( $this->unique, [] );
79 }
80
81 if ( empty( $this->options ) ) {
82 $this->options = [];
83 }
84
85 return $this->options;
86 }
87
88 // save defaults and set new fields value to main options
89 public function save_defaults() {
90 $tmp_options = $this->options;
91
92 if ( ! empty( $this->pre_fields ) ) {
93 foreach ( $this->pre_fields as $field ) {
94 if ( ! empty( $field['id'] ) ) {
95 $this->options[ $field['id'] ] = ( isset( $this->options[ $field['id'] ] ) ) ? $this->options[ $field['id'] ] : $this->get_default( $field );
96 }
97 }
98 }
99
100 if ( $this->args['save_defaults'] && empty( $this->args['show_in_customizer'] ) && empty( $tmp_options ) ) {
101 if ( $this->args['database'] === 'theme_mod' ) {
102 set_theme_mod( $this->unique, $this->options );
103 } else {
104 update_option( $this->unique, $this->options );
105 }
106 }
107 }
108
109 public function pre_fields( $sections ) {
110 $result = [];
111
112 foreach ( $sections as $key => $section ) {
113 if ( ! empty( $section['fields'] ) ) {
114 foreach ( $section['fields'] as $field ) {
115 $result[] = $field;
116 }
117 }
118 }
119
120 return $result;
121 }
122
123
124 public function pre_tabs( $sections ) {
125 $result = [];
126 $parents = [];
127
128 foreach ( $sections as $key => $section ) {
129 if ( ! empty( $section['parent'] ) ) {
130 $parents[ $section['parent'] ][] = $section;
131 unset( $sections[ $key ] );
132 }
133 }
134
135 foreach ( $sections as $key => $section ) {
136 if ( ! empty( $section['id'] ) && ! empty( $parents[ $section['id'] ] ) ) {
137 $section['subs'] = $parents[ $section['id'] ];
138 }
139 $result[] = $section;
140 }
141
142 return $result;
143 }
144
145 public function add_customize_options( $wp_customize ) {
146 if ( ! class_exists( 'WP_Customize_Panel_ADMINIFY' ) ) {
147 ADMINIFY::include_plugin_file( 'functions/customize.php' );
148 }
149
150 if ( ! empty( $this->sections ) ) {
151 $sections = $this->pre_tabs( $this->sections );
152
153 foreach ( $sections as $section ) {
154 if ( ! empty( $section['subs'] ) ) {
155 $panel_id = ( isset( $section['id'] ) ) ? $section['id'] : $this->unique . '-panel-' . $this->priority;
156
157 $wp_customize->add_panel(
158 new WP_Customize_Panel_ADMINIFY(
159 $wp_customize,
160 $panel_id,
161 [
162 'title' => ( isset( $section['title'] ) ) ? $section['title'] : null,
163 'description' => ( isset( $section['description'] ) ) ? $section['description'] : null,
164 'priority' => ( isset( $section['priority'] ) ) ? $section['priority'] : null,
165 ]
166 )
167 );
168
169 $this->priority++;
170
171 foreach ( $section['subs'] as $sub_section ) {
172 $section_id = ( isset( $sub_section['id'] ) ) ? $sub_section['id'] : $this->unique . '-section-' . $this->priority;
173
174 $this->add_section( $wp_customize, $section_id, $sub_section, $panel_id );
175
176 $this->priority++;
177 }
178 } else {
179 $section_id = ( isset( $section['id'] ) ) ? $section['id'] : $this->unique . '-section-' . $this->priority;
180
181 $this->add_section( $wp_customize, $section_id, $section, false );
182
183 $this->priority++;
184 }
185 }
186 }
187 }
188
189 // add customize section
190 public function add_section( $wp_customize, $section_id, $section_args, $panel_id ) {
191 if ( ! empty( $section_args['assign'] ) ) {
192 $section_id = $section_args['assign'];
193 } else {
194 $wp_customize->add_section(
195 new WP_Customize_Section_ADMINIFY(
196 $wp_customize,
197 $section_id,
198 [
199 'title' => ( isset( $section_args['title'] ) ) ? $section_args['title'] : '',
200 'description' => ( isset( $section_args['description'] ) ) ? $section_args['description'] : '',
201 'priority' => ( isset( $section_args['priority'] ) ) ? $section_args['priority'] : '',
202 'panel' => ( $panel_id ) ? $panel_id : '',
203 ]
204 )
205 );
206 }
207
208 if ( ! empty( $section_args['fields'] ) ) {
209 $field_key = 1;
210
211 foreach ( $section_args['fields'] as $field ) {
212 if ( isset( $field['id'] ) ) {
213 $field['default'] = $this->get_default( $field );
214 }
215
216 $field_id = ( isset( $field['id'] ) ) ? $field['id'] : '_nonce-' . $section_id . '-' . $field_key;
217 $setting_args = ( isset( $field['setting_args'] ) ) ? $field['setting_args'] : [];
218 $control_args = ( isset( $field['control_args'] ) ) ? $field['control_args'] : [];
219 $field_transport = ( isset( $field['transport'] ) ) ? $field['transport'] : $this->args['transport'];
220 $field_sanitize = ( isset( $field['sanitize'] ) ) ? $field['sanitize'] : '';
221 $field_validate = ( isset( $field['validate'] ) ) ? $field['validate'] : '';
222 $field_default = ( isset( $field['default'] ) ) ? $field['default'] : '';
223 $field_customize = ( ( isset( $field['selectors'] ) || isset( $field['selector'] ) ) && ! isset( $field['transport'] ) ) ? true : false;
224 $has_selective = ( isset( $field['selective_refresh'] ) && isset( $wp_customize->selective_refresh ) ) ? true : false;
225
226 $setting_id = $this->unique . '[' . $field_id . ']';
227 $transport = ( $has_selective || $field_customize ) ? 'postMessage' : $field_transport;
228
229 $wp_customize->add_setting(
230 $setting_id,
231 wp_parse_args(
232 $setting_args,
233 [
234 'default' => $field_default,
235 'type' => $this->args['database'],
236 'capability' => $this->args['capability'],
237 'transport' => $transport,
238 'sanitize_callback' => $field_sanitize,
239 'validate_callback' => $field_validate,
240 ]
241 )
242 );
243
244 $wp_customize->add_control(
245 new WP_Customize_Control_ADMINIFY(
246 $wp_customize,
247 $setting_id,
248 wp_parse_args(
249 $control_args,
250 [
251 'unique' => $this->unique,
252 'field' => $field,
253 'section' => $section_id,
254 'settings' => $setting_id,
255 ]
256 )
257 )
258 );
259
260 if ( $has_selective ) {
261 $wp_customize->selective_refresh->add_partial( $setting_id, $field['selective_refresh'] );
262 }
263
264 $field_key++;
265 }
266 }
267 }
268
269 }
270 }
271