| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Main core file of the "Plugin Options" framework |
| 5 |
* |
| 6 |
* The file loads the different parts of the framework |
| 7 |
* |
| 8 |
* @package skelet |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Helps tracking that the framework core was loaded |
| 13 |
* |
| 14 |
* @var bool |
| 15 |
*/ |
| 16 |
define( 'PAF', 1 ); |
| 17 |
|
| 18 |
/** |
| 19 |
* Load the class K |
| 20 |
*/ |
| 21 |
if ( ! class_exists( 'K' ) ) { |
| 22 |
|
| 23 |
require dirname( __FILE__ ) . '/lib/K/K.php'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Load JSMin and CSSmin |
| 28 |
*/ |
| 29 |
if ( ! class_exists( 'JSMin' ) ) { |
| 30 |
|
| 31 |
require dirname( __FILE__ ) . '/lib/JSMin.php'; |
| 32 |
} |
| 33 |
if ( ! class_exists( 'CSSmin' ) ) { |
| 34 |
|
| 35 |
require dirname( __FILE__ ) . '/lib/CSSmin.php'; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Include core files |
| 40 |
*/ |
| 41 |
foreach ( array( 'pages', 'options', 'shortcodes' ) as $core_file_name ) { |
| 42 |
|
| 43 |
include dirname( __FILE__ ) . '/core-' . $core_file_name . '.php'; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Enqueue JS/CSS |
| 48 |
*/ |
| 49 |
function paf_enqueue() { |
| 50 |
|
| 51 |
$protocol = 'http' . ( is_ssl() ? 's' : '' ); |
| 52 |
|
| 53 |
$js = array( |
| 54 |
// 'highlight' => "$protocol://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js", |
| 55 |
); |
| 56 |
|
| 57 |
$css = array( |
| 58 |
'highlight' => "$protocol://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/default.min.css", |
| 59 |
); |
| 60 |
|
| 61 |
foreach ( $js as $handle => $src ) { |
| 62 |
wp_enqueue_script( $handle, $src ); |
| 63 |
} |
| 64 |
foreach ( $css as $handle => $src ) { |
| 65 |
wp_enqueue_style( $handle, $src ); |
| 66 |
} |
| 67 |
} |
| 68 |
add_action( 'admin_init', 'paf_enqueue' ); |
| 69 |
|
| 70 |
/** |
| 71 |
* Load important gobal data |
| 72 |
* |
| 73 |
* The data is: |
| 74 |
* - paf |
| 75 |
* - paf_page_tabs |
| 76 |
* - paf_page_options |
| 77 |
* - paf_page_sections |
| 78 |
* - paf_page_shortcodes |
| 79 |
* - paf_page |
| 80 |
* - paf_tab |
| 81 |
*/ |
| 82 |
function paf_load() { |
| 83 |
|
| 84 |
global $paf; |
| 85 |
$paf = get_option( 'paf', array() ); |
| 86 |
|
| 87 |
global $paf_options, $paf_pages, $paf_sections, $paf_shortcodes, $paf_tabs; |
| 88 |
|
| 89 |
// Make sure $GLOBALS[ 'paf_...' ] exist |
| 90 |
foreach ( array( 'paf_options', 'paf_pages', 'paf_sections', 'paf_shortcodes', 'paf_tabs' ) as $k ) { |
| 91 |
if( empty( $GLOBALS[ $k ] ) ) { |
| 92 |
$GLOBALS[ $k ] = array(); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
global $paf_page_tabs, $paf_page_options, $paf_page_sections; |
| 97 |
$paf_page_tabs = $paf_page_options = $paf_page_sections = array(); |
| 98 |
|
| 99 |
global $paf_page, $paf_tab; |
| 100 |
$paf_page = K::get_var( 'page', $_GET ); |
| 101 |
$paf_tab = K::get_var( 'tab', $_GET ); |
| 102 |
|
| 103 |
// Get defined page tabs |
| 104 |
foreach ( $paf_tabs as $slug => $page_tab ) { |
| 105 |
if( $paf_page === $paf_tabs[ $slug ][ 'page' ] ) { |
| 106 |
$paf_page_tabs[ $slug ] = $page_tab; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Use the first tab: |
| 112 |
* - if page has tabs and none is specified in $_GET |
| 113 |
* - or if the specified tab doesn't exist |
| 114 |
*/ |
| 115 |
if( |
| 116 |
( $paf_page_tabs && ! $paf_tab ) |
| 117 |
|| ( $paf_page_tabs && $paf_tab && ! K::get_var( $paf_tab, $paf_page_tabs ) ) |
| 118 |
) { |
| 119 |
reset( $paf_page_tabs ); |
| 120 |
$paf_tab = key( $paf_page_tabs ); |
| 121 |
} |
| 122 |
|
| 123 |
// If the page has a tab, force tab-less options to use the default tab |
| 124 |
reset( $paf_options ); |
| 125 |
foreach ( $paf_options as $id => $paf_option ) { |
| 126 |
if( $paf_page === K::get_var( 'page', $paf_option ) ) { |
| 127 |
if( $paf_tab && ! K::get_var( 'tab', $paf_option ) ) { |
| 128 |
$paf_options[ $id ][ 'tab' ] = key( $paf_page_tabs ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// Get defined page and tab options |
| 134 |
reset( $paf_options ); |
| 135 |
foreach ( $paf_options as $id => $paf_option ) { |
| 136 |
if( $paf_page === K::get_var( 'page', $paf_option ) ) { |
| 137 |
if( ! $paf_tab || ( $paf_tab === $paf_option[ 'tab' ] ) ) { |
| 138 |
$paf_page_options[ $id ] = $paf_option; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// Get defined page and tab sections |
| 144 |
reset( $paf_page_options ); |
| 145 |
foreach ( $paf_page_options as $id => $paf_option ) { |
| 146 |
if ( K::get_var( 'section', $paf_option ) ) { |
| 147 |
$paf_page_sections[ $paf_option[ 'section' ] ] = K::get_var( $paf_option[ 'section' ], $paf_sections, array() ); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
add_action( 'admin_init', 'paf_load' ); |
| 152 |
|
| 153 |
/** |
| 154 |
* Save options |
| 155 |
* |
| 156 |
* The function will preserve options saved on other pages |
| 157 |
*/ |
| 158 |
function paf_save() { |
| 159 |
|
| 160 |
global $paf_page_options; |
| 161 |
|
| 162 |
// Abort saving if the nonce is not valid |
| 163 |
if ( ! wp_verify_nonce( K::get_var( 'paf_nonce', $_POST ), 'paf_save' ) ) { |
| 164 |
|
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
// Force select and radio to have a value to prevent skipping empty |
| 169 |
$_POST[ 'paf' ] = K::get_var( 'paf', $_POST, array() ); |
| 170 |
foreach ( $paf_page_options as $option_id => $option_def ) { |
| 171 |
$option_type = K::get_var( 'type', $option_def, 'text' ); |
| 172 |
switch ( $option_type ) { |
| 173 |
case 'radio': |
| 174 |
$_POST['paf'][ $option_id ] = K::get_var( $option_id, $_POST['paf'], '' ); |
| 175 |
case 'checkbox': |
| 176 |
case 'select': |
| 177 |
$_POST['paf'][ $option_id ] = K::get_var( $option_id, $_POST['paf'], array() ); |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// Combine old and saved options |
| 183 |
$paf = get_option( 'paf ', array() ); |
| 184 |
$paf = array_merge( $paf, $_POST[ 'paf' ] ); |
| 185 |
|
| 186 |
// Save |
| 187 |
delete_option( 'paf' ); |
| 188 |
add_option( 'paf', $paf, '', TRUE ); |
| 189 |
|
| 190 |
// Bind showing the success message |
| 191 |
add_action( 'admin_notices', 'paf_notice' ); |
| 192 |
} |
| 193 |
add_action( 'admin_init', 'paf_save' ); |
| 194 |
|
| 195 |
/** |
| 196 |
* Add $pages to the global $paf_pages |
| 197 |
*/ |
| 198 |
function paf_pages( $pages ) { |
| 199 |
|
| 200 |
$GLOBALS[ 'paf_pages'] = array_merge( K::get_var( 'paf_pages', $GLOBALS, array() ) , $pages ); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Add $options to the global $paf_options |
| 205 |
*/ |
| 206 |
function paf_options( $options ) { |
| 207 |
|
| 208 |
$GLOBALS[ 'paf_options'] = array_merge( K::get_var( 'paf_options', $GLOBALS, array() ) , $options ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Add $tabs to the global $paf_tabs |
| 213 |
*/ |
| 214 |
function paf_tabs( $tabs ) { |
| 215 |
|
| 216 |
$GLOBALS[ 'paf_tabs'] = array_merge( K::get_var( 'paf_tabs', $GLOBALS, array() ) , $tabs ); |
| 217 |
// ksort( $GLOBALS[ 'paf_tabs' ] ); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Add $sections to the global $paf_sections |
| 222 |
*/ |
| 223 |
function paf_sections( $sections ) { |
| 224 |
|
| 225 |
$GLOBALS[ 'paf_sections'] = array_merge( K::get_var( 'paf_sections', $GLOBALS, array() ) , $sections ); |
| 226 |
// ksort( $GLOBALS[ 'paf_sections' ] ); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Add $sections to the global $paf_sections |
| 231 |
*/ |
| 232 |
function paf_shortcodes( $shortcodes ) { |
| 233 |
|
| 234 |
$GLOBALS[ 'paf_shortcodes' ] = array_merge( K::get_var( 'paf_shortcodes', $GLOBALS, array() ) , $shortcodes ); |
| 235 |
// ksort( $GLOBALS[ 'paf_shortcodes' ] ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Show message when options are saved successfully |
| 240 |
* |
| 241 |
* Seeks in this order: tab > page > default |
| 242 |
*/ |
| 243 |
function paf_notice() { |
| 244 |
|
| 245 |
global $paf_pages, $paf_tabs, $paf_page, $paf_tab; |
| 246 |
|
| 247 |
// Look in tab configuration |
| 248 |
if ( $paf_tab && $message = K::get_var( 'success', $paf_tabs[ $paf_tab ] ) ) {} |
| 249 |
// Look in page configuration |
| 250 |
else if ( $message = K::get_var( 'success', $paf_pages[ $paf_page ] ) ) {} |
| 251 |
// Use default |
| 252 |
else { $message = __( 'Settings saved.'); } |
| 253 |
|
| 254 |
printf( |
| 255 |
'<div class="updated"><p>%s</p></div>' |
| 256 |
, $message |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Adds some JS in the header: |
| 262 |
* - paf_assets, here paf assets can be found |
| 263 |
*/ |
| 264 |
function paf_header() { |
| 265 |
|
| 266 |
$home_path = get_home_path(); |
| 267 |
$assets_path = str_replace( |
| 268 |
array( $home_path, 'core/core.php' ) |
| 269 |
, array( '', 'assets/' ) |
| 270 |
, __FILE__ |
| 271 |
); |
| 272 |
|
| 273 |
$assets_dir_url = home_url( $assets_path ); |
| 274 |
?> |
| 275 |
<script> |
| 276 |
var paf_assets = "<?php echo $assets_dir_url ?>"; |
| 277 |
hljs.initHighlightingOnLoad(); |
| 278 |
</script> |
| 279 |
<?php |
| 280 |
} |
| 281 |
// add_action( 'admin_head', 'paf_header' ); |
| 282 |
|
| 283 |
/** |
| 284 |
* Add JS file |
| 285 |
*/ |
| 286 |
function paf_asset_js( $asset, $block = FALSE ) { |
| 287 |
|
| 288 |
return paf_asset( $asset, 'js', $block ); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Add CSS file |
| 293 |
*/ |
| 294 |
function paf_asset_css( $asset, $block = FALSE ) { |
| 295 |
|
| 296 |
return paf_asset( $asset, 'css', $block ); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Add asset |
| 301 |
*/ |
| 302 |
function paf_asset( $asset, $type, $block = FALSE ) { |
| 303 |
|
| 304 |
// Trac files that are blocked in the futire |
| 305 |
static $blocked = array(); |
| 306 |
|
| 307 |
// Exit function if type is not supported |
| 308 |
if( ! in_array( $type, array( 'css', 'js' ) ) ) { |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
$js[] = 'paf'; |
| 313 |
$css[] = 'paf'; |
| 314 |
|
| 315 |
// Do nothing if asset not defined |
| 316 |
if ( ! in_array( $asset, $$type ) ) { |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
// Do nothing if already loaded |
| 321 |
if( in_array( "$type/$asset", $blocked ) ) { |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
// Mark as blocked if needed |
| 326 |
if( $block ) { |
| 327 |
$blocked[] = "$type/$asset"; |
| 328 |
} |
| 329 |
|
| 330 |
// Get source |
| 331 |
$src = dirname( __FILE__) . "/../assets/$type/$asset.$type"; |
| 332 |
$o = file_get_contents( $src ); |
| 333 |
|
| 334 |
// Minify source |
| 335 |
switch ( $type ) { |
| 336 |
case 'css': |
| 337 |
$CSSmin = new CSSmin(); |
| 338 |
$o = $CSSmin->run( $o ); |
| 339 |
case 'js': |
| 340 |
$o = JSMin::minify( $o ); |
| 341 |
} |
| 342 |
|
| 343 |
// Wrap in tags |
| 344 |
$o = sprintf( '<%s>%s</%s>' |
| 345 |
, 'css' === $type ? 'style' : 'script' |
| 346 |
, $o |
| 347 |
, 'css' === $type ? 'style' : 'script' |
| 348 |
); |
| 349 |
|
| 350 |
// Output |
| 351 |
print( $o ); |
| 352 |
} |
| 353 |
|
| 354 |
function paf_url() { |
| 355 |
|
| 356 |
return 'http' |
| 357 |
. ( is_ssl() ? 's' : '' ) |
| 358 |
. '://' |
| 359 |
. $_SERVER[ 'SERVER_NAME' ] |
| 360 |
. ( 80 != $_SERVER[ 'SERVER_PORT' ] ? ":$_SERVER[SERVER_PORT]" : '' ) |
| 361 |
. $_SERVER[ 'REQUEST_URI' ] |
| 362 |
; |
| 363 |
} |
| 364 |
|
| 365 |
function paf_htmlspecialchars_recursive( &$array ) { |
| 366 |
|
| 367 |
$array = htmlspecialchars( $array ); |
| 368 |
} |
| 369 |
|
| 370 |
function skelet_dir( $dir ) { |
| 371 |
foreach ( array( 'pages', 'tabs', 'sections', 'options', 'shortcodes' ) as $option_file_name ) { |
| 372 |
|
| 373 |
$option_file_path = $dir . '/' . $option_file_name . '.php'; |
| 374 |
if ( file_exists( $option_file_path ) ) { |
| 375 |
require $option_file_path; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|