' . wp_kses_post( $notice['description'] ) . '
';
}
}
/**
* Enqueue scripts for woo subscription notice.
*
* @return void
*/
public static function maybe_enqueue_scripts_for_subscription_notice() {
if ( 'woocommerce_page_wc-settings' !== get_current_screen()->id ) {
return;
}
WCAdminAssets::register_script( 'wp-admin-scripts', 'woo-subscriptions-notice' );
wp_enqueue_script( 'woo-subscriptions-notice' );
}
/**
* Construct the subscription notice data based on user subscriptions data.
*
* @param array $all_subs all subscription data.
* @param array $subs_to_show filtered subscriptions as condition.
* @param int $total total subscription count.
* @param array $messages message.
* @param string $type type of notice, whether it is for expiring or expired subscription.
* @return array notice data to return. Contains type, parsed_message and product_id.
*/
public static function get_subscriptions_notice_data( array $all_subs, array $subs_to_show, int $total, array $messages, string $type ) {
$utm_campaign = 'expired' === $type ?
'pu_settings_screen_renew' :
( 'missing' === $type ? 'pu_settings_screen_purchase' : 'pu_settings_screen_enable_autorenew' );
if ( 1 < $total ) {
$hyperlink_url = add_query_arg(
array(
'utm_source' => 'pu',
'utm_campaign' => $utm_campaign,
),
self::WOO_SUBSCRIPTION_PAGE_URL
);
$parsed_message = sprintf(
$messages['different_subscriptions'],
esc_attr( $total ),
esc_url( $hyperlink_url ),
esc_attr( $total ),
);
// All product ids.
$product_ids = array_map(
function ( $sub ) {
return $sub['product_id'];
},
$subs_to_show
);
return array(
'type' => 'different_subscriptions',
'parsed_message' => $parsed_message,
'product_ids' => $product_ids,
);
}
$subscription = reset( $subs_to_show );
$product_id = $subscription['product_id'];
// check if $all_subs has multiple subs for this product.
$has_multiple_subs_for_product = 1 < count(
array_filter(
$all_subs,
function ( $sub ) use ( $product_id ) {
return $product_id === $sub['product_id'];
}
)
);
$message_key = $has_multiple_subs_for_product ? 'multiple_manage' : 'single_manage';
$renew_string = __( 'Renew', 'woocommerce' );
$subscribe_string = __( 'Subscribe', 'woocommerce' );
if ( isset( $subscription['product_regular_price'] ) ) {
/* translators: 1: Product price */
$renew_string = sprintf( __( 'Renew for %1$s', 'woocommerce' ), $subscription['product_regular_price'] );
}
$expiry_date = date_i18n( 'F jS', $subscription['expires'] );
$hyperlink_url = add_query_arg(
array(
'product_id' => $product_id,
'type' => $type,
'utm_source' => 'pu',
'utm_campaign' => $utm_campaign,
),
self::WOO_SUBSCRIPTION_PAGE_URL
);
// Construct message based on template for multiple_manage or single_manage, parameter used:
// 1. Product name
// 2. Expiry date
// 3. URL to My Subscriptions page with extra params
// 4. Renew string.
if ( isset( $messages[ $message_key ] ) ) {
$parsed_message = sprintf(
$messages[ $message_key ],
esc_attr( $subscription['product_name'] ),
esc_attr( $expiry_date ),
esc_url( $hyperlink_url ),
// Show subscribe for missing subscriptions, renew otherwise.
'missing' === $type ? esc_attr( $subscribe_string ) : esc_attr( $renew_string ),
);
return array(
'type' => $message_key,
'parsed_message' => $parsed_message,
'product_id' => $product_id,
);
}
return array(
'type' => 'invalid',
'parsed_message' => '',
'product_id' => '',
);
}
/**
* Get formatted notice information for expiring subscription.
*
* @param boolean $allowed_link whether the notice description should include a link.
* @return array notice information.
*/
public static function get_expiring_subscription_notice( $allowed_link = true ) {
if ( ! WC_Helper::is_site_connected() ) {
return array();
}
if ( self::$subscription_usage_notices_already_shown ) {
return array();
}
if ( ! self::should_show_notice( self::DISMISS_EXPIRING_SUBS_NOTICE ) ) {
return array();
}
$subscriptions = WC_Helper::get_subscription_list_data();
$expiring_subscriptions = array_filter(
$subscriptions,
function ( $sub ) {
return ( ! empty( $sub['local']['installed'] ) && ! empty( $sub['product_key'] ) )
&& ( $sub['active'] || empty( $sub['connections'] ) ) // Active on current site or not connected to any sites.
&& $sub['expiring']
&& ! $sub['autorenew'];
},
);
if ( ! $expiring_subscriptions ) {
return array();
}
$total_expiring_subscriptions = count( $expiring_subscriptions );
// Don't show missing notice if there are expiring subscriptions.
self::$subscription_usage_notices_already_shown = true;
// When payment method is missing on WooCommerce.com.
$helper_notices = WC_Helper::get_notices();
if ( ! empty( $helper_notices['missing_payment_method_notice'] ) ) {
return self::get_missing_payment_method_notice( $allowed_link, $total_expiring_subscriptions );
}
// Payment method is available but there are expiring subscriptions.
$notice_data = self::get_subscriptions_notice_data(
$subscriptions,
$expiring_subscriptions,
$total_expiring_subscriptions,
array(
/* translators: 1) product name 2) expiry date 3) URL to My Subscriptions page */
'single_manage' => __( 'Your subscription for