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
delete-entry.php
133 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Delete Entry 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 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Delete_Entry ability class. |
| 20 | * |
| 21 | * Permanently deletes one or more form submission entries. |
| 22 | * |
| 23 | * @since 2.5.2 |
| 24 | */ |
| 25 | class Delete_Entry extends Abstract_Ability { |
| 26 | /** |
| 27 | * Maximum number of entries that can be deleted in a single call. |
| 28 | * |
| 29 | * @since 2.5.2 |
| 30 | */ |
| 31 | public const MAX_ENTRIES = 50; |
| 32 | |
| 33 | /** |
| 34 | * Constructor. |
| 35 | * |
| 36 | * @since 2.5.2 |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | $this->id = 'sureforms/delete-entry'; |
| 40 | $this->label = __( 'Delete Entry', 'sureforms' ); |
| 41 | $this->description = __( 'Permanently delete one or more SureForms form submission entries. This action cannot be undone.', 'sureforms' ); |
| 42 | $this->capability = 'manage_options'; |
| 43 | $this->gated = 'srfm_abilities_api_delete'; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * {@inheritDoc} |
| 48 | * |
| 49 | * @since 2.5.2 |
| 50 | */ |
| 51 | public function get_annotations() { |
| 52 | return [ |
| 53 | 'readonly' => false, |
| 54 | 'destructive' => true, |
| 55 | 'idempotent' => false, |
| 56 | 'priority' => 3.0, |
| 57 | 'openWorldHint' => false, |
| 58 | 'instructions' => 'Always confirm with the user before deleting. This permanently removes the entry and cannot be undone.', |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * {@inheritDoc} |
| 64 | * |
| 65 | * @since 2.5.2 |
| 66 | */ |
| 67 | public function get_input_schema() { |
| 68 | return [ |
| 69 | 'type' => 'object', |
| 70 | 'additionalProperties' => false, |
| 71 | 'properties' => [ |
| 72 | 'entry_ids' => [ |
| 73 | 'type' => 'array', |
| 74 | 'description' => __( 'Array of entry IDs to permanently delete.', 'sureforms' ), |
| 75 | 'items' => [ 'type' => 'integer' ], |
| 76 | ], |
| 77 | ], |
| 78 | 'required' => [ 'entry_ids' ], |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * {@inheritDoc} |
| 84 | * |
| 85 | * @since 2.5.2 |
| 86 | */ |
| 87 | public function get_output_schema() { |
| 88 | return [ |
| 89 | 'type' => 'object', |
| 90 | 'properties' => [ |
| 91 | 'success' => [ 'type' => 'boolean' ], |
| 92 | 'deleted' => [ 'type' => 'integer' ], |
| 93 | 'errors' => [ |
| 94 | 'type' => 'array', |
| 95 | 'items' => [ 'type' => 'string' ], |
| 96 | ], |
| 97 | ], |
| 98 | ]; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Execute the delete-entry ability. |
| 103 | * |
| 104 | * @param array<string,mixed> $input Validated input data. |
| 105 | * @since 2.5.2 |
| 106 | * @return array<string,mixed>|\WP_Error |
| 107 | */ |
| 108 | public function execute( $input ) { |
| 109 | $entry_ids = $input['entry_ids'] ?? []; |
| 110 | |
| 111 | if ( empty( $entry_ids ) || ! is_array( $entry_ids ) ) { |
| 112 | return new \WP_Error( |
| 113 | 'srfm_missing_entry_ids', |
| 114 | __( 'At least one entry ID is required.', 'sureforms' ), |
| 115 | [ 'status' => 400 ] |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | if ( count( $entry_ids ) > self::MAX_ENTRIES ) { |
| 120 | return new \WP_Error( |
| 121 | 'srfm_too_many_entry_ids', |
| 122 | /* translators: %d is the maximum number of entries allowed. */ |
| 123 | sprintf( __( 'Maximum %d entry IDs allowed per request.', 'sureforms' ), self::MAX_ENTRIES ), |
| 124 | [ 'status' => 400 ] |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | $entry_ids = array_map( 'absint', $entry_ids ); |
| 129 | |
| 130 | return Entries::delete_entries( $entry_ids ); |
| 131 | } |
| 132 | } |
| 133 |