Skip to content

Commit

Permalink
fix: correctly update meta
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Jan 11, 2025
1 parent 5094dbd commit 7790cc7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
49 changes: 37 additions & 12 deletions includes/bylines/class-bylines.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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();
10 changes: 6 additions & 4 deletions src/bylines/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 7790cc7

Please sign in to comment.