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-network-page.php
185 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPEL_Network_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_Network_Page extends WPRun_Base_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | private $menu_slug = 'wpel-network-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 | * Initialize |
| 31 | */ |
| 32 | protected function init( array $fields_objects ) |
| 33 | { |
| 34 | $this->tabs = array( |
| 35 | 'network-settings' => array( |
| 36 | 'title' => __( 'Multi Site Settings', 'wp-external-links' ), |
| 37 | 'icon' => '<i class="fa fa-sitemap" aria-hidden="true"></i>', |
| 38 | 'fields' => $fields_objects[ 'network-settings' ], |
| 39 | ), |
| 40 | 'network-admin-settings' => array( |
| 41 | 'title' => __( 'Admin Settings', 'wp-external-links' ), |
| 42 | 'icon' => '<i class="fa fa-cogs" aria-hidden="true"></i>', |
| 43 | 'fields' => $fields_objects[ 'network-admin-settings' ], |
| 44 | ), |
| 45 | 'support' => array( |
| 46 | 'title' => __( 'Support', 'wp-external-links' ), |
| 47 | 'icon' => '<i class="fa fa-question" aria-hidden="true"></i>', |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | // get current tab |
| 52 | //phpcs:ignore because nonce is not needed as this just sets the current tab and can be linked directly |
| 53 | if(isset($_GET['tab'])){ //phpcs:ignore |
| 54 | $this->current_tab = sanitize_text_field($_GET['tab']); //phpcs:ignore |
| 55 | } |
| 56 | |
| 57 | // set default tab |
| 58 | if ( !isset($this->current_tab) || ! key_exists( $this->current_tab, $this->tabs ) ) { |
| 59 | reset( $this->tabs ); |
| 60 | $this->current_tab = key( $this->tabs ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get option value |
| 66 | * @param string $key |
| 67 | * @param string $type |
| 68 | * @return string |
| 69 | * @triggers E_USER_NOTICE Option value cannot be found |
| 70 | */ |
| 71 | public function get_option_value( $key, $type = null ) |
| 72 | { |
| 73 | if ( null === $type ) { |
| 74 | foreach ( $this->tabs as $tab_key => $values ) { |
| 75 | if ( ! isset( $values[ 'fields' ] ) ) { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | $option_values = $values[ 'fields' ]->get_option_values(); |
| 80 | |
| 81 | if ( ! isset( $option_values[ $key ] ) ) { |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | return $option_values[ $key ]; |
| 86 | } |
| 87 | } else if ( isset( $this->tabs[ $type ][ 'fields' ] ) ) { |
| 88 | $option_values = $this->tabs[ $type ][ 'fields' ]->get_option_values(); |
| 89 | return $option_values[ $key ]; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Action for "network_admin_menu" |
| 95 | */ |
| 96 | protected function action_network_admin_menu() |
| 97 | { |
| 98 | $own_admin_menu = $this->get_option_value( 'own_admin_menu' ); |
| 99 | |
| 100 | if ( '1' === $own_admin_menu ) { |
| 101 | $this->page_hook = add_menu_page( |
| 102 | __( 'WP External Links' , 'wp-external-links' ) // page title |
| 103 | , __( 'WP External Links' , 'wp-external-links' ) // menu title |
| 104 | , 'manage_network' // capability |
| 105 | , $this->menu_slug // menu slug |
| 106 | , $this->get_callback( 'show_network_page' ) // callback |
| 107 | , WPEL_PLUGIN_URL . '/public/images/icon-small.png' // icon // icon |
| 108 | , null // position |
| 109 | ); |
| 110 | } else { |
| 111 | $this->page_hook = add_submenu_page( |
| 112 | 'settings.php' // parent slug |
| 113 | , __( 'WP External Links' , 'wp-external-links' ) // page title |
| 114 | , __( 'WP External Links' , 'wp-external-links' ) // menu title |
| 115 | , 'manage_options' // capability |
| 116 | , $this->menu_slug // menu slug |
| 117 | , $this->get_callback( 'show_network_page' ) // callback |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | add_action( 'load-'. $this->page_hook, $this->get_callback( 'add_help_tabs' ) ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Action for "admin_enqueue_scripts" |
| 126 | */ |
| 127 | protected function action_admin_enqueue_scripts() |
| 128 | { |
| 129 | $current_screen = get_current_screen(); |
| 130 | |
| 131 | if($current_screen->id == 'toplevel_page_wpel-network-settings-page-network' || $current_screen->id == 'settings_page_wpel-network-settings-page-network'){ |
| 132 | wp_enqueue_style( 'wpel-font-awesome' ); |
| 133 | wp_enqueue_style( 'wpel-admin-style' ); |
| 134 | wp_enqueue_script( 'wpel-admin-script' ); |
| 135 | } |
| 136 | |
| 137 | wp_enqueue_style( 'wpel-admin-global-style' ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Show Admin Page |
| 142 | */ |
| 143 | protected function show_network_page() |
| 144 | { |
| 145 | $template_file = WPEL_Plugin::get_plugin_dir( '/templates/network-page/main.php' ); |
| 146 | $page = $this->get_option_value( 'own_admin_menu' ) ? 'admin.php' : 'settings.php'; |
| 147 | $page_url = network_admin_url() . $page .'?page='. $this->menu_slug; |
| 148 | |
| 149 | $template_vars = array( |
| 150 | 'tabs' => $this->tabs, |
| 151 | 'current_tab' => $this->current_tab, |
| 152 | 'page_url' => $page_url, |
| 153 | 'menu_slug' => $this->menu_slug, |
| 154 | 'own_admin_menu' => $this->get_option_value( 'own_admin_menu' ), |
| 155 | ); |
| 156 | |
| 157 | $this->show_template( $template_file, $template_vars ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Add help tabs |
| 162 | */ |
| 163 | protected function add_help_tabs() |
| 164 | { |
| 165 | $screen = get_current_screen(); |
| 166 | |
| 167 | $screen->add_help_tab( array( |
| 168 | 'id' => 'under-construction', |
| 169 | 'title' => __( 'Under Construction', 'wp-external-links' ), |
| 170 | 'callback' => $this->get_callback( 'show_help_tab' ), |
| 171 | ) ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @param WP_Screen $screen |
| 176 | * @param array $args |
| 177 | */ |
| 178 | protected function show_help_tab( $screen, array $args ) |
| 179 | { |
| 180 | $template_file = WPEL_Plugin::get_plugin_dir( '/templates/network-page/help-tabs/'. $args[ 'id' ] .'.php' ); |
| 181 | $this->show_template( $template_file ); |
| 182 | } |
| 183 | |
| 184 | } |
| 185 |