PluginProbe
Customify / 2.10.2
Customify v2.10.2
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.2, at includes/class-customify-settings.php

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