| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_LOCALIZATION Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage Classes/Admin/Localization |
| 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_LOCALIZATION |
| 19 |
* |
| 20 |
* |
| 21 |
************************************************************************************************************************/ |
| 22 |
class WPPIZZA_LOCALIZATION{ |
| 23 |
/* |
| 24 |
* class ident |
| 25 |
* @var str |
| 26 |
* @since 3.0 |
| 27 |
*/ |
| 28 |
private $class_key='localization';/*to help consistency throughout class in various places*/ |
| 29 |
|
| 30 |
/* |
| 31 |
* titles/lables |
| 32 |
* @var str |
| 33 |
* @since 3.0 |
| 34 |
*/ |
| 35 |
private $submenu_page_header; |
| 36 |
private $submenu_page_title; |
| 37 |
private $submenu_caps_title; |
| 38 |
private $submenu_link_label; |
| 39 |
private $submenu_priority = 80; |
| 40 |
/****************************************************************************************************************** |
| 41 |
* |
| 42 |
* [CONSTRUCTOR] |
| 43 |
* |
| 44 |
* Setup localization submenu page |
| 45 |
* @since 3.0 |
| 46 |
* |
| 47 |
******************************************************************************************************************/ |
| 48 |
|
| 49 |
function __construct() { |
| 50 |
|
| 51 |
/*titles/labels throughout class*/ |
| 52 |
add_action('init', array( $this, 'init_admin_lables') ); |
| 53 |
/** registering submenu page -> priority 80 **/ |
| 54 |
add_action( 'admin_menu', array( $this, 'wppizza_register_submenu_page'), $this->submenu_priority ); |
| 55 |
/**add settings sections on this submenu page**/ |
| 56 |
add_action('current_screen', array( $this, 'wppizza_admin_settings_sections')); |
| 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 |
/**add to default options **/ |
| 65 |
//add_filter('wppizza_filter_setup_default_options', array( $this, 'wppizza_options_default')); |
| 66 |
/**validate options**/ |
| 67 |
add_filter('wppizza_filter_options_validate', array( $this, 'options_validate'), 10, 2 ); |
| 68 |
|
| 69 |
} |
| 70 |
/****************** |
| 71 |
* @since 3.0.26 |
| 72 |
* [admin ajax include file] |
| 73 |
*******************/ |
| 74 |
public function init_admin_lables(){ |
| 75 |
/*titles/labels throughout class*/ |
| 76 |
$this->submenu_page_header = apply_filters('wppizza_filter_admin_label_page_header_'.$this->class_key.'', __('Localization','wppizza-admin')); |
| 77 |
$this->submenu_page_title = apply_filters('wppizza_filter_admin_label_page_title_'.$this->class_key.'', __('Manage Localization','wppizza-admin')); |
| 78 |
$this->submenu_caps_title = apply_filters('wppizza_filter_admin_label_caps_title_'.$this->class_key.'', __('Localization','wppizza-admin')); |
| 79 |
$this->submenu_link_label = apply_filters('wppizza_filter_admin_label_link_label_'.$this->class_key.'', __('· Localization','wppizza-admin')); |
| 80 |
} |
| 81 |
/****************** |
| 82 |
* @since 3.0 |
| 83 |
* [admin ajax include file] |
| 84 |
*******************/ |
| 85 |
public function set_admin_ajax(){ |
| 86 |
require(WPPIZZA_PATH.'ajax/admin.ajax.wppizza.php'); |
| 87 |
die(); |
| 88 |
} |
| 89 |
/********************************************************* |
| 90 |
* |
| 91 |
* [add global helpers and enque js] |
| 92 |
* @since 3.0 |
| 93 |
* |
| 94 |
*********************************************************/ |
| 95 |
public function wppizza_add_helpers($current_screen){ |
| 96 |
if($current_screen->id == 'options' && isset($_POST[''.WPPIZZA_POST_TYPE.'_'.$this->class_key.''])){ |
| 97 |
/** return capabilities when saving options **/ |
| 98 |
add_filter( 'option_page_capability_'.WPPIZZA_SLUG.'', array($this, 'admin_option_page_capability' )); |
| 99 |
} |
| 100 |
if($current_screen->id == ''.WPPIZZA_POST_TYPE.'_page_'.$this->class_key.'' && $current_screen->post_type == WPPIZZA_POST_TYPE){ |
| 101 |
/***enqueue scripts and styles***/ |
| 102 |
add_action('admin_enqueue_scripts', array( $this, 'wppizza_enqueue_admin_scripts_and_styles')); |
| 103 |
} |
| 104 |
} |
| 105 |
/********************************************************* |
| 106 |
* |
| 107 |
* [enqueue] |
| 108 |
* @since 3.0 |
| 109 |
* |
| 110 |
*********************************************************/ |
| 111 |
public function wppizza_enqueue_admin_scripts_and_styles($hook) { |
| 112 |
/************ |
| 113 |
js |
| 114 |
***********/ |
| 115 |
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); |
| 116 |
wp_enqueue_script(WPPIZZA_SLUG.'_'.$this->class_key.''); |
| 117 |
} |
| 118 |
|
| 119 |
/********************************************************* |
| 120 |
* |
| 121 |
* [register submenu page] |
| 122 |
* @since 3.0 |
| 123 |
* |
| 124 |
*********************************************************/ |
| 125 |
public function wppizza_register_submenu_page(){ |
| 126 |
$submenu_page= array( |
| 127 |
'url' => 'edit.php?post_type='.WPPIZZA_SLUG.'', |
| 128 |
'title' => ''.WPPIZZA_NAME.' '.$this->submenu_page_title, |
| 129 |
'link_label' => $this->submenu_link_label, |
| 130 |
'caps' => 'wppizza_cap_'.$this->class_key.'', |
| 131 |
'key' => $this->class_key, |
| 132 |
'callback' => array($this, 'wppizza_admin_manage_sections') |
| 133 |
); |
| 134 |
/**add submenu page**/ |
| 135 |
$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']); |
| 136 |
/**add contextual help**/ |
| 137 |
add_action('load-'.$wppizza_submenu_page.'', array($this, 'wppizza_submenu_page_contextual_help')); |
| 138 |
} |
| 139 |
|
| 140 |
/********************************************************* |
| 141 |
* |
| 142 |
* [echo manage settings] |
| 143 |
* |
| 144 |
* wrap settings sections into div->form |
| 145 |
* add uniquely identifiable id's / classes |
| 146 |
* add h2 text |
| 147 |
* add uniquely identifiable hidden input |
| 148 |
* add submit button |
| 149 |
* |
| 150 |
* @since 3.0 |
| 151 |
* @return str |
| 152 |
* |
| 153 |
*********************************************************/ |
| 154 |
public function wppizza_admin_manage_sections(){ |
| 155 |
|
| 156 |
/* |
| 157 |
wppizza post type only |
| 158 |
*/ |
| 159 |
$screen = get_current_screen(); |
| 160 |
if($screen->post_type != WPPIZZA_POST_TYPE){return;} |
| 161 |
|
| 162 |
|
| 163 |
/** get sections settings**/ |
| 164 |
$settings=$this->wppizza_get_settings(true); |
| 165 |
|
| 166 |
/**wrap settings sections into div->form */ |
| 167 |
echo'<div id="'.WPPIZZA_SLUG.'-'.$this->class_key.'" class="'.WPPIZZA_SLUG.'-wrap '.WPPIZZA_SLUG.'-'.$this->class_key.'-wrap">'; |
| 168 |
|
| 169 |
|
| 170 |
echo"<div class='".WPPIZZA_SLUG."-admin-pageheader'>"; |
| 171 |
|
| 172 |
echo"<h2>"; |
| 173 |
/*help icon*/ |
| 174 |
echo"<a href='javascript:void(0)' class='wppizza-dashicons-admin button'><span class='dashicons dashicons-editor-help wppizza-show-admin-help'></span></a>"; |
| 175 |
echo"<span id='".WPPIZZA_SLUG."-header'>".WPPIZZA_NAME." ".$this->submenu_page_header."</span>"; |
| 176 |
echo"</h2>"; |
| 177 |
|
| 178 |
/*help text*/ |
| 179 |
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>"; |
| 180 |
|
| 181 |
echo"</div>"; |
| 182 |
|
| 183 |
/**update info / errors etc*/ |
| 184 |
settings_errors(); |
| 185 |
|
| 186 |
|
| 187 |
echo'<form action="options.php" method="post">'; |
| 188 |
echo'<input type="hidden" name="'.WPPIZZA_SLUG.'_'.$this->class_key.'" value="1" />'; |
| 189 |
|
| 190 |
/**echo wppizza settings field*/ |
| 191 |
settings_fields(WPPIZZA_SLUG); |
| 192 |
/**echo settings sections**/ |
| 193 |
foreach($settings['sections'] as $sections_key => $sections_label){ |
| 194 |
echo'<div class="wppizza-section wppizza-section-'.$sections_key.'">'; |
| 195 |
do_settings_sections($sections_key); |
| 196 |
echo'</div>'; |
| 197 |
} |
| 198 |
|
| 199 |
/**echo submit button or diabled button*/ |
| 200 |
if(WPPIZZA_DEV_ADMIN_NO_SAVE){ |
| 201 |
print '<input type="button" class="'.WPPIZZA_PREFIX.'-save-disabled" value="'.__('Saving Disabled', 'wppizza-admin').'">'; |
| 202 |
}else{ |
| 203 |
submit_button( __('Save Changes', 'wppizza-admin') ); |
| 204 |
} |
| 205 |
|
| 206 |
echo'</form>'; |
| 207 |
echo'</div>'; |
| 208 |
} |
| 209 |
/********************************************************* |
| 210 |
* |
| 211 |
* [add settings section(s) and fields] |
| 212 |
* |
| 213 |
* @since 3.0 |
| 214 |
* @return void |
| 215 |
* |
| 216 |
*********************************************************/ |
| 217 |
function wppizza_admin_settings_sections(){ |
| 218 |
global $current_screen; |
| 219 |
if($current_screen->id == ''.WPPIZZA_POST_TYPE.'_page_'.$this->class_key.'' && $current_screen->post_type == WPPIZZA_POST_TYPE){ |
| 220 |
/** get settings sections and fields **/ |
| 221 |
$settings=$this->wppizza_get_settings(true, true); |
| 222 |
|
| 223 |
/**iterate through sections*/ |
| 224 |
foreach($settings['sections'] as $sections_key=>$sections_label){ |
| 225 |
/**add section, emptiying label if no fields (if only help is defined for example)**/ |
| 226 |
$sections_label = !empty($settings['fields'][$sections_key]) ? $sections_label : '' ; |
| 227 |
add_settings_section($sections_key, $sections_label, array( $this, 'wppizza_admin_settings_section_header'), $sections_key); |
| 228 |
|
| 229 |
/**add section fields**/ |
| 230 |
if(!empty($settings['fields'][$sections_key])){ |
| 231 |
foreach($settings['fields'][$sections_key] as $fields_key=>$field_values){ |
| 232 |
add_settings_field($fields_key, $field_values[0], array( $this, 'wppizza_admin_settings_section_fields'), $sections_key, $sections_key, $field_values[1]); |
| 233 |
}} |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
} |
| 238 |
/********************************************************* |
| 239 |
* |
| 240 |
* [add contextual help to submenu page] |
| 241 |
* @since 3.0 |
| 242 |
* |
| 243 |
*********************************************************/ |
| 244 |
function wppizza_submenu_page_contextual_help(){ |
| 245 |
|
| 246 |
$screen = get_current_screen(); |
| 247 |
/** get settings sections and fields **/ |
| 248 |
$settings=$this->wppizza_get_settings(true, false, false, true); |
| 249 |
|
| 250 |
foreach($settings['sections'] as $section_key=>$section_label){ |
| 251 |
/**only add tab if there is actually any help to display**/ |
| 252 |
if(!empty($settings['help'][$section_key])){ |
| 253 |
/**initialize content for this tab**/ |
| 254 |
$help_content=''; |
| 255 |
foreach($settings['help'][$section_key] as $help_info){ |
| 256 |
/*add label*/ |
| 257 |
if(!empty($help_info['label'])){ |
| 258 |
$help_content.='<h3>'.$help_info['label'].'</h3>'; |
| 259 |
} |
| 260 |
/*add description*/ |
| 261 |
if(!empty($help_info['description']) && is_array($help_info['description'])){ |
| 262 |
foreach($help_info['description'] as $description){ |
| 263 |
$help_content.='<p>'.$description.'</p>'; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
/**add help tabs with content**/ |
| 268 |
$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>')); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
/********************************************************* |
| 275 |
* |
| 276 |
* [set settings section(s)] |
| 277 |
* @parameter $sections bool -> return sections |
| 278 |
* @parameter $fields bool -> return fields |
| 279 |
* @since 3.0 |
| 280 |
* @return array |
| 281 |
* |
| 282 |
*********************************************************/ |
| 283 |
private function wppizza_get_settings($sections=true, $fields=false, $inputs=false, $help=false){ |
| 284 |
|
| 285 |
/**ini settings array to splice into**/ |
| 286 |
$settings=array(); |
| 287 |
if($sections){ |
| 288 |
$settings['sections']=array(); |
| 289 |
} |
| 290 |
if($fields){ |
| 291 |
$settings['fields']=array(); |
| 292 |
} |
| 293 |
if($help){ |
| 294 |
$settings['help']=array(); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
allow filtering of settings sections |
| 299 |
to add additional if required |
| 300 |
**/ |
| 301 |
$settings=apply_filters('wppizza_filter_settings_sections_'.$this->class_key.'', $settings, $sections, $fields, $inputs, $help); |
| 302 |
|
| 303 |
return $settings; |
| 304 |
} |
| 305 |
|
| 306 |
/********************************************************* |
| 307 |
* |
| 308 |
* [add setting section(s) headers] |
| 309 |
* |
| 310 |
* @param array |
| 311 |
* @return str |
| 312 |
* |
| 313 |
*********************************************************/ |
| 314 |
public function wppizza_admin_settings_section_header($arg){ |
| 315 |
/*might come in useful somewhere*/ |
| 316 |
static $section_count=0;$section_count++; |
| 317 |
|
| 318 |
/**add more text headers if required*/ |
| 319 |
do_action('wppizza_settings_sections_header_'.$this->class_key.'', $arg, $section_count); |
| 320 |
} |
| 321 |
/********************************************************* |
| 322 |
* |
| 323 |
* [echo setting section(s) fields] |
| 324 |
* |
| 325 |
*********************************************************/ |
| 326 |
public function wppizza_admin_settings_section_fields($args){ |
| 327 |
/** |
| 328 |
wppizza options set |
| 329 |
**/ |
| 330 |
global $wppizza_options; |
| 331 |
|
| 332 |
/**plugin key/name - if we are writing/adding to the localization page with a different plugin we can use that plugins slug (sanitised)**/ |
| 333 |
$plugin_slug = empty($args['plugin_slug']) ? WPPIZZA_SLUG : wppizza_validate_alpha_only($args['plugin_slug']) ; |
| 334 |
/**option key - array key of parent option set in options table**/ |
| 335 |
$options_key=$args['option_key']; |
| 336 |
/**value key - array key of option set in options table (subkey of options_key)**/ |
| 337 |
$field=$args['value_key']; |
| 338 |
/**label (if any)**/ |
| 339 |
$label = !empty($args['label']) ? $args['label'] : '' ; |
| 340 |
/**description (if any)**/ |
| 341 |
$description = !empty($args['description']) ? ' <span class="description description_'.$field.'">'.implode(' ',$args['description']).'</span>' : '' ; |
| 342 |
/**is textarea**/ |
| 343 |
$is_textarea= (!empty($args['textarea']) || ( !empty($args['type']) && $args['type'] == 'html' )) ? true : false ; |
| 344 |
/**is plaintext textarea**/ |
| 345 |
$is_textarea_plaintext = (!empty($args['textarea_plaintext']) || ( !empty($args['type']) && $args['type'] == 'textarea' )) ? true : false ; |
| 346 |
/** get a distinctly set value (from external plugin for example) if set **/ |
| 347 |
$value = isset($args['value']) ? $args['value'] : $wppizza_options[$options_key][$field] ; |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
if($is_textarea){ |
| 352 |
|
| 353 |
$editorId="".strtolower(WPPIZZA_SLUG."_".$options_key."_".$field)."";/* WP 3.9 doesnt like brackets in id's*/ |
| 354 |
|
| 355 |
$editorName="".WPPIZZA_SLUG."[".$options_key."][".$field."]"; |
| 356 |
|
| 357 |
$editorSettings = array(); |
| 358 |
$editorSettings['teeny'] = 1; |
| 359 |
$editorSettings['wpautop'] = false; |
| 360 |
$editorSettings['textarea_name'] = $editorName; |
| 361 |
$editorSettings['editor_height'] = 100; |
| 362 |
$editorSettings['tinymce'] = true;/* no visual editor if false*/ |
| 363 |
$editorSettings['media_buttons'] = false ;//($field=='templates_footer_note') ? false : true;/* perhaps no media buttons only for footer note ?*/ |
| 364 |
|
| 365 |
/** |
| 366 |
output html editor |
| 367 |
**/ |
| 368 |
echo"<div class='".WPPIZZA_SLUG."_localization_textarea_wrap'>"; |
| 369 |
|
| 370 |
echo"<span>".$label."</span>"; |
| 371 |
|
| 372 |
echo"<div class='".WPPIZZA_SLUG."_localization_textarea'>"; |
| 373 |
|
| 374 |
wp_editor( $wppizza_options[$options_key][$field], $editorId, $editorSettings); |
| 375 |
|
| 376 |
echo"</div>"; |
| 377 |
|
| 378 |
echo"</div>"; |
| 379 |
|
| 380 |
}else{ |
| 381 |
|
| 382 |
/** |
| 383 |
output input field, label and description |
| 384 |
**/ |
| 385 |
echo '<label>'; |
| 386 |
|
| 387 |
/* |
| 388 |
output as plaintext textarea instead of type=text input |
| 389 |
*/ |
| 390 |
if($is_textarea_plaintext){ |
| 391 |
|
| 392 |
echo "<textarea name='".$plugin_slug."[".$options_key."][".$field."]' cols='51' rows='5'>".esc_html($value)."</textarea>"; |
| 393 |
|
| 394 |
}else{ |
| 395 |
|
| 396 |
echo "<input name='".$plugin_slug."[".$options_key."][".$field."]' size='50' type='text' value='".esc_html($value)."'>"; |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
print'' . $label . $description . ''; |
| 401 |
|
| 402 |
print'</label>'; |
| 403 |
} |
| 404 |
|
| 405 |
/******************************** |
| 406 |
* [add action for modules to hook into, also providing full args list if needed somewhere] |
| 407 |
********************************/ |
| 408 |
//do_action('wppizza_admin_settings_section_fields_'.$this->class_key.'', $wppizza_options, $options_key, $field, $label, $description, $args); |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
/**************************************************************** |
| 413 |
* |
| 414 |
* [validate options on save/update] |
| 415 |
* unlike other modules, this is in th emain subpage class |
| 416 |
* for easy validation across submodules |
| 417 |
* @since 3.0 |
| 418 |
* @return array() |
| 419 |
* |
| 420 |
****************************************************************/ |
| 421 |
function options_validate($options, $input){ |
| 422 |
/** |
| 423 |
make sure we get the full array on install/update |
| 424 |
**/ |
| 425 |
if ( empty( $_POST['_wp_http_referer'] ) ) { |
| 426 |
return $input; |
| 427 |
} |
| 428 |
|
| 429 |
if(isset($_POST[''.WPPIZZA_SLUG.'_'.$this->class_key.''])){ |
| 430 |
|
| 431 |
/* |
| 432 |
keys that are validated/allowed to have html |
| 433 |
*/ |
| 434 |
$allowHtml = apply_filters('wppizza_filter_localization_html', array()); |
| 435 |
|
| 436 |
/* |
| 437 |
array of localization variables to validate |
| 438 |
*/ |
| 439 |
$options[$this->class_key] = array();//initialize as empty array |
| 440 |
|
| 441 |
/* |
| 442 |
validate |
| 443 |
*/ |
| 444 |
foreach($input[$this->class_key] as $key=>$string){ |
| 445 |
/*add new value , but keep desciption (as its not editable on frontend)*/ |
| 446 |
if(in_array($key, $allowHtml)){$html=1;}else{$html=false;} |
| 447 |
$options[$this->class_key][$key] = wppizza_validate_string($string, $html); |
| 448 |
|
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
return $options; |
| 453 |
} |
| 454 |
|
| 455 |
/********************************************************* |
| 456 |
* |
| 457 |
* [define caps] |
| 458 |
* @since 3.0 |
| 459 |
* |
| 460 |
*********************************************************/ |
| 461 |
function wppizza_filter_define_caps($caps){ |
| 462 |
|
| 463 |
/**add editing capability for this page**/ |
| 464 |
$caps[$this->class_key]=array('name'=>$this->submenu_caps_title ,'cap'=>'wppizza_cap_'.$this->class_key.''); |
| 465 |
|
| 466 |
return $caps; |
| 467 |
} |
| 468 |
|
| 469 |
/********************************************************* |
| 470 |
* |
| 471 |
* [set required capability for this page] |
| 472 |
* @since 3.0 |
| 473 |
* |
| 474 |
*********************************************************/ |
| 475 |
function admin_option_page_capability($capability) { |
| 476 |
$capability = 'wppizza_cap_'.$this->class_key.''; |
| 477 |
return $capability; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/*************************************************************** |
| 482 |
* |
| 483 |
* [ini class] |
| 484 |
* |
| 485 |
***************************************************************/ |
| 486 |
$WPPIZZA_LOCALIZATION = new WPPIZZA_LOCALIZATION(); |
| 487 |
?> |