Untitled

 avatar
unknown
plain_text
a month ago
3.8 kB
2
Indexable
in below code this is in gutenberg block so if this is used in particular theme that is blackstone v2 and in particular site that is bmacx so we only want no header and no footer toggle we do not need no top global navigation and Simplified footer so how we can fetch only these rest should not come up in authoring side
/**
 * Template Option Panel
 */

const { useMemo } = wp.element;
const { ToggleControl } = wp.components;
const { PluginDocumentSettingPanel } = wp.editPost || {};
const { registerPlugin } = wp.plugins;
const { __ } = wp.i18n;
const { select, useSelect, useDispatch } = wp.data;
const {
	templateOptionsPostTypes,
	templateOptionsMetaKeys : {
		no_header_nav_key,
		no_footer_nav_key,
		no_network_nav_key,
		simplified_footer_key,
	}
} = BLACKSTONE_EDITOR;

/**
 * Output the template options sidebar panel
 */
function TemplateOptions() {
	const { editPost } = useDispatch( 'core/editor' );

	if (!PluginDocumentSettingPanel) {
		return null;
	}

	const currentTemplate = useSelect( ( select ) =>
		select( 'core/editor' ).getEditedPostAttribute( 'template' )
	);
	const meta = useSelect( ( select ) =>
		select( 'core/editor' ).getEditedPostAttribute( 'meta' )
	);

	const noHeaderNavValue = useMemo(
		() => {
			if ( meta != null ) {
				return meta[ no_header_nav_key ] || '';
			}
		},
		[ meta ]
	);

	const noFooterNavValue = useMemo(
		() => {
			if ( meta != null ) {
				return meta[ no_footer_nav_key ] || '';
			}
		},
		[ meta ]
	);

	const noNetworkNavValue = useMemo(
		() => {
			if ( meta != null ) {
				return meta[ no_network_nav_key ] || '';
			}
		},
		[ meta ]
	);

	const simplifiedFooterValue = useMemo(
		() => {
			if ( meta != null ) {
				return meta[ simplified_footer_key ] || '';
			}
		},
		[ meta ]
	);

	// Set the default based on legacy page template.
	let noHeaderNavDefault = 'templates/page-no-header-navigation.php' === currentTemplate || 'templates/page-no-header-footer-navigation.php' === currentTemplate;
	let noFooterNavDefault = 'templates/page-no-header-footer-navigation.php' === currentTemplate;
	if ( noHeaderNavDefault ) {
		editPost( {
			meta: {
				...meta,
				[ no_header_nav_key ]: 1,
			},
			template: '',
		} );
	}
	if ( noFooterNavDefault ) {
		editPost( {
			meta: {
				...meta,
				[ no_footer_nav_key ]: 1,
			},
			template: '',
		} );
	}

	return (
		<PluginDocumentSettingPanel
			name="blackstone-template-options-panel"
			title={ __( 'Template Options', 'blackstone' ) }
		>
			<ToggleControl
				checked={ noHeaderNavValue }
				onChange={ ( nextValue ) => {
					editPost( {
						meta: {
							...meta,
							[ no_header_nav_key ]: nextValue,
						},
					} );
				} }
				label={
					__( 'No Header Navigation', 'blackstone' )
				}
			/>
			<ToggleControl
				checked={ noFooterNavValue }
				onChange={ ( nextValue ) => {
					editPost( {
						meta: {
							...meta,
							[ no_footer_nav_key ]: nextValue,
						},
					} );
				} }
				label={
					__( 'No Footer Navigation', 'blackstone' )
				}
			/>
			<ToggleControl
				checked={ noNetworkNavValue }
				onChange={ ( nextValue ) => {
					editPost( {
						meta: {
							...meta,
							[ no_network_nav_key ]: nextValue,
						},
					} );
				} }
				label={
					__( 'No Top Global Navigation', 'blackstone' )
				}
			/>
			<ToggleControl
				checked={ simplifiedFooterValue }
				onChange={ ( nextValue ) => {
					editPost( {
						meta: {
							...meta,
							[ simplified_footer_key ]: nextValue,
						},
					} );
				} }
				label={
					__( 'Display Simplified Footer', 'blackstone' )
				}
			/>
		</PluginDocumentSettingPanel>
	);
}

if ( wp.editPost && templateOptionsPostTypes.includes( window.pagenow ) ) {
	registerPlugin( 'blackstone-template-options', {
		render: TemplateOptions,
		icon: null,
	} );
}
Leave a Comment