Automattic\WooCommerce\Admin\API

OnboardingThemes::get_recommended_themes()publicWC 1.0

Get recommended themes.

Метод класса: OnboardingThemes{}

Возвращает

WP_Error|Массив. Theme activation status.

Использование

$OnboardingThemes = new OnboardingThemes();
$OnboardingThemes->get_recommended_themes( $request );
$request(WP_REST_Request) (обязательный)
Full details about the request.

Код OnboardingThemes::get_recommended_themes() WC 9.7.1

public function get_recommended_themes( $request ) {
	// Check if "industry" and "currency" parameters are provided in the request.
	$industry = $request->get_param( 'industry' );
	$currency = $request->get_param( 'currency' ) ?? 'USD';

	// Return empty response if marketplace suggestions are disabled.
	if (
		/**
		 * Filter allow marketplace suggestions.
		 *
		 * User can disable all suggestions via filter.
		 *
		 * @since 8.3.0
		 */
		! apply_filters( 'woocommerce_allow_marketplace_suggestions', true ) ||
		get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) === 'no'
	) {

		/**
		 * Filter the onboarding recommended themes response.
		 *
		 * @since 8.3.0
		 *
		 * @param array $response The recommended themes response.
		 * @param array $filtered_themes The filtered themes.
		 * @param string $industry The industry to filter by (if provided).
		 * @param string $currency The currency to convert prices to. (USD, AUD, CAD, EUR, GBP).
		 *
		 * @return array
		 */
		return apply_filters(
			'__experimental_woocommerce_rest_get_recommended_themes',
			array(
				'themes' => array(),
				'_links' => array(
					'browse_all' => array(
						'href' => home_url( '/wp-admin/themes.php' ),
					),
				),
			),
			$industry,
			$currency
		);
	}

	$in_app_purchase_params               = \WC_Admin_Addons::get_in_app_purchase_url_params();
	$in_app_purchase_params['wccom-back'] = rawurlencode( '/wp-admin/admin.php?page=wc-admin&path=/customize-store' );

	$core_themes = array(
		array(
			'name'           => 'Twenty Twenty-Four',
			'price'          => __( 'Free', 'woocommerce' ),
			'is_free'        => true,
			'color_palettes' => array(
				array(
					'title'     => 'Black and white',
					'primary'   => '#FEFBF3',
					'secondary' => '#7F7E7A',
				),
				array(
					'title'     => 'Brown Sugar',
					'primary'   => '#EFEBE0',
					'secondary' => '#AC6239',
				),
				array(
					'title'     => 'Midnight',
					'primary'   => '#161514',
					'secondary' => '#AFADA7',
				),
				array(
					'title'     => 'Olive',
					'primary'   => '#FEFBF3',
					'secondary' => '#7F7E7A',
				),
			),
			'total_palettes' => 0,
			'slug'           => 'twentytwentyfour',
			'thumbnail_url'  => 'https://i0.wp.com/themes.svn.wordpress.org/twentytwentyfour/1.0/screenshot.png',
			'link_url'       => 'https://wordpress.org/themes/twentytwentyfour/',
		),
		array(
			'name'           => 'Highline',
			/* translators: %d: price */
			'price'          => sprintf( __( '$%d/year', 'woocommerce' ), 79 ),
			'is_free'        => false,
			'color_palettes' => array(
				array(
					'title'     => 'Primary',
					'primary'   => '#211f1d',
					'secondary' => '#211f1d',
				),
				array(
					'title'     => 'Additional color',
					'primary'   => '#aaaaaa',
					'secondary' => '#aaaaaa',
				),
				array(
					'title'     => 'Accent Background',
					'primary'   => '#b04b3c',
					'secondary' => '#b04b3c',
				),
				array(
					'title'     => 'Secondary Background',
					'primary'   => '#dabfa1',
					'secondary' => '#dabfa1',
				),
			),
			'total_palettes' => 9,
			'slug'           => 'highline',
			'thumbnail_url'  => 'https://woocommerce.com/wp-content/uploads/2023/12/Featured-image-538x403-1.png',
			'link_url'       => add_query_arg( $in_app_purchase_params, 'https://woocommerce.com/products/highline/' ),
		),
		array(
			'name'           => 'Luminate',
			/* translators: %d: price */
			'price'          => sprintf( __( '$%d/year', 'woocommerce' ), 79 ),
			'is_free'        => false,
			'color_palettes' => array(
				array(
					'title'     => 'Primary',
					'primary'   => '#242a2e',
					'secondary' => '#242a2e',
				),
				array(
					'title'     => 'Lite',
					'primary'   => '#f6f5f2',
					'secondary' => '#f6f5f2',
				),
				array(
					'title'     => 'Grey',
					'primary'   => '#a5a5a5',
					'secondary' => '#a5a5a5',
				),
				array(
					'title'     => 'Lite Grey',
					'primary'   => '#e4e4e1',
					'secondary' => '#e4e4e1',
				),
			),
			'total_palettes' => 5,
			'slug'           => 'luminate',
			'thumbnail_url'  => 'https://woocommerce.com/wp-content/uploads/2022/07/Featured-image-538x403-2.png',
			'link_url'       => add_query_arg( $in_app_purchase_params, 'https://woocommerce.com/products/luminate/' ),
		),
		array(
			'name'           => 'Gizmo',
			/* translators: %d: price */
			'price'          => sprintf( __( '$%d/year', 'woocommerce' ), 79 ),
			'is_free'        => false,
			'color_palettes' => array(
				array(
					'title'     => 'Primary',
					'primary'   => '#ff5833',
					'secondary' => '#ff5833',
				),
				array(
					'title'     => 'Foreground',
					'primary'   => '#111111',
					'secondary' => '#111111',
				),
				array(
					'title'     => 'Background',
					'primary'   => '#FFFFFF',
					'secondary' => '#FFFFFF',
				),
				array(
					'title'     => 'Base',
					'primary'   => '#595959',
					'secondary' => '#595959',
				),
			),
			'total_palettes' => 10,
			'slug'           => 'gizmo',
			'thumbnail_url'  => 'https://woocommerce.com/wp-content/uploads/2022/11/gizmo-regular-card-product-logo.jpg?w=900',
			'link_url'       => add_query_arg( $in_app_purchase_params, 'https://woocommerce.com/products/gizmo/' ),
		),
	);

	// To be implemented: 1. Fetch themes from the marketplace API. 2. Convert prices to the requested currency.
	// These are Dotcom themes.
	$default_themes = array(
		array(
			'name'           => 'Tsubaki',
			'price'          => __( 'Free', 'woocommerce' ),
			'is_free'        => true,
			'color_palettes' => array(),
			'total_palettes' => 0,
			'slug'           => 'tsubaki',
			'thumbnail_url'  => 'https://i0.wp.com/s2.wp.com/wp-content/themes/premium/tsubaki/screenshot.png',
			'link_url'       => 'https://wordpress.com/theme/tsubaki/',
		),
		array(
			'name'           => 'Tazza',
			'price'          => __( 'Free', 'woocommerce' ),
			'is_free'        => true,
			'color_palettes' => array(),
			'total_palettes' => 0,
			'slug'           => 'tazza',
			'thumbnail_url'  => 'https://i0.wp.com/s2.wp.com/wp-content/themes/premium/tazza/screenshot.png',
			'link_url'       => 'https://wordpress.com/theme/tazza/',
		),
		array(
			'name'           => 'Amulet',
			'price'          => __( 'Free', 'woocommerce' ),
			'is_free'        => true,
			'color_palettes' => array(
				array(
					'title'     => 'Default',
					'primary'   => '#FEFBF3',
					'secondary' => '#7F7E7A',
				),
				array(
					'title'     => 'Brown Sugar',
					'primary'   => '#EFEBE0',
					'secondary' => '#AC6239',
				),
				array(
					'title'     => 'Midnight',
					'primary'   => '#161514',
					'secondary' => '#AFADA7',
				),
				array(
					'title'     => 'Olive',
					'primary'   => '#FEFBF3',
					'secondary' => '#7F7E7A',
				),
			),
			'total_palettes' => 5,
			'slug'           => 'amulet',
			'thumbnail_url'  => 'https://i0.wp.com/s2.wp.com/wp-content/themes/premium/amulet/screenshot.png',
			'link_url'       => 'https://wordpress.com/theme/amulet/',
		),
		array(
			'name'           => 'Zaino',
			'price'          => __( 'Free', 'woocommerce' ),
			'is_free'        => true,
			'color_palettes' => array(
				array(
					'title'     => 'Default',
					'primary'   => '#202124',
					'secondary' => '#E3CBC0',
				),
				array(
					'title'     => 'Aubergine',
					'primary'   => '#1B1031',
					'secondary' => '#E1746D',
				),
				array(
					'title'     => 'Block out',
					'primary'   => '#FF5252',
					'secondary' => '#252525',
				),
				array(
					'title'     => 'Canary',
					'primary'   => '#FDFF85',
					'secondary' => '#353535',
				),
			),
			'total_palettes' => 11,
			'slug'           => 'zaino',
			'thumbnail_url'  => 'https://i0.wp.com/s2.wp.com/wp-content/themes/premium/zaino/screenshot.png',
			'link_url'       => 'https://wordpress.com/theme/zaino/',
		),
	);

	$ai_connection_enabled = get_option( 'woocommerce_blocks_allow_ai_connection' );
	$themes                = $ai_connection_enabled ? $default_themes : $core_themes;

	// To be implemented: Filter themes based on industry.
	if ( $industry ) {
		$filtered_themes = array_filter(
			$themes,
			function ( $theme ) use ( $industry ) {
				// Filter themes by industry.
				// Example: return $theme['industry'] === $industry;.
				return true;
			}
		);
	} else {
		$filtered_themes = $themes;
	}

	$response = array(
		'themes' => $filtered_themes,
		'_links' => array(
			'browse_all' => array(
				'href' => admin_url( 'themes.php' ),
			),
		),
	);

	/**
	 * Filter the onboarding recommended themes response.
	 *
	 * @since 8.3.0
	 *
	 * @param array $response The recommended themes response.
	 * @param array $filtered_themes The filtered themes.
	 * @param string $industry The industry to filter by (if provided).
	 * @param string $currency The currency to convert prices to. (USD, AUD, CAD, EUR, GBP).
	 *
	 * @return array
	 */
	$filtered_response = apply_filters(
		'__experimental_woocommerce_rest_get_recommended_themes',
		$response,
		$industry,
		$currency
	);

	/**
	 * Loop through themes checking to see if any are currently active
	 */
	$active_theme = get_stylesheet();

	foreach ( $filtered_response['themes'] as &$theme ) {
		if ( $theme['slug'] === $active_theme ) {
			$theme['is_active'] = true;
		} else {
			$theme['is_active'] = false;
		}
	}

	return $filtered_response;
}