bulk-get-entries.php
3 weeks ago
delete-entry.php
5 months ago
entry-parser.php
3 weeks ago
get-entry.php
3 weeks ago
list-entries.php
3 weeks ago
update-entry-status.php
5 months ago
update-entry-status.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Update Entry Status Ability. |
| 4 | * |
| 5 | * @package sureforms |
| 6 | * @since 2.5.2 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc\Abilities\Entries; |
| 10 | |
| 11 | use SRFM\Inc\Abilities\Abstract_Ability; |
| 12 | use SRFM\Inc\Entries; |
| 13 | use SRFM\Inc\Helper; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; // Exit if accessed directly. |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Update_Entry_Status ability class. |
| 21 | * |
| 22 | * Updates the status of one or more form submission entries. |
| 23 | * |
| 24 | * @since 2.5.2 |
| 25 | */ |
| 26 | class Update_Entry_Status extends Abstract_Ability { |
| 27 | /** |
| 28 | * Maximum number of entries that can be updated in a single call. |
| 29 | * |
| 30 | * @since 2.5.2 |
| 31 | */ |
| 32 | public const MAX_ENTRIES = 50; |
| 33 | |
| 34 | /** |
| 35 | * Constructor. |
| 36 | * |
| 37 | * @since 2.5.2 |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | $this->id = 'sureforms/update-entry-status'; |
| 41 | $this->label = __( 'Update Entry Status', 'sureforms' ); |
| 42 | $this->description = __( 'Update the status of one or more SureForms form submission entries. Supports read, unread, trash, and restore operations.', 'sureforms' ); |
| 43 | $this->capability = 'manage_options'; |
| 44 | $this->gated = 'srfm_abilities_api_edit'; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * {@inheritDoc} |
| 49 | * |
| 50 | * @since 2.5.2 |
| 51 | */ |
| 52 | public function get_annotations() { |
| 53 | return [ |
| 54 | 'readonly' => false, |
| 55 | 'destructive' => true, |
| 56 | 'idempotent' => true, |
| 57 | 'priority' => 2.0, |
| 58 | 'openWorldHint' => false, |
| 59 | 'instructions' => 'Confirm the target status with the user before executing. Trashing entries moves them toward permanent deletion.', |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritDoc} |
| 65 | * |
| 66 | * @since 2.5.2 |
| 67 | */ |
| 68 | public function get_input_schema() { |
| 69 | return [ |
| 70 | 'type' => 'object', |
| 71 | 'additionalProperties' => false, |
| 72 | 'properties' => [ |
| 73 | 'entry_ids' => [ |
| 74 | 'type' => 'array', |
| 75 | 'description' => __( 'Array of entry IDs to update.', 'sureforms' ), |
| 76 | 'items' => [ 'type' => 'integer' ], |
| 77 | ], |
| 78 | 'status' => [ |
| 79 | 'type' => 'string', |
| 80 | 'description' => __( 'New status for the entries.', 'sureforms' ), |
| 81 | 'enum' => [ 'read', 'unread', 'trash', 'restore' ], |
| 82 | ], |
| 83 | ], |
| 84 | 'required' => [ 'entry_ids', 'status' ], |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritDoc} |
| 90 | * |
| 91 | * @since 2.5.2 |
| 92 | */ |
| 93 | public function get_output_schema() { |
| 94 | return [ |
| 95 | 'type' => 'object', |
| 96 | 'properties' => [ |
| 97 | 'success' => [ 'type' => 'boolean' ], |
| 98 | 'updated' => [ 'type' => 'integer' ], |
| 99 | 'errors' => [ |
| 100 | 'type' => 'array', |
| 101 | 'items' => [ 'type' => 'string' ], |
| 102 | ], |
| 103 | ], |
| 104 | ]; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Execute the update-entry-status ability. |
| 109 | * |
| 110 | * @param array<string,mixed> $input Validated input data. |
| 111 | * @since 2.5.2 |
| 112 | * @return array<string,mixed>|\WP_Error |
| 113 | */ |
| 114 | public function execute( $input ) { |
| 115 | $entry_ids = $input['entry_ids'] ?? []; |
| 116 | $status = sanitize_text_field( Helper::get_string_value( $input['status'] ?? '' ) ); |
| 117 | |
| 118 | if ( empty( $entry_ids ) || ! is_array( $entry_ids ) ) { |
| 119 | return new \WP_Error( |
| 120 | 'srfm_missing_entry_ids', |
| 121 | __( 'At least one entry ID is required.', 'sureforms' ), |
| 122 | [ 'status' => 400 ] |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | if ( count( $entry_ids ) > self::MAX_ENTRIES ) { |
| 127 | return new \WP_Error( |
| 128 | 'srfm_too_many_entry_ids', |
| 129 | /* translators: %d is the maximum number of entries allowed. */ |
| 130 | sprintf( __( 'Maximum %d entry IDs allowed per request.', 'sureforms' ), self::MAX_ENTRIES ), |
| 131 | [ 'status' => 400 ] |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | $entry_ids = array_map( 'absint', $entry_ids ); |
| 136 | |
| 137 | if ( empty( $status ) ) { |
| 138 | return new \WP_Error( |
| 139 | 'srfm_missing_status', |
| 140 | __( 'Status is required.', 'sureforms' ), |
| 141 | [ 'status' => 400 ] |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | return Entries::update_status( $entry_ids, $status ); |
| 146 | } |
| 147 | } |
| 148 |