| 1 |
<?php |
| 2 |
/** |
| 3 |
* Customizer block. |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Customizer_Block |
| 14 |
*/ |
| 15 |
class GhostKit_Customizer_Block { |
| 16 |
/** |
| 17 |
* Custom options. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
public $custom_options = array(); |
| 22 |
|
| 23 |
/** |
| 24 |
* GhostKit_Customizer_Block constructor. |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
add_action( 'template_redirect', array( $this, 'maybe_find_options' ) ); |
| 28 |
add_action( 'init', array( $this, 'register_meta' ) ); |
| 29 |
add_action( 'customize_register', array( $this, 'parse_customizer_controls' ), 99999 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Try to find meta data to replace options. |
| 34 |
*/ |
| 35 |
public function maybe_find_options() { |
| 36 |
$this->custom_options = json_decode( urldecode( get_post_meta( get_queried_object_id(), 'ghostkit_customizer_options', true ) ), true ); |
| 37 |
|
| 38 |
if ( $this->custom_options && ! empty( $this->custom_options ) ) { |
| 39 |
foreach ( $this->custom_options as $opt ) { |
| 40 |
add_filter( ( isset( $opt['type'] ) ? $opt['type'] : 'theme_mod' ) . '_' . $opt['id'], array( $this, 'replace_option' ) ); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Replace option value. |
| 47 |
* |
| 48 |
* @param mixed $val - current option value. |
| 49 |
* |
| 50 |
* @return mixed |
| 51 |
*/ |
| 52 |
public function replace_option( $val ) { |
| 53 |
if ( $this->custom_options && ! empty( $this->custom_options ) ) { |
| 54 |
$this_id = current_filter(); |
| 55 |
|
| 56 |
foreach ( $this->custom_options as $opt ) { |
| 57 |
$id = ( isset( $opt['type'] ) ? $opt['type'] : 'theme_mod' ) . '_' . $opt['id']; |
| 58 |
if ( $id === $this_id ) { |
| 59 |
$val = $opt['value']; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
return $val; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Register meta. |
| 68 |
*/ |
| 69 |
public function register_meta() { |
| 70 |
register_meta( 'post', 'ghostkit_customizer_options', array( |
| 71 |
'show_in_rest' => true, |
| 72 |
'single' => true, |
| 73 |
'type' => 'string', |
| 74 |
) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Prepare customizer data and save it to the options for further use. |
| 79 |
* |
| 80 |
* @param {object} $wp_customize - customizer data. |
| 81 |
*/ |
| 82 |
public function parse_customizer_controls( $wp_customize ) { |
| 83 |
$settings = array(); |
| 84 |
foreach ( $wp_customize->settings() as $setting ) { |
| 85 |
if ( isset( $setting->type ) && ( 'theme_mod' === $setting->type || 'option' === $setting->type ) ) { |
| 86 |
$control = $wp_customize->get_control( $setting->id ); |
| 87 |
|
| 88 |
// get section and panel. |
| 89 |
$section = false; |
| 90 |
$panel = false; |
| 91 |
if ( isset( $control->section ) ) { |
| 92 |
$section_object = $wp_customize->get_section( $control->section ); |
| 93 |
if ( $section_object ) { |
| 94 |
$section = array( |
| 95 |
'id' => isset( $section_object->id ) ? $section_object->id : false, |
| 96 |
'title' => isset( $section_object->title ) ? $section_object->title : false, |
| 97 |
'description' => isset( $section_object->description ) ? $section_object->description : false, |
| 98 |
); |
| 99 |
if ( isset( $section_object->panel ) ) { |
| 100 |
$panel_object = $wp_customize->get_panel( $section_object->panel ); |
| 101 |
if ( $panel_object ) { |
| 102 |
$panel = array( |
| 103 |
'id' => isset( $panel_object->id ) ? $panel_object->id : false, |
| 104 |
'title' => isset( $panel_object->title ) ? $panel_object->title : false, |
| 105 |
'description' => isset( $panel_object->description ) ? $panel_object->description : false, |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// get control data. |
| 113 |
$settings[] = array( |
| 114 |
'id' => $setting->id, |
| 115 |
'type' => $setting->type, |
| 116 |
'default' => $setting->default, |
| 117 |
'section' => $section, |
| 118 |
'panel' => $panel, |
| 119 |
'label' => isset( $control->label ) ? $control->label : false, |
| 120 |
'description' => isset( $control->description ) ? $control->description : false, |
| 121 |
'choices' => isset( $control->choices ) ? $control->choices : false, |
| 122 |
'control_type' => isset( $control->type ) ? $control->type : false, |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
update_option( 'ghostkit_customizer_fields', $settings ); |
| 128 |
} |
| 129 |
} |
| 130 |
new GhostKit_Customizer_Block(); |
| 131 |
|