| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Flows 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 |
|
| 17 |
/** |
| 18 |
* Class Admin_Query. |
| 19 |
*/ |
| 20 |
class Flows extends ApiBase { |
| 21 |
|
| 22 |
/** |
| 23 |
* Route base. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $rest_base = '/admin/flows/'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Instance |
| 31 |
* |
| 32 |
* @access private |
| 33 |
* @var object Class object. |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
private static $instance; |
| 37 |
|
| 38 |
/** |
| 39 |
* Initiator |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* @return object initialized object of class. |
| 43 |
*/ |
| 44 |
public static function get_instance() { |
| 45 |
if ( ! isset( self::$instance ) ) { |
| 46 |
self::$instance = new self(); |
| 47 |
} |
| 48 |
return self::$instance; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Init Hooks. |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function register_routes() { |
| 58 |
|
| 59 |
// Eg. http://example.local/wp-json/cartflows/v1/admin/flows/. |
| 60 |
$namespace = $this->get_api_namespace(); |
| 61 |
|
| 62 |
register_rest_route( |
| 63 |
$namespace, |
| 64 |
$this->rest_base, |
| 65 |
array( |
| 66 |
array( |
| 67 |
'methods' => 'POST', // WP_REST_Server::READABLE. |
| 68 |
'callback' => array( $this, 'get_items' ), |
| 69 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 70 |
'args' => array(), // get_collection_params may use. |
| 71 |
), |
| 72 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 73 |
) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get items |
| 79 |
* |
| 80 |
* @param WP_REST_Request $request Full details about the request. |
| 81 |
* @return WP_REST_Response |
| 82 |
*/ |
| 83 |
public function get_items( $request ) { |
| 84 |
|
| 85 |
$post_status = 'any'; |
| 86 |
$post_count = 10; |
| 87 |
|
| 88 |
$args = array( |
| 89 |
'post_type' => CARTFLOWS_FLOW_POST_TYPE, |
| 90 |
'post_status' => $post_status, |
| 91 |
'orderby' => 'ID', |
| 92 |
); |
| 93 |
|
| 94 |
// checking if store checkout is available and removing it from the list of flows. |
| 95 |
$store_checkout_id = intval( \Cartflows_Helper::get_global_setting( '_cartflows_store_checkout' ) ); |
| 96 |
if ( 0 !== $store_checkout_id ) { |
| 97 |
$args['post__not_in'] = array( $store_checkout_id ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( null !== $request->get_param( 'paged' ) ) { |
| 101 |
$args['paged'] = absint( $request->get_param( 'paged' ) ); |
| 102 |
} |
| 103 |
|
| 104 |
$mode = $request->get_param( 'mode' ); |
| 105 |
|
| 106 |
if ( null !== $mode ) { |
| 107 |
|
| 108 |
if ( 'sandbox' === $mode ) { |
| 109 |
$args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 110 |
array( |
| 111 |
'key' => 'wcf-testing', |
| 112 |
'value' => 'yes', |
| 113 |
), |
| 114 |
); |
| 115 |
} else { |
| 116 |
$args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 117 |
'relation' => 'OR', |
| 118 |
array( |
| 119 |
'key' => 'wcf-testing', |
| 120 |
'value' => 'no', |
| 121 |
), |
| 122 |
array( |
| 123 |
'key' => 'wcf-testing', |
| 124 |
'compare' => 'NOT EXISTS', |
| 125 |
), |
| 126 |
); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if ( 'any' === $post_status ) { |
| 131 |
|
| 132 |
if ( null !== $request->get_param( 's' ) ) { |
| 133 |
$args['s'] = sanitize_text_field( $request->get_param( 's' ) ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if ( null !== $request->get_param( 'post_status' ) ) { |
| 138 |
|
| 139 |
$status = $request->get_param( 'post_status' ); |
| 140 |
|
| 141 |
// Allowlist of valid post statuses for flows. |
| 142 |
$allowed_statuses = array( 'active', 'inactive', 'publish', 'draft', 'pending' ); |
| 143 |
|
| 144 |
if ( 'active' === $status ) { |
| 145 |
$args['post_status'] = 'publish'; |
| 146 |
} elseif ( 'inactive' === $status ) { |
| 147 |
$args['post_status'] = 'draft'; |
| 148 |
} elseif ( in_array( $status, $allowed_statuses, true ) ) { |
| 149 |
$args['post_status'] = sanitize_text_field( $status ); |
| 150 |
} |
| 151 |
// Invalid statuses are silently ignored, falling through to the default below. |
| 152 |
} |
| 153 |
|
| 154 |
if ( ! isset( $args['post_status'] ) || null === $request->get_param( 'post_status' ) ) { |
| 155 |
$args['post_status'] = 'publish'; |
| 156 |
} |
| 157 |
|
| 158 |
$start_date = $request->get_param( 'start_date' ); |
| 159 |
$end_date = $request->get_param( 'end_date' ); |
| 160 |
|
| 161 |
if ( ! empty( $start_date ) && ! empty( $end_date ) ) { |
| 162 |
$args['date_query'] = array( |
| 163 |
array( |
| 164 |
'after' => sanitize_text_field( $start_date ) . ' 00:00:00', |
| 165 |
'before' => sanitize_text_field( $end_date ) . ' 23:59:59', |
| 166 |
'inclusive' => true, |
| 167 |
'column' => 'post_date', |
| 168 |
), |
| 169 |
); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if ( ! empty( $post_count ) ) { |
| 174 |
$args['posts_per_page'] = $post_count; |
| 175 |
} |
| 176 |
|
| 177 |
$result = new \WP_Query( $args ); |
| 178 |
|
| 179 |
$data = array( |
| 180 |
'items' => array(), |
| 181 |
'pagination' => array(), |
| 182 |
); |
| 183 |
|
| 184 |
if ( $result->have_posts() ) { |
| 185 |
while ( $result->have_posts() ) { |
| 186 |
$result->the_post(); |
| 187 |
|
| 188 |
global $post; |
| 189 |
|
| 190 |
$post_data = (array) $post; |
| 191 |
|
| 192 |
// Modify the date Format just to display it. |
| 193 |
$post_data['post_modified'] = date_format( date_create( $post_data['post_modified'] ), 'yy/m/d' ); |
| 194 |
$post_data['post_status'] = ucwords( $post_data['post_status'] ); |
| 195 |
|
| 196 |
$view = get_permalink( $post->ID ); |
| 197 |
$edit = admin_url( 'admin.php?page=cartflows&path=flows&action=wcf-edit-flow&flow_id=' . $post->ID ); |
| 198 |
$delete = '#'; |
| 199 |
$clone = '#'; |
| 200 |
$export = '#'; |
| 201 |
$post_data['flow_test_mode'] = ( 'yes' === wcf()->options->get_flow_meta_value( $post->ID, 'wcf-testing' ) ) ? true : false; |
| 202 |
$post_data['actions'] = array( |
| 203 |
'view' => array( |
| 204 |
'action' => 'edit', |
| 205 |
'class' => '', |
| 206 |
'attr' => array( 'target' => '_blank' ), |
| 207 |
'text' => __( 'View', 'cartflows' ), |
| 208 |
'link' => $view, |
| 209 |
|
| 210 |
), |
| 211 |
'edit' => array( |
| 212 |
'action' => 'edit', |
| 213 |
'class' => '', |
| 214 |
'attr' => array(), |
| 215 |
'text' => __( 'Edit', 'cartflows' ), |
| 216 |
'link' => $edit, |
| 217 |
|
| 218 |
), |
| 219 |
'duplicate' => array( |
| 220 |
'action' => 'clone', |
| 221 |
'attr' => array(), |
| 222 |
'class' => '', |
| 223 |
'text' => __( 'Duplicate', 'cartflows' ), |
| 224 |
'link' => $clone, |
| 225 |
), |
| 226 |
'export' => array( |
| 227 |
'action' => 'export', |
| 228 |
'attr' => array(), |
| 229 |
'class' => '', |
| 230 |
'text' => __( 'Export', 'cartflows' ), |
| 231 |
'link' => $export, |
| 232 |
), |
| 233 |
'delete' => array( |
| 234 |
'action' => 'delete', |
| 235 |
'attr' => array(), |
| 236 |
'class' => '', |
| 237 |
'text' => __( 'Delete', 'cartflows' ), |
| 238 |
'link' => $delete, |
| 239 |
), |
| 240 |
); |
| 241 |
|
| 242 |
// Fetch the revenue only for free version for the PRO it will be fetched and added by the filter later in the code. |
| 243 |
if ( ! _is_cartflows_pro() && ! is_wcf_pro_plan() ) { |
| 244 |
$post_data['revenue'] = $this->get_per_flow_revenue( $post->ID ); |
| 245 |
} |
| 246 |
|
| 247 |
$data['items'][] = $post_data; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
$data['found_posts'] = $result->found_posts; |
| 252 |
$data['post_status'] = isset( $post_data['post_status'] ) ? $post_data['post_status'] : $args['post_status']; |
| 253 |
|
| 254 |
$data['active_flows_count'] = intval( wp_count_posts( CARTFLOWS_FLOW_POST_TYPE )->publish ); |
| 255 |
$data['trash_flows_count'] = intval( wp_count_posts( CARTFLOWS_FLOW_POST_TYPE )->trash ); |
| 256 |
$data['draft_flows_count'] = intval( wp_count_posts( CARTFLOWS_FLOW_POST_TYPE )->draft ); |
| 257 |
|
| 258 |
$data['pagination'] = array( |
| 259 |
'found_posts' => $result->found_posts, |
| 260 |
'paged' => $result->query['paged'], |
| 261 |
'max_pages' => $result->max_num_pages, |
| 262 |
); |
| 263 |
|
| 264 |
// Reducing count of active_flows_count if store checkout is set. |
| 265 |
if ( 0 !== $store_checkout_id ) { |
| 266 |
$data['active_flows_count']--; |
| 267 |
} |
| 268 |
|
| 269 |
wp_reset_postdata(); |
| 270 |
|
| 271 |
$data['status'] = true; |
| 272 |
|
| 273 |
// Retrieve the revenue data from the PRO to display it on the flow listing page. |
| 274 |
if ( _is_cartflows_pro() && is_wcf_pro_plan() ) { |
| 275 |
$data = apply_filters( 'cartflows_admin_flows_page_data', $data ); |
| 276 |
} |
| 277 |
|
| 278 |
if ( ! $result->have_posts() ) { |
| 279 |
$data['status'] = false; |
| 280 |
$response = new \WP_REST_Response( $data ); |
| 281 |
$response->set_status( 200 ); |
| 282 |
return $response; |
| 283 |
} |
| 284 |
|
| 285 |
$response = new \WP_REST_Response( $data ); |
| 286 |
$response->set_status( 200 ); |
| 287 |
|
| 288 |
return $response; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Check whether a given request has permission to read notes. |
| 293 |
* |
| 294 |
* @param WP_REST_Request $request Full details about the request. |
| 295 |
* @return WP_Error|boolean |
| 296 |
*/ |
| 297 |
public function get_items_permissions_check( $request ) { |
| 298 |
|
| 299 |
if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) { |
| 300 |
return new \WP_Error( 'cartflows_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'cartflows' ), array( 'status' => rest_authorization_required_code() ) ); |
| 301 |
} |
| 302 |
|
| 303 |
return true; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Get revenue of flow. |
| 308 |
* |
| 309 |
* @param int $flow_id flow id. |
| 310 |
* @return int |
| 311 |
*/ |
| 312 |
public function get_per_flow_revenue( $flow_id ) { |
| 313 |
|
| 314 |
$gross_sale = 0; |
| 315 |
|
| 316 |
// Return if WooCommerce is not active. |
| 317 |
if ( ! wcf()->is_woo_active ) { |
| 318 |
return $gross_sale; |
| 319 |
} |
| 320 |
|
| 321 |
// Fetch primary orders: Checkout, Order Bumps. |
| 322 |
$args = array( |
| 323 |
'status' => array( 'completed', 'processing', 'cancelled' ), // Accepts a string: one of 'pending', 'processing', 'on-hold', 'completed', 'refunded, 'failed', 'cancelled', or a custom order status. |
| 324 |
'meta_key' => '_wcf_flow_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 325 |
'meta_value' => $flow_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 326 |
'meta_compare' => '=', // Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘EXISTS’ (only in WP >= 3.5), and ‘NOT EXISTS’ (also only in WP >= 3.5). Values ‘REGEXP’, ‘NOT REGEXP’ and ‘RLIKE’ were added in WordPress 3.7. Default value is ‘=’. |
| 327 |
'return' => 'ids', // Accepts a string: 'ids' or 'objects'. Default: 'objects'. |
| 328 |
); |
| 329 |
|
| 330 |
$orders = wc_get_orders( $args ); |
| 331 |
|
| 332 |
if ( ! empty( $orders ) && is_array( $orders ) ) { |
| 333 |
|
| 334 |
foreach ( $orders as $order_id ) { |
| 335 |
|
| 336 |
$order = wc_get_order( $order_id ); |
| 337 |
$user_id = $order->get_user_id(); |
| 338 |
|
| 339 |
// skip the orders which are placed by the user whose user role is Administrator. |
| 340 |
if ( $user_id && user_can( $user_id, 'cartflows_manage_flows_steps' ) ) { |
| 341 |
continue; |
| 342 |
} |
| 343 |
|
| 344 |
$order_total = $order->get_total(); |
| 345 |
if ( ! $order->has_status( 'cancelled' ) ) { |
| 346 |
$gross_sale += (float) $order_total; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return str_replace( ' ', '', wc_price( $gross_sale ) ); |
| 352 |
} |
| 353 |
} |
| 354 |
|