close_http_connection();
foreach ( $this->deferred_actions as $action ) {
call_user_func_array( $action['func'], $action['args'] );
}
}
/**
* Helper method to queue the background install of a plugin.
*
* @param string $plugin_id Plugin id used for background install.
* @param array $plugin_info Plugin info array containing at least main file and repo slug.
*/
protected function install_plugin( $plugin_id, $plugin_info ) {
if ( ! empty( $plugin_info['file'] ) && is_plugin_active( $plugin_info['file'] ) ) {
return;
}
if ( empty( $this->deferred_actions ) ) {
add_action( 'shutdown', array( $this, 'run_deferred_actions' ) );
}
array_push( $this->deferred_actions, array(
'func' => array( 'WC_Install', 'background_installer' ),
'args' => array( $plugin_id, $plugin_info )
) );
}
/**
* Helper method to queue the background install of a theme.
*
* @param string $theme_id Theme id used for background install.
*/
protected function install_theme( $theme_id ) {
if ( empty( $this->deferred_actions ) ) {
add_action( 'shutdown', array( $this, 'run_deferred_actions' ) );
}
array_push( $this->deferred_actions, array(
'func' => array( 'WC_Install', 'theme_background_installer' ),
'args' => array( $theme_id )
) );
}
/**
* Helper method to install Jetpack.
*/
protected function install_jetpack() {
$this->install_plugin( 'jetpack', array(
'file' => 'jetpack/jetpack.php',
'name' => __( 'Jetpack', 'woocommerce' ),
'repo-slug' => 'jetpack',
) );
update_option( 'woocommerce_setup_queued_jetpack_install', true );
}
/**
* Helper method to install WooCommerce Services and its Jetpack dependency.
*/
protected function install_woocommerce_services() {
$this->install_jetpack();
$this->install_plugin( 'woocommerce-services', array(
'file' => 'woocommerce-services/woocommerce-services.php',
'name' => __( 'WooCommerce Services', 'woocommerce' ),
'repo-slug' => 'woocommerce-services',
) );
}
/**
* Get the WCS shipping carrier for a given country code.
*
* Can also be used to determine if WCS supports a given country.
*
* @param $country_code
* @return bool|string Carrier name if supported, boolean False otherwise.
*/
protected function get_wcs_shipping_carrier( $country_code ) {
switch ( $country_code ) {
case 'US':
return 'USPS';
case 'CA':
return 'Canada Post';
default:
return false;
}
}
/**
* Get shipping methods based on country code.
*
* @param $country_code
* @return array
*/
protected function get_wizard_shipping_methods( $country_code ) {
$shipping_methods = array(
'live_rates' => array(
'name' => __( 'Live Rates', 'woocommerce' ),
'description' => __( 'Shipping rates updated in realtime. Powered by Jetpack.', 'woocommerce' ),
),
'flat_rate' => array(
'name' => __( 'Flat Rate', 'woocommerce' ),
'description' => __( 'Set a fixed price to cover shipping costs.', 'woocommerce' ),
'settings' => array(
'cost' => array(
'type' => 'text',
'description' => __( 'What would you like to charge for flat rate shipping?', 'woocommerce' ),
),
),
),
'free_shipping' => array(
'name' => __( 'Free Shipping', 'woocommerce' ),
'description' => __( "Don't charge for shipping.", 'woocommerce' ),
),
);
$live_rate_carrier = $this->get_wcs_shipping_carrier( $country_code );
if ( false === $live_rate_carrier || ! current_user_can('install_plugins') ) {
unset( $shipping_methods['live_rates'] );
}
return $shipping_methods;
}
/**
* Render the available shipping methods for a given country code.
*
* @param string $country_code
* @param string $input_prefix
*/
protected function shipping_method_selection_form( $country_code, $input_prefix ) {
$live_rate_carrier = $this->get_wcs_shipping_carrier( $country_code );
$selected = $live_rate_carrier ? 'live_rates' : 'flat_rate';
$shipping_methods = $this->get_wizard_shipping_methods( $country_code );
?>
$method ) : ?>
$method ) : ?>
$setting ) : ?>
countries->get_base_country();
$country_name = WC()->countries->countries[ $country_code ];
$wcs_carrier = $this->get_wcs_shipping_carrier( $country_code );
// TODO: determine how to handle existing shipping zones (or choose not to)
if ( false === $dimension_unit || false === $weight_unit ) {
if ( 'US' === $country_code ) {
$dimension_unit = 'in';
$weight_unit = 'oz';
} else {
$dimension_unit = 'cm';
$weight_unit = 'kg';
}
}
if ( $wcs_carrier ) {
$intro_text = sprintf(
/* translators: %1$s: country name, %2$s: shipping carrier name */
__( "You're all set up to ship anywhere in %1\$s, and outside of it. We recommend using live rates to get accurate %2\$s shipping prices to cover the cost of order fulfillment.", 'woocommerce' ),
$country_name,
$wcs_carrier
);
} else {
$intro_text = sprintf(
/* translators: %s: country name */
__( "You can choose which countries you'll be shipping to and with which methods. We've started you up with shipping to %s and the rest of the world.", 'woocommerce' ),
$country_name
);
}
?>
get_next_step_link() ) );
exit;
}
// Install WooCommerce Services if live rates were selected.
if (
( $setup_domestic && 'live_rates' === $domestic_method ) ||
( $setup_intl && 'live_rates' === $intl_method )
) {
$this->install_woocommerce_services();
}
/*
* If enabled, create a shipping zone containing the country the
* store is located in, with the selected method preconfigured.
*/
if ( $setup_domestic ) {
$country = WC()->countries->get_base_country();
$zone = new WC_Shipping_Zone( null );
$zone->set_zone_order( 0 );
$zone->add_location( $country, 'country' );
if ( 'live_rates' === $domestic_method ) {
// Signal WooCommerce Services to setup the domestic zone.
update_option( 'woocommerce_setup_domestic_live_rates_zone', true, 'no' );
} else {
$instance_id = $zone->add_shipping_method( $domestic_method );
}
$zone->save();
// Save chosen shipping method settings (using REST controller for convenience).
if ( isset( $instance_id ) && ! empty( $_POST['shipping_zones']['domestic'][ $domestic_method ] ) ) {
$method_controller = new WC_REST_Shipping_Zone_Methods_Controller();
$method_controller->update_item( array(
'zone_id' => $zone->get_id(),
'instance_id' => $instance_id,
'settings' => $_POST['shipping_zones']['domestic'][ $domestic_method ],
) );
}
}
// If enabled, set the selected method for the "rest of world" zone.
if ( $setup_intl ) {
if ( 'live_rates' === $intl_method ) {
// Signal WooCommerce Services to setup the international zone.
update_option( 'woocommerce_setup_intl_live_rates_zone', true, 'no' );
} else {
$zone = new WC_Shipping_Zone( 0 );
$instance_id = $zone->add_shipping_method( $intl_method );
$zone->save();
}
// Save chosen shipping method settings (using REST controller for convenience).
if ( isset( $instance_id ) && ! empty( $_POST['shipping_zones']['intl'][ $intl_method ] ) ) {
$method_controller = new WC_REST_Shipping_Zone_Methods_Controller();
$method_controller->update_item( array(
'zone_id' => $zone->get_id(),
'instance_id' => $instance_id,
'settings' => $_POST['shipping_zones']['intl'][ $intl_method ],
) );
}
}
// Notify the user that no shipping methods are configured.
if ( ! $setup_domestic && ! $setup_intl ) {
WC_Admin_Notices::add_notice( 'no_shipping_methods' );
}
wp_redirect( esc_url_raw( $this->get_next_step_link() ) );
exit;
}
/**
* https://stripe.com/global
*/
protected function is_stripe_supported_country( $country_code ) {
$stripe_supported_countries = array(
'AU',
'AT',
'BE',
'CA',
'DK',
'FI',
'FR',
'DE',
'HK',
'IE',
'JP',
'LU',
'NL',
'NZ',
'NO',
'SG',
'ES',
'SE',
'CH',
'GB',
'US',
);
return in_array( $country_code, $stripe_supported_countries );
}
/**
* Helper method to retrieve the current user's email address.
*
* @return string Email address
*/
protected function get_current_user_email() {
$current_user = wp_get_current_user();
$user_email = $current_user->user_email;
return $user_email;
}
/**
* Simple array of "in cart" gateways to show in wizard.
* @return array
*/
protected function get_wizard_in_cart_payment_gateways() {
$country = WC()->countries->get_base_country();
$user_email = $this->get_current_user_email();
$stripe_description = '
' . sprintf(
__( 'Accept all major debit and credit cards from customers in 135+ countries on your site. Learn more.', 'woocommerce' ),
'https://wordpress.org/plugins/woocommerce-gateway-stripe/'
) . '
';
$paypal_bt_description = '
' . sprintf(
__( 'Safe and secure payments using credit cards or your customer\'s PayPal account. Learn more.', 'woocommerce' ),
'https://wordpress.org/plugins/woocommerce-gateway-paypal-powered-by-braintree/'
) . '
';
$paypal_ec_description = '
' . sprintf(
__( 'Safe and secure payments using credit cards or your customer\'s PayPal account. Learn more.', 'woocommerce' ),
'https://wordpress.org/plugins/woocommerce-gateway-paypal-express-checkout/'
) . '