classic-editor.php 33 KB

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