| 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 |
private static $instance = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* Instance. |
| 22 |
* |
| 23 |
*/ |
| 24 |
public static function instance() { |
| 25 |
if ( is_null( self::$instance ) ) { |
| 26 |
self::$instance = new self(); |
| 27 |
} |
| 28 |
return self::$instance; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
if ( defined( 'MERCHANT_AWL_ACTIVE' ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
| 41 |
add_action( 'wp_ajax_merchant_notifications_read', array( $this, 'ajax_notifications_read' ) ); |
| 42 |
add_action('admin_footer', array( $this, 'footer_internal_scripts' )); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Include required classes. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function add_admin_menu() { |
| 51 |
global $submenu; |
| 52 |
|
| 53 |
// Dashboard. |
| 54 |
add_menu_page( |
| 55 |
$this->page_title, |
| 56 |
$this->page_title, |
| 57 |
$this->capability, |
| 58 |
$this->plugin_slug, |
| 59 |
array( $this, 'page_dashboard' ), |
| 60 |
MERCHANT_URI . 'assets/images/merchant-logo.svg', |
| 61 |
$this->priority |
| 62 |
); |
| 63 |
|
| 64 |
// Dashboard Sub Item. |
| 65 |
add_submenu_page( |
| 66 |
$this->plugin_slug, |
| 67 |
esc_html__('Dashboard', 'merchant'), |
| 68 |
esc_html__('Dashboard', 'merchant'), |
| 69 |
'manage_options', |
| 70 |
$this->plugin_slug, |
| 71 |
'', |
| 72 |
1 |
| 73 |
); |
| 74 |
|
| 75 |
// Enabled Modules. |
| 76 |
add_submenu_page( |
| 77 |
$this->plugin_slug, |
| 78 |
esc_html__('Enabled Modules', 'merchant'), |
| 79 |
esc_html__('Enabled Modules', 'merchant'), |
| 80 |
'manage_options', |
| 81 |
'admin.php?page=merchant§ion=modules', |
| 82 |
'', |
| 83 |
2 |
| 84 |
); |
| 85 |
|
| 86 |
// Settings. |
| 87 |
add_submenu_page( |
| 88 |
$this->plugin_slug, |
| 89 |
esc_html__('Settings', 'merchant'), |
| 90 |
esc_html__('Settings', 'merchant'), |
| 91 |
'manage_options', |
| 92 |
'admin.php?page=merchant§ion=settings', |
| 93 |
'', |
| 94 |
3 |
| 95 |
); |
| 96 |
|
| 97 |
// Add 'Upgrade' link. |
| 98 |
if( ! defined( 'MERCHANT_PRO_VERSION' ) ) { |
| 99 |
add_submenu_page( |
| 100 |
$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', |
| 105 |
'', |
| 106 |
4 |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get Notifications |
| 113 |
* |
| 114 |
* @return array |
| 115 |
*/ |
| 116 |
public function get_notifications() { |
| 117 |
$notifications = get_transient( 'merchant_notifications' ); |
| 118 |
|
| 119 |
if ( ! empty( $notifications ) ) { |
| 120 |
$this->notifications = $notifications; |
| 121 |
} else { |
| 122 |
|
| 123 |
/** |
| 124 |
* Hook: merchant_changelog_api_url |
| 125 |
* |
| 126 |
* @since 1.0 |
| 127 |
*/ |
| 128 |
$response = wp_remote_get( apply_filters( 'merchant_changelog_api_url', 'https://athemes.com/wp-json/wp/v2/notifications?theme=7103&per_page=3' ) ); |
| 129 |
|
| 130 |
if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) { |
| 131 |
$this->notifications = json_decode( wp_remote_retrieve_body( $response ) ); |
| 132 |
set_transient( 'merchant_notifications', $this->notifications, 24 * HOUR_IN_SECONDS ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $this->notifications; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Check if the latest notification is read |
| 141 |
* |
| 142 |
* @return bool |
| 143 |
*/ |
| 144 |
public function is_latest_notification_read() { |
| 145 |
|
| 146 |
if ( ! isset( $this->notifications ) || empty( $this->notifications ) ) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
$user_id = get_current_user_id(); |
| 151 |
$user_read_meta = get_user_meta( $user_id, 'merchant_dashboard_notifications_latest_read', true ); |
| 152 |
|
| 153 |
$last_notification_date = strtotime( is_string( $this->notifications[0]->post_date ) ? $this->notifications[0]->post_date : '' ); |
| 154 |
$last_notification_date_ondb = $user_read_meta ? strtotime( $user_read_meta ) : false; |
| 155 |
|
| 156 |
if ( ! $last_notification_date_ondb ) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
if ( $last_notification_date > $last_notification_date_ondb ) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Ajax notifications. |
| 169 |
* |
| 170 |
* @return void |
| 171 |
*/ |
| 172 |
public function ajax_notifications_read() { |
| 173 |
check_ajax_referer( 'merchant', 'nonce' ); |
| 174 |
|
| 175 |
// Check current user capabilities |
| 176 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 177 |
wp_send_json_error( 'You are not allowed to do this.' ); |
| 178 |
} |
| 179 |
|
| 180 |
$latest_notification_date = ( isset( $_POST[ 'latest_notification_date' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'latest_notification_date' ] ) ) : false; |
| 181 |
|
| 182 |
update_user_meta( get_current_user_id(), 'merchant_dashboard_notifications_latest_read', $latest_notification_date ); |
| 183 |
|
| 184 |
wp_send_json_success(); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Include Page dashboard |
| 189 |
* |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
public function page_dashboard() { |
| 193 |
require_once MERCHANT_DIR . 'admin/pages/page-dashboard.php'; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Footer internal scripts |
| 198 |
* |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function footer_internal_scripts() { |
| 202 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 203 |
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? str_replace( '/wp-admin/', '', $_SERVER['REQUEST_URI'] ) : ''; |
| 204 |
?> |
| 205 |
<style> |
| 206 |
#adminmenu .toplevel_page_merchant .wp-submenu li.current a { |
| 207 |
color: rgba(240, 246, 252, 0.7); |
| 208 |
font-weight: 400; |
| 209 |
} |
| 210 |
|
| 211 |
#adminmenu .toplevel_page_merchant .wp-submenu li a[href="<?php echo $request_uri; //phpcs:ignore ?>"] { |
| 212 |
color: #fff; |
| 213 |
font-weight: 600; |
| 214 |
} |
| 215 |
|
| 216 |
#adminmenu .toplevel_page_merchant .wp-submenu a[href="https://athemes.com/merchant-upgrade?utm_source=theme_submenu_page&utm_medium=button&utm_campaign=Merchant"] { |
| 217 |
background-color: green; |
| 218 |
color: #FFF; |
| 219 |
} |
| 220 |
</style> |
| 221 |
<script type="text/javascript"> |
| 222 |
document.addEventListener("DOMContentLoaded", function () { |
| 223 |
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"]'); |
| 224 |
|
| 225 |
if (merchantUpsellMenuItem) { |
| 226 |
merchantUpsellMenuItem.addEventListener('click', function (e) { |
| 227 |
e.preventDefault(); |
| 228 |
|
| 229 |
const href = this.getAttribute('href'); |
| 230 |
window.open(href, '_blank'); |
| 231 |
}); |
| 232 |
} |
| 233 |
}); |
| 234 |
</script> |
| 235 |
<?php |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
Merchant_Admin_Menu::instance(); |
| 240 |
|
| 241 |
} |
| 242 |
|