PluginProbe
Customify / 2.10.7
Customify v2.10.7
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / includes / class-customify-settings.php

class-customify-settings.php in Customify 2.10.7, at includes/class-customify-settings.php

490 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This is the class that handles the plugin settings page.
4 *
5 * @see https://pixelgrade.com
6 * @author Pixelgrade
7 * @since 2.4.0
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly
12 }
13
14 class Customify_Settings {
15
16 /**
17 * Instance of this class.
18 * @var object
19 */
20 protected static $_instance = null;
21
22 /**
23 * Slug of the plugin screen.
24 * @var string
25 */
26 protected $plugin_screen_hook_suffix = null;
27
28 public $plugin_settings;
29
30 /**
31 * The main plugin file.
32 * @var string
33 * @access public
34 */
35 public $file;
36
37 public $slug;
38 public $version;
39
40 private $plugin_config = null;
41
42 protected function __construct( $file, $slug, $version = '1.0.0' ) {
43 $this->file = $file;
44 $this->slug = $slug;
45 $this->version = $version;
46
47 require plugin_dir_path( $this->file ) . 'includes/admin-settings/core/bootstrap.php';
48
49 // Load the plugin's settings from the DB.
50 // We use the settings key directly to avoid loading the full config (which has translation calls) too early.
51 $this->plugin_settings = get_option( 'pixcustomify_settings' );
52
53 // Register all the needed hooks
54 $this->register_hooks();
55 }
56
57 /**
58 * Get the plugin config, loading it on first access.
59 *
60 * This lazy-loads the config to avoid calling translation functions before the textdomain is loaded.
61 *
62 * @return array
63 */
64 private function get_lazy_plugin_config() {
65 if ( null === $this->plugin_config ) {
66 $this->plugin_config = self::get_plugin_config();
67 }
68
69 return $this->plugin_config;
70 }
71
72 /**
73 * Register our actions and filters
74 */
75 function register_hooks() {
76
77 // Starting with the menu item for this plugin
78 add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) );
79
80 // Add an action link pointing to the options page.
81 add_filter( 'plugin_action_links_' . plugin_basename( $this->file ), array( $this, 'add_action_links' ) );
82
83 // Load admin stylesheet and JavaScript.
84 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
85 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
86
87
88 add_action( 'rest_api_init', array( $this, 'add_rest_routes_api' ) );
89 }
90
91 /**
92 * Register the administration menu for this plugin into the WordPress Dashboard menu.
93 */
94 function add_plugin_admin_menu() {
95 $this->plugin_screen_hook_suffix = add_options_page(
96 esc_html__( 'Customify', 'customify' ),
97 esc_html__( 'Customify', 'customify' ),
98 'manage_options',
99 $this->slug,
100 array( $this, 'display_plugin_admin_page' )
101 );
102 }
103
104 /**
105 * Render the settings page for this plugin.
106 */
107 function display_plugin_admin_page() {
108 // Check the nonce, in case the form was submitted.
109 if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
110 check_admin_referer( 'customify_settings_save', '_wpnonce-customify-settings' );
111 }
112
113 $config = Customify_Settings::get_plugin_config();
114
115 // Invoke the processor.
116 /**
117 * @var PixCustomifyProcessorImpl $processor
118 */
119 $processor = pixcustomify::processor( $config );
120 $status = $processor->status();
121 $errors = $processor->errors();
122
123 // Do the saving and display the form.
124 include_once plugin_dir_path( $this->file ) . 'includes/admin-settings/views/admin.php';
125 }
126
127 /**
128 * Settings page styles
129 */
130 function enqueue_admin_styles() {
131
132 if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
133 return;
134 }
135
136 $screen = get_current_screen();
137 if ( $screen->id === $this->plugin_screen_hook_suffix ) {
138 $rtl_suffix = is_rtl() ? '-rtl' : '';
139 wp_enqueue_style( $this->slug . '-admin-styles', plugins_url( 'css/admin' . $rtl_suffix . '.css', $this->file ), array(), $this->version );
140 }
141 }
142
143 /**
144 * Settings page scripts
145 */
146 function enqueue_admin_scripts() {
147
148 if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
149 return;
150 }
151
152 $screen = get_current_screen();
153 if ( $screen->id == $this->plugin_screen_hook_suffix ) {
154 $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
155
156 wp_enqueue_script( $this->slug . '-settings-page-script',
157 plugins_url( 'js/settings-page' . $suffix . '.js', $this->file ),
158 array( 'jquery' ), $this->version );
159
160 wp_add_inline_script( $this->slug . '-settings-page-script',
161 PixCustomify_Customizer::getlocalizeToWindowScript( 'customify',
162 array(
163 'config' => array(
164 'ajax_url' => admin_url( 'admin-ajax.php' ),
165 'wp_rest' => array(
166 'root' => esc_url_raw( rest_url() ),
167 'nonce' => wp_create_nonce( 'wp_rest' ),
168 'customify_settings_nonce' => wp_create_nonce( 'customify_settings_nonce' )
169 ),
170 )
171 )
172 ) );
173 }
174
175 wp_localize_script( $this->slug . '-customizer-scripts', 'WP_API_Settings', array(
176 'root' => esc_url_raw( rest_url() ),
177 'nonce' => wp_create_nonce( 'wp_rest' )
178 ) );
179 }
180
181 /**
182 * Add settings action link to the plugins page.
183 */
184 public function add_action_links( $links ) {
185 return array_merge( array( 'settings' => '<a href="' . esc_url( menu_page_url( $this->slug, false ) ) . '">' . esc_html__( 'Settings', 'customify' ) . '</a>' ), $links );
186 }
187
188 public function add_rest_routes_api() {
189 register_rest_route( 'customify/v1', '/delete_theme_mod', array(
190 'methods' => 'POST',
191 'callback' => array( $this, 'delete_theme_mod' ),
192 'permission_callback' => array( $this, 'permission_nonce_callback' ),
193 ) );
194 }
195
196 public function delete_theme_mod() {
197 if ( ! current_user_can( 'manage_options' ) ) {
198 wp_send_json_error( esc_html__('You don\'t have admin privileges.', 'customify' ) );
199 }
200
201 $key = PixCustomifyPlugin()->get_options_key();
202
203 if ( empty( $key ) ) {
204 wp_send_json_error('no option key');
205 }
206
207 remove_theme_mod( $key );
208
209 PixCustomifyPlugin()->invalidate_all_caches();
210
211 wp_send_json_success('Deleted ' . $key . ' theme mod!');
212 }
213
214 public function permission_nonce_callback() {
215 return wp_verify_nonce( $this->get_nonce(), 'customify_settings_nonce' );
216 }
217
218 private function get_nonce() {
219 $nonce = null;
220
221 if ( isset( $_REQUEST['customify_settings_nonce'] ) ) {
222 $nonce = wp_unslash( $_REQUEST['customify_settings_nonce'] );
223 } elseif ( isset( $_POST['customify_settings_nonce'] ) ) {
224 $nonce = wp_unslash( $_POST['customify_settings_nonce'] );
225 }
226
227 return $nonce;
228 }
229
230 static public function get_plugin_config() {
231
232 $debug = false;
233 if ( isset( $_GET['debug'] ) && $_GET['debug'] === 'true' ) {
234 $debug = true;
235 }
236
237 return array(
238 'plugin-name' => 'pixcustomify',
239 'settings-key' => 'pixcustomify_settings',
240 'textdomain' => 'customify',
241 'template-paths' => array(
242 plugin_dir_path( __FILE__ ) . 'admin-settings/core/views/form-partials/',
243 plugin_dir_path( __FILE__ ) . 'admin-settings/views/form-partials/',
244 ),
245 'fields' => array(
246 'hiddens' => array(
247 'type' => 'group',
248 'options' => array(
249 'settings_saved_once' => array(
250 'default' => '0',
251 'value' => '1',
252 'type' => 'hidden',
253 ),
254 ),
255 ),
256 'general' => array(
257 'type' => 'postbox',
258 'label' => esc_html__( 'General Settings', 'customify' ),
259 'options' => array(
260 'values_store_mod' => array(
261 'name' => 'values_store_mod',
262 'label' => esc_html__( 'Store values as:', 'customify' ),
263 'desc' => esc_html__( 'You can store the values globally so you can use them with other themes or store them as a "theme_mod" which will make an individual set of options only for the current theme', 'customify' ),
264 'default' => 'theme_mod',
265 'type' => 'select',
266 'options' => array(
267 'option' => esc_html__( 'Option (global options)', 'customify' ),
268 'theme_mod' => esc_html__( 'Theme Mod (per theme options)', 'customify' ),
269 ),
270 ),
271
272 'disable_default_sections' => array(
273 'name' => 'disable_default_sections',
274 'label' => esc_html__( 'Disable default sections', 'customify' ),
275 'desc' => esc_html__( 'You can disable default sections', 'customify' ),
276 'type' => 'multicheckbox',
277 'options' => array(
278 'nav' => esc_html__( 'Navigation', 'customify' ),
279 'static_front_page' => esc_html__( 'Front Page', 'customify' ),
280 'title_tagline' => esc_html__( 'Title', 'customify' ),
281 'colors' => esc_html__( 'Colors', 'customify' ),
282 'background_image' => esc_html__( 'Background', 'customify' ),
283 'header_image' => esc_html__( 'Header', 'customify' ),
284 'widgets' => esc_html__( 'Widgets', 'customify' ),
285 ),
286 ),
287
288 'enable_reset_buttons' => array(
289 'name' => 'enable_reset_buttons',
290 'label' => esc_html__( 'Enable Reset Buttons', 'customify' ),
291 'desc' => esc_html__( 'You can enable "Reset to defaults" buttons for panels / sections or all settings. We have disabled this feature by default to avoid accidental resets. If you are sure that you need it please enable this.', 'customify' ),
292 'default' => false,
293 'type' => 'switch',
294 ),
295
296 'enable_editor_style' => array(
297 'name' => 'enable_editor_style',
298 'label' => esc_html__( 'Enable Editor Style', 'customify' ),
299 'desc' => esc_html__( 'The styling added by Customify in front-end can be added in the WordPress editor too by enabling this option', 'customify' ),
300 'default' => true,
301 'type' => 'switch',
302 ),
303 ),
304 ),
305 'output' => array(
306 'type' => 'postbox',
307 'label' => esc_html__( 'Output Settings', 'customify' ),
308 'options' => array(
309 'style_resources_location' => array(
310 'name' => 'style_resources_location',
311 'label' => esc_html__( 'Styles location:', 'customify' ),
312 'desc' => esc_html__( 'Here you can decide where to put your style output, in header or footer', 'customify' ),
313 'default' => 'wp_footer',
314 'type' => 'select',
315 'options' => array(
316 'wp_head' => esc_html__( 'In header (just before the head tag)', 'customify' ),
317 'wp_footer' => esc_html__( 'Footer (just before the end of the body tag)', 'customify' ),
318 ),
319 ),
320 ),
321 ),
322 'typography' => array(
323 'type' => 'postbox',
324 'label' => esc_html__( 'Typography Settings', 'customify' ),
325 'options' => array(
326 'typography' => array(
327 'label' => esc_html__( 'Enable Typography Options', 'customify' ),
328 'default' => true,
329 'type' => 'switch',
330 'show_group' => 'typography_group',
331 'display_option' => true,
332 ),
333
334 'typography_group' => array(
335 'type' => 'group',
336 'options' => array(
337 'typography_system_fonts' => array(
338 'name' => 'typography_system_fonts',
339 'label' => esc_html__( 'Use system fonts', 'customify' ),
340 'desc' => esc_html__( 'Would you like to have system fonts available in the font controls?', 'customify' ),
341 'default' => true,
342 'type' => 'switch',
343 ),
344 'typography_google_fonts' => array(
345 'name' => 'typography_google_fonts',
346 'label' => esc_html__( 'Use Google fonts:', 'customify' ),
347 'desc' => esc_html__( 'Would you like to have Google fonts available in the font controls?', 'customify' ),
348 'default' => true,
349 'type' => 'switch',
350 'show_group' => 'typography_google_fonts_group',
351 'display_option' => true,
352 ),
353 'typography_google_fonts_group' => array(
354 'type' => 'group',
355 'options' => array(
356 'typography_group_google_fonts' => array(
357 'name' => 'typography_group_google_fonts',
358 'label' => esc_html__( 'Group Google fonts:', 'customify' ),
359 'desc' => esc_html__( 'You can chose to see the Google fonts in groups', 'customify' ),
360 'default' => true,
361 'type' => 'switch',
362 ),
363 ),
364 ),
365 'typography_cloud_fonts' => array(
366 'name' => 'typography_cloud_fonts',
367 'label' => esc_html__( 'Use cloud fonts', 'customify' ),
368 'desc' => esc_html__( 'Would you to have Cloud fonts available in the font controls?', 'customify' ),
369 'default' => true,
370 'type' => 'switch',
371 'display_option' => true,
372 ),
373 ),
374 ),
375 ),
376 ),
377 'tools' => array(
378 'type' => 'postbox',
379 'label' => esc_html__( 'Tools', 'customify' ),
380 'options' => array(
381 'reset_theme_mod' => array(
382 'name' => 'reset_theme_mod',
383 'label' => esc_html__( 'Reset', 'customify' ),
384 'type' => 'reset_theme_mod',
385 ),
386 ),
387 ),
388 ),
389 'callbacks' => array(
390 'invalidate_caches' => 'pixcustomify_cache_invalidate_cache',
391 ),
392 'processor' => array(
393 // callback signature: (array $input, customifyProcessor $processor)
394 'preupdate' => array(
395 // callbacks to run before update process
396 // cleanup and validation has been performed on data
397 ),
398 'postupdate' => array(
399 'invalidate_caches'
400 ),
401 ),
402 'cleanup' => array(
403 'switch' => array( 'switch_not_available' ),
404 ),
405 'checks' => array(
406 'counter' => array( 'is_numeric', 'not_empty' ),
407 ),
408 'errors' => array(
409 'not_empty' => __( 'Invalid Value.', 'customify' ),
410 ),
411 // shows exception traces on error
412 'debug' => $debug,
413
414 ); # config
415 }
416
417 /**
418 * Get an option's value from the config file
419 *
420 * @param $option
421 * @param null $default
422 *
423 * @return bool|null
424 */
425 public function get_config_option( $option, $default = null ) {
426 $config = $this->get_lazy_plugin_config();
427
428 if ( isset( $config[ $option ] ) ) {
429 return $config[ $option ];
430 } elseif ( $default !== null ) {
431 return $default;
432 }
433
434 return false;
435 }
436
437 public function get_plugin_setting( $option, $default = null ) {
438
439 if ( isset( $this->plugin_settings[ $option ] ) ) {
440 return $this->plugin_settings[ $option ];
441 } elseif ( $default !== null ) {
442 return $default;
443 }
444
445 return false;
446 }
447
448 /**
449 * Main Customify_Settings Instance
450 *
451 * Ensures only one instance of Customify_Settings is loaded or can be loaded.
452 *
453 * @static
454 *
455 * @param string $file File.
456 * @param string $slug Plugin slug.
457 * @param string $version Optional.
458 *
459 * @return Customify_Settings Main Customify_Settings instance
460 */
461 public static function instance( $file, $slug, $version = '1.0.0' ) {
462 // If the single instance hasn't been set, set it now.
463 if ( is_null( self::$_instance ) ) {
464 self::$_instance = new self( $file, $slug, $version );
465 }
466
467 return self::$_instance;
468 }
469
470 /**
471 * Cloning is forbidden.
472 *
473 * @since 1.5.0
474 */
475 public function __clone() {
476
477 _doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null );
478 }
479
480 /**
481 * Unserializing instances of this class is forbidden.
482 *
483 * @since 1.5.0
484 */
485 public function __wakeup() {
486
487 _doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null );
488 }
489 }
490