| 1 |
<?php |
| 2 |
|
| 3 |
class SGPMMenu |
| 4 |
{ |
| 5 |
/** |
| 6 |
* Reference to base plugin class. |
| 7 |
* |
| 8 |
* @var SGPMBase |
| 9 |
*/ |
| 10 |
protected $base; |
| 11 |
|
| 12 |
/** |
| 13 |
* Admin page hook identifier. |
| 14 |
* |
| 15 |
* @var string|null |
| 16 |
*/ |
| 17 |
protected $hook; |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$this->set(); |
| 22 |
|
| 23 |
add_action('admin_menu', array($this, 'menu')); |
| 24 |
add_action('admin_post_sgpm_options_save', array(new SGPMPage, 'optionsSave')); |
| 25 |
add_action('admin_post_sgpm_general_settings_save', array(new SGPMPage, 'generalSettingsSave')); |
| 26 |
add_action('wp_ajax_sgpm_change_popup_status', array(new SGPMPage, 'changePopupStatus')); |
| 27 |
add_action('wp_ajax_sgpm_add_condition_rule_row', array(new SGPMPage, 'addConditionRuleRow')); |
| 28 |
add_action('wp_ajax_sgpm_change_condition_rule_row', array(new SGPMPage, 'changeConditionRuleRow')); |
| 29 |
add_action('wp_ajax_sgpm_select2_search_data', array(new SGPMPage, 'select2SearchData')); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Sets our object instance and base class instance. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
*/ |
| 37 |
public function set() |
| 38 |
{ |
| 39 |
$this->base = SGPMBase::getInstance(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Loads assets. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
*/ |
| 47 |
public function assets() |
| 48 |
{ |
| 49 |
add_action('admin_enqueue_scripts', array($this, 'styles')); |
| 50 |
add_action('admin_enqueue_scripts', array($this, 'scripts')); |
| 51 |
add_action('in_admin_header', array($this, 'outputPluginScreenBanner')); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Register and enqueue CSS. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
*/ |
| 59 |
public function styles() |
| 60 |
{ |
| 61 |
wp_register_style($this->base->pluginSlug.'-main', SGPM_ASSETS_URL.'css/main.css', array(), $this->base->version); |
| 62 |
wp_enqueue_style($this->base->pluginSlug.'-main'); |
| 63 |
wp_register_style($this->base->pluginSlug.'-tab', SGPM_ASSETS_URL.'css/jquery.pwstabs.min.css', array(), $this->base->version); |
| 64 |
wp_enqueue_style($this->base->pluginSlug.'-tab'); |
| 65 |
wp_register_style($this->base->pluginSlug.'-select2', SGPM_ASSETS_URL.'css/select2.min.css', array(), $this->base->version); |
| 66 |
wp_enqueue_style($this->base->pluginSlug.'-select2'); |
| 67 |
wp_register_style($this->base->pluginSlug.'-switch-button-toggles', SGPM_ASSETS_URL.'css/switch-button-toggles.css', array(), $this->base->version); |
| 68 |
wp_enqueue_style($this->base->pluginSlug.'-switch-button-toggles'); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Register and enqueue JS. |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
*/ |
| 76 |
public function scripts() |
| 77 |
{ |
| 78 |
wp_register_script( $this->base->pluginSlug.'-main', SGPM_ASSETS_URL.'js/main.js', array('jquery'), $this->base->version, true ); |
| 79 |
wp_enqueue_script($this->base->pluginSlug.'-main'); |
| 80 |
wp_register_script($this->base->pluginSlug.'-tab', SGPM_ASSETS_URL.'js/jquery.pwstabs.min.js', array('jquery'), $this->base->version, true ); |
| 81 |
wp_enqueue_script($this->base->pluginSlug.'-tab'); |
| 82 |
wp_register_script( $this->base->pluginSlug.'-select2', SGPM_ASSETS_URL.'js/select2.min.js', array('jquery'), $this->base->version, true ); |
| 83 |
wp_enqueue_script($this->base->pluginSlug.'-select2'); |
| 84 |
wp_register_script($this->base->pluginSlug.'-options-panel', SGPM_ASSETS_URL.'js/SGPMOptionsPanel.js', array('jquery'), $this->base->version, true ); |
| 85 |
wp_enqueue_script($this->base->pluginSlug.'-options-panel'); |
| 86 |
wp_register_script($this->base->pluginSlug.'-switch-button-toggles', SGPM_ASSETS_URL.'js/switch-button-toggles.js', array('jquery'), $this->base->version, true ); |
| 87 |
wp_enqueue_script($this->base->pluginSlug.'-switch-button-toggles'); |
| 88 |
|
| 89 |
$localizeData = array( |
| 90 |
'handle' => $this->base->pluginSlug.'-options-panel', |
| 91 |
'name' => 'SGPM_JS_PARAMS', |
| 92 |
'data' => array( |
| 93 |
'url' => admin_url('admin-ajax.php'), |
| 94 |
'nonce' => wp_create_nonce(SGPM_AJAX_NONCE) |
| 95 |
) |
| 96 |
); |
| 97 |
wp_localize_script($localizeData['handle'], $localizeData['name'], $localizeData['data']); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Echo out plugin header banner |
| 102 |
* |
| 103 |
* @since 1.0.0 |
| 104 |
*/ |
| 105 |
public function outputPluginScreenBanner() |
| 106 |
{ |
| 107 |
$html = ''; |
| 108 |
$html .= '<div class="sgpm-static-banner">'; |
| 109 |
$html .= '<div class="sgpm-inner-container">'; |
| 110 |
$html .= '<div class="sgpm-logo-wrapper"><a href="'.esc_url_raw(SGPM_SERVICE_URL).SGPM_UTM_SOURCE_URL.'" target="_blank"><img class="sgpm-brand-logo" title="Popup Maker" alt="Popup Maker" src="'.SGPM_IMG_URL.'popup-maker-logo-glow.png"></a></div>'; |
| 111 |
$html .= '<div class="sgpm-static-menu"><ul>'; |
| 112 |
$html .= '<li><a class="sgpm-menu-link" href="'.esc_url_raw('https://wordpress.org/support/plugin/popup-maker-wp').'" target="_blank">'. __('Help', 'popup-maker-api').'</a></li>'; |
| 113 |
$html .= '<li><a class="sgpm-create-popup-btn" href="'.esc_url_raw(SGPM_SERVICE_URL).SGPM_UTM_SOURCE_URL.'" target="_blank">Home</a></li>'; |
| 114 |
$html .= '</ul></div>'; |
| 115 |
$html .= '</div>'; |
| 116 |
$html .= '</div>'; |
| 117 |
echo wp_kses($html, array( |
| 118 |
'div' => array('class' => []), |
| 119 |
'ul' => array('class' => []), |
| 120 |
'li' => array('class' => []), |
| 121 |
'a' => array('class' => [], 'href' => [], 'target' => []), |
| 122 |
'img' => array('class' => [], 'alt' => [], 'title' => [], 'src' => []), |
| 123 |
)); |
| 124 |
} |
| 125 |
|
| 126 |
private function allowedToUsePlugin() |
| 127 |
{ |
| 128 |
$allowedToUsePlugin = true; |
| 129 |
|
| 130 |
// get user data |
| 131 |
$isAdmin = current_user_can('administrator'); |
| 132 |
$currentUserId = get_current_user_id(); |
| 133 |
$user = get_userdata($currentUserId); |
| 134 |
$userRoles = $user->roles; |
| 135 |
|
| 136 |
$selectedUserRoles = get_option('sgpm_popup_maker_user_roles'); |
| 137 |
if (!is_array($selectedUserRoles)) { |
| 138 |
$selectedUserRoles = array(); |
| 139 |
} |
| 140 |
$hasCurrentUserRoleInSelectedList = array_intersect($userRoles, $selectedUserRoles); |
| 141 |
|
| 142 |
// check role capatibility |
| 143 |
if (!$isAdmin && count($selectedUserRoles) && empty($hasCurrentUserRoleInSelectedList)) { |
| 144 |
$allowedToUsePlugin = false; |
| 145 |
} |
| 146 |
|
| 147 |
return $allowedToUsePlugin; |
| 148 |
} |
| 149 |
|
| 150 |
public function menu() |
| 151 |
{ |
| 152 |
$allowedToUsePlugin = $this->allowedToUsePlugin(); |
| 153 |
if (!$allowedToUsePlugin) return; |
| 154 |
|
| 155 |
$selectedUserRoles = get_option('sgpm_popup_maker_user_roles'); |
| 156 |
if (!is_array($selectedUserRoles)) { |
| 157 |
$selectedUserRoles = array(); |
| 158 |
} |
| 159 |
|
| 160 |
$this->hook = add_menu_page("Popup Maker", "Popup Maker", "read", "popup-maker-api-settings", array($this, 'page'), 'dashicons-welcome-widgets-menus'); |
| 161 |
|
| 162 |
if ($this->hook) { |
| 163 |
add_action('load-'.$this->hook, array($this, 'assets')); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
public function page() |
| 168 |
{ |
| 169 |
$sgpmPage = new SGPMPage(); |
| 170 |
$sgpmPage->init(); |
| 171 |
} |
| 172 |
} |
| 173 |
|