Skip to content

Commit

Permalink
Ensure that postmeta keys registered via register_meta() don't appear…
Browse files Browse the repository at this point in the history
… in Custom Fields.

When they appear in the Custom Fields metabox alongside the Block Editor,
the values saved in the `edit_post()` callback can overwrite what's sent
by the Block Editor.
  • Loading branch information
boonebgorges committed Sep 5, 2024
1 parent 048e3c9 commit 44c7e19
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions classes/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function init() {
add_action( 'save_post_' . self::get_module_post_type(), [ $this, 'maybe_create_all_modules_page' ] );
add_action( 'before_delete_post', [ $this, 'remove_page_from_modules' ] );

add_filter( 'is_protected_meta', [ $this, 'is_protected_meta' ], 10, 3 );

// OpenLab Attributions support for Module post type.
add_filter( 'ol_image_attribution_supported_post_types', [ $this, 'add_openlab_attribution_support' ] );
}
Expand Down Expand Up @@ -235,6 +237,7 @@ public function register_metas() {
'single' => true,
'show_in_rest' => true,
'description' => __( 'Description', 'openlab-modules' ),
'auth_callback' => '__return_true',
]
);

Expand All @@ -247,6 +250,7 @@ public function register_metas() {
'single' => true,
'show_in_rest' => true,
'description' => __( 'Acknowledgements', 'openlab-modules' ),
'auth_callback' => '__return_true',
]
);

Expand All @@ -259,6 +263,7 @@ public function register_metas() {
'single' => true,
'show_in_rest' => true,
'description' => __( 'Module Page IDs', 'openlab-modules' ),
'auth_callback' => '__return_true',
]
);

Expand All @@ -272,6 +277,7 @@ public function register_metas() {
'single' => true,
'show_in_rest' => true,
'description' => __( 'Link to Module', 'openlab-modules' ),
'auth_callback' => '__return_true',
]
);
}
Expand Down Expand Up @@ -567,4 +573,35 @@ public function add_openlab_attribution_support( $post_types ) {

return $post_types;
}

/**
* Determines if a meta key is protected.
*
* We mark our custom meta as protected so that it doesn't appear in the Custom Fields
* metabox, where `edit_post()` uses empty values to overwrite what's provided in
* the block editor.
*
* @param bool $is_protected Whether the meta key is protected.
* @param string $meta_key Meta key.
* @param string $meta_type Meta type.
* @return bool
*/
public function is_protected_meta( $is_protected, $meta_key, $meta_type ) {
if ( 'post' !== $meta_type ) {
return $is_protected;
}

$keys = [
'module_description',
'module_acknowledgements',
'module_page_ids',
'link_to_module',
];

if ( in_array( $meta_key, $keys, true ) ) {
return true;
}

return $is_protected;
}
}

0 comments on commit 44c7e19

Please sign in to comment.