| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class used for the admin menu and to enqueue styles and/or scripts. |
| 10 |
*/ |
| 11 |
class P_Admin_Menu extends P_Core { |
| 12 |
|
| 13 |
/** |
| 14 |
* Add the actions required for the admin menu. |
| 15 |
* |
| 16 |
* @param Patchstack $core |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function __construct( $core ) { |
| 20 |
parent::__construct( $core ); |
| 21 |
|
| 22 |
if ( defined( 'PS_DISABLE_MENU' ) && PS_DISABLE_MENU ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
add_action( 'admin_head', [ $this, 'add_meta_nonce' ] ); |
| 27 |
add_action( 'admin_menu', [ $this, 'add_menu_pages' ] ); |
| 28 |
add_action( 'network_admin_menu', [ $this, 'network_menu' ] ); |
| 29 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] ); |
| 30 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 31 |
add_filter( 'plugin_action_links_' . $this->plugin->basename, [ $this, 'admin_settings' ] ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Add Patchstack nonce as meta tag so we can access it in our JavaScript files. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function add_meta_nonce() { |
| 40 |
$screen = get_current_screen(); |
| 41 |
if ( current_user_can( 'manage_options' ) && isset( $screen->base ) && stripos( $screen->base, 'patchstack' ) !== false ) { |
| 42 |
echo '<meta name="patchstack_nonce" value="' . wp_create_nonce( 'patchstack-nonce' ) . '">'; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Register the wp-admin menu items. |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function add_menu_pages() { |
| 52 |
add_submenu_page( $this->plugin->name, 'Main', esc_attr__( 'Settings', 'patchstack' ), 'manage_options', $this->plugin->name, [ $this, 'render_settings_page' ] ); |
| 53 |
add_options_page( 'Security', 'Security', 'manage_options', $this->plugin->name ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Register the menu pages for multisite/networks. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function network_menu() { |
| 62 |
add_menu_page( 'Patchstack', 'Patchstack', 'manage_options', 'patchstack-multisite', [ $this->plugin->multisite, 'sites_section_callback' ] ); |
| 63 |
add_submenu_page( 'patchstack-multisite', 'Activate', 'Activate', 'manage_options', 'patchstack-multisite-settings', [ $this, 'render_settings_page' ] ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Render the settings page. |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function render_settings_page() { |
| 72 |
require dirname( __FILE__ ) . '/../views/pages/settings.php'; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Register the stylesheets for the backend. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function enqueue_styles() { |
| 81 |
$screen = get_current_screen(); |
| 82 |
|
| 83 |
// Load the Patchstack style on all Patchstack pages except site overview. |
| 84 |
if ( isset( $screen->base, $_GET['page'] ) && stripos( $screen->base, 'patchstack' ) !== false && $_GET['page'] != 'patchstack-multisite' ) { |
| 85 |
wp_enqueue_style( 'patchstack', $this->plugin->url . 'assets/css/patchstack.min.css', [], $this->plugin->version ); |
| 86 |
wp_enqueue_style( 'patchstack-hint', 'https://cdnjs.cloudflare.com/ajax/libs/hint.css/3.0.0/hint.min.css', [], $this->plugin->version ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Register the JavaScript for the admin area. |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function enqueue_scripts() { |
| 96 |
$screen = get_current_screen(); |
| 97 |
if ( isset( $screen->base ) && ( stripos( $screen->base, 'patchstack' ) !== false ) && current_user_can( 'manage_options' ) ) { |
| 98 |
wp_enqueue_script( 'patchstack', $this->plugin->url . 'assets/js/patchstack.min.js', [ 'jquery' ], $this->plugin->version ); |
| 99 |
wp_enqueue_script( 'google-jsapi', 'https://www.google.com/jsapi' ); |
| 100 |
wp_localize_script( |
| 101 |
'patchstack', |
| 102 |
'PatchstackVars', |
| 103 |
[ |
| 104 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 105 |
'nonce' => wp_create_nonce( 'patchstack-nonce' ), |
| 106 |
'error_message' => esc_attr__( 'Sorry, there was a problem processing your request.', 'patchstack' ), |
| 107 |
] |
| 108 |
); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Add the "Settings" hyperlink to the Patchstack section on the plugins page of WordPress. |
| 114 |
* |
| 115 |
* @param array $links |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function admin_settings( $links ) { |
| 119 |
return array_merge( [ '<a href="admin.php?page=patchstack&tab=license">' . esc_attr__( 'Settings', 'patchstack' ) . '</a>' ], $links ); |
| 120 |
} |
| 121 |
} |
| 122 |
|