rules = $rules; $this->settings = $settings; add_action( 'rest_api_init', array( $this, 'register_routes' ) ); } /** * Permission callback. * * @return bool */ public function can_manage() { return current_user_can( Hidexo_Lite_Plugin::capability() ); } /** * Register routes. */ public function register_routes() { $id_arg = array( 'id' => array( 'type' => 'string', 'required' => true, 'validate_callback' => function ( $v ) { return is_string( $v ) && (bool) preg_match( '/^[A-Za-z0-9-]{8,64}$/', $v ); }, ), ); register_rest_route( self::NS, '/rules', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'list_rules' ), 'permission_callback' => array( $this, 'can_manage' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'create_rule' ), 'permission_callback' => array( $this, 'can_manage' ), ), ) ); register_rest_route( self::NS, '/rules/(?P[A-Za-z0-9-]+)', array( array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_rule' ), 'permission_callback' => array( $this, 'can_manage' ), 'args' => $id_arg, ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_rule' ), 'permission_callback' => array( $this, 'can_manage' ), 'args' => $id_arg, ), ) ); register_rest_route( self::NS, '/rules/(?P[A-Za-z0-9-]+)/status', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'set_status' ), 'permission_callback' => array( $this, 'can_manage' ), 'args' => $id_arg, ) ); register_rest_route( self::NS, '/rules/(?P[A-Za-z0-9-]+)/health', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'set_health' ), 'permission_callback' => array( $this, 'can_manage' ), 'args' => $id_arg, ) ); register_rest_route( self::NS, '/settings', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => function () { return rest_ensure_response( $this->settings->get() ); }, 'permission_callback' => array( $this, 'can_manage' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'save_settings' ), 'permission_callback' => array( $this, 'can_manage' ), ), ) ); } /** * Dashboard payload for the current state. * * @return array */ private function snapshot() { return array( 'rules' => array_map( array( $this->rules, 'public_view' ), $this->rules->all() ), 'active' => $this->rules->count_active(), 'hidden' => $this->rules->hidden_element_count(), ); } /** * GET /rules. * * @return WP_REST_Response */ public function list_rules() { return rest_ensure_response( $this->snapshot() ); } /** * POST /rules. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function create_rule( WP_REST_Request $request ) { $body = $request->get_json_params(); if ( ! is_array( $body ) ) { return new WP_Error( 'hidexo_bad_request', __( 'Invalid request.', 'hidexo-lite' ), array( 'status' => 400 ) ); } $clean = Hidexo_Lite_Rule_Validator::sanitize_new( $body ); if ( is_wp_error( $clean ) ) { return $clean; } $rule = $this->rules->create( $clean ); if ( is_wp_error( $rule ) ) { return $rule; } return rest_ensure_response( array_merge( $this->snapshot(), array( 'rule' => $this->rules->public_view( $rule ) ) ) ); } /** * PUT/PATCH /rules/: edit name and scope. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function update_rule( WP_REST_Request $request ) { $body = $request->get_json_params(); $body = is_array( $body ) ? $body : array(); $res = $this->rules->update_fields( $request['id'], $body ); if ( is_wp_error( $res ) ) { return $res; } return rest_ensure_response( $this->snapshot() ); } /** * POST /rules//status: pause or activate. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function set_status( WP_REST_Request $request ) { $body = $request->get_json_params(); $status = ( is_array( $body ) && isset( $body['status'] ) && 'active' === $body['status'] ) ? 'active' : 'paused'; $res = $this->rules->set_status( $request['id'], $status ); if ( is_wp_error( $res ) ) { return $res; } return rest_ensure_response( $this->snapshot() ); } /** * DELETE /rules/. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function delete_rule( WP_REST_Request $request ) { if ( ! $this->rules->delete( $request['id'] ) ) { return new WP_Error( 'hidexo_not_found', __( 'Rule not found.', 'hidexo-lite' ), array( 'status' => 404 ) ); } return rest_ensure_response( $this->snapshot() ); } /** * POST /rules//health. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function set_health( WP_REST_Request $request ) { $body = $request->get_json_params(); $body = is_array( $body ) ? $body : array(); $res = $this->rules->set_health( $request['id'], isset( $body['resolved'] ) ? (int) $body['resolved'] : 0, isset( $body['total'] ) ? (int) $body['total'] : 0 ); if ( is_wp_error( $res ) ) { return $res; } return rest_ensure_response( $this->snapshot() ); } /** * POST /settings. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function save_settings( WP_REST_Request $request ) { $body = $request->get_json_params(); return rest_ensure_response( $this->settings->update( is_array( $body ) ? $body : array() ) ); } }