network-fields
1 year ago
settings-fields
1 year ago
class-wpel-network-page.php
3 months ago
class-wpel-settings-page.php
1 month ago
class-wpel-settings-page.php
405 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class WPEL_Settings_Page |
| 5 | * |
| 6 | * @package WPEL |
| 7 | * @category WordPress Plugin |
| 8 | * @version 2.3 |
| 9 | * @link https://www.webfactoryltd.com/ |
| 10 | * @license Dual licensed under the MIT and GPLv2+ licenses |
| 11 | */ |
| 12 | final class WPEL_Settings_Page extends WPRun_Base_1x0x0 |
| 13 | { |
| 14 | |
| 15 | /** |
| 16 | * @var string |
| 17 | */ |
| 18 | private $menu_slug = 'wpel-settings-page'; |
| 19 | |
| 20 | /** |
| 21 | * @var string |
| 22 | */ |
| 23 | private $current_tab = null; |
| 24 | |
| 25 | /** |
| 26 | * @var array |
| 27 | */ |
| 28 | private $tabs = array(); |
| 29 | |
| 30 | /** |
| 31 | * @var WPEL_Network_Page |
| 32 | */ |
| 33 | private $network_page = null; |
| 34 | |
| 35 | /** |
| 36 | * Initialize |
| 37 | */ |
| 38 | protected function init($network_page, array $fields_objects) |
| 39 | { |
| 40 | $this->network_page = $network_page; |
| 41 | |
| 42 | $this->tabs = array( |
| 43 | 'external-links' => array( |
| 44 | 'title' => __('External Links', 'wp-external-links'), |
| 45 | 'icon' => false, |
| 46 | 'fields' => $fields_objects['external-links'], |
| 47 | ), |
| 48 | 'internal-links' => array( |
| 49 | 'title' => __('Internal Links', 'wp-external-links'), |
| 50 | 'icon' => false, |
| 51 | 'fields' => $fields_objects['internal-links'], |
| 52 | ), |
| 53 | 'excluded-links' => array( |
| 54 | 'title' => __('Excluded Links', 'wp-external-links'), |
| 55 | 'icon' => false, |
| 56 | 'fields' => $fields_objects['excluded-links'], |
| 57 | ), |
| 58 | 'exceptions' => array( |
| 59 | 'title' => __('Exceptions', 'wp-external-links'), |
| 60 | 'icon' => false, |
| 61 | 'fields' => $fields_objects['exceptions'], |
| 62 | ), |
| 63 | 'link-rules' => array( |
| 64 | 'title' => '<span class="dashicons dashicons-star-filled"></span>' . __('Link Rules', 'wp-external-links'), |
| 65 | 'icon' => false, |
| 66 | ), |
| 67 | |
| 68 | 'exit-confirmation' => array( |
| 69 | 'title' => '<span class="dashicons dashicons-star-filled"></span>' . __('Exit Confirmation', 'wp-external-links'), |
| 70 | 'icon' => false, |
| 71 | 'fields' => $fields_objects['exit-confirmation'], |
| 72 | ), |
| 73 | 'link-checking' => array( |
| 74 | 'title' => '<span class="dashicons dashicons-star-filled"></span>' . __('Link Checker', 'wp-external-links'), |
| 75 | 'icon' => false, |
| 76 | ), |
| 77 | 'pro' => array( |
| 78 | 'title' => __('PRO', 'wp-external-links'), |
| 79 | 'icon' => false, |
| 80 | ), |
| 81 | 'support' => array( |
| 82 | 'title' => __('Support', 'wp-external-links'), |
| 83 | 'icon' => false, |
| 84 | ), |
| 85 | ); |
| 86 | |
| 87 | // check excluded links tab available |
| 88 | if ($this->get_option_value('excludes_as_internal_links', 'exceptions')) { |
| 89 | unset($this->tabs['excluded-links']); |
| 90 | } |
| 91 | |
| 92 | // get current tab |
| 93 | //phpcs:ignore because nonce is not needed as this just sets the current tab and can be linked directly |
| 94 | if(isset($_GET['tab'])){ //phpcs:ignore |
| 95 | $this->current_tab = sanitize_text_field($_GET['tab']); //phpcs:ignore |
| 96 | } |
| 97 | |
| 98 | // set default tab |
| 99 | if ( !isset($this->current_tab) || !key_exists($this->current_tab, $this->tabs)) { |
| 100 | reset($this->tabs); |
| 101 | $this->current_tab = key($this->tabs); |
| 102 | } |
| 103 | |
| 104 | add_filter('plugin_action_links_' . plugin_basename(TEST_WPEL_PLUGIN_FILE), array($this, 'plugin_action_links')); |
| 105 | add_filter('admin_footer_text', array($this, 'admin_footer_text')); |
| 106 | add_action('admin_action_wpel_install_wpcaptcha', array($this, 'install_wpcaptcha')); |
| 107 | } |
| 108 | |
| 109 | // auto download / install / activate WP Captcha plugin |
| 110 | function install_wpcaptcha() |
| 111 | { |
| 112 | check_ajax_referer('install_wpcaptcha'); |
| 113 | |
| 114 | if (false === current_user_can('manage_options')) { |
| 115 | wp_die('Sorry, you have to be an admin to run this action.'); |
| 116 | } |
| 117 | |
| 118 | $plugin_slug = 'advanced-google-recaptcha/advanced-google-recaptcha.php'; |
| 119 | $plugin_zip = 'https://downloads.wordpress.org/plugin/advanced-google-recaptcha.latest-stable.zip'; |
| 120 | |
| 121 | @include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 122 | @include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 123 | @include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 124 | @include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 125 | @include_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 126 | echo '<style> |
| 127 | body{ |
| 128 | font-family: sans-serif; |
| 129 | font-size: 14px; |
| 130 | line-height: 1.5; |
| 131 | color: #444; |
| 132 | } |
| 133 | </style>'; |
| 134 | |
| 135 | echo '<div style="margin: 20px; color:#444;">'; |
| 136 | echo 'If things are not done in a minute <a target="_parent" href="' . esc_url(admin_url('plugin-install.php?s=google%20recaptcha%20webfactory&tab=search&type=term')) . '">install the plugin manually via Plugins page</a><br><br>'; |
| 137 | echo 'Starting ...<br><br>'; |
| 138 | |
| 139 | wp_cache_flush(); |
| 140 | $upgrader = new Plugin_Upgrader(); |
| 141 | echo 'Check if Advanced Google ReCaptcha is already installed ... <br />'; |
| 142 | if (self::is_plugin_installed($plugin_slug)) { |
| 143 | echo 'Advanced Google ReCaptcha is already installed! <br /><br />Making sure it\'s the latest version.<br />'; |
| 144 | $upgrader->upgrade($plugin_slug); |
| 145 | $installed = true; |
| 146 | } else { |
| 147 | echo 'Installing Advanced Google ReCaptcha.<br />'; |
| 148 | $installed = $upgrader->install($plugin_zip); |
| 149 | } |
| 150 | wp_cache_flush(); |
| 151 | |
| 152 | if (!is_wp_error($installed) && $installed) { |
| 153 | echo 'Activating Advanced Google ReCaptcha.<br />'; |
| 154 | $activate = activate_plugin($plugin_slug); |
| 155 | |
| 156 | if (is_null($activate)) { |
| 157 | echo 'Advanced Google ReCaptcha Activated.<br />'; |
| 158 | |
| 159 | echo '<script>setTimeout(function() { top.location = "admin.php?page=wpel-settings-page"; }, 1000);</script>'; |
| 160 | echo '<br>If you are not redirected in a few seconds - <a href="admin.php?page=wpel-settings-page" target="_parent">click here</a>.'; |
| 161 | } |
| 162 | } else { |
| 163 | echo 'Could not install Advanced Google ReCaptcha. You\'ll have to <a target="_parent" href="' . esc_url(admin_url('plugin-install.php?s=google%20recaptcha%20webfactory&tab=search&type=term')) . '">download and install manually</a>.'; |
| 164 | } |
| 165 | |
| 166 | echo '</div>'; |
| 167 | } // install_wpcaptcha |
| 168 | |
| 169 | static function is_plugin_installed($slug) |
| 170 | { |
| 171 | if (!function_exists('get_plugins')) { |
| 172 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 173 | } |
| 174 | $all_plugins = get_plugins(); |
| 175 | |
| 176 | if (!empty($all_plugins[$slug])) { |
| 177 | return true; |
| 178 | } else { |
| 179 | return false; |
| 180 | } |
| 181 | } // is_plugin_installed |
| 182 | |
| 183 | /** |
| 184 | * Add powered by text in admin footer |
| 185 | * |
| 186 | * @param string $text Default footer text. |
| 187 | * |
| 188 | * @return string |
| 189 | */ |
| 190 | function admin_footer_text($text) |
| 191 | { |
| 192 | $current_screen = get_current_screen(); |
| 193 | if (!empty($current_screen) && $current_screen->id == 'toplevel_page_wpel-settings-page') { |
| 194 | $plugin_version = get_option('wpel-version'); |
| 195 | $text = '<i>WP External Links v' . esc_attr($plugin_version) . ' by <a href="https://www.webfactoryltd.com?ref=wp-external-links" title="WebFactory Ltd" target="_blank">WebFactory Ltd</a>. Please <a target="_blank" href="https://wordpress.org/support/plugin/wp-external-links/reviews/#new-post" title="Rate the plugin">rate the plugin <span>� |
| 196 | � |
| 197 | � |
| 198 | � |
| 199 | � |
| 200 | </span></a> to help us spread the word. Thank you 🙌</i>'; |
| 201 | } |
| 202 | |
| 203 | return $text; |
| 204 | } // admin_footer_text |
| 205 | |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * Add "Configure Settings" action link to plugins table, left part |
| 210 | * |
| 211 | * @param array $links Initial list of links. |
| 212 | * |
| 213 | * @return array |
| 214 | */ |
| 215 | function plugin_action_links($links) |
| 216 | { |
| 217 | $settings_link = '<a href="' . admin_url('admin.php?page=wpel-settings-page') . '" title="Open WP External Links Settings">Configure</a>'; |
| 218 | $pro_link = '<a href="' . admin_url('admin.php?page=wpel-settings-page#open-pro-dialog') . '" title="Get PRO version"><b>Get PRO</b></a>'; |
| 219 | |
| 220 | array_unshift($links, $pro_link); |
| 221 | array_unshift($links, $settings_link); |
| 222 | |
| 223 | return $links; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Get option value |
| 228 | * @param string $key |
| 229 | * @param string $type |
| 230 | * @return string |
| 231 | * @triggers E_USER_NOTICE Option value cannot be found |
| 232 | */ |
| 233 | public function get_option_value($key, $type = null) |
| 234 | { |
| 235 | if ('own_admin_menu' == $key) { |
| 236 | return '1'; |
| 237 | } |
| 238 | |
| 239 | if (null === $type) { |
| 240 | foreach ($this->tabs as $tab_key => $values) { |
| 241 | if (!isset($values['fields'])) { |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | $option_values = $values['fields']->get_option_values(); |
| 246 | |
| 247 | if (!isset($option_values[$key])) { |
| 248 | continue; |
| 249 | } |
| 250 | |
| 251 | return $option_values[$key]; |
| 252 | } |
| 253 | } else if (isset($this->tabs[$type]['fields'])) { |
| 254 | $option_values = $this->tabs[$type]['fields']->get_option_values(); |
| 255 | return @$option_values[$key]; |
| 256 | } |
| 257 | |
| 258 | if($key == 'icon_type'){ |
| 259 | return false; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Action for "admin_menu" |
| 265 | */ |
| 266 | protected function action_admin_menu() |
| 267 | { |
| 268 | $capability = $this->network_page->get_option_value('capability'); |
| 269 | |
| 270 | $own_admin_menu = $this->get_option_value('own_admin_menu', 'admin'); |
| 271 | |
| 272 | if ('1' === $own_admin_menu) { |
| 273 | $this->page_hook = add_menu_page( |
| 274 | __('WP External Links', 'wp-external-links') // page title |
| 275 | , |
| 276 | __('WP External Links', 'wp-external-links') // menu title |
| 277 | , |
| 278 | $capability // capability |
| 279 | , |
| 280 | $this->menu_slug // id |
| 281 | , |
| 282 | $this->get_callback('show_admin_page') // callback |
| 283 | , |
| 284 | WPEL_PLUGIN_URL . 'public/images/icon-small.png' // icon |
| 285 | , |
| 286 | null // position |
| 287 | ); |
| 288 | } else { |
| 289 | $this->page_hook = add_options_page( |
| 290 | __('WP External Links', 'wp-external-links') // page title |
| 291 | , |
| 292 | __('WP External Links', 'wp-external-links') // menu title |
| 293 | , |
| 294 | $capability // capability |
| 295 | , |
| 296 | $this->menu_slug // id |
| 297 | , |
| 298 | $this->get_callback('show_admin_page') // callback |
| 299 | ); |
| 300 | } |
| 301 | |
| 302 | add_action('load-' . $this->page_hook, $this->get_callback('add_help_tabs')); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Set default option values for new created sites |
| 307 | * @param integer $blog_id |
| 308 | */ |
| 309 | protected function action_wpmu_new_blog($blog_id) |
| 310 | { |
| 311 | $default_site_id = $this->network_page->get_option_value('default_settings_site'); |
| 312 | |
| 313 | foreach ($this->tabs as $tab_key => $values) { |
| 314 | if (!isset($values['fields'])) { |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | $option_name = $values['fields']->get_setting('option_name'); |
| 319 | |
| 320 | $default_option_values = get_blog_option($default_site_id, $option_name, array()); |
| 321 | update_blog_option($blog_id, $option_name, $default_option_values); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Action for "admin_enqueue_scripts" |
| 327 | */ |
| 328 | protected function action_admin_enqueue_scripts() |
| 329 | { |
| 330 | $current_screen = get_current_screen(); |
| 331 | $plugin_version = get_option('wpel-version'); |
| 332 | |
| 333 | if ($current_screen->id == 'toplevel_page_wpel-settings-page' || $current_screen->id == 'settings_page_wpel-settings-page') { |
| 334 | wp_enqueue_script('jquery-ui-core'); |
| 335 | wp_enqueue_script('jquery-ui-accordion'); |
| 336 | |
| 337 | wp_enqueue_style('wp-jquery-ui-dialog'); |
| 338 | wp_enqueue_script('jquery-ui-position'); |
| 339 | wp_enqueue_script('jquery-ui-dialog'); |
| 340 | |
| 341 | wp_enqueue_style('wpel-font-awesome'); |
| 342 | wp_enqueue_style('wpel-admin-style'); |
| 343 | wp_enqueue_script('wpel-admin-script'); |
| 344 | |
| 345 | wp_enqueue_style('wp-color-picker'); |
| 346 | wp_enqueue_script('wp-color-picker'); |
| 347 | |
| 348 | wp_enqueue_style('jquery-ui-smoothness', plugins_url('/public/css/jquery-ui.css', WPEL_Plugin::get_plugin_file()), false, $plugin_version); |
| 349 | wp_enqueue_style('wpel-admin-font', plugins_url('/public/css/poppins.css', WPEL_Plugin::get_plugin_file()), false, $plugin_version); |
| 350 | } |
| 351 | |
| 352 | wp_enqueue_style('wpel-admin-global-style'); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Show Admin Page |
| 357 | */ |
| 358 | protected function show_admin_page() |
| 359 | { |
| 360 | $template_file = WPEL_Plugin::get_plugin_dir('/templates/settings-page/main.php'); |
| 361 | $page = $this->get_option_value('own_admin_menu') ? 'admin.php' : 'options-general.php'; |
| 362 | $page_url = admin_url() . $page . '?page=' . $this->menu_slug; |
| 363 | |
| 364 | $template_vars = array( |
| 365 | 'tabs' => $this->tabs, |
| 366 | 'current_tab' => $this->current_tab, |
| 367 | 'page_url' => $page_url, |
| 368 | 'menu_slug' => $this->menu_slug, |
| 369 | 'own_admin_menu' => $this->get_option_value('own_admin_menu', 'admin'), |
| 370 | ); |
| 371 | |
| 372 | $this->show_template($template_file, $template_vars); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Add help tabs |
| 377 | */ |
| 378 | protected function add_help_tabs() |
| 379 | { |
| 380 | $screen = get_current_screen(); |
| 381 | return; |
| 382 | |
| 383 | $screen->add_help_tab(array( |
| 384 | 'id' => 'under-construction', |
| 385 | 'title' => __('Under Construction', 'wp-external-links'), |
| 386 | 'callback' => $this->get_callback('show_help_tab'), |
| 387 | )); |
| 388 | $screen->add_help_tab(array( |
| 389 | 'id' => 'data-attributes', |
| 390 | 'title' => __('Data Attributes', 'wp-external-links'), |
| 391 | 'callback' => $this->get_callback('show_help_tab'), |
| 392 | )); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * @param WP_Screen $screen |
| 397 | * @param array $args |
| 398 | */ |
| 399 | protected function show_help_tab($screen, array $args) |
| 400 | { |
| 401 | $template_file = WPEL_Plugin::get_plugin_dir('/templates/settings-page/help-tabs/' . $args['id'] . '.php'); |
| 402 | $this->show_template($template_file); |
| 403 | } |
| 404 | } |
| 405 |