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