Agentic
4 months ago
BlockTemplates
4 weeks ago
EmailImprovements
1 year ago
EmailPreview
4 weeks ago
Emails
1 year ago
ImportExport
1 year ago
Logging
1 year ago
Marketing
2 years ago
Notes
4 weeks ago
Onboarding
5 months ago
Orders
4 weeks ago
ProductForm
2 years ago
ProductReviews
4 weeks ago
RemoteFreeExtensions
4 weeks ago
Schedulers
4 weeks ago
Settings
2 weeks ago
Suggestions
4 weeks ago
WCPayPromotion
10 months ago
ActivityPanels.php
3 years ago
Analytics.php
4 weeks ago
CategoryLookup.php
4 years ago
Coupons.php
1 year ago
CouponsMovedTrait.php
5 months ago
CustomerEffortScoreTracks.php
7 months ago
Events.php
2 months ago
FeaturePlugin.php
4 months ago
Homescreen.php
1 month ago
Loader.php
4 weeks ago
Marketing.php
1 year ago
Marketplace.php
5 months ago
MobileAppBanner.php
4 years ago
OrderMilestoneEasterEgg.php
4 weeks ago
RemoteInboxNotifications.php
3 years ago
Settings.php
2 weeks ago
ShippingLabelBanner.php
1 year ago
ShippingLabelBannerDisplayRules.php
1 year ago
SiteHealth.php
3 years ago
Survey.php
4 years ago
SystemStatusReport.php
1 year ago
Translations.php
1 year ago
WCAdminAssets.php
2 weeks ago
WCAdminSharedSettings.php
1 year ago
WCAdminUser.php
9 months ago
WcPayWelcomePage.php
1 year ago
Marketing.php
240 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Marketing. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin; |
| 7 | |
| 8 | use Automattic\WooCommerce\Admin\Features\Features; |
| 9 | use Automattic\WooCommerce\Admin\Marketing\InstalledExtensions; |
| 10 | use Automattic\WooCommerce\Admin\PageController; |
| 11 | |
| 12 | /** |
| 13 | * Contains backend logic for the Marketing feature. |
| 14 | */ |
| 15 | class Marketing { |
| 16 | |
| 17 | use CouponsMovedTrait; |
| 18 | |
| 19 | /** |
| 20 | * Constant representing the key for the submenu name value in the global $submenu array. |
| 21 | * |
| 22 | * @var int |
| 23 | */ |
| 24 | const SUBMENU_NAME_KEY = 0; |
| 25 | |
| 26 | /** |
| 27 | * Constant representing the key for the submenu location value in the global $submenu array. |
| 28 | * |
| 29 | * @var int |
| 30 | */ |
| 31 | const SUBMENU_LOCATION_KEY = 2; |
| 32 | |
| 33 | /** |
| 34 | * Class instance. |
| 35 | * |
| 36 | * @var Marketing instance |
| 37 | */ |
| 38 | protected static $instance = null; |
| 39 | |
| 40 | /** |
| 41 | * Get class instance. |
| 42 | */ |
| 43 | public static function get_instance() { |
| 44 | if ( ! self::$instance ) { |
| 45 | self::$instance = new self(); |
| 46 | } |
| 47 | return self::$instance; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Hook into WooCommerce. |
| 52 | */ |
| 53 | public function __construct() { |
| 54 | if ( ! is_admin() ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | add_action( 'admin_menu', array( $this, 'register_pages' ), 5 ); |
| 59 | add_action( 'admin_menu', array( $this, 'add_parent_menu_item' ), 6 ); |
| 60 | |
| 61 | // Overwrite submenu default ordering for marketing menu. High priority gives plugins the chance to register their own menu items. |
| 62 | add_action( 'admin_menu', array( $this, 'reorder_marketing_submenu' ), 99 ); |
| 63 | |
| 64 | add_filter( 'woocommerce_admin_shared_settings', array( $this, 'component_settings' ), 30 ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Add main marketing menu item. |
| 69 | * |
| 70 | * Uses priority of 9 so other items can easily be added at the default priority (10). |
| 71 | */ |
| 72 | public function add_parent_menu_item() { |
| 73 | if ( ! Features::is_enabled( 'navigation' ) ) { |
| 74 | add_menu_page( |
| 75 | __( 'Marketing', 'woocommerce' ), |
| 76 | __( 'Marketing', 'woocommerce' ), |
| 77 | 'manage_woocommerce', |
| 78 | 'woocommerce-marketing', |
| 79 | null, |
| 80 | 'dashicons-megaphone', |
| 81 | 58 |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | PageController::get_instance()->connect_page( |
| 86 | array( |
| 87 | 'id' => 'woocommerce-marketing', |
| 88 | 'title' => 'Marketing', |
| 89 | 'capability' => 'manage_woocommerce', |
| 90 | 'path' => 'wc-admin&path=/marketing', |
| 91 | ) |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Registers report pages. |
| 97 | */ |
| 98 | public function register_pages() { |
| 99 | $this->register_overview_page(); |
| 100 | |
| 101 | $controller = PageController::get_instance(); |
| 102 | $defaults = array( |
| 103 | 'parent' => 'woocommerce-marketing', |
| 104 | 'existing_page' => false, |
| 105 | ); |
| 106 | |
| 107 | /** |
| 108 | * Filters marketing menu items. |
| 109 | * |
| 110 | * @since 4.1.0 |
| 111 | * @param array $items Marketing pages. |
| 112 | */ |
| 113 | $marketing_pages = apply_filters( 'woocommerce_marketing_menu_items', array() ); |
| 114 | foreach ( $marketing_pages as $marketing_page ) { |
| 115 | if ( ! is_array( $marketing_page ) ) { |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | $marketing_page = array_merge( $defaults, $marketing_page ); |
| 120 | |
| 121 | if ( $marketing_page['existing_page'] ) { |
| 122 | $controller->connect_page( $marketing_page ); |
| 123 | } else { |
| 124 | $controller->register_page( $marketing_page ); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Register the main Marketing page, which is Marketing > Overview. |
| 131 | * |
| 132 | * This is done separately because we need to ensure the page is registered properly and |
| 133 | * that the link is done properly. For some reason the normal page registration process |
| 134 | * gives us the wrong menu link. |
| 135 | */ |
| 136 | protected function register_overview_page() { |
| 137 | global $submenu; |
| 138 | |
| 139 | // First register the page. |
| 140 | PageController::get_instance()->register_page( |
| 141 | array( |
| 142 | 'id' => 'woocommerce-marketing-overview', |
| 143 | 'title' => __( 'Overview', 'woocommerce' ), |
| 144 | 'path' => 'wc-admin&path=/marketing', |
| 145 | 'parent' => 'woocommerce-marketing', |
| 146 | ) |
| 147 | ); |
| 148 | |
| 149 | // Now fix the path, since register_page() gets it wrong. |
| 150 | if ( ! isset( $submenu['woocommerce-marketing'] ) ) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | foreach ( $submenu['woocommerce-marketing'] as &$item ) { |
| 155 | // The "slug" (aka the path) is the third item in the array. |
| 156 | if ( 0 === strpos( $item[2], 'wc-admin' ) ) { |
| 157 | $item[2] = 'admin.php?page=' . $item[2]; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Order marketing menu items alphabetically. |
| 164 | * Overview should be first, and Coupons should be second, followed by other marketing menu items. |
| 165 | * |
| 166 | * @return void |
| 167 | */ |
| 168 | public function reorder_marketing_submenu() { |
| 169 | global $submenu; |
| 170 | |
| 171 | if ( ! isset( $submenu['woocommerce-marketing'] ) ) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | $marketing_submenu = $submenu['woocommerce-marketing']; |
| 176 | $new_menu_order = array(); |
| 177 | |
| 178 | // Overview should be first. |
| 179 | $overview_key = array_search( 'Overview', array_column( $marketing_submenu, self::SUBMENU_NAME_KEY ), true ); |
| 180 | |
| 181 | if ( false === $overview_key ) { |
| 182 | /* |
| 183 | * If Overview is not found, we may be on a site with a different language. |
| 184 | * We can use a fallback and try to find the overview page by its path. |
| 185 | */ |
| 186 | $overview_key = array_search( 'admin.php?page=wc-admin&path=/marketing', array_column( $marketing_submenu, self::SUBMENU_LOCATION_KEY ), true ); |
| 187 | } |
| 188 | |
| 189 | if ( false !== $overview_key ) { |
| 190 | $new_menu_order[] = $marketing_submenu[ $overview_key ]; |
| 191 | array_splice( $marketing_submenu, $overview_key, 1 ); |
| 192 | } |
| 193 | |
| 194 | // Coupons should be second. |
| 195 | $coupons_key = array_search( 'Coupons', array_column( $marketing_submenu, self::SUBMENU_NAME_KEY ), true ); |
| 196 | |
| 197 | if ( false === $coupons_key ) { |
| 198 | /* |
| 199 | * If Coupons is not found, we may be on a site with a different language. |
| 200 | * We can use a fallback and try to find the coupons page by its path. |
| 201 | */ |
| 202 | $coupons_key = array_search( 'edit.php?post_type=shop_coupon', array_column( $marketing_submenu, self::SUBMENU_LOCATION_KEY ), true ); |
| 203 | } |
| 204 | |
| 205 | if ( false !== $coupons_key ) { |
| 206 | $new_menu_order[] = $marketing_submenu[ $coupons_key ]; |
| 207 | array_splice( $marketing_submenu, $coupons_key, 1 ); |
| 208 | } |
| 209 | |
| 210 | // Sort the rest of the items alphabetically. |
| 211 | usort( |
| 212 | $marketing_submenu, |
| 213 | function ( $a, $b ) { |
| 214 | return strcmp( $a[0], $b[0] ); |
| 215 | } |
| 216 | ); |
| 217 | |
| 218 | $new_menu_order = array_merge( $new_menu_order, $marketing_submenu ); |
| 219 | |
| 220 | $submenu['woocommerce-marketing'] = $new_menu_order; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Add settings for marketing feature. |
| 225 | * |
| 226 | * @param array $settings Component settings. |
| 227 | * @return array |
| 228 | */ |
| 229 | public function component_settings( $settings ) { |
| 230 | // Bail early if not on a wc-admin powered page. |
| 231 | if ( ! PageController::is_admin_page() ) { |
| 232 | return $settings; |
| 233 | } |
| 234 | |
| 235 | $settings['marketing']['installedExtensions'] = InstalledExtensions::get_data(); |
| 236 | |
| 237 | return $settings; |
| 238 | } |
| 239 | } |
| 240 |