Skip to content

Commit b8b9598

Browse files
authored
fix: Add null safety checks for meta attributes in slotfills (#762)
* Update slotfills with additional checks for meta attributes, rename vars * Add jsdoc comments and text domain for translated strings * Remove unused useState import and add checks for post editing context before showing the toggles
1 parent 7110c46 commit b8b9598

3 files changed

Lines changed: 56 additions & 32 deletions

File tree

build/slotfills.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array(), 'version' => '2e9554884f0c0233f177');
1+
<?php return array('dependencies' => array(), 'version' => '8640506d113757c694bd');

build/slotfills.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/blocks/slotfills.js

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,74 @@
1-
( function ( wp ) {
1+
(function (wp) {
22
const { registerPlugin } = wp.plugins;
33
const { PluginPostStatusInfo } = wp.editPost;
44
const { ToggleControl } = wp.components;
55
const { useSelect, useDispatch } = wp.data;
6-
const { createElement, useState } = wp.element;
6+
const { createElement } = wp.element;
77
const i18n = window.wp.i18n;
88

9-
// Custom Sticky Post Toggle Component
9+
/**
10+
* Component for toggling featured status of a post.
11+
*
12+
* @since 2.1.0
13+
* @return {Element} The toggle control element.
14+
*/
1015
const StickyToggle = () => {
11-
const { editPost } = useDispatch( 'core/editor' );
12-
const handleChange = ( newChecked ) => {
13-
editPost( { meta: { featured: newChecked } } );
16+
const { editPost } = useDispatch('core/editor');
17+
const handleChange = (newChecked) => {
18+
editPost({ meta: { featured: newChecked } });
1419
};
1520

16-
const isSticky = useSelect( function ( select ) {
17-
return select( 'core/editor' ).getEditedPostAttribute( 'meta' )
18-
.featured;
19-
}, [] );
21+
const isSticky = useSelect(function (select) {
22+
const meta = select('core/editor').getEditedPostAttribute('meta');
23+
return meta?.featured || false;
24+
}, []);
2025

21-
return createElement( ToggleControl, {
22-
label: i18n.__( 'Featured' ),
26+
return createElement(ToggleControl, {
27+
label: i18n.__('Featured', 'tour-operator'),
2328
checked: isSticky,
2429
onChange: handleChange,
25-
} );
30+
});
2631
};
2732

28-
// Custom Disable Single Toggle Component
33+
/**
34+
* Component for toggling the disable single post view setting.
35+
*
36+
* @since 2.1.0
37+
* @return {Element} The toggle control element.
38+
*/
2939
const DisableSingleToggle = () => {
30-
const { editPost } = useDispatch( 'core/editor' );
31-
const handleChange = ( newChecked ) => {
32-
editPost( { meta: { disable_single: newChecked } } );
40+
const { editPost } = useDispatch('core/editor');
41+
const handleChange = (newChecked) => {
42+
editPost({ meta: { disable_single: newChecked } });
3343
};
3444

35-
const isSticky = useSelect( function ( select ) {
36-
return select( 'core/editor' ).getEditedPostAttribute( 'meta' )
37-
.disable_single;
38-
}, [] );
45+
const isDisabled = useSelect(function (select) {
46+
const meta = select('core/editor').getEditedPostAttribute('meta');
47+
return meta?.disable_single || false;
48+
}, []);
3949

40-
return createElement( ToggleControl, {
41-
label: i18n.__( 'Disable Single' ),
42-
checked: isSticky,
50+
return createElement(ToggleControl, {
51+
label: i18n.__('Disable Single', 'tour-operator'),
52+
checked: isDisabled,
4353
onChange: handleChange,
44-
} );
54+
});
4555
};
4656

4757
const LsxToStatusPlugin = function () {
58+
// Check if we're in a post editing context (not template editor)
59+
const isEditingPost = useSelect(function (select) {
60+
const currentPost = select('core/editor')?.getCurrentPost?.();
61+
const postType = select('core/editor')?.getCurrentPostType?.();
62+
63+
// We're editing a post if we have a current post with an ID and it's not a template
64+
return currentPost && currentPost.id && postType !== 'wp_template';
65+
}, []);
66+
67+
// Only show toggles when editing a post (not template)
68+
if (!isEditingPost) {
69+
return null;
70+
}
71+
4872
return createElement(
4973
PluginPostStatusInfo,
5074
{
@@ -61,21 +85,21 @@
6185
{
6286
className: 'toggle-row',
6387
},
64-
createElement( StickyToggle )
88+
createElement(StickyToggle)
6589
),
6690
createElement(
6791
'div',
6892
{
6993
className: 'toggle-row',
7094
},
71-
createElement( DisableSingleToggle )
95+
createElement(DisableSingleToggle)
7296
)
7397
)
7498
);
7599
};
76100

77-
registerPlugin( 'lsx-to-status-plugin', {
101+
registerPlugin('lsx-to-status-plugin', {
78102
render: LsxToStatusPlugin,
79103
icon: null,
80-
} );
81-
} )( window.wp );
104+
});
105+
})(window.wp);

0 commit comments

Comments
 (0)