Changeset 1983751 for classic-editor
- Timestamp:
- 12/01/2018 05:17:34 PM (7 years ago)
- Location:
- classic-editor/trunk
- Files:
-
- 2 added
- 1 edited
-
classic-editor.php (modified) (22 diffs)
-
js (added)
-
js/block-editor-plugin.js (added)
Legend:
- Unmodified
- Added
- Removed
-
classic-editor/trunk/classic-editor.php
r1979544 r1983751 29 29 if ( ! class_exists( 'Classic_Editor' ) ) : 30 30 class Classic_Editor { 31 const plugin_version = 1.0; 32 private static $settings; 31 33 32 34 private function __construct() {} 33 35 34 36 public static function init_actions() { 35 $gutenberg = false; 36 $block_editor = false; 37 $post_id = 0; 37 $supported_wp_version = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' ); 38 38 39 39 register_activation_hook( __FILE__, array( __CLASS__, 'activate' ) ); 40 40 register_deactivation_hook( __FILE__, array( __CLASS__, 'deactivate' ) ); 41 41 42 // Always remove the "Try Gutenberg" dashboard widget. See https://core.trac.wordpress.org/ticket/44635.43 remove_action( 'try_gutenberg_panel', 'wp_try_gutenberg_panel' );44 45 42 // Show warning on the post-upgrade screen (about.php). 46 add_action( 'all_admin_notices', array( __CLASS__, 'post_upgrade_notice' ) ); 47 48 if ( has_filter( 'replace_editor', 'gutenberg_init' ) ) { 49 // Gutenberg is installed and activated. 50 $gutenberg = true; 51 $post_id = self::get_edited_post_id(); 52 } 53 54 if ( version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' ) ) { 55 // Block editor. 56 $block_editor = true; 57 } 58 59 if ( ! $gutenberg && ! $block_editor ) { 60 return; // Nothing to do :) 61 } 43 add_action( 'all_admin_notices', array( __CLASS__, 'notice_after_upgrade' ) ); 44 45 // Move the Privacy Page notice back under the title. 46 add_action( 'admin_init', array( __CLASS__, 'on_admin_init' ) ); 62 47 63 48 $settings = self::get_settings(); 64 49 65 50 if ( ! $settings['hide-settings-ui'] ) { 66 // Show the plugin's settings, and the link to them in the plugins list table.51 // Show the plugin's admin settings, and the link to them in the plugins list table. 67 52 add_filter( 'plugin_action_links', array( __CLASS__, 'add_settings_link' ), 10, 2 ); 68 53 add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); 69 54 70 // User settings. 71 add_action( 'personal_options_update', array( __CLASS__, 'save_user_settings' ) ); 72 add_action( 'profile_personal_options', array( __CLASS__, 'settings' ) ); 73 } 74 75 if ( $block_editor && $settings['replace'] ) { 55 if ( $supported_wp_version && $settings['allow-users'] ) { 56 // User settings. 57 add_action( 'personal_options_update', array( __CLASS__, 'save_user_settings' ) ); 58 add_action( 'profile_personal_options', array( __CLASS__, 'user_settings' ) ); 59 } 60 } 61 62 if ( ! $supported_wp_version ) { 63 // TODO: Should we also show a notice that the settings will apply after WordPress is upgraded to 5.0+? 64 return; 65 } 66 67 if ( $settings['editor'] === 'block' && ! $settings['allow-users'] ) { 68 return; // Nothing else to do :) 69 } elseif ( $settings['editor'] === 'classic' && ! $settings['allow-users'] ) { 76 70 // Consider disabling other block editor functionality. 77 71 add_filter( 'use_block_editor_for_post_type', '__return_false', 100 ); 78 } 79 80 if ( $gutenberg && ( $settings['replace'] || self::is_classic( $post_id ) ) ) { 81 // gutenberg.php 82 remove_action( 'admin_menu', 'gutenberg_menu' ); 83 remove_action( 'admin_notices', 'gutenberg_build_files_notice' ); 84 remove_action( 'admin_notices', 'gutenberg_wordpress_version_notice' ); 85 remove_action( 'admin_init', 'gutenberg_redirect_demo' ); 86 87 remove_filter( 'replace_editor', 'gutenberg_init' ); 88 89 // lib/client-assets.php 90 remove_action( 'wp_enqueue_scripts', 'gutenberg_register_scripts_and_styles', 5 ); 91 remove_action( 'admin_enqueue_scripts', 'gutenberg_register_scripts_and_styles', 5 ); 92 remove_action( 'wp_enqueue_scripts', 'gutenberg_common_scripts_and_styles' ); 93 remove_action( 'admin_enqueue_scripts', 'gutenberg_common_scripts_and_styles' ); 94 95 // lib/compat.php 96 remove_filter( 'wp_refresh_nonces', 'gutenberg_add_rest_nonce_to_heartbeat_response_headers' ); 97 98 // lib/rest-api.php 99 remove_action( 'rest_api_init', 'gutenberg_register_rest_routes' ); 100 remove_action( 'rest_api_init', 'gutenberg_add_taxonomy_visibility_field' ); 101 102 remove_filter( 'rest_request_after_callbacks', 'gutenberg_filter_oembed_result' ); 103 remove_filter( 'registered_post_type', 'gutenberg_register_post_prepare_functions' ); 104 remove_filter( 'register_post_type_args', 'gutenberg_filter_post_type_labels' ); 105 106 // lib/meta-box-partial-page.php 107 remove_action( 'do_meta_boxes', 'gutenberg_meta_box_save', 1000 ); 108 remove_action( 'submitpost_box', 'gutenberg_intercept_meta_box_render' ); 109 remove_action( 'submitpage_box', 'gutenberg_intercept_meta_box_render' ); 110 remove_action( 'edit_page_form', 'gutenberg_intercept_meta_box_render' ); 111 remove_action( 'edit_form_advanced', 'gutenberg_intercept_meta_box_render' ); 112 113 remove_filter( 'redirect_post_location', 'gutenberg_meta_box_save_redirect' ); 114 remove_filter( 'filter_gutenberg_meta_boxes', 'gutenberg_filter_meta_boxes' ); 115 } 116 117 if ( $gutenberg && $settings['replace'] ) { 118 // gutenberg.php 119 remove_action( 'admin_init', 'gutenberg_add_edit_link_filters' ); 120 remove_action( 'admin_print_scripts-edit.php', 'gutenberg_replace_default_add_new_button' ); 121 122 remove_filter( 'body_class', 'gutenberg_add_responsive_body_class' ); 123 remove_filter( 'admin_url', 'gutenberg_modify_add_new_button_url' ); 124 125 // Keep 126 // remove_filter( 'wp_kses_allowed_html', 'gutenberg_kses_allowedtags', 10, 2 ); // not needed in 5.0 127 // remove_filter( 'bulk_actions-edit-wp_block', 'gutenberg_block_bulk_actions' ); 128 129 // lib/compat.php 130 remove_action( 'admin_enqueue_scripts', 'gutenberg_check_if_classic_needs_warning_about_blocks' ); 131 132 // lib/register.php 133 remove_action( 'edit_form_top', 'gutenberg_remember_classic_editor_when_saving_posts' ); 134 135 remove_filter( 'redirect_post_location', 'gutenberg_redirect_to_classic_editor_when_saving_posts' ); 136 remove_filter( 'get_edit_post_link', 'gutenberg_revisions_link_to_editor' ); 137 remove_filter( 'wp_prepare_revision_for_js', 'gutenberg_revisions_restore' ); 138 remove_filter( 'display_post_states', 'gutenberg_add_gutenberg_post_state' ); 139 140 // lib/plugin-compat.php 141 remove_filter( 'rest_pre_insert_post', 'gutenberg_remove_wpcom_markdown_support' ); 142 143 // Keep 144 145 // lib/blocks.php 146 // remove_filter( 'the_content', 'do_blocks', 9 ); 147 148 // Continue to disable wpautop inside TinyMCE for posts that were started in Gutenberg. 149 // remove_filter( 'wp_editor_settings', 'gutenberg_disable_editor_settings_wpautop' ); 150 151 // Keep the tweaks to the PHP wpautop. 152 // add_filter( 'the_content', 'wpautop' ); 153 // remove_filter( 'the_content', 'gutenberg_wpautop', 8 ); 154 155 // remove_action( 'init', 'gutenberg_register_post_types' ); 156 } 157 158 if ( ! $settings['replace'] ) { 159 // Menus 160 add_action( 'admin_menu', array( __CLASS__, 'add_submenus' ) ); 161 72 } else { 162 73 // Row actions (edit.php) 163 74 add_filter( 'page_row_actions', array( __CLASS__, 'add_edit_links' ), 15, 2 ); … … 168 79 add_filter( 'redirect_post_location', array( __CLASS__, 'redirect_location' ) ); 169 80 add_action( 'edit_form_top', array( __CLASS__, 'add_field' ) ); 81 add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_box' ), 10, 2 ); 82 83 // TODO: needs https://github.com/WordPress/gutenberg/pull/12309 84 // add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_scripts' ) ); 170 85 171 86 if ( $settings['remember'] ) { … … 176 91 } 177 92 178 private static function get_settings( ) {93 private static function get_settings( $refresh = 'no' ) { 179 94 /** 180 95 * Can be used to override the plugin's settings and hide the settings UI. 181 96 * 182 * Has to return an associative array with (up to) three keys with boolean values.97 * Has to return an associative array with three keys. 183 98 * The defaults are: 184 * ' replace' => true,99 * 'editor' => 'classic', // Accepted values: 'classic', 'block'. 185 100 * 'remember' => false, 186 101 * 'allow_users' => true, … … 192 107 if ( is_array( $settings ) ) { 193 108 // Normalize... 109 $editor = 'classic'; 110 111 if ( isset( $settings['editor'] ) && $settings['editor'] === 'block' ) { 112 $editor = 'block'; 113 } 114 194 115 return array( 195 ' replace' => ! empty( $settings['replace'] ),196 'remember' => ( empty( $settings['replace'] ) &&! empty( $settings['remember'] ) ),197 'allow-users' => ( ! isset( $settings['allow-users'] ) || $settings['allow-users'] ), 116 'editor' => $editor, 117 'remember' => ( ! empty( $settings['remember'] ) ), 118 'allow-users' => ( ! isset( $settings['allow-users'] ) || $settings['allow-users'] ), // Allow by default. 198 119 'hide-settings-ui' => true, 199 120 ); 200 121 } 201 122 202 $use_defaults = true; 203 $replace = true; 204 $remember = false; 205 206 if ( ( ! isset( $GLOBALS['pagenow'] ) || $GLOBALS['pagenow'] !== 'options-writing.php' ) && get_option( 'classic-editor-allow-users' ) !== 'disallow' ) { 207 $user_id = 0; // Allow admins to set a user's options? 208 $option = get_user_option( 'classic-editor-settings', $user_id ); 209 210 if ( ! empty( $option ) ) { 211 $use_defaults = false; 212 213 if ( $option === 'remember' ) { 214 $remember = true; 215 $replace = false; 216 } else { 217 $remember = false; 218 $replace = ( $option !== 'no-replace' ); 123 if ( ! empty( self::$settings ) && $refresh === 'no' ) { 124 return self::$settings; 125 } 126 127 $allow_users = ( get_option( 'classic-editor-allow-users' ) !== 'disallow' ); 128 $remember = ( get_option( 'classic-editor-remember' ) === 'remember' ); 129 $option = get_option( 'classic-editor-replace' ); 130 131 // Normalize old options. 132 if ( $option === 'block' || $option === 'no-replace' ) { 133 $editor = 'block'; 134 } else { 135 // `empty( $option ) || $option === 'classic' || $option === 'replace'`. 136 $editor = 'classic'; 137 } 138 139 // Override the defaults withthe user options. 140 if ( ( ! isset( $GLOBALS['pagenow'] ) || $GLOBALS['pagenow'] !== 'options-writing.php' ) && $allow_users ) { 141 $user_options = get_user_option( 'classic-editor-settings' ); 142 143 if ( is_array( $user_options ) ) { 144 if ( isset( $user_options['remember'] ) ) { 145 $remember = $user_options['remember'] === 'remember'; 146 } 147 148 if ( isset( $user_options['editor'] ) && ( $user_options['editor'] === 'block' || $user_options['editor'] === 'classic' ) ) { 149 $editor = $user_options['editor']; 219 150 } 220 151 } 221 152 } 222 153 223 if ( $use_defaults ) { 224 $replace = get_option( 'classic-editor-replace' ) !== 'no-replace'; 225 $remember = get_option( 'classic-editor-remember' ) === 'remember'; 226 } 227 228 return array( 229 'replace' => $replace, 154 self::$settings = array( 155 'editor' => $editor, 230 156 'remember' => $remember, 231 157 'hide-settings-ui' => false, 232 'allow-users' => get_option( 'classic-editor-allow-users' ) !== 'disallow',158 'allow-users' => $allow_users, 233 159 ); 160 161 return self::$settings; 234 162 } 235 163 236 164 private static function is_classic( $post_id = 0 ) { 165 if ( ! $post_id ) { 166 $post_id = self::get_edited_post_id(); 167 } 168 237 169 if ( $post_id ) { 238 170 $settings = self::get_settings(); … … 256 188 } 257 189 190 /** 191 * Early get the edited post ID when loading the Edit Post screen. 192 */ 258 193 private static function get_edited_post_id() { 259 194 if ( … … 273 208 // Add an option to Settings -> Writing 274 209 register_setting( 'writing', 'classic-editor-replace', array( 275 'sanitize_callback' => array( __CLASS__, 'validate_option s' ),210 'sanitize_callback' => array( __CLASS__, 'validate_option_editor' ), 276 211 ) ); 277 212 278 213 register_setting( 'writing', 'classic-editor-remember', array( 279 'sanitize_callback' => array( __CLASS__, 'validate_option s' ),214 'sanitize_callback' => array( __CLASS__, 'validate_option_remember' ), 280 215 ) ); 281 216 282 217 register_setting( 'writing', 'classic-editor-allow-users', array( 283 'sanitize_callback' => array( __CLASS__, 'validate_option s_allow_users' ),218 'sanitize_callback' => array( __CLASS__, 'validate_option_allow_users' ), 284 219 ) ); 285 220 … … 288 223 ) ); 289 224 290 add_settings_field( 'classic-editor', __( 'Classic Editor settings', 'classic-editor' ), array( __CLASS__, 'settings' ), 'writing' ); 225 $headint_1 = __( 'Default editor for all users', 'classic-editor' ); 226 $heading_2 = __( 'Open the last editor used for each post', 'classic-editor' ); 227 $heading_3 = __( 'Allow users to switch editors', 'classic-editor' ); 228 229 add_settings_field( 'classic-editor-1', $headint_1, array( __CLASS__, 'settings_1' ), 'writing' ); 230 add_settings_field( 'classic-editor-2', $heading_2, array( __CLASS__, 'settings_2' ), 'writing' ); 231 add_settings_field( 'classic-editor-3', $heading_3, array( __CLASS__, 'settings_3' ), 'writing' ); 291 232 } 292 233 … … 303 244 } 304 245 305 $value = self::validate_options( $_POST['classic-editor-replace'] ); 306 307 if ( $value === 'no-replace' && $_POST['classic-editor-remember'] === 'remember' ) { 308 $value = 'remember'; 309 310 } 311 312 update_user_option( $user_id, 'classic-editor-settings', $value ); 246 $editor = self::validate_option_editor( $_POST['classic-editor-replace'] ); 247 $remember = self::validate_option_remember( $_POST['classic-editor-remember'] ); 248 249 $options = array( 250 'editor' => $editor, 251 'remember' => $remember, 252 ); 253 254 update_user_option( $user_id, 'classic-editor-settings', $options ); 313 255 } 314 256 } … … 317 259 * Validate 318 260 */ 319 public static function validate_options( $value ) { 320 if ( $value === 'no-replace' || $value === 'remember' ) { 321 return $value; 322 } 323 324 return 'replace'; 325 } 326 327 public static function validate_options_allow_users( $value ) { 261 public static function validate_option_editor( $value ) { 262 if ( $value === 'block' ) { 263 return 'block'; 264 } 265 266 return 'classic'; 267 } 268 269 public static function validate_option_remember( $value ) { 270 if ( $value === 'remember' ) { 271 return 'remember'; 272 } 273 274 return 'no-remember'; 275 } 276 277 public static function validate_option_allow_users( $value ) { 328 278 if ( $value === 'allow' ) { 329 279 return 'allow'; … … 333 283 } 334 284 335 /** 336 * Output HTML for the settings. 337 */ 338 public static function settings() { 339 global $user_can_edit; 340 $settings = self::get_settings(); 285 public static function settings_1() { 286 $settings = self::get_settings( 'refresh' ); 341 287 342 288 if ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE ) { 343 if ( ! $user_can_edit || ! $settings['allow-users'] ) { 344 // Show these options to "author" and above, same as the "Disable the Visual editor when writing" checkbox. 345 return; 346 } 347 348 $site_wide = false; 289 $label = __( 'Select editor.', 'classic-editor' ); 349 290 } else { 350 $site_wide = true; 351 } 352 353 354 $disabled = $settings['replace'] ? ' disabled' : ''; 355 356 if ( ! $site_wide ) { 357 ?> 358 <table class="form-table"> 359 <tr> 360 <th scope="row"><?php _e( 'Classic Editor settings', 'classic-editor' ); ?></th> 361 <td> 362 <?php 363 364 wp_nonce_field( 'allow-user-settings', 'classic-editor-user-settings' ); 291 $label = __( 'Select default editor for all users.', 'classic-editor' ); 365 292 } 366 293 367 294 ?> 368 <div id="classic-editor-options" style="margin: 0;"> 295 <div class="classic-editor-options"> 296 <label for="classic-editor-replace" class="screen-reader-text"> 297 <?php echo $label; ?> 298 </label> 299 <select name="classic-editor-replace" id="classic-editor-replace"> 300 <option value="classic"> 301 <?php _e( 'Classic Editor', 'classic-editor' ); ?> 302 </option> 303 <option value="block"<?php if ( $settings['editor'] === 'block' ) echo ' selected'; ?>> 304 <?php _e( 'Block Editor', 'classic-editor' ); ?> 305 </option> 306 </select> 307 </div> 369 308 <?php 370 371 if ( $site_wide ) { 372 ?>373 <h4 style="margin: 0.4em 0 0.7em;"><?php _e( 'Default site-wide options', 'classic-editor' ); ?></h4>374 <?php375 }309 } 310 311 public static function settings_2() { 312 $settings = self::get_settings(); 313 $disabled = ! $settings['allow-users'] ? ' disabled' : ''; 314 $padding = is_rtl() ? 'padding-left: 1em;' : 'padding-right: 1em;'; 376 315 377 316 ?> 378 <p> 379 <input type="radio" name="classic-editor-replace" id="classic-editor-replace" value="replace"<?php if ( $settings['replace'] ) echo ' checked'; ?> /> 380 <label for="classic-editor-replace"> 381 <?php _e( 'Replace the Block editor with the Classic editor.', 'classic-editor' ); ?> 382 </label> 383 </p> 384 385 <p> 386 <input type="radio" name="classic-editor-replace" id="classic-editor-no-replace" value="no-replace"<?php if ( ! $settings['replace'] ) echo ' checked'; ?> /> 387 <label for="classic-editor-no-replace"> 388 <?php _e( 'Use the Block editor by default and include optional links back to the Classic editor.', 'classic-editor' ); ?> 389 </label> 390 </p> 391 392 <p> 393 <input type="checkbox" name="classic-editor-remember" id="classic-editor-remember" value="remember"<?php echo $disabled; if ( $settings['remember'] ) echo ' checked'; ?> /> 394 <label for="classic-editor-remember"> 395 <?php _e( 'Remember whether the Block or the Classic editor was used for each post and open the same editor next time.', 'classic-editor' ); ?> 396 </label> 397 </p> 398 399 <?php 400 401 if ( $site_wide ) { 402 ?> 403 <h4 style="margin-bottom: 0.7em;"><?php _e( 'Admin options', 'classic-editor' ); ?></h4> 404 <p class="help"><?php _e( 'When enabled each user can set their personal preferences on the User Profile screen.', 'classic-editor' ); ?></p> 405 <p> 406 <input type="checkbox" name="classic-editor-allow-users" id="classic-editor-allow-users" value="allow"<?php if ( $settings['allow-users'] ) echo ' checked'; ?> /> 407 <label for="classic-editor-allow-users"> 408 <?php _e( 'Let each user choose their settings.', 'classic-editor' ); ?> 317 <div class="classic-editor-options"> 318 <label style="<?php echo $padding ?>"> 319 <input type="radio" name="classic-editor-remember" id="classic-editor-remember" value="remember"<?php echo $disabled; if ( ! $disabled && $settings['remember'] ) echo ' checked'; ?> /> 320 <?php _e( 'Yes', 'classic-editor' ); ?> 409 321 </label> 410 </p> 411 <?php 412 } 413 414 ?> 322 323 <label style="<?php echo $padding ?>"> 324 <input type="radio" name="classic-editor-remember" id="classic-editor-no-remember" value="no-remember"<?php echo $disabled; if ( ! $disabled && ! $settings['remember'] ) echo ' checked'; ?> /> 325 <?php _e( 'No', 'classic-editor' ); ?> 326 </label> 327 </div> 415 328 <script> 416 329 jQuery( 'document' ).ready( function( $ ) { 417 var checkbox = $( '#classic-editor-remember' );418 var isChecked = checkbox.prop( 'checked' );419 420 330 if ( window.location.hash === '#classic-editor-options' ) { 421 $( ' #classic-editor-options' ).closest( 'td' ).addClass( 'highlight' );331 $( '.classic-editor-options' ).closest( 'td' ).addClass( 'highlight' ); 422 332 } 423 $( 'input[type="radio"][name="classic-editor-replace"]' ).on( 'change', function( event ) { 424 if ( $( event.target ).val() === 'replace' ) { 425 checkbox.prop({ checked: false, disabled: true }); 333 334 $( 'input[name="classic-editor-allow-users"]' ).on( 'change', function() { 335 if ( $( this ).val() === 'allow' ) { 336 $( 'input[name="classic-editor-remember"]' ).prop({ disabled: false }); 426 337 } else { 427 checkbox.prop({ checked: isChecked, disabled: false });338 $( 'input[name="classic-editor-remember"]' ).prop({ checked: false, disabled: true }); 428 339 } 429 340 }); 430 341 } ); 431 342 </script> 343 <?php 344 } 345 346 public static function settings_3() { 347 $settings = self::get_settings( 'refresh' ); 348 $padding = is_rtl() ? 'padding-left: 1em;' : 'padding-right: 1em;'; 349 350 ?> 351 <div class="classic-editor-options"> 352 <label style="<?php echo $padding ?>"> 353 <input type="radio" name="classic-editor-allow-users" value="allow"<?php if ( $settings['allow-users'] ) echo ' checked'; ?> /> 354 <?php _e( 'Yes', 'classic-editor' ); ?> 355 </label> 356 357 <label style="<?php echo $padding ?>"> 358 <input type="radio" name="classic-editor-allow-users" value="no-allow"<?php if ( ! $settings['allow-users'] ) echo ' checked'; ?> /> 359 <?php _e( 'No', 'classic-editor' ); ?> 360 </label> 432 361 </div> 433 362 <?php 434 435 if ( ! $site_wide ) { 436 ?> 437 </td> 363 } 364 365 /** 366 * Shown on the Profile page when allowed by admin. 367 */ 368 public static function user_settings() { 369 global $user_can_edit; 370 $settings = self::get_settings( 'update' ); 371 372 if ( 373 ! defined( 'IS_PROFILE_PAGE' ) || 374 ! IS_PROFILE_PAGE || 375 ! $user_can_edit || 376 ! $settings['allow-users'] 377 ) { 378 return; 379 } 380 381 ?> 382 <table class="form-table"> 383 <tr> 384 <th scope="row"><?php _e( 'Editor', 'classic-editor' ); ?></th> 385 <td> 386 <?php wp_nonce_field( 'allow-user-settings', 'classic-editor-user-settings' ); ?> 387 <?php self::settings_1(); ?> 388 </td> 438 389 </tr> 439 </table> 440 <?php 441 } 442 } 443 444 public static function post_upgrade_notice() { 390 <tr> 391 <th scope="row"><?php _e( 'Open the last editor used for each post', 'classic-editor' ); ?></th> 392 <td> 393 <?php self::settings_2(); ?> 394 </td> 395 </tr> 396 </table> 397 <?php 398 } 399 400 public static function notice_after_upgrade() { 445 401 global $pagenow; 446 402 $settings = self::get_settings(); 447 403 448 if ( $pagenow !== 'about.php' || $settings['hide-settings-ui'] || ! $settings['replace']) {404 if ( $pagenow !== 'about.php' || $settings['hide-settings-ui'] || $settings['editor'] !== 'classic' ) { 449 405 // No need to show when the settings are preset from another plugin or when not replacing the Block Editor. 450 406 return; … … 454 410 455 411 if ( $settings['allow-users'] && current_user_can( 'edit_posts' ) ) { 456 $message .= ' ' . sprintf( __( 'Change the %1$sC alssic Editor settings%2$s on your User Profile page.', 'classic-editor' ), '<a href="profile.php#classic-editor-options">', '</a>' );412 $message .= ' ' . sprintf( __( 'Change the %1$sClassic Editor settings%2$s on your User Profile page.', 'classic-editor' ), '<a href="profile.php#classic-editor-options">', '</a>' ); 457 413 } elseif ( current_user_can( 'manage_options' ) ) { 458 $message .= ' ' . sprintf( __( 'Change the %1$sC alssic Editor settings%2$s.', 'classic-editor' ), '<a href="options-writing.php#classic-editor-options">', '</a>' );414 $message .= ' ' . sprintf( __( 'Change the %1$sClassic Editor settings%2$s.', 'classic-editor' ), '<a href="options-writing.php#classic-editor-options">', '</a>' ); 459 415 } 460 416 461 417 ?> 462 <div id="message" class="error notice" style="display: block !important"> <p>463 < ?php echo $message; ?>464 </ p></div>418 <div id="message" class="error notice" style="display: block !important"> 419 <p><?php echo $message; ?></p> 420 </div> 465 421 <?php 466 422 } … … 477 433 478 434 /** 479 * Remember when the Classic editor was used to edit a post.435 * Remember when the Classic Editor was used to edit a post. 480 436 */ 481 437 public static function remember_classic( $post ) { … … 485 441 } 486 442 443 /** 444 * Remember when the Block Editor was used to edit a post. 445 */ 487 446 public static function remember_block_editor( $editor_settings, $post ) { 488 447 if ( ! empty( $post->ID ) ) { … … 494 453 495 454 private static function remember( $post_id, $editor ) { 496 if ( use_block_editor_for_post_type( get_post_type( $post_id ) ) ) { 497 if ( get_post_meta( $post_id, 'classic-editor-rememebr', true ) !== $editor ) { 498 update_post_meta( $post_id, 'classic-editor-rememebr', $editor ); 499 } 500 } 501 } 502 455 if ( 456 use_block_editor_for_post_type( get_post_type( $post_id ) ) && 457 get_post_meta( $post_id, 'classic-editor-rememebr', true ) !== $editor 458 ) { 459 update_post_meta( $post_id, 'classic-editor-rememebr', $editor ); 460 } 461 } 462 463 /** 464 * Uses the `use_block_editor_for_post` filter. 465 * Passes through `$which_editor` for Block Editor (it's sets to `true` but may be changed by another plugin). 466 * Returns `false` for Classic Editor. 467 */ 503 468 public static function choose_editor( $which_editor, $post ) { 504 // Open the Block editor when no $post and for "Add New" links. 505 if ( empty( $post->ID ) || ( $post->post_status === 'auto-draft' && ! self::is_classic() ) ) { 506 return $which_editor; 469 $settings = self::get_settings(); 470 471 // Open the default editor when no $post and for "Add New" links. 472 if ( empty( $post->ID ) || $post->post_status === 'auto-draft' ) { 473 return $settings['editor'] === 'classic' ? false : $which_editor; 507 474 } 508 475 … … 532 499 */ 533 500 public static function get_edit_post_link( $url ) { 534 if ( isset( $_REQUEST['classic-editor'] ) ) { 501 $settings = self::get_settings(); 502 503 if ( isset( $_REQUEST['classic-editor'] ) || $settings['editor'] === 'classic' ) { 535 504 $url = add_query_arg( 'classic-editor', '', $url ); 536 505 } … … 539 508 } 540 509 541 /** 542 * Add an `Add New (Classic)` submenu for Posts, Pages, etc. 543 */ 544 public static function add_submenus() { 545 foreach ( get_post_types( array( 'show_ui' => true ) ) as $type ) { 546 $type_obj = get_post_type_object( $type ); 547 548 if ( ! $type_obj->show_in_menu || ! use_block_editor_for_post_type( $type ) ) { 549 continue; 550 } 551 552 if ( $type_obj->show_in_menu === true ) { 553 if ( 'post' === $type ) { 554 $parent_slug = 'edit.php'; 555 } elseif ( 'page' === $type ) { 556 $parent_slug = 'edit.php?post_type=page'; 557 } else { 558 // Not for a submenu. 559 continue; 510 public static function add_meta_box( $post_type, $post ) { 511 if ( ! self::is_classic( $post->ID ) || ! use_block_editor_for_post_type( $post_type ) ) { 512 return; 513 } 514 515 $id = 'classic-editor-switch-editor'; 516 $title = __( 'Editor', 'classic-editor' ); 517 $callback = array( __CLASS__, 'do_meta_box' ); 518 $args = array( 519 '__back_compat_meta_box' => true, 520 ); 521 522 add_meta_box( $id, $title, $callback, null, 'side', 'default', $args ); 523 } 524 525 public static function do_meta_box( $post ) { 526 $edit_url = get_edit_post_link( $post->ID, 'raw' ); 527 $settings = self::get_settings(); 528 529 $edit_url = remove_query_arg( 'classic-editor', $edit_url ); 530 531 if ( $settings['remember'] ) { 532 // Forget the previous value when going to a specific editor. 533 $edit_url = add_query_arg( 'classic-editor__forget', '', $edit_url ); 534 } 535 536 ?> 537 <p> 538 <label class="screen-reader-text" for="classic-editor-switch-editor"><?php _e( 'Select editor' ); ?></label> 539 <select id="classic-editor-switch-editor" style="width: 100%;max-width: 20em;"> 540 <option value=""><?php _e( 'Classic Editor', 'classic-editor' ); ?></option> 541 <option value="" data-url="<?php echo esc_url( $edit_url ); ?>"><?php _e( 'Block Editor', 'classic-editor' ); ?></option> 542 </select> 543 </p> 544 <script> 545 jQuery( 'document' ).ready( function( $ ) { 546 var $select = $( '#classic-editor-switch-editor' ); 547 $select.on( 'change', function( event ) { 548 var url = $select.find( ':selected' ).attr( 'data-url' ); 549 if ( url ) { 550 document.location = url; 560 551 } 561 } else { 562 $parent_slug = $type_obj->show_in_menu; 563 } 564 565 $item_name = $type_obj->labels->add_new . ' ' . __( ' (Classic editor)', 'classic-editor' ); 566 $path = "post-new.php?post_type={$type}&classic-editor"; 567 add_submenu_page( $parent_slug, $type_obj->labels->add_new, $item_name, $type_obj->cap->edit_posts, $path ); 568 569 // Add "Edit in Classic editor" and "Edit in Block editor" submenu items. 570 $post_id = self::get_edited_post_id(); 571 572 if ( $post_id && get_post_type( $post_id ) === $type ) { 573 $edit_url = "post.php?post={$post_id}&action=edit"; 574 $settings = self::get_settings(); 575 576 if ( $settings['remember'] ) { 577 // Forget the previous value when going to a specific editor. 578 $edit_url = add_query_arg( 'classic-editor__forget', '', $edit_url ); 579 } 580 581 // Block editor. 582 $name = __( 'Edit in Block editor', 'classic-editor' ); 583 add_submenu_page( $parent_slug, $type_obj->labels->edit_item, $name, $type_obj->cap->edit_posts, $edit_url ); 584 585 // Classic editor. 586 $name = __( 'Edit in Classic editor', 'classic-editor' ); 587 $url = add_query_arg( 'classic-editor', '', $edit_url ); 588 add_submenu_page( $parent_slug, $type_obj->labels->edit_item, $name, $type_obj->cap->edit_posts, $url ); 589 } 590 } 552 } ); 553 } ); 554 </script> 555 <?php 556 } 557 558 public static function enqueue_scripts() { 559 wp_enqueue_script( 560 'classic-editor-add-submenu', 561 plugins_url( 'js/block-editor-plugin.js', __FILE__ ), 562 array( 'wp-element', 'wp-components', 'lodash' ), 563 self::plugin_version, 564 true 565 ); 566 567 wp_localize_script( 568 'classic-editor-add-submenu', 569 'classicEditorPluginL10n', 570 array( 'linkText' => __( 'Switch to Classic Editor', 'classic-editor' ) ) 571 ); 591 572 } 592 573 … … 598 579 599 580 if ( $file === 'classic-editor/classic-editor.php' && ! $settings['hide-settings-ui'] && current_user_can( 'manage_options' ) ) { 600 $settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'options-writing.php#classic-editor-options' ), __( 'Settings', 'classic-editor' ) ); 601 array_unshift( $links, $settings_link ); 581 (array) $links[] = sprintf( '<a href="%s">%s</a>', admin_url( 'options-writing.php#classic-editor-options' ), __( 'Settings', 'classic-editor' ) ); 602 582 } 603 583 … … 607 587 /** 608 588 * Adds links to the post/page screens to edit any post or page in 609 * the Classic editor.589 * the Classic or Block editor. 610 590 * 611 591 * @param array $actions Post actions. 612 592 * @param WP_Post $post Edited post. 613 593 * 614 * @return array Updated post actions.594 * @return array Updated post actions. 615 595 */ 616 596 public static function add_edit_links( $actions, $post ) { … … 641 621 642 622 // Link to the Block editor. 623 $url = remove_query_arg( 'classic-editor', $edit_url ); 643 624 $text = __( 'Block editor', 'classic-editor' ); 644 625 /* translators: %s: post title */ 645 626 $label = sprintf( __( 'Edit “%s” in the Block editor', 'classic-editor' ), $title ); 646 $edit_block = sprintf( '<a href="%s" aria-label="%s">%s</a>', esc_url( $ edit_url ), esc_attr( $label ), $text );627 $edit_block = sprintf( '<a href="%s" aria-label="%s">%s</a>', esc_url( $url ), esc_attr( $label ), $text ); 647 628 648 629 // Link to the Classic editor. … … 665 646 } 666 647 648 public static function on_admin_init() { 649 global $pagenow; 650 651 if ( $pagenow !== 'post.php' ) { 652 return; 653 } 654 655 $settings = self::get_settings(); 656 $post_id = self::get_edited_post_id(); 657 658 if ( $post_id && ( $settings['editor'] === 'classic' || self::is_classic( $post_id ) ) ) { 659 // Move the Privacy Policy help notice back under the title field. 660 remove_action( 'admin_notices', array( 'WP_Privacy_Policy_Content', 'notice' ) ); 661 add_action( 'edit_form_after_title', array( 'WP_Privacy_Policy_Content', 'notice' ) ); 662 } 663 } 664 667 665 /** 668 666 * Set defaults on activation. … … 670 668 public static function activate() { 671 669 if ( ! get_option( 'classic-editor-replace' ) ) { 672 update_option( 'classic-editor-replace', ' replace' );670 update_option( 'classic-editor-replace', 'classic' ); 673 671 update_option( 'classic-editor-remember', '' ); 674 672 update_option( 'classic-editor-allow-users', 'allow' ); … … 686 684 } 687 685 688 add_action( 'plugins_loaded', array( 'Classic_Editor', 'init_actions' ) , 20);686 add_action( 'plugins_loaded', array( 'Classic_Editor', 'init_actions' ) ); 689 687 690 688 endif;
Note: See TracChangeset
for help on using the changeset viewer.