| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_SETTINGS Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage Submenu Pages / Classes / Settings |
| 7 |
* @copyright Copyright (c) 2015, Oliver Bach |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 3.0 |
| 10 |
* |
| 11 |
*/ |
| 12 |
if ( ! defined( 'ABSPATH' ) ) exit;/*Exit if accessed directly*/ |
| 13 |
|
| 14 |
|
| 15 |
/**************************************************************************************************************************************************************** |
| 16 |
* |
| 17 |
* |
| 18 |
* WPPIZZA_SETTINGS CLASS |
| 19 |
* |
| 20 |
* |
| 21 |
****************************************************************************************************************************************************************/ |
| 22 |
class WPPIZZA_SUBPAGES_SETTINGS{ |
| 23 |
/* |
| 24 |
* wppizza_settings class ident |
| 25 |
* @var str |
| 26 |
* @since 3.0 |
| 27 |
*/ |
| 28 |
private $class_key='settings'; |
| 29 |
/* |
| 30 |
* titles/lables |
| 31 |
* @var str |
| 32 |
* @since 3.0 |
| 33 |
*/ |
| 34 |
private $submenu_page_header; |
| 35 |
private $submenu_page_title; |
| 36 |
private $submenu_caps_title; |
| 37 |
private $submenu_link_label; |
| 38 |
private $submenu_priority = 0; |
| 39 |
/****************************************************************************************************************** |
| 40 |
* |
| 41 |
* [CONSTRUCTOR] |
| 42 |
* |
| 43 |
* Setup wppizza_settings subpage |
| 44 |
* @since 3.0 |
| 45 |
* |
| 46 |
******************************************************************************************************************/ |
| 47 |
function __construct() { |
| 48 |
|
| 49 |
/*titles/labels throughout class*/ |
| 50 |
add_action('init', array( $this, 'init_admin_lables') ); |
| 51 |
/** registering submenu page -> priority 0 **/ |
| 52 |
add_action('admin_menu', array( $this, 'wppizza_register_submenu_page'), $this->submenu_priority ); |
| 53 |
|
| 54 |
/**add settings sections on this submenu page**/ |
| 55 |
add_action('current_screen', array( $this, 'wppizza_admin_settings_sections')); |
| 56 |
|
| 57 |
/*register capabilities for this page*/ |
| 58 |
add_filter('wppizza_filter_define_caps', array( $this, 'wppizza_filter_define_caps'), $this->submenu_priority); |
| 59 |
/*execute some helper functions once to use their return multiple times */ |
| 60 |
add_action('current_screen', array( $this, 'wppizza_add_helpers') ); |
| 61 |
/**admin ajax**/ |
| 62 |
add_action('wp_ajax_wppizza_admin_'.$this->class_key.'_ajax', array($this, 'set_admin_ajax') ); |
| 63 |
} |
| 64 |
|
| 65 |
/****************** |
| 66 |
* @since 3.0.26 |
| 67 |
* [admin ajax include file] |
| 68 |
*******************/ |
| 69 |
public function init_admin_lables(){ |
| 70 |
/*titles/labels throughout class*/ |
| 71 |
$this->submenu_page_header = apply_filters('wppizza_filter_admin_label_page_header_'.$this->class_key.'', __('Global Settings','wppizza-admin')); |
| 72 |
$this->submenu_page_title = apply_filters('wppizza_filter_admin_label_page_title_'.$this->class_key.'', __('Manage Settings','wppizza-admin')); |
| 73 |
$this->submenu_caps_title = apply_filters('wppizza_filter_admin_label_caps_title_'.$this->class_key.'', __('Settings','wppizza-admin')); |
| 74 |
$this->submenu_link_label = apply_filters('wppizza_filter_admin_label_link_label_'.$this->class_key.'', __('· Settings','wppizza-admin')); |
| 75 |
} |
| 76 |
|
| 77 |
/****************** |
| 78 |
* @since 3.0 |
| 79 |
* [admin ajax include file] |
| 80 |
*******************/ |
| 81 |
public function set_admin_ajax(){ |
| 82 |
require(WPPIZZA_PATH.'ajax/admin.ajax.wppizza.php'); |
| 83 |
die(); |
| 84 |
} |
| 85 |
|
| 86 |
/********************************************************* |
| 87 |
* |
| 88 |
* [add contextual help to submenu page] |
| 89 |
* @since 3.0 |
| 90 |
* |
| 91 |
*********************************************************/ |
| 92 |
function wppizza_submenu_page_contextual_help(){ |
| 93 |
|
| 94 |
$screen = get_current_screen(); |
| 95 |
/** get settings sections and fields **/ |
| 96 |
$settings=$this->wppizza_get_settings(true, false, false, true); |
| 97 |
|
| 98 |
foreach($settings['sections'] as $section_key=>$section_label){ |
| 99 |
/**only add tab if there is actually any help to display**/ |
| 100 |
if(!empty($settings['help'][$section_key])){ |
| 101 |
/**initialize content for this tab**/ |
| 102 |
$help_content=''; |
| 103 |
foreach($settings['help'][$section_key] as $help_info){ |
| 104 |
/*add label*/ |
| 105 |
if(!empty($help_info['label'])){ |
| 106 |
$help_content.='<h3>'.$help_info['label'].'</h3>'; |
| 107 |
} |
| 108 |
/*add description*/ |
| 109 |
if(!empty($help_info['description']) && is_array($help_info['description'])){ |
| 110 |
foreach($help_info['description'] as $description){ |
| 111 |
$help_content.='<p>'.$description.'</p>'; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
/**add help tabs with content**/ |
| 116 |
$screen->add_help_tab( array('id' => 'wppizza_'.$this->class_key.'_'.$section_key.'', 'title' => $section_label, 'content' => '<div class="wppizza_admin_context_help">'.$help_content.'</div>')); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
/********************************************************* |
| 122 |
* |
| 123 |
* [define caps] |
| 124 |
* @since 3.0 |
| 125 |
* |
| 126 |
*********************************************************/ |
| 127 |
function wppizza_filter_define_caps($caps){ |
| 128 |
|
| 129 |
/**add editing capability for this page**/ |
| 130 |
$caps[$this->class_key]=array('name'=>$this->submenu_caps_title, 'cap'=>'wppizza_cap_'.$this->class_key.''); |
| 131 |
|
| 132 |
return $caps; |
| 133 |
} |
| 134 |
/********************************************************* |
| 135 |
* |
| 136 |
* [set required capability for this page] |
| 137 |
* @since 3.0 |
| 138 |
* |
| 139 |
*********************************************************/ |
| 140 |
function admin_option_page_capability($capability) { |
| 141 |
$capability = 'wppizza_cap_'.$this->class_key.''; |
| 142 |
return $capability; |
| 143 |
} |
| 144 |
/********************************************************* |
| 145 |
* |
| 146 |
* [register submenu page] |
| 147 |
* @since 3.0 |
| 148 |
* |
| 149 |
*********************************************************/ |
| 150 |
function wppizza_register_submenu_page(){ |
| 151 |
/**submenu page values*/ |
| 152 |
$submenu_page = array( |
| 153 |
'url' => 'edit.php?post_type='.WPPIZZA_SLUG.'', |
| 154 |
'title' => ''.WPPIZZA_NAME.' '.$this->submenu_page_title, |
| 155 |
'link_label' => $this->submenu_link_label, |
| 156 |
'caps' => 'wppizza_cap_'.$this->class_key.'', |
| 157 |
'key' => $this->class_key, |
| 158 |
'callback' => array($this, 'wppizza_admin_manage_sections') |
| 159 |
); |
| 160 |
/**add submenu page**/ |
| 161 |
$wppizza_submenu_page=add_submenu_page($submenu_page['url'], $submenu_page['title'], $submenu_page['link_label'], $submenu_page['caps'], $submenu_page['key'], $submenu_page['callback']); |
| 162 |
/**add contextual help**/ |
| 163 |
add_action('load-'.$wppizza_submenu_page.'', array($this, 'wppizza_submenu_page_contextual_help')); |
| 164 |
} |
| 165 |
|
| 166 |
/********************************************************* |
| 167 |
* |
| 168 |
* [add helpers] |
| 169 |
* @since 3.0 |
| 170 |
* |
| 171 |
*********************************************************/ |
| 172 |
function wppizza_add_helpers($current_screen){ |
| 173 |
if($current_screen->id == 'options' && isset($_POST[''.WPPIZZA_POST_TYPE.'_'.$this->class_key.''])){ |
| 174 |
/** return capabilities when saving options **/ |
| 175 |
add_filter( 'option_page_capability_'.WPPIZZA_SLUG.'', array($this, 'admin_option_page_capability' )); |
| 176 |
} |
| 177 |
if($current_screen->id == ''.WPPIZZA_POST_TYPE.'_page_'.$this->class_key.'' && $current_screen->post_type == WPPIZZA_POST_TYPE){ |
| 178 |
/***enqueue scripts and styles***/ |
| 179 |
add_action('admin_enqueue_scripts', array( $this, 'wppizza_enqueue_admin_scripts_and_styles')); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/********************************************************* |
| 184 |
* |
| 185 |
* [enqueue] |
| 186 |
* @since 3.0 |
| 187 |
* |
| 188 |
*********************************************************/ |
| 189 |
public function wppizza_enqueue_admin_scripts_and_styles($hook) { |
| 190 |
|
| 191 |
/************ |
| 192 |
css |
| 193 |
***********/ |
| 194 |
|
| 195 |
/** chosen **/ |
| 196 |
wp_register_style(WPPIZZA_SLUG.'-chosen', plugins_url( 'css/wppizza-chosen.min.css', WPPIZZA_PLUGIN_PATH ), array(), WPPIZZA_VERSION); |
| 197 |
wp_enqueue_style(WPPIZZA_SLUG.'-chosen'); |
| 198 |
|
| 199 |
/************ |
| 200 |
js |
| 201 |
***********/ |
| 202 |
|
| 203 |
/** chosen **/ |
| 204 |
wp_register_script(WPPIZZA_SLUG.'-chosen', plugins_url( 'js/chosen.jquery.min.js', WPPIZZA_PLUGIN_PATH ), array('jquery'), WPPIZZA_VERSION ,true); |
| 205 |
wp_enqueue_script(WPPIZZA_SLUG.'-chosen'); |
| 206 |
|
| 207 |
wp_register_script(WPPIZZA_SLUG.'_'.$this->class_key.'', plugins_url( 'js/scripts.admin.'.$this->class_key.'.js', WPPIZZA_PLUGIN_PATH ), array('jquery'), WPPIZZA_VERSION ,true); |
| 208 |
wp_enqueue_script(WPPIZZA_SLUG.'_'.$this->class_key.''); |
| 209 |
} |
| 210 |
/********************************************************* |
| 211 |
* |
| 212 |
* [set settings section(s)] |
| 213 |
* @parameter $sections bool -> return sections |
| 214 |
* @parameter $fields bool -> return fields |
| 215 |
* @since 3.0 |
| 216 |
* @return array |
| 217 |
* |
| 218 |
*********************************************************/ |
| 219 |
function wppizza_get_settings($sections=true, $fields=false, $inputs=false, $help=false){ |
| 220 |
global $blog_id; |
| 221 |
/**ini settings array to splice into**/ |
| 222 |
$settings=array(); |
| 223 |
if($sections){ |
| 224 |
$settings['sections']=array(); |
| 225 |
} |
| 226 |
if($fields){ |
| 227 |
$settings['fields']=array(); |
| 228 |
} |
| 229 |
if($help){ |
| 230 |
$settings['help']=array(); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
allow filtering of settings sections |
| 236 |
do add additional if required |
| 237 |
**/ |
| 238 |
$settings=apply_filters('wppizza_filter_settings_sections_'.$this->class_key.'', $settings, $sections, $fields, $inputs, $help); |
| 239 |
|
| 240 |
return $settings; |
| 241 |
} |
| 242 |
/********************************************************* |
| 243 |
* |
| 244 |
* [add settings section(s) and fields] |
| 245 |
* |
| 246 |
* @since 3.0 |
| 247 |
* @return void |
| 248 |
* |
| 249 |
*********************************************************/ |
| 250 |
function wppizza_admin_settings_sections(){ |
| 251 |
global $current_screen; |
| 252 |
if($current_screen->id == ''.WPPIZZA_POST_TYPE.'_page_'.$this->class_key.'' && $current_screen->post_type == WPPIZZA_POST_TYPE){ |
| 253 |
/** get settings sections and fields **/ |
| 254 |
$settings=$this->wppizza_get_settings(true, true); |
| 255 |
|
| 256 |
/**iterate through sections*/ |
| 257 |
foreach($settings['sections'] as $sections_key=>$sections_label){ |
| 258 |
/**add section**/ |
| 259 |
add_settings_section($sections_key, $sections_label, array( $this, 'wppizza_admin_settings_section_header'), $sections_key); |
| 260 |
|
| 261 |
/**add section fields**/ |
| 262 |
foreach($settings['fields'][$sections_key] as $fields_key=>$field_values){ |
| 263 |
add_settings_field($fields_key, $field_values[0], array( $this, 'wppizza_admin_settings_section_fields'), $sections_key, $sections_key, $field_values[1]); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
} |
| 269 |
/********************************************************* |
| 270 |
* |
| 271 |
* [add setting section(s) headers] |
| 272 |
* |
| 273 |
* @param array |
| 274 |
* @return str |
| 275 |
* |
| 276 |
*********************************************************/ |
| 277 |
function wppizza_admin_settings_section_header($arg){ |
| 278 |
/*might come in useful somewhere*/ |
| 279 |
static $section_count=0;$section_count++; |
| 280 |
/**add more text headers if required*/ |
| 281 |
do_action('wppizza_settings_sections_header_'.$this->class_key.'', $arg, $section_count); |
| 282 |
} |
| 283 |
|
| 284 |
/********************************************************* |
| 285 |
* |
| 286 |
* [echo manage settings] |
| 287 |
* |
| 288 |
* wrap settings sections into div->form |
| 289 |
* add uniquely identifiable id's / classes |
| 290 |
* add h2 text |
| 291 |
* add uniquely identifiable hidden input |
| 292 |
* add submit button |
| 293 |
* |
| 294 |
* @since 3.0 |
| 295 |
* @return str |
| 296 |
* |
| 297 |
*********************************************************/ |
| 298 |
function wppizza_admin_manage_sections(){ |
| 299 |
/* |
| 300 |
wppizza post type only |
| 301 |
*/ |
| 302 |
$screen = get_current_screen(); |
| 303 |
if($screen->post_type != WPPIZZA_POST_TYPE){return;} |
| 304 |
|
| 305 |
|
| 306 |
/** get sections settings**/ |
| 307 |
$settings=$this->wppizza_get_settings(true); |
| 308 |
|
| 309 |
/**wrap settings sections into div->form */ |
| 310 |
echo'<div id="'.WPPIZZA_SLUG.'-'.$this->class_key.'" class="'.WPPIZZA_SLUG.'-wrap '.WPPIZZA_SLUG.'-'.$this->class_key.'-wrap">'; |
| 311 |
|
| 312 |
|
| 313 |
echo"<div class='".WPPIZZA_SLUG."-admin-pageheader'>"; |
| 314 |
|
| 315 |
echo"<h2>"; |
| 316 |
/*help icon*/ |
| 317 |
echo"<a href='javascript:void(0)' class='wppizza-dashicons-admin button'><span class='dashicons dashicons-editor-help wppizza-show-admin-help'></span></a>"; |
| 318 |
echo"<span id='".WPPIZZA_SLUG."-header'>".WPPIZZA_NAME." ".$this->submenu_page_header."</span>"; |
| 319 |
echo"</h2>"; |
| 320 |
|
| 321 |
/*help text*/ |
| 322 |
echo"<span class='wppizza-help-hint'>".__('Note: Some options will have more details in the <a href="javascript:void(0)" class="wppizza-show-admin-help">help screen</a>','wppizza-admin')."</span>"; |
| 323 |
|
| 324 |
echo"</div>"; |
| 325 |
|
| 326 |
/**update info / errors etc*/ |
| 327 |
settings_errors(); |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
echo'<form action="options.php" method="post">'; |
| 332 |
echo'<input type="hidden" name="'.WPPIZZA_SLUG.'_'.$this->class_key.'" value="1" />'; |
| 333 |
|
| 334 |
/**echo wppizza settings field*/ |
| 335 |
settings_fields(WPPIZZA_SLUG); |
| 336 |
|
| 337 |
/**echo settings sections**/ |
| 338 |
foreach($settings['sections'] as $sections_key => $sections_values){ |
| 339 |
echo'<div class="wppizza-section wppizza-section-'.$sections_key.'">'; |
| 340 |
do_settings_sections($sections_key); |
| 341 |
echo'</div>'; |
| 342 |
} |
| 343 |
|
| 344 |
/**echo submit button or diabled button*/ |
| 345 |
if(WPPIZZA_DEV_ADMIN_NO_SAVE){ |
| 346 |
print '<input type="button" class="'.WPPIZZA_PREFIX.'-save-disabled" value="'.__('Saving Disabled', 'wppizza-admin').'">'; |
| 347 |
}else{ |
| 348 |
submit_button( __('Save Changes', 'wppizza-admin') ); |
| 349 |
} |
| 350 |
|
| 351 |
echo'</form>'; |
| 352 |
echo'</div>'; |
| 353 |
} |
| 354 |
|
| 355 |
/********************************************************* |
| 356 |
* |
| 357 |
* [echo setting section(s) fields] |
| 358 |
* |
| 359 |
*********************************************************/ |
| 360 |
function wppizza_admin_settings_section_fields($args){ |
| 361 |
/** wppizza options set **/ |
| 362 |
global $wppizza_options; |
| 363 |
|
| 364 |
/**option key - array key of parent option set in options table**/ |
| 365 |
$options_key = !empty($args['option_key']) ? $args['option_key'] : false; |
| 366 |
/**value key - array key of option set in options table (subkey of options_key)**/ |
| 367 |
$field = !empty($args['value_key']) ? $args['value_key'] : false; |
| 368 |
/**label (if any)**/ |
| 369 |
$label = !empty($args['label']) ? $args['label'] : '' ; |
| 370 |
/**description (if any)**/ |
| 371 |
$description = !empty($args['description']) ? '<br /><span class="description description_'.$field.'">'.implode('</span><br /><span class="description description_'.$field.'">',$args['description']).'</span>' : '' ; |
| 372 |
|
| 373 |
/******************************** |
| 374 |
* [add action for modules to hook into, also providing full args list if needed somewhere] |
| 375 |
********************************/ |
| 376 |
do_action('wppizza_admin_settings_section_fields_'.$this->class_key.'', $wppizza_options, $options_key, $field, $label, $description, $args); |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
/*************************************************************** |
| 382 |
* |
| 383 |
* [ini] |
| 384 |
* |
| 385 |
***************************************************************/ |
| 386 |
$WPPIZZA_SUBPAGES_SETTINGS = new WPPIZZA_SUBPAGES_SETTINGS(); |
| 387 |
?> |