PHP 8.2.31
Preview: AnalyticsImports.php Size: 9.09 KB
/home/nshryvcy/radiantskinclinics.org/wp-content/plugins/woocommerce/src/Admin/API/AnalyticsImports.php

<?php
/**
 * REST API Analytics Imports Controller
 *
 * Handles requests to get batch import status and trigger manual imports.
 */

declare( strict_types = 1 );

namespace Automattic\WooCommerce\Admin\API;

use WP_Error;
use Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler;

defined( 'ABSPATH' ) || exit;

/**
 * REST API Analytics Imports Controller.
 *
 * @internal
 */
class AnalyticsImports extends \WC_REST_Data_Controller {
	/**
	 * Endpoint namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'wc-analytics';

	/**
	 * Route base.
	 *
	 * @var string
	 */
	protected $rest_base = 'imports';

	/**
	 * Register routes.
	 *
	 * @return void
	 */
	public function register_routes(): void {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/status',
			array(
				array(
					'methods'             => \WP_REST_Server::READABLE,
					'callback'            => array( $this, 'get_status' ),
					'permission_callback' => array( $this, 'permissions_check' ),
				),
				'schema' => array( $this, 'get_status_schema' ),
			)
		);

		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/trigger',
			array(
				array(
					'methods'             => \WP_REST_Server::CREATABLE,
					'callback'            => array( $this, 'trigger_import' ),
					'permission_callback' => array( $this, 'permissions_check' ),
				),
				'schema' => array( $this, 'get_trigger_schema' ),
			)
		);
	}

	/**
	 * Check if a given request has access to analytics imports.
	 *
	 * @param  \WP_REST_Request<array<string, mixed>> $request Full details about the request.
	 * @return WP_Error|boolean
	 */
	public function permissions_check( $request ) {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			return new WP_Error(
				'woocommerce_rest_cannot_access',
				__( 'Sorry, you cannot access analytics imports.', 'woocommerce' ),
				array( 'status' => rest_authorization_required_code() )
			);
		}

		return true;
	}

	/**
	 * Get the current import status.
	 *
	 * @param  \WP_REST_Request<array<string, mixed>> $request Full details about the request.
	 * @return \WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function get_status( $request ) {
		$is_scheduled_mode = $this->is_scheduled_import_enabled();
		$mode              = $is_scheduled_mode ? 'scheduled' : 'immediate';

		$response = array(
			'mode'                      => $mode,
			'last_processed_date'       => null,
			'next_scheduled'            => null,
			'import_in_progress_or_due' => null,
		);

		// For scheduled mode, populate additional fields.
		if ( $is_scheduled_mode ) {
			$last_processed_gmt                    = get_option( OrdersScheduler::LAST_PROCESSED_ORDER_DATE_OPTION, null );
			$response['last_processed_date']       = ( is_string( $last_processed_gmt ) && $last_processed_gmt ) ? get_date_from_gmt( $last_processed_gmt, 'Y-m-d H:i:s' ) : null;
			$response['next_scheduled']            = $this->get_next_scheduled_time();
			$response['import_in_progress_or_due'] = $this->is_import_in_progress_or_due();
		}

		return rest_ensure_response( $response );
	}

	/**
	 * Trigger a manual import.
	 *
	 * @param  \WP_REST_Request<array<string, mixed>> $request Full details about the request.
	 * @return \WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
	 */
	public function trigger_import( $request ) {
		$is_scheduled_mode = $this->is_scheduled_import_enabled();

		// Return error if in immediate mode.
		if ( ! $is_scheduled_mode ) {
			return new WP_Error(
				'woocommerce_rest_analytics_import_immediate_mode',
				__( 'Manual import is not available in immediate mode. Imports happen automatically.', 'woocommerce' ),
				array( 'status' => 400 )
			);
		}

		// Check if an import is already in progress or due to run soon.
		if ( $this->is_import_in_progress_or_due() ) {
			return new WP_Error(
				'woocommerce_rest_analytics_import_in_progress',
				__( 'A batch import is already in progress or scheduled to run soon. Please wait for it to complete before triggering a new import.', 'woocommerce' ),
				array( 'status' => 400 )
			);
		}

		// Trigger the batch import immediately by rescheduling the recurring processor.
		// This unschedules the current recurring action and reschedules it to run now.
		$action_hook = OrdersScheduler::get_action( OrdersScheduler::PROCESS_PENDING_ORDERS_BATCH_ACTION );
		if ( ! is_string( $action_hook ) ) {
			return new WP_Error(
				'woocommerce_rest_analytics_import_invalid_action',
				__( 'Invalid action hook for batch import.', 'woocommerce' ),
				array( 'status' => 500 )
			);
		}
		WC()->queue()->cancel_all( $action_hook, array(), (string) OrdersScheduler::$group );
		OrdersScheduler::schedule_recurring_batch_processor();

		return rest_ensure_response(
			array(
				'success' => true,
				'message' => __( 'Batch import triggered successfully.', 'woocommerce' ),
			)
		);
	}

	/**
	 * Check if scheduled import is enabled.
	 *
	 * @return bool
	 */
	private function is_scheduled_import_enabled() {
		return 'yes' === get_option( OrdersScheduler::SCHEDULED_IMPORT_OPTION, OrdersScheduler::SCHEDULED_IMPORT_OPTION_DEFAULT_VALUE );
	}

	/**
	 * Get the next scheduled time for the batch processor.
	 *
	 * @return string|null Datetime string in site timezone or null if not scheduled.
	 */
	private function get_next_scheduled_time() {
		$action_hook = OrdersScheduler::get_action( OrdersScheduler::PROCESS_PENDING_ORDERS_BATCH_ACTION );
		if ( ! is_string( $action_hook ) ) {
			return null;
		}
		$next_time = WC()->queue()->get_next( $action_hook, array(), (string) OrdersScheduler::$group );

		if ( ! $next_time ) {
			return null;
		}

		// Convert UTC timestamp to site timezone.
		return get_date_from_gmt( $next_time->format( 'Y-m-d H:i:s' ), 'Y-m-d H:i:s' );
	}

	/**
	 * Get the schema for the status endpoint, conforming to JSON Schema.
	 *
	 * @return array
	 */
	public function get_status_schema() {
		$schema = array(
			'$schema'    => 'https://json-schema.org/draft-04/schema#',
			'title'      => 'analytics_import_status',
			'type'       => 'object',
			'properties' => array(
				'mode'                      => array(
					'type'        => 'string',
					'enum'        => array( 'scheduled', 'immediate' ),
					'description' => __( 'Current import mode.', 'woocommerce' ),
					'context'     => array( 'view' ),
					'readonly'    => true,
				),
				'last_processed_date'       => array(
					'type'        => array( 'string', 'null' ),
					'description' => __( 'Last processed order date (null in immediate mode).', 'woocommerce' ),
					'context'     => array( 'view' ),
					'readonly'    => true,
				),
				'next_scheduled'            => array(
					'type'        => array( 'string', 'null' ),
					'description' => __( 'Next scheduled import time (null in immediate mode).', 'woocommerce' ),
					'context'     => array( 'view' ),
					'readonly'    => true,
				),
				'import_in_progress_or_due' => array(
					'type'        => array( 'boolean', 'null' ),
					'description' => __( 'Whether a batch import is currently running or scheduled to run within the next minute (null in immediate mode).', 'woocommerce' ),
					'context'     => array( 'view' ),
					'readonly'    => true,
				),
			),
		);

		return $this->add_additional_fields_schema( $schema );
	}

	/**
	 * Get the schema for the trigger endpoint, conforming to JSON Schema.
	 *
	 * @return array
	 */
	public function get_trigger_schema() {
		$schema = array(
			'$schema'    => 'https://json-schema.org/draft-04/schema#',
			'title'      => 'analytics_import_trigger',
			'type'       => 'object',
			'properties' => array(
				'success' => array(
					'type'        => 'boolean',
					'description' => __( 'Whether the trigger was successful.', 'woocommerce' ),
					'context'     => array( 'view' ),
					'readonly'    => true,
				),
				'message' => array(
					'type'        => 'string',
					'description' => __( 'Result message.', 'woocommerce' ),
					'context'     => array( 'view' ),
					'readonly'    => true,
				),
			),
		);

		return $this->add_additional_fields_schema( $schema );
	}

	/**
	 * Check if a batch import is currently in progress or due to run soon.
	 *
	 * @return bool True if a batch import is in progress or scheduled to run within the next minute, false otherwise.
	 */
	private function is_import_in_progress_or_due() {
		$hook = OrdersScheduler::get_action( OrdersScheduler::PROCESS_PENDING_ORDERS_BATCH_ACTION );
		if ( ! is_string( $hook ) ) {
			return false;
		}

		// Check for actions with 'in-progress' status.
		$in_progress_actions = WC()->queue()->search(
			array(
				'hook'     => $hook,
				'status'   => 'in-progress',
				'per_page' => 1,
			),
			'ids'
		);

		if ( ! empty( $in_progress_actions ) ) {
			return true;
		}

		// Check if the next scheduled import is due within 1 minute.
		$next_scheduled = WC()->queue()->get_next( $hook, array(), (string) OrdersScheduler::$group );
		if ( $next_scheduled ) {
			$time_until_next = $next_scheduled->getTimestamp() - time();
			// Consider it "due" if it's scheduled to run within the next 60 seconds.
			if ( $time_until_next <= MINUTE_IN_SECONDS ) {
				return true;
			}
		}

		return false;
	}
}

Directory Contents

Dirs: 3 × Files: 46

Name Size Perms Modified Actions
AI DIR
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
Reports DIR
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
Templates DIR
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
9.09 KB lrw-r--r-- 2026-01-19 14:46:18
Edit Download
2.15 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
3.40 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
2.11 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
939 B lrw-r--r-- 2022-04-20 06:50:54
Edit Download
1.12 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
4.15 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
1.82 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
1.70 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
10.57 KB lrw-r--r-- 2026-01-19 14:46:18
Edit Download
5.15 KB lrw-r--r-- 2024-12-18 22:19:16
Edit Download
18.22 KB lrw-r--r-- 2024-07-30 19:31:16
Edit Download
4.90 KB lrw-r--r-- 2026-05-05 14:26:50
Edit Download
9.64 KB lrw-r--r-- 2024-03-26 16:56:02
Edit Download
6.02 KB lrw-r--r-- 2023-01-25 03:19:12
Edit Download
5.74 KB lrw-r--r-- 2023-01-25 03:19:12
Edit Download
3.41 KB lrw-r--r-- 2026-05-05 14:26:50
Edit Download
5.94 KB lrw-r--r-- 2024-01-30 23:24:56
Edit Download
2.10 KB lrw-r--r-- 2022-09-20 22:53:36
Edit Download
2.39 KB lrw-r--r-- 2023-03-21 20:45:06
Edit Download
25.32 KB lrw-r--r-- 2026-02-23 17:58:34
Edit Download
2.38 KB lrw-r--r-- 2025-03-03 22:28:12
Edit Download
2.58 KB lrw-r--r-- 2024-07-30 19:31:16
Edit Download
10.77 KB lrw-r--r-- 2026-05-05 14:26:50
Edit Download
1.94 KB lrw-r--r-- 2024-01-30 23:24:56
Edit Download
1.80 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
18.38 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
32.02 KB lrw-r--r-- 2025-10-06 17:56:06
Edit Download
5.49 KB lrw-r--r-- 2025-10-06 17:56:06
Edit Download
10.00 KB lrw-r--r-- 2025-05-26 19:11:58
Edit Download
10.13 KB lrw-r--r-- 2024-07-30 19:31:16
Edit Download
5.91 KB lrw-r--r-- 2026-05-05 14:26:50
Edit Download
21.27 KB lrw-r--r-- 2026-05-05 14:26:50
Edit Download
4.46 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
4.36 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
458 B lrw-r--r-- 2022-04-20 06:50:54
Edit Download
3.06 KB lrw-r--r-- 2023-02-22 07:17:34
Edit Download
1.30 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
9.73 KB lrw-r--r-- 2024-10-21 23:53:16
Edit Download
17.62 KB lrw-r--r-- 2026-02-23 17:58:34
Edit Download
6.03 KB lrw-r--r-- 2025-01-21 18:53:44
Edit Download
878 B lrw-r--r-- 2023-03-21 20:45:06
Edit Download
4.20 KB lrw-r--r-- 2025-03-03 22:28:12
Edit Download
6.33 KB lrw-r--r-- 2026-05-11 17:17:08
Edit Download
4.90 KB lrw-r--r-- 2022-04-20 06:50:54
Edit Download
6.12 KB lrw-r--r-- 2024-02-27 18:59:46
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).