| @@ -14,8 +14,9 @@ | ||
| 14 | 14 | public $plugin_slug = 'merchant'; |
| 15 | 15 | public $capability = 'manage_options'; |
| 16 | 16 | public $priority = 58; |
| 17 | 17 | public $notifications = array(); |
| 18 | + public static $installed_plugins = array(); | |
| 18 | 19 | private static $instance = null; |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | 22 | * Instance. |
| @@ -32,18 +33,179 @@ | ||
| 32 | 33 | * Constructor. |
| 33 | 34 | * |
| 34 | 35 | */ |
| 35 | 36 | public function __construct() { |
| 36 | - if ( defined( 'MERCHANT_AWL_ACTIVE' ) ) { | |
| 37 | + if ( function_exists( 'merchant_white_label_is_applied' ) && merchant_white_label_is_applied() ) { | |
| 38 | + // Register the pages, then drop their menu entries. The settings URL | |
| 39 | + // keeps working for the admin who turned White Label on, who would | |
| 40 | + // otherwise have no way back to the switch. | |
| 41 | + add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); | |
| 42 | + add_action( 'admin_menu', array( $this, 'hide_branded_menu' ), 99999 ); | |
| 43 | + | |
| 37 | 44 | return; |
| 38 | 45 | } |
| 39 | 46 | |
| 40 | 47 | add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
| 48 | + add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_menu' ), 100 ); | |
| 49 | + add_action( 'admin_enqueue_scripts', array( $this, 'analytics_assets' ) ); | |
| 50 | + add_action( 'wp_dashboard_setup', array( $this, 'dashboard_analytics_widget' ) ); | |
| 41 | 51 | add_action( 'wp_ajax_merchant_notifications_read', array( $this, 'ajax_notifications_read' ) ); |
| 42 | 52 | add_action('admin_footer', array( $this, 'footer_internal_scripts' )); |
| 43 | 53 | } |
| 44 | 54 | |
| 45 | 55 | /** |
| 56 | + * Hide Merchant's menu entries while White Label is applied. | |
| 57 | + * | |
| 58 | + * Removing the entries leaves the registered pages callable, so a direct | |
| 59 | + * URL still works — the same approach the aThemes White Label plugin uses. | |
| 60 | + * | |
| 61 | + * @return void | |
| 62 | + */ | |
| 63 | + public function hide_branded_menu() { | |
| 64 | + remove_menu_page( $this->plugin_slug ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Add dashboard widget. | |
| 69 | + * | |
| 70 | + * @return void | |
| 71 | + */ | |
| 72 | + public function dashboard_analytics_widget() { | |
| 73 | + if( class_exists( 'WooCommerce' ) ) { | |
| 74 | + wp_add_dashboard_widget( | |
| 75 | + 'merchant_modules_revenue', // Widget slug. | |
| 76 | + esc_html__( 'Daily added revenue by Merchant', 'merchant' ), // Title. | |
| 77 | + array( $this, 'dashboard_analytics_widget_content' ) // Display function. | |
| 78 | + ); | |
| 79 | + } | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Dashboard widget content. | |
| 84 | + * | |
| 85 | + * @return void | |
| 86 | + */ | |
| 87 | + public function dashboard_analytics_widget_content() { | |
| 88 | + $reports = new Merchant_Analytics_Data_Reports(); | |
| 89 | + $date_ranges = $reports->get_last_and_previous_7_days_ranges(); | |
| 90 | + ?> | |
| 91 | + <div class="merchant-analytics-widget widget-chart-section"> | |
| 92 | + <div class="chart" data-period="<?php | |
| 93 | + echo esc_attr( wp_json_encode( $reports->get_revenue_chart_report( $date_ranges['recent_period']['start'], $date_ranges['recent_period']['end'] ) ) ) | |
| 94 | + ?>"></div> | |
| 95 | + <div class="foot"> | |
| 96 | + <div class="date-range"> | |
| 97 | + <span> | |
| 98 | + <input type="text" class="date-range-input" readonly value="<?php | |
| 99 | + echo esc_attr( implode( ' - ', array_values( $date_ranges['recent_period'] ) ) ) ?>" placeholder="<?php | |
| 100 | + esc_attr_e( 'Select date range', 'merchant' ); ?>"> | |
| 101 | + </span> | |
| 102 | + <span class="merchant-analytics-loading-spinner"></span> | |
| 103 | + </div> | |
| 104 | + <div class="analytics-link"> | |
| 105 | + <a href="<?php echo esc_url( admin_url( 'admin.php?page=merchant§ion=analytics' ) ); ?>"><?php | |
| 106 | + esc_html_e( 'View Full Analytics', 'merchant' ); ?></a> | |
| 107 | + </div> | |
| 108 | + </div> | |
| 109 | + </div> | |
| 110 | + <?php | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Enqueue assets for analytics page. | |
| 115 | + * | |
| 116 | + * @return void | |
| 117 | + */ | |
| 118 | + public function analytics_assets( $hook ) { | |
| 119 | + global $pagenow; | |
| 120 | + $section = sanitize_text_field( $_GET['section'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 121 | + | |
| 122 | + if ( | |
| 123 | + ( $hook === 'toplevel_page_merchant' && $section !== 'settings' && class_exists( 'WooCommerce' ) ) | |
| 124 | + || ( $pagenow === 'index.php' && class_exists( 'WooCommerce' ) ) | |
| 125 | + ) { | |
| 126 | + wp_enqueue_style('date-picker', MERCHANT_URI . 'assets/vendor/air-datepicker/air-datepicker.css', array(), MERCHANT_VERSION, 'all' ); | |
| 127 | + wp_enqueue_style( 'merchant-analytics', MERCHANT_URI . 'assets/css/admin/analytics.css', array(), MERCHANT_VERSION ); | |
| 128 | + wp_enqueue_script('date-picker', MERCHANT_URI . 'assets/vendor/air-datepicker/air-datepicker.js', array( 'jquery' ), MERCHANT_VERSION, true ); | |
| 129 | + wp_enqueue_script( 'apexcharts', MERCHANT_URI . 'assets/js/vendor/apexcharts.min.js', array( 'jquery' ), MERCHANT_VERSION, true ); | |
| 130 | + wp_enqueue_script( 'merchant-analytics', MERCHANT_URI . 'assets/js/admin/analytics.js', array( 'jquery', 'apexcharts' ), MERCHANT_VERSION, true ); | |
| 131 | + wp_localize_script( 'merchant-analytics', 'merchant_analytics', array( | |
| 132 | + 'nonce' => wp_create_nonce( 'merchant' ), | |
| 133 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 134 | + 'currency_name' => get_woocommerce_currency(), | |
| 135 | + 'currency_symbol' => get_woocommerce_currency_symbol(), | |
| 136 | + 'labels' => array( | |
| 137 | + 'orders' => esc_html__( 'orders', 'merchant' ), | |
| 138 | + 'orders_aov' => esc_html__( 'Orders AOV', 'merchant' ), | |
| 139 | + ), | |
| 140 | + ) ); | |
| 141 | + | |
| 142 | + wp_localize_script( 'merchant-analytics', 'merchant_datepicker_locale', array( | |
| 143 | + wp_json_encode( | |
| 144 | + array( | |
| 145 | + 'days' => array( | |
| 146 | + esc_html__( 'Sunday', 'merchant' ), | |
| 147 | + esc_html__( 'Monday', 'merchant' ), | |
| 148 | + esc_html__( 'Tuesday', 'merchant' ), | |
| 149 | + esc_html__( 'Wednesday', 'merchant' ), | |
| 150 | + esc_html__( 'Thursday', 'merchant' ), | |
| 151 | + esc_html__( 'Friday', 'merchant' ), | |
| 152 | + esc_html__( 'Saturday', 'merchant' ), | |
| 153 | + ), | |
| 154 | + 'daysShort' => array( | |
| 155 | + esc_html__( 'Sun', 'merchant' ), | |
| 156 | + esc_html__( 'Mon', 'merchant' ), | |
| 157 | + esc_html__( 'Tue', 'merchant' ), | |
| 158 | + esc_html__( 'Wed', 'merchant' ), | |
| 159 | + esc_html__( 'Thu', 'merchant' ), | |
| 160 | + esc_html__( 'Fri', 'merchant' ), | |
| 161 | + esc_html__( 'Sat', 'merchant' ), | |
| 162 | + ), | |
| 163 | + 'daysMin' => array( | |
| 164 | + esc_html__( 'Su', 'merchant' ), | |
| 165 | + esc_html__( 'Mo', 'merchant' ), | |
| 166 | + esc_html__( 'Tu', 'merchant' ), | |
| 167 | + esc_html__( 'We', 'merchant' ), | |
| 168 | + esc_html__( 'Th', 'merchant' ), | |
| 169 | + esc_html__( 'Fr', 'merchant' ), | |
| 170 | + esc_html__( 'Sa', 'merchant' ), | |
| 171 | + ), | |
| 172 | + 'months' => array( | |
| 173 | + esc_html__( 'January', 'merchant' ), | |
| 174 | + esc_html__( 'February', 'merchant' ), | |
| 175 | + esc_html__( 'March', 'merchant' ), | |
| 176 | + esc_html__( 'April', 'merchant' ), | |
| 177 | + esc_html__( 'May', 'merchant' ), | |
| 178 | + esc_html__( 'June', 'merchant' ), | |
| 179 | + esc_html__( 'July', 'merchant' ), | |
| 180 | + esc_html__( 'August', 'merchant' ), | |
| 181 | + esc_html__( 'September', 'merchant' ), | |
| 182 | + esc_html__( 'October', 'merchant' ), | |
| 183 | + esc_html__( 'November', 'merchant' ), | |
| 184 | + esc_html__( 'December', 'merchant' ), | |
| 185 | + ), | |
| 186 | + 'monthsShort' => array( | |
| 187 | + esc_html__( 'Jan', 'merchant' ), | |
| 188 | + esc_html__( 'Feb', 'merchant' ), | |
| 189 | + esc_html__( 'Mar', 'merchant' ), | |
| 190 | + esc_html__( 'Apr', 'merchant' ), | |
| 191 | + esc_html__( 'May', 'merchant' ), | |
| 192 | + esc_html__( 'Jun', 'merchant' ), | |
| 193 | + esc_html__( 'Jul', 'merchant' ), | |
| 194 | + esc_html__( 'Aug', 'merchant' ), | |
| 195 | + esc_html__( 'Sep', 'merchant' ), | |
| 196 | + esc_html__( 'Oct', 'merchant' ), | |
| 197 | + esc_html__( 'Nov', 'merchant' ), | |
| 198 | + esc_html__( 'Dec', 'merchant' ), | |
| 199 | + ), | |
| 200 | + 'clear' => esc_html__( 'Clear', 'merchant' ), | |
| 201 | + ) | |
| 202 | + ), | |
| 203 | + ) ); | |
| 204 | + } | |
| 205 | + } | |
| 206 | + | |
| 207 | + /** | |
| 46 | 208 | * Include required classes. |
| 47 | 209 | * |
| 48 | 210 | * @return void |
| 49 | 211 | */ |
| @@ -65,50 +227,185 @@ | ||
| 65 | 227 | add_submenu_page( |
| 66 | 228 | $this->plugin_slug, |
| 67 | 229 | esc_html__('Dashboard', 'merchant'), |
| 68 | 230 | esc_html__('Dashboard', 'merchant'), |
| 69 | - 'manage_options', | |
| 231 | + $this->capability, | |
| 70 | 232 | $this->plugin_slug, |
| 71 | 233 | '', |
| 72 | 234 | 1 |
| 73 | 235 | ); |
| 74 | 236 | |
| 75 | - // Enabled Modules. | |
| 237 | + // All Modules. | |
| 76 | 238 | add_submenu_page( |
| 77 | 239 | $this->plugin_slug, |
| 78 | - esc_html__('Enabled Modules', 'merchant'), | |
| 79 | - esc_html__('Enabled Modules', 'merchant'), | |
| 80 | - 'manage_options', | |
| 240 | + esc_html__('Modules', 'merchant'), | |
| 241 | + esc_html__('Modules', 'merchant'), | |
| 242 | + $this->capability, | |
| 81 | 243 | 'admin.php?page=merchant§ion=modules', |
| 82 | 244 | '', |
| 83 | 245 | 2 |
| 84 | 246 | ); |
| 85 | 247 | |
| 248 | + if( class_exists( 'WooCommerce' ) ) { | |
| 249 | + // Campaigns. | |
| 250 | + add_submenu_page( | |
| 251 | + $this->plugin_slug, | |
| 252 | + esc_html__( 'Campaigns', 'merchant' ), | |
| 253 | + esc_html__( 'Campaigns', 'merchant' ), | |
| 254 | + $this->capability, | |
| 255 | + 'admin.php?page=merchant§ion=campaigns', | |
| 256 | + '', | |
| 257 | + 3 | |
| 258 | + ); | |
| 259 | + | |
| 260 | + // Analytics. | |
| 261 | + add_submenu_page( | |
| 262 | + $this->plugin_slug, | |
| 263 | + esc_html__( 'Analytics', 'merchant' ), | |
| 264 | + esc_html__( 'Analytics', 'merchant' ), | |
| 265 | + $this->capability, | |
| 266 | + 'admin.php?page=merchant§ion=analytics', | |
| 267 | + '', | |
| 268 | + 4 | |
| 269 | + ); | |
| 270 | + } | |
| 271 | + | |
| 86 | 272 | // Settings. |
| 87 | 273 | add_submenu_page( |
| 88 | 274 | $this->plugin_slug, |
| 89 | 275 | esc_html__('Settings', 'merchant'), |
| 90 | 276 | esc_html__('Settings', 'merchant'), |
| 91 | - 'manage_options', | |
| 277 | + $this->capability, | |
| 92 | 278 | 'admin.php?page=merchant§ion=settings', |
| 93 | 279 | '', |
| 94 | - 3 | |
| 280 | + 5 | |
| 95 | 281 | ); |
| 96 | 282 | |
| 97 | 283 | // Add 'Upgrade' link. |
| 98 | - if( ! defined( 'MERCHANT_PRO_VERSION' ) ) { | |
| 284 | + if ( ! defined( 'MERCHANT_PRO_VERSION' ) ) { | |
| 285 | + $url = merchant_admin_upgrade_link( | |
| 286 | + 'https://athemes.com/merchant-upgrade', | |
| 287 | + array( | |
| 288 | + 'utm_source' => 'theme_submenu_page', | |
| 289 | + 'utm_medium' => 'button', | |
| 290 | + 'utm_campaign' => 'Merchant', | |
| 291 | + ), | |
| 292 | + 'theme-submenu-page-upgrade-link' | |
| 293 | + ); | |
| 99 | 294 | add_submenu_page( |
| 100 | 295 | $this->plugin_slug, |
| 101 | - esc_html__('Upgrade to Pro', 'merchant'), | |
| 102 | - esc_html__('Upgrade to Pro', 'merchant'), | |
| 103 | - 'manage_options', | |
| 104 | - 'https://athemes.com/merchant-upgrade?utm_source=theme_submenu_page&utm_medium=button&utm_campaign=Merchant', | |
| 296 | + esc_html__( 'Upgrade to Pro', 'merchant' ), | |
| 297 | + esc_html__( 'Upgrade to Pro', 'merchant' ), | |
| 298 | + $this->capability, | |
| 299 | + esc_url( $url ), | |
| 105 | 300 | '', |
| 106 | - 4 | |
| 301 | + 7 | |
| 107 | 302 | ); |
| 108 | 303 | } |
| 109 | 304 | } |
| 110 | 305 | |
| 306 | + /** | |
| 307 | + * Add admin bar menu. | |
| 308 | + * | |
| 309 | + * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance. | |
| 310 | + */ | |
| 311 | + public function add_admin_bar_menu( $wp_admin_bar ) { | |
| 312 | + // Check if the current user has the capability to manage options | |
| 313 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 314 | + return; | |
| 315 | + } | |
| 316 | + | |
| 317 | + // Parent menu item (Dashboard) | |
| 318 | + $wp_admin_bar->add_node( array( | |
| 319 | + 'id' => 'merchant-dashboard', | |
| 320 | + 'title' => esc_html__( 'Merchant', 'merchant' ), // Use your plugin name or page title | |
| 321 | + 'href' => admin_url( 'admin.php?page=merchant' ), // Link to the main dashboard | |
| 322 | + 'meta' => array( | |
| 323 | + 'title' => esc_html__( 'Merchant Dashboard', 'merchant' ), | |
| 324 | + ), | |
| 325 | + ) ); | |
| 326 | + | |
| 327 | + // Dashboard Sub Item | |
| 328 | + $wp_admin_bar->add_node( array( | |
| 329 | + 'id' => 'merchant-dashboard-sub', | |
| 330 | + 'parent' => 'merchant-dashboard', | |
| 331 | + 'title' => esc_html__( 'Dashboard', 'merchant' ), | |
| 332 | + 'href' => admin_url( 'admin.php?page=merchant' ), | |
| 333 | + 'meta' => array( | |
| 334 | + 'title' => esc_html__( 'Dashboard', 'merchant' ), | |
| 335 | + ), | |
| 336 | + ) ); | |
| 337 | + | |
| 338 | + // All Modules | |
| 339 | + $wp_admin_bar->add_node( array( | |
| 340 | + 'id' => 'merchant-modules', | |
| 341 | + 'parent' => 'merchant-dashboard', | |
| 342 | + 'title' => esc_html__( 'Modules', 'merchant' ), | |
| 343 | + 'href' => admin_url( 'admin.php?page=merchant§ion=modules' ), | |
| 344 | + 'meta' => array( | |
| 345 | + 'title' => esc_html__( 'Modules', 'merchant' ), | |
| 346 | + ), | |
| 347 | + ) ); | |
| 348 | + | |
| 349 | + if( class_exists( 'WooCommerce' ) ) { | |
| 350 | + // Settings | |
| 351 | + $wp_admin_bar->add_node( array( | |
| 352 | + 'id' => 'merchant-settings', | |
| 353 | + 'parent' => 'merchant-dashboard', | |
| 354 | + 'title' => esc_html__( 'Settings', 'merchant' ), | |
| 355 | + 'href' => admin_url( 'admin.php?page=merchant§ion=settings' ), | |
| 356 | + 'meta' => array( | |
| 357 | + 'title' => esc_html__( 'Settings', 'merchant' ), | |
| 358 | + ), | |
| 359 | + ) ); | |
| 360 | + | |
| 361 | + // Campaigns | |
| 362 | + $wp_admin_bar->add_node( array( | |
| 363 | + 'id' => 'merchant-campaigns', | |
| 364 | + 'parent' => 'merchant-dashboard', | |
| 365 | + 'title' => esc_html__( 'Campaigns', 'merchant' ), | |
| 366 | + 'href' => admin_url( 'admin.php?page=merchant§ion=campaigns' ), | |
| 367 | + 'meta' => array( | |
| 368 | + 'title' => esc_html__( 'Campaigns', 'merchant' ), | |
| 369 | + ), | |
| 370 | + ) ); | |
| 371 | + } | |
| 372 | + | |
| 373 | + // Analytics | |
| 374 | + $wp_admin_bar->add_node( array( | |
| 375 | + 'id' => 'merchant-analytics', | |
| 376 | + 'parent' => 'merchant-dashboard', | |
| 377 | + 'title' => esc_html__( 'Analytics', 'merchant' ), | |
| 378 | + 'href' => admin_url( 'admin.php?page=merchant§ion=analytics' ), | |
| 379 | + 'meta' => array( | |
| 380 | + 'title' => esc_html__( 'Analytics', 'merchant' ), | |
| 381 | + ), | |
| 382 | + ) ); | |
| 383 | + | |
| 384 | + // Upgrade to Pro (if not already defined) | |
| 385 | + if ( ! defined( 'MERCHANT_PRO_VERSION' ) ) { | |
| 386 | + $url = merchant_admin_upgrade_link( | |
| 387 | + 'https://athemes.com/merchant-upgrade', | |
| 388 | + array( | |
| 389 | + 'utm_source' => 'theme_submenu_page', | |
| 390 | + 'utm_medium' => 'button', | |
| 391 | + 'utm_campaign' => 'Merchant', | |
| 392 | + ), | |
| 393 | + 'adminbar-submenu-page-upgrade-link' | |
| 394 | + ); | |
| 395 | + $wp_admin_bar->add_node( array( | |
| 396 | + 'id' => 'merchant-upgrade', | |
| 397 | + 'parent' => 'merchant-dashboard', | |
| 398 | + 'title' => esc_html__( 'Upgrade to Pro', 'merchant' ), | |
| 399 | + 'href' => esc_url( $url ), | |
| 400 | + 'meta' => array( | |
| 401 | + 'title' => esc_html__( 'Upgrade to Pro', 'merchant' ), | |
| 402 | + 'target' => '_blank', // Open link in a new tab | |
| 403 | + ), | |
| 404 | + ) ); | |
| 405 | + } | |
| 406 | + } | |
| 407 | + | |
| 111 | 408 | /** |
| 112 | 409 | * Get Notifications |
| 113 | 410 | * |
| 114 | 411 | * @return array |
| @@ -184,13 +481,30 @@ | ||
| 184 | 481 | wp_send_json_success(); |
| 185 | 482 | } |
| 186 | 483 | |
| 187 | 484 | /** |
| 485 | + * Get installed plugins | |
| 486 | + * Provide the installed plugins array into the installed plugins static property for memoization purposes. | |
| 487 | + * | |
| 488 | + * @return array | |
| 489 | + */ | |
| 490 | + public static function get_installed_plugins() { | |
| 491 | + if ( empty( self::$installed_plugins ) ) { | |
| 492 | + if ( ! function_exists( 'get_plugins' ) ) { | |
| 493 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 494 | + } | |
| 495 | + self::$installed_plugins = get_plugins(); | |
| 496 | + } | |
| 497 | + return self::$installed_plugins; | |
| 498 | + } | |
| 499 | + | |
| 500 | + /** | |
| 188 | 501 | * Include Page dashboard |
| 189 | 502 | * |
| 190 | 503 | * @return void |
| 191 | 504 | */ |
| 192 | 505 | public function page_dashboard() { |
| 506 | + self::get_installed_plugins(); | |
| 193 | 507 | require_once MERCHANT_DIR . 'admin/pages/page-dashboard.php'; |
| 194 | 508 | } |
| 195 | 509 | |
| 196 | 510 | /** |