network-fields
7 years ago
settings-fields
7 years ago
class-wpel-network-page.php
7 years ago
class-wpel-settings-page.php
7 years ago
class-wpel-settings-page.php
234 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPEL_Settings_Page |
| 4 | * |
| 5 | * @package WPEL |
| 6 | * @category WordPress Plugin |
| 7 | * @version 2.3 |
| 8 | * @link https://www.webfactoryltd.com/ |
| 9 | * @license Dual licensed under the MIT and GPLv2+ licenses |
| 10 | */ |
| 11 | final class WPEL_Settings_Page extends WPRun_Base_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | private $menu_slug = 'wpel-settings-page'; |
| 18 | |
| 19 | /** |
| 20 | * @var string |
| 21 | */ |
| 22 | private $current_tab = null; |
| 23 | |
| 24 | /** |
| 25 | * @var array |
| 26 | */ |
| 27 | private $tabs = array(); |
| 28 | |
| 29 | /** |
| 30 | * @var WPEL_Network_Page |
| 31 | */ |
| 32 | private $network_page = null; |
| 33 | |
| 34 | /** |
| 35 | * Initialize |
| 36 | */ |
| 37 | protected function init( $network_page, array $fields_objects ) |
| 38 | { |
| 39 | $this->network_page = $network_page; |
| 40 | |
| 41 | $this->tabs = array( |
| 42 | 'external-links' => array( |
| 43 | 'title' => __( 'External Links', 'wp-external-links' ), |
| 44 | 'icon' => '<i class="fa fa-external-link-square" aria-hidden="true"></i>', |
| 45 | 'fields' => $fields_objects[ 'external-links' ], |
| 46 | ), |
| 47 | 'internal-links' => array( |
| 48 | 'title' => __( 'Internal Links', 'wp-external-links' ), |
| 49 | 'icon' => '<i class="fa fa-link" aria-hidden="true"></i>', |
| 50 | 'fields' => $fields_objects[ 'internal-links' ], |
| 51 | ), |
| 52 | 'excluded-links' => array( |
| 53 | 'title' => __( 'Excluded Links', 'wp-external-links' ), |
| 54 | 'icon' => '<i class="fa fa-share-square" aria-hidden="true"></i>', |
| 55 | 'fields' => $fields_objects[ 'excluded-links' ], |
| 56 | ), |
| 57 | 'exceptions' => array( |
| 58 | 'title' => __( 'Exceptions', 'wp-external-links' ), |
| 59 | 'icon' => '<i class="fa fa-th-large" aria-hidden="true"></i>', |
| 60 | 'fields' => $fields_objects[ 'exceptions' ], |
| 61 | ), |
| 62 | 'admin' => array( |
| 63 | 'title' => __( 'Admin Settings', 'wp-external-links' ), |
| 64 | 'icon' => '<i class="fa fa-cogs" aria-hidden="true"></i>', |
| 65 | 'fields' => $fields_objects[ 'admin' ], |
| 66 | ), |
| 67 | 'support' => array( |
| 68 | 'title' => __( 'Support', 'wp-external-links' ), |
| 69 | 'icon' => '<i class="fa fa-question" aria-hidden="true"></i>', |
| 70 | ), |
| 71 | ); |
| 72 | |
| 73 | // check excluded links tab available |
| 74 | if ( $this->get_option_value( 'excludes_as_internal_links', 'exceptions' ) ) { |
| 75 | unset( $this->tabs[ 'excluded-links' ] ); |
| 76 | } |
| 77 | |
| 78 | // get current tab |
| 79 | $this->current_tab = filter_input( INPUT_GET, 'tab', FILTER_SANITIZE_STRING ); |
| 80 | |
| 81 | // set default tab |
| 82 | if ( ! key_exists( $this->current_tab, $this->tabs ) ) { |
| 83 | reset( $this->tabs ); |
| 84 | $this->current_tab = key( $this->tabs ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get option value |
| 90 | * @param string $key |
| 91 | * @param string $type |
| 92 | * @return string |
| 93 | * @triggers E_USER_NOTICE Option value cannot be found |
| 94 | */ |
| 95 | public function get_option_value( $key, $type = null ) |
| 96 | { |
| 97 | if ( null === $type ) { |
| 98 | foreach ( $this->tabs as $tab_key => $values ) { |
| 99 | if ( ! isset( $values[ 'fields' ] ) ) { |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | $option_values = $values[ 'fields' ]->get_option_values(); |
| 104 | |
| 105 | if ( ! isset( $option_values[ $key ] ) ) { |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | return $option_values[ $key ]; |
| 110 | } |
| 111 | } else if ( isset( $this->tabs[ $type ][ 'fields' ] ) ) { |
| 112 | $option_values = $this->tabs[ $type ][ 'fields' ]->get_option_values(); |
| 113 | return $option_values[ $key ]; |
| 114 | } |
| 115 | |
| 116 | trigger_error( 'Option value "'. $key .'" cannot be found.' ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Action for "admin_menu" |
| 121 | */ |
| 122 | protected function action_admin_menu() |
| 123 | { |
| 124 | $capability = $this->network_page->get_option_value( 'capability' ); |
| 125 | |
| 126 | $own_admin_menu = $this->get_option_value( 'own_admin_menu', 'admin' ); |
| 127 | |
| 128 | if ( '1' === $own_admin_menu ) { |
| 129 | $this->page_hook = add_menu_page( |
| 130 | __( 'WP External Links' , 'wp-external-links' ) // page title |
| 131 | , __( 'External Links' , 'wp-external-links' ) // menu title |
| 132 | , $capability // capability |
| 133 | , $this->menu_slug // id |
| 134 | , $this->get_callback( 'show_admin_page' ) // callback |
| 135 | , 'none' // icon |
| 136 | , null // position |
| 137 | ); |
| 138 | } else { |
| 139 | $this->page_hook = add_options_page( |
| 140 | __( 'WP External Links' , 'wp-external-links' ) // page title |
| 141 | , __( 'External Links' , 'wp-external-links' ) // menu title |
| 142 | , $capability // capability |
| 143 | , $this->menu_slug // id |
| 144 | , $this->get_callback( 'show_admin_page' ) // callback |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | add_action( 'load-'. $this->page_hook, $this->get_callback( 'add_help_tabs' ) ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Set default option values for new created sites |
| 153 | * @param integer $blog_id |
| 154 | */ |
| 155 | protected function action_wpmu_new_blog( $blog_id ) |
| 156 | { |
| 157 | $default_site_id = $this->network_page->get_option_value( 'default_settings_site' ); |
| 158 | |
| 159 | foreach ( $this->tabs as $tab_key => $values ) { |
| 160 | if ( ! isset( $values[ 'fields' ] ) ) { |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | $option_name = $values[ 'fields' ]->get_setting( 'option_name' ); |
| 165 | |
| 166 | $default_option_values = get_blog_option( $default_site_id, $option_name, array() ); |
| 167 | update_blog_option( $blog_id, $option_name, $default_option_values ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Action for "admin_enqueue_scripts" |
| 173 | */ |
| 174 | protected function action_admin_enqueue_scripts() |
| 175 | { |
| 176 | wp_enqueue_style( 'font-awesome' ); |
| 177 | wp_enqueue_style( 'wpel-admin-style' ); |
| 178 | wp_enqueue_script( 'wpel-admin-script' ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Show Admin Page |
| 183 | */ |
| 184 | protected function show_admin_page() |
| 185 | { |
| 186 | $template_file = WPEL_Plugin::get_plugin_dir( '/templates/settings-page/main.php' ); |
| 187 | $page = $this->get_option_value( 'own_admin_menu' ) ? 'admin.php' : 'options-general.php'; |
| 188 | $page_url = admin_url() . $page .'?page='. $this->menu_slug; |
| 189 | |
| 190 | $template_vars = array( |
| 191 | 'tabs' => $this->tabs, |
| 192 | 'current_tab' => $this->current_tab, |
| 193 | 'page_url' => $page_url, |
| 194 | 'menu_slug' => $this->menu_slug, |
| 195 | 'own_admin_menu' => $this->get_option_value( 'own_admin_menu', 'admin' ), |
| 196 | ); |
| 197 | |
| 198 | $this->show_template( $template_file, $template_vars ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Add help tabs |
| 203 | */ |
| 204 | protected function add_help_tabs() |
| 205 | { |
| 206 | $screen = get_current_screen(); |
| 207 | return; |
| 208 | |
| 209 | $screen->add_help_tab( array( |
| 210 | 'id' => 'under-construction', |
| 211 | 'title' => __( 'Under Construction', 'wp-external-links' ), |
| 212 | 'callback' => $this->get_callback( 'show_help_tab' ), |
| 213 | ) ); |
| 214 | $screen->add_help_tab( array( |
| 215 | 'id' => 'data-attributes', |
| 216 | 'title' => __( 'Data Attributes', 'wp-external-links' ), |
| 217 | 'callback' => $this->get_callback( 'show_help_tab' ), |
| 218 | ) ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @param WP_Screen $screen |
| 223 | * @param array $args |
| 224 | */ |
| 225 | protected function show_help_tab( $screen, array $args ) |
| 226 | { |
| 227 | $template_file = WPEL_Plugin::get_plugin_dir( '/templates/settings-page/help-tabs/'. $args[ 'id' ] .'.php' ); |
| 228 | $this->show_template( $template_file ); |
| 229 | } |
| 230 | |
| 231 | } |
| 232 | |
| 233 | /*?>*/ |
| 234 |