/** * Noindex SEO - Gutenberg Sidebar Panel * Version: 3.0.0 * * Provides a native Gutenberg sidebar panel for controlling robots directives * on individual posts and pages: boolean flags plus the value directives * introduced in 3.6.0 (max-snippet, max-image-preview, max-video-preview, * unavailable_after). * * @package noindex-seo */ (function (wp) { const { registerPlugin } = wp.plugins; const { PluginDocumentSettingPanel } = wp.editPost; const { CheckboxControl, PanelRow, SelectControl, TextControl } = wp.components; const { useSelect, useDispatch } = wp.data; const { createElement: el } = wp.element; const { __ } = wp.i18n; /** * Boolean directives: checkbox controls backed by integer meta (0/1). */ const booleanDirectives = ['noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex', 'notranslate']; /** * Value directives: inputs backed by string meta ('' = off). */ const valueDirectives = ['max-snippet', 'max-image-preview', 'max-video-preview', 'unavailable_after']; /** * Robots Directives Sidebar Panel Component */ const NoindexSeoSidebarPanel = function () { // Get current post meta values. const postMeta = useSelect( function (select) { return select( 'core/editor' ).getEditedPostAttribute( 'meta' ) || {}; } ); const { editPost } = useDispatch( 'core/editor' ); // Current override state. const override = Boolean( postMeta._noindex_seo_override ); // Update a boolean meta value (0/1). const updateBoolean = function (key, value) { const newMeta = {}; newMeta[key] = value ? 1 : 0; editPost( { meta: newMeta } ); }; // Update a value-directive meta value (string, '' = off). const updateValue = function (key, value) { const newMeta = {}; newMeta[key] = value || ''; editPost( { meta: newMeta } ); }; // Clear all directive meta values. const clearAllDirectives = function () { const newMeta = {}; booleanDirectives.forEach( function (directive) { newMeta['_noindex_seo_' + directive] = 0; } ); valueDirectives.forEach( function (directive) { newMeta['_noindex_seo_' + directive] = ''; } ); editPost( { meta: newMeta } ); }; // Handle override toggle. const handleOverrideChange = function (value) { updateBoolean( '_noindex_seo_override', value ); // If disabling override, clear all directives. if ( ! value) { clearAllDirectives(); } }; // Build the preview list: names for booleans, name:value for values. const activeDirectives = []; if (override) { booleanDirectives.forEach( function (directive) { if ( Boolean( postMeta['_noindex_seo_' + directive] ) ) { activeDirectives.push( directive ); } } ); valueDirectives.forEach( function (directive) { const value = postMeta['_noindex_seo_' + directive]; if ( value ) { activeDirectives.push( directive + ':' + value ); } } ); } // Boolean directive rows. const booleanRows = booleanDirectives.map( function (directive) { return el( PanelRow, { key: directive + '-row' }, el( CheckboxControl, { label: __( directive, 'noindex-seo' ), checked: Boolean( postMeta['_noindex_seo_' + directive] ), onChange: function (value) { updateBoolean( '_noindex_seo_' + directive, value ); }, } ) ); } ); // Value directive rows. const valueRows = valueDirectives.map( function (directive) { const metaKey = '_noindex_seo_' + directive; if ( 'max-image-preview' === directive) { return el( PanelRow, { key: directive + '-row' }, el( SelectControl, { label: __( directive, 'noindex-seo' ), value: postMeta[metaKey] || '', options: [ { value: '', label: __( 'Off', 'noindex-seo' ) }, { value: 'none', label: __( 'none', 'noindex-seo' ) }, { value: 'standard', label: __( 'standard', 'noindex-seo' ) }, { value: 'large', label: __( 'large', 'noindex-seo' ) }, ], onChange: function (value) { updateValue( metaKey, value ); }, } ) ); } const inputType = ( 'unavailable_after' === directive) ? 'datetime-local' : 'number'; return el( PanelRow, { key: directive + '-row' }, el( TextControl, { type: inputType, label: __( directive, 'noindex-seo' ), value: ( postMeta[metaKey] || '' ).toString().replace( ' ', 'T' ), onChange: function (value) { updateValue( metaKey, value ); }, } ) ); } ); return el( PluginDocumentSettingPanel, { name: 'noindex-seo-panel', title: __( 'Search Engine Visibility', 'noindex-seo' ), icon: 'visibility', }, [ // Override checkbox. el( PanelRow, { key: 'override-row' }, el( CheckboxControl, { label: __( 'Override global settings', 'noindex-seo' ), help: __( 'Enable to set custom robots directives for this content', 'noindex-seo' ), checked: override, onChange: handleOverrideChange, } ) ), // Directives (shown only if override is enabled). override && el( 'div', { key: 'directives-container', style: { borderTop: '1px solid #ddd', paddingTop: '16px', marginTop: '8px', } }, [ el( 'p', { key: 'directives-label', style: { fontSize: '12px', fontWeight: '600', marginBottom: '12px', color: '#1e1e1e', } }, __( 'Robots Directives:', 'noindex-seo' ) ), booleanRows, valueRows, // Preview section. activeDirectives.length > 0 && el( 'div', { key: 'preview-section', style: { marginTop: '16px', padding: '12px', background: '#f0f6fc', border: '1px solid #0969da', borderRadius: '4px', } }, [ el( 'p', { key: 'preview-label', style: { fontSize: '11px', fontWeight: '600', marginBottom: '8px', color: '#0969da', textTransform: 'uppercase', } }, __( 'Active Directives:', 'noindex-seo' ) ), el( 'code', { key: 'preview-code', style: { fontSize: '12px', display: 'block', padding: '8px', background: '#fff', border: '1px solid #d0d7de', borderRadius: '3px', wordBreak: 'break-word', } }, activeDirectives.join( ', ' ) ) ] ) ] ) ] ); }; // Register the plugin. registerPlugin( 'noindex-seo-sidebar', { render: NoindexSeoSidebarPanel, } ); })( window.wp );