namespace, 'count/(?P\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\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\w+)', array( 'methods' => WP_REST_Server::DELETABLE, '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( 'activate_plugins' ); } }