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
duplicate-form.php
120 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Duplicate 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\Duplicate_Form as Duplicate_Form_Handler; |
| 13 | use SRFM\Inc\Helper; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; // Exit if accessed directly. |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Duplicate_Form ability class. |
| 21 | * |
| 22 | * Duplicates an existing SureForms form with all its fields and metadata. |
| 23 | * |
| 24 | * @since 2.5.2 |
| 25 | */ |
| 26 | class Duplicate_Form extends Abstract_Ability { |
| 27 | /** |
| 28 | * Constructor. |
| 29 | * |
| 30 | * @since 2.5.2 |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | $this->id = 'sureforms/duplicate-form'; |
| 34 | $this->label = __( 'Duplicate SureForms Form', 'sureforms' ); |
| 35 | $this->description = __( 'Duplicate an existing SureForms form with all its fields, metadata, and settings. The new form is created as a draft.', 'sureforms' ); |
| 36 | $this->capability = 'manage_options'; |
| 37 | $this->gated = 'srfm_abilities_api_edit'; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * {@inheritDoc} |
| 42 | * |
| 43 | * @since 2.5.2 |
| 44 | */ |
| 45 | public function get_annotations() { |
| 46 | return [ |
| 47 | 'readonly' => false, |
| 48 | 'destructive' => false, |
| 49 | 'idempotent' => false, |
| 50 | 'priority' => 2.0, |
| 51 | 'openWorldHint' => false, |
| 52 | 'instructions' => 'Confirm which form is being duplicated and the intended title suffix with the user.', |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * {@inheritDoc} |
| 58 | * |
| 59 | * @since 2.5.2 |
| 60 | */ |
| 61 | public function get_input_schema() { |
| 62 | return [ |
| 63 | 'type' => 'object', |
| 64 | 'additionalProperties' => false, |
| 65 | 'properties' => [ |
| 66 | 'form_id' => [ |
| 67 | 'type' => 'integer', |
| 68 | 'description' => __( 'The ID of the form to duplicate.', 'sureforms' ), |
| 69 | ], |
| 70 | 'title_suffix' => [ |
| 71 | 'type' => 'string', |
| 72 | 'description' => __( 'Suffix to append to the duplicated form title.', 'sureforms' ), |
| 73 | 'default' => ' (Copy)', |
| 74 | ], |
| 75 | ], |
| 76 | 'required' => [ 'form_id' ], |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * {@inheritDoc} |
| 82 | * |
| 83 | * @since 2.5.2 |
| 84 | */ |
| 85 | public function get_output_schema() { |
| 86 | return [ |
| 87 | 'type' => 'object', |
| 88 | 'properties' => [ |
| 89 | 'original_form_id' => [ 'type' => 'integer' ], |
| 90 | 'new_form_id' => [ 'type' => 'integer' ], |
| 91 | 'new_form_title' => [ 'type' => 'string' ], |
| 92 | 'edit_url' => [ 'type' => 'string' ], |
| 93 | 'shortcode' => [ 'type' => 'string' ], |
| 94 | ], |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Execute the duplicate-form ability. |
| 100 | * |
| 101 | * @param array<string,mixed> $input Validated input data. |
| 102 | * @since 2.5.2 |
| 103 | * @return array<string,mixed>|\WP_Error |
| 104 | */ |
| 105 | public function execute( $input ) { |
| 106 | $form_id = Helper::get_integer_value( $input['form_id'] ?? 0 ); |
| 107 | $title_suffix = ! empty( $input['title_suffix'] ) ? sanitize_text_field( Helper::get_string_value( $input['title_suffix'] ) ) : ' (Copy)'; |
| 108 | |
| 109 | $result = Duplicate_Form_Handler::get_instance()->duplicate_form( $form_id, $title_suffix ); |
| 110 | |
| 111 | if ( is_wp_error( $result ) ) { |
| 112 | return $result; |
| 113 | } |
| 114 | |
| 115 | $result['shortcode'] = sprintf( '[sureforms id="%d"]', Helper::get_integer_value( $result['new_form_id'] ?? 0 ) ); |
| 116 | |
| 117 | return $result; |
| 118 | } |
| 119 | } |
| 120 |