1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AMP class that implements a template style editor in the Customizer. |
4
|
|
|
* |
5
|
|
|
* A direct, formed link to the AMP editor in the Customizer is added via |
6
|
|
|
* {@see amp_customizer_editor_link()} as a submenu to the Appearance menu. |
7
|
|
|
* |
8
|
|
|
* @since 0.4 |
9
|
|
|
*/ |
10
|
|
|
class AMP_Template_Customizer { |
11
|
|
|
/** |
12
|
|
|
* AMP template editor panel ID. |
13
|
|
|
* |
14
|
|
|
* @since 0.4 |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
const PANEL_ID = 'amp_panel'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Customizer instance. |
21
|
|
|
* |
22
|
|
|
* @since 0.4 |
23
|
|
|
* @access protected |
24
|
|
|
* @var WP_Customize_Manager $wp_customize |
25
|
|
|
*/ |
26
|
|
|
protected $wp_customize; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initialize the template Customizer feature class. |
30
|
|
|
* |
31
|
|
|
* @static |
32
|
|
|
* @since 0.4 |
33
|
|
|
* @access public |
34
|
|
|
* |
35
|
|
|
* @param WP_Customize_Manager $wp_customize Customizer instance. |
36
|
|
|
*/ |
37
|
|
|
public static function init( $wp_customize ) { |
38
|
|
|
$self = new self(); |
39
|
|
|
|
40
|
|
|
$self->wp_customize = $wp_customize; |
41
|
|
|
|
42
|
|
|
do_action( 'amp_customizer_init', $self ); |
43
|
|
|
|
44
|
|
|
// Settings need to be registered for regular customize requests as well (since save is handled there) |
45
|
|
|
$self->register_settings(); |
46
|
|
|
|
47
|
|
|
// Our custom panels only need to go for AMP Customizer requests though |
48
|
|
|
if ( self::is_amp_customizer() ) { |
49
|
|
|
if ( empty( $_GET['url'] ) ) { |
50
|
|
|
$wp_customize->set_preview_url( amp_admin_get_preview_permalink() ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$self->_unregister_core_ui(); |
54
|
|
|
$self->register_ui(); |
55
|
|
|
} elseif ( is_customize_preview() ) { |
56
|
|
|
// Delay preview-specific actions until we're sure we're rendering an AMP page, since it's too early for `is_amp_endpoint()` here. |
57
|
|
|
add_action( 'pre_amp_render_post', array( $self, 'init_preview' ) ); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Filters the core components to unhook the nav_menus and widgets panels. |
63
|
|
|
* |
64
|
|
|
* @since 0.4 |
65
|
|
|
* @access private |
66
|
|
|
* |
67
|
|
|
* @return array Array of core Customizer components to keep active. |
68
|
|
|
*/ |
69
|
|
|
public static function _unregister_core_panels( $panels ) { |
70
|
|
|
if ( self::is_amp_customizer() ) { |
71
|
|
|
$panels = array(); |
72
|
|
|
} |
73
|
|
|
return $panels; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Removes all non-AMP sections and panels. |
78
|
|
|
* |
79
|
|
|
* Provides a clean, standalone instance-like experience by removing all non-AMP registered panels and sections. |
80
|
|
|
* |
81
|
|
|
* @since 0.4 |
82
|
|
|
* @access private |
83
|
|
|
*/ |
84
|
|
|
private function _unregister_core_ui() { |
85
|
|
|
$panels = $this->wp_customize->panels(); |
86
|
|
|
$sections = $this->wp_customize->sections(); |
87
|
|
|
|
88
|
|
|
foreach ( $panels as $panel_id => $object ) { |
89
|
|
|
$this->wp_customize->remove_panel( $panel_id ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach ( $sections as $section_id => $object ) { |
93
|
|
|
$this->wp_customize->remove_section( $section_id ); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function remove_all_panels( $components ) { |
98
|
|
|
error_log("remove panels"); |
99
|
|
|
error_log(print_r($components, 1)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function init_preview() { |
103
|
|
|
// Preview needs controls registered too for postMessage communication. |
104
|
|
|
$this->register_ui(); |
105
|
|
|
|
106
|
|
|
add_action( 'amp_post_template_footer', array( $this, 'add_preview_scripts' ) ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sets up the AMP Customizer preview. |
111
|
|
|
*/ |
112
|
|
|
public function register_ui() { |
113
|
|
|
add_action( 'customize_controls_enqueue_scripts', array( $this, 'add_customizer_scripts' ) ); |
114
|
|
|
add_filter( 'customize_previewable_devices', array( $this, 'force_mobile_preview' ) ); |
115
|
|
|
|
116
|
|
|
$this->wp_customize->add_panel( self::PANEL_ID, array( |
117
|
|
|
'type' => 'amp', |
118
|
|
|
'title' => __( 'AMP', 'amp' ), |
119
|
|
|
'description' => sprintf( __( '<a href="%s" target="_blank">The AMP Project</a> is a Google-led initiative that dramatically improves loading speeds on phones and tablets. You can use the Customizer to preview changes to your AMP template before publishing them.', 'amp' ), 'https://ampproject.org' ), |
120
|
|
|
) ); |
121
|
|
|
|
122
|
|
|
do_action( 'amp_customizer_register_ui', $this->wp_customize ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Registers settings for customizing AMP templates. |
127
|
|
|
* |
128
|
|
|
* @since 0.4 |
129
|
|
|
* @access public |
130
|
|
|
*/ |
131
|
|
|
public function register_settings() { |
132
|
|
|
do_action( 'amp_customizer_register_settings', $this->wp_customize ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function add_customizer_scripts() { |
136
|
|
|
wp_enqueue_script( 'wp-util' ); // fix `wp.template is not a function` |
137
|
|
|
do_action( 'amp_customizer_enqueue_scripts' ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Enqueues scripts and fires the 'wp_footer' action so we can output customizer scripts. |
142
|
|
|
* |
143
|
|
|
* This breaks AMP validation in the customizer but is necessary for the live preview. |
144
|
|
|
* |
145
|
|
|
* @since 0.4 |
146
|
|
|
* @access public |
147
|
|
|
*/ |
148
|
|
|
public function add_preview_scripts() { |
149
|
|
|
wp_enqueue_script( |
150
|
|
|
'amp-customizer', |
151
|
|
|
amp_get_asset_url( 'js/amp-customizer-preview.js' ), |
152
|
|
|
array( 'jquery', 'customize-preview', 'wp-util' ), |
153
|
|
|
$version = false, |
154
|
|
|
$footer = true |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
do_action( 'amp_customizer_enqueue_preview_scripts', $this->wp_customize ); |
158
|
|
|
|
159
|
|
|
/** This action is documented in wp-includes/general-template.php */ |
160
|
|
|
do_action( 'wp_footer' ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function force_mobile_preview( $devices ) { |
164
|
|
|
if ( isset( $devices[ 'mobile' ] ) ) { |
165
|
|
|
$devices['mobile']['default'] = true; |
166
|
|
|
unset( $devices['desktop']['default'] ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $devices; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public static function is_amp_customizer() { |
173
|
|
|
return ! empty( $_REQUEST[ AMP_CUSTOMIZER_QUERY_VAR ] ); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|