Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ wp revisions dump [--hard] [--yes]
**OPTIONS**

[--hard]
Hard delete. Slower, uses wp_delete_post_revision(). Alias to wp revisions clean -1
Hard delete. Slower, uses wp_delete_post_revision(). Alias to `wp revisions clean -1`

[--yes]
Answer yes to the confirmation message.
Expand All @@ -74,22 +74,25 @@ wp revisions dump [--hard] [--yes]
Delete old revisions

~~~
wp revisions clean [<keep>] [--post_type=<post-type>] [--after-date=<yyyy-mm-dd>] [--before-date=<yyyy-mm-dd>] [--post_id=<post-id>] [--hard] [--dry-run]
wp revisions clean [<keep>] [--filter-keep] [--post_type=<post-type>] [--after-date=<yyyy-mm-dd>] [--before-date=<yyyy-mm-dd>] [--post_id=<post-id>] [--hard] [--dry-run]
~~~

**OPTIONS**

[<keep>]
Number of revisions to keep per post. Defaults to WP_POST_REVISIONS if it is an integer
Number of revisions to keep per post. Defaults to WP_POST_REVISIONS if it is an integer.

[--filter-keep]
Allow `wp_revisions_to_keep` filter to override keep number.

[--post_type=<post-type>]
Clean revisions for given post type(s). Default: any

[--after-date=<yyyy-mm-dd>]
Clean revisions on posts published on or after this date. Default: none.
Clean revisions on posts published on or after this date (GMT). Default: none.

[--before-date=<yyyy-mm-dd>]
Clean revisions on posts published on or before this date. Default: none.
Clean revisions on posts published before this date (GMT). Default: none.

[--post_id=<post-id>]
Clean revisions for given post.
Expand All @@ -116,7 +119,7 @@ wp revisions clean [<keep>] [--post_type=<post-type>] [--after-date=<yyyy-mm-dd>
Generate revisions

~~~
wp revisions generate [<count>] [--post_type=<post-type>] [--post_id=<post-id>]
wp revisions generate [<count>] [--post_type=<post-type>] [--post_id=<post-id>] [--oldest_date=<oldest-date>]
~~~

**OPTIONS**
Expand All @@ -130,6 +133,9 @@ wp revisions generate [<count>] [--post_type=<post-type>] [--post_id=<post-id>]
[--post_id=<post-id>]
Generate revisions for given post.

[--oldest_date=<oldest-date>]
Oldest date for revisions. Default: 5 years ago

**EXAMPLES**

wp revisions generate 10
Expand All @@ -154,11 +160,19 @@ wp revisions status

## Installing

Installing this package requires WP-CLI v2.1 or greater. Update to the latest stable release with `wp cli update`.
Installing this package requires WP-CLI v2.12 or greater. Update to the latest stable release with `wp cli update`.

Once you've done so, you can install the latest stable version of this package with:

```bash
wp package install trepmal/wp-revisions-cli:@stable
```

Once you've done so, you can install this package with:
To install the latest development version of this package, use the following command instead:

wp package install git@github.com:trepmal/wp-revisions-cli.git
```bash
wp package install trepmal/wp-revisions-cli:dev-master
```

## Contributing

Expand All @@ -184,7 +198,7 @@ Once you've decided to commit the time to seeing your pull request through, [ple

## Support

Github issues aren't for general support questions, but there are other venues you can try: https://wp-cli.org/#support
GitHub issues aren't for general support questions, but there are other venues you can try: https://wp-cli.org/#support


*This README.md is generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). To suggest changes, please submit a pull request against the corresponding part of the codebase.*
49 changes: 39 additions & 10 deletions inc/class-revisions-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ function ( $i ) {
// get revisions of those IDs.
$revs = $wpdb->get_results(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- IN statement
"SELECT * FROM $wpdb->posts WHERE post_type = 'revision' AND post_parent IN ({$post__in}) ORDER BY post_parent DESC"
"SELECT * FROM $wpdb->posts WHERE post_type = 'revision' AND post_parent IN ({$post__in}) ORDER BY post_parent, post_date, ID DESC"
);
} else {
$revs = [];
}
} else {

$revs = $wpdb->get_results(
"SELECT * FROM $wpdb->posts WHERE post_type = 'revision' ORDER BY post_parent DESC"
"SELECT * FROM $wpdb->posts WHERE post_type = 'revision' ORDER BY post_parent, post_date, ID DESC"
);

}
Expand Down Expand Up @@ -176,10 +176,10 @@ public function status( $args = array(), $assoc_args = array() ) {
* : Clean revisions for given post type(s). Default: any
*
* [--after-date=<yyyy-mm-dd>]
* : Clean revisions on posts published on or after this date. Default: none.
* : Clean revisions on posts published on or after this date (GMT). Default: none.
*
* [--before-date=<yyyy-mm-dd>]
* : Clean revisions on posts published on or before this date. Default: none.
* : Clean revisions on posts published before this date (GMT). Default: none.
*
* [--post_id=<post-id>]
* : Clean revisions for given post.
Expand Down Expand Up @@ -213,8 +213,8 @@ public function clean( $args = array(), $assoc_args = array() ) {
$filter_keep = WP_CLI\Utils\get_flag_value( $assoc_args, 'filter-keep', false );
$post_type = WP_CLI\Utils\get_flag_value( $assoc_args, 'post_type', false );
$post_id = WP_CLI\Utils\get_flag_value( $assoc_args, 'post_id', false );
$after_date = WP_CLI\Utils\get_flag_value( $assoc_args, 'after_date', false );
$before_date = WP_CLI\Utils\get_flag_value( $assoc_args, 'before_date', false );
$after_date = WP_CLI\Utils\get_flag_value( $assoc_args, 'after-date', false );
$before_date = WP_CLI\Utils\get_flag_value( $assoc_args, 'before-date', false );
$hard = WP_CLI\Utils\get_flag_value( $assoc_args, 'hard', false );
$dry_run = WP_CLI\Utils\get_flag_value( $assoc_args, 'dry-run', false );

Expand Down Expand Up @@ -247,8 +247,9 @@ function ( $i ) {
$strto_aft = $after_date ? strtotime( $after_date ) : false;
$strto_bef = $before_date ? strtotime( $before_date ) : false;

$after_ymd = $strto_aft ? date( 'Y-m-d', $strto_aft ) : false;
$before_ymd = $strto_bef ? date( 'Y-m-d', $strto_bef ) : false;
// use wp_date to get local time, since we query against post_date column
$after_ymd = $strto_aft ? wp_date( 'Y-m-d', $strto_aft ) : false;
$before_ymd = $strto_bef ? wp_date( 'Y-m-d', $strto_bef ) : false;

if ( $after_ymd && $before_ymd ) {
$where .= $wpdb->prepare( ' AND (post_date < %s AND post_date > %s)', $before_ymd, $after_ymd );
Expand Down Expand Up @@ -368,6 +369,9 @@ function ( $i ) {
* [--post_id=<post-id>]
* : Generate revisions for given post.
*
* [--oldest_date=<oldest-date>]
* : Oldest date for revisions. Default: 5 years ago
*
* ## EXAMPLES
*
* wp revisions generate 10
Expand All @@ -379,8 +383,17 @@ public function generate( $args = array(), $assoc_args = array() ) {
$count = WP_CLI\Utils\get_flag_value( $args, 0, 15 );
$count = absint( $count );

$post_id = WP_CLI\Utils\get_flag_value( $assoc_args, 'post_id', false );
$post_type = WP_CLI\Utils\get_flag_value( $assoc_args, 'post_type', false );
$post_id = WP_CLI\Utils\get_flag_value( $assoc_args, 'post_id', false );
$post_type = WP_CLI\Utils\get_flag_value( $assoc_args, 'post_type', false );
$oldest_date = WP_CLI\Utils\get_flag_value( $assoc_args, 'oldest_date', '5 years ago' );

$oldest_date_time = strtotime( $oldest_date );
if ( false === $oldest_date_time ) {
WP_CLI::error( 'oldest_date value invalid' );
}

// distribute revisions between oldest date and current time
$interval = round( ( time() - $oldest_date_time ) / $count );

global $wpdb;

Expand Down Expand Up @@ -415,13 +428,29 @@ public function generate( $args = array(), $assoc_args = array() ) {
$this->start_bulk_operation();

remove_all_filters( 'wp_revisions_to_keep' );
remove_all_filters( 'wp_insert_post_data' );
add_filter( 'wp_save_post_revision_check_for_changes', '__return_false' );

$inc = 0;
foreach ( $posts as $post_id ) {
$notify->tick();

for ( $i = 0; $i < $count; $i++ ) {

$time = $oldest_date_time + ( $interval * $i );

add_filter(
'wp_insert_post_data',
function ( $data ) use ( $time ) {
$data['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', $time );
$data['post_date'] = wp_date( 'Y-m-d H:i:s', $time );
$data['post_modified_gmt'] = gmdate( 'Y-m-d H:i:s', $time );
$data['post_modified'] = wp_date( 'Y-m-d H:i:s', $time );
return $data;
},
10
);

wp_save_post_revision( $post_id );
}
++$inc;
Expand Down
10 changes: 9 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<ruleset name="WP-CLI-PROJECT-NAME">
<description>Custom ruleset for WP-CLI PROJECT NAME</description>
<description>Custom ruleset for WP Revisions CLI</description>

<!-- What to scan. -->
<file>.</file>
Expand All @@ -21,4 +21,12 @@
<!-- Rules: Include the base ruleset for WP-CLI projects. -->
<rule ref="WP_CLI_CS"/>

<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array">
<element value="Revisions"/>
</property>
</properties>
</rule>

</ruleset>