PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.0.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.0.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 / forms-data.php
forms-data.php
116 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sureforms get forms title and Ids.
4 *
5 * @package sureforms.
6 * @since 0.0.1
7 */
8
9 namespace SRFM\Inc;
10
11 use WP_REST_Response;
12 use WP_Error;
13 use SRFM\Inc\Traits\Get_Instance;
14 use SRFM\Inc\Helper;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19
20 /**
21 * Load Defaults Class.
22 *
23 * @since 0.0.1
24 */
25 class Forms_Data {
26 use Get_Instance;
27
28 /**
29 * Constructor
30 *
31 * @since 0.0.1
32 */
33 public function __construct() {
34 add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] );
35 }
36
37 /**
38 * Add custom API Route load-form-defaults
39 *
40 * @return void
41 * @since 0.0.1
42 */
43 public function register_custom_endpoint() {
44 register_rest_route(
45 'sureforms/v1',
46 '/forms-data',
47 [
48 'methods' => 'GET',
49 'callback' => [ $this, 'load_forms' ],
50 'permission_callback' => [ $this, 'get_form_permissions_check' ],
51 ]
52 );
53 }
54
55 /**
56 * Checks whether a given request has permission to read the form.
57 *
58 * @param \WP_REST_Request $request Full details about the request.
59 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
60 * @since 0.0.1
61 */
62 public function get_form_permissions_check( $request ) {
63 if ( current_user_can( 'edit_posts' ) ) {
64 return true;
65 }
66
67 return new \WP_Error(
68 'rest_cannot_view',
69 __( 'Sorry, you are not allowed to view the form.', 'sureforms' ),
70 [ 'status' => \rest_authorization_required_code() ]
71 );
72 }
73
74 /**
75 * Handle Form status
76 *
77 * @param \WP_REST_Request $request Full details about the request.
78 *
79 * @return WP_REST_Response
80 * @since 0.0.1
81 */
82 public function load_forms( $request ) {
83
84 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
85
86 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
87 wp_send_json_error(
88 [
89 'data' => __( 'Nonce verification failed.', 'sureforms' ),
90 'status' => false,
91 ]
92 );
93 }
94
95 $args = [
96 'post_type' => 'sureforms_form',
97 'post_status' => 'publish',
98 'posts_per_page' => -1, // Retrieve all posts.
99 ];
100
101 $form_posts = get_posts( $args );
102
103 $data = [];
104
105 foreach ( $form_posts as $post ) {
106 $data[] = [
107 'id' => $post->ID,
108 'title' => $post->post_title,
109 'content' => $post->post_content,
110 ];
111 }
112
113 return new WP_REST_Response( $data );
114 }
115 }
116