| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Flow data. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace CartflowsAdmin\AdminCore\Api; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
use CartflowsAdmin\AdminCore\Api\ApiBase; |
| 16 |
use CartflowsAdmin\AdminCore\Inc\AdminHelper; |
| 17 |
|
| 18 |
use CartflowsAdmin\AdminCore\Inc\FlowMeta; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Admin_Query. |
| 22 |
*/ |
| 23 |
class FlowData extends ApiBase { |
| 24 |
|
| 25 |
/** |
| 26 |
* Route base. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $rest_base = '/admin/flow-data/'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Instance |
| 34 |
* |
| 35 |
* @access private |
| 36 |
* @var object Class object. |
| 37 |
* @since 1.0.0 |
| 38 |
*/ |
| 39 |
private static $instance; |
| 40 |
|
| 41 |
/** |
| 42 |
* Initiator |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* @return object initialized object of class. |
| 46 |
*/ |
| 47 |
public static function get_instance() { |
| 48 |
if ( ! isset( self::$instance ) ) { |
| 49 |
self::$instance = new self(); |
| 50 |
} |
| 51 |
return self::$instance; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Init Hooks. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function register_routes() { |
| 61 |
|
| 62 |
$namespace = $this->get_api_namespace(); |
| 63 |
|
| 64 |
register_rest_route( |
| 65 |
$namespace, |
| 66 |
$this->rest_base . '(?P<id>[\d-]+)', |
| 67 |
array( |
| 68 |
'args' => array( |
| 69 |
'id' => array( |
| 70 |
'description' => __( 'Flow ID.', 'cartflows' ), |
| 71 |
'type' => 'integer', |
| 72 |
), |
| 73 |
), |
| 74 |
array( |
| 75 |
'methods' => \WP_REST_Server::READABLE, |
| 76 |
'callback' => array( $this, 'get_item' ), |
| 77 |
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 78 |
), |
| 79 |
|
| 80 |
/* |
| 81 |
Start - Not yet working but needed |
| 82 |
array( |
| 83 |
'methods' => \WP_REST_Server::EDITABLE, |
| 84 |
'callback' => array( $this, 'update_item' ), |
| 85 |
'permission_callback' => array( $this, 'update_items_permissions_check' ), |
| 86 |
), |
| 87 |
End - Not yet working but needed |
| 88 |
*/ |
| 89 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 90 |
) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get flow data. |
| 96 |
* |
| 97 |
* @param WP_REST_Request $request Full details about the request. |
| 98 |
* @return WP_Error|boolean |
| 99 |
*/ |
| 100 |
public function get_item( $request ) { |
| 101 |
|
| 102 |
$flow_id = absint( $request->get_param( 'id' ) ); |
| 103 |
|
| 104 |
// Validate the post type to prevent accessing non-flow posts. |
| 105 |
if ( CARTFLOWS_FLOW_POST_TYPE !== get_post_type( $flow_id ) ) { |
| 106 |
return new \WP_Error( 'cartflows_invalid_flow', __( 'Invalid flow ID.', 'cartflows' ), array( 'status' => 404 ) ); |
| 107 |
} |
| 108 |
|
| 109 |
$meta_options = AdminHelper::get_flow_meta_options( $flow_id ); |
| 110 |
|
| 111 |
/* Setup steps data */ |
| 112 |
$steps = AdminHelper::prepare_step_data( $flow_id, $meta_options ); |
| 113 |
|
| 114 |
$first_step_url = \Cartflows_Flow_Post_Type::get_instance()->get_first_step_url( get_post( $flow_id ) ); |
| 115 |
|
| 116 |
$data = apply_filters( |
| 117 |
'cartflows_admin_flow_data', |
| 118 |
array( |
| 119 |
'id' => $flow_id, |
| 120 |
// Use the raw stored title to avoid the_title filters (wptexturize) turning dashes into HTML entities. |
| 121 |
'title' => get_post_field( 'post_title', $flow_id, 'raw' ), |
| 122 |
'slug' => get_post_field( 'post_name', $flow_id, 'edit' ), |
| 123 |
'link' => $first_step_url ? $first_step_url : '#', |
| 124 |
'status' => get_post_status( $flow_id ), |
| 125 |
'steps' => $steps, |
| 126 |
'options' => $meta_options, |
| 127 |
'settings_data' => FlowMeta::get_meta_settings( $flow_id ), |
| 128 |
), |
| 129 |
$flow_id |
| 130 |
); |
| 131 |
|
| 132 |
$response = new \WP_REST_Response( $data ); |
| 133 |
$response->set_status( 200 ); |
| 134 |
|
| 135 |
return $response; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Check whether a given request has permission to read notes. |
| 140 |
* |
| 141 |
* @param WP_REST_Request $request Full details about the request. |
| 142 |
* @return WP_Error|boolean |
| 143 |
*/ |
| 144 |
public function get_item_permissions_check( $request ) { |
| 145 |
|
| 146 |
if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) { |
| 147 |
return new \WP_Error( 'cartflows_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'cartflows' ), array( 'status' => rest_authorization_required_code() ) ); |
| 148 |
} |
| 149 |
|
| 150 |
return true; |
| 151 |
} |
| 152 |
} |
| 153 |
|