| 1 |
<?php |
| 2 |
/** |
| 3 |
* The k framework |
| 4 |
* |
| 5 |
* @author Nabil Kadimi <nabil@kadimi.com> |
| 6 |
* @version 1.0.4 |
| 7 |
* @package k_framework |
| 8 |
*/ |
| 9 |
class K { |
| 10 |
|
| 11 |
/** |
| 12 |
* Gets a variable |
| 13 |
*/ |
| 14 |
static function get_var( $name, $array = null, $default = null ) { |
| 15 |
|
| 16 |
if( is_null( $array ) ) { |
| 17 |
$array = $GLOBALS; |
| 18 |
} |
| 19 |
if( is_array( $array ) && array_key_exists( $name, $array ) ) { |
| 20 |
return $array[ $name ]; |
| 21 |
} else { |
| 22 |
return $default; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Gets a variable |
| 28 |
*/ |
| 29 |
static function allowed_html() { |
| 30 |
return [ |
| 31 |
'a' => [ |
| 32 |
'href' => true, |
| 33 |
'title' => true, |
| 34 |
'target' => true, |
| 35 |
'class' => true, |
| 36 |
'download' => true, |
| 37 |
'id' => true, |
| 38 |
], |
| 39 |
'img' => [ |
| 40 |
'src' => true, |
| 41 |
'id' => true, |
| 42 |
'class' => true, |
| 43 |
], |
| 44 |
'h1' => [ |
| 45 |
'id' => true, |
| 46 |
'class' => true, |
| 47 |
], |
| 48 |
'h2' => [ |
| 49 |
'id' => true, |
| 50 |
'class' => true, |
| 51 |
], |
| 52 |
'h3' => [ |
| 53 |
'id' => true, |
| 54 |
'class' => true, |
| 55 |
], |
| 56 |
'button' => [ |
| 57 |
'type' => true, |
| 58 |
'title' => true, |
| 59 |
'data-wysihtml5-command' => true, |
| 60 |
'data-wysihtml5-dialog-action' => true, |
| 61 |
'data-wysihtml5-action' => true, |
| 62 |
'class' => true, |
| 63 |
'id' => true, |
| 64 |
'name' => true, |
| 65 |
], |
| 66 |
'option' => [ |
| 67 |
'selected' => true, |
| 68 |
'id' => true, |
| 69 |
'class' => true, |
| 70 |
'value' => true |
| 71 |
], |
| 72 |
'optgroup' => [ |
| 73 |
'label' => true, |
| 74 |
], |
| 75 |
'input' => [ |
| 76 |
'name' => true, |
| 77 |
'id' => true, |
| 78 |
'class' => true, |
| 79 |
'value' => true, |
| 80 |
'type' => true, |
| 81 |
'placeholder' => true, |
| 82 |
'checked' => true, |
| 83 |
'hidden' => true, |
| 84 |
'readonly' => true, |
| 85 |
'size' => true, |
| 86 |
'min' => true, |
| 87 |
'max' => true, |
| 88 |
'style' => true, |
| 89 |
'data-wysihtml5-dialog-field' => true, |
| 90 |
], |
| 91 |
'select' => [ |
| 92 |
'name' => true, |
| 93 |
'id' => true, |
| 94 |
'class' => true, |
| 95 |
'value' => true, |
| 96 |
'style' => true, |
| 97 |
], |
| 98 |
'textarea' => [ |
| 99 |
'name' => true, |
| 100 |
'id' => true, |
| 101 |
'class' => true, |
| 102 |
'value' => true, |
| 103 |
'style' => true, |
| 104 |
'rows' => true |
| 105 |
], |
| 106 |
'table' => [ |
| 107 |
'id' => true, |
| 108 |
'class' => true, |
| 109 |
], |
| 110 |
'tr' => [ |
| 111 |
'id' => true, |
| 112 |
'class' => true, |
| 113 |
], |
| 114 |
'th' => [ |
| 115 |
'id' => true, |
| 116 |
'class' => true, |
| 117 |
'title' => true, |
| 118 |
], |
| 119 |
'td' => [ |
| 120 |
'id' => true, |
| 121 |
'class' => true, |
| 122 |
'title' => true, |
| 123 |
], |
| 124 |
'form' => [ |
| 125 |
'name' => true, |
| 126 |
'id' => true, |
| 127 |
'class' => true, |
| 128 |
'action' => true, |
| 129 |
'method' => true, |
| 130 |
'data-fca_eoi_list_id' => true, |
| 131 |
'data-fca_eoi_thank_you_mode' => true, |
| 132 |
'data-fca_eoi_thank_you_text_color' => true, |
| 133 |
'data-fca_eoi_thank_you_bg_color' => true, |
| 134 |
'data-fca_eoi_thank_you_page' => true, |
| 135 |
'data-fca_eoi_success_cookie_duration' => true, |
| 136 |
'data-fca_eoi_sub_msg' => true, |
| 137 |
'data-fca_eoi_push_page' => true, |
| 138 |
], |
| 139 |
'label' => [ |
| 140 |
'for' => true, |
| 141 |
'id' => true, |
| 142 |
'class' => true, |
| 143 |
], |
| 144 |
'p' => [ |
| 145 |
'id' => true, |
| 146 |
'class' => true, |
| 147 |
], |
| 148 |
'div' => [ |
| 149 |
'id' => true, |
| 150 |
'class' => true, |
| 151 |
'style' => true, |
| 152 |
'data-layout-id' => true, |
| 153 |
'data-layout-type' => true, |
| 154 |
'data-layout-order' => true, |
| 155 |
'data-wysihtml5-dialog' => true, |
| 156 |
], |
| 157 |
'span' => [ |
| 158 |
'id' => true, |
| 159 |
'class' => true, |
| 160 |
'title' => true, |
| 161 |
'data-on' => true, |
| 162 |
'data-off' => true |
| 163 |
], |
| 164 |
'fieldset' => [ |
| 165 |
'id' => true, |
| 166 |
], |
| 167 |
'legend' => true, |
| 168 |
'br' => true, |
| 169 |
'style' => true, |
| 170 |
|
| 171 |
]; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Prints or returns an input field |
| 176 |
*/ |
| 177 |
static function input( $name ) { |
| 178 |
|
| 179 |
// $params |
| 180 |
if( func_num_args() > 1 ) { |
| 181 |
$params = func_get_arg(1); |
| 182 |
} |
| 183 |
if( empty( $params ) ) { |
| 184 |
$params = array(); |
| 185 |
} |
| 186 |
|
| 187 |
// $args |
| 188 |
if( func_num_args() > 2 ) { |
| 189 |
$args = func_get_arg(2); |
| 190 |
} |
| 191 |
if( empty( $args ) ) { |
| 192 |
$args = array(); |
| 193 |
} |
| 194 |
|
| 195 |
// Load defaults |
| 196 |
$params += array( |
| 197 |
'type' => 'text', |
| 198 |
'id' => '', |
| 199 |
'value' => '' |
| 200 |
); |
| 201 |
|
| 202 |
// Add name |
| 203 |
$params[ 'name' ] = $name; |
| 204 |
|
| 205 |
// Build the input field html |
| 206 |
$input = sprintf( '<input %s/>', K::params_str( $params ) ); |
| 207 |
|
| 208 |
// Format |
| 209 |
if( ! empty ( $args[ 'format' ] ) ) { |
| 210 |
$input = str_replace( |
| 211 |
array( ':input', ':name', ':id', ':value' ), |
| 212 |
array( $input, $name, $params[ 'id'], $params[ 'value'] ), |
| 213 |
$args[ 'format' ] |
| 214 |
); |
| 215 |
} |
| 216 |
|
| 217 |
// Add default color picker |
| 218 |
if( |
| 219 |
! empty ( $args[ 'colorpicker' ] ) |
| 220 |
|| ( |
| 221 |
'text' === $params[ 'type' ] |
| 222 |
&& preg_match( '/_color\]?$/', $name) |
| 223 |
&& empty( $args[ 'nocolorpicker' ] ) |
| 224 |
) |
| 225 |
) { |
| 226 |
ob_start(); |
| 227 |
$input .= ob_get_clean(); |
| 228 |
} |
| 229 |
|
| 230 |
// Print or return the input field HTML |
| 231 |
if( ! empty( $args[ 'return' ] ) ) { |
| 232 |
return $input; |
| 233 |
} else { |
| 234 |
echo wp_kses( $input, K::allowed_html() ); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Prints or returns an input field |
| 240 |
*/ |
| 241 |
static function textarea( $name ) { |
| 242 |
|
| 243 |
// $params |
| 244 |
if( func_num_args() > 1 ) { |
| 245 |
$params = func_get_arg(1); |
| 246 |
} |
| 247 |
if( ! is_array( $params ) ) { |
| 248 |
$params = array(); |
| 249 |
} |
| 250 |
|
| 251 |
// $args |
| 252 |
if( func_num_args() > 2 ) { |
| 253 |
$args = func_get_arg(2); |
| 254 |
} |
| 255 |
if( ! is_array( $args ) ) { |
| 256 |
$args = array(); |
| 257 |
} |
| 258 |
|
| 259 |
// Load defaults |
| 260 |
$params += array( |
| 261 |
'id' => '', |
| 262 |
); |
| 263 |
|
| 264 |
// Add name |
| 265 |
$params[ 'name' ] = $name; |
| 266 |
|
| 267 |
// Set $value |
| 268 |
$value = empty( $args[ 'value' ] ) ? '' : $args[ 'value' ]; |
| 269 |
|
| 270 |
// Build textarea html |
| 271 |
if( K::get_var( 'editor', $args ) ) { |
| 272 |
// Remove the name since it's attached to the editor |
| 273 |
$params_for_editor = $params; |
| 274 |
$name = ( $params[ 'name' ] ); |
| 275 |
$placeholder = 'Get the latest content first.'; |
| 276 |
unset( $params_for_editor[ 'name' ] ); |
| 277 |
|
| 278 |
// Build |
| 279 |
ob_start(); |
| 280 |
wp_enqueue_style('fca_eoi_wysi_css', FCA_EOI_PLUGIN_URL . '/assets/vendor/wysi/wysi.min.css', array(), FCA_EOI_VER ); |
| 281 |
wp_enqueue_script('fca_eoi_wysi_js_main', FCA_EOI_PLUGIN_URL . '/assets/vendor/wysi/wysihtml.min.js', array(), FCA_EOI_VER, true ); |
| 282 |
wp_enqueue_script('fca_eoi_wysi_js', FCA_EOI_PLUGIN_URL . '/assets/vendor/wysi/wysi.js', array( 'jquery', 'fca_eoi_wysi_js_main' ), FCA_EOI_VER, true ); |
| 283 |
|
| 284 |
|
| 285 |
$admin_data = array ( |
| 286 |
'stylesheet' => FCA_EOI_PLUGIN_URL . '/assets/vendor/wysi/wysi.min.css', |
| 287 |
'editor' => 'full' |
| 288 |
); |
| 289 |
|
| 290 |
wp_localize_script( 'fca_eoi_wysi_js', 'fcaEoiAdminData', $admin_data ); |
| 291 |
|
| 292 |
$html = ''; |
| 293 |
$html .= "<div class='fca-wysiwyg-nav' style='display:none'>"; |
| 294 |
$html .= '<div class="fca-wysiwyg-group fca-wysiwyg-text-group">'; |
| 295 |
$html .= '<button type="button" data-wysihtml5-command="bold" class="fca-nav-bold fca-nav-rounded-left" ><span class="dashicons dashicons-editor-bold"></span></button>'; |
| 296 |
$html .= '<button type="button" data-wysihtml5-command="italic" class="fca-nav-italic fca-nav-no-border" ><span class="dashicons dashicons-editor-italic"></span></button>'; |
| 297 |
$html .= '<button type="button" data-wysihtml5-command="underline" class="fca-nav-underline fca-nav-rounded-right" ><span class="dashicons dashicons-editor-underline"></span></button>'; |
| 298 |
$html .= "</div>"; |
| 299 |
$html .= '<div class="fca-wysiwyg-group fca-wysiwyg-alignment-group">'; |
| 300 |
$html .= '<button type="button" data-wysihtml5-command="justifyLeft" class="fca-nav-justifyLeft fca-nav-rounded-left" ><span class="dashicons dashicons-editor-alignleft"></span></button>'; |
| 301 |
$html .= '<button type="button" data-wysihtml5-command="justifyCenter" class="fca-nav-justifyCenter fca-nav-no-border" ><span class="dashicons dashicons-editor-aligncenter"></span></button>'; |
| 302 |
$html .= '<button type="button" data-wysihtml5-command="justifyRight" class="fca-nav-justifyRight fca-nav-rounded-right" ><span class="dashicons dashicons-editor-alignright"></span></button>'; |
| 303 |
$html .= "</div>"; |
| 304 |
|
| 305 |
$html .= '<div class="fca-wysiwyg-group fca-wysiwyg-link-group">'; |
| 306 |
$html .= '<button type="button" data-wysihtml5-command="createLink" style="border-right: 0;" class="fca-wysiwyg-link-group fca-nav-rounded-left"><span class="dashicons dashicons-admin-links"></span></button>'; |
| 307 |
$html .= '<button type="button" data-wysihtml5-command="unlink" class="fca-wysiwyg-link-group fca-nav-rounded-right"><span class="dashicons dashicons-editor-unlink"></span></button>'; |
| 308 |
$html .= "</div>"; |
| 309 |
|
| 310 |
$html .= '<div class="fca-wysiwyg-group fca-wysiwyg-image-group">'; |
| 311 |
$html .= '<button type="button" class="fca-wysiwyg-insert-image" data-wysihtml5-command="insertImage"><span class="dashicons dashicons-format-image"></span></button>'; |
| 312 |
$html .= "</div>"; |
| 313 |
|
| 314 |
$html .= '<div class="fca-wysiwyg-url-dialog" data-wysihtml5-dialog="createLink" style="display: none">'; |
| 315 |
$html .= '<input data-wysihtml5-dialog-field="href" value="http://">'; |
| 316 |
$html .= '<button type="button" class="button button-secondary button-small" data-wysihtml5-dialog-action="cancel">Cancel</button>'; |
| 317 |
$html .= '<button type="button" class="button button-primary button-small" data-wysihtml5-dialog-action="save">OK</button>'; |
| 318 |
$html .= "</div>"; |
| 319 |
|
| 320 |
$html .= '<button class="fca-wysiwyg-view-html action" type="button" data-wysihtml5-action="change_view">HTML</button>'; |
| 321 |
|
| 322 |
$html .= "</div>"; |
| 323 |
$html .= "<textarea class='fca-wysiwyg-html fca-eoi-input-wysi $name' name='$name' placeholder='$placeholder'>$value</textarea>"; |
| 324 |
|
| 325 |
echo wp_kses( $html, K::allowed_html() ); |
| 326 |
$textarea = ob_get_clean(); |
| 327 |
$textarea = sprintf( '<div %s>%s</div>', K::params_str( $params_for_editor ), $textarea ); |
| 328 |
} else { |
| 329 |
$textarea = sprintf( '<textarea %s>%s</textarea>', K::params_str( $params ), $value ); |
| 330 |
} |
| 331 |
|
| 332 |
// Format |
| 333 |
if( ! empty ( $args[ 'format' ] ) ) { |
| 334 |
$textarea = str_replace( |
| 335 |
array( ':textarea', ':value', ':name', ':id' ), |
| 336 |
array( $textarea, $value, $name, $params[ 'id' ] ), |
| 337 |
$args[ 'format' ] |
| 338 |
); |
| 339 |
} |
| 340 |
|
| 341 |
// Print or return the textarea field HTML |
| 342 |
if( ! empty( $args[ 'return' ] ) ) { |
| 343 |
return $textarea; |
| 344 |
} else { |
| 345 |
echo wp_kses( $textarea, K::allowed_html() ); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Prints or returns an dropdown select |
| 351 |
*/ |
| 352 |
static function select( $name ) { |
| 353 |
|
| 354 |
// $params |
| 355 |
if( func_num_args() > 1 ) { |
| 356 |
$params = func_get_arg(1); |
| 357 |
} |
| 358 |
if( empty( $params ) ) { |
| 359 |
$params = array(); |
| 360 |
} |
| 361 |
// Load defaults |
| 362 |
$params += array( |
| 363 |
'id' => '', |
| 364 |
); |
| 365 |
|
| 366 |
// Sanitize $params[multiple], and Add brackets if the former is true |
| 367 |
if( ! empty( $params[ 'multiple' ] ) ) { |
| 368 |
$params[ 'multiple' ] = 'multiple'; |
| 369 |
$name .= '[]'; |
| 370 |
} |
| 371 |
|
| 372 |
// Add name |
| 373 |
$params[ 'name' ] = $name; |
| 374 |
|
| 375 |
// $args |
| 376 |
if( func_num_args() > 2 ) { |
| 377 |
$args = func_get_arg(2); |
| 378 |
} |
| 379 |
if( empty( $args ) ) { |
| 380 |
$args = array(); |
| 381 |
} |
| 382 |
$args += array( |
| 383 |
'default' => '', |
| 384 |
'options' => array(), |
| 385 |
'html_before' => '', |
| 386 |
'html_after' => '', |
| 387 |
'selected' => '', |
| 388 |
); |
| 389 |
|
| 390 |
// Make 'selected' an array |
| 391 |
if( $selected = $args[ 'selected' ] ) { |
| 392 |
if( ! is_array( $selected ) ) { |
| 393 |
$selected = array( $selected ); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
// Use 'default' if 'selected' is empty |
| 398 |
if( ! $selected ) { |
| 399 |
$selected = array( $args[ 'default' ] ); |
| 400 |
} |
| 401 |
|
| 402 |
// Build options |
| 403 |
$options = ''; |
| 404 |
foreach ( $args[ 'options' ] as $value => $label ) { |
| 405 |
$options .= K::wrap( |
| 406 |
$label |
| 407 |
, array( |
| 408 |
'value' => $value, |
| 409 |
'selected' => ( in_array( $value, $selected ) ) |
| 410 |
? 'selected' |
| 411 |
: null |
| 412 |
, |
| 413 |
) |
| 414 |
, array( |
| 415 |
'in' => 'option', |
| 416 |
'return' => true, |
| 417 |
) |
| 418 |
); |
| 419 |
} |
| 420 |
|
| 421 |
// Build the input field html |
| 422 |
$select = sprintf( '%s<select %s>%s</select>%s', $args[ 'html_before' ], K::params_str( $params ), $options, $args[ 'html_after' ] ); |
| 423 |
|
| 424 |
// Format |
| 425 |
if( ! empty ( $args[ 'format' ] ) ) { |
| 426 |
$select = str_replace( |
| 427 |
array( ':select', ':name', ':id' ), |
| 428 |
array( $select, $name, $params[ 'id'] ), |
| 429 |
$args[ 'format' ] |
| 430 |
); |
| 431 |
} |
| 432 |
|
| 433 |
// Print or return the input field HTML |
| 434 |
if( ! empty( $args[ 'return' ] ) ) { |
| 435 |
return $select; |
| 436 |
} else { |
| 437 |
echo wp_kses( $select, K::allowed_html() ); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Prints or returns an input field |
| 443 |
* |
| 444 |
* @param array $controls The array of controls |
| 445 |
*/ |
| 446 |
static function fieldset( $legend, $controls = array() ) { |
| 447 |
|
| 448 |
// $params |
| 449 |
if( func_num_args() > 2 ) { |
| 450 |
$params = func_get_arg(2); |
| 451 |
} |
| 452 |
if( empty( $params ) ) { |
| 453 |
$params = array(); |
| 454 |
} |
| 455 |
|
| 456 |
// $args |
| 457 |
if( func_num_args() > 3 ) { |
| 458 |
$args = func_get_arg(3); |
| 459 |
} |
| 460 |
if( empty( $args ) ) { |
| 461 |
$args = array(); |
| 462 |
} |
| 463 |
|
| 464 |
// Inner HTML placeholder |
| 465 |
$innerHTML = ''; |
| 466 |
|
| 467 |
// Put controls in placehoder |
| 468 |
foreach( $controls as $control ) { |
| 469 |
|
| 470 |
// Fill params if needed |
| 471 |
$control[2] = ! empty( $control[2] ) ? $control[2] : array(); |
| 472 |
|
| 473 |
// Set $args['return'] to false |
| 474 |
if( empty( $control[3] ) ) { |
| 475 |
$control[3] = array(); |
| 476 |
} |
| 477 |
$control[3][ 'return' ] = true; |
| 478 |
|
| 479 |
// Get control HTML |
| 480 |
$innerHTML .= call_user_func( |
| 481 |
/* Callback */ 'K::' . $control[0], |
| 482 |
/* Name/content */ $control[1], |
| 483 |
/* Params*/ $control[2], |
| 484 |
/* Args */ $control[3] |
| 485 |
); |
| 486 |
} |
| 487 |
|
| 488 |
// Prepare HTML |
| 489 |
$HTML = str_replace( |
| 490 |
array( ':legend', ':controls', ':parameters' ), |
| 491 |
array( |
| 492 |
! empty( $legend ) ? $legend : '', |
| 493 |
$innerHTML, |
| 494 |
K::params_str( $params ) |
| 495 |
), |
| 496 |
'<fieldset :parameters><legend>:legend</legend>:controls</fieldset>' |
| 497 |
); |
| 498 |
|
| 499 |
// Print or return the input field HTML |
| 500 |
if( ! empty ( $args[ 'return' ] ) ) { |
| 501 |
return $HTML; |
| 502 |
} else { |
| 503 |
echo wp_kses( $HTML, K::allowed_html() ); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
* Wraps given input in an html tag |
| 509 |
*/ |
| 510 |
static function wrap( $content = '' ) { |
| 511 |
|
| 512 |
// $params |
| 513 |
if( func_num_args() > 1 ) { |
| 514 |
$params = func_get_arg(1); |
| 515 |
} |
| 516 |
if( empty( $params ) ) { |
| 517 |
$params = array(); |
| 518 |
} |
| 519 |
|
| 520 |
// $args |
| 521 |
if( func_num_args() > 2 ) { |
| 522 |
$args = func_get_arg(2); |
| 523 |
} |
| 524 |
if( empty( $args ) ) { |
| 525 |
$args = array(); |
| 526 |
} |
| 527 |
$args += array( |
| 528 |
'in' => 'div', |
| 529 |
'html_before' => '', |
| 530 |
'html_after' => '', |
| 531 |
); |
| 532 |
|
| 533 |
// Build the input field html |
| 534 |
$html = sprintf( '%s<%s %s>%s</%s>%s', |
| 535 |
$args[ 'html_before' ], |
| 536 |
$args[ 'in' ], |
| 537 |
K::params_str( $params ), |
| 538 |
$content, |
| 539 |
$args[ 'in' ], |
| 540 |
$args[ 'html_after' ] |
| 541 |
); |
| 542 |
|
| 543 |
// Print or return the input field HTML |
| 544 |
if( ! empty( $args[ 'return' ] ) ) { |
| 545 |
return $html; |
| 546 |
} else { |
| 547 |
echo wp_kses( $html, K::allowed_html() ); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* Prepares an array of params and their values to be added to an html element |
| 553 |
*/ |
| 554 |
static function params_str( $params ) { |
| 555 |
$params_str = ''; |
| 556 |
foreach( $params as $parameter => $value ) { |
| 557 |
if( $value ) { |
| 558 |
if( strlen( $value ) ) { |
| 559 |
$params_str .= sprintf( ' %s="%s"', $parameter, esc_attr( $value ) ); |
| 560 |
|
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
return $params_str; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
add_action( 'in_admin_footer', 'k_scripts' ); |
| 569 |
function k_scripts() { |
| 570 |
?> |
| 571 |
<style> |
| 572 |
fieldset.k { |
| 573 |
border: solid 1px lightgray; |
| 574 |
margin-bottom: 1em; |
| 575 |
padding-left: .5em; |
| 576 |
padding-right: .5em; |
| 577 |
} |
| 578 |
fieldset.k legend { |
| 579 |
background: white; |
| 580 |
padding: .25em .5em; |
| 581 |
box-shadow: 0 0 3px silver; |
| 582 |
transition: all .5s; |
| 583 |
} |
| 584 |
fieldset.k legend.highlighted { |
| 585 |
background: lightgray; |
| 586 |
box-shadow: 0 0 1px black; |
| 587 |
} |
| 588 |
fieldset.k.expanded legend { |
| 589 |
font-weight: bold; |
| 590 |
} |
| 591 |
fieldset.k.collapsible legend { |
| 592 |
cursor: pointer; |
| 593 |
} |
| 594 |
fieldset.k.collapsed :not(legend) { |
| 595 |
display: none; |
| 596 |
} |
| 597 |
fieldset.k.collapsed { |
| 598 |
display: inline; |
| 599 |
border: none; |
| 600 |
} |
| 601 |
</style> |
| 602 |
<script> |
| 603 |
jQuery( document ).ready( function( $ ) { |
| 604 |
$( '.collapsible legend' ).click( function (){ |
| 605 |
$this = $( this ); |
| 606 |
$fieldset = $this.parent(); |
| 607 |
// Collapse all except target |
| 608 |
$fieldset.parent() |
| 609 |
.find( 'fieldset' ) |
| 610 |
.not( $fieldset ) |
| 611 |
.removeClass( 'expanded' ) |
| 612 |
.addClass( 'collapsed' ) |
| 613 |
.trigger( 'collapse' ); |
| 614 |
// Toggle target |
| 615 |
$fieldset.toggleClass( 'collapsed' ); |
| 616 |
// Add expanded class to non-collpased and vice-versa, then trigger events |
| 617 |
if ( $fieldset.hasClass('collapsed') ) { |
| 618 |
$fieldset.removeClass( 'expanded' ); |
| 619 |
$fieldset.trigger( 'collapse' ); |
| 620 |
} else { |
| 621 |
$fieldset.addClass( 'expanded' ); |
| 622 |
$fieldset.trigger( 'expand' ); |
| 623 |
} |
| 624 |
} ); |
| 625 |
} ); |
| 626 |
</script> |
| 627 |
<?php |
| 628 |
} |
| 629 |
|
| 630 |
function k_selector( $name, $selected_options = array(), $return = false ) { |
| 631 |
|
| 632 |
global $post; |
| 633 |
// Dirty fix to restore the global $post |
| 634 |
$post_bak = $post; |
| 635 |
|
| 636 |
// Get all post types except media |
| 637 |
$post_types = get_post_types( array( 'public' => true ) ); |
| 638 |
unset( $post_types[ 'attachment' ] ); |
| 639 |
|
| 640 |
$ret = ob_start(); |
| 641 |
|
| 642 |
// Start ouput |
| 643 |
echo '<select data-placeholder="Type to search for posts, categories or pages." name="' . esc_attr( $name ) . '[]" class="select2" multiple="multiple" style="width: 100%;">'; |
| 644 |
|
| 645 |
// Front page |
| 646 |
K::wrap( 'None', |
| 647 |
array( |
| 648 |
'value' => '', |
| 649 |
'selected' => in_array( '', $selected_options ), |
| 650 |
), |
| 651 |
array( 'in' => 'option' ) |
| 652 |
); |
| 653 |
|
| 654 |
K::wrap( 'Front page', |
| 655 |
array( |
| 656 |
'value' => '~', |
| 657 |
'selected' => in_array( '~', $selected_options ), |
| 658 |
), |
| 659 |
array( 'in' => 'option' ) |
| 660 |
); |
| 661 |
|
| 662 |
foreach ($post_types as $post_type => $post_type_args ) { |
| 663 |
|
| 664 |
$post_type_obj = get_post_type_object( $post_type ); |
| 665 |
$post_type_name = $post_type_obj->labels->singular_name; |
| 666 |
|
| 667 |
$options = array(); |
| 668 |
|
| 669 |
// Add taxonomy/terms options |
| 670 |
$taxonomies = get_object_taxonomies( $post_type ); |
| 671 |
foreach ( $taxonomies as $taxonomy ) { |
| 672 |
$taxonomy_obj = get_taxonomy( $taxonomy ); |
| 673 |
$taxonomy_name = $taxonomy_obj->labels->singular_name; |
| 674 |
$terms = get_categories("taxonomy=$taxonomy&type=$post_type"); |
| 675 |
foreach ($terms as $term) { |
| 676 |
$options[ 'taxonomies' ][ "$post_type:$term->term_id" ] = |
| 677 |
$post_type_name |
| 678 |
. " › $taxonomy_name" |
| 679 |
. " › $term->name" |
| 680 |
; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
// Add posts options |
| 685 |
$the_query = new WP_Query( "post_type=$post_type&posts_per_page=-1" ); |
| 686 |
if ( $the_query->have_posts() ) { |
| 687 |
|
| 688 |
while ( $the_query->have_posts() ) { |
| 689 |
$the_query->the_post(); |
| 690 |
$options[ 'posts' ][ '#' . get_the_ID() ] = $post_type_name |
| 691 |
. ' › ' |
| 692 |
. '#' . get_the_ID() . ' – ' |
| 693 |
. ( get_the_title() ? get_the_title() : '(no title)' ) |
| 694 |
; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
// Dirty fix to restore the global $post |
| 699 |
$post = $post_bak; |
| 700 |
|
| 701 |
// Posts > All |
| 702 |
echo '<optgroup label="' . esc_attr( $post_type_name ) . '">'; |
| 703 |
printf( |
| 704 |
'<option value="%s" %s >%s</option>' |
| 705 |
, esc_attr( $post_type ) |
| 706 |
, ( in_array( $post_type, $selected_options ) ? 'selected' : '' ) |
| 707 |
, esc_html( $post_type_name ) . ' › All' |
| 708 |
); |
| 709 |
echo '</optgroup>'; |
| 710 |
|
| 711 |
// Posts > Taoxonomies |
| 712 |
if ( ! empty( $options[ 'taxonomies' ] ) ) { |
| 713 |
printf( |
| 714 |
'<optgroup label="%s">' |
| 715 |
, esc_attr( $post_type_name ) . ' › Taxonomies' |
| 716 |
); |
| 717 |
foreach ( $options[ 'taxonomies' ] as $k => $v ) { |
| 718 |
$selected = ( in_array( $k, $selected_options ) ) ? 'selected="selected"' : ''; |
| 719 |
printf( '<option value="%s" %s >%s</option>', esc_attr( $k ), esc_attr( $selected ), esc_html( $v ) ); |
| 720 |
} |
| 721 |
echo '</optgroup>'; |
| 722 |
} |
| 723 |
|
| 724 |
// Posts > content |
| 725 |
if ( ! empty( $options[ 'posts' ] ) ) { |
| 726 |
printf( '<optgroup label="%s">' |
| 727 |
, esc_attr( $post_type_name ) . ' › Content' |
| 728 |
); |
| 729 |
foreach ( $options[ 'posts' ] as $k => $v ) { |
| 730 |
$selected = ( in_array( $k, $selected_options ) ) ? 'selected="selected"' : ''; |
| 731 |
printf( '<option value="%s" %s >%s</option>' |
| 732 |
, esc_attr( $k ) |
| 733 |
, esc_attr( $selected ) |
| 734 |
, esc_html( $v ) |
| 735 |
); |
| 736 |
} |
| 737 |
echo '</optgroup>'; |
| 738 |
} |
| 739 |
} |
| 740 |
echo '</select>'; |
| 741 |
|
| 742 |
$ret = ob_get_clean(); |
| 743 |
|
| 744 |
if ( $return ) { |
| 745 |
return $ret; |
| 746 |
} else { |
| 747 |
echo wp_kses( $ret, K::allowed_html() ); |
| 748 |
} |
| 749 |
} |