This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
/
class-customize-posts-plugin.php
309 lines (270 loc) · 10.4 KB
/
class-customize-posts-plugin.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
* Customize Posts Plugin Class
*
* Implements post management in the Customizer.
*
* @package WordPress
* @subpackage Customize
*/
/**
* Class Customize_Posts_Plugin
*/
class Customize_Posts_Plugin {
/**
* Plugin version.
*
* @var string
*/
public $version;
/**
* Edit Post Preview.
*
* @var Edit_Post_Preview
*/
public $edit_post_preview;
/**
* Page template controller.
*
* @var WP_Customize_Page_Template_Controller
*/
public $page_template_controller;
/**
* Page template controller.
*
* @var WP_Customize_Featured_Image_Controller
*/
public $featured_image_controller;
/**
* Plugin constructor.
*
* @access public
*/
public function __construct() {
if ( ! $this->has_required_core_version() ) {
add_action( 'admin_notices', array( $this, 'show_core_version_dependency_failure' ) );
return;
}
// Parse plugin version.
if ( preg_match( '/Version:\s*(\S+)/', file_get_contents( dirname( __FILE__ ) . '/../customize-posts.php' ), $matches ) ) {
$this->version = $matches[1];
}
require_once dirname( __FILE__ ) . '/class-edit-post-preview.php';
$this->edit_post_preview = new Edit_Post_Preview( $this );
add_action( 'wp_default_scripts', array( $this, 'register_scripts' ), 11 );
add_action( 'wp_default_styles', array( $this, 'register_styles' ), 11 );
add_action( 'init', array( $this, 'register_customize_draft' ) );
add_filter( 'user_has_cap', array( $this, 'grant_customize_capability' ), 10, 3 );
add_filter( 'customize_loaded_components', array( $this, 'filter_customize_loaded_components' ), 100, 2 );
add_action( 'customize_register', array( $this, 'load_support_classes' ) );
require_once dirname( __FILE__ ) . '/class-wp-customize-postmeta-controller.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-page-template-controller.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-featured-image-controller.php';
$this->page_template_controller = new WP_Customize_Page_Template_Controller();
$this->featured_image_controller = new WP_Customize_Featured_Image_Controller();
}
/**
* Determine whether the dependencies are satisfied for the plugin.
*
* @return bool
*/
function has_required_core_version() {
$has_required_wp_version = version_compare( str_replace( array( '-src' ), '', $GLOBALS['wp_version'] ), '4.5-beta2', '>=' );
return $has_required_wp_version;
}
/**
* Register the `customize-draft` post status.
*
* @action init
* @access public
*/
public function register_customize_draft() {
register_post_status( 'customize-draft', array(
'label' => 'customize-draft',
'public' => false,
'internal' => true,
'protected' => true,
'exclude_from_search' => true,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => false,
) );
}
/**
* Let users who can edit posts also access the Customizer because there is something for them there.
*
* @see https://core.trac.wordpress.org/ticket/28605
* @param array $allcaps All capabilities.
* @param array $caps Capabilities.
* @param array $args Args.
* @return array All capabilities.
*/
function grant_customize_capability( $allcaps, $caps, $args ) {
if ( ! empty( $allcaps['edit_posts'] ) && ! empty( $args ) && 'customize' === $args[0] ) {
$allcaps = array_merge( $allcaps, array_fill_keys( $caps, true ) );
}
return $allcaps;
}
/**
* Bootstrap.
*
* This will be part of the WP_Customize_Manager::__construct() or another such class constructor in #coremerge.
*
* @param array $components Components.
* @param WP_Customize_Manager $wp_customize Manager.
* @return array Components.
*/
function filter_customize_loaded_components( $components, $wp_customize ) {
require_once dirname( __FILE__ ) . '/class-wp-customize-posts.php';
$wp_customize->posts = new WP_Customize_Posts( $wp_customize );
return $components;
}
/**
* Load theme and plugin compatibility classes.
*
* @codeCoverageIgnore
*
* @param WP_Customize_Manager $wp_customize Manager.
*/
function load_support_classes( $wp_customize ) {
// Theme & Plugin Support.
require_once dirname( __FILE__ ) . '/class-customize-posts-support.php';
require_once dirname( __FILE__ ) . '/class-customize-posts-plugin-support.php';
require_once dirname( __FILE__ ) . '/class-customize-posts-theme-support.php';
foreach ( array( 'theme', 'plugin' ) as $type ) {
foreach ( glob( dirname( __FILE__ ) . '/' . $type . '-support/class-*.php' ) as $file_path ) {
require_once $file_path;
$class_name = str_replace( '-', '_', preg_replace( '/^class-(.+)\.php$/', '$1', basename( $file_path ) ) );
if ( class_exists( $class_name ) ) {
$wp_customize->posts->add_support( new $class_name( $wp_customize->posts ) );
}
}
}
}
/**
* Show error dependency failure notice.
*/
function show_core_version_dependency_failure() {
?>
<div class="error">
<p><?php esc_html_e( 'Customize Posts requires WordPress 4.5-beta2 and should have the Customize Setting Validation plugin active.', 'customize-posts' ); ?></p>
</div>
<?php
}
/**
* Register scripts for Customize Posts.
*
* @param WP_Scripts $wp_scripts Scripts.
*/
public function register_scripts( WP_Scripts $wp_scripts ) {
$suffix = ( SCRIPT_DEBUG ? '' : '.min' ) . '.js';
require_once ABSPATH . WPINC . '/class-wp-customize-setting.php';
$is_gte_wp46_beta = method_exists( 'WP_Customize_Setting', 'validate' );
if ( ! $is_gte_wp46_beta ) {
$handle = 'customize-controls-patched-36521';
$src = plugins_url( 'js/customize-controls-patched-36521' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
}
$handle = 'customize-posts-panel';
$src = plugins_url( 'js/customize-posts-panel' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-post-section';
$src = plugins_url( 'js/customize-post-section' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-dynamic-control';
$src = plugins_url( 'js/customize-dynamic-control' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-posts';
$src = plugins_url( 'js/customize-posts' . $suffix, dirname( __FILE__ ) );
$deps = array(
'jquery',
'wp-backbone',
'customize-controls',
'customize-posts-panel',
'customize-post-section',
'customize-dynamic-control',
'underscore',
);
if ( ! $is_gte_wp46_beta ) {
$deps[] = 'customize-controls-patched-36521';
}
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-post-field-partial';
$src = plugins_url( 'js/customize-post-field-partial' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-selective-refresh' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-preview-posts';
$src = plugins_url( 'js/customize-preview-posts' . $suffix, dirname( __FILE__ ) );
$deps = array( 'jquery', 'customize-preview', 'customize-post-field-partial' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'edit-post-preview-admin';
$src = plugins_url( 'js/edit-post-preview-admin' . $suffix, dirname( __FILE__ ) );
$deps = array( 'post' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'edit-post-preview-customize';
$src = plugins_url( 'js/edit-post-preview-customize' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
// Page templates.
$handle = 'customize-page-template';
$src = plugins_url( 'js/customize-page-template' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls', 'customize-posts' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'edit-post-preview-admin-page-template';
$src = plugins_url( 'js/edit-post-preview-admin-page-template' . $suffix, dirname( __FILE__ ) );
$deps = array( 'edit-post-preview-admin' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
// Featured images.
$handle = 'customize-featured-image';
$src = plugins_url( 'js/customize-featured-image' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls', 'customize-posts' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'edit-post-preview-admin-featured-image';
$src = plugins_url( 'js/edit-post-preview-admin-featured-image' . $suffix, dirname( __FILE__ ) );
$deps = array( 'edit-post-preview-admin' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-preview-featured-image';
$src = plugins_url( 'js/customize-preview-featured-image' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-preview', 'customize-selective-refresh' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
}
/**
* Register styles for Customize Posts.
*
* @param WP_Styles $wp_styles Styles.
*/
public function register_styles( WP_Styles $wp_styles ) {
$suffix = ( SCRIPT_DEBUG ? '' : '.min' ) . '.css';
$handle = 'customize-posts';
$src = plugins_url( 'css/customize-posts' . $suffix, dirname( __FILE__ ) );
$deps = array( 'wp-admin' );
$version = $this->version;
$wp_styles->add( $handle, $src, $deps, $version );
$handle = 'customize-controls-sticky-headers';
$src = plugins_url( 'css/customize-controls-sticky-headers' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls' );
$version = $this->version;
$wp_styles->add( $handle, $src, $deps, $version );
$handle = 'edit-post-preview-customize';
$src = plugins_url( 'css/edit-post-preview-customize' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls' );
$wp_styles->add( $handle, $src, $deps, $this->version );
}
}