Last active
June 24, 2019 05:59
-
-
Save 1shiharat/af3d46b7b007afd0e891 to your computer and use it in GitHub Desktop.
WordPress Theme Customizer Setting json Import.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* class WP_Theme_Customizer_Import_Json | |
* | |
* method list: | |
* - __construct() | |
* - register_json() | |
* - set_capability() | |
* - register_customizer() | |
* - get_theme_name() | |
* - set_section() | |
* - set_setting() | |
* - set_control() | |
* - deploy_settings() | |
* - add_section() | |
* - add_setting() | |
* - add_control() | |
* | |
* Example : | |
* $wp_theme_customizer = new WP_Theme_Customizer_Import_Json( dirname(__FILE__) . 'theme-customizer-setting.json' ); | |
*/ | |
class WP_Theme_Customizer_Import_Json | |
{ | |
/** | |
* Theme Customizer Instance | |
* @var object | |
*/ | |
private $wp_customize; | |
/** | |
* theme slug name | |
* | |
* @var string | |
*/ | |
private $theme_name = ''; | |
/** | |
* editor capability | |
* | |
* @var string | |
*/ | |
private $capability = ''; | |
/** | |
* Set Json File Path | |
* | |
* @var string | |
*/ | |
private $json_path = ''; | |
/** | |
* set settings object | |
* | |
* @var objecct | |
*/ | |
private $settings; | |
/** | |
* construct | |
* | |
* @param string $json_path | |
* | |
* @return void | |
*/ | |
public function __construct( $json_path ) | |
{ | |
// json path | |
$this->json_path = $json_path; | |
// theme slug | |
$this->theme_name = $this->get_theme_name(); | |
// editor capability | |
$this->capability = $this->set_capability(); | |
// register json | |
$this->settings = $this->register_json( apply_filters( 'set_theme_customizer_json_file', $json_path ) ); | |
/** | |
* Register action hook | |
* | |
* @see wp-includes/class-wp-theme-customizer-manager.php | |
*/ | |
add_action( 'customize_register', array( $this, 'register_customizer' ) ); | |
} | |
/** | |
* Register Json File | |
* | |
* @param string $json_path | |
* @return object | |
*/ | |
public function register_json() | |
{ | |
$this->settings = json_decode( file_get_contents( $this->json_path ) ); | |
return $this->settings; | |
} | |
/** | |
* Set Theme Customizer Editor Capability | |
* | |
* @return string | |
*/ | |
private function set_capability() | |
{ | |
return $this->settings->setting->capability; | |
} | |
/** | |
* Register Theme Customizer | |
* | |
* @return void | |
*/ | |
public function register_customizer( $wp_customize ) | |
{ | |
$this->wp_customize = $wp_customize; | |
$this->theme_slug = 'theme_mods_' . $this->settings->setting->theme_slug; | |
foreach ( $this->settings->sections as $section_name => $section ) : | |
// register section | |
$this->add_section( $section_name, $section->title, $section->priority ); | |
foreach ( $section->setting as $setting_name => $settings ) : | |
// register setting | |
$this->add_setting( $setting_name, $settings ); | |
// register control | |
$this->add_control( $section_name, $setting_name, $settings ); | |
endforeach; | |
endforeach; | |
} | |
/** | |
* Enhanced Theme Slug | |
* | |
* @return string template name | |
*/ | |
private function get_theme_name() | |
{ | |
return get_template(); | |
} | |
/** | |
* Set Section Value | |
* | |
* @param string $title | |
* @param int $priority | |
* @return array | |
*/ | |
private function set_section( $title = '', $priority ) | |
{ | |
if ( ! $priority ) | |
$priority = 0; | |
return array( | |
'title' => $title, | |
'priority' => $priority, | |
); | |
} | |
/** | |
* Set Setting Value | |
* | |
* @param array $settings | |
* | |
* @return array setting argment | |
*/ | |
private function set_setting( $settings ) | |
{ | |
$setting_arg = array(); | |
$setting_arg['capability'] = $this->capability; | |
return $this->deploy_settings( $setting_arg, $settings, array( 'label', 'choices' ) ); | |
} | |
/** | |
* Set Control Value | |
* | |
* @param string $section_name | |
* @param object $settings | |
* | |
* @return array formated array value | |
*/ | |
public function set_control( $section_name, $setting_name, $settings ) | |
{ | |
$setting_arg = array(); | |
$setting_arg['section'] = $section_name; | |
$setting_arg['settings'] = $this->theme_slug . '['. $setting_name . ']'; | |
return $this->deploy_settings( $setting_arg, $settings, array( 'transport', 'capability', 'default', 'type' ) ); | |
} | |
/** | |
* Control & Setting Value formater | |
* | |
* @param object $setting_arg | |
* @param object $settings | |
* @param array $exclude_keys | |
* | |
* @return array / formated array value | |
*/ | |
private function deploy_settings( $setting_arg, $settings, $exclude_keys = array() ) | |
{ | |
foreach ( $settings as $setting_key => $setting ) { | |
if ( 'choices' == $setting_key ) { | |
foreach ( $setting as $choise_key => $choise ) { | |
$setting_arg['choices'][$choise_key] = $choise; | |
}; | |
} elseif ( in_array( $setting_key, $exclude_keys ) ) { | |
unset( $setting_arg[$setting_key] ); | |
continue; | |
} else { | |
$setting_arg[$setting_key] = $setting; | |
}; | |
}; | |
return $setting_arg; | |
} | |
/** | |
* Add Section to WP Theme Customizer | |
* | |
* @param string $section_name | |
* @param string $title | |
* @param integer $priority | |
* | |
* @return void | |
*/ | |
private function add_section( $section_name, $title, $priority = 0 ) | |
{ | |
$this->wp_customize->add_section( | |
// section_name | |
$section_name, | |
$this->set_section( | |
$title, | |
$priority | |
) | |
); | |
} | |
/** | |
* Add Setting to WP Theme Customizer | |
* | |
* @param string $setting_name | |
* @param object $settings | |
* | |
* @return void | |
*/ | |
private function add_setting( $setting_name, $settings ) | |
{ | |
$this->wp_customize-> | |
add_setting( | |
$this->theme_slug . '['. $setting_name . ']', | |
$this->set_setting( $settings ) | |
); | |
} | |
/** | |
* Add Control to WP Theme Customizer | |
* | |
* @param string $section_name | |
* @param string $setting_name | |
* @param object $settings | |
* | |
* @return void | |
*/ | |
private function add_control( $section_name, $setting_name, $settings ) | |
{ | |
switch ( $settings->type ) { | |
// type "color" | |
case 'color': | |
$settings->sanitize_callback = 'sanitize_hex_color'; | |
$this->wp_customize-> | |
add_control( | |
new WP_Customize_Color_Control( | |
$this->wp_customize, | |
$setting_name, | |
$this->set_control( $section_name, $setting_name, $settings ) | |
) | |
); | |
break; | |
// type "image" | |
case 'image': | |
$this->wp_customize-> | |
add_control( | |
new MultiImageControl( | |
$this->wp_customize, | |
$setting_name, | |
$this->set_control( $section_name, $setting_name, $settings ) | |
) | |
); | |
break; | |
default: | |
$this->wp_customize-> | |
add_control( | |
$setting_name, | |
$this->set_control( $section_name, $setting_name, $settings ) | |
); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment