| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Common Settings Data Query. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace CartflowsAdmin\AdminLegacyCore\Api; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
use CartflowsAdmin\AdminLegacyCore\Api\ApiBase; |
| 16 |
use CartflowsAdmin\AdminLegacyCore\Inc\AdminHelper; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class Admin_Query. |
| 20 |
*/ |
| 21 |
class HomePage extends ApiBase { |
| 22 |
|
| 23 |
/** |
| 24 |
* Route base. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $rest_base = '/admin/homepage/'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Route base. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $rest_base_setup = '/admin/setup-checklist/'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Instance |
| 39 |
* |
| 40 |
* @access private |
| 41 |
* @var object Class object. |
| 42 |
* @since 1.0.0 |
| 43 |
*/ |
| 44 |
private static $instance; |
| 45 |
|
| 46 |
/** |
| 47 |
* Initiator |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* @return object initialized object of class. |
| 51 |
*/ |
| 52 |
public static function get_instance() { |
| 53 |
if ( ! isset( self::$instance ) ) { |
| 54 |
self::$instance = new self(); |
| 55 |
} |
| 56 |
return self::$instance; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Init Hooks. |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function register_routes() { |
| 66 |
|
| 67 |
$namespace = $this->get_api_namespace(); |
| 68 |
|
| 69 |
register_rest_route( |
| 70 |
$namespace, |
| 71 |
$this->rest_base, |
| 72 |
array( |
| 73 |
array( |
| 74 |
'methods' => \WP_REST_Server::READABLE, |
| 75 |
'callback' => array( $this, 'get_home_page_settings' ), |
| 76 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 77 |
'args' => array(), |
| 78 |
), |
| 79 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 80 |
) |
| 81 |
); |
| 82 |
|
| 83 |
register_rest_route( |
| 84 |
$namespace, |
| 85 |
$this->rest_base_setup, |
| 86 |
array( |
| 87 |
array( |
| 88 |
'methods' => 'POST', // WP_REST_Server::READABLE. |
| 89 |
'callback' => array( $this, 'get_setup_details' ), |
| 90 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 91 |
'args' => array(), // get_collection_params may use. |
| 92 |
), |
| 93 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get common settings. |
| 100 |
* |
| 101 |
* @param WP_REST_Request $request Full details about the request. |
| 102 |
*/ |
| 103 |
public function get_home_page_settings( $request ) { |
| 104 |
|
| 105 |
$box_id = sanitize_text_field( $request['wcf_box_id'] ); |
| 106 |
|
| 107 |
// Allowlist: only permit CartFlows-specific option keys to prevent arbitrary option reads. |
| 108 |
if ( empty( $box_id ) || ( 0 !== strpos( $box_id, '_cartflows_' ) && 0 !== strpos( $box_id, 'cartflows_' ) && 0 !== strpos( $box_id, 'wcf-' ) && 0 !== strpos( $box_id, 'wcf_' ) ) ) { |
| 109 |
return new \WP_Error( 'cartflows_invalid_box_id', __( 'Invalid box ID.', 'cartflows' ), array( 'status' => 400 ) ); |
| 110 |
} |
| 111 |
|
| 112 |
$boxes_settings = AdminHelper::get_admin_settings_option( $box_id ); |
| 113 |
|
| 114 |
$home_page_settings = array( |
| 115 |
'is_hidden' => $boxes_settings, |
| 116 |
); |
| 117 |
|
| 118 |
return $home_page_settings; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get items |
| 123 |
* |
| 124 |
* @param WP_REST_Request $request Full details about the request. |
| 125 |
* @return WP_REST_Response |
| 126 |
*/ |
| 127 |
public function get_setup_details( $request ) { |
| 128 |
|
| 129 |
// Get only the checkout steps. |
| 130 |
$checkout_steps = get_posts( |
| 131 |
array( |
| 132 |
'post_type' => CARTFLOWS_STEP_POST_TYPE, |
| 133 |
'post_status' => array( 'publish' ), |
| 134 |
'orderby' => 'ID', |
| 135 |
'order' => 'DESC', |
| 136 |
'meta_query' => array( //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 137 |
array( |
| 138 |
'key' => 'wcf-step-type', |
| 139 |
'value' => 'checkout', |
| 140 |
'compare' => '===', |
| 141 |
), |
| 142 |
), |
| 143 |
'fields' => 'ids', |
| 144 |
'numberposts' => 10, |
| 145 |
) |
| 146 |
); |
| 147 |
|
| 148 |
// Select the first checkout step from the array. |
| 149 |
$first_checkout_id = ! empty( $checkout_steps ) && is_array( $checkout_steps ) ? reset( $checkout_steps ) : ''; |
| 150 |
// Get the flow ID from the checkout step from the first selected checkout ID from the array. |
| 151 |
$first_step_flow_id = ! empty( $first_checkout_id ) ? (int) wcf()->utils->get_flow_id_from_step_id( $first_checkout_id ) : ''; |
| 152 |
|
| 153 |
// Prepare the data for the free version and use the filter to add the PRO related data. |
| 154 |
$setup_data = apply_filters( |
| 155 |
'cartflows_onboarding_setup_details', |
| 156 |
array( |
| 157 |
'flow_exists' => intval( wp_count_posts( CARTFLOWS_FLOW_POST_TYPE )->publish ), |
| 158 |
'checkout_step_id' => $first_checkout_id, |
| 159 |
'checkout_flow_id' => $first_step_flow_id, |
| 160 |
), |
| 161 |
$checkout_steps |
| 162 |
); |
| 163 |
|
| 164 |
$response = new \WP_REST_Response( $setup_data ); |
| 165 |
$response->set_status( 200 ); |
| 166 |
|
| 167 |
return $response; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Check whether a given request has permission to read notes. |
| 172 |
* |
| 173 |
* @param WP_REST_Request $request Full details about the request. |
| 174 |
* @return WP_Error|boolean |
| 175 |
*/ |
| 176 |
public function get_items_permissions_check( $request ) { |
| 177 |
|
| 178 |
if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) { |
| 179 |
return new \WP_Error( 'cartflows_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'cartflows' ), array( 'status' => rest_authorization_required_code() ) ); |
| 180 |
} |
| 181 |
|
| 182 |
return true; |
| 183 |
} |
| 184 |
} |
| 185 |
|