*/
namespace RankMath\Admin;
use RankMath\KB;
use RankMath\Helper;
use RankMath\Helpers\Param;
use RankMath\Runner;
use RankMath\Traits\Hooker;
use RankMath\Traits\Ajax;
use RankMath\Admin\Page;
defined( 'ABSPATH' ) || exit;
/**
* Admin_Menu class.
*
* @codeCoverageIgnore
*/
class Admin_Menu implements Runner {
use Hooker, Ajax;
/**
* Register hooks.
*/
public function hooks() {
$this->action( 'init', 'register_pages' );
$this->action( 'admin_menu', 'fix_admin_menu', 999 );
$this->action( 'admin_head', 'icon_css' );
$this->ajax( 'remove_offer_page', 'remove_offer_page' );
}
/**
* Register admin pages for plugin.
*/
public function register_pages() {
$this->maybe_deregister();
if ( Helper::is_invalid_registration() && ! is_network_admin() ) {
return;
}
$current_user = wp_get_current_user();
$capabilities = array_keys( Helper::get_roles_capabilities() );
if ( empty( array_intersect( $current_user->roles, $capabilities ) ) && ! current_user_can( 'setup_network' ) ) {
return;
}
// Dashboard / Welcome / About.
new Page(
'rank-math',
esc_html__( 'Rank Math', 'rank-math' ),
[
'position' => 50,
'menu_title' => 'Rank Math',
'capability' => 'level_1',
'icon' => 'data:image/svg+xml;base64,' . \base64_encode( '' ),
'render' => Admin_Helper::get_view( 'dashboard' ),
'classes' => [ 'rank-math-page' ],
'assets' => [
'styles' => [ 'rank-math-dashboard' => '' ],
'scripts' => [
'rank-math-dashboard' => '',
],
],
'is_network' => is_network_admin() && Helper::is_plugin_active_for_network(),
]
);
}
/**
* Fix menu names.
*/
public function fix_admin_menu() {
// Replace the main menu name "Rank Math" with "Rank Math SEO".
global $menu;
foreach ( $menu as $key => $item ) {
if ( 'Rank Math' === $item[0] ) {
$menu[ $key ][0] = esc_html__( 'Rank Math SEO', 'rank-math' );
break;
}
}
// Replace the first submenu name "Rank Math" with "Dashboard".
global $submenu;
if ( ! isset( $submenu['rank-math'] ) ) {
return;
}
if ( 'Rank Math' === $submenu['rank-math'][0][0] ) {
if ( current_user_can( 'manage_options' ) ) {
$plan = Helper::get_content_ai_plan();
$notification = ( empty( $plan ) || 'free' === $plan ) && get_option( 'rank_math_view_modules' ) ? ' 1' : '';
$submenu['rank-math'][0][0] = esc_html__( 'Dashboard', 'rank-math' ) . $notification;
} else {
unset( $submenu['rank-math'][0] );
}
}
if ( empty( $submenu['rank-math'] ) ) {
return;
}
$submenu['rank-math'][] = [ esc_html__( 'Help & Support', 'rank-math' ) . '', 'level_1', KB::get( 'knowledgebase', 'Sidebar Help Link' ) ];
$this->add_offer_link( $submenu );
// Store ID of first_menu item so we can use it in the Admin menu item.
set_transient( 'rank_math_first_submenu_id', array_values( $submenu['rank-math'] )[0][2] );
}
/**
* Print icon CSS for admin menu bar.
*/
public function icon_css() {
?>
get_active_offer();
if ( ! $offer ) {
return;
}
$submenu['rank-math'][] = [ current( $offer ) . ' ', 'level_1', KB::get( 'offer', 'Offer Menu Item' ) ];
}
/**
* Ajax handler callback to store active offer so it doesn't show up again on the site.
*/
public function remove_offer_page() {
check_ajax_referer( 'rank-math-ajax-nonce', 'security' );
$offer = $this->get_active_offer();
set_site_transient( 'rank_math_active_offer', key( $offer ) );
}
/**
* Function to get active offer
*/
private function get_active_offer() {
// Early Bail if PRO plugin is active.
if ( defined( 'RANK_MATH_PRO_FILE' ) ) {
return false;
}
$timezone = new \DateTimeZone( 'Asia/Kolkata' );
$current_date = new \DateTime( 'now', $timezone );
$dates = [
'christmas' => [
'start' => '2023-12-17',
'end' => '2023-12-26',
'text' => esc_html__( 'Christmas Sale', 'rank-math' ),
],
'new-year' => [
'start' => '2023-12-31',
'end' => '2024-01-05',
'text' => esc_html__( 'New Year Sale', 'rank-math' ),
],
'anniversary' => [
'start' => '2024-11-06',
'end' => '2024-11-13',
'text' => esc_html__( 'Anniversary Sale', 'rank-math' ),
],
'black-friday' => [
'start' => '2024-11-27',
'end' => '2024-12-01',
'text' => esc_html__( 'Black Friday Sale', 'rank-math' ),
],
'cyber-monday' => [
'start' => '2024-12-02',
'end' => '2024-12-04',
'text' => esc_html__( 'Cyber Monday Sale', 'rank-math' ),
],
];
$stored_offer = get_site_transient( 'rank_math_active_offer' );
$active_offer = '';
foreach ( $dates as $key => $date ) {
$start_date = new \DateTime( $date['start'] . ' 16:00:00', $timezone );
$end_date = new \DateTime( $date['end'] . ' 16:00:00', $timezone );
if ( $stored_offer !== $key && $current_date >= $start_date && $current_date <= $end_date ) {
$active_offer = [ $key => $date['text'] ];
break;
}
}
return $active_offer;
}
}