plug_classic.php 32 KB

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