PluginProbe
Customify / 2.5.2
Customify v2.5.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.5.2, at includes/class-customify-settings.php

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