|
1 | | -( function ( wp ) { |
| 1 | +(function (wp) { |
2 | 2 | const { registerPlugin } = wp.plugins; |
3 | 3 | const { PluginPostStatusInfo } = wp.editPost; |
4 | 4 | const { ToggleControl } = wp.components; |
5 | 5 | const { useSelect, useDispatch } = wp.data; |
6 | | - const { createElement, useState } = wp.element; |
| 6 | + const { createElement } = wp.element; |
7 | 7 | const i18n = window.wp.i18n; |
8 | 8 |
|
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 | + */ |
10 | 15 | 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 } }); |
14 | 19 | }; |
15 | 20 |
|
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 | + }, []); |
20 | 25 |
|
21 | | - return createElement( ToggleControl, { |
22 | | - label: i18n.__( 'Featured' ), |
| 26 | + return createElement(ToggleControl, { |
| 27 | + label: i18n.__('Featured', 'tour-operator'), |
23 | 28 | checked: isSticky, |
24 | 29 | onChange: handleChange, |
25 | | - } ); |
| 30 | + }); |
26 | 31 | }; |
27 | 32 |
|
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 | + */ |
29 | 39 | 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 } }); |
33 | 43 | }; |
34 | 44 |
|
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 | + }, []); |
39 | 49 |
|
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, |
43 | 53 | onChange: handleChange, |
44 | | - } ); |
| 54 | + }); |
45 | 55 | }; |
46 | 56 |
|
47 | 57 | 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 | + |
48 | 72 | return createElement( |
49 | 73 | PluginPostStatusInfo, |
50 | 74 | { |
|
61 | 85 | { |
62 | 86 | className: 'toggle-row', |
63 | 87 | }, |
64 | | - createElement( StickyToggle ) |
| 88 | + createElement(StickyToggle) |
65 | 89 | ), |
66 | 90 | createElement( |
67 | 91 | 'div', |
68 | 92 | { |
69 | 93 | className: 'toggle-row', |
70 | 94 | }, |
71 | | - createElement( DisableSingleToggle ) |
| 95 | + createElement(DisableSingleToggle) |
72 | 96 | ) |
73 | 97 | ) |
74 | 98 | ); |
75 | 99 | }; |
76 | 100 |
|
77 | | - registerPlugin( 'lsx-to-status-plugin', { |
| 101 | + registerPlugin('lsx-to-status-plugin', { |
78 | 102 | render: LsxToStatusPlugin, |
79 | 103 | icon: null, |
80 | | - } ); |
81 | | -} )( window.wp ); |
| 104 | + }); |
| 105 | +})(window.wp); |
0 commit comments