Navigation
5 years ago
ActivityPanels.php
5 years ago
Analytics.php
5 years ago
AnalyticsDashboard.php
5 years ago
Coupons.php
5 years ago
CouponsMovedTrait.php
5 years ago
CustomerEffortScoreTracks.php
5 years ago
Features.php
5 years ago
Homescreen.php
5 years ago
Marketing.php
5 years ago
MobileAppBanner.php
5 years ago
Onboarding.php
5 years ago
OnboardingTasks.php
5 years ago
Settings.php
5 years ago
ShippingLabelBanner.php
5 years ago
ShippingLabelBannerDisplayRules.php
5 years ago
Homescreen.php
166 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Homescreen. |
| 4 | * NOTE: DO NOT edit this file in WooCommerce core, this is generated from woocommerce-admin. |
| 5 | */ |
| 6 | |
| 7 | namespace Automattic\WooCommerce\Admin\Features; |
| 8 | |
| 9 | use Automattic\WooCommerce\Admin\Loader; |
| 10 | |
| 11 | /** |
| 12 | * Contains backend logic for the homescreen feature. |
| 13 | */ |
| 14 | class Homescreen { |
| 15 | /** |
| 16 | * Menu slug. |
| 17 | */ |
| 18 | const MENU_SLUG = 'wc-admin'; |
| 19 | |
| 20 | /** |
| 21 | * Class instance. |
| 22 | * |
| 23 | * @var Homescreen instance |
| 24 | */ |
| 25 | protected static $instance = null; |
| 26 | |
| 27 | /** |
| 28 | * Get class instance. |
| 29 | */ |
| 30 | public static function get_instance() { |
| 31 | if ( ! self::$instance ) { |
| 32 | self::$instance = new self(); |
| 33 | } |
| 34 | return self::$instance; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Hook into WooCommerce. |
| 39 | */ |
| 40 | public function __construct() { |
| 41 | add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) ); |
| 42 | add_action( 'admin_menu', array( $this, 'register_page' ) ); |
| 43 | // In WC Core 5.1 $submenu manipulation occurs in admin_menu, not admin_head. See https://github.com/woocommerce/woocommerce/pull/29088. |
| 44 | if ( version_compare( WC_VERSION, '5.1', '>=' ) ) { |
| 45 | // priority is 20 to run after admin_menu hook for woocommerce runs, so that submenu is populated. |
| 46 | add_action( 'admin_menu', array( $this, 'update_link_structure' ), 20 ); |
| 47 | } else { |
| 48 | // priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165. |
| 49 | add_action( 'admin_head', array( $this, 'update_link_structure' ), 20 ); |
| 50 | } |
| 51 | add_filter( 'woocommerce_admin_plugins_whitelist', array( $this, 'get_homescreen_allowed_plugins' ) ); |
| 52 | add_filter( 'woocommerce_admin_preload_options', array( $this, 'preload_options' ) ); |
| 53 | add_filter( 'woocommerce_shared_settings', array( $this, 'component_settings' ), 20 ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Adds fields so that we can store performance indicators, row settings, and chart type settings for users. |
| 58 | * |
| 59 | * @param array $user_data_fields User data fields. |
| 60 | * @return array |
| 61 | */ |
| 62 | public function add_user_data_fields( $user_data_fields ) { |
| 63 | return array_merge( |
| 64 | $user_data_fields, |
| 65 | array( |
| 66 | 'homepage_layout', |
| 67 | 'homepage_stats', |
| 68 | 'task_list_tracked_started_tasks', |
| 69 | 'help_panel_highlight_shown', |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Registers home page. |
| 76 | */ |
| 77 | public function register_page() { |
| 78 | wc_admin_register_page( |
| 79 | array( |
| 80 | 'id' => 'woocommerce-home', |
| 81 | 'title' => __( 'Home', 'woocommerce' ), |
| 82 | 'parent' => 'woocommerce', |
| 83 | 'path' => self::MENU_SLUG, |
| 84 | 'order' => 0, |
| 85 | 'capability' => 'manage_woocommerce', |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Update the WooCommerce menu structure to make our main dashboard/handler |
| 92 | * the top level link for 'WooCommerce'. |
| 93 | */ |
| 94 | public function update_link_structure() { |
| 95 | global $submenu; |
| 96 | // User does not have capabilites to see the submenu. |
| 97 | if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu['woocommerce'] ) ) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | $wc_admin_key = null; |
| 102 | foreach ( $submenu['woocommerce'] as $submenu_key => $submenu_item ) { |
| 103 | if ( self::MENU_SLUG === $submenu_item[2] ) { |
| 104 | $wc_admin_key = $submenu_key; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if ( ! $wc_admin_key ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | $menu = $submenu['woocommerce'][ $wc_admin_key ]; |
| 114 | |
| 115 | // Move menu item to top of array. |
| 116 | unset( $submenu['woocommerce'][ $wc_admin_key ] ); |
| 117 | array_unshift( $submenu['woocommerce'], $menu ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Gets an array of plugins that can be installed & activated via the home screen. |
| 122 | * |
| 123 | * @param array $plugins Array of plugin slugs to be allowed. |
| 124 | * |
| 125 | * @return array |
| 126 | */ |
| 127 | public static function get_homescreen_allowed_plugins( $plugins ) { |
| 128 | $homescreen_plugins = array( |
| 129 | 'jetpack' => 'jetpack/jetpack.php', |
| 130 | ); |
| 131 | |
| 132 | return array_merge( $plugins, $homescreen_plugins ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Preload options to prime state of the application. |
| 137 | * |
| 138 | * @param array $options Array of options to preload. |
| 139 | * @return array |
| 140 | */ |
| 141 | public function preload_options( $options ) { |
| 142 | $options[] = 'woocommerce_default_homepage_layout'; |
| 143 | |
| 144 | return $options; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Add data to the shared component settings. |
| 149 | * |
| 150 | * @param array $settings Shared component settings. |
| 151 | */ |
| 152 | public function component_settings( $settings ) { |
| 153 | $allowed_statuses = Loader::get_order_statuses( wc_get_order_statuses() ); |
| 154 | |
| 155 | // Remove the Draft Order status (from the Checkout Block). |
| 156 | unset( $allowed_statuses['checkout-draft'] ); |
| 157 | |
| 158 | $status_counts = array_map( 'wc_orders_count', array_keys( $allowed_statuses ) ); |
| 159 | $product_counts = wp_count_posts( 'product' ); |
| 160 | $settings['orderCount'] = array_sum( $status_counts ); |
| 161 | $settings['publishedProductCount'] = $product_counts->publish; |
| 162 | |
| 163 | return $settings; |
| 164 | } |
| 165 | } |
| 166 |