使用日期范围隐藏或显示古登堡自定义块

时间:2018-11-14 作者:Jim-miraidev

我已经创建了一个带有dateFrom和dateTo属性的自定义块(有效),但希望在这些日期之间隐藏前端的块。

不确定这是否可能?

我不仅仅想添加一个类并用css隐藏,因为我不想让块可见,即使在代码中也是如此。如能给我任何建议或给我指出正确的方向,我将不胜感激。

import classnames from \'classnames\'
import ResizableBox from \'re-resizable\'

const { isFinite, find, omit } = window.lodash;
const { __ } = wp.i18n; // Import __() from wp.i18n
const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks
const { 
    PanelBody, 
    DateTimePicker,
} = wp.components;
const { __experimentalGetSettings  } = wp.date;
const { Fragment } = wp.element;
const { compose,withState } = wp.compose;
const {
    InspectorControls,
    InnerBlocks,
} = wp.editor;

registerBlockType( \'block/timed-block\', {
    title: __( \'Timed block\' ), // Block title.
    icon: \'align-center\', // Block icon from Dashicons → https://developer.wordpress.org/resource/dashicons/.
    category: \'layout\', // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed.
    keywords: [
        __( \'section\' ),
        __( \'container\' ),
    ],
    attributes: {
        dateFrom: {
            type: \'strings\',
        },
        dateTo: {
            type: \'string\',
        },
    },
    edit: function( {  attributes, setAttributes,className }) {

        const settings = __experimentalGetSettings();
        const is12HourTime = /a(?!\\\\)/i.test(
            settings.formats.time
                .toLowerCase() // Test only the lower case a
                .replace( /\\\\\\\\/g, \'\' ) // Replace "//" with empty strings
                .split( \'\' ).reverse().join( \'\' ) // Reverse the string and test for "a" not followed by a slash
        );

        const { 
            dateFrom,
            dateTo
        } = attributes;

        return (
            <Fragment>
                <InspectorControls>
                    <PanelBody
                        title={ __( \'Date from\' ) }
                        initialOpen={ false }
                    >
                     <DateTimePicker
                            currentDate={ dateFrom }
                            onChange={ ( date ) => {
                                setAttributes( {
                                    dateFrom: date,
                                } );
                            } }
                            locale={ settings.l10n.locale }
                            is12Hour={ is12HourTime }
                        />
                    </PanelBody>
                    <PanelBody
                        title={ __( \'Date to\' ) }
                        initialOpen={ false }
                    >
                    <DateTimePicker
                            currentDate={ dateTo }
                            onChange={ ( date ) => {
                                setAttributes( {
                                    dateTo: date,
                                } );
                            } }
                            locale={ settings.l10n.locale }
                            is12Hour={ is12HourTime }
                        />

                    </PanelBody>

                </InspectorControls>
                <section
                className={ className }
                >   
                    <InnerBlocks />
                </section>
            </Fragment>
        );
    },

    save: function( { attributes, className }) {
        const { 
            dateFrom,
            dateTo
        } = attributes;

            return (
                <section className={className}>
                <InnerBlocks.Content />
                </section>
            );


    },

    /**
     * wrapper props
     * @param {*} attributes 
     */
    getEditWrapperProps( attributes ) {
        return { \'data-align\': \'wide\' };
    },
} );
更新

我尝试按如下方式添加和删除过滤器,但似乎无法将BlockName输出

remove_filter( \'the_content\', \'do_blocks\', 7 );

add_filter( \'the_content\', function( $content ) {


    $blocks = gutenberg_parse_blocks( $content );
    $output = \'\';

    foreach ( $blocks as $block ) {


         if ( \'block/timed-block\' === $block[\'blockName\'] ) {
             $output .= \'\';
         } else {
            $output .= gutenberg_render_block( $block );
         }

    }
    return $output;

}, 10, 2 );

1 个回复
最合适的回答,由SO网友:birgire 整理而成

下面是一个未经测试的建议,删除类型为的HTML块:

<!-- wp:block/timed-block {"dateFrom":"2018-11-14", "dateTo":"2018-12-18"} -->
    <section>
       ...
    </section>
<!-- /wp:block/timed-block -->
从渲染内容中,使用render_block 过滤器(5.0以上版本提供):

add_filter( \'render_block\', function( $block_content, $block ) {
    // Remove the block/timed-block from the rendered content.
    if ( \'block/timed-block\' === $block[\'blockName\'] ) {
        $block_content = \'\';
    }
    return $block_content;
}, 10, 2 );
可以使用解析的属性进一步处理日期约束$block[\'attr\'][\'dateFrom\']$block[\'attr\'][\'dateTo\'] 根据需要。

希望有帮助!

结束

相关推荐