PluginProbe ʕ •ᴥ•ʔ
Classic Editor / 1.6.4
Classic Editor v1.6.4
1.7.0 trunk 0.1 0.2 0.3 0.4 0.5 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7
classic-editor / classic-editor.php
classic-editor Last commit date
js 5 years ago LICENSE.md 5 years ago classic-editor.php 1 year ago readme.txt 1 year ago
classic-editor.php
982 lines
1 <?php
2 /**
3 * Classic Editor
4 *
5 * Plugin Name: Classic Editor
6 * Plugin URI: https://wordpress.org/plugins/classic-editor/
7 * Description: Enables the WordPress classic editor and the old-style Edit Post screen with TinyMCE, Meta Boxes, etc. Supports the older plugins that extend this screen.
8 * Version: 1.6.4
9 * Author: WordPress Contributors
10 * Author URI: https://github.com/WordPress/classic-editor/
11 * License: GPLv2 or later
12 * License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
13 * Text Domain: classic-editor
14 * Domain Path: /languages
15 * Requires at least: 4.9
16 * Requires PHP: 5.2.4
17 *
18 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
19 * General Public License version 2, as published by the Free Software Foundation. You may NOT assume
20 * that you can use any other version of the GPL.
21 *
22 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
23 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 */
25
26 if ( ! defined( 'ABSPATH' ) ) {
27 die( 'Invalid request.' );
28 }
29
30 if ( ! class_exists( 'Classic_Editor' ) ) :
31 class Classic_Editor {
32 private static $settings;
33 private static $supported_post_types = array();
34
35 private function __construct() {}
36
37 public static function init_actions() {
38 $block_editor = has_action( 'enqueue_block_assets' );
39 $gutenberg = function_exists( 'gutenberg_register_scripts_and_styles' );
40
41 register_activation_hook( __FILE__, array( __CLASS__, 'activate' ) );
42
43 $settings = self::get_settings();
44
45 if ( is_multisite() ) {
46 add_action( 'wpmu_options', array( __CLASS__, 'network_settings' ) );
47 add_action( 'update_wpmu_options', array( __CLASS__, 'save_network_settings' ) );
48 }
49
50 if ( ! $settings['hide-settings-ui'] ) {
51 // Add a link to the plugin's settings and/or network admin settings in the plugins list table.
52 add_filter( 'plugin_action_links', array( __CLASS__, 'add_settings_link' ), 10, 2 );
53 add_filter( 'network_admin_plugin_action_links', array( __CLASS__, 'add_settings_link' ), 10, 2 );
54
55 add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
56
57 if ( $settings['allow-users'] ) {
58 // User settings.
59 add_action( 'personal_options_update', array( __CLASS__, 'save_user_settings' ) );
60 add_action( 'edit_user_profile_update', array( __CLASS__, 'save_user_settings' ) );
61 add_action( 'profile_personal_options', array( __CLASS__, 'user_settings' ) );
62 add_action( 'edit_user_profile', array( __CLASS__, 'user_settings') );
63 }
64 }
65
66 // Always remove the "Try Gutenberg" dashboard widget. See https://core.trac.wordpress.org/ticket/44635.
67 remove_action( 'try_gutenberg_panel', 'wp_try_gutenberg_panel' );
68
69 if ( ! $block_editor && ! $gutenberg ) {
70 return;
71 }
72
73 if ( $settings['allow-users'] ) {
74 // Also used in Gutenberg.
75 add_filter( 'use_block_editor_for_post', array( __CLASS__, 'choose_editor' ), 100, 2 );
76
77 if ( $gutenberg ) {
78 // Support older Gutenberg versions.
79 add_filter( 'gutenberg_can_edit_post', array( __CLASS__, 'choose_editor' ), 100, 2 );
80
81 if ( $settings['editor'] === 'classic' ) {
82 self::remove_gutenberg_hooks( 'some' );
83 }
84 }
85
86 add_filter( 'get_edit_post_link', array( __CLASS__, 'get_edit_post_link' ) );
87 add_filter( 'redirect_post_location', array( __CLASS__, 'redirect_location' ) );
88 add_action( 'edit_form_top', array( __CLASS__, 'add_redirect_helper' ) );
89 add_action( 'admin_head-edit.php', array( __CLASS__, 'add_edit_php_inline_style' ) );
90
91 add_action( 'edit_form_top', array( __CLASS__, 'remember_classic_editor' ) );
92
93 if ( version_compare( $GLOBALS['wp_version'], '5.8', '>=' ) ) {
94 add_filter( 'block_editor_settings_all', array( __CLASS__, 'remember_block_editor' ), 10, 2 );
95 } else {
96 add_filter( 'block_editor_settings', array( __CLASS__, 'remember_block_editor' ), 10, 2 );
97 }
98
99 // Post state (edit.php)
100 add_filter( 'display_post_states', array( __CLASS__, 'add_post_state' ), 10, 2 );
101 // Row actions (edit.php)
102 add_filter( 'page_row_actions', array( __CLASS__, 'add_edit_links' ), 15, 2 );
103 add_filter( 'post_row_actions', array( __CLASS__, 'add_edit_links' ), 15, 2 );
104
105 // Switch editors while editing a post
106 add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_box' ), 10, 2 );
107 add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_scripts' ) );
108 } else {
109 if ( $settings['editor'] === 'classic' ) {
110 // Also used in Gutenberg.
111 // Consider disabling other Block Editor functionality.
112 add_filter( 'use_block_editor_for_post_type', '__return_false', 100 );
113
114 if ( $gutenberg ) {
115 // Support older Gutenberg versions.
116 add_filter( 'gutenberg_can_edit_post_type', '__return_false', 100 );
117 self::remove_gutenberg_hooks();
118 }
119 } else {
120 // $settings['editor'] === 'block', nothing to do :)
121 return;
122 }
123 }
124
125 if ( $block_editor ) {
126 // Move the Privacy Page notice back under the title.
127 add_action( 'admin_init', array( __CLASS__, 'on_admin_init' ) );
128 }
129 if ( $gutenberg ) {
130 // These are handled by this plugin. All are older, not used in 5.3+.
131 remove_action( 'admin_init', 'gutenberg_add_edit_link_filters' );
132 remove_action( 'admin_print_scripts-edit.php', 'gutenberg_replace_default_add_new_button' );
133 remove_filter( 'redirect_post_location', 'gutenberg_redirect_to_classic_editor_when_saving_posts' );
134 remove_filter( 'display_post_states', 'gutenberg_add_gutenberg_post_state' );
135 remove_action( 'edit_form_top', 'gutenberg_remember_classic_editor_when_saving_posts' );
136 }
137 }
138
139 public static function remove_gutenberg_hooks( $remove = 'all' ) {
140 remove_action( 'admin_menu', 'gutenberg_menu' );
141 remove_action( 'admin_init', 'gutenberg_redirect_demo' );
142
143 if ( $remove !== 'all' ) {
144 return;
145 }
146
147 // Gutenberg 5.3+
148 remove_action( 'wp_enqueue_scripts', 'gutenberg_register_scripts_and_styles' );
149 remove_action( 'admin_enqueue_scripts', 'gutenberg_register_scripts_and_styles' );
150 remove_action( 'admin_notices', 'gutenberg_wordpress_version_notice' );
151 remove_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' );
152 remove_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
153 remove_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' );
154 remove_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' );
155 remove_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' );
156 remove_action( 'admin_enqueue_scripts', 'gutenberg_widgets_init' );
157 remove_action( 'admin_notices', 'gutenberg_build_files_notice' );
158
159 remove_filter( 'load_script_translation_file', 'gutenberg_override_translation_file' );
160 remove_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
161 remove_filter( 'default_content', 'gutenberg_default_demo_content' );
162 remove_filter( 'default_title', 'gutenberg_default_demo_title' );
163 remove_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' );
164 remove_filter( 'rest_request_after_callbacks', 'gutenberg_filter_oembed_result' );
165
166 // Previously used, compat for older Gutenberg versions.
167 remove_filter( 'wp_refresh_nonces', 'gutenberg_add_rest_nonce_to_heartbeat_response_headers' );
168 remove_filter( 'get_edit_post_link', 'gutenberg_revisions_link_to_editor' );
169 remove_filter( 'wp_prepare_revision_for_js', 'gutenberg_revisions_restore' );
170
171 remove_action( 'rest_api_init', 'gutenberg_register_rest_routes' );
172 remove_action( 'rest_api_init', 'gutenberg_add_taxonomy_visibility_field' );
173 remove_filter( 'registered_post_type', 'gutenberg_register_post_prepare_functions' );
174
175 remove_action( 'do_meta_boxes', 'gutenberg_meta_box_save' );
176 remove_action( 'submitpost_box', 'gutenberg_intercept_meta_box_render' );
177 remove_action( 'submitpage_box', 'gutenberg_intercept_meta_box_render' );
178 remove_action( 'edit_page_form', 'gutenberg_intercept_meta_box_render' );
179 remove_action( 'edit_form_advanced', 'gutenberg_intercept_meta_box_render' );
180 remove_filter( 'redirect_post_location', 'gutenberg_meta_box_save_redirect' );
181 remove_filter( 'filter_gutenberg_meta_boxes', 'gutenberg_filter_meta_boxes' );
182
183 remove_filter( 'body_class', 'gutenberg_add_responsive_body_class' );
184 remove_filter( 'admin_url', 'gutenberg_modify_add_new_button_url' ); // old
185 remove_action( 'admin_enqueue_scripts', 'gutenberg_check_if_classic_needs_warning_about_blocks' );
186 remove_filter( 'register_post_type_args', 'gutenberg_filter_post_type_labels' );
187
188 // phpcs:disable Squiz.PHP.CommentedOutCode.Found
189 // Keep
190 // remove_filter( 'wp_kses_allowed_html', 'gutenberg_kses_allowedtags', 10, 2 ); // not needed in 5.0
191 // remove_filter( 'bulk_actions-edit-wp_block', 'gutenberg_block_bulk_actions' );
192 // remove_filter( 'wp_insert_post_data', 'gutenberg_remove_wpcom_markdown_support' );
193 // remove_filter( 'the_content', 'do_blocks', 9 );
194 // remove_action( 'init', 'gutenberg_register_post_types' );
195
196 // Continue to manage wpautop for posts that were edited in Gutenberg.
197 // remove_filter( 'wp_editor_settings', 'gutenberg_disable_editor_settings_wpautop' );
198 // remove_filter( 'the_content', 'gutenberg_wpautop', 8 );
199 // phpcs:enable Squiz.PHP.CommentedOutCode.Found
200
201 }
202
203 private static function get_settings( $refresh = 'no', $user_id = 0 ) {
204 /**
205 * Can be used to override the plugin's settings. Always hides the settings UI when used (as users cannot change the settings).
206 *
207 * Has to return an associative array with two keys.
208 * The defaults are:
209 * 'editor' => 'classic', // Accepted values: 'classic', 'block'.
210 * 'allow-users' => false,
211 *
212 * @param boolean To override the settings return an array with the above keys. Default false.
213 */
214 $settings = apply_filters( 'classic_editor_plugin_settings', false );
215
216 if ( is_array( $settings ) ) {
217 return array(
218 'editor' => ( isset( $settings['editor'] ) && $settings['editor'] === 'block' ) ? 'block' : 'classic',
219 'allow-users' => ! empty( $settings['allow-users'] ),
220 'hide-settings-ui' => true,
221 );
222 }
223
224 if ( ! empty( self::$settings ) && $refresh === 'no' ) {
225 return self::$settings;
226 }
227
228 if ( is_multisite() ) {
229 $defaults = array(
230 'editor' => get_network_option( null, 'classic-editor-replace' ) === 'block' ? 'block' : 'classic',
231 'allow-users' => false,
232 );
233
234 /**
235 * Filters the default network options.
236 *
237 * @param array $defaults The default options array. See `classic_editor_plugin_settings` for supported keys and values.
238 */
239 $defaults = apply_filters( 'classic_editor_network_default_settings', $defaults );
240
241 if ( get_network_option( null, 'classic-editor-allow-sites' ) !== 'allow' ) {
242 // Per-site settings are disabled. Return default network options nad hide the settings UI.
243 $defaults['hide-settings-ui'] = true;
244 return $defaults;
245 }
246
247 // Override with the site options.
248 $editor_option = get_option( 'classic-editor-replace' );
249 $allow_users_option = get_option( 'classic-editor-allow-users' );
250
251 if ( $editor_option ) {
252 $defaults['editor'] = $editor_option;
253 }
254 if ( $allow_users_option ) {
255 $defaults['allow-users'] = ( $allow_users_option === 'allow' );
256 }
257
258 $editor = ( isset( $defaults['editor'] ) && $defaults['editor'] === 'block' ) ? 'block' : 'classic';
259 $allow_users = ! empty( $defaults['allow-users'] );
260 } else {
261 $allow_users = ( get_option( 'classic-editor-allow-users' ) === 'allow' );
262 $option = get_option( 'classic-editor-replace' );
263
264 // Normalize old options.
265 if ( $option === 'block' || $option === 'no-replace' ) {
266 $editor = 'block';
267 } else {
268 // empty( $option ) || $option === 'classic' || $option === 'replace'
269 $editor = 'classic';
270 }
271 }
272
273 // Override the defaults with the user options.
274 if ( ( ! isset( $GLOBALS['pagenow'] ) || $GLOBALS['pagenow'] !== 'options-writing.php' ) && $allow_users ) {
275
276 $user_options = get_user_option( 'classic-editor-settings', $user_id );
277
278 if ( $user_options === 'block' || $user_options === 'classic' ) {
279 $editor = $user_options;
280 }
281 }
282
283 self::$settings = array(
284 'editor' => $editor,
285 'hide-settings-ui' => false,
286 'allow-users' => $allow_users,
287 );
288
289 return self::$settings;
290 }
291
292 private static function is_classic( $post_id = 0 ) {
293 if ( ! $post_id ) {
294 $post_id = self::get_edited_post_id();
295 }
296
297 if ( $post_id ) {
298 $settings = self::get_settings();
299
300 if ( $settings['allow-users'] && ! isset( $_GET['classic-editor__forget'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
301 $which = get_post_meta( $post_id, 'classic-editor-remember', true );
302
303 if ( $which ) {
304 // The editor choice will be "remembered" when the post is opened in either the classic or the block editor.
305 if ( 'classic-editor' === $which ) {
306 return true;
307 } elseif ( 'block-editor' === $which ) {
308 return false;
309 }
310 }
311
312 return ( ! self::has_blocks( $post_id ) );
313 }
314 }
315
316 if ( isset( $_GET['classic-editor'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
317 return true;
318 }
319
320 return false;
321 }
322
323 /**
324 * Get the edited post ID (early) when loading the Edit Post screen.
325 */
326 private static function get_edited_post_id() {
327 // phpcs:disable WordPress.Security.NonceVerification.Recommended
328 if (
329 ! empty( $_GET['post'] ) &&
330 ! empty( $_GET['action'] ) &&
331 $_GET['action'] === 'edit' &&
332 ! empty( $GLOBALS['pagenow'] ) &&
333 $GLOBALS['pagenow'] === 'post.php'
334 ) {
335 return (int) $_GET['post']; // post_ID
336 }
337 // phpcs:enable WordPress.Security.NonceVerification.Recommended
338
339 return 0;
340 }
341
342 public static function register_settings() {
343 // Add an option to Settings -> Writing
344 register_setting( 'writing', 'classic-editor-replace', array(
345 'sanitize_callback' => array( __CLASS__, 'validate_option_editor' ),
346 ) );
347
348 register_setting( 'writing', 'classic-editor-allow-users', array(
349 'sanitize_callback' => array( __CLASS__, 'validate_option_allow_users' ),
350 ) );
351
352 $allowed_options = array(
353 'writing' => array(
354 'classic-editor-replace',
355 'classic-editor-allow-users'
356 ),
357 );
358
359 if ( function_exists( 'add_allowed_options' ) ) {
360 add_allowed_options( $allowed_options );
361 } else {
362 add_option_whitelist( $allowed_options );
363 }
364
365 $heading_1 = __( 'Default editor for all users', 'classic-editor' );
366 $heading_2 = __( 'Allow users to switch editors', 'classic-editor' );
367
368 add_settings_field( 'classic-editor-1', $heading_1, array( __CLASS__, 'settings_1' ), 'writing' );
369 add_settings_field( 'classic-editor-2', $heading_2, array( __CLASS__, 'settings_2' ), 'writing' );
370 }
371
372 public static function save_user_settings( $user_id ) {
373 if (
374 isset( $_POST['classic-editor-user-settings'] ) &&
375 isset( $_POST['classic-editor-replace'] ) &&
376 wp_verify_nonce( $_POST['classic-editor-user-settings'], 'allow-user-settings' ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
377 ) {
378 $user_id = (int) $user_id;
379
380 if ( $user_id !== get_current_user_id() && ! current_user_can( 'edit_user', $user_id ) ) {
381 return;
382 }
383
384 $editor = self::validate_option_editor( $_POST['classic-editor-replace'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
385 update_user_option( $user_id, 'classic-editor-settings', $editor );
386 }
387 }
388
389 /**
390 * Validate
391 */
392 public static function validate_option_editor( $value ) {
393 if ( $value === 'block' ) {
394 return 'block';
395 }
396
397 return 'classic';
398 }
399
400 public static function validate_option_allow_users( $value ) {
401 if ( $value === 'allow' ) {
402 return 'allow';
403 }
404
405 return 'disallow';
406 }
407
408 public static function settings_1( $user_id = 0 ) {
409 $settings = self::get_settings( 'refresh', $user_id );
410
411 ?>
412 <div class="classic-editor-options">
413 <p>
414 <input type="radio" name="classic-editor-replace" id="classic-editor-classic" value="classic"<?php if ( $settings['editor'] === 'classic' ) echo ' checked'; ?> />
415 <label for="classic-editor-classic"><?php _ex( 'Classic editor', 'Editor Name', 'classic-editor' ); ?></label>
416 </p>
417 <p>
418 <input type="radio" name="classic-editor-replace" id="classic-editor-block" value="block"<?php if ( $settings['editor'] !== 'classic' ) echo ' checked'; ?> />
419 <label for="classic-editor-block"><?php _ex( 'Block editor', 'Editor Name', 'classic-editor' ); ?></label>
420 </p>
421 </div>
422 <script>
423 jQuery( 'document' ).ready( function( $ ) {
424 if ( window.location.hash === '#classic-editor-options' ) {
425 $( '.classic-editor-options' ).closest( 'td' ).addClass( 'highlight' );
426 }
427 } );
428 </script>
429 <?php
430 }
431
432 public static function settings_2() {
433 $settings = self::get_settings( 'refresh' );
434
435 ?>
436 <div class="classic-editor-options">
437 <p>
438 <input type="radio" name="classic-editor-allow-users" id="classic-editor-allow" value="allow"<?php if ( $settings['allow-users'] ) echo ' checked'; ?> />
439 <label for="classic-editor-allow"><?php _e( 'Yes', 'classic-editor' ); ?></label>
440 </p>
441 <p>
442 <input type="radio" name="classic-editor-allow-users" id="classic-editor-disallow" value="disallow"<?php if ( ! $settings['allow-users'] ) echo ' checked'; ?> />
443 <label for="classic-editor-disallow"><?php _e( 'No', 'classic-editor' ); ?></label>
444 </p>
445 </div>
446 <?php
447 }
448
449 /**
450 * Shown on the Profile page when allowed by admin.
451 */
452 public static function user_settings( $user = null ) {
453 global $user_can_edit;
454 $settings = self::get_settings( 'update' );
455
456 if ( ! $user_can_edit || ! $settings['allow-users'] ) {
457 return;
458 }
459
460 if ( $user instanceof WP_User ) {
461 $user_id = (int) $user->ID;
462 } else {
463 $user_id = 0;
464 }
465
466 ?>
467 <table class="form-table">
468 <tr class="classic-editor-user-options">
469 <th scope="row"><?php _e( 'Default Editor', 'classic-editor' ); ?></th>
470 <td>
471 <?php wp_nonce_field( 'allow-user-settings', 'classic-editor-user-settings' ); ?>
472 <?php self::settings_1( $user_id ); ?>
473 </td>
474 </tr>
475 </table>
476 <script>jQuery( 'tr.user-rich-editing-wrap' ).before( jQuery( 'tr.classic-editor-user-options' ) );</script>
477 <?php
478 }
479
480 public static function network_settings() {
481 $editor = get_network_option( null, 'classic-editor-replace' );
482 $is_checked = ( get_network_option( null, 'classic-editor-allow-sites' ) === 'allow' );
483
484 ?>
485 <h2 id="classic-editor-options"><?php _e( 'Editor Settings', 'classic-editor' ); ?></h2>
486 <table class="form-table">
487 <?php wp_nonce_field( 'allow-site-admin-settings', 'classic-editor-network-settings' ); ?>
488 <tr>
489 <th scope="row"><?php _e( 'Default editor for all sites', 'classic-editor' ); ?></th>
490 <td>
491 <p>
492 <input type="radio" name="classic-editor-replace" id="classic-editor-classic" value="classic"<?php if ( $editor !== 'block' ) echo ' checked'; ?> />
493 <label for="classic-editor-classic"><?php _ex( 'Classic Editor', 'Editor Name', 'classic-editor' ); ?></label>
494 </p>
495 <p>
496 <input type="radio" name="classic-editor-replace" id="classic-editor-block" value="block"<?php if ( $editor === 'block' ) echo ' checked'; ?> />
497 <label for="classic-editor-block"><?php _ex( 'Block editor', 'Editor Name', 'classic-editor' ); ?></label>
498 </p>
499 </td>
500 </tr>
501 <tr>
502 <th scope="row"><?php _e( 'Change settings', 'classic-editor' ); ?></th>
503 <td>
504 <input type="checkbox" name="classic-editor-allow-sites" id="classic-editor-allow-sites" value="allow"<?php if ( $is_checked ) echo ' checked'; ?>>
505 <label for="classic-editor-allow-sites"><?php _e( 'Allow site admins to change settings', 'classic-editor' ); ?></label>
506 <p class="description"><?php _e( 'By default the block editor is replaced with the classic editor and users cannot switch editors.', 'classic-editor' ); ?></p>
507 </td>
508 </tr>
509 </table>
510 <?php
511 }
512
513 public static function save_network_settings() {
514 if (
515 isset( $_POST['classic-editor-network-settings'] ) &&
516 current_user_can( 'manage_network_options' ) &&
517 wp_verify_nonce( $_POST['classic-editor-network-settings'], 'allow-site-admin-settings' ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
518 ) {
519 if ( isset( $_POST['classic-editor-replace'] ) && $_POST['classic-editor-replace'] === 'block' ) {
520 update_network_option( null, 'classic-editor-replace', 'block' );
521 } else {
522 update_network_option( null, 'classic-editor-replace', 'classic' );
523 }
524 if ( isset( $_POST['classic-editor-allow-sites'] ) && $_POST['classic-editor-allow-sites'] === 'allow' ) {
525 update_network_option( null, 'classic-editor-allow-sites', 'allow' );
526 } else {
527 update_network_option( null, 'classic-editor-allow-sites', 'disallow' );
528 }
529 }
530 }
531
532 /**
533 * Add a hidden field in edit-form-advanced.php
534 * to help redirect back to the classic editor on saving.
535 */
536 public static function add_redirect_helper() {
537 ?>
538 <input type="hidden" name="classic-editor" value="" />
539 <?php
540 }
541
542 /**
543 * Remember when the classic editor was used to edit a post.
544 */
545 public static function remember_classic_editor( $post ) {
546 $post_type = get_post_type( $post );
547
548 if ( $post_type && post_type_supports( $post_type, 'editor' ) ) {
549 self::remember( $post->ID, 'classic-editor' );
550 }
551 }
552
553 /**
554 * Remember when the block editor was used to edit a post.
555 */
556 public static function remember_block_editor( $editor_settings, $context ) {
557 if ( is_a( $context, 'WP_Post' ) ) {
558 $post = $context;
559 } elseif ( ! empty( $context->post ) ) {
560 $post = $context->post;
561 } else {
562 return $editor_settings;
563 }
564
565 $post_type = get_post_type( $post );
566
567 if ( $post_type && self::can_edit_post_type( $post_type ) ) {
568 self::remember( $post->ID, 'block-editor' );
569 }
570
571 return $editor_settings;
572 }
573
574 private static function remember( $post_id, $editor ) {
575 if ( get_post_meta( $post_id, 'classic-editor-remember', true ) !== $editor ) {
576 update_post_meta( $post_id, 'classic-editor-remember', $editor );
577 }
578 }
579
580 /**
581 * Choose which editor to use for a post.
582 *
583 * Passes through `$which_editor` for block editor (it's sets to `true` but may be changed by another plugin).
584 *
585 * @uses `use_block_editor_for_post` filter.
586 *
587 * @param boolean $use_block_editor True for block editor, false for classic editor.
588 * @param WP_Post $post The post being edited.
589 * @return boolean True for block editor, false for classic editor.
590 */
591 public static function choose_editor( $use_block_editor, $post ) {
592 $settings = self::get_settings();
593 $editors = self::get_enabled_editors_for_post( $post );
594
595 // If no editor is supported, pass through `$use_block_editor`.
596 if ( ! $editors['block_editor'] && ! $editors['classic_editor'] ) {
597 return $use_block_editor;
598 }
599
600 // Open the default editor when no $post and for "Add New" links,
601 // or the alternate editor when the user is switching editors.
602 // phpcs:disable WordPress.Security.NonceVerification.Recommended
603 if ( empty( $post->ID ) || $post->post_status === 'auto-draft' ) {
604 if (
605 ( $settings['editor'] === 'classic' && ! isset( $_GET['classic-editor__forget'] ) ) || // Add New
606 ( isset( $_GET['classic-editor'] ) && isset( $_GET['classic-editor__forget'] ) ) // Switch to classic editor when no draft post.
607 ) {
608 $use_block_editor = false;
609 }
610 } elseif ( self::is_classic( $post->ID ) ) {
611 $use_block_editor = false;
612 }
613 // phpcs:enable WordPress.Security.NonceVerification.Recommended
614
615 // Enforce the editor if set by plugins.
616 if ( $use_block_editor && ! $editors['block_editor'] ) {
617 $use_block_editor = false;
618 } elseif ( ! $use_block_editor && ! $editors['classic_editor'] && $editors['block_editor'] ) {
619 $use_block_editor = true;
620 }
621
622 return $use_block_editor;
623 }
624
625 /**
626 * Keep the `classic-editor` query arg through redirects when saving posts.
627 */
628 public static function redirect_location( $location ) {
629 if (
630 isset( $_REQUEST['classic-editor'] ) || // phpcs:ignore WordPress.Security.NonceVerification.Recommended
631 ( isset( $_POST['_wp_http_referer'] ) && strpos( $_POST['_wp_http_referer'], '&classic-editor' ) !== false ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
632 ) {
633 $location = add_query_arg( 'classic-editor', '', $location );
634 }
635
636 return $location;
637 }
638
639 /**
640 * Keep the `classic-editor` query arg when looking at revisions.
641 */
642 public static function get_edit_post_link( $url ) {
643 $settings = self::get_settings();
644
645 if ( isset( $_REQUEST['classic-editor'] ) || $settings['editor'] === 'classic' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
646 $url = add_query_arg( 'classic-editor', '', $url );
647 }
648
649 return $url;
650 }
651
652 public static function add_meta_box( $post_type, $post ) {
653 $editors = self::get_enabled_editors_for_post( $post );
654
655 if ( ! $editors['block_editor'] || ! $editors['classic_editor'] ) {
656 // Editors cannot be switched.
657 return;
658 }
659
660 $id = 'classic-editor-switch-editor';
661 $title = __( 'Editor', 'classic-editor' );
662 $callback = array( __CLASS__, 'do_meta_box' );
663 $args = array(
664 '__back_compat_meta_box' => true,
665 );
666
667 add_meta_box( $id, $title, $callback, null, 'side', 'default', $args );
668 }
669
670 public static function do_meta_box( $post ) {
671 $edit_url = get_edit_post_link( $post->ID, 'raw' );
672
673 // Switching to block editor.
674 $edit_url = remove_query_arg( 'classic-editor', $edit_url );
675 // Forget the previous value when going to a specific editor.
676 $edit_url = add_query_arg( 'classic-editor__forget', '', $edit_url );
677
678 ?>
679 <p style="margin: 1em 0;">
680 <a href="<?php echo esc_url( $edit_url ); ?>"><?php _e( 'Switch to block editor', 'classic-editor' ); ?></a>
681 </p>
682 <?php
683 }
684
685 public static function enqueue_block_editor_scripts() {
686 // get_enabled_editors_for_post() needs a WP_Post or post_ID.
687 if ( empty( $GLOBALS['post'] ) ) {
688 return;
689 }
690
691 $editors = self::get_enabled_editors_for_post( $GLOBALS['post'] );
692
693 if ( ! $editors['classic_editor'] ) {
694 // Editor cannot be switched.
695 return;
696 }
697
698 wp_enqueue_script(
699 'classic-editor-plugin',
700 plugins_url( 'js/block-editor-plugin.js', __FILE__ ),
701 array( 'wp-element', 'wp-components', 'lodash' ),
702 '1.4',
703 true
704 );
705
706 wp_localize_script(
707 'classic-editor-plugin',
708 'classicEditorPluginL10n',
709 array( 'linkText' => __( 'Switch to classic editor', 'classic-editor' ) )
710 );
711 }
712
713 /**
714 * Add a link to the settings on the Plugins screen.
715 */
716 public static function add_settings_link( $links, $file ) {
717 $settings = self::get_settings();
718
719 if ( $file === 'classic-editor/classic-editor.php' && ! $settings['hide-settings-ui'] && current_user_can( 'manage_options' ) ) {
720 if ( current_filter() === 'plugin_action_links' ) {
721 $url = admin_url( 'options-writing.php#classic-editor-options' );
722 } else {
723 $url = admin_url( '/network/settings.php#classic-editor-options' );
724 }
725
726 // Prevent warnings in PHP 7.0+ when a plugin uses this filter incorrectly.
727 $links = (array) $links;
728 $links[] = sprintf( '<a href="%s">%s</a>', $url, __( 'Settings', 'classic-editor' ) );
729 }
730
731 return $links;
732 }
733
734 private static function can_edit_post_type( $post_type ) {
735 $can_edit = false;
736
737 if ( function_exists( 'gutenberg_can_edit_post_type' ) ) {
738 $can_edit = gutenberg_can_edit_post_type( $post_type );
739 } elseif ( function_exists( 'use_block_editor_for_post_type' ) ) {
740 $can_edit = use_block_editor_for_post_type( $post_type );
741 }
742
743 return $can_edit;
744 }
745
746 /**
747 * Checks which editors are enabled for the post type.
748 *
749 * @param string $post_type The post type.
750 * @return array Associative array of the editors and whether they are enabled for the post type.
751 */
752 private static function get_enabled_editors_for_post_type( $post_type ) {
753 if ( isset( self::$supported_post_types[ $post_type ] ) ) {
754 return self::$supported_post_types[ $post_type ];
755 }
756
757 $classic_editor = post_type_supports( $post_type, 'editor' );
758 $block_editor = self::can_edit_post_type( $post_type );
759
760 $editors = array(
761 'classic_editor' => $classic_editor,
762 'block_editor' => $block_editor,
763 );
764
765 /**
766 * Filters the editors that are enabled for the post type.
767 *
768 * @param array $editors Associative array of the editors and whether they are enabled for the post type.
769 * @param string $post_type The post type.
770 */
771 $editors = apply_filters( 'classic_editor_enabled_editors_for_post_type', $editors, $post_type );
772 self::$supported_post_types[ $post_type ] = $editors;
773
774 return $editors;
775 }
776
777 /**
778 * Checks which editors are enabled for the post.
779 *
780 * @param WP_Post $post The post object.
781 * @return array Associative array of the editors and whether they are enabled for the post.
782 */
783 private static function get_enabled_editors_for_post( $post ) {
784 $post_type = get_post_type( $post );
785
786 if ( ! $post_type ) {
787 return array(
788 'classic_editor' => false,
789 'block_editor' => false,
790 );
791 }
792
793 $editors = self::get_enabled_editors_for_post_type( $post_type );
794
795 /**
796 * Filters the editors that are enabled for the post.
797 *
798 * @param array $editors Associative array of the editors and whether they are enabled for the post.
799 * @param WP_Post $post The post object.
800 */
801 return apply_filters( 'classic_editor_enabled_editors_for_post', $editors, $post );
802 }
803
804 /**
805 * Adds links to the post/page screens to edit any post or page in
806 * the classic editor or block editor.
807 *
808 * @param array $actions Post actions.
809 * @param WP_Post $post Edited post.
810 * @return array Updated post actions.
811 */
812 public static function add_edit_links( $actions, $post ) {
813 // This is in Gutenberg, don't duplicate it.
814 if ( array_key_exists( 'classic', $actions ) ) {
815 unset( $actions['classic'] );
816 }
817
818 if ( ! array_key_exists( 'edit', $actions ) ) {
819 return $actions;
820 }
821
822 $edit_url = get_edit_post_link( $post->ID, 'raw' );
823
824 if ( ! $edit_url ) {
825 return $actions;
826 }
827
828 $editors = self::get_enabled_editors_for_post( $post );
829
830 // Do not show the links if only one editor is available.
831 if ( ! $editors['classic_editor'] || ! $editors['block_editor'] ) {
832 return $actions;
833 }
834
835 // Forget the previous value when going to a specific editor.
836 $edit_url = add_query_arg( 'classic-editor__forget', '', $edit_url );
837
838 // Build the edit actions. See also: WP_Posts_List_Table::handle_row_actions().
839 $title = _draft_or_post_title( $post->ID );
840
841 // Link to the block editor.
842 $url = remove_query_arg( 'classic-editor', $edit_url );
843 $text = _x( 'Edit (block editor)', 'Editor Name', 'classic-editor' );
844 /* translators: %s: post title */
845 $label = sprintf( __( 'Edit &#8220;%s&#8221; in the block editor', 'classic-editor' ), $title );
846 $edit_block = sprintf( '<a href="%s" aria-label="%s">%s</a>', esc_url( $url ), esc_attr( $label ), $text );
847
848 // Link to the classic editor.
849 $url = add_query_arg( 'classic-editor', '', $edit_url );
850 $text = _x( 'Edit (classic editor)', 'Editor Name', 'classic-editor' );
851 /* translators: %s: post title */
852 $label = sprintf( __( 'Edit &#8220;%s&#8221; in the classic editor', 'classic-editor' ), $title );
853 $edit_classic = sprintf( '<a href="%s" aria-label="%s">%s</a>', esc_url( $url ), esc_attr( $label ), $text );
854
855 $edit_actions = array(
856 'classic-editor-block' => $edit_block,
857 'classic-editor-classic' => $edit_classic,
858 );
859
860 // Insert the new Edit actions instead of the Edit action.
861 $edit_offset = array_search( 'edit', array_keys( $actions ), true );
862 array_splice( $actions, $edit_offset, 1, $edit_actions );
863
864 return $actions;
865 }
866
867 /**
868 * Show the editor that will be used in a "post state" in the Posts list table.
869 */
870 public static function add_post_state( $post_states, $post ) {
871 if ( get_post_status( $post ) === 'trash' ) {
872 return $post_states;
873 }
874
875 $editors = self::get_enabled_editors_for_post( $post );
876
877 if ( ! $editors['classic_editor'] && ! $editors['block_editor'] ) {
878 return $post_states;
879 } elseif ( $editors['classic_editor'] && ! $editors['block_editor'] ) {
880 // Forced to classic editor.
881 $state = '<span class="classic-editor-forced-state">' . _x( 'classic editor', 'Editor Name', 'classic-editor' ) . '</span>';
882 } elseif ( ! $editors['classic_editor'] && $editors['block_editor'] ) {
883 // Forced to block editor.
884 $state = '<span class="classic-editor-forced-state">' . _x( 'block editor', 'Editor Name', 'classic-editor' ) . '</span>';
885 } else {
886 $last_editor = get_post_meta( $post->ID, 'classic-editor-remember', true );
887
888 if ( $last_editor ) {
889 $is_classic = ( $last_editor === 'classic-editor' );
890 } elseif ( ! empty( $post->post_content ) ) {
891 $is_classic = ! self::has_blocks( $post->post_content );
892 } else {
893 $settings = self::get_settings();
894 $is_classic = ( $settings['editor'] === 'classic' );
895 }
896
897 $state = $is_classic ? _x( 'Classic editor', 'Editor Name', 'classic-editor' ) : _x( 'Block editor', 'Editor Name', 'classic-editor' );
898 }
899
900 // Fix PHP 7+ warnings if another plugin returns unexpected type.
901 $post_states = (array) $post_states;
902 $post_states['classic-editor-plugin'] = $state;
903
904 return $post_states;
905 }
906
907 public static function add_edit_php_inline_style() {
908 ?>
909 <style>
910 .classic-editor-forced-state {
911 font-style: italic;
912 font-weight: 400;
913 color: #72777c;
914 font-size: small;
915 }
916 </style>
917 <?php
918 }
919
920 public static function on_admin_init() {
921 global $pagenow;
922
923 if ( $pagenow !== 'post.php' ) {
924 return;
925 }
926
927 $settings = self::get_settings();
928 $post_id = self::get_edited_post_id();
929
930 if ( $post_id && ( $settings['editor'] === 'classic' || self::is_classic( $post_id ) ) ) {
931 // Move the Privacy Policy help notice back under the title field.
932 remove_action( 'admin_notices', array( 'WP_Privacy_Policy_Content', 'notice' ) );
933 add_action( 'edit_form_after_title', array( 'WP_Privacy_Policy_Content', 'notice' ) );
934 }
935 }
936
937 // Need to support WP < 5.0
938 private static function has_blocks( $post = null ) {
939 if ( ! is_string( $post ) ) {
940 $wp_post = get_post( $post );
941
942 if ( $wp_post instanceof WP_Post ) {
943 $post = $wp_post->post_content;
944 }
945 }
946
947 return false !== strpos( (string) $post, '<!-- wp:' );
948 }
949
950 /**
951 * Set defaults on activation.
952 */
953 public static function activate() {
954 register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
955
956 if ( is_multisite() ) {
957 add_network_option( null, 'classic-editor-replace', 'classic' );
958 add_network_option( null, 'classic-editor-allow-sites', 'disallow' );
959 }
960
961 add_option( 'classic-editor-replace', 'classic' );
962 add_option( 'classic-editor-allow-users', 'disallow' );
963 }
964
965 /**
966 * Delete the options on uninstall.
967 */
968 public static function uninstall() {
969 if ( is_multisite() ) {
970 delete_network_option( null, 'classic-editor-replace' );
971 delete_network_option( null, 'classic-editor-allow-sites' );
972 }
973
974 delete_option( 'classic-editor-replace' );
975 delete_option( 'classic-editor-allow-users' );
976 }
977 }
978
979 add_action( 'plugins_loaded', array( 'Classic_Editor', 'init_actions' ) );
980
981 endif;
982