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
get-shortcode.php
107 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Get Shortcode 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 | * Get_Shortcode ability class. |
| 20 | * |
| 21 | * Returns the shortcode and block markup for embedding a SureForms form. |
| 22 | * |
| 23 | * @since 2.5.2 |
| 24 | */ |
| 25 | class Get_Shortcode extends Abstract_Ability { |
| 26 | /** |
| 27 | * Constructor. |
| 28 | * |
| 29 | * @since 2.5.2 |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | $this->id = 'sureforms/get-shortcode'; |
| 33 | $this->label = __( 'Get Form Shortcode', 'sureforms' ); |
| 34 | $this->description = __( 'Get the shortcode and block markup needed to embed a SureForms form on any page or post.', 'sureforms' ); |
| 35 | $this->capability = 'manage_options'; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * {@inheritDoc} |
| 40 | */ |
| 41 | public function get_annotations() { |
| 42 | return [ |
| 43 | 'readonly' => true, |
| 44 | 'destructive' => false, |
| 45 | 'idempotent' => true, |
| 46 | 'priority' => 1.0, |
| 47 | 'openWorldHint' => false, |
| 48 | ]; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * {@inheritDoc} |
| 53 | */ |
| 54 | public function get_input_schema() { |
| 55 | return [ |
| 56 | 'type' => 'object', |
| 57 | 'additionalProperties' => false, |
| 58 | 'properties' => [ |
| 59 | 'form_id' => [ |
| 60 | 'type' => 'integer', |
| 61 | 'description' => __( 'The ID of the form to get the shortcode for.', 'sureforms' ), |
| 62 | ], |
| 63 | ], |
| 64 | 'required' => [ 'form_id' ], |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * {@inheritDoc} |
| 70 | */ |
| 71 | public function get_output_schema() { |
| 72 | return [ |
| 73 | 'type' => 'object', |
| 74 | 'properties' => [ |
| 75 | 'form_id' => [ 'type' => 'integer' ], |
| 76 | 'shortcode' => [ 'type' => 'string' ], |
| 77 | 'block_markup' => [ 'type' => 'string' ], |
| 78 | ], |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Execute the get-shortcode ability. |
| 84 | * |
| 85 | * @param array<string,mixed> $input Validated input data. |
| 86 | * @return array<string,mixed>|\WP_Error |
| 87 | */ |
| 88 | public function execute( $input ) { |
| 89 | $form_id = Helper::get_integer_value( $input['form_id'] ?? 0 ); |
| 90 | $post = get_post( $form_id ); |
| 91 | |
| 92 | if ( ! $post || SRFM_FORMS_POST_TYPE !== $post->post_type ) { |
| 93 | return new \WP_Error( |
| 94 | 'srfm_form_not_found', |
| 95 | __( 'Form not found.', 'sureforms' ), |
| 96 | [ 'status' => 404 ] |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return [ |
| 101 | 'form_id' => $form_id, |
| 102 | 'shortcode' => sprintf( '[sureforms id="%d"]', $form_id ), |
| 103 | 'block_markup' => sprintf( '<!-- wp:srfm/form {"id":%d} /-->', $form_id ), |
| 104 | ]; |
| 105 | } |
| 106 | } |
| 107 |