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 / customize-options.class.php

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

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