PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.2.7
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.2.7
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / admin / classes / class-merchant-admin-menu.php

class-merchant-admin-menu.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.2.7, at admin/classes/class-merchant-admin-menu.php

584 lines 18.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ( defined( 'MERCHANT_AWL_ACTIVE' ) ) {
38 return;
39 }
40
41 if( $this->is_patcher_page() ) {
42 add_action('admin_enqueue_scripts', array( $this, 'enqueue_patcher_scripts' ));
43 }
44
45 add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
46 add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_menu' ), 100 );
47 add_action( 'admin_enqueue_scripts', array( $this, 'analytics_assets' ) );
48 add_action( 'wp_dashboard_setup', array( $this, 'dashboard_analytics_widget' ) );
49 add_action( 'wp_ajax_merchant_notifications_read', array( $this, 'ajax_notifications_read' ) );
50 add_action('admin_footer', array( $this, 'footer_internal_scripts' ));
51 }
52
53 /**
54 * Add dashboard widget.
55 *
56 * @return void
57 */
58 public function dashboard_analytics_widget() {
59 if( class_exists( 'WooCommerce' ) ) {
60 wp_add_dashboard_widget(
61 'merchant_modules_revenue', // Widget slug.
62 esc_html__( 'Daily added revenue by Merchant', 'merchant' ), // Title.
63 array( $this, 'dashboard_analytics_widget_content' ) // Display function.
64 );
65 }
66 }
67
68 /**
69 * Dashboard widget content.
70 *
71 * @return void
72 */
73 public function dashboard_analytics_widget_content() {
74 $reports = new Merchant_Analytics_Data_Reports();
75 $date_ranges = $reports->get_last_and_previous_7_days_ranges();
76 ?>
77 <div class="merchant-analytics-widget widget-chart-section">
78 <div class="chart" data-period="<?php
79 echo esc_attr( wp_json_encode( $reports->get_revenue_chart_report( $date_ranges['recent_period']['start'], $date_ranges['recent_period']['end'] ) ) )
80 ?>"></div>
81 <div class="foot">
82 <div class="date-range">
83 <span>
84 <input type="text" class="date-range-input" readonly value="<?php
85 echo esc_attr( implode( ' - ', array_values( $date_ranges['recent_period'] ) ) ) ?>" placeholder="<?php
86 esc_attr_e( 'Select date range', 'merchant' ); ?>">
87 </span>
88 <span class="merchant-analytics-loading-spinner"></span>
89 </div>
90 <div class="analytics-link">
91 <a href="<?php echo esc_url( admin_url( 'admin.php?page=merchant&section=analytics' ) ); ?>"><?php
92 esc_html_e( 'View Full Analytics', 'merchant' ); ?></a>
93 </div>
94 </div>
95 </div>
96 <?php
97 }
98
99 /**
100 * Enqueue assets for analytics page.
101 *
102 * @return void
103 */
104 public function analytics_assets( $hook ) {
105 global $pagenow;
106 $section = sanitize_text_field( $_GET['section'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
107
108 if (
109 ( $hook === 'toplevel_page_merchant' && $section !== 'settings' && class_exists( 'WooCommerce' ) )
110 || ( $pagenow === 'index.php' && class_exists( 'WooCommerce' ) )
111 ) {
112 wp_enqueue_style('date-picker', MERCHANT_URI . 'assets/vendor/air-datepicker/air-datepicker.css', array(), MERCHANT_VERSION, 'all' );
113 wp_enqueue_style( 'merchant-analytics', MERCHANT_URI . 'assets/css/admin/analytics.css', array(), MERCHANT_VERSION );
114 wp_enqueue_script('date-picker', MERCHANT_URI . 'assets/vendor/air-datepicker/air-datepicker.js', array( 'jquery' ), MERCHANT_VERSION, true );
115 wp_enqueue_script( 'apexcharts', MERCHANT_URI . 'assets/js/vendor/apexcharts.min.js', array( 'jquery' ), MERCHANT_VERSION, true );
116 wp_enqueue_script( 'merchant-analytics', MERCHANT_URI . 'assets/js/admin/analytics.js', array( 'jquery', 'apexcharts' ), MERCHANT_VERSION, true );
117 wp_localize_script( 'merchant-analytics', 'merchant_analytics', array(
118 'nonce' => wp_create_nonce( 'merchant' ),
119 'ajax_url' => admin_url( 'admin-ajax.php' ),
120 'currency_name' => get_woocommerce_currency(),
121 'currency_symbol' => get_woocommerce_currency_symbol(),
122 'labels' => array(
123 'orders' => esc_html__( 'orders', 'merchant' ),
124 'orders_aov' => esc_html__( 'Orders AOV', 'merchant' ),
125 ),
126 ) );
127
128 wp_localize_script( 'merchant-analytics', 'merchant_datepicker_locale', array(
129 wp_json_encode(
130 array(
131 'days' => array(
132 esc_html__( 'Sunday', 'merchant' ),
133 esc_html__( 'Monday', 'merchant' ),
134 esc_html__( 'Tuesday', 'merchant' ),
135 esc_html__( 'Wednesday', 'merchant' ),
136 esc_html__( 'Thursday', 'merchant' ),
137 esc_html__( 'Friday', 'merchant' ),
138 esc_html__( 'Saturday', 'merchant' ),
139 ),
140 'daysShort' => array(
141 esc_html__( 'Sun', 'merchant' ),
142 esc_html__( 'Mon', 'merchant' ),
143 esc_html__( 'Tue', 'merchant' ),
144 esc_html__( 'Wed', 'merchant' ),
145 esc_html__( 'Thu', 'merchant' ),
146 esc_html__( 'Fri', 'merchant' ),
147 esc_html__( 'Sat', 'merchant' ),
148 ),
149 'daysMin' => array(
150 esc_html__( 'Su', 'merchant' ),
151 esc_html__( 'Mo', 'merchant' ),
152 esc_html__( 'Tu', 'merchant' ),
153 esc_html__( 'We', 'merchant' ),
154 esc_html__( 'Th', 'merchant' ),
155 esc_html__( 'Fr', 'merchant' ),
156 esc_html__( 'Sa', 'merchant' ),
157 ),
158 'months' => array(
159 esc_html__( 'January', 'merchant' ),
160 esc_html__( 'February', 'merchant' ),
161 esc_html__( 'March', 'merchant' ),
162 esc_html__( 'April', 'merchant' ),
163 esc_html__( 'May', 'merchant' ),
164 esc_html__( 'June', 'merchant' ),
165 esc_html__( 'July', 'merchant' ),
166 esc_html__( 'August', 'merchant' ),
167 esc_html__( 'September', 'merchant' ),
168 esc_html__( 'October', 'merchant' ),
169 esc_html__( 'November', 'merchant' ),
170 esc_html__( 'December', 'merchant' ),
171 ),
172 'monthsShort' => array(
173 esc_html__( 'Jan', 'merchant' ),
174 esc_html__( 'Feb', 'merchant' ),
175 esc_html__( 'Mar', 'merchant' ),
176 esc_html__( 'Apr', 'merchant' ),
177 esc_html__( 'May', 'merchant' ),
178 esc_html__( 'Jun', 'merchant' ),
179 esc_html__( 'Jul', 'merchant' ),
180 esc_html__( 'Aug', 'merchant' ),
181 esc_html__( 'Sep', 'merchant' ),
182 esc_html__( 'Oct', 'merchant' ),
183 esc_html__( 'Nov', 'merchant' ),
184 esc_html__( 'Dec', 'merchant' ),
185 ),
186 'clear' => esc_html__( 'Clear', 'merchant' ),
187 )
188 ),
189 ) );
190 }
191 }
192
193 /**
194 * Is aThemes Patcher page.
195 *
196 * @return bool
197 */
198 public function is_patcher_page() {
199 global $pagenow;
200
201 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
202 return $pagenow === 'admin.php' && ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] === 'athemes-patcher-preview-mp' );
203 }
204
205 /**
206 * Enqueue aThemes Patcher preview scripts and styles.
207 *
208 * @return void
209 */
210 public function enqueue_patcher_scripts() {
211 wp_enqueue_style( 'wp-components' );
212 }
213
214 /**
215 * Include required classes.
216 *
217 * @return void
218 */
219 public function add_admin_menu() {
220 global $submenu;
221
222 // Dashboard.
223 add_menu_page(
224 $this->page_title,
225 $this->page_title,
226 $this->capability,
227 $this->plugin_slug,
228 array( $this, 'page_dashboard' ),
229 MERCHANT_URI . 'assets/images/merchant-logo.svg',
230 $this->priority
231 );
232
233 // Dashboard Sub Item.
234 add_submenu_page(
235 $this->plugin_slug,
236 esc_html__('Dashboard', 'merchant'),
237 esc_html__('Dashboard', 'merchant'),
238 $this->capability,
239 $this->plugin_slug,
240 '',
241 1
242 );
243
244 // All Modules.
245 add_submenu_page(
246 $this->plugin_slug,
247 esc_html__('Modules', 'merchant'),
248 esc_html__('Modules', 'merchant'),
249 $this->capability,
250 'admin.php?page=merchant&section=modules',
251 '',
252 2
253 );
254
255 if( class_exists( 'WooCommerce' ) ) {
256 // Campaigns.
257 add_submenu_page(
258 $this->plugin_slug,
259 esc_html__( 'Campaigns', 'merchant' ),
260 esc_html__( 'Campaigns', 'merchant' ),
261 $this->capability,
262 'admin.php?page=merchant&section=campaigns',
263 '',
264 3
265 );
266
267 // Analytics.
268 add_submenu_page(
269 $this->plugin_slug,
270 esc_html__( 'Analytics', 'merchant' ),
271 esc_html__( 'Analytics', 'merchant' ),
272 $this->capability,
273 'admin.php?page=merchant&section=analytics',
274 '',
275 4
276 );
277 }
278
279 // Settings.
280 add_submenu_page(
281 $this->plugin_slug,
282 esc_html__('Settings', 'merchant'),
283 esc_html__('Settings', 'merchant'),
284 $this->capability,
285 'admin.php?page=merchant&section=settings',
286 '',
287 5
288 );
289
290 // Add 'aThemes Patcher' link
291 add_submenu_page( // phpcs:ignore WPThemeReview.PluginTerritory.NoAddAdminPages.add_menu_pages_add_submenu_page
292 'merchant',
293 esc_html__('Patcher', 'merchant'),
294 esc_html__('Patcher', 'merchant'),
295 $this->capability,
296 'athemes-patcher-preview-mp',
297 array( $this, 'html_patcher' ),
298 6
299 );
300
301
302 // Add 'Upgrade' link.
303 if ( ! defined( 'MERCHANT_PRO_VERSION' ) ) {
304 $url = merchant_admin_upgrade_link(
305 'https://athemes.com/merchant-upgrade',
306 array(
307 'utm_source' => 'theme_submenu_page',
308 'utm_medium' => 'button',
309 'utm_campaign' => 'Merchant',
310 ),
311 'theme-submenu-page-upgrade-link'
312 );
313 add_submenu_page(
314 $this->plugin_slug,
315 esc_html__( 'Upgrade to Pro', 'merchant' ),
316 esc_html__( 'Upgrade to Pro', 'merchant' ),
317 $this->capability,
318 esc_url( $url ),
319 '',
320 7
321 );
322 }
323 }
324
325 /**
326 * Add admin bar menu.
327 *
328 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
329 */
330 public function add_admin_bar_menu( $wp_admin_bar ) {
331 // Check if the current user has the capability to manage options
332 if ( ! current_user_can( 'manage_options' ) ) {
333 return;
334 }
335
336 // Parent menu item (Dashboard)
337 $wp_admin_bar->add_node( array(
338 'id' => 'merchant-dashboard',
339 'title' => esc_html__( 'Merchant', 'merchant' ), // Use your plugin name or page title
340 'href' => admin_url( 'admin.php?page=merchant' ), // Link to the main dashboard
341 'meta' => array(
342 'title' => esc_html__( 'Merchant Dashboard', 'merchant' ),
343 ),
344 ) );
345
346 // Dashboard Sub Item
347 $wp_admin_bar->add_node( array(
348 'id' => 'merchant-dashboard-sub',
349 'parent' => 'merchant-dashboard',
350 'title' => esc_html__( 'Dashboard', 'merchant' ),
351 'href' => admin_url( 'admin.php?page=merchant' ),
352 'meta' => array(
353 'title' => esc_html__( 'Dashboard', 'merchant' ),
354 ),
355 ) );
356
357 // All Modules
358 $wp_admin_bar->add_node( array(
359 'id' => 'merchant-modules',
360 'parent' => 'merchant-dashboard',
361 'title' => esc_html__( 'Modules', 'merchant' ),
362 'href' => admin_url( 'admin.php?page=merchant&section=modules' ),
363 'meta' => array(
364 'title' => esc_html__( 'Modules', 'merchant' ),
365 ),
366 ) );
367
368 if( class_exists( 'WooCommerce' ) ) {
369 // Settings
370 $wp_admin_bar->add_node( array(
371 'id' => 'merchant-settings',
372 'parent' => 'merchant-dashboard',
373 'title' => esc_html__( 'Settings', 'merchant' ),
374 'href' => admin_url( 'admin.php?page=merchant&section=settings' ),
375 'meta' => array(
376 'title' => esc_html__( 'Settings', 'merchant' ),
377 ),
378 ) );
379
380 // Campaigns
381 $wp_admin_bar->add_node( array(
382 'id' => 'merchant-campaigns',
383 'parent' => 'merchant-dashboard',
384 'title' => esc_html__( 'Campaigns', 'merchant' ),
385 'href' => admin_url( 'admin.php?page=merchant&section=campaigns' ),
386 'meta' => array(
387 'title' => esc_html__( 'Campaigns', 'merchant' ),
388 ),
389 ) );
390 }
391
392 // Analytics
393 $wp_admin_bar->add_node( array(
394 'id' => 'merchant-analytics',
395 'parent' => 'merchant-dashboard',
396 'title' => esc_html__( 'Analytics', 'merchant' ),
397 'href' => admin_url( 'admin.php?page=merchant&section=analytics' ),
398 'meta' => array(
399 'title' => esc_html__( 'Analytics', 'merchant' ),
400 ),
401 ) );
402
403 // Upgrade to Pro (if not already defined)
404 if ( ! defined( 'MERCHANT_PRO_VERSION' ) ) {
405 $url = merchant_admin_upgrade_link(
406 'https://athemes.com/merchant-upgrade',
407 array(
408 'utm_source' => 'theme_submenu_page',
409 'utm_medium' => 'button',
410 'utm_campaign' => 'Merchant',
411 ),
412 'adminbar-submenu-page-upgrade-link'
413 );
414 $wp_admin_bar->add_node( array(
415 'id' => 'merchant-upgrade',
416 'parent' => 'merchant-dashboard',
417 'title' => esc_html__( 'Upgrade to Pro', 'merchant' ),
418 'href' => esc_url( $url ),
419 'meta' => array(
420 'title' => esc_html__( 'Upgrade to Pro', 'merchant' ),
421 'target' => '_blank', // Open link in a new tab
422 ),
423 ) );
424 }
425 }
426
427 /**
428 * Get Notifications
429 *
430 * @return array
431 */
432 public function get_notifications() {
433 $notifications = get_transient( 'merchant_notifications' );
434
435 if ( ! empty( $notifications ) ) {
436 $this->notifications = $notifications;
437 } else {
438
439 /**
440 * Hook: merchant_changelog_api_url
441 *
442 * @since 1.0
443 */
444 $response = wp_remote_get( apply_filters( 'merchant_changelog_api_url', 'https://athemes.com/wp-json/wp/v2/notifications?theme=7103&per_page=3' ) );
445
446 if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) {
447 $this->notifications = json_decode( wp_remote_retrieve_body( $response ) );
448 set_transient( 'merchant_notifications', $this->notifications, 24 * HOUR_IN_SECONDS );
449 }
450 }
451
452 return $this->notifications;
453 }
454
455 /**
456 * Check if the latest notification is read
457 *
458 * @return bool
459 */
460 public function is_latest_notification_read() {
461
462 if ( ! isset( $this->notifications ) || empty( $this->notifications ) ) {
463 return false;
464 }
465
466 $user_id = get_current_user_id();
467 $user_read_meta = get_user_meta( $user_id, 'merchant_dashboard_notifications_latest_read', true );
468
469 $last_notification_date = strtotime( is_string( $this->notifications[0]->post_date ) ? $this->notifications[0]->post_date : '' );
470 $last_notification_date_ondb = $user_read_meta ? strtotime( $user_read_meta ) : false;
471
472 if ( ! $last_notification_date_ondb ) {
473 return false;
474 }
475
476 if ( $last_notification_date > $last_notification_date_ondb ) {
477 return false;
478 }
479
480 return true;
481 }
482
483 /**
484 * Ajax notifications.
485 *
486 * @return void
487 */
488 public function ajax_notifications_read() {
489 check_ajax_referer( 'merchant', 'nonce' );
490
491 // Check current user capabilities
492 if ( ! current_user_can( 'manage_options' ) ) {
493 wp_send_json_error( 'You are not allowed to do this.' );
494 }
495
496 $latest_notification_date = ( isset( $_POST[ 'latest_notification_date' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'latest_notification_date' ] ) ) : false;
497
498 update_user_meta( get_current_user_id(), 'merchant_dashboard_notifications_latest_read', $latest_notification_date );
499
500 wp_send_json_success();
501 }
502
503 /**
504 * Get installed plugins
505 * Provide the installed plugins array into the installed plugins static property for memoization purposes.
506 *
507 * @return array
508 */
509 public static function get_installed_plugins() {
510 if ( empty( self::$installed_plugins ) ) {
511 if ( ! function_exists( 'get_plugins' ) ) {
512 require_once ABSPATH . 'wp-admin/includes/plugin.php';
513 }
514 self::$installed_plugins = get_plugins();
515 }
516 return self::$installed_plugins;
517 }
518
519 /**
520 * Include Page dashboard
521 *
522 * @return void
523 */
524 public function page_dashboard() {
525 self::get_installed_plugins();
526 require_once MERCHANT_DIR . 'admin/pages/page-dashboard.php';
527 }
528
529 /**
530 * Footer internal scripts
531 *
532 * @return void
533 */
534 public function footer_internal_scripts() {
535 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
536 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? str_replace( '/wp-admin/', '', $_SERVER['REQUEST_URI'] ) : '';
537 ?>
538 <style>
539 #adminmenu .toplevel_page_merchant .wp-submenu li.current a {
540 color: rgba(240, 246, 252, 0.7);
541 font-weight: 400;
542 }
543
544 #adminmenu .toplevel_page_merchant .wp-submenu li a[href="<?php echo $request_uri; //phpcs:ignore ?>"] {
545 color: #fff;
546 font-weight: 600;
547 }
548
549 #adminmenu .toplevel_page_merchant .wp-submenu a[href="https://athemes.com/merchant-upgrade?utm_source=theme_submenu_page&utm_medium=button&utm_campaign=Merchant"] {
550 background-color: green;
551 color: #FFF;
552 }
553 </style>
554 <script type="text/javascript">
555 document.addEventListener("DOMContentLoaded", function () {
556 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"]');
557
558 if (merchantUpsellMenuItem) {
559 merchantUpsellMenuItem.addEventListener('click', function (e) {
560 e.preventDefault();
561
562 const href = this.getAttribute('href');
563 window.open(href, '_blank');
564 });
565 }
566 });
567 </script>
568 <?php
569 }
570
571 /**
572 * HTML aThemes Patcher.
573 *
574 * @return void
575 */
576 public function html_patcher() {
577 require_once MERCHANT_DIR . 'admin/pages/page-patcher.php';
578 }
579 }
580
581 Merchant_Admin_Menu::instance();
582
583 }
584