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