| @@ -1,159 +1,555 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Merchant_Admin_Menu Class. | |
| 4 | - */ | |
| 5 | - | |
| 6 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | -if ( ! class_exists( 'Merchant_Admin_Menu' ) ) { | |
| 11 | - | |
| 12 | - class Merchant_Admin_Menu { | |
| 13 | - | |
| 14 | - /** | |
| 15 | - * Page title. | |
| 16 | - */ | |
| 17 | - public $page_title = 'Merchant'; | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * Plugin slug. | |
| 21 | - */ | |
| 22 | - public $plugin_slug = 'merchant'; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Plugin capability. | |
| 26 | - */ | |
| 27 | - public $capability = 'manage_options'; | |
| 28 | - | |
| 29 | - /** | |
| 30 | - * Plugin priority. | |
| 31 | - */ | |
| 32 | - public $priority = 58; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * Plugin notifications. | |
| 36 | - */ | |
| 37 | - public $notifications = array(); | |
| 38 | - | |
| 39 | - /** | |
| 40 | - * The single class instance. | |
| 41 | - */ | |
| 42 | - private static $instance = null; | |
| 43 | - | |
| 44 | - /** | |
| 45 | - * Instance. | |
| 46 | - */ | |
| 47 | - public static function instance() { | |
| 48 | - if ( is_null( self::$instance ) ) { | |
| 49 | - self::$instance = new self(); | |
| 50 | - } | |
| 51 | - return self::$instance; | |
| 52 | - } | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * Constructor. | |
| 56 | - */ | |
| 57 | - public function __construct() { | |
| 58 | - | |
| 59 | - add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); | |
| 60 | - add_action( 'wp_ajax_merchant_notifications_read', array( $this, 'ajax_notifications_read' ) ); | |
| 61 | - } | |
| 62 | - | |
| 63 | - /** | |
| 64 | - * Include required classes. | |
| 65 | - */ | |
| 66 | - public function add_admin_menu() { | |
| 67 | - | |
| 68 | - global $submenu; | |
| 69 | - | |
| 70 | - // Dashboard | |
| 71 | - add_menu_page( | |
| 72 | - $this->page_title, | |
| 73 | - $this->page_title, | |
| 74 | - $this->capability, | |
| 75 | - $this->plugin_slug, | |
| 76 | - array( $this, 'page_dashboard' ), | |
| 77 | - MERCHANT_URI . 'assets/images/merchant-logo.svg', | |
| 78 | - $this->priority | |
| 79 | - ); | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * Get Notifications | |
| 84 | - */ | |
| 85 | - public function get_notifications() { | |
| 86 | - $notifications = get_transient( 'merchant_notifications' ); | |
| 87 | - | |
| 88 | - if ( ! empty( $notifications ) ) { | |
| 89 | - $this->notifications = $notifications; | |
| 90 | - } else { | |
| 91 | - | |
| 92 | - /** | |
| 93 | - * Hook: merchant_changelog_api_url | |
| 94 | - * | |
| 95 | - * @since 1.0 | |
| 96 | - */ | |
| 97 | - $response = wp_remote_get( apply_filters( 'merchant_changelog_api_url', 'https://athemes.com/wp-json/wp/v2/notifications?theme=7103&per_page=3' ) ); | |
| 98 | - | |
| 99 | - if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) { | |
| 100 | - $this->notifications = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 101 | - set_transient( 'merchant_notifications', $this->notifications, 24 * HOUR_IN_SECONDS ); | |
| 102 | - } | |
| 103 | - } | |
| 104 | - | |
| 105 | - return $this->notifications; | |
| 106 | - } | |
| 107 | - | |
| 108 | - /** | |
| 109 | - * Check if the latest notification is read | |
| 110 | - */ | |
| 111 | - public function is_latest_notification_read() { | |
| 112 | - | |
| 113 | - if ( ! isset( $this->notifications ) || empty( $this->notifications ) ) { | |
| 114 | - return false; | |
| 115 | - } | |
| 116 | - | |
| 117 | - $user_id = get_current_user_id(); | |
| 118 | - $user_read_meta = get_user_meta( $user_id, 'merchant_dashboard_notifications_latest_read', true ); | |
| 119 | - | |
| 120 | - $last_notification_date = strtotime( is_string( $this->notifications[0]->post_date ) ? $this->notifications[0]->post_date : '' ); | |
| 121 | - $last_notification_date_ondb = $user_read_meta ? strtotime( $user_read_meta ) : false; | |
| 122 | - | |
| 123 | - if ( ! $last_notification_date_ondb ) { | |
| 124 | - return false; | |
| 125 | - } | |
| 126 | - | |
| 127 | - if ( $last_notification_date > $last_notification_date_ondb ) { | |
| 128 | - return false; | |
| 129 | - } | |
| 130 | - | |
| 131 | - return true; | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * Ajax notifications. | |
| 136 | - */ | |
| 137 | - public function ajax_notifications_read() { | |
| 138 | - check_ajax_referer( 'merchant', 'nonce' ); | |
| 139 | - | |
| 140 | - // Check current user capabilities | |
| 141 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 142 | - wp_send_json_error( 'You are not allowed to do this.' ); | |
| 143 | - } | |
| 144 | - | |
| 145 | - $latest_notification_date = ( isset( $_POST[ 'latest_notification_date' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'latest_notification_date' ] ) ) : false; | |
| 146 | - | |
| 147 | - update_user_meta( get_current_user_id(), 'merchant_dashboard_notifications_latest_read', $latest_notification_date ); | |
| 148 | - | |
| 149 | - wp_send_json_success(); | |
| 150 | - } | |
| 151 | - | |
| 152 | - public function page_dashboard() { | |
| 153 | - require_once MERCHANT_DIR . 'admin/pages/page-dashboard.php'; | |
| 154 | - } | |
| 155 | - } | |
| 156 | - | |
| 157 | - Merchant_Admin_Menu::instance(); | |
| 158 | - | |
| 159 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Merchant_Admin_Menu Class. | |
| 4 | + */ | |
| 5 | + | |
| 6 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 7 | + exit; // Exit if accessed directly | |
| 8 | +} | |
| 9 | + | |
| 10 | +if ( ! class_exists( 'Merchant_Admin_Menu' ) ) { | |
| 11 | + | |
| 12 | + class Merchant_Admin_Menu { | |
| 13 | + public $page_title = 'Merchant'; | |
| 14 | + public $plugin_slug = 'merchant'; | |
| 15 | + public $capability = 'manage_options'; | |
| 16 | + public $priority = 58; | |
| 17 | + public $notifications = array(); | |
| 18 | + public static $installed_plugins = array(); | |
| 19 | + private static $instance = null; | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Instance. | |
| 23 | + * | |
| 24 | + */ | |
| 25 | + public static function instance() { | |
| 26 | + if ( is_null( self::$instance ) ) { | |
| 27 | + self::$instance = new self(); | |
| 28 | + } | |
| 29 | + return self::$instance; | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Constructor. | |
| 34 | + * | |
| 35 | + */ | |
| 36 | + public function __construct() { | |
| 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 | + | |
| 44 | + return; | |
| 45 | + } | |
| 46 | + | |
| 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' ) ); | |
| 51 | + add_action( 'wp_ajax_merchant_notifications_read', array( $this, 'ajax_notifications_read' ) ); | |
| 52 | + add_action('admin_footer', array( $this, 'footer_internal_scripts' )); | |
| 53 | + } | |
| 54 | + | |
| 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 | + /** | |
| 208 | + * Include required classes. | |
| 209 | + * | |
| 210 | + * @return void | |
| 211 | + */ | |
| 212 | + public function add_admin_menu() { | |
| 213 | + global $submenu; | |
| 214 | + | |
| 215 | + // Dashboard. | |
| 216 | + add_menu_page( | |
| 217 | + $this->page_title, | |
| 218 | + $this->page_title, | |
| 219 | + $this->capability, | |
| 220 | + $this->plugin_slug, | |
| 221 | + array( $this, 'page_dashboard' ), | |
| 222 | + MERCHANT_URI . 'assets/images/merchant-logo.svg', | |
| 223 | + $this->priority | |
| 224 | + ); | |
| 225 | + | |
| 226 | + // Dashboard Sub Item. | |
| 227 | + add_submenu_page( | |
| 228 | + $this->plugin_slug, | |
| 229 | + esc_html__('Dashboard', 'merchant'), | |
| 230 | + esc_html__('Dashboard', 'merchant'), | |
| 231 | + $this->capability, | |
| 232 | + $this->plugin_slug, | |
| 233 | + '', | |
| 234 | + 1 | |
| 235 | + ); | |
| 236 | + | |
| 237 | + // All Modules. | |
| 238 | + add_submenu_page( | |
| 239 | + $this->plugin_slug, | |
| 240 | + esc_html__('Modules', 'merchant'), | |
| 241 | + esc_html__('Modules', 'merchant'), | |
| 242 | + $this->capability, | |
| 243 | + 'admin.php?page=merchant§ion=modules', | |
| 244 | + '', | |
| 245 | + 2 | |
| 246 | + ); | |
| 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 | + | |
| 272 | + // Settings. | |
| 273 | + add_submenu_page( | |
| 274 | + $this->plugin_slug, | |
| 275 | + esc_html__('Settings', 'merchant'), | |
| 276 | + esc_html__('Settings', 'merchant'), | |
| 277 | + $this->capability, | |
| 278 | + 'admin.php?page=merchant§ion=settings', | |
| 279 | + '', | |
| 280 | + 5 | |
| 281 | + ); | |
| 282 | + | |
| 283 | + // Add 'Upgrade' link. | |
| 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 | + ); | |
| 294 | + add_submenu_page( | |
| 295 | + $this->plugin_slug, | |
| 296 | + esc_html__( 'Upgrade to Pro', 'merchant' ), | |
| 297 | + esc_html__( 'Upgrade to Pro', 'merchant' ), | |
| 298 | + $this->capability, | |
| 299 | + esc_url( $url ), | |
| 300 | + '', | |
| 301 | + 7 | |
| 302 | + ); | |
| 303 | + } | |
| 304 | + } | |
| 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 | + | |
| 408 | + /** | |
| 409 | + * Get Notifications | |
| 410 | + * | |
| 411 | + * @return array | |
| 412 | + */ | |
| 413 | + public function get_notifications() { | |
| 414 | + $notifications = get_transient( 'merchant_notifications' ); | |
| 415 | + | |
| 416 | + if ( ! empty( $notifications ) ) { | |
| 417 | + $this->notifications = $notifications; | |
| 418 | + } else { | |
| 419 | + | |
| 420 | + /** | |
| 421 | + * Hook: merchant_changelog_api_url | |
| 422 | + * | |
| 423 | + * @since 1.0 | |
| 424 | + */ | |
| 425 | + $response = wp_remote_get( apply_filters( 'merchant_changelog_api_url', 'https://athemes.com/wp-json/wp/v2/notifications?theme=7103&per_page=3' ) ); | |
| 426 | + | |
| 427 | + if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) { | |
| 428 | + $this->notifications = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 429 | + set_transient( 'merchant_notifications', $this->notifications, 24 * HOUR_IN_SECONDS ); | |
| 430 | + } | |
| 431 | + } | |
| 432 | + | |
| 433 | + return $this->notifications; | |
| 434 | + } | |
| 435 | + | |
| 436 | + /** | |
| 437 | + * Check if the latest notification is read | |
| 438 | + * | |
| 439 | + * @return bool | |
| 440 | + */ | |
| 441 | + public function is_latest_notification_read() { | |
| 442 | + | |
| 443 | + if ( ! isset( $this->notifications ) || empty( $this->notifications ) ) { | |
| 444 | + return false; | |
| 445 | + } | |
| 446 | + | |
| 447 | + $user_id = get_current_user_id(); | |
| 448 | + $user_read_meta = get_user_meta( $user_id, 'merchant_dashboard_notifications_latest_read', true ); | |
| 449 | + | |
| 450 | + $last_notification_date = strtotime( is_string( $this->notifications[0]->post_date ) ? $this->notifications[0]->post_date : '' ); | |
| 451 | + $last_notification_date_ondb = $user_read_meta ? strtotime( $user_read_meta ) : false; | |
| 452 | + | |
| 453 | + if ( ! $last_notification_date_ondb ) { | |
| 454 | + return false; | |
| 455 | + } | |
| 456 | + | |
| 457 | + if ( $last_notification_date > $last_notification_date_ondb ) { | |
| 458 | + return false; | |
| 459 | + } | |
| 460 | + | |
| 461 | + return true; | |
| 462 | + } | |
| 463 | + | |
| 464 | + /** | |
| 465 | + * Ajax notifications. | |
| 466 | + * | |
| 467 | + * @return void | |
| 468 | + */ | |
| 469 | + public function ajax_notifications_read() { | |
| 470 | + check_ajax_referer( 'merchant', 'nonce' ); | |
| 471 | + | |
| 472 | + // Check current user capabilities | |
| 473 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 474 | + wp_send_json_error( 'You are not allowed to do this.' ); | |
| 475 | + } | |
| 476 | + | |
| 477 | + $latest_notification_date = ( isset( $_POST[ 'latest_notification_date' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'latest_notification_date' ] ) ) : false; | |
| 478 | + | |
| 479 | + update_user_meta( get_current_user_id(), 'merchant_dashboard_notifications_latest_read', $latest_notification_date ); | |
| 480 | + | |
| 481 | + wp_send_json_success(); | |
| 482 | + } | |
| 483 | + | |
| 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 | + /** | |
| 501 | + * Include Page dashboard | |
| 502 | + * | |
| 503 | + * @return void | |
| 504 | + */ | |
| 505 | + public function page_dashboard() { | |
| 506 | + self::get_installed_plugins(); | |
| 507 | + require_once MERCHANT_DIR . 'admin/pages/page-dashboard.php'; | |
| 508 | + } | |
| 509 | + | |
| 510 | + /** | |
| 511 | + * Footer internal scripts | |
| 512 | + * | |
| 513 | + * @return void | |
| 514 | + */ | |
| 515 | + public function footer_internal_scripts() { | |
| 516 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 517 | + $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? str_replace( '/wp-admin/', '', $_SERVER['REQUEST_URI'] ) : ''; | |
| 518 | + ?> | |
| 519 | + <style> | |
| 520 | + #adminmenu .toplevel_page_merchant .wp-submenu li.current a { | |
| 521 | + color: rgba(240, 246, 252, 0.7); | |
| 522 | + font-weight: 400; | |
| 523 | + } | |
| 524 | + | |
| 525 | + #adminmenu .toplevel_page_merchant .wp-submenu li a[href="<?php echo $request_uri; //phpcs:ignore ?>"] { | |
| 526 | + color: #fff; | |
| 527 | + font-weight: 600; | |
| 528 | + } | |
| 529 | + | |
| 530 | + #adminmenu .toplevel_page_merchant .wp-submenu a[href="https://athemes.com/merchant-upgrade?utm_source=theme_submenu_page&utm_medium=button&utm_campaign=Merchant"] { | |
| 531 | + background-color: green; | |
| 532 | + color: #FFF; | |
| 533 | + } | |
| 534 | + </style> | |
| 535 | + <script type="text/javascript"> | |
| 536 | + document.addEventListener("DOMContentLoaded", function () { | |
| 537 | + const merchantUpsellMenuItem = document.querySelector('#adminmenu .toplevel_page_merchant .wp-submenu a[href="https://athemes.com/merchant-upgrade?utm_source=theme_submenu_page&utm_medium=button&utm_campaign=Merchant"]'); | |
| 538 | + | |
| 539 | + if (merchantUpsellMenuItem) { | |
| 540 | + merchantUpsellMenuItem.addEventListener('click', function (e) { | |
| 541 | + e.preventDefault(); | |
| 542 | + | |
| 543 | + const href = this.getAttribute('href'); | |
| 544 | + window.open(href, '_blank'); | |
| 545 | + }); | |
| 546 | + } | |
| 547 | + }); | |
| 548 | + </script> | |
| 549 | + <?php | |
| 550 | + } | |
| 551 | + } | |
| 552 | + | |
| 553 | + Merchant_Admin_Menu::instance(); | |
| 554 | + | |
| 555 | +} | |