getAuthorizationHeader(); if ( $headers != '' ) { if ( preg_match( '/Bearer\s(\S+)/', $headers, $matches ) ) { return $matches[1]; } } return ''; } /** * Validate Sent Bearer with saved. */ private function validateRequest() { $token = get_option( 'aftersalesprogr_api_token', '' ); if ( $token == '' ) { echo json_encode( [ 'meta' => [ 'is_successful' => false, 'message' => 'Token is not set.', 'version' => AFTERSALESPROGR_VERSION, ], ], JSON_PRETTY_PRINT ); die(); } $bearer = $this->getBearerToken(); if ( $bearer == '' ) { echo json_encode( [ 'meta' => [ 'is_successful' => false, 'message' => 'Bearer could not be read.', 'version' => AFTERSALESPROGR_VERSION, ], ], JSON_PRETTY_PRINT ); die(); } if ( $bearer != $token ) { echo json_encode( [ 'meta' => [ 'is_successful' => false, 'message' => 'Authorization failed.', 'version' => AFTERSALESPROGR_VERSION, ], ], JSON_PRETTY_PRINT ); die(); } } private function getOrders(): array { global $wpdb; $where_conditions = ["type = 'shop_order'"]; $where_conditions[] = "status != 'trash'"; // Equivalent to 'any' status $params = []; if (!empty($_REQUEST['ids'])) { $id = intval($_REQUEST['ids']); if ($id > 0) { $where_conditions[] = "id = %d"; $params[] = $id; } } $updated_at_from = $_REQUEST['updated_at_from'] ?? $_REQUEST['after'] ?? null; if (!empty($updated_at_from)) { $where_conditions[] = "date_updated_gmt > %s"; $params[] = sanitize_text_field($updated_at_from); } if (!empty($_REQUEST['updated_at_to'])) { $where_conditions[] = "date_updated_gmt <= %s"; $params[] = sanitize_text_field($_REQUEST['updated_at_to']); } // Handle status filtering if (!empty($_REQUEST['status'])) { $statuses = explode(',', sanitize_text_field($_REQUEST['status'])); $formatted_statuses = array_map(function($status) { $status = trim($status); return strpos($status, 'wc-') === 0 ? $status : 'wc-' . $status; }, $statuses); $status_placeholders = implode(',', array_fill(0, count($formatted_statuses), '%s')); $where_conditions[] = "status IN ({$status_placeholders})"; $params = array_merge($params, $formatted_statuses); } $where_clause = implode(' AND ', $where_conditions); if (!empty($params)) { $query = $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}wc_orders WHERE {$where_clause} ORDER BY date_updated_gmt DESC", ...$params ); } else { $query = "SELECT id FROM {$wpdb->prefix}wc_orders WHERE {$where_clause} ORDER BY date_updated_gmt DESC"; } return array_map('intval', $wpdb->get_col($query)); } public function list_orders() { $this->validateRequest(); /* * Get Orders */ $order_ids = $this->getOrders(); if (empty($order_ids)) { echo json_encode( [ 'meta' => [ 'is_successful' => true, 'message' => 'No orders found.', ], 'data' => [ 'orders' => [], ], ], JSON_PRETTY_PRINT ); die(); } $responseOrders = []; foreach ($order_ids as $order_id) { $order = wc_get_order($order_id); $orderDataMapFunction = get_option( 'aftersalesprogr_order_data_mapper' ); if ( ! function_exists( $orderDataMapFunction ) ) { $orderDataMapFunction = 'aftersalesprogr_map_order_data'; } $tempData = ( $orderDataMapFunction )( $order ); if (!empty($tempData)) { $responseOrders[] = $tempData; } } echo json_encode( [ 'meta' => [ 'is_successful' => true, 'message' => 'No orders found.', ], 'data' => [ 'orders' => $responseOrders, ], ], JSON_PRETTY_PRINT ); } public function complete_order( $order_id = null ) { $this->validateRequest(); $envelope = [ 'meta' => [ 'is_successful' => false, ], 'data' => [], ]; $order_id = (int) $order_id; if ( $order_id <= 0 ) { $envelope['meta']['message'] = 'Wrong order_id.'; } else { $order = wc_get_order( $order_id ); if ( ! $order ) { $envelope['meta']['message'] = 'Order not found.'; } else { $envelope['meta']['is_successful'] = true; if ( isset( $_REQUEST['comment'] ) && $_REQUEST['comment'] != '' ) { $is_customer = isset( $_REQUEST['is_customer'] ) && $_REQUEST['is_customer'] == 1; $order->add_order_note( sanitize_text_field( $_REQUEST['comment'] ), $is_customer ); } $status = sanitize_text_field( $_REQUEST['order_status'] ); $order->update_status( $status ); } } echo json_encode( $envelope, JSON_PRETTY_PRINT ); } public function check_active_plugins( $name ): bool { $active_plugins = get_option( 'active_plugins' ); foreach ( $active_plugins as $active_plugin ) { if ( strpos( $active_plugin, $name ) !== false ) { return true; } } return false; } public function get_order_statuses() { $this->validateRequest(); $envelope = [ 'meta' => [ 'is_successful' => false, ], 'data' => [], ]; $statuses = wc_get_order_statuses(); if ( isset( $statuses ) ) { $envelope['meta']['is_successful'] = true; $envelope['data']['statuses'] = $statuses; } echo json_encode( $envelope, JSON_PRETTY_PRINT ); } public function getProductArgs() { $args = []; $args['posts_per_page'] = - 1; $args['date_query'] = []; /* * support deprecated use of after */ $updated_at_from = $_REQUEST['updated_at_from'] ?? $_REQUEST['after'] ?? null; if ( isset( $updated_at_from ) ) { $args['date_query'][] = [ 'after' => sanitize_text_field( $updated_at_from ), 'column' => 'post_modified', ]; } if ( isset( $_REQUEST['updated_at_to'] ) ) { $args['date_query'][] = [ 'before' => sanitize_text_field( $_REQUEST['updated_at_to'] ), 'column' => 'post_modified', ]; } if ( isset( $_REQUEST['created_at_from'] ) ) { $args['date_query'][] = [ 'after' => sanitize_text_field( $_REQUEST['created_at_from'] ), 'column' => 'post_date', ]; } if ( isset( $_REQUEST['created_at_to'] ) ) { $args['date_query'][] = [ 'before' => sanitize_text_field( $_REQUEST['created_at_to'] ), 'column' => 'post_date', ]; } $args['post_status'] = 'any'; if ( isset( $_REQUEST['status'] ) ) { $args['post_status'] = explode( ',', sanitize_text_field( $_REQUEST['status'] ) ); } return $args; } public function get_products() { $this->validateRequest(); $args = $this->getProductArgs(); $envelope = [ 'meta' => [ 'is_successful' => false, ], 'data' => [], ]; $args['orderby'] = 'ID'; $products = wc_get_products( $args ); if (empty($products)) { echo json_encode( [ 'meta' => [ 'is_successful' => true, 'message' => 'No products found.', ], 'data' => [ 'products' => [], ], ], JSON_PRETTY_PRINT ); die(); } $responseProducts = []; $mapProductFunction = get_option( 'aftersalesprogr_product_mapper' ); if ( ! function_exists( $mapProductFunction ) ) { $mapProductFunction = 'aftersalesprogr_map_product'; } foreach ( $products as $product ) { $responseProducts[] = ( $mapProductFunction )( $product ); } if ( ! empty( $responseProducts ) ) { $envelope['meta']['is_successful'] = true; $envelope['data']['products'] = $responseProducts; } echo json_encode( $envelope, JSON_PRETTY_PRINT ); } public function get_categories() { $this->validateRequest(); $envelope = [ 'meta' => [ 'is_successful' => false, ], 'data' => [], ]; $taxonomy = 'product_cat'; $orderby = 'name'; $show_count = 0; // 1 for yes, 0 for no $pad_counts = 0; // 1 for yes, 0 for no $hierarchical = 1; // 1 for yes, 0 for no $title = ''; $empty = 0; $args = [ 'taxonomy' => $taxonomy, 'orderby' => $orderby, 'show_count' => $show_count, 'pad_counts' => $pad_counts, 'hierarchical' => $hierarchical, 'title_li' => $title, 'hide_empty' => $empty, ]; $all_categories = get_categories( $args ); if (empty($all_categories)) { echo json_encode( [ 'meta' => [ 'is_successful' => true, 'message' => 'No categories found.', ], 'data' => [ 'categories' => [], ], ], JSON_PRETTY_PRINT ); die(); } $envelope['meta']['is_successful'] = true; $envelope['data']['categories'] = $all_categories; echo json_encode( $envelope, JSON_PRETTY_PRINT ); } public function get_version() { $this->validateRequest(); $envelope = [ 'meta' => [ 'is_successful' => true, ], 'data' => [ 'plugin_version' => AFTERSALESPROGR_VERSION, 'wp_version' => get_bloginfo( 'version' ), 'php_version' => phpversion(), 'wc_version' => get_option( 'woocommerce_version' ), 'url' => get_bloginfo( 'wpurl' ), 'plugins' => get_option( 'active_plugins' ), 'configuration' => [ 'trackingwidget_status' => get_option('aftersalesprogr_trackingwidget_status'), 'trackingwidget_uuid' => get_option('aftersalesprogr_trackingwidget_uuid'), 'order_data_mapper' => get_option('aftersalesprogr_order_data_mapper'), 'product_mapper' => get_option('aftersalesprogr_product_mapper'), 'woocommerce_settings' => get_option('woocommerce_Aftersalespro_ShippingMethod_settings'), ], ], ]; echo json_encode( $envelope, JSON_PRETTY_PRINT ); } } header( 'Content-Type: application/json' ); $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : null; switch ( $action ): case 'complete_order': $orderId = isset( $_REQUEST['order_id'] ) ? intval( $_REQUEST['order_id'] ) : null; ( new AfterSalesProGrApi() )->complete_order( $orderId ); break; case 'get_orders_statuses': ( new AfterSalesProGrApi() )->get_order_statuses(); break; case 'get_products': ( new AfterSalesProGrApi() )->get_products(); break; case 'version': ( new AfterSalesProGrApi() )->get_version(); break; case 'get_orders': ( new AfterSalesProGrApi() )->list_orders(); break; case 'get_categories': ( new AfterSalesProGrApi() )->get_categories(); break; default: echo json_encode( [ 'meta' => [ 'is_successful' => true, 'message' => 'OK', 'version' => AFTERSALESPROGR_VERSION, ], ], JSON_PRETTY_PRINT ); break; endswitch;