forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-theme-customizer-section.php
161 lines (131 loc) · 5.01 KB
/
class-theme-customizer-section.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFrameworkThemeCustomizerSection {
private $defaultSettings = array(
'name' => '', // Name of the menu item
// 'parent' => null, // slug of parent, if blank, then this is a top level menu
'id' => '', // Unique ID of the menu item
'panel' => '', // The Name of the panel to create
'panel_id' => '', // The panel ID to create / add to. If this is blank & `panel` is given, this will be generated
'capability' => 'edit_theme_options', // User role
// 'icon' => 'dashicons-admin-generic', // Menu icon for top level menus only
'desc' => '', // Description
'position' => 30 // Menu position for top level menus only
);
public $settings;
public $options = array();
public $owner;
// Makes sure we only load live previewing CSS only once
private static $generatedHeadCSSPreview = false;
function __construct( $settings, $owner ) {
$this->owner = $owner;
$this->settings = array_merge( $this->defaultSettings, $settings );
if ( empty( $this->settings['name'] ) ) {
$this->settings['name'] = __( "More Options", TF_I18NDOMAIN );
}
if ( empty( $this->settings['id'] ) ) {
$this->settings['id'] = str_replace( ' ', '-', trim( strtolower( $this->settings['name'] ) ) );
}
if ( empty( $this->settings['panel_id'] ) ) {
$this->settings['panel_id'] = str_replace( ' ', '-', trim( strtolower( $this->settings['panel'] ) ) );
}
add_action( 'customize_register', array( $this, 'register' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'loadUploaderScript' ) );
}
public function loadUploaderScript() {
wp_enqueue_media();
wp_enqueue_script( 'tf-theme-customizer-serialize', TitanFramework::getURL( 'js/serialize.js', __FILE__ ) );
wp_enqueue_style( 'tf-admin-theme-customizer-styles', TitanFramework::getURL( 'css/admin-theme-customizer-styles.css', __FILE__ ) );
}
public function getID() {
return $this->settings['id'];
}
public function livePreview() {
?>
<script>
jQuery(document).ready(function($) {
<?php
foreach ( $this->options as $option ):
if ( empty( $option->settings['livepreview'] ) ):
continue;
endif;
?>
wp.customize( '<?php echo $option->getID() ?>', function( v ) {
v.bind( function( value ) {
<?php
// Some options may want to insert custom jQuery code before manipulation of live preview
if ( ! empty( $option->settings['id'] ) ) {
do_action( 'tf_livepreview_pre_' . $this->owner->optionNamespace, $option->settings['id'], $option->settings['type'], $option );
}
echo $option->settings['livepreview'];
?>
} );
} );
<?php
endforeach;
?>
});
</script>
<?php
}
/**
* Prints out CSS styles for refresh previewing
*
* @return void
* @since 1.3
*/
public function printPreviewCSS() {
if ( self::$generatedHeadCSSPreview ) {
return;
}
self::$generatedHeadCSSPreview = true;
echo "<style>" . $this->owner->cssInstance->generateCSS() . "</style>";
}
public function register( $wp_customize ) {
add_action( 'wp_head', array( $this, 'printPreviewCSS' ), 1000 );
// Create the panel
if ( ! empty( $this->settings['panel_id'] ) ) {
$existingPanels = $wp_customize->panels();
if ( ! array_key_exists( $this->settings['panel_id'], $existingPanels ) ) {
$wp_customize->add_panel( $this->settings['panel_id'], array(
'title' => $this->settings['panel'],
'priority' => $this->settings['position'],
'capability' => $this->settings['capability'],
) );
}
}
// Create the section
$existingSections = $wp_customize->sections();
if ( ! array_key_exists( $this->settings['id'], $existingSections ) ) {
$wp_customize->add_section( $this->settings['id'], array(
'title' => $this->settings['name'],
'priority' => $this->settings['position'],
'description' => $this->settings['desc'],
'capability' => $this->settings['capability'],
'panel' => empty( $this->settings['panel_id'] ) ? '' : $this->settings['panel_id'],
) );
}
// Unfortunately we have to call each option's register from here
foreach ( $this->options as $index => $option ) {
if ( ! empty( $option->settings['id'] ) ) {
$wp_customize->add_setting( $option->getID() , array(
'default' => $option->settings['default'],
'transport' => empty( $option->settings['livepreview'] ) ? 'refresh' : 'postMessage',
) );
}
// We add the index here, this will be used to order the controls because of this minor bug:
// https://core.trac.wordpress.org/ticket/20733
$option->registerCustomizerControl( $wp_customize, $this, $index + 100 );
}
add_action( 'wp_footer', array( $this, 'livePreview' ) );
}
public function createOption( $settings ) {
if ( ! apply_filters( 'tf_create_option_continue_' . $this->owner->optionNamespace, true, $settings ) ) {
return null;
}
$obj = TitanFrameworkOption::factory( $settings, $this );
$this->options[] = $obj;
do_action( 'tf_create_option_' . $this->owner->optionNamespace, $obj );
return $obj;
}
}