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