| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_MODULE_SETTINGS_MISCELLANEOUS Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage WPPIZZA_MODULE_SETTINGS_MISCELLANEOUS |
| 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 |
* |
| 19 |
* |
| 20 |
* |
| 21 |
* |
| 22 |
************************************************************************************************************************/ |
| 23 |
class WPPIZZA_MODULE_SETTINGS_MISCELLANEOUS{ |
| 24 |
|
| 25 |
private $settings_page = 'settings';/* which admin subpage (identified there by this->class_key) are we adding this to */ |
| 26 |
|
| 27 |
|
| 28 |
private $section_key = 'miscellaneous';/* must be unique */ |
| 29 |
|
| 30 |
|
| 31 |
function __construct() { |
| 32 |
/********************************************************** |
| 33 |
[add settings to admin] |
| 34 |
***********************************************************/ |
| 35 |
if(is_admin()){ |
| 36 |
/* add admin options settings page*/ |
| 37 |
add_filter('wppizza_filter_settings_sections_'.$this->settings_page.'', array($this, 'admin_options_settings'), 60, 5); |
| 38 |
/* add admin options settings page fields */ |
| 39 |
add_action('wppizza_admin_settings_section_fields_'.$this->settings_page.'', array($this, 'admin_options_fields_settings'), 10, 5); |
| 40 |
/**add default options **/ |
| 41 |
add_filter('wppizza_filter_setup_default_options', array( $this, 'options_default')); |
| 42 |
/**validate options**/ |
| 43 |
add_filter('wppizza_filter_options_validate', array( $this, 'options_validate'), 10, 2 ); |
| 44 |
} |
| 45 |
/********************************************************** |
| 46 |
[filter/actions depending on settings] |
| 47 |
***********************************************************/ |
| 48 |
if(!is_admin()){ |
| 49 |
/*dequeue if set**/ |
| 50 |
add_action('wp_print_scripts', array( $this, 'dequeue_scripts'),100); |
| 51 |
/*force inclusion of all scripts and styles on all pages **/ |
| 52 |
add_filter('wppizza_filter_force_scripts_and_styles', array( $this, 'force_scripts_and_styles')); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/******************************************************************************************************************************************************* |
| 57 |
* |
| 58 |
* |
| 59 |
* |
| 60 |
* [frontend filters] |
| 61 |
* |
| 62 |
* |
| 63 |
* |
| 64 |
********************************************************************************************************************************************************/ |
| 65 |
/************************************* |
| 66 |
|
| 67 |
force inclusion of all scripts and styles on all pages |
| 68 |
|
| 69 |
**************************************/ |
| 70 |
function force_scripts_and_styles($bool){ |
| 71 |
global $wppizza_options; |
| 72 |
$bool = !empty($wppizza_options[$this->settings_page]['always_load_all_scripts_and_styles']) ? true : false; |
| 73 |
return $bool; |
| 74 |
} |
| 75 |
/**************************************************************************************************************** |
| 76 |
* |
| 77 |
* [dequeue scripts on demand] |
| 78 |
* |
| 79 |
****************************************************************************************************************/ |
| 80 |
function dequeue_scripts(){ |
| 81 |
global $wppizza_options; |
| 82 |
|
| 83 |
if(!empty($wppizza_options[$this->settings_page]['dequeue_scripts'])){ |
| 84 |
/*dequeue main*/ |
| 85 |
if($wppizza_options[$this->settings_page]['dequeue_scripts']=='all'){ |
| 86 |
wp_dequeue_script(WPPIZZA_SLUG); |
| 87 |
} |
| 88 |
/*dequeue jquery validate too or only*/ |
| 89 |
if($wppizza_options[$this->settings_page]['dequeue_scripts']=='all' || $wppizza_options[$this->settings_page]['dequeue_scripts']=='validation'){ |
| 90 |
wp_dequeue_script(WPPIZZA_SLUG.'-validate'); |
| 91 |
wp_dequeue_script(WPPIZZA_SLUG.'-validate-methods'); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/******************************************************************************************************************************************************* |
| 97 |
* |
| 98 |
* |
| 99 |
* |
| 100 |
* [add admin page options] |
| 101 |
* |
| 102 |
* |
| 103 |
* |
| 104 |
********************************************************************************************************************************************************/ |
| 105 |
|
| 106 |
/*------------------------------------------------------------------------------ |
| 107 |
# |
| 108 |
# |
| 109 |
# [settings page] |
| 110 |
# |
| 111 |
# |
| 112 |
------------------------------------------------------------------------------*/ |
| 113 |
|
| 114 |
/*------------------------------------------------------------------------------ |
| 115 |
# [settings section - setting page] |
| 116 |
# @since 3.0 |
| 117 |
# @return array() |
| 118 |
------------------------------------------------------------------------------*/ |
| 119 |
function admin_options_settings($settings, $sections, $fields, $inputs, $help){ |
| 120 |
|
| 121 |
/*section*/ |
| 122 |
if($sections){ |
| 123 |
$settings['sections'][$this->section_key] = __('Miscellaneous', 'wppizza-admin'); |
| 124 |
} |
| 125 |
/*help*/ |
| 126 |
if($help){ |
| 127 |
$settings['help'][$this->section_key][] = array( |
| 128 |
'label'=>__('Miscellaneous Options', 'wppizza-admin'), |
| 129 |
'description'=>array( |
| 130 |
__('Please adjust settings as appropriate according to the information provided next to each individual option.', 'wppizza-admin') |
| 131 |
) |
| 132 |
); |
| 133 |
} |
| 134 |
/*fields*/ |
| 135 |
if($fields){ |
| 136 |
$field = 'always_load_all_scripts_and_styles'; |
| 137 |
$settings['fields'][$this->section_key][$field] = array( __('Load css and js on all pages', 'wppizza-admin'), array( |
| 138 |
'value_key'=>$field, |
| 139 |
'option_key'=>$this->settings_page, |
| 140 |
'label'=>__('load *all* css and javascripts on all pages. Might be necessary for themes that hijack normal pagelinks', 'wppizza-admin'), |
| 141 |
'description'=>array() |
| 142 |
)); |
| 143 |
$field = 'dequeue_scripts'; |
| 144 |
$settings['fields'][$this->section_key][$field] = array( __('Dequeue wppizza scripts', 'wppizza-admin'), array( |
| 145 |
'value_key'=>$field, |
| 146 |
'option_key'=>$this->settings_page, |
| 147 |
'label'=>__('If you are *certain* you do not require the main wppizza javascript and jquery validation and nothing else depends on them, or another plugin is already including jquery validation elsewhere, use the settings above as required. If you do not know, just leave it as is', 'wppizza-admin'), |
| 148 |
'description'=>array() |
| 149 |
)); |
| 150 |
} |
| 151 |
|
| 152 |
return $settings; |
| 153 |
} |
| 154 |
/*------------------------------------------------------------------------------ |
| 155 |
# [output option fields - setting page] |
| 156 |
# @since 3.0 |
| 157 |
# @return array() |
| 158 |
------------------------------------------------------------------------------*/ |
| 159 |
function admin_options_fields_settings($wppizza_options, $options_key, $field, $label, $description){ |
| 160 |
|
| 161 |
if($field=='always_load_all_scripts_and_styles'){ |
| 162 |
echo "<label>"; |
| 163 |
echo "<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' type='checkbox' ". checked($wppizza_options[$options_key][$field],true,false)." value='1' />"; |
| 164 |
echo "" . $label . ""; |
| 165 |
echo "</label>"; |
| 166 |
echo"".$description.""; |
| 167 |
} |
| 168 |
if($field=='dequeue_scripts'){ |
| 169 |
echo "<label>"; |
| 170 |
echo "<select name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' />"; |
| 171 |
echo "<option value='' ".selected($wppizza_options[$options_key][$field],"",false).">".__('leave as is', 'wppizza-admin')."</option>"; |
| 172 |
echo "<option value='all' ".selected($wppizza_options[$options_key][$field],"all",false).">".__('dequeue both, main wppizza and jquery validation', 'wppizza-admin')."</option>"; |
| 173 |
echo "<option value='validation' ".selected($wppizza_options[$options_key][$field],"validation",false).">".__('dequeue jquery validation only', 'wppizza-admin')."</option>"; |
| 174 |
echo "</select>"; |
| 175 |
echo "" . $label . ""; |
| 176 |
echo "</label> "; |
| 177 |
echo "" . $description . ""; |
| 178 |
echo"<br /><span class='wppizza-highlight'>".__('NOTE: if you dequeue any scripts other plugins rely on, you WILL break things', 'wppizza-admin')."</span>"; |
| 179 |
} |
| 180 |
} |
| 181 |
/*------------------------------------------------------------------------------ |
| 182 |
# [insert default option on install] |
| 183 |
# $parameter $options array() | filter passing on filtered options |
| 184 |
# @since 3.0 |
| 185 |
# @return array() |
| 186 |
------------------------------------------------------------------------------*/ |
| 187 |
function options_default($options){ |
| 188 |
|
| 189 |
$options[$this->settings_page]['always_load_all_scripts_and_styles'] = false; |
| 190 |
$options[$this->settings_page]['dequeue_scripts'] = ''; |
| 191 |
|
| 192 |
return $options; |
| 193 |
} |
| 194 |
|
| 195 |
/*------------------------------------------------------------------------------ |
| 196 |
# [validate options on save/update] |
| 197 |
# |
| 198 |
# @since 3.0 |
| 199 |
# @return array() |
| 200 |
------------------------------------------------------------------------------*/ |
| 201 |
function options_validate($options, $input){ |
| 202 |
/**make sure we get the full array on install/update**/ |
| 203 |
if ( empty( $_POST['_wp_http_referer'] ) ) { |
| 204 |
return $input; |
| 205 |
} |
| 206 |
/******************************** |
| 207 |
* [validate] |
| 208 |
********************************/ |
| 209 |
if(isset($_POST[''.WPPIZZA_SLUG.'_'.$this->settings_page.''])){ |
| 210 |
$options[$this->settings_page]['dequeue_scripts'] = wppizza_validate_alpha_only($input[$this->settings_page]['dequeue_scripts']); |
| 211 |
$options[$this->settings_page]['always_load_all_scripts_and_styles'] = !empty($input[$this->settings_page]['always_load_all_scripts_and_styles']) ? true : false; |
| 212 |
} |
| 213 |
return $options; |
| 214 |
} |
| 215 |
} |
| 216 |
/*************************************************************** |
| 217 |
* |
| 218 |
* [ini] |
| 219 |
* |
| 220 |
***************************************************************/ |
| 221 |
$WPPIZZA_MODULE_SETTINGS_MISCELLANEOUS = new WPPIZZA_MODULE_SETTINGS_MISCELLANEOUS(); |
| 222 |
?> |