Permalink
Browse files

Add WP REST API Support. Fixes #59

  • Loading branch information...
lesterchan committed Feb 20, 2018
1 parent 5e52e92 commit 2a14c75288aaa6f4a4990505597f9fd89f0039b6
Showing with 225 additions and 2 deletions.
  1. +36 −1 README.md
  2. +183 −0 class-api.php
  3. +1 −1 class-command.php
  4. +5 −0 wp-sweep.php
View
@@ -49,6 +49,37 @@ Following delete functions are used:
* delete_transient()
* delete_site_transient()
WP-Sweep WP REST API Endpoints
* `/wp-json/sweep/v1/count/<Name>`. Get the number of items that we will be sweeping.
* `/wp-json/sweep/v1/details/<Name>`. Get the details of the items that we will be sweeping.
* `/wp-json/sweep/v1/sweep/<Name>`. Runs sweep for that particular item.
WP-Sweep WP-CLI Commands
* `wp sweep --all`. Runs sweep for all items.
* `wp sweep <Name>`. Runs sweep for that particular item.
* `wp sweep <Name1> <Name2>`. Run sweep for the selected items.
WP-Sweep Available Items:
* revisions
* auto_drafts
* deleted_posts
* unapproved_comments
* spam_comments
* deleted_comments
* transient_options
* orphan_postmeta
* orphan_commentmeta
* orphan_usermeta
* orphan_termmeta
* orphan_term_relationships
* unused_terms
* duplicated_postmeta
* duplicated_commentmeta
* duplicated_usermeta
* duplicated_termmeta
* optimize_database
* oembed_postmeta
WP-Sweep is not compatible with the following plugins:
* [Meta Slider](https://wordpress.org/support/plugin/ml-slider/)
* [Viba Portfolio](https://codecanyon.net/item/viba-portfolio-wordpress-plugin/9561599)
@@ -66,8 +97,12 @@ WP-Sweep is not compatible with the following plugins:
I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.
## Changelog
### 1.1.0
* NEW: Added WP Rest API Endpoint support, sweep/v1/count/<name>, sweep/v1/details/<name>, and sweep/v1/sweep/<name>
* FIXED: Follow as close as possible to WordPress Coding Standards
### 1.0.12
* NEw: Bump to WordPress 4.9
* NEW: Bump to WordPress 4.9
* NEW: Update README to incompatible plugins
### 1.0.10
View
@@ -0,0 +1,183 @@
<?php
/**
* WP-Sweep WP-API
*
* @package wp-sweep
*/
/**
* Class WPSweep_Api
*/
class WPSweep_Api {
/**
* WP-Sweep WP Rest API namespace
*
* @var string
*/
private $namespace = 'sweep/v1';
/**
* List of sweeps
*
* @var array
*/
private $sweeps = array(
'revisions',
'auto_drafts',
'deleted_posts',
'unapproved_comments',
'spam_comments',
'deleted_comments',
'transient_options',
'orphan_postmeta',
'orphan_commentmeta',
'orphan_usermeta',
'orphan_termmeta',
'orphan_term_relationships',
'unused_terms',
'duplicated_postmeta',
'duplicated_commentmeta',
'duplicated_usermeta',
'duplicated_termmeta',
'optimize_database',
'oembed_postmeta',
);
/**
* Register WP-Sweep API Routes
*
* @since 1.1.0
*
* @access public
* @return void
*/
public function __construct() {
add_action( 'rest_api_init', function() {
register_rest_route( $this->namespace, 'count/(?P<name>\w+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'count' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'validate_callback' => array( $this, 'is_sweep_name_valid' ),
),
),
));
register_rest_route( $this->namespace, 'details/(?P<name>\w+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'details' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'validate_callback' => array( $this, 'is_sweep_name_valid' ),
),
),
));
register_rest_route( $this->namespace, 'sweep/(?P<name>\w+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'sweep' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'validate_callback' => array( $this, 'is_sweep_name_valid' ),
),
),
));
});
}
/**
* Sweep item count
*
* @since 1.1.0
*
* @access public
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function count( $request ) {
$params = $request->get_params();
$sweep = new WPSweep();
$count = (int) $sweep->count( $params['name'] );
return new WP_REST_Response( array(
'name' => $params['name'],
'count' => $count,
) );
}
/**
* Sweep details
*
* @since 1.1.0
*
* @access public
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function details( $request ) {
$params = $request->get_params();
$sweep = new WPSweep();
$details = $sweep->details( $params['name'] );
return new WP_REST_Response( array(
'name' => $params['name'],
'count' => count( $details ),
'data' => $details,
) );
}
/**
* Lets do the sweeping
*
* @since 1.1.0
*
* @access public
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function sweep( $request ) {
$params = $request->get_params();
$sweep = new WPSweep();
$results = $sweep->sweep( $params['name'] );
return new WP_REST_Response( array(
'success' => ! empty( $results ),
'name' => $params['name'],
'message' => empty( $results ) ? __( 'No items left to sweep.', 'wp-sweep' ) : $results,
) );
}
/**
* Check whether a sweep is valid
*
* @since 1.1.0
*
* @access public
* @param string $name Sweep name.
* @return bool Is the sweep name valid?
*/
public function is_sweep_name_valid( $name ) {
return in_array( $name, $this->sweeps, true );
}
/**
* Check whether the function is allowed to be run. Must have either capabilities to enact action, or a valid nonce.
*
* @since 1.1.0
*
* @access public
* @return bool Does the user has access to sweep?
*/
public function permission_check() {
return current_user_can( 'manage_options' );
}
}
new WPSweep_Api();
View
@@ -37,7 +37,7 @@ class WPSweep_Command extends WP_CLI_Command {
* duplicated_usermeta
* duplicated_termmeta
* optimize_database
* oembed_postmet
* oembed_postmeta
*
* ## EXAMPLES
*
View
@@ -40,6 +40,11 @@
*/
define( 'WP_SWEEP_VERSION', '1.1.0' );
/**
* WP Rest API
*/
require __DIR__ . '/class-api.php';
/**
* WP-Sweep class
*

0 comments on commit 2a14c75

Please sign in to comment.