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