PluginProbe
Essential Widgets / 3.1
Essential Widgets v3.1
3.2.1 3.3 3.2 trunk 1.0.0 1.0.1 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 1.9 2.0 2.1 2.2 All 31 releases
← All changes | admin/class-essential-widgets-admin.php +214 -120 1.1 → 3.1 View file →
@@ -1,6 +1,10 @@
1 1 <?php
2 2
3 +if ( ! defined( 'ABSPATH' ) ) {
4 + exit; // Exit if accessed directly.
5 +}
6 +
3 7 /**
4 8 * The admin-specific functionality of the plugin.
5 9 *
6 10 * @link https://catchplugins.com
@@ -9,153 +13,243 @@
9 13 * @package Essential_Widgets
10 14 * @subpackage Essential_Widgets/admin
11 15 */
12 16
13 -/**
14 - * The admin-specific functionality of the plugin.
15 - *
16 - * Defines the plugin name, version, and two examples hooks for how to
17 - * enqueue the admin-specific stylesheet and JavaScript.
18 - *
19 - * @package Essential_Widgets
20 - * @subpackage Essential_Widgets/admin
21 - * @author Catch Plugins <[email protected]>
22 - */
23 17 class Essential_Widgets_Admin {
24 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 +
25 44 /**
26 - * The ID of this plugin.
27 - *
28 - * @since 1.0.0
29 - * @access private
30 - * @var string $plugin_name The ID of this plugin.
31 - */
32 - private $plugin_name;
45 + * Register hooks for admin.
46 + */
47 + public function hooks() {
48 + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
49 + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
50 + add_action( 'admin_menu', array( $this, 'add_plugin_settings_menu' ) );
51 + add_filter( 'plugin_action_links', array( $this, 'add_plugin_meta_links' ), 10, 2 );
52 + add_action( 'wp_ajax_ew_switch', array( $this, 'ew_switch' ) );
53 + add_action( 'admin_init', array( $this, 'register_settings' ) );
54 + add_filter( 'plugin_action_links_' . ESSENTIAL_WIDGETS_BASENAME, [ $this, 'action_links' ] );
33 55
56 + }
57 +
58 + /**
59 + * Enqueue admin styles.
60 + *
61 + * @param string $hook_suffix
62 + */
63 + public function enqueue_styles( $hook_suffix ) {
64 + // Only load on plugin admin page
65 + if ( 'toplevel_page_essential-widgets' === $hook_suffix ) {
66 + wp_enqueue_style(
67 + $this->plugin_name . '-display-dashboard-admin',
68 + plugin_dir_url( __FILE__ ) . 'css/essential-widgets-dasbhoard-admin.css',
69 + array(),
70 + $this->version,
71 + 'all'
72 + );
73 +
74 + wp_enqueue_style(
75 + $this->plugin_name . '-display-dashboard',
76 + plugin_dir_url( __FILE__ ) . 'css/admin-dashboard.css',
77 + array(),
78 + $this->version,
79 + 'all'
80 + );
81 + }
82 +
83 + // Always load on widgets and customizer screens
84 + global $pagenow;
85 + if ( in_array( $pagenow, array( 'widgets.php', 'customize.php' ), true ) ) {
86 + wp_enqueue_style(
87 + $this->plugin_name,
88 + plugin_dir_url( __FILE__ ) . 'css/essential-widgets-admin.css',
89 + array(),
90 + $this->version,
91 + 'all'
92 + );
93 + }
94 + }
95 +
34 96 /**
35 - * The version of this plugin.
36 - *
37 - * @since 1.0.0
38 - * @access private
39 - * @var string $version The current version of this plugin.
40 - */
41 - private $version;
97 + * Enqueue admin scripts.
98 + *
99 + * @param string $hook_suffix
100 + */
101 + public function enqueue_scripts( $hook_suffix ) {
102 + if ( 'toplevel_page_essential-widgets' === $hook_suffix ) {
103 + wp_enqueue_script(
104 + 'minHeight',
105 + plugin_dir_url( __FILE__ ) . 'js/jquery.matchHeight.min.js',
106 + array( 'jquery' ),
107 + $this->version,
108 + true
109 + );
42 110
111 + wp_enqueue_script(
112 + $this->plugin_name . '-admin-script',
113 + plugin_dir_url( __FILE__ ) . 'js/admin-scripts.js',
114 + array( 'minHeight', 'jquery' ),
115 + $this->version,
116 + true
117 + );
118 + }
119 +
120 + global $pagenow;
121 + if ( in_array( $pagenow, array( 'widgets.php', 'customize.php' ), true ) ) {
122 + wp_enqueue_script(
123 + $this->plugin_name,
124 + plugin_dir_url( __FILE__ ) . 'js/essential-widgets-admin.js',
125 + array( 'jquery' ),
126 + $this->version,
127 + true
128 + );
129 + }
130 + }
131 +
132 + /**
133 + * Add plugin menu in admin.
134 + */
135 + public function add_plugin_settings_menu() {
136 + add_menu_page(
137 + esc_html__( 'Essential Widgets', 'essential-widgets' ),
138 + esc_html__( 'Essential Widgets', 'essential-widgets' ),
139 + 'manage_options',
140 + 'essential-widgets',
141 + array( $this, 'settings_page' ),
142 + '',
143 + 99
144 + );
145 + }
146 +
43 147 /**
44 - * Initialize the class and set its properties.
45 - *
46 - * @since 1.0.0
47 - * @param string $plugin_name The name of this plugin.
48 - * @param string $version The version of this plugin.
49 - */
50 - public function __construct( $plugin_name, $version ) {
148 + * Render the settings page.
149 + */
150 + public function settings_page() {
151 + if ( ! current_user_can( 'manage_options' ) ) {
152 + wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'essential-widgets' ) );
153 + }
51 154
52 - $this->plugin_name = $plugin_name;
53 - $this->version = $version;
155 + require plugin_dir_path( __FILE__ ) . 'partials/essential-widgets-admin-display.php';
156 + }
54 157
55 - }
158 + /**
159 + * Register plugin settings.
160 + */
161 + public function register_settings() {
162 + register_setting(
163 + 'essential-widgets-group',
164 + 'essential_widgets_options',
165 + array( $this, 'sanitize_callback' )
166 + );
167 + }
56 168
57 169 /**
58 - * Register the stylesheets for the admin area.
59 - *
60 - * @since 1.0.0
61 - */
62 - public function enqueue_styles() {
170 + * Ajax switch handler (On/Off toggle).
171 + */
172 + public function ew_switch() {
173 + $post_data = wp_unslash( $_POST );
63 174
64 - /**
65 - * This function is provided for demonstration purposes only.
66 - *
67 - * An instance of this class should be passed to the run() function
68 - * defined in Essential_Widgets_Loader as all of the hooks are defined
69 - * in that particular class.
70 - *
71 - * The Essential_Widgets_Loader will then create the relationship
72 - * between the defined hooks and the functions defined in this
73 - * class.
74 - */
175 + if ( empty( $post_data['security'] ) || ! wp_verify_nonce( sanitize_text_field( $post_data['security'] ), 'ew_nonce' ) ) {
176 + wp_die( esc_html__( 'Unauthorized access!', 'essential-widgets' ) );
177 + }
75 178
76 - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/essential-widgets-admin.css', array(), $this->version, 'all' );
179 + if ( ! current_user_can( 'manage_options' ) ) {
180 + wp_die( esc_html__( 'Permission denied!', 'essential-widgets' ) );
181 + }
77 182
78 - }
183 + $value = ( isset( $post_data['value'] ) && 'true' === $post_data['value'] ) ? 1 : 0;
184 + $option_name = isset( $post_data['option_name'] ) ? sanitize_key( $post_data['option_name'] ) : '';
79 185
80 - /**
81 - * Register the JavaScript for the admin area.
82 - *
83 - * @since 1.0.0
84 - */
85 - public function enqueue_scripts() {
186 + if ( empty( $option_name ) ) {
187 + wp_die( esc_html__( 'Invalid option.', 'essential-widgets' ) );
188 + }
86 189
87 - /**
88 - * This function is provided for demonstration purposes only.
89 - *
90 - * An instance of this class should be passed to the run() function
91 - * defined in Essential_Widgets_Loader as all of the hooks are defined
92 - * in that particular class.
93 - *
94 - * The Essential_Widgets_Loader will then create the relationship
95 - * between the defined hooks and the functions defined in this
96 - * class.
97 - */
190 + $option_value = essential_widgets_get_options();
191 + $option_value[ $option_name ] = $value;
98 192
99 - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/essential-widgets-admin.js', array( 'jquery' ), $this->version, false );
193 + if ( update_option( 'essential_widgets_options', $option_value ) ) {
194 + echo esc_html( $value );
195 + } else {
196 + esc_html_e( 'Connection Error. Please try again.', 'essential-widgets' );
197 + }
100 198
101 - }
199 + wp_die();
200 + }
102 201
103 202 /**
104 - * Catch Instagram Feed Gallery Widget: action_links
105 - * Catch Instagram Feed Gallery Widget Settings Link function callback
106 - *
107 - * @param arrray $links Link url.
108 - *
109 - * @param arrray $file File name.
110 - */
111 - public function action_links( $links, $file ) {
112 - if ( $file === $this->plugin_name . '/' . $this->plugin_name . '.php' ) {
113 - $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=essential_widgets' ) ) . '">' . esc_html__( 'Settings', 'essential-widgets' ) . '</a>';
203 + * Add plugin meta links on plugin page.
204 + *
205 + * @param array $meta_fields
206 + * @param string $file
207 + * @return array
208 + */
209 + public function add_plugin_meta_links( $meta_fields, $file ) {
210 + if ( ESSENTIAL_WIDGETS_BASENAME === $file ) {
211 + $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>';
212 + $meta_fields[] = '<a href="' . esc_url( 'https://wordpress.org/support/plugin/essential-widgets/reviews/#new-post' ) . '" target="_blank" title="' . esc_attr__( 'Rate', 'essential-widgets' ) . '">'
213 + . '<i class="ct-rate-stars">'
214 + . 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)
215 + . '</i></a>';
114 216
115 - array_unshift( $links, $settings_link );
116 - }
117 - return $links;
118 - }
217 + $stars_color = '#ffb900';
119 218
120 - /**
121 - * Catch Instagram Feed Gallery Widget: add_plugin_settings_menu
122 - * add Catch Instagram Feed Gallery Widget to menu
123 - */
124 - public function add_plugin_settings_menu() {
125 - add_menu_page(
126 - esc_html__( 'Essential Widgets', 'essential-widgets' ),
127 - esc_html__( 'Essential Widgets', 'essential-widgets' ),
128 - 'manage_options',
129 - 'essential_widgets',
130 - array( $this, 'settings_page' ),
131 - '',
132 - '99.01564'
133 - );
134 - }
219 + echo '<style>'
220 + . '.ct-rate-stars{display:inline-block;color:' . esc_attr( $stars_color ) . ';position:relative;top:3px;}'
221 + . '.ct-rate-stars svg{fill:' . esc_attr( $stars_color ) . ';}'
222 + . '.ct-rate-stars svg:hover{fill:' . esc_attr( $stars_color ) . ';}'
223 + . '.ct-rate-stars svg:hover ~ svg{fill:none;}'
224 + . '</style>';
225 + }
135 226
227 + return $meta_fields;
228 + }
229 +
136 230 /**
137 - * Catch Instagram Feed Gallery Widget: catch_web_tools_settings_page
138 - * Catch Instagram Feed Gallery Widget Setting function
139 - */
140 - public function settings_page() {
141 - if ( ! current_user_can( 'manage_options' ) ) {
142 - wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
143 - }
231 + * Dummy sanitize callback.
232 + *
233 + * @param array $input
234 + * @return array
235 + */
236 + public function sanitize_callback( $input ) {
237 + if ( ! is_array( $input ) ) {
238 + return array();
239 + }
144 240
145 - require plugin_dir_path( __FILE__ ) . 'partials/essential-widgets-admin-display.php';
146 - }
241 + $sanitized = array();
242 + foreach ( $input as $key => $value ) {
243 + $sanitized[ sanitize_key( $key ) ] = sanitize_text_field( $value );
244 + }
147 245
148 - /**
149 - * Catch Instagram Feed Gallery Widget: register_settings
150 - * Catch Instagram Feed Gallery Widget Register Settings
151 - */
152 - public function register_settings() {
153 - register_setting(
154 - 'essential-widgets-group',
155 - 'essential_widgets_options',
156 - array( $this, 'sanitize_callback' )
157 - );
158 - }
246 + return $sanitized;
247 + }
159 248
249 + public function action_links( $links ) {
250 + $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=essential-widgets' ) ) . '">' . esc_html__( 'Settings', 'essential-widgets' ) . '</a>';
251 + array_unshift( $links, $settings_link );
252 + return $links;
253 + }
160 254
161 255 }