PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / trunk
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce vtrunk
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 1.1.20 1.1.21 All 160 releases
cartflows / admin-core / api / step-data.php

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

157 lines 4.4 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 use CartflowsAdmin\AdminCore\Inc\StepMeta;
18
19 /**
20 * Class StepData.
21 */
22 class StepData extends ApiBase {
23
24 /**
25 * Route base.
26 *
27 * @var string
28 */
29 protected $rest_base = '/admin/step-data/';
30
31 /**
32 * Instance
33 *
34 * @access private
35 * @var object Class object.
36 * @since 1.0.0
37 */
38 private static $instance;
39
40 /**
41 * Initiator
42 *
43 * @since 1.0.0
44 * @return object initialized object of class.
45 */
46 public static function get_instance() {
47 if ( ! isset( self::$instance ) ) {
48 self::$instance = new self();
49 }
50 return self::$instance;
51 }
52
53 /**
54 * Init Hooks.
55 *
56 * @since 1.0.0
57 * @return void
58 */
59 public function register_routes() {
60
61 $namespace = $this->get_api_namespace();
62
63 register_rest_route(
64 $namespace,
65 $this->rest_base . '(?P<id>[\d-]+)',
66 array(
67 'args' => array(
68 'id' => array(
69 'description' => __( 'Step ID.', 'cartflows' ),
70 'type' => 'integer',
71 ),
72 ),
73 array(
74 'methods' => \WP_REST_Server::READABLE,
75 'callback' => array( $this, 'get_item' ),
76 'permission_callback' => array( $this, 'get_item_permissions_check' ),
77 ),
78 'schema' => array( $this, 'get_public_item_schema' ),
79 )
80 );
81 }
82
83 /**
84 * Get step data.
85 *
86 * @param WP_REST_Request $request Full details about the request.
87 * @return WP_Error|boolean
88 */
89 public function get_item( $request ) {
90
91 $step_id = absint( $request->get_param( 'id' ) );
92
93 // Validate the post type to prevent accessing non-step posts.
94 if ( CARTFLOWS_STEP_POST_TYPE !== get_post_type( $step_id ) ) {
95 return new \WP_Error( 'cartflows_invalid_step', __( 'Invalid step ID.', 'cartflows' ), array( 'status' => 404 ) );
96 }
97
98 $meta_options = array();
99 $meta_options = AdminHelper::get_step_meta_options( $step_id );
100
101 $step_type = $meta_options['type'];
102 $flow_id_meta = get_post_meta( $step_id, 'wcf-flow-id', true );
103 $flow_id = is_numeric( $flow_id_meta ) ? absint( $flow_id_meta ) : 0;
104 $edit_step = get_edit_post_link( $step_id, 'edit' );
105 $view_step = get_permalink( $step_id );
106 $page_builder_edit = \Cartflows_Helper::get_page_builder_edit_link( $step_id );
107
108 /* Get Settings */
109 $settings_data = StepMeta::get_meta_settings( $step_id, $step_type );
110
111 $meta_options['options']['step_post_name'] = get_post_field( 'post_name', $step_id, 'edit' );
112
113 /* Prepare data */
114 $data = apply_filters(
115 'cartflows_admin_' . $step_type . '_step_data',
116 array(
117 'id' => $step_id,
118 // Use the raw stored titles to avoid the_title filters (wptexturize) turning dashes into HTML entities.
119 'flow_title' => get_post_field( 'post_title', $flow_id, 'raw' ),
120 'title' => get_post_field( 'post_title', $step_id, 'raw' ),
121 'type' => $step_type,
122 'tabs' => isset( $settings_data['tabs'] ) ? $settings_data['tabs'] : '',
123 'settings_data' => isset( $settings_data['settings'] ) ? $settings_data['settings'] : '',
124 'page_settings' => isset( $settings_data['page_settings'] ) ? $settings_data['page_settings'] : '',
125 'design_settings' => isset( $settings_data['design_settings'] ) ? $settings_data['design_settings'] : '',
126 'options' => isset( $meta_options['options'] ) ? $meta_options['options'] : '',
127 'view' => $view_step,
128 'edit' => $edit_step,
129 'page_builder_edit' => $page_builder_edit,
130 // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
131 // 'slug' => get_post_field( 'post_name', $step_id, 'edit' ),
132 ),
133 $step_id
134 );
135
136 $response = new \WP_REST_Response( $data );
137 $response->set_status( 200 );
138
139 return $response;
140 }
141
142 /**
143 * Check whether a given request has permission to read notes.
144 *
145 * @param WP_REST_Request $request Full details about the request.
146 * @return WP_Error|boolean
147 */
148 public function get_item_permissions_check( $request ) {
149
150 if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) {
151 return new \WP_Error( 'cartflows_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'cartflows' ), array( 'status' => rest_authorization_required_code() ) );
152 }
153
154 return true;
155 }
156 }
157