Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/classic-editor/classic-editor.php

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