Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Admin
File: Homescreen.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WooCommerce Homescreen.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
namespace Automattic\WooCommerce\Internal\Admin;
[5] Fix | Delete
[6] Fix | Delete
use Automattic\WooCommerce\Admin\Features\Features;
[7] Fix | Delete
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\Shipping;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Contains backend logic for the homescreen feature.
[11] Fix | Delete
*/
[12] Fix | Delete
class Homescreen {
[13] Fix | Delete
/**
[14] Fix | Delete
* Menu slug.
[15] Fix | Delete
*/
[16] Fix | Delete
const MENU_SLUG = 'wc-admin';
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Class instance.
[20] Fix | Delete
*
[21] Fix | Delete
* @var Homescreen instance
[22] Fix | Delete
*/
[23] Fix | Delete
protected static $instance = null;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Get class instance.
[27] Fix | Delete
*/
[28] Fix | Delete
public static function get_instance() {
[29] Fix | Delete
if ( ! self::$instance ) {
[30] Fix | Delete
self::$instance = new self();
[31] Fix | Delete
}
[32] Fix | Delete
return self::$instance;
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Hook into WooCommerce.
[37] Fix | Delete
*/
[38] Fix | Delete
public function __construct() {
[39] Fix | Delete
add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
[40] Fix | Delete
add_action( 'admin_menu', array( $this, 'register_page' ) );
[41] Fix | Delete
// In WC Core 5.1 $submenu manipulation occurs in admin_menu, not admin_head. See https://github.com/woocommerce/woocommerce/pull/29088.
[42] Fix | Delete
if ( version_compare( WC_VERSION, '5.1', '>=' ) ) {
[43] Fix | Delete
// priority is 20 to run after admin_menu hook for woocommerce runs, so that submenu is populated.
[44] Fix | Delete
add_action( 'admin_menu', array( $this, 'possibly_remove_woocommerce_menu' ) );
[45] Fix | Delete
add_action( 'admin_menu', array( $this, 'update_link_structure' ), 20 );
[46] Fix | Delete
} else {
[47] Fix | Delete
// priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165.
[48] Fix | Delete
add_action( 'admin_head', array( $this, 'update_link_structure' ), 20 );
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
add_filter( 'woocommerce_admin_preload_options', array( $this, 'preload_options' ) );
[52] Fix | Delete
[53] Fix | Delete
if ( Features::is_enabled( 'shipping-smart-defaults' ) ) {
[54] Fix | Delete
add_filter(
[55] Fix | Delete
'woocommerce_admin_shared_settings',
[56] Fix | Delete
array( $this, 'maybe_set_default_shipping_options_on_home' ),
[57] Fix | Delete
9999
[58] Fix | Delete
);
[59] Fix | Delete
}
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Set free shipping in the same country as the store default
[64] Fix | Delete
* Flag rate in all other countries when any of the following conditions are true
[65] Fix | Delete
*
[66] Fix | Delete
* - The store sells physical products, has JP and WCS installed and connected, and is located in the US.
[67] Fix | Delete
* - The store sells physical products, and is not located in US/Canada/Australia/UK (irrelevant if JP is installed or not).
[68] Fix | Delete
* - The store sells physical products and is located in US, but JP and WCS are not installed.
[69] Fix | Delete
*
[70] Fix | Delete
* @param array $settings shared admin settings.
[71] Fix | Delete
* @return array
[72] Fix | Delete
*/
[73] Fix | Delete
public function maybe_set_default_shipping_options_on_home( $settings ) {
[74] Fix | Delete
if ( ! function_exists( 'get_current_screen' ) ) {
[75] Fix | Delete
return $settings;
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
$current_screen = get_current_screen();
[79] Fix | Delete
[80] Fix | Delete
// Abort if it's not the homescreen.
[81] Fix | Delete
if ( ! isset( $current_screen->id ) || 'woocommerce_page_wc-admin' !== $current_screen->id ) {
[82] Fix | Delete
return $settings;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
// Abort if we already created the shipping options.
[86] Fix | Delete
$already_created = get_option( 'woocommerce_admin_created_default_shipping_zones' );
[87] Fix | Delete
if ( $already_created === 'yes' ) {
[88] Fix | Delete
return $settings;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
$zone_count = count( \WC_Data_Store::load( 'shipping-zone' )->get_zones() );
[92] Fix | Delete
if ( $zone_count ) {
[93] Fix | Delete
update_option( 'woocommerce_admin_created_default_shipping_zones', 'yes' );
[94] Fix | Delete
update_option( 'woocommerce_admin_reviewed_default_shipping_zones', 'yes' );
[95] Fix | Delete
return $settings;
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
$user_skipped_obw = $settings['onboarding']['profile']['skipped'] ?? false;
[99] Fix | Delete
$store_address = $settings['preloadSettings']['general']['woocommerce_store_address'] ?? '';
[100] Fix | Delete
$product_types = $settings['onboarding']['profile']['product_types'] ?? array();
[101] Fix | Delete
$user_has_set_store_country = $settings['onboarding']['profile']['is_store_country_set'] ?? false;
[102] Fix | Delete
[103] Fix | Delete
// Do not proceed if user has not filled out their country in the onboarding profiler.
[104] Fix | Delete
if ( ! $user_has_set_store_country ) {
[105] Fix | Delete
return $settings;
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
// If user skipped the obw or has not completed the store_details
[109] Fix | Delete
// then we assume the user is going to sell physical products.
[110] Fix | Delete
if ( $user_skipped_obw || '' === $store_address ) {
[111] Fix | Delete
$product_types[] = 'physical';
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
if ( false === in_array( 'physical', $product_types, true ) ) {
[115] Fix | Delete
return $settings;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
$country_code = wc_format_country_state_string( $settings['preloadSettings']['general']['woocommerce_default_country'] )['country'];
[119] Fix | Delete
$country_name = WC()->countries->get_countries()[ $country_code ] ?? null;
[120] Fix | Delete
[121] Fix | Delete
$is_jetpack_installed = in_array( 'jetpack', $settings['plugins']['installedPlugins'] ?? array(), true );
[122] Fix | Delete
$is_wcs_installed = in_array( 'woocommerce-services', $settings['plugins']['installedPlugins'] ?? array(), true );
[123] Fix | Delete
[124] Fix | Delete
if (
[125] Fix | Delete
( 'US' === $country_code && $is_jetpack_installed )
[126] Fix | Delete
||
[127] Fix | Delete
( ! in_array( $country_code, array( 'CA', 'AU', 'NZ', 'SG', 'HK', 'GB', 'ES', 'IT', 'DE', 'FR', 'CL', 'AR', 'PE', 'BR', 'UY', 'GT', 'NL', 'AT', 'BE' ), true ) )
[128] Fix | Delete
||
[129] Fix | Delete
( 'US' === $country_code && false === $is_jetpack_installed && false === $is_wcs_installed )
[130] Fix | Delete
) {
[131] Fix | Delete
$zone = new \WC_Shipping_Zone();
[132] Fix | Delete
$zone->set_zone_name( $country_name );
[133] Fix | Delete
$zone->add_location( $country_code, 'country' );
[134] Fix | Delete
[135] Fix | Delete
// Method creation has no default title, use the REST API to add a title.
[136] Fix | Delete
$instance_id = $zone->add_shipping_method( 'free_shipping' );
[137] Fix | Delete
$request = new \WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
[138] Fix | Delete
$request->set_body_params(
[139] Fix | Delete
array(
[140] Fix | Delete
'settings' => array(
[141] Fix | Delete
'title' => 'Free shipping',
[142] Fix | Delete
),
[143] Fix | Delete
)
[144] Fix | Delete
);
[145] Fix | Delete
rest_do_request( $request );
[146] Fix | Delete
[147] Fix | Delete
update_option( 'woocommerce_admin_created_default_shipping_zones', 'yes' );
[148] Fix | Delete
Shipping::delete_zone_count_transient();
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
return $settings;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Adds fields so that we can store performance indicators, row settings, and chart type settings for users.
[156] Fix | Delete
*
[157] Fix | Delete
* @param array $user_data_fields User data fields.
[158] Fix | Delete
* @return array
[159] Fix | Delete
*/
[160] Fix | Delete
public function add_user_data_fields( $user_data_fields ) {
[161] Fix | Delete
return array_merge(
[162] Fix | Delete
$user_data_fields,
[163] Fix | Delete
array(
[164] Fix | Delete
'homepage_layout',
[165] Fix | Delete
'homepage_stats',
[166] Fix | Delete
'task_list_tracked_started_tasks',
[167] Fix | Delete
)
[168] Fix | Delete
);
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
/**
[172] Fix | Delete
* Registers home page.
[173] Fix | Delete
*/
[174] Fix | Delete
public function register_page() {
[175] Fix | Delete
// Register a top-level item for users who cannot view the core WooCommerce menu.
[176] Fix | Delete
if ( ! self::is_admin_user() ) {
[177] Fix | Delete
wc_admin_register_page(
[178] Fix | Delete
array(
[179] Fix | Delete
'id' => 'woocommerce-home',
[180] Fix | Delete
'title' => __( 'WooCommerce', 'woocommerce' ),
[181] Fix | Delete
'path' => self::MENU_SLUG,
[182] Fix | Delete
'capability' => 'read',
[183] Fix | Delete
)
[184] Fix | Delete
);
[185] Fix | Delete
return;
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
wc_admin_register_page(
[189] Fix | Delete
array(
[190] Fix | Delete
'id' => 'woocommerce-home',
[191] Fix | Delete
'title' => __( 'Home', 'woocommerce' ),
[192] Fix | Delete
'parent' => 'woocommerce',
[193] Fix | Delete
'path' => self::MENU_SLUG,
[194] Fix | Delete
'order' => 0,
[195] Fix | Delete
'capability' => 'read',
[196] Fix | Delete
)
[197] Fix | Delete
);
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
/**
[201] Fix | Delete
* Check if the user can access the top-level WooCommerce item.
[202] Fix | Delete
*
[203] Fix | Delete
* @return bool
[204] Fix | Delete
*/
[205] Fix | Delete
public static function is_admin_user() {
[206] Fix | Delete
if ( ! class_exists( 'WC_Admin_Menus', false ) ) {
[207] Fix | Delete
include_once WC_ABSPATH . 'includes/admin/class-wc-admin-menus.php';
[208] Fix | Delete
}
[209] Fix | Delete
if ( method_exists( 'WC_Admin_Menus', 'can_view_woocommerce_menu_item' ) ) {
[210] Fix | Delete
return \WC_Admin_Menus::can_view_woocommerce_menu_item() || current_user_can( 'manage_woocommerce' );
[211] Fix | Delete
} else {
[212] Fix | Delete
// We leave this line for WC versions <= 6.2.
[213] Fix | Delete
return current_user_can( 'edit_others_shop_orders' ) || current_user_can( 'manage_woocommerce' );
[214] Fix | Delete
}
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
/**
[218] Fix | Delete
* Possibly remove the WooCommerce menu item if it was purely used to access wc-admin pages.
[219] Fix | Delete
*/
[220] Fix | Delete
public function possibly_remove_woocommerce_menu() {
[221] Fix | Delete
global $menu;
[222] Fix | Delete
[223] Fix | Delete
if ( self::is_admin_user() ) {
[224] Fix | Delete
return;
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
foreach ( $menu as $key => $menu_item ) {
[228] Fix | Delete
if ( self::MENU_SLUG !== $menu_item[2] || 'read' !== $menu_item[1] ) {
[229] Fix | Delete
continue;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
unset( $menu[ $key ] );
[233] Fix | Delete
}
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* Update the WooCommerce menu structure to make our main dashboard/handler
[238] Fix | Delete
* the top level link for 'WooCommerce'.
[239] Fix | Delete
*/
[240] Fix | Delete
public function update_link_structure() {
[241] Fix | Delete
global $submenu;
[242] Fix | Delete
// User does not have capabilities to see the submenu.
[243] Fix | Delete
if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu['woocommerce'] ) ) {
[244] Fix | Delete
return;
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
$wc_admin_key = null;
[248] Fix | Delete
foreach ( $submenu['woocommerce'] as $submenu_key => $submenu_item ) {
[249] Fix | Delete
if ( self::MENU_SLUG === $submenu_item[2] ) {
[250] Fix | Delete
$wc_admin_key = $submenu_key;
[251] Fix | Delete
break;
[252] Fix | Delete
}
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
if ( ! $wc_admin_key ) {
[256] Fix | Delete
return;
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
$menu = $submenu['woocommerce'][ $wc_admin_key ];
[260] Fix | Delete
[261] Fix | Delete
// Move menu item to top of array.
[262] Fix | Delete
unset( $submenu['woocommerce'][ $wc_admin_key ] );
[263] Fix | Delete
array_unshift( $submenu['woocommerce'], $menu );
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
/**
[267] Fix | Delete
* Preload options to prime state of the application.
[268] Fix | Delete
*
[269] Fix | Delete
* @param array $options Array of options to preload.
[270] Fix | Delete
* @return array
[271] Fix | Delete
*/
[272] Fix | Delete
public function preload_options( $options ) {
[273] Fix | Delete
$options[] = 'woocommerce_default_homepage_layout';
[274] Fix | Delete
$options[] = 'woocommerce_admin_install_timestamp';
[275] Fix | Delete
[276] Fix | Delete
return $options;
[277] Fix | Delete
}
[278] Fix | Delete
}
[279] Fix | Delete
[280] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function