Automattic\WooCommerce\Internal\Admin

OrderMilestoneEasterEgg::handle_admin_enqueue_scriptspublicWC 1.0

Enqueues the milestone overlay script when the current order is a qualifying milestone.

Method of the class: OrderMilestoneEasterEgg{}

Hooks from the method

Returns

null. Nothing (null).

Usage

$OrderMilestoneEasterEgg = new OrderMilestoneEasterEgg();
$OrderMilestoneEasterEgg->handle_admin_enqueue_scripts(): void;

OrderMilestoneEasterEgg::handle_admin_enqueue_scripts() code WC 10.9.1

public function handle_admin_enqueue_scripts(): void {
	/**
	 * Filters whether the order milestone easter egg feature is enabled.
	 *
	 * Return false to disable the feature entirely — no order queries or assets will be loaded.
	 *
	 * @param bool $enabled Whether the feature is enabled. Default true.
	 *
	 * @since 10.9.0
	 */
	if ( ! apply_filters( 'wc_order_milestone_egg_enabled', true ) ) {
		return;
	}

	if ( ! function_exists( 'wc_get_order' ) ) {
		return;
	}

	// phpcs:disable WordPress.Security.NonceVerification.Recommended
	$woo_egg_key  = isset( $_GET['woo_egg'] ) ? sanitize_text_field( wp_unslash( $_GET['woo_egg'] ) ) : '';
	$page_param   = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
	$action_param = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
	$id_param     = isset( $_GET['id'] ) ? absint( wp_unslash( $_GET['id'] ) ) : 0;
	// phpcs:enable WordPress.Security.NonceVerification.Recommended

	// Preview: ?woo_egg=first|hundred|thousand lets admins preview any milestone without real orders.
	// Only available when WP_DEBUG is enabled to prevent accidental triggering in production.
	$is_debug_preview = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) && current_user_can( 'manage_options' ) && '' !== $woo_egg_key;

	// Respect the user's opt-out preference (debug preview always shows).
	if ( ! $is_debug_preview && get_user_meta( get_current_user_id(), '_wc_egg_opted_out', true ) ) {
		return;
	}

	// Only run milestone logic on the HPOS order edit page to avoid overhead on every admin page.
	$is_order_edit_page = 'wc-orders' === $page_param && 'edit' === $action_param;

	if ( ! $is_debug_preview && ! $is_order_edit_page ) {
		return;
	}

	// For real order pages: check cheaply whether the current order qualifies
	// before running the milestone lookup. The lookup relies on HPOS columns.
	if ( ! $is_debug_preview ) {
		if (
			! OrderUtil::custom_orders_table_usage_is_enabled()
			|| $id_param <= 0
			|| ! $this->is_qualifying_order( $id_param )
		) {
			return;
		}
	}

	$milestone_map = $is_debug_preview ? array() : $this->get_milestone_map();

	if ( ! $is_debug_preview && empty( $milestone_map ) ) {
		return;
	}

	// Remove milestones the current user has already seen.
	if ( ! $is_debug_preview ) {
		$user_id = get_current_user_id();
		foreach ( array_keys( $milestone_map ) as $order_id ) {
			if ( get_user_meta( $user_id, '_wc_egg_seen_' . $order_id, true ) ) {
				unset( $milestone_map[ $order_id ] );
			}
		}
		if ( empty( $milestone_map ) ) {
			return;
		}

		// Only show the overlay when the current order is itself the milestone.
		if ( ! isset( $milestone_map[ $id_param ] ) ) {
			return;
		}
		$milestone_map = array( $id_param => $milestone_map[ $id_param ] );
	}

	// Only load the SVG variants needed for the matched milestones.
	$all_msgs = array();
	if ( $is_debug_preview ) {
		$all_msgs        = $this->get_milestone_messages();
		$preview_variant = $all_msgs[ $woo_egg_key ]['variant'] ?? null;
		$needed_variants = $preview_variant ? array( $preview_variant ) : array_keys( $this->get_variant_map() );
	} else {
		$needed_variants = array_unique(
			array_filter( array_column( array_values( $milestone_map ), 'variant' ) )
		);
	}

	$svg_data = $this->get_svg_data( $needed_variants );
	$labels   = $this->get_ui_labels();

	WCAdminAssets::register_script( 'wp-admin-scripts', 'order-milestone-easter-egg', true );

	$localize_data = array(
		'milestones' => $milestone_map,
		'svgData'    => $svg_data,
		'labels'     => $labels,
		'dismiss'    => array(
			'url'   => admin_url( 'admin-ajax.php' ),
			'nonce' => wp_create_nonce( 'wc_egg_dismiss' ),
		),
	);

	if ( $is_debug_preview ) {
		$localize_data['allMilestones'] = $all_msgs;
	}

	wp_localize_script( 'wc-admin-order-milestone-easter-egg', 'wcOrderMilestoneEgg', $localize_data );
}