PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.1
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.1
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / admin-core / api / flow-data.php

flow-data.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.1, at admin-core/api/flow-data.php

152 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'title' => get_the_title( $flow_id ),
121 'slug' => get_post_field( 'post_name', $flow_id, 'edit' ),
122 'link' => $first_step_url ? $first_step_url : '#',
123 'status' => get_post_status( $flow_id ),
124 'steps' => $steps,
125 'options' => $meta_options,
126 'settings_data' => FlowMeta::get_meta_settings( $flow_id ),
127 ),
128 $flow_id
129 );
130
131 $response = new \WP_REST_Response( $data );
132 $response->set_status( 200 );
133
134 return $response;
135 }
136
137 /**
138 * Check whether a given request has permission to read notes.
139 *
140 * @param WP_REST_Request $request Full details about the request.
141 * @return WP_Error|boolean
142 */
143 public function get_item_permissions_check( $request ) {
144
145 if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) {
146 return new \WP_Error( 'cartflows_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'cartflows' ), array( 'status' => rest_authorization_required_code() ) );
147 }
148
149 return true;
150 }
151 }
152