| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package skelet |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* Gets options |
| 9 |
* |
| 10 |
* Return all paf options array |
| 11 |
* If $option_id is set, the function will that option current value |
| 12 |
* If not find it will return it's default value if it's defined |
| 13 |
* |
| 14 |
* @param string $option_id |
| 15 |
* @return mixed The paf option value |
| 16 |
*/ |
| 17 |
function paf( $option_id = '' ) { |
| 18 |
|
| 19 |
$paf = get_option( 'paf', array() ); |
| 20 |
|
| 21 |
if( strlen( $option_id ) ) { |
| 22 |
if( isset( $paf[ $option_id ] ) ) { |
| 23 |
return $paf[ $option_id ]; |
| 24 |
} else { |
| 25 |
$def = paf_d( $option_id ); |
| 26 |
return K::get_var( 'default', $def ); |
| 27 |
} |
| 28 |
} else { |
| 29 |
return $paf; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get option definition |
| 35 |
*/ |
| 36 |
function paf_d( $option_id ){ |
| 37 |
|
| 38 |
global $paf_options; |
| 39 |
|
| 40 |
return K::get_var( $option_id, $paf_options, FALSE ); |
| 41 |
} |
| 42 |
|
| 43 |
function paf_print_option( $option_id, $alt = array() ) { |
| 44 |
|
| 45 |
if ( $alt ) { |
| 46 |
$option = $alt; |
| 47 |
} else { |
| 48 |
global $paf_page_options; |
| 49 |
$option = $paf_page_options[ $option_id ]; |
| 50 |
} |
| 51 |
|
| 52 |
$option = paf_option_prepare( $option, $option_id ); |
| 53 |
$option_type = K::get_var( 'type', $option, 'text' ); |
| 54 |
|
| 55 |
// Determine the option callback function |
| 56 |
$callback = 'paf_print_option_type_' . $option_type; |
| 57 |
if( ! function_exists( $callback ) ) { |
| 58 |
$callback = 'paf_print_option_type_not_implemented'; |
| 59 |
} |
| 60 |
|
| 61 |
// Sort option parameters for a better display when using 'description' = '~' |
| 62 |
ksort( $option ); |
| 63 |
|
| 64 |
/** |
| 65 |
* Call the function coresponding to the option, |
| 66 |
* e.g. paf_print_option_type_text or paf_print_option_type_media |
| 67 |
*/ |
| 68 |
call_user_func( $callback, array( $option_id => $option ) ); |
| 69 |
} |
| 70 |
|
| 71 |
function paf_option_return_title( $option_def ) { |
| 72 |
|
| 73 |
$option_id = key( $option_def ); |
| 74 |
$option = $option_def[ $option_id ]; |
| 75 |
|
| 76 |
$title = K::get_var( 'title', $option, $option_id ); |
| 77 |
$subtitle = K::get_var( 'subtitle', $option ); |
| 78 |
|
| 79 |
$return = |
| 80 |
$title |
| 81 |
. ( $subtitle |
| 82 |
? '<br /><span style="font-weight: normal; font-style: italic; font-size: .9em;">' . $subtitle . '</span>' |
| 83 |
: '' |
| 84 |
) |
| 85 |
; |
| 86 |
|
| 87 |
return $return; |
| 88 |
} |
| 89 |
|
| 90 |
function paf_option_return_format( $option_type = 'input' ) { |
| 91 |
|
| 92 |
switch ( $option_type ) { |
| 93 |
case 'media': |
| 94 |
return '<table class="form-table"><tbody><tr><th scope="row">%s</th><td>:input%s<br />%s</td></tr></tbody></table>'; |
| 95 |
case 'input': |
| 96 |
case 'select': |
| 97 |
case 'textarea': |
| 98 |
default: |
| 99 |
return '<table class="form-table"><tbody><tr><th scope="row">%s</th><td>:' . $option_type . '<br />%s</td></tr></tbody></table>' ; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Prepare option |
| 105 |
* |
| 106 |
* Change different option attributes to a suitable format |
| 107 |
*/ |
| 108 |
function paf_option_prepare( $option, $option_id = null ) { |
| 109 |
|
| 110 |
// Add type if not specified |
| 111 |
if( ! isset( $option[ 'type' ] ) ) { |
| 112 |
$option[ 'type' ] = 'text'; |
| 113 |
} |
| 114 |
|
| 115 |
// Format selected as an array |
| 116 |
if( isset( $option[ 'selected' ] ) ) { |
| 117 |
$option[ 'selected' ] = K::get_var( 'selected', $option, array() ); |
| 118 |
if ( ! is_array( $option[ 'selected' ] ) ) { |
| 119 |
$option[ 'selected' ] = explode( ',', $option[ 'selected' ] ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// Format default as an array |
| 124 |
if( in_array( $option[ 'type' ], array( 'select', 'radio', 'checkbox' ) ) ) { |
| 125 |
if( isset( $option[ 'default' ] ) ) { |
| 126 |
$option[ 'default' ] = K::get_var( 'default', $option, array() ); |
| 127 |
if ( ! is_array( $option[ 'default' ] ) ) { |
| 128 |
$option[ 'default' ] = explode( ',', $option[ 'default' ] ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// Add code for generating the option is the description is "~" |
| 134 |
if( $option_id ) { |
| 135 |
if( '~' === K::get_var( 'description', $option ) ) { |
| 136 |
$option[ 'description' ] = paf_option_return_dump( $option_id ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return $option; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Generate formatted and syntax highlighted dump |
| 145 |
*/ |
| 146 |
function paf_option_return_dump( $option_id ) { |
| 147 |
|
| 148 |
global $paf_options; |
| 149 |
$option = $paf_options[ $option_id ]; |
| 150 |
ksort( $option ); |
| 151 |
|
| 152 |
array_walk_recursive( $option, 'paf_htmlspecialchars_recursive' ); |
| 153 |
|
| 154 |
$dump = K::wrap( |
| 155 |
"\$options[ '$option_id' ] = " |
| 156 |
. var_export( $option, true ) |
| 157 |
. ';' |
| 158 |
, array( |
| 159 |
'class' => 'php paf-code-block', |
| 160 |
) |
| 161 |
, array( |
| 162 |
'html_before' => '<pre>', |
| 163 |
'html_after' => '</pre>', |
| 164 |
'in' => 'code', |
| 165 |
'return' => true, |
| 166 |
) |
| 167 |
); |
| 168 |
|
| 169 |
// Remove white space before 'array (' |
| 170 |
$dump = preg_replace( '/=>\s+array \(/s', '=> array (', $dump ); |
| 171 |
|
| 172 |
// Replace 2 spaces with 4 spaces |
| 173 |
$pattern = "/((?: )+)(\d+|'|array|\))/"; |
| 174 |
$dump = preg_replace( $pattern, '\1\1\2', $dump ); |
| 175 |
|
| 176 |
return $dump; |
| 177 |
} |
| 178 |
|
| 179 |
function paf_print_option_type_text( $option_def ) { |
| 180 |
|
| 181 |
$option_id = key( $option_def ); |
| 182 |
$option = $option_def[ $option_id ]; |
| 183 |
|
| 184 |
K::input( 'paf[' . $option_id . ']' |
| 185 |
, array( |
| 186 |
'class' => 'regular-text', |
| 187 |
'placeholder' => K::get_var( 'placeholder', $option ), |
| 188 |
'value' => isset( $option[ 'value' ] ) |
| 189 |
? $option[ 'value' ] |
| 190 |
: paf( $option_id ) |
| 191 |
, |
| 192 |
'data-paf-conditions' => K::get_var( 'conditions', $option ) |
| 193 |
? urlencode( json_encode( K::get_var( 'conditions', $option ), JSON_FORCE_OBJECT ) ) |
| 194 |
: null |
| 195 |
, |
| 196 |
'data-paf-default' => K::get_var( 'default', $option ), |
| 197 |
) |
| 198 |
, array( |
| 199 |
'colorpicker' => K::get_var( 'colorpicker', $option, FALSE ), |
| 200 |
'format' => sprintf( |
| 201 |
paf_option_return_format() |
| 202 |
, paf_option_return_title( $option_def ) |
| 203 |
, K::get_var( 'description', $option, '' ) |
| 204 |
) |
| 205 |
) |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
function paf_print_option_type_textarea( $option_def ) { |
| 210 |
|
| 211 |
$option_id = key( $option_def ); |
| 212 |
$option = $option_def[ $option_id ]; |
| 213 |
|
| 214 |
if( '~' === K::get_var( 'description', $option ) ) { |
| 215 |
$option[ 'description' ] = paf_option_return_dump( $option_id ); |
| 216 |
} |
| 217 |
|
| 218 |
$style = ''; |
| 219 |
foreach( array( 'height', 'width' ) as $prop ) { |
| 220 |
$style .= K::get_var( $prop, $option ) |
| 221 |
? sprintf( '%:%;', $prop, $option[ $prop ] ) |
| 222 |
: '' |
| 223 |
; |
| 224 |
} |
| 225 |
|
| 226 |
K::textarea( 'paf[' . $option_id . ']' |
| 227 |
, array( |
| 228 |
'class' => K::get_var( 'cols', $option ) ? '' : 'large-text', |
| 229 |
'cols' => K::get_var( 'cols', $option ), |
| 230 |
'rows' => K::get_var( 'rows', $option ), |
| 231 |
'style' => $style, |
| 232 |
'data-paf-conditions' => K::get_var( 'conditions', $option ) |
| 233 |
? urlencode( json_encode( K::get_var( 'conditions', $option ), JSON_FORCE_OBJECT ) ) |
| 234 |
: null |
| 235 |
, |
| 236 |
'data-paf-default' => K::get_var( 'default', $option ), |
| 237 |
) |
| 238 |
, array( |
| 239 |
'value' => isset( $option[ 'value' ] ) |
| 240 |
? $option[ 'value' ] |
| 241 |
: paf( $option_id ) |
| 242 |
, |
| 243 |
'editor' => K::get_var( 'editor', $option, FALSE ), |
| 244 |
'editor_height' => K::get_var( 'editor_height', $option ), |
| 245 |
'format' => sprintf( |
| 246 |
paf_option_return_format( 'textarea' ) |
| 247 |
, paf_option_return_title( $option_def ) |
| 248 |
, K::get_var( 'description', $option, '' ) |
| 249 |
), |
| 250 |
'media_buttons' => K::get_var( 'media_buttons', $option, TRUE ), |
| 251 |
'teeny' => K::get_var( 'teeny', $option ), |
| 252 |
'textarea_rows' => K::get_var( 'textarea_rows', $option, 20 ), |
| 253 |
) |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
function paf_print_option_type_select( $option_def ) { |
| 258 |
|
| 259 |
$option_id = key( $option_def ); |
| 260 |
$option = $option_def[ $option_id ]; |
| 261 |
|
| 262 |
if( '~' === K::get_var( 'description', $option ) ) { |
| 263 |
$option[ 'description' ] = paf_option_return_dump( $option_id ); |
| 264 |
} |
| 265 |
|
| 266 |
$is_radio = 'radio' === $option[ 'type' ]; |
| 267 |
$is_checkbox = 'checkbox' === $option[ 'type' ]; |
| 268 |
$is_select = 'select' === $option[ 'type' ]; |
| 269 |
$is_multiple = $is_checkbox || K::get_var( 'multiple', $option ); |
| 270 |
|
| 271 |
// Enqueue select 2 |
| 272 |
if( ! $is_checkbox && ! $is_radio ) { |
| 273 |
$protocol = is_ssl() ? 'https' : 'http'; |
| 274 |
wp_enqueue_script( 'select2', $protocol . '://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.js' ); |
| 275 |
wp_enqueue_style( 'select2', $protocol . '://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.css' ); |
| 276 |
} |
| 277 |
|
| 278 |
$options = array(); |
| 279 |
switch ( K::get_var( 'options', $option, array() ) ) { |
| 280 |
case 'posts': |
| 281 |
$posts = query_posts( K::get_var( 'args', $option, '' ) ); |
| 282 |
foreach ( $posts as $post ) { |
| 283 |
$options[ $post->ID ] = $post->post_title; |
| 284 |
} |
| 285 |
if ( $is_select && ! $is_multiple ) { |
| 286 |
$options = array( '' ) + $options; |
| 287 |
} |
| 288 |
break; |
| 289 |
case 'terms': |
| 290 |
$taxonomies = K::get_var( 'taxonomies', $option, 'category,post_tag,link_category,post_format' ); |
| 291 |
if ( ! is_array( $taxonomies ) ) { |
| 292 |
$taxonomies = explode( ',', $taxonomies ); |
| 293 |
} |
| 294 |
$args = K::get_var( 'args', $option, array() ); |
| 295 |
$terms = get_terms( $taxonomies, $args ); |
| 296 |
foreach ( $terms as $term ) { |
| 297 |
$options[ $term->term_id ] = $term->name; |
| 298 |
} |
| 299 |
if ( $is_select && ! $is_multiple ) { |
| 300 |
$options = array( '' ) + $options; |
| 301 |
} |
| 302 |
break; |
| 303 |
default: |
| 304 |
$options = K::get_var( 'options', $option, array() ); |
| 305 |
break; |
| 306 |
} |
| 307 |
|
| 308 |
// Add an empty option to prevent auto-selecting the first radio |
| 309 |
if( $is_radio ) { |
| 310 |
$options = array( '__none__' => '' ) + $options; |
| 311 |
} |
| 312 |
|
| 313 |
// Escape HTML |
| 314 |
foreach ( $options as $k => $v ) { |
| 315 |
$options[ $k ] = htmlspecialchars( $v ); |
| 316 |
} |
| 317 |
|
| 318 |
K::select( 'paf[' . $option_id . ']' |
| 319 |
, array( |
| 320 |
'class' => 'paf-option-type-' . $option[ 'type' ], |
| 321 |
'data-paf-separator' => K::get_var( 'separator', $option, '<br />' ), |
| 322 |
'multiple' => $is_multiple, |
| 323 |
'style' => 'min-width: 25em;', |
| 324 |
'data-paf-conditions' => K::get_var( 'conditions', $option ) |
| 325 |
? urlencode( json_encode( K::get_var( 'conditions', $option ), JSON_FORCE_OBJECT ) ) |
| 326 |
: null |
| 327 |
, |
| 328 |
'data-paf-default' => K::get_var( 'default', $option ) |
| 329 |
? urlencode( json_encode( K::get_var( 'default', $option ), JSON_FORCE_OBJECT ) ) |
| 330 |
: null |
| 331 |
, |
| 332 |
) |
| 333 |
, array( |
| 334 |
'options' => $options, |
| 335 |
'selected' => isset( $option[ 'selected' ] ) |
| 336 |
? $option[ 'selected' ] |
| 337 |
: paf( $option_id ) |
| 338 |
, |
| 339 |
'format' => sprintf( |
| 340 |
paf_option_return_format( 'select' ) |
| 341 |
, paf_option_return_title( $option_def ) |
| 342 |
, K::get_var( 'description', $option, '' ) |
| 343 |
) |
| 344 |
) |
| 345 |
); |
| 346 |
} |
| 347 |
|
| 348 |
function paf_print_option_type_radio( $option_def ) { |
| 349 |
|
| 350 |
return paf_print_option_type_select( $option_def ); |
| 351 |
} |
| 352 |
|
| 353 |
function paf_print_option_type_checkbox( $option_def ) { |
| 354 |
|
| 355 |
return paf_print_option_type_select( $option_def ); |
| 356 |
} |
| 357 |
|
| 358 |
function paf_print_option_type_media( $option_def ) { |
| 359 |
|
| 360 |
$option_id = key( $option_def ); |
| 361 |
$option = $option_def[ $option_id ]; |
| 362 |
|
| 363 |
if( '~' === K::get_var( 'description', $option ) ) { |
| 364 |
$option[ 'description' ] = paf_option_return_dump( $option_id ); |
| 365 |
} |
| 366 |
|
| 367 |
$button_text = K::get_var( 'button_text', $option, __( 'Select media' ) ); |
| 368 |
|
| 369 |
// Output |
| 370 |
K::input( 'paf[' . $option_id . ']' |
| 371 |
, array( |
| 372 |
'class' => 'paf-option-type-media regular-text', |
| 373 |
'placeholder' => K::get_var( 'placeholder', $option ), |
| 374 |
'value' => isset( $option[ 'value' ] ) |
| 375 |
? $option[ 'value' ] |
| 376 |
: paf( $option_id ) |
| 377 |
, |
| 378 |
'data-paf-conditions' => K::get_var( 'conditions', $option ) |
| 379 |
? urlencode( json_encode( K::get_var( 'conditions', $option ), JSON_FORCE_OBJECT ) ) |
| 380 |
: null |
| 381 |
, |
| 382 |
'data-paf-default' => K::get_var( 'default', $option ), |
| 383 |
) |
| 384 |
, array( |
| 385 |
'format' => sprintf( |
| 386 |
paf_option_return_format( 'media' ) |
| 387 |
, paf_option_return_title( $option_def ) |
| 388 |
, '<a class="button">' . $button_text . '</a>' |
| 389 |
, K::get_var( 'description', $option, '' ) |
| 390 |
) |
| 391 |
) |
| 392 |
); |
| 393 |
} |
| 394 |
|
| 395 |
function paf_print_option_type_not_implemented( $option_def ) { |
| 396 |
|
| 397 |
$option_id = key( $option_def ); |
| 398 |
$option = $option_def[ $option_id ]; |
| 399 |
|
| 400 |
if( '~' === K::get_var( 'description', $option ) ) { |
| 401 |
$option[ 'description' ] = paf_option_return_dump( $option_id ); |
| 402 |
} |
| 403 |
|
| 404 |
K::input( 'paf[' . $option_id . ']' |
| 405 |
, array( |
| 406 |
'value' => K::get_var( 'value', $option, '' ), |
| 407 |
) |
| 408 |
, array( |
| 409 |
'format' => sprintf( |
| 410 |
paf_option_return_format() |
| 411 |
, paf_option_return_title( $option_def ) |
| 412 |
, sprintf( |
| 413 |
'<p class="description"><span class="dashicons dashicons-no"></span> ' . __( 'The option type <code>%s</code> is not yet implemented' ) . '</p>' |
| 414 |
, $option[ 'type' ] |
| 415 |
) |
| 416 |
, K::get_var( 'description', $option, '' ) |
| 417 |
) |
| 418 |
) |
| 419 |
); |
| 420 |
} |
| 421 |
|