alleyinteractive / wp-bulk-task

A library to assist with running performant bulk tasks against WordPress database objects.
GNU General Public License v2.0
13 stars 0 forks source link

Add support for disabling of updating the post modified date #31

Closed mogmarsh closed 2 months ago

mogmarsh commented 2 months ago

Description

It is a common use case to not update the post date when running a bulk task. I think it would make sense to add this functionality into the Bulk Task with some sort of way to enable it for a task.

The code to do it looks something like the following, and currently must be copied from project to project every time it is needed.

add_filter( 'wp_insert_post_data', [ $this, 'stop_modified_date_update' ], 10, 2 );

(do your update)

remove_filter( 'wp_insert_post_data', [ $this, 'stop_modified_date_update' ], 10, 2 );

/**
     * Returns the previous modified date for a post.
     *
     * @param array $new The new post data.
     * @param array $old The old post data.
     * @return array
     */
    public function stop_modified_date_update( $new, $old ): array {
        if ( ! isset( $old['post_modified'] ) || ! isset( $old['post_modified_gmt'] ) ) {
            return $new;
        }
        $new['post_modified']     = $old['post_modified'];
        $new['post_modified_gmt'] = $old['post_modified_gmt'];
        return $new;
    }

Use Case

When instantiating a bulk task or running a bulk task, users could set an option or flag to disable updating the post_modified and post_modified_gmt values.

renatonascalves commented 2 months ago

@mogmarsh Had you have a chance to look at our custom trait? It was designed to support this scenario, and others.

https://github.com/alleyinteractive/wp-bulk-task/blob/6772f7ab6abd5e1209ba5b80fb37cf6a6cf1e33d/src/trait-bulk-task-side-effects.php#L50

renatonascalves commented 2 months ago

We have doc on how to use it here: https://github.com/alleyinteractive/wp-bulk-task?tab=readme-ov-file#use

mogmarsh commented 2 months ago

@renatonascalves Nice! I hadn't seen that. I will close this issue.

mogmarsh commented 2 months ago

already exists