-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.php
333 lines (261 loc) · 11.7 KB
/
utils.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
namespace PublishPress\Revisions;
class Utils {
public static function isRESTurl() {
static $arr_url;
if (!isset($arr_url)) {
$arr_url = wp_parse_url(get_option('siteurl'));
}
if ($arr_url) {
$path = isset($arr_url['path']) ? $arr_url['path'] : '';
if (!isset($_SERVER['REQUEST_URI'])) {
return false;
}
if (0 === strpos(esc_url_raw($_SERVER['REQUEST_URI']), $path . '/wp-json/oembed/')) {
return false;
}
if (0 === strpos(esc_url_raw($_SERVER['REQUEST_URI']), $path . '/wp-json/')) {
return true;
}
}
return false;
}
/**
* Returns true if is a beta or stable version of WP 5.
*
* @return bool
*/
public static function isWp5()
{
global $wp_version;
return version_compare($wp_version, '5.0', '>=') || substr($wp_version, 0, 2) === '5.';
}
/**
* Based on Edit Flow's \Block_Editor_Compatible::should_apply_compat method.
*
* @return bool
*/
public static function isBlockEditorActive($post_type = '', $args = []) {
global $current_user, $wp_version;
$defaults = ['force' => false, 'suppress_filter' => false, 'force_refresh' => false];
$args = array_merge($defaults, $args);
$suppress_filter = $args['suppress_filter'];
if (isset($args['force'])) {
if ('classic' === $args['force']) {
return false;
}
if ('gutenberg' === $args['force']) {
return true;
}
}
// If the editor is being accessed in this request, we have an easy and reliable test
if ((did_action('load-post.php') || did_action('load-post-new.php')) && did_action('admin_enqueue_scripts')) {
if (did_action('enqueue_block_editor_assets')) {
return true;
}
$is_gutenberg_edit = true;
}
// For other requests (or if the decision needs to be made prior to admin_enqueue_scripts action), proceed with other logic...
static $buffer;
if (!isset($buffer)) {
$buffer = [];
}
if (!$post_type) {
if (!$post_type = rvy_detect_post_type()) {
$post_type = 'page';
}
}
if ($post_type_obj = get_post_type_object($post_type)) {
if (!$post_type_obj->show_in_rest) {
return false;
}
}
if (empty($is_gutenberg_edit) && class_exists('acf_pro') && empty($post_type_obj->_builtin) && !post_type_supports($post_type, 'editor') && !defined('REVISIONARY_STANDARD_CLASSIC_EDITOR_DETECTION')) {
return false;
}
if (isset($buffer[$post_type]) && empty($args['force_refresh']) && !$suppress_filter) {
return $buffer[$post_type];
}
if (class_exists('Classic_Editor')) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if (isset($_REQUEST['classic-editor__forget']) && (isset($_REQUEST['classic']) || isset($_REQUEST['classic-editor']))) {
return false;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
} elseif (isset($_REQUEST['classic-editor__forget']) && !isset($_REQUEST['classic']) && !isset($_REQUEST['classic-editor'])) {
return true;
} elseif (get_option('classic-editor-allow-users') === 'allow') {
if ($post_id = rvy_detect_post_id()) {
$which = get_post_meta( $post_id, 'classic-editor-remember', true );
if ('block-editor' == $which) {
return true;
} elseif ('classic-editor' == $which) {
return false;
}
} else {
$use_block = ('block' == get_user_meta($current_user->ID, 'wp_classic-editor-settings'));
if (version_compare($wp_version, '5.9-beta', '>=')) {
if ($has_nav_action = has_action('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type')) {
remove_action('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type');
}
if ($has_nav_filter = has_filter('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type')) {
remove_filter('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type');
}
}
$use_block = $use_block && apply_filters('use_block_editor_for_post_type', $use_block, $post_type, PHP_INT_MAX);
if (version_compare($wp_version, '5.9-beta', '>=') && !empty($has_nav_filter)) {
add_filter('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type', 10, 2 );
}
return $use_block;
}
}
}
// Divi: Classic Editor option
if (function_exists('et_get_option') && ( 'on' == et_get_option( 'et_enable_classic_editor', 'off' ))) {
return false;
}
$pluginsState = array(
'classic-editor' => class_exists( 'Classic_Editor' ),
'gutenberg' => function_exists( 'the_gutenberg_project' ),
'gutenberg-ramp' => class_exists('Gutenberg_Ramp'),
'disable-gutenberg' => class_exists('DisableGutenberg'),
);
$conditions = [];
if ($suppress_filter) remove_filter('use_block_editor_for_post_type', $suppress_filter, 10, 2);
/**
* 5.0:
*
* Classic editor either disabled or enabled (either via an option or with GET argument).
* It's a hairy conditional :(
*/
if (version_compare($wp_version, '5.9-beta', '>=')) {
if ($has_nav_action = has_action('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type')) {
remove_action('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type');
}
if ($has_nav_filter = has_filter('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type')) {
remove_filter('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type');
}
}
if ($val = (self::isWp5() || $pluginsState['gutenberg'])
&& ! $pluginsState['classic-editor']
&& ! $pluginsState['gutenberg-ramp']
&& ! $pluginsState['disable-gutenberg']
&& apply_filters('use_block_editor_for_post_type', true, $post_type, PHP_INT_MAX)) {
$_post = get_post(rvy_detect_post_id());
if (!empty($_post) && !is_null($_post)) {
$val = $val && apply_filters('use_block_editor_for_post', true, $_post, PHP_INT_MAX);
}
}
$conditions[] = $val;
$conditions[] = self::isWp5()
&& $pluginsState['classic-editor']
&& (get_option('classic-editor-replace') === 'block'
&& ! isset($_GET['classic-editor__forget'])); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
$conditions[] = self::isWp5()
&& $pluginsState['classic-editor']
&& (get_option('classic-editor-replace') === 'classic'
&& isset($_GET['classic-editor__forget'])); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if ($val = $pluginsState['gutenberg-ramp']) {
$_post = get_post(rvy_detect_post_id());
if (!empty($_post) && !is_null($_post)) {
$val = $val && apply_filters('use_block_editor_for_post', true, $_post, PHP_INT_MAX);
}
}
$conditions[] = $val;
$conditions[] = $pluginsState['disable-gutenberg']
&& !self::disableGutenberg(rvy_detect_post_id());
if (version_compare($wp_version, '5.9-beta', '>=') && !empty($has_nav_filter)) {
add_filter('use_block_editor_for_post_type', '_disable_block_editor_for_navigation_post_type', 10, 2 );
}
// Returns true if at least one condition is true.
$result = count(
array_filter($conditions,
function ($c) {
return (bool)$c;
}
)
) > 0;
if (!$suppress_filter) {
$buffer[$post_type] = $result;
}
// Returns true if at least one condition is true.
return $result;
}
// Port function from Disable Gutenberg plugin due to problematic early is_plugin_active() function call
private static function disableGutenberg($post_id = false) {
if (function_exists('disable_gutenberg_whitelist_id') && disable_gutenberg_whitelist_id($post_id)) return false;
if (function_exists('disable_gutenberg_whitelist_slug') && disable_gutenberg_whitelist_slug($post_id)) return false;
if (function_exists('disable_gutenberg_whitelist_title') && disable_gutenberg_whitelist_title($post_id)) return false;
if (isset($_GET['block-editor'])) return false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if (isset($_GET['classic-editor'])) return true; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if (isset($_POST['classic-editor'])) return true; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if (function_exists('disable_gutenberg_disable_all') && disable_gutenberg_disable_all()) return true;
if (function_exists('disable_gutenberg_disable_user_role') && disable_gutenberg_disable_user_role()) return true;
if (function_exists('disable_gutenberg_disable_post_type') && disable_gutenberg_disable_post_type()) return true;
if (function_exists('disable_gutenberg_disable_templates') && disable_gutenberg_disable_templates()) return true;
if (function_exists('disable_gutenberg_disable_ids') && disable_gutenberg_disable_ids($post_id)) return true;
return false;
}
/**
* Adds slashes only to strings.
*
* @param mixed $value Value to slash only if string.
*
* @return string|mixed
*/
public static function addslashes_to_strings_only( $value ) {
return \is_string( $value ) ? \addslashes( $value ) : $value;
}
/**
* Replaces faulty core wp_slash().
*
* Until WP 5.5 wp_slash() recursively added slashes not just to strings in array/objects, leading to errors.
*
* @param mixed $value What to add slashes to.
*
* @return mixed
*/
public static function recursively_slash_strings( $value ) {
return self::map_deep( $value, [ self::class, 'addslashes_to_strings_only' ] );
}
public static function map_deep( $value, $callback ) {
if ( is_array( $value ) ) {
foreach ( $value as $index => $item ) {
$value[ $index ] = self::map_deep( $item, $callback );
}
} elseif ( is_object( $value ) ) {
if ( get_class($value) === "__PHP_Incomplete_Class" ) {
return $value;
}
$object_vars = get_object_vars( $value );
foreach ( $object_vars as $property_name => $property_value ) {
$value->$property_name = self::map_deep( $property_value, $callback );
}
} else {
$value = call_user_func( $callback, $value );
}
return $value;
}
public static function get_post_autosave($post_id, $user_id) {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$autosave = $wpdb->get_row(
$wpdb->prepare(
"SELECT *
FROM $wpdb->posts
WHERE post_parent = %d
AND post_type = 'revision'
AND post_status = 'inherit'
AND post_name LIKE '%" . intval($post_id) . "-autosave%'
AND post_author = %d
ORDER BY post_date DESC
LIMIT 1",
$post_id,
$user_id
)
);
if ( ! $autosave ) {
return false;
}
return get_post($autosave);
}
}