NoteActions.php
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Admin Note Action controller |
| 4 | * |
| 5 | * Handles requests to the admin note action endpoint. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\Notes\Note; |
| 13 | use Automattic\WooCommerce\Admin\Notes\Notes as NotesFactory; |
| 14 | use Automattic\WooCommerce\Internal\Admin\Notes\NoteActionForbiddenException; |
| 15 | |
| 16 | /** |
| 17 | * REST API Admin Note Action controller class. |
| 18 | * |
| 19 | * @internal |
| 20 | * @extends WC_REST_CRUD_Controller |
| 21 | */ |
| 22 | class NoteActions extends Notes { |
| 23 | |
| 24 | /** |
| 25 | * Register the routes for admin notes. |
| 26 | */ |
| 27 | public function register_routes() { |
| 28 | register_rest_route( |
| 29 | $this->namespace, |
| 30 | '/' . $this->rest_base . '/(?P<note_id>[\d-]+)/action/(?P<action_id>[\d-]+)', |
| 31 | array( |
| 32 | 'args' => array( |
| 33 | 'note_id' => array( |
| 34 | 'description' => __( 'Unique ID for the Note.', 'woocommerce' ), |
| 35 | 'type' => 'integer', |
| 36 | ), |
| 37 | 'action_id' => array( |
| 38 | 'description' => __( 'Unique ID for the Note Action.', 'woocommerce' ), |
| 39 | 'type' => 'integer', |
| 40 | ), |
| 41 | ), |
| 42 | array( |
| 43 | 'methods' => \WP_REST_Server::EDITABLE, |
| 44 | 'callback' => array( $this, 'trigger_note_action' ), |
| 45 | // @todo - double check these permissions for taking note actions. |
| 46 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 47 | ), |
| 48 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Trigger a note action. |
| 55 | * |
| 56 | * @param WP_REST_Request $request Full details about the request. |
| 57 | * @return WP_REST_Request|WP_Error |
| 58 | */ |
| 59 | public function trigger_note_action( $request ) { |
| 60 | $note = NotesFactory::get_note( $request->get_param( 'note_id' ) ); |
| 61 | |
| 62 | if ( ! $note ) { |
| 63 | return new \WP_Error( |
| 64 | 'woocommerce_note_invalid_id', |
| 65 | __( 'Sorry, there is no resource with that ID.', 'woocommerce' ), |
| 66 | array( 'status' => 404 ) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | $note->set_is_read( true ); |
| 71 | $note->save(); |
| 72 | |
| 73 | $triggered_action = NotesFactory::get_action_by_id( $note, $request->get_param( 'action_id' ) ); |
| 74 | |
| 75 | if ( ! $triggered_action ) { |
| 76 | return new \WP_Error( |
| 77 | 'woocommerce_note_action_invalid_id', |
| 78 | __( 'Sorry, there is no resource with that ID.', 'woocommerce' ), |
| 79 | array( 'status' => 404 ) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | try { |
| 84 | $triggered_note = NotesFactory::trigger_note_action( $note, $triggered_action ); |
| 85 | } catch ( NoteActionForbiddenException $e ) { |
| 86 | // Handlers hooked into `woocommerce_note_action[_*]` throw this typed |
| 87 | // exception when the current user lacks the per-action capability the |
| 88 | // handler enforces (the route-level permission check is intentionally |
| 89 | // coarser). Convert it to a 403 so REST clients get correct HTTP |
| 90 | // semantics. Any other exception bubbles uncaught so genuine server |
| 91 | // faults surface as 500s instead of being masked as auth errors. |
| 92 | // |
| 93 | // The ignore below matches the same `return.type` issue already captured |
| 94 | // in the PHPStan baseline for the other two WP_Error returns in this |
| 95 | // method (broken `@return` docblock — unqualified WP class names resolve |
| 96 | // in the current namespace). Localized here so the baseline doesn't grow. |
| 97 | // @phpstan-ignore-next-line return.type -- see rationale above. |
| 98 | return new \WP_Error( |
| 99 | 'woocommerce_note_action_forbidden', |
| 100 | $e->getMessage(), |
| 101 | array( 'status' => rest_authorization_required_code() ) |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | $data = $triggered_note->get_data(); |
| 106 | $data = $this->prepare_item_for_response( $data, $request ); |
| 107 | $data = $this->prepare_response_for_collection( $data ); |
| 108 | |
| 109 | return rest_ensure_response( $data ); |
| 110 | } |
| 111 | } |
| 112 |