2013-08-06 13:04:24 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* Addons Page
2013-08-06 13:04:24 +00:00
*
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Admin
2015-11-20 15:27:52 +00:00
* @ version 2.5 . 0
2013-08-06 13:04:24 +00:00
*/
2020-02-01 06:18:47 +00:00
use Automattic\Jetpack\Constants ;
2014-09-20 19:52:30 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2015-12-03 13:38:13 +00:00
exit ;
2014-09-20 19:52:30 +00:00
}
2013-08-06 13:04:24 +00:00
/**
2015-11-03 12:28:01 +00:00
* WC_Admin_Addons Class .
2013-08-06 13:04:24 +00:00
*/
class WC_Admin_Addons {
2016-09-20 17:42:46 +00:00
/**
* Get featured for the addons screen
*
* @ return array of objects
*/
public static function get_featured () {
2019-07-04 03:06:02 +00:00
$featured = get_transient ( 'wc_addons_featured' );
if ( false === $featured ) {
2021-01-05 14:40:58 +00:00
$headers = array ();
2021-01-07 12:14:38 +00:00
$auth = WC_Helper_Options :: get ( 'auth' );
2021-01-05 14:40:58 +00:00
if ( ! empty ( $auth [ 'access_token' ] ) ) {
$headers [ 'Authorization' ] = 'Bearer ' . $auth [ 'access_token' ];
}
$raw_featured = wp_safe_remote_get (
'https://woocommerce.com/wp-json/wccom-extensions/1.0/featured' ,
array (
2021-01-07 12:14:38 +00:00
'headers' => $headers ,
2021-01-05 14:40:58 +00:00
'user-agent' => 'WooCommerce Addons Page' ,
)
);
2016-09-20 17:42:46 +00:00
if ( ! is_wp_error ( $raw_featured ) ) {
$featured = json_decode ( wp_remote_retrieve_body ( $raw_featured ) );
if ( $featured ) {
2021-01-05 14:45:34 +00:00
set_transient ( 'wc_addons_featured' , $featured , DAY_IN_SECONDS );
2016-09-20 17:42:46 +00:00
}
}
}
if ( is_object ( $featured ) ) {
self :: output_featured_sections ( $featured -> sections );
return $featured ;
}
}
2017-11-27 21:11:55 +00:00
/**
2017-11-29 18:03:22 +00:00
* Build url parameter string
2017-11-27 21:11:55 +00:00
*
2021-05-06 14:16:49 +00:00
* @ param string $category Addon ( sub ) category .
* @ param string $term Search terms .
* @ param string $country Store country .
2017-11-29 18:03:22 +00:00
*
* @ return string url parameter string
2017-11-27 21:11:55 +00:00
*/
public static function build_parameter_string ( $category , $term , $country ) {
2020-05-07 23:19:31 +00:00
$parameters = array (
2017-12-05 15:06:58 +00:00
'category' => $category ,
2018-03-05 18:59:17 +00:00
'term' => $term ,
'country' => $country ,
2017-11-27 21:11:55 +00:00
);
2020-05-07 23:19:31 +00:00
return '?' . http_build_query ( $parameters );
2017-11-27 21:11:55 +00:00
}
/**
* Call API to get extensions
*
2021-05-06 14:16:49 +00:00
* @ param string $category Addon ( sub ) category .
* @ param string $term Search terms .
* @ param string $country Store country .
2017-11-29 18:03:22 +00:00
*
2017-11-27 21:11:55 +00:00
* @ return array of extensions
*/
public static function get_extension_data ( $category , $term , $country ) {
2021-05-06 14:16:49 +00:00
$parameters = self :: build_parameter_string ( $category , $term , $country );
2021-01-05 14:40:58 +00:00
$headers = array ();
2021-01-07 12:14:38 +00:00
$auth = WC_Helper_Options :: get ( 'auth' );
2021-01-05 14:40:58 +00:00
if ( ! empty ( $auth [ 'access_token' ] ) ) {
$headers [ 'Authorization' ] = 'Bearer ' . $auth [ 'access_token' ];
}
2021-01-13 20:53:53 +00:00
$raw_extensions = wp_safe_remote_get (
2021-01-05 14:40:58 +00:00
'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' . $parameters ,
array ( 'headers' => $headers )
2017-12-06 17:02:09 +00:00
);
2021-01-05 14:40:58 +00:00
2017-12-06 17:02:09 +00:00
if ( ! is_wp_error ( $raw_extensions ) ) {
$addons = json_decode ( wp_remote_retrieve_body ( $raw_extensions ) ) -> products ;
2017-11-27 21:11:55 +00:00
}
return $addons ;
}
2015-11-19 12:27:34 +00:00
/**
2015-12-03 13:38:13 +00:00
* Get sections for the addons screen
2016-09-20 17:42:46 +00:00
*
2015-12-03 13:38:13 +00:00
* @ return array of objects
2015-11-19 12:27:34 +00:00
*/
2015-12-03 13:38:13 +00:00
public static function get_sections () {
2017-12-05 15:06:58 +00:00
$addon_sections = get_transient ( 'wc_addons_sections' );
2017-12-06 17:02:09 +00:00
if ( false === ( $addon_sections ) ) {
2017-12-05 15:06:58 +00:00
$raw_sections = wp_safe_remote_get (
'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories'
);
2015-12-03 13:38:13 +00:00
if ( ! is_wp_error ( $raw_sections ) ) {
2017-12-05 15:06:58 +00:00
$addon_sections = json_decode ( wp_remote_retrieve_body ( $raw_sections ) );
if ( $addon_sections ) {
set_transient ( 'wc_addons_sections' , $addon_sections , WEEK_IN_SECONDS );
2015-12-03 13:38:13 +00:00
}
}
2017-12-06 17:02:09 +00:00
}
return apply_filters ( 'woocommerce_addons_sections' , $addon_sections );
2015-12-03 13:38:13 +00:00
}
/**
2016-01-05 15:24:48 +00:00
* Get section for the addons screen .
*
2021-05-06 14:16:49 +00:00
* @ param string $section_id Required section ID .
2016-01-05 15:24:48 +00:00
*
2015-12-03 13:38:13 +00:00
* @ return object | bool
*/
public static function get_section ( $section_id ) {
$sections = self :: get_sections ();
2015-12-03 14:04:36 +00:00
if ( isset ( $sections [ $section_id ] ) ) {
return $sections [ $section_id ];
2015-12-03 13:38:13 +00:00
}
return false ;
}
/**
2016-01-05 15:24:48 +00:00
* Get section content for the addons screen .
*
2021-05-06 14:16:49 +00:00
* @ param string $section_id Required section ID .
2016-01-05 15:24:48 +00:00
*
2015-12-17 12:57:59 +00:00
* @ return array
2015-12-03 13:38:13 +00:00
*/
public static function get_section_data ( $section_id ) {
$section = self :: get_section ( $section_id );
$section_data = '' ;
if ( ! empty ( $section -> endpoint ) ) {
2019-07-04 03:06:02 +00:00
$section_data = get_transient ( 'wc_addons_section_' . $section_id );
if ( false === $section_data ) {
2015-12-03 13:38:13 +00:00
$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 ) );
2015-12-03 14:04:36 +00:00
if ( ! empty ( $section_data -> products ) ) {
2015-12-03 13:42:58 +00:00
set_transient ( 'wc_addons_section_' . $section_id , $section_data , WEEK_IN_SECONDS );
2015-12-03 13:38:13 +00:00
}
}
}
}
2015-12-03 14:04:36 +00:00
return apply_filters ( 'woocommerce_addons_section_data' , $section_data -> products , $section_id );
2015-12-03 13:38:13 +00:00
}
/**
* Handles the outputting of a contextually aware Storefront link ( points to child themes if Storefront is already active ) .
*/
public static function output_storefront_button () {
2015-12-17 12:57:59 +00:00
$template = get_option ( 'template' );
$stylesheet = get_option ( 'stylesheet' );
2015-12-03 13:38:13 +00:00
if ( 'storefront' === $template ) {
if ( 'storefront' === $stylesheet ) {
2016-07-05 22:52:16 +00:00
$url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/' ;
2015-12-03 13:38:13 +00:00
$text = __ ( 'Need a fresh look? Try Storefront child themes' , 'woocommerce' );
$utm_content = 'nostorefrontchildtheme' ;
} else {
2016-07-05 22:52:16 +00:00
$url = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/' ;
2015-12-03 13:38:13 +00:00
$text = __ ( 'View more Storefront child themes' , 'woocommerce' );
$utm_content = 'hasstorefrontchildtheme' ;
}
} else {
2016-07-05 22:52:16 +00:00
$url = 'https://woocommerce.com/storefront/' ;
2015-12-03 13:38:13 +00:00
$text = __ ( 'Need a theme? Try Storefront' , 'woocommerce' );
2015-11-19 14:44:04 +00:00
$utm_content = 'nostorefront' ;
2015-11-19 12:27:34 +00:00
}
2018-03-05 18:59:17 +00:00
$url = add_query_arg (
array (
'utm_source' => 'addons' ,
'utm_medium' => 'product' ,
'utm_campaign' => 'woocommerceplugin' ,
'utm_content' => $utm_content ,
2019-07-04 03:06:02 +00:00
),
$url
2018-03-05 18:59:17 +00:00
);
2015-11-19 14:44:04 +00:00
2015-11-19 12:35:20 +00:00
echo '<a href="' . esc_url ( $url ) . '" class="add-new-h2">' . esc_html ( $text ) . '</a>' . " \n " ;
2015-11-19 12:27:34 +00:00
}
2016-09-20 17:42:46 +00:00
/**
* Handles the outputting of a banner block .
*
2019-07-04 03:06:02 +00:00
* @ param object $block Banner data .
2016-09-20 17:42:46 +00:00
*/
public static function output_banner_block ( $block ) {
?>
< div class = " addons-banner-block " >
< h1 >< ? php echo esc_html ( $block -> title ); ?> </h1>
< p >< ? php echo esc_html ( $block -> description ); ?> </p>
< div class = " addons-banner-block-items " >
< ? php foreach ( $block -> items as $item ) : ?>
2017-11-29 17:41:08 +00:00
< ? php if ( self :: show_extension ( $item ) ) : ?>
< div class = " addons-banner-block-item " >
< div class = " addons-banner-block-item-icon " >
< img class = " addons-img " src = " <?php echo esc_url( $item->image ); ?> " />
</ div >
< div class = " addons-banner-block-item-content " >
< h3 >< ? php echo esc_html ( $item -> title ); ?> </h3>
< p >< ? php echo esc_html ( $item -> description ); ?> </p>
< ? php
2021-05-06 14:16:49 +00:00
self :: output_button (
$item -> href ,
$item -> button ,
'addons-button-solid' ,
$item -> plugin
);
2017-11-29 17:41:08 +00:00
?>
</ div >
2016-09-20 17:42:46 +00:00
</ div >
2017-11-29 17:41:08 +00:00
< ? php endif ; ?>
2016-09-20 17:42:46 +00:00
< ? php endforeach ; ?>
</ div >
</ div >
< ? php
}
/**
* Handles the outputting of a column .
*
2019-07-04 03:06:02 +00:00
* @ param object $block Column data .
2016-09-20 17:42:46 +00:00
*/
public static function output_column ( $block ) {
if ( isset ( $block -> container ) && 'column_container_start' === $block -> container ) {
?>
< div class = " addons-column-section " >
< ? php
}
if ( 'column_start' === $block -> module ) {
?>
< div class = " addons-column " >
< ? php
} else {
?>
</ div >
< ? php
}
if ( isset ( $block -> container ) && 'column_container_end' === $block -> container ) {
?>
</ div >
< ? php
}
}
/**
* Handles the outputting of a column block .
*
2019-07-04 03:06:02 +00:00
* @ param object $block Column block data .
2016-09-20 17:42:46 +00:00
*/
public static function output_column_block ( $block ) {
?>
< div class = " addons-column-block " >
< h1 >< ? php echo esc_html ( $block -> title ); ?> </h1>
< p >< ? php echo esc_html ( $block -> description ); ?> </p>
< ? php foreach ( $block -> items as $item ) : ?>
2017-11-29 17:41:08 +00:00
< ? php if ( self :: show_extension ( $item ) ) : ?>
< div class = " addons-column-block-item " >
< div class = " addons-column-block-item-icon " >
< img class = " addons-img " src = " <?php echo esc_url( $item->image ); ?> " />
</ div >
< div class = " addons-column-block-item-content " >
< h2 >< ? php echo esc_html ( $item -> title ); ?> </h2>
< ? php
2021-05-06 14:16:49 +00:00
self :: output_button (
$item -> href ,
$item -> button ,
'addons-button-solid' ,
$item -> plugin
);
2017-11-29 17:41:08 +00:00
?>
< p >< ? php echo esc_html ( $item -> description ); ?> </p>
</ div >
2016-09-20 17:42:46 +00:00
</ div >
2017-11-29 17:41:08 +00:00
< ? php endif ; ?>
2016-09-20 17:42:46 +00:00
< ? php endforeach ; ?>
</ div >
< ? php
}
/**
* Handles the outputting of a small light block .
*
2019-07-04 03:06:02 +00:00
* @ param object $block Block data .
2016-09-20 17:42:46 +00:00
*/
public static function output_small_light_block ( $block ) {
?>
< div class = " addons-small-light-block " >
2018-03-05 18:59:17 +00:00
< img class = " addons-img " src = " <?php echo esc_url( $block->image ); ?> " />
2016-09-20 17:42:46 +00:00
< div class = " addons-small-light-block-content " >
< h1 >< ? php echo esc_html ( $block -> title ); ?> </h1>
< p >< ? php echo esc_html ( $block -> description ); ?> </p>
< div class = " addons-small-light-block-buttons " >
< ? php foreach ( $block -> buttons as $button ) : ?>
< ? php
2021-05-06 14:16:49 +00:00
self :: output_button (
$button -> href ,
$button -> text ,
'addons-button-solid'
);
2016-09-20 17:42:46 +00:00
?>
< ? php endforeach ; ?>
</ div >
</ div >
</ div >
< ? php
}
/**
* Handles the outputting of a small dark block .
*
2019-07-04 03:06:02 +00:00
* @ param object $block Block data .
2016-09-20 17:42:46 +00:00
*/
public static function output_small_dark_block ( $block ) {
?>
< div class = " addons-small-dark-block " >
< h1 >< ? php echo esc_html ( $block -> title ); ?> </h1>
< p >< ? php echo esc_html ( $block -> description ); ?> </p>
< div class = " addons-small-dark-items " >
< ? php foreach ( $block -> items as $item ) : ?>
< div class = " addons-small-dark-item " >
2016-11-08 20:31:39 +00:00
< ? php if ( ! empty ( $item -> image ) ) : ?>
2016-10-04 15:47:38 +00:00
< div class = " addons-small-dark-item-icon " >
2016-10-04 16:15:52 +00:00
< img class = " addons-img " src = " <?php echo esc_url( $item->image ); ?> " />
2016-10-04 15:47:38 +00:00
</ div >
< ? php endif ; ?>
2016-09-20 17:42:46 +00:00
< ? php
2021-05-06 14:16:49 +00:00
self :: output_button (
$item -> href ,
$item -> button ,
'addons-button-outline-white'
);
2016-09-20 17:42:46 +00:00
?>
</ div >
< ? php endforeach ; ?>
</ div >
</ div >
< ? php
}
2017-05-30 22:33:13 +00:00
/**
* Handles the outputting of the WooCommerce Services banner block .
*
2019-07-04 03:06:02 +00:00
* @ param object $block Block data .
2017-05-30 22:33:13 +00:00
*/
2017-06-14 10:24:45 +00:00
public static function output_wcs_banner_block ( $block = array () ) {
2017-05-30 22:33:13 +00:00
$is_active = is_plugin_active ( 'woocommerce-services/woocommerce-services.php' );
2017-05-31 04:56:26 +00:00
$location = wc_get_base_location ();
2017-05-30 22:33:13 +00:00
if (
2021-06-10 18:07:24 +00:00
! in_array ( $location [ 'country' ], array ( 'US' ), true ) ||
2017-05-30 22:33:13 +00:00
$is_active ||
! current_user_can ( 'install_plugins' ) ||
! current_user_can ( 'activate_plugins' )
) {
return ;
}
2017-05-31 05:31:10 +00:00
$button_url = wp_nonce_url (
2018-03-05 18:59:17 +00:00
add_query_arg (
array (
'install-addon' => 'woocommerce-services' ,
)
),
2017-05-31 05:31:10 +00:00
'install-addon_woocommerce-services'
2017-05-30 22:33:13 +00:00
);
2017-05-31 04:59:27 +00:00
$defaults = array (
2021-06-10 17:29:32 +00:00
'image' => WC () -> plugin_url () . '/assets/images/wcs-extensions-banner-3x.jpg' ,
2020-11-09 12:22:20 +00:00
'image_alt' => __ ( 'WooCommerce Shipping' , 'woocommerce' ),
2021-06-10 17:29:32 +00:00
'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' ),
2017-05-31 04:59:27 +00:00
'button' => __ ( 'Free - Install now' , 'woocommerce' ),
'href' => $button_url ,
2017-06-14 10:24:45 +00:00
'logos' => array (),
2017-05-31 04:59:27 +00:00
);
2017-06-14 10:24:45 +00:00
switch ( $location [ 'country' ] ) {
case 'US' :
$local_defaults = array (
2018-03-05 18:59:17 +00:00
'logos' => array_merge (
2019-07-04 03:06:02 +00:00
$defaults [ 'logos' ],
array (
2018-03-05 18:59:17 +00:00
array (
'link' => WC () -> plugin_url () . '/assets/images/wcs-usps-logo.png' ,
'alt' => 'USPS logo' ,
),
2021-06-10 17:29:32 +00:00
array (
'link' => WC () -> plugin_url () . '/assets/images/wcs-dhlexpress-logo.png' ,
'alt' => 'DHL Express logo' ,
),
2018-03-05 18:59:17 +00:00
)
),
2017-06-14 10:24:45 +00:00
);
break ;
default :
$local_defaults = array ();
}
$block_data = array_merge ( $defaults , $local_defaults , $block );
2017-05-30 22:33:13 +00:00
?>
< div class = " addons-wcs-banner-block " >
2021-06-10 17:29:32 +00:00
< div class = " addons-wcs-banner-block-image is-full-image " >
2017-06-05 15:28:28 +00:00
< img
class = " addons-img "
src = " <?php echo esc_url( $block_data['image'] ); ?> "
alt = " <?php echo esc_attr( $block_data['image_alt'] ); ?> "
/>
</ div >
2017-05-30 22:33:13 +00:00
< div class = " addons-wcs-banner-block-content " >
2017-05-31 04:59:27 +00:00
< h1 >< ? php echo esc_html ( $block_data [ 'title' ] ); ?> </h1>
< p >< ? php echo esc_html ( $block_data [ 'description' ] ); ?> </p>
2021-06-10 17:29:32 +00:00
< ul class = " wcs-logos-container " >
2017-06-14 10:24:45 +00:00
< ? php foreach ( $block_data [ 'logos' ] as $logo ) : ?>
< li >
< img
2019-08-28 14:01:58 +00:00
alt = " <?php echo esc_attr( $logo['alt'] ); ?> "
2017-06-14 10:24:45 +00:00
class = " wcs-service-logo "
src = " <?php echo esc_url( $logo['link'] ); ?> "
>
</ li >
< ? php endforeach ; ?>
</ ul >
2017-05-30 22:33:13 +00:00
< ? php
2021-05-06 14:16:49 +00:00
self :: output_button (
$block_data [ 'href' ],
$block_data [ 'button' ],
'addons-button-outline-purple'
);
2017-05-30 22:33:13 +00:00
?>
</ div >
</ div >
2019-07-04 03:06:02 +00:00
< ? php
2017-05-30 22:33:13 +00:00
}
2020-11-09 12:19:11 +00:00
/**
* Handles the outputting of the WooCommerce Pay banner block .
*
* @ param object $block Block data .
*/
public static function output_wcpay_banner_block ( $block = array () ) {
$is_active = is_plugin_active ( 'woocommerce-payments/woocommerce-payments.php' );
$location = wc_get_base_location ();
if (
! in_array ( $location [ 'country' ], array ( 'US' ), true ) ||
$is_active ||
! current_user_can ( 'install_plugins' ) ||
! current_user_can ( 'activate_plugins' )
) {
return ;
}
$button_url = wp_nonce_url (
add_query_arg (
array (
'install-addon' => 'woocommerce-payments' ,
)
),
'install-addon_woocommerce-payments'
);
$defaults = array (
'image' => WC () -> plugin_url () . '/assets/images/wcpayments-icon-secure.png' ,
'image_alt' => __ ( 'WooCommerce Payments' , 'woocommerce' ),
2020-11-10 17:26:44 +00:00
'title' => __ ( 'Payments made simple, with no monthly fees — exclusively for WooCommerce stores.' , 'woocommerce' ),
2020-11-09 13:21:38 +00:00
'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' ),
2020-11-10 14:48:11 +00:00
'button' => __ ( 'Free - Install now' , 'woocommerce' ),
2020-11-09 12:19:11 +00:00
'href' => $button_url ,
'logos' => array (),
);
$block_data = array_merge ( $defaults , $block );
?>
< div class = " addons-wcs-banner-block " >
< div class = " addons-wcs-banner-block-image " >
< img
class = " addons-img "
src = " <?php echo esc_url( $block_data['image'] ); ?> "
alt = " <?php echo esc_attr( $block_data['image_alt'] ); ?> "
/>
</ div >
< div class = " addons-wcs-banner-block-content " >
< h1 >< ? php echo esc_html ( $block_data [ 'title' ] ); ?> </h1>
< p >< ? php echo esc_html ( $block_data [ 'description' ] ); ?> </p>
< ? php
2021-05-06 14:16:49 +00:00
self :: output_button (
$block_data [ 'href' ],
$block_data [ 'button' ],
'addons-button-outline-purple'
);
2020-11-09 12:19:11 +00:00
?>
</ div >
</ div >
< ? php
}
2021-05-06 14:16:49 +00:00
/**
* Handles the output of a full - width block .
*
2021-05-06 14:40:02 +00:00
* @ param array $section Section data .
2021-05-06 14:16:49 +00:00
*/
2021-05-06 21:12:07 +00:00
public static function output_promotion_block ( $section ) {
2021-05-06 11:55:30 +00:00
if (
! current_user_can ( 'install_plugins' ) ||
! current_user_can ( 'activate_plugins' )
) {
return ;
}
$section_object = ( object ) $section ;
2021-05-12 16:48:37 +00:00
if ( ! empty ( $section_object -> geowhitelist ) ) {
$section_object -> geowhitelist = explode ( ',' , $section_object -> geowhitelist );
2021-05-06 11:55:30 +00:00
}
2021-05-12 16:54:20 +00:00
if ( ! empty ( $section_object -> geoblacklist ) ) {
$section_object -> geoblacklist = explode ( ',' , $section_object -> geoblacklist );
}
2021-05-06 11:55:30 +00:00
if ( ! self :: show_extension ( $section_object ) ) {
return ;
}
?>
2021-05-06 21:12:07 +00:00
< div class = " addons-banner-block addons-promotion-block " >
2021-05-06 11:55:30 +00:00
< img
class = " addons-img "
src = " <?php echo esc_url( $section['image'] ); ?> "
alt = " <?php echo esc_attr( $section['image_alt'] ); ?> "
/>
2021-05-06 21:12:07 +00:00
< div class = " addons-promotion-block-content " >
< h1 class = " addons-promotion-block-title " >< ? php echo esc_html ( $section [ 'title' ] ); ?> </h1>
< div class = " addons-promotion-block-description " >
2021-05-06 11:55:30 +00:00
< ? php echo wp_kses_post ( $section [ 'description' ] ); ?>
</ div >
2021-05-06 21:12:07 +00:00
< div class = " addons-promotion-block-buttons " >
2021-05-06 11:55:30 +00:00
< ? php
if ( $section [ 'button_1' ] ) {
self :: output_button (
$section [ 'button_1_href' ],
$section [ 'button_1' ],
'addons-button-expandable addons-button-solid' ,
$section [ 'plugin' ]
);
}
if ( $section [ 'button_2' ] ) {
self :: output_button (
$section [ 'button_2_href' ],
$section [ 'button_2' ],
'addons-button-expandable addons-button-outline-purple' ,
$section [ 'plugin' ]
);
}
?>
</ div >
</ div >
</ div >
< ? php
}
2016-09-20 17:42:46 +00:00
/**
* Handles the outputting of featured sections
*
2019-07-04 03:06:02 +00:00
* @ param array $sections Section data .
2016-09-20 17:42:46 +00:00
*/
public static function output_featured_sections ( $sections ) {
foreach ( $sections as $section ) {
switch ( $section -> 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 ;
2017-05-30 22:34:05 +00:00
case 'wcs_banner_block' :
2017-06-14 10:24:45 +00:00
self :: output_wcs_banner_block ( ( array ) $section );
2017-05-30 22:34:05 +00:00
break ;
2020-11-09 12:19:11 +00:00
case 'wcpay_banner_block' :
self :: output_wcpay_banner_block ( ( array ) $section );
2020-11-10 17:36:34 +00:00
break ;
2021-05-06 21:12:07 +00:00
case 'promotion_block' :
self :: output_promotion_block ( ( array ) $section );
2021-05-06 11:55:30 +00:00
break ;
2016-09-20 17:42:46 +00:00
}
}
}
2019-07-04 07:33:38 +00:00
/**
* Returns in - app - purchase URL params .
*/
public static function get_in_app_purchase_url_params () {
2019-07-15 06:07:57 +00:00
// 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 () );
2019-07-04 07:33:38 +00:00
return array (
2019-08-17 15:37:02 +00:00
'wccom-site' => site_url (),
2019-11-22 12:02:58 +00:00
'wccom-back' => rawurlencode ( $back_admin_path ),
2020-02-01 06:18:47 +00:00
'wccom-woo-version' => Constants :: get_constant ( 'WC_VERSION' ),
2019-08-17 15:37:02 +00:00
'wccom-connect-nonce' => wp_create_nonce ( 'connect' ),
2019-07-04 07:33:38 +00:00
);
}
2019-07-04 03:53:27 +00:00
/**
* 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 .
*
2021-05-06 14:16:49 +00:00
* @ param string $url Destination URL .
2019-07-04 03:53:27 +00:00
*/
public static function add_in_app_purchase_url_params ( $url ) {
return add_query_arg (
2019-07-04 07:33:38 +00:00
self :: get_in_app_purchase_url_params (),
2019-07-04 03:53:27 +00:00
$url
);
}
2016-09-20 17:42:46 +00:00
/**
* Outputs a button .
*
2019-07-04 03:06:02 +00:00
* @ param string $url Destination URL .
* @ param string $text Button label text .
2019-07-14 20:50:08 +00:00
* @ param string $style Button style class .
2019-07-04 03:06:02 +00:00
* @ param string $plugin The plugin the button is promoting .
2016-09-20 17:42:46 +00:00
*/
2019-07-14 20:50:08 +00:00
public static function output_button ( $url , $text , $style , $plugin = '' ) {
2020-11-10 17:26:44 +00:00
$style = __ ( 'Free' , 'woocommerce' ) === $text ? 'addons-button-outline-purple' : $style ;
2019-07-14 20:50:08 +00:00
$style = is_plugin_active ( $plugin ) ? 'addons-button-installed' : $style ;
2018-03-05 18:59:17 +00:00
$text = is_plugin_active ( $plugin ) ? __ ( 'Installed' , 'woocommerce' ) : $text ;
2019-07-04 03:53:27 +00:00
$url = self :: add_in_app_purchase_url_params ( $url );
2016-09-20 17:42:46 +00:00
?>
< a
2019-07-14 20:50:08 +00:00
class = " addons-button <?php echo esc_attr( $style ); ?> "
2016-09-20 17:42:46 +00:00
href = " <?php echo esc_url( $url ); ?> " >
< ? php echo esc_html ( $text ); ?>
</ a >
< ? php
}
2013-08-06 13:04:24 +00:00
/**
2015-12-03 13:38:13 +00:00
* Handles output of the addons page in admin .
2013-08-06 13:04:24 +00:00
*/
2014-06-04 10:16:19 +00:00
public static function output () {
2019-07-04 03:06:02 +00:00
$section = isset ( $_GET [ 'section' ] ) ? sanitize_text_field ( wp_unslash ( $_GET [ 'section' ] ) ) : '_featured' ;
2019-12-20 17:21:08 +00:00
$search = isset ( $_GET [ 'search' ] ) ? sanitize_text_field ( wp_unslash ( $_GET [ 'search' ] ) ) : '' ;
2019-07-04 03:06:02 +00:00
2017-05-30 16:46:26 +00:00
if ( isset ( $_GET [ 'section' ] ) && 'helper' === $_GET [ 'section' ] ) {
do_action ( 'woocommerce_helper_output' );
return ;
}
2020-11-09 12:48:49 +00:00
if ( isset ( $_GET [ 'install-addon' ] ) ) {
switch ( $_GET [ 'install-addon' ] ) {
case 'woocommerce-services' :
self :: install_woocommerce_services_addon ();
2020-11-10 14:48:11 +00:00
break ;
2020-11-09 12:48:49 +00:00
case 'woocommerce-payments' :
2021-02-23 03:25:40 +00:00
self :: install_woocommerce_payments_addon ( $section );
2020-11-10 14:48:11 +00:00
break ;
2020-11-09 12:48:49 +00:00
default :
// Do nothing.
2020-11-10 14:48:11 +00:00
break ;
2020-11-09 12:48:49 +00:00
}
2017-05-31 05:31:10 +00:00
}
2015-12-03 13:38:13 +00:00
$sections = self :: get_sections ();
$theme = wp_get_theme ();
2019-07-04 03:06:02 +00:00
$current_section = isset ( $_GET [ 'section' ] ) ? $section : '_featured' ;
2018-03-05 18:59:17 +00:00
$addons = array ();
2017-11-27 21:11:55 +00:00
2017-12-05 15:06:58 +00:00
if ( '_featured' !== $current_section ) {
2019-07-04 03:06:02 +00:00
$category = $section ? $section : null ;
$term = $search ? $search : null ;
2018-03-05 18:59:17 +00:00
$country = WC () -> countries -> get_base_country ();
$addons = self :: get_extension_data ( $category , $term , $country );
2017-11-27 21:11:55 +00:00
}
2017-08-23 13:38:00 +00:00
/**
* Addon page view .
*
2017-11-27 21:11:55 +00:00
* @ uses $addons
2017-08-23 13:38:00 +00:00
* @ uses $sections
* @ uses $theme
* @ uses $current_section
*/
2018-03-05 18:59:17 +00:00
include_once dirname ( __FILE__ ) . '/views/html-admin-page-addons.php' ;
2013-08-06 13:04:24 +00:00
}
2017-05-31 05:31:10 +00:00
/**
* 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 ;
}
2017-11-29 17:41:08 +00:00
2020-11-09 12:48:49 +00:00
/**
* Install WooCommerce Payments from the Extensions screens .
*
2021-02-23 03:25:40 +00:00
* @ param string $section Optional . Extenstions tab .
*
2020-11-09 12:48:49 +00:00
* @ return void
*/
2021-02-23 03:25:40 +00:00
public static function install_woocommerce_payments_addon ( $section = '_featured' ) {
2020-11-09 12:48:49 +00:00
check_admin_referer ( 'install-addon_woocommerce-payments' );
2020-11-10 16:28:47 +00:00
$wcpay_plugin_id = 'woocommerce-payments' ;
$wcpay_plugin = array (
2020-11-09 12:48:49 +00:00
'name' => __ ( 'WooCommerce Payments' , 'woocommerce' ),
'repo-slug' => 'woocommerce-payments' ,
);
2021-02-08 08:29:44 +00:00
WC_Install :: background_installer ( $wcpay_plugin_id , $wcpay_plugin );
2021-02-23 03:25:40 +00:00
do_action ( 'woocommerce_addon_installed' , $wcpay_plugin_id , $section );
2020-11-09 12:48:49 +00:00
wp_safe_redirect ( remove_query_arg ( array ( 'install-addon' , '_wpnonce' ) ) );
exit ;
}
2017-11-29 17:41:08 +00:00
/**
* Should an extension be shown on the featured page .
*
2019-07-04 03:06:02 +00:00
* @ param object $item Item data .
2017-11-29 17:41:08 +00:00
* @ return boolean
*/
public static function show_extension ( $item ) {
$location = WC () -> countries -> get_base_country ();
2021-05-12 16:48:37 +00:00
if ( isset ( $item -> geowhitelist ) && ! in_array ( $location , $item -> geowhitelist , true ) ) {
2017-12-07 16:47:33 +00:00
return false ;
2017-11-29 17:41:08 +00:00
}
2021-05-12 16:48:37 +00:00
if ( isset ( $item -> geoblacklist ) && in_array ( $location , $item -> geoblacklist , true ) ) {
2017-12-07 16:47:33 +00:00
return false ;
2017-11-29 17:41:08 +00:00
}
if ( is_plugin_active ( $item -> plugin ) ) {
2017-12-07 16:47:33 +00:00
return false ;
2017-11-29 17:41:08 +00:00
}
2017-12-07 16:47:33 +00:00
return true ;
2017-11-29 17:41:08 +00:00
}
2014-09-20 19:52:30 +00:00
}