From 7790cc7e030da019cc82a1918bcd000112d0c7df Mon Sep 17 00:00:00 2001 From: Rasmy Nguyen Date: Sat, 11 Jan 2025 17:13:41 -0500 Subject: [PATCH] fix: correctly update meta --- includes/bylines/class-bylines.php | 49 ++++++++++++++++++++++-------- src/bylines/index.js | 10 +++--- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/includes/bylines/class-bylines.php b/includes/bylines/class-bylines.php index 6b2fe84e13..81f54f3ce4 100644 --- a/includes/bylines/class-bylines.php +++ b/includes/bylines/class-bylines.php @@ -11,6 +11,20 @@ * Class to handle custom bylines. */ class Bylines { + /** + * Meta key for the active flag. + * + * @var string + */ + const META_KEY_ACTIVE = '_newspack_byline_active'; + + /** + * Meta key for the byline. + * + * @var string + */ + const META_KEY_BYLINE = '_newspack_byline'; + /** * Initializes the class. */ @@ -63,26 +77,37 @@ public static function enqueue_block_editor_assets() { public static function register_post_meta() { \register_post_meta( 'post', - 'newspack_byline_enabled', + self::META_KEY_ACTIVE, [ - 'default' => false, - 'description' => 'Whether custom bylines is enabled for the post.', - 'show_in_rest' => true, - 'single' => true, - 'type' => 'boolean', + 'default' => false, + 'description' => 'Whether custom bylines is enabled for the post.', + 'show_in_rest' => true, + 'single' => true, + 'type' => 'boolean', + 'auth_callback' => [ __CLASS__, 'auth_callback' ], ] ); \register_post_meta( 'post', - 'newspack_byline', + self::META_KEY_BYLINE, [ - 'default' => '', - 'description' => 'A custom byline for the post', - 'show_in_rest' => true, - 'single' => true, - 'type' => 'string', + 'default' => '', + 'description' => 'A custom byline for the post', + 'show_in_rest' => true, + 'single' => true, + 'type' => 'string', + 'auth_callback' => [ __CLASS__, 'auth_callback' ], ] ); } + + /** + * Auth callback for custom post meta. + * + * @return bool True if current user can access, false otherwise. + */ + public static function auth_callback() { + return \current_user_can( 'edit_posts' ); + } } Bylines::init(); diff --git a/src/bylines/index.js b/src/bylines/index.js index e34d095078..466e153487 100644 --- a/src/bylines/index.js +++ b/src/bylines/index.js @@ -16,20 +16,22 @@ import { useState } from 'react'; import './style.scss'; // const ALLOWED_TAGS = [ 'Author' ]; +const META_KEY_ACTIVE = '_newspack_byline_active'; +const META_KEY_BYLINE = '_newspack_byline'; const BylinesSettingsPanel = () => { const { editPost } = useDispatch( 'core/editor' ); const { getEditedPostAttribute } = useSelect( select => select( 'core/editor' ) ); - const [ isEnabled, setIsEnabled ] = useState( !! getEditedPostAttribute( 'meta' )?.newspack_byline_enabled ); - const [ byline, setByline ] = useState( getEditedPostAttribute( 'meta' )?.newspack_byline ?? '' ); + const [ isEnabled, setIsEnabled ] = useState( !! getEditedPostAttribute( 'meta' )[ META_KEY_ACTIVE ] ); + const [ byline, setByline ] = useState( getEditedPostAttribute( 'meta' )[ META_KEY_BYLINE ] || '' ); // Enabled toggle handler. const handleEnableToggle = value => { - editPost( { meta: { newspack_byline_enabled: value } } ); + editPost( { meta: { [ META_KEY_ACTIVE ]: value } } ); setIsEnabled( value ); } // Byline change handler. const handleBylineChange = value => { - editPost( { meta: { newspack_byline: value } } ); + editPost( { meta: { [ META_KEY_BYLINE ]: value } } ); setByline( value ); } return (