Automattic\WooCommerce\Blueprint\Importers

ImportSetSiteOptions::get_collation_restricted_option_indexes │ private │ WC 1.0

Match candidates against restricted names using the options table collation.

The empty option_name branches give both derived tables the real column type and collation without requiring a restricted option row to exist.

Method of the class: ImportSetSiteOptions{}

No Hooks.

Returns

array<int>. Indexes of candidates that match a restricted option.

Usage

// private - for code of main (parent) class only
$result = $this->get_collation_restricted_option_indexes( $candidate_names ): array;
$candidate_names(array) (required)
Trimmed option names from the blueprint.

ImportSetSiteOptions::get_collation_restricted_option_indexes() code WC 11.1.2

private function get_collation_restricted_option_indexes( array $candidate_names ): array {
	global $wpdb;

	if ( empty( $candidate_names ) || ! isset( $wpdb ) || ! is_object( $wpdb ) || ! isset( $wpdb->options ) || ! method_exists( $wpdb, 'get_col' ) || ! method_exists( $wpdb, 'prepare' ) ) {
		return array();
	}

	$candidate_rows  = implode(
		' UNION ALL ',
		array_fill( 0, count( $candidate_names ), 'SELECT %d AS candidate_index, %s AS option_name' )
	);
	$restricted_rows = implode(
		' UNION ALL ',
		array_fill( 0, count( self::RESTRICTED_OPTIONS ), 'SELECT %s AS option_name' )
	);
	$query_args      = array();

	foreach ( $candidate_names as $candidate_index => $candidate_name ) {
		$query_args[] = $candidate_index;
		$query_args[] = $candidate_name;
	}
	$query_args = array_merge( $query_args, self::RESTRICTED_OPTIONS );

	$query = "
		SELECT DISTINCT candidates.candidate_index
		FROM (
			SELECT 0 AS candidate_index, option_name FROM {$wpdb->options} WHERE 1 = 0
			UNION ALL {$candidate_rows}
		) AS candidates
		INNER JOIN (
			SELECT option_name FROM {$wpdb->options} WHERE 1 = 0
			UNION ALL {$restricted_rows}
		) AS restricted_options USING ( option_name )
	";

	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- A single prepared comparison applies the options table's collation without an N-query loop.
	$restricted_indexes = $wpdb->get_col( $wpdb->prepare( $query, $query_args ) );

	return array_map( 'intval', $restricted_indexes );
}