This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
gutenberg-ramp.php
270 lines (227 loc) · 7.93 KB
/
gutenberg-ramp.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
<?php
/**
* Gutenberg Ramp
*
* Plugin Name: Gutenberg Ramp
* Description: Allows theme authors to control the circumstances under which the Gutenberg editor loads. Options include "load" (1 loads all the time, 0 loads never) "post_ids" (load for particular posts) "post_types" (load for particular posts types.)
* Version: 1.2
* Author: Automattic, Inc.
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: gutenberg-ramp
*/
/*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License version 2, as published by the Free Software Foundation. You may NOT assume
* that you can use any other version of the GPL.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
include __DIR__ . '/inc/class-gutenberg-ramp.php';
include __DIR__ . '/inc/class-gutenberg-ramp-criteria.php';
include __DIR__ . '/inc/admin/class-gutenberg-ramp-post-type-settings-ui.php';
include __DIR__ . '/inc/admin/class-gutenberg-ramp-compatibility-check.php';
/**
* This function allows themes to specify Gutenberg loading critera.
* In and of itself it doesn't cause any change to Gutenberg's loading behavior.
* However, it governs the option which stores the criteria under which Gutenberg will load
*
* `gutenberg_ramp_load_gutenberg` must be called in the theme before `admin_init`, normally from functions.php or the like
*
* @param bool|array $criteria The criteria used to determine whether Gutenberg should be loaded
*
* @link https://github.com/Automattic/gutenberg-ramp#code-examples
*/
function gutenberg_ramp_load_gutenberg( $criteria = true ) {
// Ignore the awkward phrasing in the 3rd param; we're skipping the word "Use" because core already adds that.
_deprecated_function( 'gutenberg_ramp_load_gutenberg', '0.3', ' built-in filters instead. See https://developer.wordpress.org/reference/hooks/use_block_editor_for_post/ for more information' );
$vip_go_cache_string = 'skip';
$vip_go_cache_group = 'vip-go-gutenberg-ramp-group';
$vip_go_cache_key = 'vip-go-gutenberg-ramp';
// If on VIP Go when using WP-CLI, alert
// VIP that someone is still using the plugin, but
// rate-limit this.
if (
( defined( 'WPCOM_IS_VIP_ENV' ) ) &&
( true === WPCOM_IS_VIP_ENV ) &&
( defined( 'WP_CLI' ) ) &&
( WP_CLI ) &&
( $vip_go_cache_string !== wp_cache_get( $vip_go_cache_key, $vip_go_cache_group ) ) &&
( function_exists( 'wpcom_vip_irc' ) )
) {
wp_cache_set(
$vip_go_cache_key,
$vip_go_cache_string,
$vip_go_cache_group,
HOUR_IN_SECONDS * 10
);
wpcom_vip_irc(
'#vip-gutenberg-ramp-deprecation',
sprintf(
'Site with ID %d is still using Gutenberg Ramp plugin (via %s())',
VIP_GO_APP_ID,
__FUNCTION__
),
0 // Lowest priority
);
}
// only admin requests should refresh loading behavior
if ( ! is_admin() ) {
return;
}
$gutenberg_ramp = Gutenberg_Ramp::get_instance();
/**
* Transform `load` values from booleans to integers
* because they're stored as integers eventually
*/
if ( is_bool( $criteria ) ) {
$criteria = [ 'load' => (int) $criteria ];
} else if ( isset( $criteria['load'] ) && is_bool( $criteria['load'] ) ) {
$criteria['load'] = (int) $criteria['load'];
}
$stored_criteria = $gutenberg_ramp->criteria->get();
if ( $criteria !== $stored_criteria ) {
// the criteria specified in code have changed -- update them
$gutenberg_ramp->criteria->set( $criteria );
}
// indicate that we've loaded the plugin.
$gutenberg_ramp->active = true;
}
/**
* Remove split new post links and Gutenberg menu h/t Ozz
* see https://github.com/azaozz/classic-editor/blob/master/classic-editor.php#L108
*/
function gutenberg_ramp_remove_dashboard_links() {
remove_action( 'admin_menu', 'gutenberg_menu' );
remove_action( 'admin_print_scripts-edit.php', 'gutenberg_replace_default_add_new_button' );
/**
* Safe to remove when Gutenberg is merged
* `gutenberg_modify_add_new_button_url` was removed from Gutenberg a while ago
*
* Keeping this here for now only just in case someone hasn't updated Gutenberg in a long time.
*
* @todo: Remove when WordPress 5.0 is released
* @link https://github.com/WordPress/gutenberg/pull/4690
*/
remove_filter( 'admin_url', 'gutenberg_modify_add_new_button_url' );
}
add_action( 'plugins_loaded', 'gutenberg_ramp_remove_dashboard_links', 10, 0 );
/**
* Initialize Admin UI
*/
function gutenberg_ramp_initialize_admin_ui() {
new Gutenberg_Ramp_Post_Type_Settings_UI();
}
add_action( 'admin_init', 'gutenberg_ramp_initialize_admin_ui' );
/**
* Fallback function for people who tried Gutenberg Ramp before version 0.3
*
* @param bool $criteria
*
* @deprecated 0.3.0
*/
function ramp_for_gutenberg_load_gutenberg( $criteria = false ) {
_deprecated_function( 'ramp_for_gutenberg_load_gutenberg', '0.3', 'gutenberg_ramp_load_gutenberg' );
gutenberg_ramp_load_gutenberg( $criteria );
}
/**
* @return bool|string
*/
function gutenberg_ramp_get_validated_gutenberg_load_path() {
$gutenberg_plugin_path = apply_filters( 'gutenberg_ramp_gutenberg_load_path', WP_PLUGIN_DIR . '/gutenberg/gutenberg.php' );
if ( empty( $gutenberg_plugin_path )
|| 0 !== validate_file( $gutenberg_plugin_path )
|| ! file_exists( $gutenberg_plugin_path )
) {
return false;
}
return $gutenberg_plugin_path;
}
/**
* Ramp expects Gutenberg to be active
* This function is going to load Gutenberg plugin if it's not already active
*
* @return bool
*/
function gutenberg_ramp_require_gutenberg() {
/**
* `register_block_type` exists in both Gutenberg and WordPress 5.0
* If the function already exists - don't manually include Gutenberg Plugin
*/
if ( function_exists( 'register_block_type' ) ) {
return false;
}
// perform any actions required before loading gutenberg
do_action( 'gutenberg_ramp_before_load_gutenberg' );
$validated_load_path = gutenberg_ramp_get_validated_gutenberg_load_path();
if ( false !== $validated_load_path ) {
include_once $validated_load_path;
}
return true;
}
/**
* Require Gutenberg Plugin
*/
gutenberg_ramp_require_gutenberg();
if ( Gutenberg_Ramp_Compatibility_Check::should_check_compatibility() ) {
$ramp_compatibility = new Gutenberg_Ramp_Compatibility_Check();
add_action( 'admin_init', [ $ramp_compatibility, 'maybe_display_notice' ] );
}
/**
* Display notice about plugin being obsolete.
*/
function gutenberg_ramp_admin_notice() {
/*
* Only display when user can manage
* options, i.e. only to admins.
*/
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$gutenberg_ramp = Gutenberg_Ramp::get_instance();
/*
* Do not display if the plugin is
* not loaded.
*/
if ( false === $gutenberg_ramp->active ) {
return;
}
$is_vip_env = ( ( defined( 'WPCOM_IS_VIP_ENV' ) ) && ( true === WPCOM_IS_VIP_ENV ) );
if ( $is_vip_env ) {
$notice_url = 'https://lobby.vip.wordpress.com/2020/11/23/removing-gutenberg-ramp/';
}
else {
$notice_url = 'https://developer.wordpress.org/reference/hooks/use_block_editor_for_post/';
}
?>
<div class="notice notice-success is-dismissible">
<p><?php
printf(
esc_html__(
'The Gutenberg Ramp plugin is deprecated and your installation may require some changes. Please read %sour documentation%s for more information.',
'gutenberg-ramp'
),
sprintf(
'<a href="%s" target="_new">',
esc_url( $notice_url )
),
'</a>'
); ?>
</p>
</div>
<?php
}
add_action( 'admin_notices', 'gutenberg_ramp_admin_notice' );
/**
* Initialize Gutenberg Ramp
*/
$gutenberg_ramp = Gutenberg_Ramp::get_instance();
/**
* Tell Gutenberg when not to load:
*/
// Gutenberg >= 3.5
add_filter( 'gutenberg_can_edit_post', [ $gutenberg_ramp, 'maybe_load_gutenberg' ], 10, 2 );
// WordPress >= 5.0
add_filter( 'use_block_editor_for_post', [ $gutenberg_ramp, 'maybe_load_gutenberg' ], 10, 2 );