| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly. |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* The admin-specific functionality of the plugin. |
| 9 |
* |
| 10 |
* @link https://catchplugins.com |
| 11 |
* @since 1.0.0 |
| 12 |
* |
| 13 |
* @package Essential_Widgets |
| 14 |
* @subpackage Essential_Widgets/admin |
| 15 |
*/ |
| 16 |
|
| 17 |
class Essential_Widgets_Admin { |
| 18 |
|
| 19 |
/** |
| 20 |
* Plugin name. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $plugin_name; |
| 25 |
|
| 26 |
/** |
| 27 |
* Plugin version. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $version; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor. |
| 35 |
* |
| 36 |
* @param string $plugin_name |
| 37 |
* @param string $version |
| 38 |
*/ |
| 39 |
public function __construct( $plugin_name, $version ) { |
| 40 |
$this->plugin_name = $plugin_name; |
| 41 |
$this->version = $version; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Enqueue admin styles. |
| 46 |
* |
| 47 |
* @param string $hook_suffix |
| 48 |
*/ |
| 49 |
public function enqueue_styles( $hook_suffix ) { |
| 50 |
// Only load on plugin admin page |
| 51 |
if ( 'toplevel_page_essential-widgets' === $hook_suffix ) { |
| 52 |
wp_enqueue_style( |
| 53 |
$this->plugin_name . '-display-dashboard-admin', |
| 54 |
plugin_dir_url( __FILE__ ) . 'css/essential-widgets-dasbhoard-admin.css', |
| 55 |
array(), |
| 56 |
$this->version, |
| 57 |
'all' |
| 58 |
); |
| 59 |
|
| 60 |
wp_enqueue_style( |
| 61 |
$this->plugin_name . '-display-dashboard', |
| 62 |
plugin_dir_url( __FILE__ ) . 'css/admin-dashboard.css', |
| 63 |
array(), |
| 64 |
$this->version, |
| 65 |
'all' |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
// Always load on widgets and customizer screens |
| 70 |
global $pagenow; |
| 71 |
if ( in_array( $pagenow, array( 'widgets.php', 'customize.php' ), true ) ) { |
| 72 |
wp_enqueue_style( |
| 73 |
$this->plugin_name, |
| 74 |
plugin_dir_url( __FILE__ ) . 'css/essential-widgets-admin.css', |
| 75 |
array(), |
| 76 |
$this->version, |
| 77 |
'all' |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Enqueue admin scripts. |
| 84 |
* |
| 85 |
* @param string $hook_suffix |
| 86 |
*/ |
| 87 |
public function enqueue_scripts( $hook_suffix ) { |
| 88 |
if ( 'toplevel_page_essential-widgets' === $hook_suffix ) { |
| 89 |
wp_enqueue_script( |
| 90 |
'minHeight', |
| 91 |
plugin_dir_url( __FILE__ ) . 'js/jquery.matchHeight.min.js', |
| 92 |
array( 'jquery' ), |
| 93 |
$this->version, |
| 94 |
true |
| 95 |
); |
| 96 |
|
| 97 |
wp_enqueue_script( |
| 98 |
$this->plugin_name . '-admin-script', |
| 99 |
plugin_dir_url( __FILE__ ) . 'js/admin-scripts.js', |
| 100 |
array( 'minHeight', 'jquery' ), |
| 101 |
$this->version, |
| 102 |
true |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
global $pagenow; |
| 107 |
if ( in_array( $pagenow, array( 'widgets.php', 'customize.php' ), true ) ) { |
| 108 |
wp_enqueue_script( |
| 109 |
$this->plugin_name, |
| 110 |
plugin_dir_url( __FILE__ ) . 'js/essential-widgets-admin.js', |
| 111 |
array( 'jquery' ), |
| 112 |
$this->version, |
| 113 |
true |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Add plugin menu in admin. |
| 120 |
*/ |
| 121 |
public function add_plugin_settings_menu() { |
| 122 |
add_menu_page( |
| 123 |
esc_html__( 'Essential Widgets', 'essential-widgets' ), |
| 124 |
esc_html__( 'Essential Widgets', 'essential-widgets' ), |
| 125 |
'manage_options', |
| 126 |
'essential-widgets', |
| 127 |
array( $this, 'settings_page' ), |
| 128 |
'', |
| 129 |
99 |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Render the settings page. |
| 135 |
*/ |
| 136 |
public function settings_page() { |
| 137 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 138 |
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'essential-widgets' ) ); |
| 139 |
} |
| 140 |
|
| 141 |
require plugin_dir_path( __FILE__ ) . 'partials/essential-widgets-admin-display.php'; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Register plugin settings. |
| 146 |
*/ |
| 147 |
public function register_settings() { |
| 148 |
register_setting( |
| 149 |
'essential-widgets-group', |
| 150 |
'essential_widgets_options', |
| 151 |
array( $this, 'sanitize_callback' ) |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Ajax switch handler (On/Off toggle). |
| 157 |
*/ |
| 158 |
public function ew_switch() { |
| 159 |
$post_data = wp_unslash( $_POST ); |
| 160 |
|
| 161 |
if ( empty( $post_data['security'] ) || ! wp_verify_nonce( sanitize_text_field( $post_data['security'] ), 'ew_nonce' ) ) { |
| 162 |
wp_die( esc_html__( 'Unauthorized access!', 'essential-widgets' ) ); |
| 163 |
} |
| 164 |
|
| 165 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 166 |
wp_die( esc_html__( 'Permission denied!', 'essential-widgets' ) ); |
| 167 |
} |
| 168 |
|
| 169 |
$value = ( isset( $post_data['value'] ) && 'true' === $post_data['value'] ) ? 1 : 0; |
| 170 |
$option_name = isset( $post_data['option_name'] ) ? sanitize_key( $post_data['option_name'] ) : ''; |
| 171 |
|
| 172 |
if ( empty( $option_name ) ) { |
| 173 |
wp_die( esc_html__( 'Invalid option.', 'essential-widgets' ) ); |
| 174 |
} |
| 175 |
|
| 176 |
$option_value = essential_widgets_get_options(); |
| 177 |
$option_value[ $option_name ] = $value; |
| 178 |
|
| 179 |
if ( update_option( 'essential_widgets_options', $option_value ) ) { |
| 180 |
echo esc_html( $value ); |
| 181 |
} else { |
| 182 |
esc_html_e( 'Connection Error. Please try again.', 'essential-widgets' ); |
| 183 |
} |
| 184 |
|
| 185 |
wp_die(); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Add plugin meta links on plugin page. |
| 190 |
* |
| 191 |
* @param array $meta_fields |
| 192 |
* @param string $file |
| 193 |
* @return array |
| 194 |
*/ |
| 195 |
public function add_plugin_meta_links( $meta_fields, $file ) { |
| 196 |
if ( ESSENTIAL_WIDGETS_BASENAME === $file ) { |
| 197 |
$meta_fields[] = '<a href="' . esc_url( 'https://catchplugins.com/support-forum/forum/essential-widgets-free-wordpress-plugin/' ) . '" target="_blank">' . esc_html__( 'Support Forum', 'essential-widgets' ) . '</a>'; |
| 198 |
$meta_fields[] = '<a href="' . esc_url( 'https://wordpress.org/support/plugin/essential-widgets/reviews/#new-post' ) . '" target="_blank" title="' . esc_attr__( 'Rate', 'essential-widgets' ) . '">' |
| 199 |
. '<i class="ct-rate-stars">' |
| 200 |
. str_repeat('<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-star"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>', 5) |
| 201 |
. '</i></a>'; |
| 202 |
|
| 203 |
$stars_color = '#ffb900'; |
| 204 |
|
| 205 |
echo '<style>' |
| 206 |
. '.ct-rate-stars{display:inline-block;color:' . esc_attr( $stars_color ) . ';position:relative;top:3px;}' |
| 207 |
. '.ct-rate-stars svg{fill:' . esc_attr( $stars_color ) . ';}' |
| 208 |
. '.ct-rate-stars svg:hover{fill:' . esc_attr( $stars_color ) . ';}' |
| 209 |
. '.ct-rate-stars svg:hover ~ svg{fill:none;}' |
| 210 |
. '</style>'; |
| 211 |
} |
| 212 |
|
| 213 |
return $meta_fields; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Dummy sanitize callback. |
| 218 |
* |
| 219 |
* @param array $input |
| 220 |
* @return array |
| 221 |
*/ |
| 222 |
public function sanitize_callback( $input ) { |
| 223 |
if ( ! is_array( $input ) ) { |
| 224 |
return array(); |
| 225 |
} |
| 226 |
|
| 227 |
$sanitized = array(); |
| 228 |
foreach ( $input as $key => $value ) { |
| 229 |
$sanitized[ sanitize_key( $key ) ] = sanitize_text_field( $value ); |
| 230 |
} |
| 231 |
|
| 232 |
return $sanitized; |
| 233 |
} |
| 234 |
|
| 235 |
public function action_links( $links ) { |
| 236 |
$settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=essential-widgets' ) ) . '">' . esc_html__( 'Settings', 'essential-widgets' ) . '</a>'; |
| 237 |
array_unshift( $links, $settings_link ); |
| 238 |
return $links; |
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
|