$headers,
'user-agent' => 'WooCommerce Addons Page',
)
);
if ( ! is_wp_error( $raw_featured ) ) {
$featured = json_decode( wp_remote_retrieve_body( $raw_featured ) );
if ( $featured ) {
set_transient( 'wc_addons_featured', $featured, DAY_IN_SECONDS );
}
}
}
if ( is_object( $featured ) ) {
self::output_featured_sections( $featured->sections );
return $featured;
}
}
/**
* Build url parameter string
*
* @param string $category Addon (sub) category.
* @param string $term Search terms.
* @param string $country Store country.
*
* @return string url parameter string
*/
public static function build_parameter_string( $category, $term, $country ) {
$parameters = array(
'category' => $category,
'term' => $term,
'country' => $country,
);
return '?' . http_build_query( $parameters );
}
/**
* Call API to get extensions
*
* @param string $category Addon (sub) category.
* @param string $term Search terms.
* @param string $country Store country.
*
* @return array of extensions
*/
public static function get_extension_data( $category, $term, $country ) {
$parameters = self::build_parameter_string( $category, $term, $country );
$headers = array();
$auth = WC_Helper_Options::get( 'auth' );
if ( ! empty( $auth['access_token'] ) ) {
$headers['Authorization'] = 'Bearer ' . $auth['access_token'];
}
$raw_extensions = wp_safe_remote_get(
'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' . $parameters,
array( 'headers' => $headers )
);
if ( ! is_wp_error( $raw_extensions ) ) {
$addons = json_decode( wp_remote_retrieve_body( $raw_extensions ) )->products;
}
return $addons;
}
/**
* Get sections for the addons screen
*
* @return array of objects
*/
public static function get_sections() {
$addon_sections = get_transient( 'wc_addons_sections' );
if ( false === ( $addon_sections ) ) {
$raw_sections = wp_safe_remote_get(
'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories'
);
if ( ! is_wp_error( $raw_sections ) ) {
$addon_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) );
if ( $addon_sections ) {
set_transient( 'wc_addons_sections', $addon_sections, WEEK_IN_SECONDS );
}
}
}
return apply_filters( 'woocommerce_addons_sections', $addon_sections );
}
/**
* Get section for the addons screen.
*
* @param string $section_id Required section ID.
*
* @return object|bool
*/
public static function get_section( $section_id ) {
$sections = self::get_sections();
if ( isset( $sections[ $section_id ] ) ) {
return $sections[ $section_id ];
}
return false;
}
/**
* Get section content for the addons screen.
*
* @param string $section_id Required section ID.
*
* @return array
*/
public static function get_section_data( $section_id ) {
$section = self::get_section( $section_id );
$section_data = '';
if ( ! empty( $section->endpoint ) ) {
$section_data = get_transient( 'wc_addons_section_' . $section_id );
if ( false === $section_data ) {
$raw_section = wp_safe_remote_get( esc_url_raw( $section->endpoint ), array( 'user-agent' => 'WooCommerce Addons Page' ) );
if ( ! is_wp_error( $raw_section ) ) {
$section_data = json_decode( wp_remote_retrieve_body( $raw_section ) );
if ( ! empty( $section_data->products ) ) {
set_transient( 'wc_addons_section_' . $section_id, $section_data, WEEK_IN_SECONDS );
}
}
}
}
return apply_filters( 'woocommerce_addons_section_data', $section_data->products, $section_id );
}
/**
* Handles the outputting of a contextually aware Storefront link (points to child themes if Storefront is already active).
*/
public static function output_storefront_button() {
$template = get_option( 'template' );
$stylesheet = get_option( 'stylesheet' );
if ( 'storefront' === $template ) {
if ( 'storefront' === $stylesheet ) {
$url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/';
$text = __( 'Need a fresh look? Try Storefront child themes', 'woocommerce' );
$utm_content = 'nostorefrontchildtheme';
} else {
$url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/';
$text = __( 'View more Storefront child themes', 'woocommerce' );
$utm_content = 'hasstorefrontchildtheme';
}
} else {
$url = 'https://woocommerce.com/storefront/';
$text = __( 'Need a theme? Try Storefront', 'woocommerce' );
$utm_content = 'nostorefront';
}
$url = add_query_arg(
array(
'utm_source' => 'addons',
'utm_medium' => 'product',
'utm_campaign' => 'woocommerceplugin',
'utm_content' => $utm_content,
),
$url
);
echo '' . esc_html( $text ) . '' . "\n";
}
/**
* Handles the outputting of a banner block.
*
* @param object $block Banner data.
*/
public static function output_banner_block( $block ) {
?>
title ); ?>
description ); ?>
items as $item ) : ?>
title ); ?>
description ); ?>
href,
$item->button,
'addons-button-solid',
$item->plugin
);
?>
container ) && 'column_container_start' === $block->container ) {
?>
module ) {
?>
container ) && 'column_container_end' === $block->container ) {
?>
title ); ?>
description ); ?>
items as $item ) : ?>
title ); ?>
href,
$item->button,
'addons-button-solid',
$item->plugin
);
?>
description ); ?>
title ); ?>
description ); ?>
buttons as $button ) : ?>
href,
$button->text,
'addons-button-solid'
);
?>
title ); ?>
description ); ?>
items as $item ) : ?>
image ) ) : ?>
href,
$item->button,
'addons-button-outline-white'
);
?>
'woocommerce-services',
)
),
'install-addon_woocommerce-services'
);
$defaults = array(
'image' => WC()->plugin_url() . '/assets/images/wcs-extensions-banner-3x.jpg',
'image_alt' => __( 'WooCommerce Shipping', 'woocommerce' ),
'title' => __( 'Save time and money with WooCommerce Shipping', 'woocommerce' ),
'description' => __( 'Print discounted USPS and DHL labels straight from your WooCommerce dashboard and save on shipping.', 'woocommerce' ),
'button' => __( 'Free - Install now', 'woocommerce' ),
'href' => $button_url,
'logos' => array(),
);
switch ( $location['country'] ) {
case 'US':
$local_defaults = array(
'logos' => array_merge(
$defaults['logos'],
array(
array(
'link' => WC()->plugin_url() . '/assets/images/wcs-usps-logo.png',
'alt' => 'USPS logo',
),
array(
'link' => WC()->plugin_url() . '/assets/images/wcs-dhlexpress-logo.png',
'alt' => 'DHL Express logo',
),
)
),
);
break;
default:
$local_defaults = array();
}
$block_data = array_merge( $defaults, $local_defaults, $block );
?>
'woocommerce-payments',
)
),
'install-addon_woocommerce-payments'
);
$defaults = array(
'image' => WC()->plugin_url() . '/assets/images/wcpayments-icon-secure.png',
'image_alt' => __( 'WooCommerce Payments', 'woocommerce' ),
'title' => __( 'Payments made simple, with no monthly fees — exclusively for WooCommerce stores.', 'woocommerce' ),
'description' => __( 'Securely accept cards in your store. See payments, track cash flow into your bank account, and stay on top of disputes – right from your dashboard.', 'woocommerce' ),
'button' => __( 'Free - Install now', 'woocommerce' ),
'href' => $button_url,
'logos' => array(),
);
$block_data = array_merge( $defaults, $block );
?>
geowhitelist ) ) {
$section_object->geowhitelist = explode( ',', $section_object->geowhitelist );
}
if ( ! empty( $section_object->geoblacklist ) ) {
$section_object->geoblacklist = explode( ',', $section_object->geoblacklist );
}
if ( ! self::show_extension( $section_object ) ) {
return;
}
?>
module ) {
case 'banner_block':
self::output_banner_block( $section );
break;
case 'column_start':
self::output_column( $section );
break;
case 'column_end':
self::output_column( $section );
break;
case 'column_block':
self::output_column_block( $section );
break;
case 'small_light_block':
self::output_small_light_block( $section );
break;
case 'small_dark_block':
self::output_small_dark_block( $section );
break;
case 'wcs_banner_block':
self::output_wcs_banner_block( (array) $section );
break;
case 'wcpay_banner_block':
self::output_wcpay_banner_block( (array) $section );
break;
case 'promotion_block':
self::output_promotion_block( (array) $section );
break;
}
}
}
/**
* Returns in-app-purchase URL params.
*/
public static function get_in_app_purchase_url_params() {
// Get url (from path onward) for the current page,
// so WCCOM "back" link returns user to where they were.
$back_admin_path = add_query_arg( array() );
return array(
'wccom-site' => site_url(),
'wccom-back' => rawurlencode( $back_admin_path ),
'wccom-woo-version' => Constants::get_constant( 'WC_VERSION' ),
'wccom-connect-nonce' => wp_create_nonce( 'connect' ),
);
}
/**
* Add in-app-purchase URL params to link.
*
* Adds various url parameters to a url to support a streamlined
* flow for obtaining and setting up WooCommerce extensons.
*
* @param string $url Destination URL.
*/
public static function add_in_app_purchase_url_params( $url ) {
return add_query_arg(
self::get_in_app_purchase_url_params(),
$url
);
}
/**
* Outputs a button.
*
* @param string $url Destination URL.
* @param string $text Button label text.
* @param string $style Button style class.
* @param string $plugin The plugin the button is promoting.
*/
public static function output_button( $url, $text, $style, $plugin = '' ) {
$style = __( 'Free', 'woocommerce' ) === $text ? 'addons-button-outline-purple' : $style;
$style = is_plugin_active( $plugin ) ? 'addons-button-installed' : $style;
$text = is_plugin_active( $plugin ) ? __( 'Installed', 'woocommerce' ) : $text;
$url = self::add_in_app_purchase_url_params( $url );
?>
countries->get_base_country();
$addons = self::get_extension_data( $category, $term, $country );
}
/**
* Addon page view.
*
* @uses $addons
* @uses $sections
* @uses $theme
* @uses $current_section
*/
include_once dirname( __FILE__ ) . '/views/html-admin-page-addons.php';
}
/**
* Install WooCommerce Services from Extensions screens.
*/
public static function install_woocommerce_services_addon() {
check_admin_referer( 'install-addon_woocommerce-services' );
$services_plugin_id = 'woocommerce-services';
$services_plugin = array(
'name' => __( 'WooCommerce Services', 'woocommerce' ),
'repo-slug' => 'woocommerce-services',
);
WC_Install::background_installer( $services_plugin_id, $services_plugin );
wp_safe_redirect( remove_query_arg( array( 'install-addon', '_wpnonce' ) ) );
exit;
}
/**
* Install WooCommerce Payments from the Extensions screens.
*
* @param string $section Optional. Extenstions tab.
*
* @return void
*/
public static function install_woocommerce_payments_addon( $section = '_featured' ) {
check_admin_referer( 'install-addon_woocommerce-payments' );
$wcpay_plugin_id = 'woocommerce-payments';
$wcpay_plugin = array(
'name' => __( 'WooCommerce Payments', 'woocommerce' ),
'repo-slug' => 'woocommerce-payments',
);
WC_Install::background_installer( $wcpay_plugin_id, $wcpay_plugin );
do_action( 'woocommerce_addon_installed', $wcpay_plugin_id, $section );
wp_safe_redirect( remove_query_arg( array( 'install-addon', '_wpnonce' ) ) );
exit;
}
/**
* Should an extension be shown on the featured page.
*
* @param object $item Item data.
* @return boolean
*/
public static function show_extension( $item ) {
$location = WC()->countries->get_base_country();
if ( isset( $item->geowhitelist ) && ! in_array( $location, $item->geowhitelist, true ) ) {
return false;
}
if ( isset( $item->geoblacklist ) && in_array( $location, $item->geoblacklist, true ) ) {
return false;
}
if ( is_plugin_active( $item->plugin ) ) {
return false;
}
return true;
}
/**
* We're displaying page=wc-addons and page=wc-addons§ion=helper as two separate pages.
* When we're on those pages, add body classes to distinguishe them.
*
* @param string $admin_body_class
*
* @return mixed|string
*/
public static function filter_admin_body_classes( string $admin_body_class = '' ) {
if ( isset( $_GET['section'] ) && 'helper' === $_GET['section'] ) {
return " $admin_body_class woocommerce_page_wc-subscriptions ";
}
return " $admin_body_class woocommerce_page_wc-marketplace ";
}
}