forked from xwp/wp-customize-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-customize-posts-plugin.php
More file actions
255 lines (222 loc) · 8.37 KB
/
class-customize-posts-plugin.php
File metadata and controls
255 lines (222 loc) · 8.37 KB
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
<?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_filter( 'user_has_cap', array( $this, 'grant_customize_capability' ), 10, 3 );
add_filter( 'customize_loaded_components', array( $this, 'filter_customize_loaded_components' ), 100, 2 );
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;
}
/**
* 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;
}
/**
* 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';
$plugin_dir_url = plugin_dir_url( dirname( __FILE__ ) );
$handle = 'customize-controls-patched-36521';
$src = $plugin_dir_url . 'js/customize-controls-patched-36521' . $suffix;
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-posts-panel';
$src = $plugin_dir_url . 'js/customize-posts-panel' . $suffix;
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-post-section';
$src = $plugin_dir_url . 'js/customize-post-section' . $suffix;
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-dynamic-control';
$src = $plugin_dir_url . 'js/customize-dynamic-control' . $suffix;
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-posts';
$src = $plugin_dir_url . 'js/customize-posts' . $suffix;
$deps = array(
'jquery',
'wp-backbone',
'customize-controls',
'customize-posts-panel',
'customize-post-section',
'customize-dynamic-control',
'underscore',
);
if ( version_compare( str_replace( array( '-src' ), '', $GLOBALS['wp_version'] ), '4.6-beta1', '<' ) ) {
$deps[] = 'customize-controls-patched-36521';
}
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-post-field-partial';
$src = $plugin_dir_url . 'js/customize-post-field-partial' . $suffix;
$deps = array( 'customize-selective-refresh' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'customize-preview-posts';
$src = $plugin_dir_url . 'js/customize-preview-posts' . $suffix;
$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 = $plugin_dir_url . 'js/edit-post-preview-admin' . $suffix;
$deps = array( 'post' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
$handle = 'edit-post-preview-customize';
$src = $plugin_dir_url . 'js/edit-post-preview-customize' . $suffix;
$deps = array( 'customize-controls' );
$in_footer = 1;
$wp_scripts->add( $handle, $src, $deps, $this->version, $in_footer );
// Page templates.
$handle = 'customize-page-template';
$src = $plugin_dir_url . 'js/customize-page-template' . $suffix;
$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 = $plugin_dir_url . 'js/edit-post-preview-admin-page-template' . $suffix;
$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 = $plugin_dir_url . 'js/customize-featured-image' . $suffix;
$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 = $plugin_dir_url . 'js/edit-post-preview-admin-featured-image' . $suffix;
$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 = $plugin_dir_url . 'js/customize-preview-featured-image' . $suffix;
$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';
$plugin_dir_url = plugin_dir_url( dirname( __FILE__ ) );
$handle = 'customize-posts';
$src = $plugin_dir_url . 'css/customize-posts' . $suffix;
$deps = array( 'wp-admin' );
$version = $this->version;
$wp_styles->add( $handle, $src, $deps, $version );
$handle = 'edit-post-preview-customize';
$src = $plugin_dir_url . 'css/edit-post-preview-customize' . $suffix;
$deps = array( 'customize-controls' );
$wp_styles->add( $handle, $src, $deps, $this->version );
}
}