PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.5.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.5.2
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / abilities / forms / delete-form.php
delete-form.php
126 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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, or permanently delete it when force is set to true.', 'sureforms' );
35 $this->capability = 'manage_options';
36 }
37
38 /**
39 * {@inheritDoc}
40 */
41 public function get_annotations() {
42 return [
43 'readonly' => false,
44 'destructive' => true,
45 'idempotent' => false,
46 ];
47 }
48
49 /**
50 * {@inheritDoc}
51 */
52 public function get_input_schema() {
53 return [
54 'type' => 'object',
55 'properties' => [
56 'form_id' => [
57 'type' => 'integer',
58 'description' => __( 'The ID of the form to delete.', 'sureforms' ),
59 ],
60 'force' => [
61 'type' => 'boolean',
62 'description' => __( 'If true, permanently deletes the form. Otherwise moves to trash.', 'sureforms' ),
63 'default' => false,
64 ],
65 ],
66 'required' => [ 'form_id' ],
67 ];
68 }
69
70 /**
71 * {@inheritDoc}
72 */
73 public function get_output_schema() {
74 return [
75 'type' => 'object',
76 'properties' => [
77 'form_id' => [ 'type' => 'integer' ],
78 'deleted' => [ 'type' => 'boolean' ],
79 'previous_status' => [ 'type' => 'string' ],
80 ],
81 ];
82 }
83
84 /**
85 * Execute the delete-form ability.
86 *
87 * @param array<string,mixed> $input Validated input data.
88 * @return array<string,mixed>|\WP_Error
89 */
90 public function execute( $input ) {
91 $form_id = Helper::get_integer_value( $input['form_id'] ?? 0 );
92 $force = ! empty( $input['force'] );
93 $post = get_post( $form_id );
94
95 if ( ! $post || SRFM_FORMS_POST_TYPE !== $post->post_type ) {
96 return new \WP_Error(
97 'srfm_form_not_found',
98 __( 'Form not found.', 'sureforms' ),
99 [ 'status' => 404 ]
100 );
101 }
102
103 $previous_status = $post->post_status;
104
105 if ( $force ) {
106 $result = wp_delete_post( $form_id, true );
107 } else {
108 $result = wp_trash_post( $form_id );
109 }
110
111 if ( ! $result ) {
112 return new \WP_Error(
113 'srfm_delete_failed',
114 __( 'Failed to delete the form.', 'sureforms' ),
115 [ 'status' => 500 ]
116 );
117 }
118
119 return [
120 'form_id' => $form_id,
121 'deleted' => true,
122 'previous_status' => $previous_status,
123 ];
124 }
125 }
126