| 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 |
/** |
| 65 |
* Include required classes. |
| 66 |
*/ |
| 67 |
public function add_admin_menu() { |
| 68 |
|
| 69 |
global $submenu; |
| 70 |
|
| 71 |
// Dashboard |
| 72 |
add_menu_page( |
| 73 |
$this->page_title, |
| 74 |
$this->page_title, |
| 75 |
$this->capability, |
| 76 |
$this->plugin_slug, |
| 77 |
array( $this, 'page_dashboard' ), |
| 78 |
MERCHANT_URI . 'assets/images/merchant-logo.svg', |
| 79 |
$this->priority |
| 80 |
); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get Notifications |
| 86 |
*/ |
| 87 |
public function get_notifications() { |
| 88 |
$notifications = get_transient( 'merchant_notifications' ); |
| 89 |
|
| 90 |
if ( ! empty( $notifications ) ) { |
| 91 |
$this->notifications = $notifications; |
| 92 |
} else { |
| 93 |
|
| 94 |
/** |
| 95 |
* Hook: merchant_changelog_api_url |
| 96 |
* |
| 97 |
* @since 1.0 |
| 98 |
*/ |
| 99 |
$response = wp_remote_get( apply_filters( 'merchant_changelog_api_url', 'https://athemes.com/wp-json/wp/v2/notifications?theme=7103&per_page=3' ) ); |
| 100 |
|
| 101 |
if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) { |
| 102 |
$this->notifications = json_decode( wp_remote_retrieve_body( $response ) ); |
| 103 |
set_transient( 'merchant_notifications', $this->notifications, 24 * HOUR_IN_SECONDS ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return $this->notifications; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Check if the latest notification is read |
| 112 |
*/ |
| 113 |
public function is_latest_notification_read() { |
| 114 |
|
| 115 |
if ( ! isset( $this->notifications ) || empty( $this->notifications ) ) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
$user_id = get_current_user_id(); |
| 120 |
$user_read_meta = get_user_meta( $user_id, 'merchant_dashboard_notifications_latest_read', true ); |
| 121 |
|
| 122 |
$last_notification_date = strtotime( is_string( $this->notifications[0]->post_date ) ? $this->notifications[0]->post_date : '' ); |
| 123 |
$last_notification_date_ondb = $user_read_meta ? strtotime( $user_read_meta ) : false; |
| 124 |
|
| 125 |
if ( ! $last_notification_date_ondb ) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
if ( $last_notification_date > $last_notification_date_ondb ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
return true; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Ajax notifications. |
| 139 |
*/ |
| 140 |
public function ajax_notifications_read() { |
| 141 |
check_ajax_referer( 'merchant', 'nonce' ); |
| 142 |
|
| 143 |
// Check current user capabilities |
| 144 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 145 |
wp_send_json_error( 'You are not allowed to do this.' ); |
| 146 |
} |
| 147 |
|
| 148 |
$latest_notification_date = ( isset( $_POST[ 'latest_notification_date' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'latest_notification_date' ] ) ) : false; |
| 149 |
|
| 150 |
update_user_meta( get_current_user_id(), 'merchant_dashboard_notifications_latest_read', $latest_notification_date ); |
| 151 |
|
| 152 |
wp_send_json_success(); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
public function page_dashboard() { |
| 157 |
require_once MERCHANT_DIR . 'admin/pages/page-dashboard.php'; |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
Merchant_Admin_Menu::instance(); |
| 163 |
|
| 164 |
} |
| 165 |
|