create-form.php
1 month ago
delete-form.php
5 months ago
duplicate-form.php
5 months ago
form-field-schema.php
2 months ago
form-metadata.php
1 month ago
get-form-stats.php
5 months ago
get-form.php
5 months ago
get-shortcode.php
5 months ago
list-forms.php
2 months ago
update-form.php
1 month ago
delete-form.php
131 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Delete Form Ability. |
| 4 | * |
| 5 | * @package sureforms |
| 6 | * @since 2.5.2 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM\Inc\Abilities\Forms; |
| 10 | |
| 11 | use SRFM\Inc\Abilities\Abstract_Ability; |
| 12 | use SRFM\Inc\Helper; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Delete_Form ability class. |
| 20 | * |
| 21 | * Trashes or permanently deletes a SureForms form. |
| 22 | * |
| 23 | * @since 2.5.2 |
| 24 | */ |
| 25 | class Delete_Form extends Abstract_Ability { |
| 26 | /** |
| 27 | * Constructor. |
| 28 | * |
| 29 | * @since 2.5.2 |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | $this->id = 'sureforms/delete-form'; |
| 33 | $this->label = __( 'Delete SureForms Form', 'sureforms' ); |
| 34 | $this->description = __( 'Move a SureForms form to trash. When force is true, the form and all its metadata are permanently deleted — this cannot be undone.', 'sureforms' ); |
| 35 | $this->capability = 'manage_options'; |
| 36 | $this->gated = 'srfm_abilities_api_delete'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * {@inheritDoc} |
| 41 | */ |
| 42 | public function get_annotations() { |
| 43 | return [ |
| 44 | 'readonly' => false, |
| 45 | 'destructive' => true, |
| 46 | 'idempotent' => false, |
| 47 | 'priority' => 3.0, |
| 48 | 'openWorldHint' => false, |
| 49 | 'instructions' => 'Always confirm with the user before deleting. When force is true, the form and all its data are permanently destroyed and cannot be recovered.', |
| 50 | ]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * {@inheritDoc} |
| 55 | */ |
| 56 | public function get_input_schema() { |
| 57 | return [ |
| 58 | 'type' => 'object', |
| 59 | 'additionalProperties' => false, |
| 60 | 'properties' => [ |
| 61 | 'form_id' => [ |
| 62 | 'type' => 'integer', |
| 63 | 'description' => __( 'The ID of the form to delete.', 'sureforms' ), |
| 64 | ], |
| 65 | 'force' => [ |
| 66 | 'type' => 'boolean', |
| 67 | 'description' => __( 'If true, permanently deletes the form. Otherwise moves to trash.', 'sureforms' ), |
| 68 | 'default' => false, |
| 69 | ], |
| 70 | ], |
| 71 | 'required' => [ 'form_id' ], |
| 72 | ]; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * {@inheritDoc} |
| 77 | */ |
| 78 | public function get_output_schema() { |
| 79 | return [ |
| 80 | 'type' => 'object', |
| 81 | 'properties' => [ |
| 82 | 'form_id' => [ 'type' => 'integer' ], |
| 83 | 'deleted' => [ 'type' => 'boolean' ], |
| 84 | 'previous_status' => [ 'type' => 'string' ], |
| 85 | ], |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Execute the delete-form ability. |
| 91 | * |
| 92 | * @param array<string,mixed> $input Validated input data. |
| 93 | * @return array<string,mixed>|\WP_Error |
| 94 | */ |
| 95 | public function execute( $input ) { |
| 96 | $form_id = Helper::get_integer_value( $input['form_id'] ?? 0 ); |
| 97 | $force = ! empty( $input['force'] ); |
| 98 | $post = get_post( $form_id ); |
| 99 | |
| 100 | if ( ! $post || SRFM_FORMS_POST_TYPE !== $post->post_type ) { |
| 101 | return new \WP_Error( |
| 102 | 'srfm_form_not_found', |
| 103 | __( 'Form not found.', 'sureforms' ), |
| 104 | [ 'status' => 404 ] |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | $previous_status = $post->post_status; |
| 109 | |
| 110 | if ( $force ) { |
| 111 | $result = wp_delete_post( $form_id, true ); |
| 112 | } else { |
| 113 | $result = wp_trash_post( $form_id ); |
| 114 | } |
| 115 | |
| 116 | if ( ! $result ) { |
| 117 | return new \WP_Error( |
| 118 | 'srfm_delete_failed', |
| 119 | __( 'Failed to delete the form.', 'sureforms' ), |
| 120 | [ 'status' => 500 ] |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | return [ |
| 125 | 'form_id' => $form_id, |
| 126 | 'deleted' => true, |
| 127 | 'previous_status' => $previous_status, |
| 128 | ]; |
| 129 | } |
| 130 | } |
| 131 |