Support WordPress Rest API
bhubbard opened this issue · 2 comments
bhubbard commented
I would love to see support for the rest api, here is a quick working code example:
/**
* Sweep Rest Routes initialization class.
*/
class Sweep_Rest_Api {
/**
* Create the rest API routes.
*
* @access public
* @return void
*/
public function __construct() {
add_action( 'rest_api_init', function(){
register_rest_route( 'sweep/v1', 'sweep', array(
'methods' => array( 'get', 'delete' ),
'callback' => array( $this, 'sweep' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'default' => '',
'description' => 'What would you like to sweep?',
'type' => 'string',
),
),
));
});
}
/**
* get_stream function.
*
* @access public
* @return void
*/
public function sweep( WP_REST_Request $request ) {
$name = $request['name'];
$sweep = new WPSweep();
$results = $sweep->sweep( $name );
return rest_ensure_response( $results );
}
/**
* Check whether the function is allowed to be run.
*
* Must have either capabilities to enact action, or a valid nonce.
*
* @param [type] $data [description]
* @return [type] [description]
*/
public function permission_check( $data ) {
if ( ! current_user_can( 'manage_options' ) ) {
return new WP_Error( 'forbidden', 'You are not allowed to do that.', array( 'status' => 403 ) );
}
return true;
}
}
new Sweep_Rest_Api();
lesterchan commented
Not sure why we need an API for a sweep. If you need to cron it, you can use WP CLI with it.
Having said that, if there are enough people request for it, I can consider =)
bhubbard commented
Having a rest endpoint is a great way to sweep multiple installs at once, especially when they are on different servers so you can't use WP-CLI.