Editor: Add custom fields meta box support in the block editor.

This brings support for the custom fields meta box into the new block editor.

The `webpack` and `copy-webpack-plugin` packages have also been updated.

This does not bump the `@wordpress` packages like in [43861] because of conflicts with package versions already installed in `trunk`. The packages will be brought up to date in a subsequent merge.

Merges [43861] and [43863] into trunk.

See #45145.
Fixes #45257.

git-svn-id: https://develop.svn.wordpress.org/trunk@44260 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers 2018-12-17 03:51:08 +00:00
parent b6a378d345
commit e63acae939
7 changed files with 90 additions and 11 deletions

6
package-lock.json generated
View File

@ -3562,9 +3562,9 @@
"dev": true
},
"copy-webpack-plugin": {
"version": "4.5.2",
"resolved": "https://registry.npmjs.org/copy-webpack-plugin/-/copy-webpack-plugin-4.5.2.tgz",
"integrity": "sha512-zmC33E8FFSq3AbflTvqvPvBo621H36Afsxlui91d+QyZxPIuXghfnTsa1CuqiAaCPgJoSUWfTFbKJnadZpKEbQ==",
"version": "4.6.0",
"resolved": "https://registry.npmjs.org/copy-webpack-plugin/-/copy-webpack-plugin-4.6.0.tgz",
"integrity": "sha512-Y+SQCF+0NoWQryez2zXn5J5knmr9z/9qSQt7fbL78u83rxmigOy8X5+BFn8CFSuX+nKT8gpYwJX68ekqtQt6ZA==",
"dev": true,
"requires": {
"cacache": "^10.0.4",

View File

@ -17,7 +17,7 @@
"@wordpress/library-export-default-webpack-plugin": "1.0.4",
"autoprefixer": "^9.1.5",
"check-node-version": "3.2.0",
"copy-webpack-plugin": "^4.5.2",
"copy-webpack-plugin": "^4.6.0",
"cssnano": "^4.1.4",
"grunt": "~1.0.3",
"grunt-banner": "^0.6.0",
@ -49,7 +49,7 @@
"source-map-loader": "^0.2.4",
"uglify-js": "^3.4.9",
"uglifyjs-webpack-plugin": "^2.0.1",
"webpack": "^4.20.2",
"webpack": "^4.24.0",
"webpack-dev-server": "^3.1.9",
"webpack-livereload-plugin": "^2.1.1"
},

View File

@ -202,6 +202,29 @@ if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
}
}
// Image sizes.
$image_sizes = get_intermediate_image_sizes();
$image_sizes[] = 'full';
/** This filter is documented in wp-admin/includes/media.php */
$image_size_names = apply_filters(
'image_size_names_choose',
array(
'thumbnail' => __( 'Thumbnail' ),
'medium' => __( 'Medium' ),
'large' => __( 'Large' ),
'full' => __( 'Full Size' ),
)
);
$available_image_sizes = array();
foreach ( $image_sizes as $image_size_slug ) {
$available_image_sizes[] = array(
'slug' => $image_size_slug,
'name' => isset( $image_size_names[ $image_size_slug ] ) ? $image_size_names[ $image_size_slug ] : $image_size_slug,
);
}
// Lock settings.
$user_id = wp_check_post_lock( $post->ID );
if ( $user_id ) {
@ -257,12 +280,17 @@ $editor_settings = array(
'maxUploadFileSize' => $max_upload_size,
'allowedMimeTypes' => get_allowed_mime_types(),
'styles' => $styles,
'availableImageSizes' => $available_image_sizes,
'postLock' => $lock_details,
'postLockUtils' => array(
'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ),
'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
),
// Whether or not to load the 'postcustom' meta box is stored as a user meta
// field so that we're not always loading its assets.
'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
);
$autosave = wp_get_post_autosave( $post_ID );

View File

@ -1463,11 +1463,21 @@ function register_and_do_post_meta_boxes( $post ) {
}
if ( post_type_supports( $post_type, 'custom-fields' ) ) {
$args = array(
'__back_compat_meta_box' => ! (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
'__block_editor_compatible_meta_box' => true,
);
add_meta_box( 'postcustom', __( 'Custom Fields' ), 'post_custom_meta_box', null, 'normal', 'core', $args );
$screen = get_current_screen();
if ( ! $screen || ! $screen->is_block_editor() || (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ) ) {
add_meta_box(
'postcustom',
__( 'Custom Fields' ),
'post_custom_meta_box',
null,
'normal',
'core',
array(
'__back_compat_meta_box' => false,
'__block_editor_compatible_meta_box' => true,
)
);
}
}
/**

View File

@ -2112,7 +2112,7 @@ function get_block_categories( $post ) {
array(
'slug' => 'common',
'title' => __( 'Common Blocks' ),
'icon' => 'screenoptions',
'icon' => null,
),
array(
'slug' => 'formatting',
@ -2214,6 +2214,10 @@ function the_block_editor_meta_boxes() {
<form class="metabox-base-form">
<?php the_block_editor_meta_box_post_form_hidden_fields( $post ); ?>
</form>
<form id="toggle-custom-fields-form" method="post" action="<?php echo esc_attr( admin_url( 'post.php' ) ); ?>">
<?php wp_nonce_field( 'toggle-custom-fields' ); ?>
<input type="hidden" name="action" value="toggle-custom-fields" />
</form>
<?php foreach ( $locations as $location ) : ?>
<form class="metabox-location-<?php echo esc_attr( $location ); ?>" onsubmit="return false;">
<div id="poststuff" class="sidebar-open">
@ -2289,6 +2293,29 @@ function the_block_editor_meta_boxes() {
printf( "<script type='text/javascript'>\n%s\n</script>\n", trim( $script ) );
}
/**
* If the 'postcustom' meta box is enabled, then we need to perform some
* extra initialization on it.
*/
$enable_custom_fields = (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true );
if ( $enable_custom_fields ) {
$script = "( function( $ ) {
if ( $('#postcustom').length ) {
$( '#the-list' ).wpList( {
addBefore: function( s ) {
s.data += '&post_id=$post->ID';
return s;
},
addAfter: function() {
$('table#list-table').show();
}
});
}
} )( jQuery );";
wp_enqueue_script( 'wp-lists' );
wp_add_inline_script( 'wp-lists', $script );
}
// Reset meta box data.
$wp_meta_boxes = $_original_meta_boxes;
}

View File

@ -312,6 +312,18 @@ switch ( $action ) {
wp_redirect( $url );
exit();
case 'toggle-custom-fields':
check_admin_referer( 'toggle-custom-fields' );
$current_user_id = get_current_user_id();
if ( $current_user_id ) {
$enable_custom_fields = (bool) get_user_meta( $current_user_id, 'enable_custom_fields', true );
update_user_meta( $current_user_id, 'enable_custom_fields', ! $enable_custom_fields );
}
wp_safe_redirect( wp_get_referer() );
exit();
default:
/**
* Fires for a given custom post action request.

View File

@ -235,6 +235,7 @@ function wp_default_packages_scripts( &$scripts ) {
'data' => array(
'lodash',
'wp-compose',
'wp-deprecated',
'wp-element',
'wp-is-shallow-equal',
'wp-polyfill',
@ -351,6 +352,7 @@ function wp_default_packages_scripts( &$scripts ) {
'lodash',
'wp-blocks',
'wp-data',
'wp-deprecated',
'wp-escape-html',
'wp-polyfill',
),