1

I've added a checkbox on my Gutenberg post, that when checked, is supposed to prevent Gutenberg from updating the post's post_modified and post_modified_gmt columns. The checkbox manages its own state, but the status of the checkbox isn't submitted when I update the post.

My block

Here's the code to add the checkbox (I'm aware I should be using JSX instead, but I don't like it).

'use strict';
if (window.wp) {

    (function (wp) {

        // Only applies to published posts. NOT WORKING.
        //if(wp.data.select('core/editor').getEditedPostAttribute('status') !== 'publish') return;

        var registerPlugin = wp.plugins.registerPlugin,
            PluginPostStatusInfo = wp.editPost.PluginPostStatusInfo,
            CheckboxControl = wp.components.CheckboxControl,
            withSelect = wp.data.withSelect,
            __ = wp.i18n.__;

        var LastUpdatedTime = withSelect(function (select) {
            return {
                'lastUpdated': select('core/editor').getEditedPostAttribute('modified'),
            };
        })(function (props) {
            return [ 
                wp.element.createElement(
                    'span',
                    null,
                    __('Last Modified','rabbit')
                ),
                wp.element.createElement(
                    'div',
                    null,
                    wp.element.createElement(
                        'strong',
                        null,
                        props.lastUpdated
                    )
                )
            ];
        });

        var PluginPostStatusInfoUpdateLastModified = function () {
            var [preventUpdateTime, setPreventUpdateTime] = wp.element.useState(false),
                handleCheckboxChange = function() {
                    setPreventUpdateTime(!preventUpdateTime);
                };
            
            return wp.element.createElement(
                PluginPostStatusInfo,
                { 'className': 'edit-post-update-last-modified' },
                wp.element.createElement(CheckboxControl, {
                    'label': __('Do not update last modified time','rabbit'),
                    'checked': preventUpdateTime,
                    'onChange': handleCheckboxChange,
                    'name': 'no_update_last_modified'
                })
            );
        };
        
        var PluginPostStatusInfoLastModified = function () {
            return wp.element.createElement(
                PluginPostStatusInfo,
                { 'className': 'edit-post-last-modified' },
                wp.element.createElement(LastUpdatedTime)
            );
        };

        registerPlugin('post-status-info-last-modified', { 'render': PluginPostStatusInfoLastModified });
        registerPlugin('post-status-info-update-last-modified', { 'render': PluginPostStatusInfoUpdateLastModified });
        
    })(window.wp);

}

Because the checkbox's state isn't sent to PHP, I cannot detect if it is checked in the wp_insert_post_data filter:

// shit don't work
if($_POST['no_update_last_modified']) { ... }

But beyond that, even if I just make both wp_insert_post_data and rest_prepare_post unset the last modified and last modified GMT attributes, they just both straight up fail, so I'm suspecting Gutenberg uses another method to update the last modified date of my post:

static function wp_insert_post_data($data, $postarr, $unsanitized_postarr, $update) {
        
        if(!$update) return $data;
        
        // Check if it's the right post type to apply this modification
        if(in_array($data['post_type'], self::$post_types)) { // && !empty($_POST['no_update_last_modified'])) {
            unset($data['post_modified'], $data['post_modified_gmt']);
        }
        
        return $data;
    }
    
    static function rest_prepare_post( $data, $post, $request ) {
        
        if(!is_a($data,'WP_REST_Response')) return $data;
        
        // Check if it's a REST API request
        $arr = $data->get_data();

        unset( $arr['modified'], $arr['modified_gmt'] );
        
        $data->set_data( $arr );
        set_transient('REST',$data);
        return $data;
    }

Anyone can help? Gutenberg has abysmal documentation. It is so difficult to find literally any good documentation on how to do anything with it.

P.S. Yes, I know I can use a metabox to do it, but it is much sleeker to integrate this into Gutenberg.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.