field-advanced-link.php
3 months ago
field-button-group.php
1 week ago
field-button.php
7 months ago
field-checkbox.php
3 months ago
field-clone.php
2 years ago
field-code-editor.php
3 months ago
field-column.php
3 years ago
field-dynamic-render.php
3 years ago
field-file.php
3 months ago
field-flexible-content-actions-title.php
3 months ago
field-flexible-content-actions-toggle.php
3 months ago
field-flexible-content-actions.php
3 months ago
field-flexible-content-async.php
4 months ago
field-flexible-content-compatibility.php
3 months ago
field-flexible-content-controls.php
9 months ago
field-flexible-content-hide.php
3 months ago
field-flexible-content-hooks.php
9 months ago
field-flexible-content-modal-edit.php
9 months ago
field-flexible-content-modal-select.php
3 months ago
field-flexible-content-modal-settings.php
3 months ago
field-flexible-content-popup.php
3 months ago
field-flexible-content-preview.php
3 months ago
field-flexible-content-state.php
9 months ago
field-flexible-content-thumbnail.php
9 months ago
field-flexible-content-wysiwyg.php
9 months ago
field-flexible-content.php
3 months ago
field-forms.php
3 months ago
field-gallery.php
3 months ago
field-google-map.php
3 months ago
field-group.php
3 months ago
field-hidden.php
3 years ago
field-image.php
3 months ago
field-post-object.php
3 months ago
field-post-statuses.php
3 months ago
field-post-types.php
3 months ago
field-radio.php
3 months ago
field-recaptcha.php
3 months ago
field-relationship.php
3 months ago
field-repeater.php
3 months ago
field-select.php
3 months ago
field-slug.php
1 year ago
field-taxonomies.php
3 months ago
field-taxonomy-terms.php
3 months ago
field-taxonomy.php
3 months ago
field-textarea.php
3 months ago
field-user-roles.php
3 months ago
field-user.php
3 months ago
field-wysiwyg.php
3 months ago
field-flexible-content-modal-select.php
596 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_field_flexible_content_select')): |
| 8 | |
| 9 | class acfe_field_flexible_content_select{ |
| 10 | |
| 11 | /** |
| 12 | * construct |
| 13 | */ |
| 14 | function __construct(){ |
| 15 | |
| 16 | // Hooks |
| 17 | add_filter('acfe/flexible/defaults_field', array($this, 'defaults_field'), 10); |
| 18 | add_filter('acfe/flexible/defaults_layout', array($this, 'defaults_layout'), 10); |
| 19 | |
| 20 | add_action('acfe/flexible/render_field_settings', array($this, 'render_field_settings'), 10); |
| 21 | add_action('acfe/flexible/render_layout_settings', array($this, 'render_layout_settings'), 10, 3); |
| 22 | |
| 23 | add_filter('acfe/flexible/wrapper_attributes', array($this, 'wrapper_attributes'), 10, 2); |
| 24 | add_filter('acfe/flexible/layouts/select_atts', array($this, 'select_atts'), 10, 3); |
| 25 | add_filter('acfe/flexible/layouts/select_label', array($this, 'select_label'), 10, 3); |
| 26 | |
| 27 | add_action('acfe/flexible/render_popup_select', array($this, 'render_popup_select_open_wrapper'), 5); |
| 28 | add_action('acfe/flexible/render_popup_select', array($this, 'render_popup_select_categories'), 7); |
| 29 | add_action('acfe/flexible/render_popup_select', array($this, 'render_popup_select_before_layouts'), 9); |
| 30 | add_action('acfe/flexible/render_popup_select', array($this, 'render_popup_select_close_wrapper'), 15); |
| 31 | |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * defaults_field |
| 37 | * |
| 38 | * @param $field |
| 39 | * |
| 40 | * @return mixed |
| 41 | */ |
| 42 | function defaults_field($field){ |
| 43 | |
| 44 | $field['acfe_flexible_modal'] = array( |
| 45 | 'acfe_flexible_modal_enabled' => false, |
| 46 | 'acfe_flexible_modal_title' => false, |
| 47 | 'acfe_flexible_modal_size' => 'xlarge', |
| 48 | 'acfe_flexible_modal_col' => '4', |
| 49 | 'acfe_flexible_modal_categories' => false, |
| 50 | ); |
| 51 | |
| 52 | return $field; |
| 53 | |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * defaults_layout |
| 59 | * |
| 60 | * @param $layout |
| 61 | * |
| 62 | * @return mixed |
| 63 | */ |
| 64 | function defaults_layout($layout){ |
| 65 | |
| 66 | $layout['acfe_flexible_category'] = false; |
| 67 | |
| 68 | return $layout; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * render_field_settings |
| 75 | * |
| 76 | * @param $field |
| 77 | */ |
| 78 | function render_field_settings($field){ |
| 79 | |
| 80 | acf_render_field_setting($field, array( |
| 81 | 'label' => __('Select Modal', 'acfe'), |
| 82 | 'name' => 'acfe_flexible_modal', |
| 83 | 'key' => 'acfe_flexible_modal', |
| 84 | 'instructions' => __('Select layouts in a modal', 'acfe') . '. ' . '<a href="https://www.acf-extended.com/features/fields/flexible-content/modal-settings#selection-modal" target="_blank">' . __('See documentation', 'acfe') . '</a>', |
| 85 | 'type' => 'group', |
| 86 | 'layout' => 'block', |
| 87 | 'sub_fields' => array( |
| 88 | array( |
| 89 | 'label' => '', |
| 90 | 'name' => 'acfe_flexible_modal_enabled', |
| 91 | 'key' => 'acfe_flexible_modal_enabled', |
| 92 | 'type' => 'true_false', |
| 93 | 'instructions' => '', |
| 94 | 'required' => false, |
| 95 | 'wrapper' => array( |
| 96 | 'class' => 'acfe_width_auto', |
| 97 | 'id' => '', |
| 98 | ), |
| 99 | 'message' => '', |
| 100 | 'default_value' => false, |
| 101 | 'ui' => true, |
| 102 | 'ui_on_text' => '', |
| 103 | 'ui_off_text' => '', |
| 104 | 'conditional_logic' => false, |
| 105 | ), |
| 106 | array( |
| 107 | 'label' => '', |
| 108 | 'name' => 'acfe_flexible_modal_title', |
| 109 | 'key' => 'acfe_flexible_modal_title', |
| 110 | 'type' => 'text', |
| 111 | 'prepend' => __('Modal Title', 'acfe'), |
| 112 | 'placeholder' => $field['button_label'], |
| 113 | 'instructions' => false, |
| 114 | 'required' => false, |
| 115 | 'wrapper' => array( |
| 116 | 'width' => '25', |
| 117 | 'class' => '', |
| 118 | 'id' => '', |
| 119 | ), |
| 120 | 'conditional_logic' => array( |
| 121 | array( |
| 122 | array( |
| 123 | 'field' => 'acfe_flexible_modal_enabled', |
| 124 | 'operator' => '==', |
| 125 | 'value' => '1', |
| 126 | ) |
| 127 | ) |
| 128 | ) |
| 129 | ), |
| 130 | array( |
| 131 | 'label' => '', |
| 132 | 'name' => 'acfe_flexible_modal_size', |
| 133 | 'key' => 'acfe_flexible_modal_size', |
| 134 | 'type' => 'select', |
| 135 | 'prepend' => '', |
| 136 | 'instructions' => false, |
| 137 | 'required' => false, |
| 138 | 'choices' => array( |
| 139 | 'small' => __('Small', 'acfe'), |
| 140 | 'medium' => __('Medium', 'acfe'), |
| 141 | 'large' => __('Large', 'acfe'), |
| 142 | 'xlarge' => __('Extra Large', 'acfe'), |
| 143 | 'full' => __('Full', 'acfe'), |
| 144 | ), |
| 145 | 'default_value' => 'large', |
| 146 | 'wrapper' => array( |
| 147 | 'width' => '25', |
| 148 | 'class' => '', |
| 149 | 'id' => '', |
| 150 | 'data-acfe-prepend' => __('Size', 'acfe'), |
| 151 | ), |
| 152 | 'conditional_logic' => array( |
| 153 | array( |
| 154 | array( |
| 155 | 'field' => 'acfe_flexible_modal_enabled', |
| 156 | 'operator' => '==', |
| 157 | 'value' => '1', |
| 158 | ) |
| 159 | ) |
| 160 | ) |
| 161 | ), |
| 162 | array( |
| 163 | 'label' => '', |
| 164 | 'name' => 'acfe_flexible_modal_col', |
| 165 | 'key' => 'acfe_flexible_modal_col', |
| 166 | 'type' => 'select', |
| 167 | 'prepend' => '', |
| 168 | 'instructions' => false, |
| 169 | 'required' => false, |
| 170 | 'choices' => array( |
| 171 | '1' => '1', |
| 172 | '2' => '2', |
| 173 | '3' => '3', |
| 174 | '4' => '4', |
| 175 | '5' => '5', |
| 176 | '6' => '6', |
| 177 | ), |
| 178 | 'default_value' => '4', |
| 179 | 'wrapper' => array( |
| 180 | 'width' => '15', |
| 181 | 'class' => '', |
| 182 | 'id' => '', |
| 183 | 'data-acfe-prepend' => __('Cols', 'acfe'), |
| 184 | ), |
| 185 | 'conditional_logic' => array( |
| 186 | array( |
| 187 | array( |
| 188 | 'field' => 'acfe_flexible_modal_enabled', |
| 189 | 'operator' => '==', |
| 190 | 'value' => '1', |
| 191 | ) |
| 192 | ) |
| 193 | ) |
| 194 | ), |
| 195 | array( |
| 196 | 'label' => '', |
| 197 | 'name' => 'acfe_flexible_modal_categories', |
| 198 | 'key' => 'acfe_flexible_modal_categories', |
| 199 | 'type' => 'select', |
| 200 | 'prepend' => '', |
| 201 | 'instructions' => false, |
| 202 | 'required' => false, |
| 203 | 'choices' => array( |
| 204 | '1' => __('Enabled', 'acfe'), |
| 205 | ), |
| 206 | 'default_value' => '', |
| 207 | 'allow_null' => true, |
| 208 | 'placeholder' => __('Disabled', 'acfe'), |
| 209 | 'wrapper' => array( |
| 210 | 'width' => '25', |
| 211 | 'class' => '', |
| 212 | 'id' => '', |
| 213 | 'data-acfe-prepend' => __('Categories', 'acfe'), |
| 214 | ), |
| 215 | 'conditional_logic' => array( |
| 216 | array( |
| 217 | array( |
| 218 | 'field' => 'acfe_flexible_modal_enabled', |
| 219 | 'operator' => '==', |
| 220 | 'value' => '1', |
| 221 | ) |
| 222 | ) |
| 223 | ) |
| 224 | ), |
| 225 | ), |
| 226 | 'conditional_logic' => array( |
| 227 | array( |
| 228 | array( |
| 229 | 'field' => 'acfe_flexible_advanced', |
| 230 | 'operator' => '==', |
| 231 | 'value' => '1', |
| 232 | ), |
| 233 | ) |
| 234 | ), |
| 235 | 'wrapper' => array( |
| 236 | 'class' => 'acfe-field-setting-flex' |
| 237 | ) |
| 238 | )); |
| 239 | |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /** |
| 244 | * render_layout_settings |
| 245 | * |
| 246 | * @param $field |
| 247 | * @param $layout |
| 248 | * @param $prefix |
| 249 | */ |
| 250 | function render_layout_settings($field, $layout, $prefix){ |
| 251 | |
| 252 | if(!$field['acfe_flexible_modal']['acfe_flexible_modal_categories']){ |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | // Title |
| 257 | echo '</li>'; |
| 258 | acf_render_field_wrap(array( |
| 259 | 'label' => __('Select Modal', 'acfe'), |
| 260 | 'type' => 'hidden', |
| 261 | 'name' => 'acfe_flexible_grid_label', |
| 262 | 'wrapper' => array( |
| 263 | 'class' => 'acfe-flexible-field-setting', |
| 264 | ) |
| 265 | ), 'ul'); |
| 266 | echo '<li>'; |
| 267 | |
| 268 | acf_render_field_wrap(array( |
| 269 | 'prepend' => __('Category', 'acfe'), |
| 270 | 'name' => 'acfe_flexible_category', |
| 271 | 'type' => 'select', |
| 272 | 'ui' => 1, |
| 273 | 'multiple' => 1, |
| 274 | 'allow_custom' => 1, |
| 275 | 'class' => 'acf-fc-meta-name', |
| 276 | 'prefix' => $prefix, |
| 277 | 'value' => $layout['acfe_flexible_category'], |
| 278 | 'placeholder' => __('Enter category', 'acfe'), |
| 279 | 'wrapper' => array( |
| 280 | 'data-acfe-prepend' => 'Categories', |
| 281 | ), |
| 282 | ), 'ul'); |
| 283 | |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /** |
| 288 | * wrapper_attributes |
| 289 | * |
| 290 | * @param $wrapper |
| 291 | * @param $field |
| 292 | * |
| 293 | * @return mixed |
| 294 | */ |
| 295 | function wrapper_attributes($wrapper, $field){ |
| 296 | |
| 297 | if(!$field['acfe_flexible_modal']['acfe_flexible_modal_enabled']){ |
| 298 | return $wrapper; |
| 299 | } |
| 300 | |
| 301 | // modal settings |
| 302 | $wrapper['data-acfe-flexible-modal'] = 1; |
| 303 | $wrapper['data-acfe-flexible-modal-col'] = $field['acfe_flexible_modal']['acfe_flexible_modal_col']; |
| 304 | $wrapper['data-acfe-flexible-modal-size'] = $field['acfe_flexible_modal']['acfe_flexible_modal_size']; |
| 305 | $wrapper['data-acfe-flexible-modal-categories'] = $field['acfe_flexible_modal']['acfe_flexible_modal_categories']; |
| 306 | $wrapper['data-acfe-flexible-modal-title'] = $field['button_label']; |
| 307 | |
| 308 | // modal title |
| 309 | if(!empty($field['acfe_flexible_modal']['acfe_flexible_modal_title'])){ |
| 310 | $wrapper['data-acfe-flexible-modal-title'] = $field['acfe_flexible_modal']['acfe_flexible_modal_title']; |
| 311 | } |
| 312 | |
| 313 | return $wrapper; |
| 314 | |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /** |
| 319 | * select_atts |
| 320 | * |
| 321 | * @param $atts |
| 322 | * @param $layout |
| 323 | * @param $field |
| 324 | * |
| 325 | * @return mixed |
| 326 | */ |
| 327 | function select_atts($atts, $layout, $field){ |
| 328 | |
| 329 | // check setting |
| 330 | if($field['acfe_flexible_modal']['acfe_flexible_modal_categories']){ |
| 331 | $atts['data-category'] = $this->get_layout_categories_names($layout); |
| 332 | } |
| 333 | |
| 334 | // return |
| 335 | return $atts; |
| 336 | |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /** |
| 341 | * select_label |
| 342 | * |
| 343 | * @param $label |
| 344 | * @param $layout |
| 345 | * @param $field |
| 346 | * |
| 347 | * @return mixed|string |
| 348 | */ |
| 349 | function select_label($label, $layout, $field){ |
| 350 | |
| 351 | // check modal select + thumbnail |
| 352 | if($field['acfe_flexible_modal']['acfe_flexible_modal_enabled']){ |
| 353 | $label = "<div class='acfe-fc-layout-label'>{$label}</div>"; |
| 354 | } |
| 355 | |
| 356 | return $label; |
| 357 | |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /** |
| 362 | * render_popup_select_open_wrapper |
| 363 | * |
| 364 | * @param $field |
| 365 | * |
| 366 | * @return void |
| 367 | */ |
| 368 | function render_popup_select_open_wrapper($field){ |
| 369 | |
| 370 | // open wrapper |
| 371 | if($field['acfe_flexible_modal']['acfe_flexible_modal_enabled']){ |
| 372 | echo '<div>'; |
| 373 | } |
| 374 | |
| 375 | } |
| 376 | |
| 377 | |
| 378 | /** |
| 379 | * render_popup_select_categories |
| 380 | * |
| 381 | * @param $field |
| 382 | * |
| 383 | * @return void |
| 384 | */ |
| 385 | function render_popup_select_categories($field){ |
| 386 | |
| 387 | // categories |
| 388 | if(!$field['acfe_flexible_modal']['acfe_flexible_modal_categories']){ |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | // wrapper filters |
| 393 | $wrapper = array('class' => 'acfe-nav-tabs acfe-fc-categories'); |
| 394 | $wrapper = apply_filters("acfe/flexible/modal_select_categories_wrapper", $wrapper, $field); |
| 395 | $wrapper = apply_filters("acfe/flexible/modal_select_categories_wrapper/name={$field['_name']}", $wrapper, $field); |
| 396 | $wrapper = apply_filters("acfe/flexible/modal_select_categories_wrapper/key={$field['key']}", $wrapper, $field); |
| 397 | |
| 398 | // wrapper |
| 399 | echo '<div ' . acf_esc_attrs($wrapper) . '>'; |
| 400 | |
| 401 | // loop categories |
| 402 | foreach($this->get_categories($field) as $name => $category){ |
| 403 | |
| 404 | $atts = array( |
| 405 | 'href' => '#', |
| 406 | 'class' => 'acfe-nav-tab', |
| 407 | 'data-category' => $name, |
| 408 | ); |
| 409 | |
| 410 | if($name === 'acfe-all'){ |
| 411 | $atts['class'] .= ' -active'; |
| 412 | } |
| 413 | |
| 414 | echo '<a ' . acf_esc_attrs($atts) . '>'; |
| 415 | echo acf_esc_html($category); |
| 416 | echo '</a>'; |
| 417 | |
| 418 | } |
| 419 | |
| 420 | echo '</div>'; |
| 421 | |
| 422 | } |
| 423 | |
| 424 | |
| 425 | |
| 426 | /** |
| 427 | * render_popup_select_before_layouts |
| 428 | * |
| 429 | * @param $field |
| 430 | * |
| 431 | * @return void |
| 432 | */ |
| 433 | function render_popup_select_before_layouts($field){ |
| 434 | |
| 435 | // check setting |
| 436 | if(!$field['acfe_flexible_modal']['acfe_flexible_modal_enabled']){ |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | $atts = array( |
| 441 | 'class' => 'acfe-fc-layouts', |
| 442 | ); |
| 443 | |
| 444 | // get col size |
| 445 | $col = (int) $field['acfe_flexible_modal']['acfe_flexible_modal_col']; |
| 446 | $atts['class'] .= " -col-{$col}"; |
| 447 | |
| 448 | if(!$field['acfe_flexible_layouts_thumbnails']){ |
| 449 | $atts['class'] .= ' -no-thumbnails'; |
| 450 | } |
| 451 | |
| 452 | // get col size |
| 453 | echo '<div ' . acf_esc_attrs($atts) . '>'; |
| 454 | |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /** |
| 459 | * render_popup_select_close_wrapper |
| 460 | * |
| 461 | * @param $field |
| 462 | * |
| 463 | * @return void |
| 464 | */ |
| 465 | function render_popup_select_close_wrapper($field){ |
| 466 | |
| 467 | // close wrapper |
| 468 | if($field['acfe_flexible_modal']['acfe_flexible_modal_enabled']){ |
| 469 | echo '</div></div>'; |
| 470 | } |
| 471 | |
| 472 | } |
| 473 | |
| 474 | |
| 475 | /** |
| 476 | * get_categories |
| 477 | * |
| 478 | * @param $field |
| 479 | * |
| 480 | * @return array |
| 481 | */ |
| 482 | function get_categories($field){ |
| 483 | |
| 484 | // initial array |
| 485 | $storage = array(); |
| 486 | |
| 487 | // check settings |
| 488 | if(!$field['acfe_flexible_modal']['acfe_flexible_modal_categories']){ |
| 489 | return $storage; |
| 490 | } |
| 491 | |
| 492 | // loop layouts |
| 493 | foreach($field['layouts'] as $layout){ |
| 494 | |
| 495 | // loop categories |
| 496 | foreach($this->get_layout_categories($layout) as $name => $category){ |
| 497 | |
| 498 | // make sure category is unique |
| 499 | if(!isset($storage[ $name ])){ |
| 500 | $storage[ $name ] = $category; |
| 501 | } |
| 502 | |
| 503 | } |
| 504 | |
| 505 | } |
| 506 | |
| 507 | // sort by keys alphabetically |
| 508 | ksort($storage); |
| 509 | |
| 510 | // prepare categories array |
| 511 | $categories = array( |
| 512 | 'acfe-all' => '<span class="dashicons dashicons-screenoptions"></span><span class="label">' . __('All Layouts', 'acfe') . '</span>', |
| 513 | ); |
| 514 | |
| 515 | // loop storage |
| 516 | foreach($storage as $name => $category){ |
| 517 | $categories[ $name ] = $category; |
| 518 | } |
| 519 | |
| 520 | // filters |
| 521 | $categories = apply_filters("acfe/flexible/modal_select_categories", $categories, $field); |
| 522 | $categories = apply_filters("acfe/flexible/modal_select_categories/name={$field['_name']}", $categories, $field); |
| 523 | $categories = apply_filters("acfe/flexible/modal_select_categories/key={$field['key']}", $categories, $field); |
| 524 | |
| 525 | // return |
| 526 | return $categories; |
| 527 | |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /** |
| 532 | * get_layout_categories |
| 533 | * |
| 534 | * @param $layout |
| 535 | * |
| 536 | * @return array |
| 537 | */ |
| 538 | function get_layout_categories($layout){ |
| 539 | |
| 540 | // get layout categories |
| 541 | $categories = $layout['acfe_flexible_category']; |
| 542 | |
| 543 | // back-compatibility |
| 544 | if(is_string($categories) && !empty($categories)){ |
| 545 | $categories = explode('|', $categories); |
| 546 | $categories = array_map('trim', $categories); |
| 547 | } |
| 548 | |
| 549 | // sanitize |
| 550 | $categories = acfe_as_array($categories); |
| 551 | $categories = array_filter($categories); |
| 552 | |
| 553 | $storage = array(); |
| 554 | |
| 555 | // loop categories |
| 556 | foreach($categories as $category){ |
| 557 | |
| 558 | //slugify |
| 559 | $name = sanitize_title($category); |
| 560 | |
| 561 | // make sure category is unique |
| 562 | if(!isset($storage[ $name ])){ |
| 563 | $storage[ $name ] = $category; |
| 564 | } |
| 565 | |
| 566 | } |
| 567 | |
| 568 | // return |
| 569 | return $storage; |
| 570 | |
| 571 | } |
| 572 | |
| 573 | |
| 574 | /** |
| 575 | * get_layout_categories_names |
| 576 | * |
| 577 | * @param $layout |
| 578 | * |
| 579 | * @return array |
| 580 | */ |
| 581 | function get_layout_categories_names($layout){ |
| 582 | |
| 583 | // get layout categories |
| 584 | $layout_categories = $this->get_layout_categories($layout); |
| 585 | $layout_categories = array_keys($layout_categories); |
| 586 | |
| 587 | // return |
| 588 | return $layout_categories; |
| 589 | |
| 590 | } |
| 591 | |
| 592 | } |
| 593 | |
| 594 | acf_new_instance('acfe_field_flexible_content_select'); |
| 595 | |
| 596 | endif; |