Skip to content

Commit

Permalink
Fix delete command (#144)
Browse files Browse the repository at this point in the history
* Fix delete command

* Unset limit param

* Update CHANGELOG.md
  • Loading branch information
HPiirainen authored Oct 21, 2024
1 parent 63e0965 commit 0ce6521
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 54 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [2.0.12] - 2024-10-21

### Fixed
- Fix delete command.

## [2.0.11] - 2024-09-06

### Fixed
Expand Down
55 changes: 32 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,33 @@ RediPress is also built with extensive amount of hooks and filters to customize

<!-- code_chunk_output -->

- [Requirements](#requirements)
- [Installation and initialization](#installation-and-initialization)
- [Usage](#usage)
- [Extra parameters](#extra-parameters)
- [Weights](#weights)
- [Post types](#post-types)
- [Authors](#authors)
- [Taxonomy terms](#taxonomy-terms)
- [Meta values](#meta-values)
- [Fuzzy matching](#fuzzy-matching)
- [Multisite search](#multisite-search)
- [Expanding](#expanding)
- [Adding custom fields](#adding-custom-fields)
- [Modifying the post object](#modifying-the-post-object)
- [Third party plugins](#third-party-plugins)
- [Advanced Custom Fields](#advanced-custom-fields)
- [Polylang](#polylang)
- [Troubleshooting](#troubleshooting)
- [WP-CLI](#WP-CLI)
- [RediPress](#redipress)
- [Table of Contents](#table-of-contents)
- [Requirements](#requirements)
- [Installation and initialization](#installation-and-initialization)
- [Indexing multisite installations](#indexing-multisite-installations)
- [Usage](#usage)
- [Extra parameters](#extra-parameters)
- [Geolocation](#geolocation)
- [Sorting](#sorting)
- [Weights](#weights)
- [Post types](#post-types)
- [Authors](#authors)
- [Taxonomy terms](#taxonomy-terms)
- [Meta values](#meta-values)
- [Fuzzy matching](#fuzzy-matching)
- [Multisite search](#multisite-search)
- [Filters](#filters)
- [Query parts](#query-parts)
- [Expanding](#expanding)
- [Adding custom fields](#adding-custom-fields)
- [Modifying the post object](#modifying-the-post-object)
- [Third party plugins](#third-party-plugins)
- [Advanced Custom Fields](#advanced-custom-fields)
- [Polylang](#polylang)
- [Troubleshooting](#troubleshooting)
- [WP-CLI](#wp-cli)
- [command: wp redipress delete](#command-wp-redipress-delete)

<!-- /code_chunk_output -->

Expand Down Expand Up @@ -328,12 +336,13 @@ If you run into problems you can try dropping all indexes by running `wp redipre

### command: wp redipress delete

description: Delete posts from the index by the arguments.
arguments:
Delete posts from the index by the arguments.

Arguments:
- `document field:` Any field from the document that should match the given value.
- `limit:` If not defined the default value is 100.

example usage:
Example usage:
```bash
wp redipress delete --blog_id=1 --post_type=article --limit=500
wp redipress delete posts --blog_id=1 --post_type=article --limit=500
```
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: RediPress
* Plugin URI: https://github.com/devgeniem/redipress
* Description: A WordPress plugin that provides a blazing fast search engine and WP Query performance enhancements.
* Version: 2.0.11
* Version: 2.0.12
* Author: Geniem
* Author URI: http://www.geniem.fi/
* License: GPL3
Expand Down
73 changes: 43 additions & 30 deletions src/CLI/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,33 @@

namespace Geniem\RediPress\CLI;

use Geniem\RediPress\Redis\Client;
use WP_CLI;

/**
* RediPress CLI delete command class.
*/
class Delete implements Command {

/**
* RediPress wrapper for the Predis client
*
* @var Client
*/
protected $client;

/**
* Index name
*
* @var string
*/
protected $index;

/**
* Class constructor.
*/
public function __construct() {

// Get RediPress settings.
$this->client = apply_filters( 'redipress/client', null );
$this->index = \Geniem\RediPress\Settings::get( 'index' );
$this->client = \apply_filters( 'redipress/client', null );
}

/**
Expand All @@ -29,39 +41,42 @@ public function __construct() {
* @param array $assoc_args The optional command parameters.
* @return boolean
*/
public function run( array $args = [], array $assoc_args = [] ) : bool {

public function run( array $args = [], array $assoc_args = [] ): bool {
// Default limit to 100.
$limit = $assoc_args['limit'] ?? '100';

// Blog_id and post_type.
if ( ! empty( $assoc_args ) ) {
// Remove the optional limit parameter, that should not be passed to the Redisearch query.
unset( $assoc_args['limit'] );

if ( count( $args ) === 1 ) {
$this->index = $args[0];
return $this->delete_posts( $assoc_args, $limit );
}

WP_CLI::error( 'RediPress: "delete" command doesn\'t accept more than one parameter.' );
elseif ( count( $args ) > 1 ) {
WP_CLI::error( 'RediPress: "delete" command does not accept more than one parameter.' );
return false;
}

return false;
}

/**
* Delete posts from the index.
*
* @param int $args The query args.
* @param int $query_vars Query limit.
* @param array $args The query args.
* @param int $limit Query limit.
* @return boolean
*/
public function delete_posts( $query_vars, $limit ) {

$doc_ids = $this->get_doc_ids( $query_vars, $limit );
public function delete_posts( $args, $limit ) {
$doc_ids = $this->get_doc_ids( $args, $limit );

$removed_doc_ids = [];

// Loop through doc_ids to be removed and
// run the delete command in RediSearch index.
if ( ! empty( $doc_ids ) && is_array( $doc_ids ) ) {
foreach ( $doc_ids as $doc_id ) {
$this->delete_index( $doc_id );
$this->delete_from_index( $doc_id );
$removed_doc_ids[] = $doc_id;
}
}
Expand All @@ -83,19 +98,19 @@ public function delete_posts( $query_vars, $limit ) {
/**
* Get the doc ids.
*
* @param int $args The query args.
* @param int $query_vars Query limit.
* @param array $args The query args.
* @param int $limit Query limit.
* @return array An array of doc ids.
*/
protected function get_doc_ids( $query_vars, $limit ) {
protected function get_doc_ids( $args, $limit ) {

// Get the posts.
// Do the RediSearch query.
$doc_ids = $this->client->raw_command(
'FT.SEARCH',
[
$this->index,
$this->build_where( $query_vars ),
$this->build_where( $args ),
'RETURN',
1,
'post_id',
Expand All @@ -118,13 +133,11 @@ protected function get_doc_ids( $query_vars, $limit ) {
* @param string $doc_id RediSearch doc_id.
* @return void
*/
protected function delete_index( $doc_id ) {

// Do the delete.
protected function delete_from_index( $doc_id ) {
if ( is_string( $doc_id ) ) {
// Do the delete from index.
// Delete post from the index.
$this->client->raw_command(
'HDEL',
'DEL',
[
$this->index,
$doc_id,
Expand Down Expand Up @@ -166,7 +179,7 @@ public function build_where( $query_vars ) {
$where = $where . ' ';
}

$idx++;
++$idx;
}
}
}
Expand All @@ -179,16 +192,16 @@ public function build_where( $query_vars ) {
*
* @return integer
*/
public static function get_min_parameters() : int {
return 0;
public static function get_min_parameters(): int {
return 1;
}

/**
* Returns the maximum amount of parameters the command accepts.
*
* @return integer
*/
public static function get_max_parameters() : int {
return 3;
public static function get_max_parameters(): int {
return 1;
}
}

0 comments on commit 0ce6521

Please sign in to comment.