Make WordPress Core


Ignore:
Timestamp:
11/23/2018 08:14:07 AM (8 years ago)
Author:
pento
Message:

Block Editor: Add a placeholder for meta boxes that don't work in the block editor.

If a meta box is registered with the __block_editor_compatible_meta_box set to false, it's indicating that it doesn't work in the block editor. If that's the case, we can add a place holder to inform the user that they'll need to use the classic interface to work with this meta box.

Props pento, jorgefilipecosta, peterwilsoncc, karmatosed, noisysocks, dd32.
See #45217.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-admin/includes/template.php

    r43885 r43941  
    999999}
    10001000
     1001
     1002/**
     1003 * Function that renders a "fake" meta box with an information message,
     1004 * shown on the block editor, when an incompatible meta box is found.
     1005 *
     1006 * @since 5.0.0
     1007 *
     1008 * @param mixed $object The data object being rendered on this screen.
     1009 * @param array $box    {
     1010 *     Custom formats meta box arguments.
     1011 *
     1012 *     @type string   $id           Meta box 'id' attribute.
     1013 *     @type string   $title        Meta box title.
     1014 *     @type callable $old_callback The original callback for this meta box.
     1015 *     @type array    $args         Extra meta box arguments.
     1016 * }
     1017 */
     1018function do_block_editor_incompatible_meta_box( $object, $box ) {
     1019        $plugin  = _get_plugin_from_callback( $box['old_callback'] );
     1020        $plugins = get_plugins();
     1021        echo '<p>';
     1022        if ( $plugin ) {
     1023                /* translators: %s: the name of the plugin that generated this meta box. */
     1024                printf( __( "This meta box, from the %s plugin, isn't compatible with the block editor." ), "<strong>{$plugin['Name']}</strong>" );
     1025        } else {
     1026                _e( "This meta box isn't compatible with the block editor." );
     1027        }
     1028        echo '</p>';
     1029
     1030        if ( empty( $plugins['classic-editor/classic-editor.php'] ) ) {
     1031                if ( current_user_can( 'install_plugins' ) ) {
     1032                        echo '<p>';
     1033                        /* translators: %s: A link to install the Classic Editor plugin. */
     1034                        printf( __( 'Please install the <a href="%s">Classic Editor plugin</a> to use this meta box.'), esc_url( self_admin_url( 'plugin-install.php?tab=featured' ) ) );
     1035                        echo '</p>';
     1036                }
     1037        } elseif ( is_plugin_inactive( 'classic-editor/classic-editor.php' ) ) {
     1038                if ( current_user_can( 'activate_plugins' ) ) {
     1039                        $activate_url = wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=classic-editor/classic-editor.php' ), 'activate-plugin_classic-editor/classic-editor.php' );
     1040                        echo '<p>';
     1041                        /* translators: %s: A link to activate the Classic Editor plugin. */
     1042                        printf( __( 'Please activate the <a href="%s">Classic Editor plugin</a> to use this meta box.'), esc_url( $activate_url ) );
     1043                        echo '</p>';
     1044                }
     1045        } elseif ( $object instanceof WP_Post ) {
     1046                $edit_url = add_query_arg( 'classic-editor', '', get_edit_post_link( $object ) );
     1047                echo '<p>';
     1048                /* translators: %s: A link to use the Classic Editor plugin. */
     1049                printf( __( 'Please open the <a href="%s">classic editor</a> to use this meta box.'), esc_url( $edit_url ) );
     1050                echo '</p>';
     1051        }
     1052}
     1053
     1054/**
     1055 * Internal helper function to find the plugin from a meta box callback.
     1056 *
     1057 * @since 5.0.0
     1058 *
     1059 * @access private
     1060 *
     1061 * @param callable $callback The callback function to check.
     1062 * @return array|null The plugin that the callback belongs to, or null if it doesn't belong to a plugin.
     1063 */
     1064function _get_plugin_from_callback( $callback ) {
     1065        try {
     1066                if ( is_array( $callback ) ) {
     1067                        $reflection = new ReflectionMethod( $callback[0], $callback[1] );
     1068                } elseif ( is_string( $callback) && false !== strpos( $callback, '::' ) ) {
     1069                        $reflection = new ReflectionMethod( $callback );
     1070                } else {
     1071                        $reflection = new ReflectionFunction( $callback );
     1072                }
     1073        } catch ( ReflectionException $exception ) {
     1074                // We could not properly reflect on the callable, so we abort here.
     1075                return null;
     1076        }
     1077
     1078        // Don't show an error if it's an internal PHP function.
     1079        if ( ! $reflection->isInternal() ) {
     1080
     1081                // Only show errors if the meta box was registered by a plugin.
     1082                $filename = wp_normalize_path( $reflection->getFileName() );
     1083                $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
     1084                if ( strpos( $filename, $plugin_dir ) === 0 ) {
     1085                        $filename = str_replace( $plugin_dir, '', $filename );
     1086                        $filename = preg_replace( '|^/([^/]*/).*$|', '\\1', $filename );
     1087
     1088                        $plugins = get_plugins();
     1089                        foreach ( $plugins as $name => $plugin ) {
     1090                                if ( strpos( $name, $filename ) === 0 ) {
     1091                                        return $plugin;
     1092                                }
     1093                        }
     1094                }
     1095        }
     1096
     1097        return null;
     1098}
     1099
    10011100/**
    10021101 * Meta-Box template function
     
    10601159                                                }
    10611160
    1062                                                 // If a meta box doesn't work in the block editor, don't show it in the block editor.
    1063                                                 if ( $screen->is_block_editor() && isset( $box['args']['__block_editor_compatible_meta_box'] ) && ! $box['args']['__block_editor_compatible_meta_box'] ) {
    1064                                                         continue;
    1065                                                 }
    1066 
    10671161                                                if ( isset( $box['args']['__block_editor_compatible_meta_box'] ) ) {
    10681162                                                        $block_compatible = (bool) $box['args']['__block_editor_compatible_meta_box'];
    10691163                                                        unset( $box['args']['__block_editor_compatible_meta_box'] );
     1164                                                }
     1165
     1166                                                // If the meta box is declared as incompatible with the block editor, override the callback function.
     1167                                                if ( ! $block_compatible && $screen->is_block_editor() ) {
     1168                                                        $box['old_callback'] = $box['callback'];
     1169                                                        $box['callback']     = 'do_block_editor_incompatible_meta_box';
    10701170                                                }
    10711171
     
    10981198
    10991199                                        if ( WP_DEBUG && ! $block_compatible && 'edit' === $screen->parent_base && ! $screen->is_block_editor() && ! isset( $_GET['meta-box-loader'] ) ) {
    1100                                                 if ( is_array( $box['callback'] ) ) {
    1101                                                         $reflection = new ReflectionMethod( $box['callback'][0], $box['callback'][1] );
    1102                                                 } elseif ( false !== strpos( $box['callback'], '::' ) ) {
    1103                                                         $reflection = new ReflectionMethod( $box['callback'] );
    1104                                                 } else {
    1105                                                         $reflection = new ReflectionFunction( $box['callback'] );
    1106                                                 }
    1107 
    1108                                                 // Don't show an error if it's an internal PHP function.
    1109                                                 if ( ! $reflection->isInternal() ) {
    1110 
    1111                                                         // Only show errors if the meta box was registered by a plugin.
    1112                                                         $filename = $reflection->getFileName();
    1113                                                         if ( strpos( $filename, WP_PLUGIN_DIR ) === 0 ) {
    1114                                                                 $filename = str_replace( WP_PLUGIN_DIR, '', $filename );
    1115                                                                 $filename = preg_replace( '|^/([^/]*/).*$|', '\\1', $filename );
    1116 
    1117                                                                 $plugins = get_plugins();
    1118                                                                 foreach ( $plugins as $name => $plugin ) {
    1119                                                                         if ( strpos( $name, $filename ) === 0 ) {
    1120                                                                                 ?>
    1121                                                                                         <div class="error inline">
    1122                                                                                                 <p>
    1123                                                                                                         <?php
    1124                                                                                                                 /* translators: %s: the name of the plugin that generated this meta box. */
    1125                                                                                                                 printf( __( "This meta box, from the %s plugin, isn't compatible with the block editor." ), "<strong>{$plugin['Name']}</strong>" );
    1126                                                                                                         ?>
    1127                                                                                                 </p>
    1128                                                                                         </div>
    1129                                                                                 <?php
    1130                                                                         }
    1131                                                                 }
    1132                                                         }
     1200                                                $plugin = _get_plugin_from_callback( $box['callback'] );
     1201                                                if ( $plugin ) {
     1202                                                ?>
     1203                                                        <div class="error inline">
     1204                                                                <p>
     1205                                                                        <?php
     1206                                                                                /* translators: %s: the name of the plugin that generated this meta box. */
     1207                                                                                printf( __( "This meta box, from the %s plugin, isn't compatible with the block editor." ), "<strong>{$plugin['Name']}</strong>" );
     1208                                                                        ?>
     1209                                                                </p>
     1210                                                        </div>
     1211                                                <?php
    11331212                                                }
    11341213                                        }
Note: See TracChangeset for help on using the changeset viewer.