array( 'show_in_rest' => true, 'single' => true, 'type' => 'boolean', 'auth_callback' => 'addifect_meta_auth_callback', ), '_addifect_post_data' => array( 'show_in_rest' => false, // Kept false to prevent REST response bloat. 'single' => true, 'type' => 'string', 'auth_callback' => 'addifect_meta_auth_callback', ), ); // Register each meta key for every target post type. foreach ( $target_post_types as $post_type ) { foreach ( $meta_fields as $meta_key => $args ) { register_post_meta( $post_type, $meta_key, $args ); } } } add_action( 'init', 'addifect_register_file_meta', 11 ); /** * Authorization callback for Addifect meta. * * This ensures that: * 1. Users with the master 'addifect_edit_design_files' cap can save. * 2. Authors and Editors can save IF they have permission to edit the specific post. * * Note: This does NOT block the frontend from reading the meta. * * @param bool $allowed Whether the user can edit the meta. * @param string $meta_key The meta key. * @param int $post_id The ID of the post. * @param int $user_id The ID of the user. * @return bool */ function addifect_meta_auth_callback( $allowed, $meta_key, $post_id, $user_id ) { // Check for master Addifect capability. if ( user_can( $user_id, 'addifect_edit_design_files' ) ) { return true; } // Fallback: Can the user edit this specific post/page? // This allows Authors to save Addifect data on their own posts. if ( user_can( $user_id, 'edit_post', $post_id ) ) { return true; } return false; } /** * Register Private Meta for Addifect Design */ function addifect_register_design_meta() { // 1. The Index (The Map of the Design) register_post_meta( 'addifect_design', '_addifect_design_index', array( 'single' => true, 'type' => 'object', 'show_in_rest' => false, 'auth_callback' => function () { return current_user_can( 'addifect_edit_design_files' ); }, ) ); // 2. The Templates (The Actual Content) register_post_meta( 'addifect_design', '_addifect_design_templates', array( 'single' => true, 'type' => 'object', 'show_in_rest' => false, 'auth_callback' => function () { return current_user_can( 'addifect_edit_design_files' ); }, ) ); } add_action( 'init', 'addifect_register_design_meta' );