| @@ -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,208 +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 <info@catchplugins.com> | |
| 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 | |
| 34 | - /** | |
| 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; | |
| 56 | + } | |
| 42 | 57 | |
| 43 | - /** | |
| 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 ) { | |
| 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 | + ); | |
| 51 | 73 | |
| 52 | - $this->plugin_name = $plugin_name; | |
| 53 | - $this->version = $version; | |
| 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 | + } | |
| 54 | 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 | + } | |
| 55 | 94 | } |
| 56 | 95 | |
| 57 | 96 | /** |
| 58 | - * Register the stylesheets for the admin area. | |
| 59 | - * | |
| 60 | - * @since 1.0.0 | |
| 61 | - */ | |
| 62 | - public function enqueue_styles() { | |
| 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 | + ); | |
| 63 | 110 | |
| 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 | - */ | |
| 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 | + } | |
| 75 | 119 | |
| 76 | - if( isset( $_GET['page'] ) && 'essential-widgets' == $_GET['page'] ) { | |
| 77 | - wp_enqueue_style( $this->plugin_name. '-display-dashboard-admin', plugin_dir_url( __FILE__ ) . 'css/essential-widgets-dasbhoard-admin.css', array(), $this->version, 'all' ); | |
| 78 | - wp_enqueue_style( $this->plugin_name. '-display-dashboard', plugin_dir_url( __FILE__ ) . 'css/admin-dashboard.css', array(), $this->version, 'all' ); | |
| 79 | - } | |
| 80 | 120 | global $pagenow; |
| 81 | - if( 'widgets.php' === $pagenow || 'customize.php' === $pagenow) { | |
| 82 | - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/essential-widgets-admin.css', array(), $this->version, 'all' ); | |
| 83 | - } | |
| 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 | + } | |
| 84 | 131 | |
| 85 | - } | |
| 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 | + } | |
| 86 | 146 | |
| 87 | 147 | /** |
| 88 | - * Register the JavaScript for the admin area. | |
| 89 | - * | |
| 90 | - * @since 1.0.0 | |
| 91 | - */ | |
| 92 | - public function enqueue_scripts() { | |
| 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 | + } | |
| 93 | 154 | |
| 94 | - /** | |
| 95 | - * This function is provided for demonstration purposes only. | |
| 96 | - * | |
| 97 | - * An instance of this class should be passed to the run() function | |
| 98 | - * defined in Essential_Widgets_Loader as all of the hooks are defined | |
| 99 | - * in that particular class. | |
| 100 | - * | |
| 101 | - * The Essential_Widgets_Loader will then create the relationship | |
| 102 | - * between the defined hooks and the functions defined in this | |
| 103 | - * class. | |
| 104 | - */ | |
| 155 | + require plugin_dir_path( __FILE__ ) . 'partials/essential-widgets-admin-display.php'; | |
| 156 | + } | |
| 105 | 157 | |
| 106 | - if( isset( $_GET['page'] ) && 'essential-widgets' == $_GET['page'] ) { | |
| 107 | - wp_enqueue_script( 'minHeight', plugin_dir_url( __FILE__ ) . 'js/jquery.matchHeight.min.js', array( 'jquery' ), $this->version, false ); | |
| 108 | - wp_enqueue_script( $this->plugin_name . '-admin-script', plugin_dir_url( __FILE__ ) . 'js/admin-scripts.js', array( 'minHeight', 'jquery' ), $this->version, false ); | |
| 109 | - } | |
| 110 | - global $pagenow; | |
| 111 | - if( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) { | |
| 112 | - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/essential-widgets-admin.js', array( 'jquery' ), $this->version, false ); | |
| 113 | - } | |
| 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 | + } | |
| 114 | 168 | |
| 115 | - } | |
| 169 | + /** | |
| 170 | + * Ajax switch handler (On/Off toggle). | |
| 171 | + */ | |
| 172 | + public function ew_switch() { | |
| 173 | + $post_data = wp_unslash( $_POST ); | |
| 116 | 174 | |
| 117 | - /** | |
| 118 | - * Essential Widgets: action_links | |
| 119 | - * Essential Widgets Settings Link function callback | |
| 120 | - * | |
| 121 | - * @param arrray $links Link url. | |
| 122 | - * | |
| 123 | - * @param arrray $file File name. | |
| 124 | - */ | |
| 125 | - public function action_links( $links, $file ) { | |
| 126 | - if ( $file === $this->plugin_name . '/' . $this->plugin_name . '.php' ) { | |
| 127 | - $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=essential-widgets' ) ) . '">' . esc_html__( 'Settings', 'essential-widgets' ) . '</a>'; | |
| 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 | + } | |
| 128 | 178 | |
| 129 | - array_unshift( $links, $settings_link ); | |
| 130 | - } | |
| 131 | - return $links; | |
| 132 | - } | |
| 179 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 180 | + wp_die( esc_html__( 'Permission denied!', 'essential-widgets' ) ); | |
| 181 | + } | |
| 133 | 182 | |
| 134 | - /** | |
| 135 | - * Essential Widgets: add_plugin_settings_menu | |
| 136 | - * add Essential Widgets to menu | |
| 137 | - */ | |
| 138 | - public function add_plugin_settings_menu() { | |
| 139 | - add_menu_page( | |
| 140 | - esc_html__( 'Essential Widgets', 'essential-widgets' ), | |
| 141 | - esc_html__( 'Essential Widgets', 'essential-widgets' ), | |
| 142 | - 'manage_options', | |
| 143 | - 'essential-widgets', | |
| 144 | - array( $this, 'settings_page' ), | |
| 145 | - '', | |
| 146 | - '99.01564' | |
| 147 | - ); | |
| 148 | - } | |
| 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'] ) : ''; | |
| 149 | 185 | |
| 150 | - /** | |
| 151 | - * Essential Widgets: catch_web_tools_settings_page | |
| 152 | - * Essential Widgets Setting function | |
| 153 | - */ | |
| 154 | - public function settings_page() { | |
| 155 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 156 | - wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) ); | |
| 157 | - } | |
| 186 | + if ( empty( $option_name ) ) { | |
| 187 | + wp_die( esc_html__( 'Invalid option.', 'essential-widgets' ) ); | |
| 188 | + } | |
| 158 | 189 | |
| 159 | - require plugin_dir_path( __FILE__ ) . 'partials/essential-widgets-admin-display.php'; | |
| 160 | - } | |
| 190 | + $option_value = essential_widgets_get_options(); | |
| 191 | + $option_value[ $option_name ] = $value; | |
| 161 | 192 | |
| 162 | - /** | |
| 163 | - * Essential Widgets: register_settings | |
| 164 | - * Essential Widgets Register Settings | |
| 165 | - */ | |
| 166 | - public function register_settings() { | |
| 167 | - register_setting( | |
| 168 | - 'essential-widgets-group', | |
| 169 | - 'essential_widgets_options', | |
| 170 | - array( $this, 'sanitize_callback' ) | |
| 171 | - ); | |
| 172 | - } | |
| 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 | + } | |
| 173 | 198 | |
| 174 | - public function ew_switch() { | |
| 175 | - $value = ( 'true' == $_POST['value'] ) ? 1 : 0; | |
| 199 | + wp_die(); | |
| 200 | + } | |
| 176 | 201 | |
| 177 | - $option_name = $_POST['option_name']; | |
| 202 | + /** | |
| 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>'; | |
| 178 | 216 | |
| 179 | - $option_value = essential_widgets_get_options(); | |
| 217 | + $stars_color = '#ffb900'; | |
| 180 | 218 | |
| 181 | - $option_value[$option_name] = $value; | |
| 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 | + } | |
| 182 | 226 | |
| 183 | - if( update_option( 'essential_widgets_options', $option_value ) ) { | |
| 184 | - echo $value; | |
| 185 | - } else { | |
| 186 | - esc_html_e( 'Connection Error. Please try again.', 'essential-widgets' ); | |
| 187 | - } | |
| 227 | + return $meta_fields; | |
| 228 | + } | |
| 188 | 229 | |
| 189 | - wp_die(); // this is required to terminate immediately and return a proper response | |
| 190 | - } | |
| 191 | - function add_plugin_meta_links( $meta_fields, $file ){ | |
| 192 | - if( ESSENTIAL_WIDGETS_BASENAME == $file ) { | |
| 193 | - $meta_fields[] = "<a href='https://catchplugins.com/support-forum/forum/essential-widgets-free-wordpress-plugin/' target='_blank'>Support Forum</a>"; | |
| 194 | - $meta_fields[] = "<a href='https://wordpress.org/support/plugin/essential-widgets/reviews/#new-post' target='_blank' title='Rate'> | |
| 195 | - <i class='ct-rate-stars'>" | |
| 196 | - . "<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>" | |
| 197 | - . "<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>" | |
| 198 | - . "<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>" | |
| 199 | - . "<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>" | |
| 200 | - . "<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>" | |
| 201 | - . "</i></a>"; | |
| 230 | + /** | |
| 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 | + } | |
| 202 | 240 | |
| 203 | - $stars_color = "#ffb900"; | |
| 241 | + $sanitized = array(); | |
| 242 | + foreach ( $input as $key => $value ) { | |
| 243 | + $sanitized[ sanitize_key( $key ) ] = sanitize_text_field( $value ); | |
| 244 | + } | |
| 204 | 245 | |
| 205 | - echo "<style>" | |
| 206 | - . ".ct-rate-stars{display:inline-block;color:" . $stars_color . ";position:relative;top:3px;}" | |
| 207 | - . ".ct-rate-stars svg{fill:" . $stars_color . ";}" | |
| 208 | - . ".ct-rate-stars svg:hover{fill:" . $stars_color . "}" | |
| 209 | - . ".ct-rate-stars svg:hover ~ svg{fill:none;}" | |
| 210 | - . "</style>"; | |
| 211 | - } | |
| 246 | + return $sanitized; | |
| 247 | + } | |
| 212 | 248 | |
| 213 | - return $meta_fields; | |
| 214 | - } | |
| 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 | + } | |
| 215 | 254 | |
| 216 | 255 | } |