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-async.php
201 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_field_flexible_content_async')): |
| 8 | |
| 9 | class acfe_field_flexible_content_async{ |
| 10 | |
| 11 | /** |
| 12 | * construct |
| 13 | */ |
| 14 | function __construct(){ |
| 15 | |
| 16 | // Hooks |
| 17 | add_filter('acfe/flexible/defaults_field', array($this, 'defaults_field'), 5); |
| 18 | add_action('acfe/flexible/render_field_settings', array($this, 'render_field_settings'), 5); |
| 19 | add_filter('acfe/flexible/wrapper_attributes', array($this, 'wrapper_attributes'), 10, 2); |
| 20 | add_filter('acfe/flexible/layouts/model', array($this, 'layout_model'), 10, 3); |
| 21 | |
| 22 | // Ajax |
| 23 | add_action('wp_ajax_acfe/flexible/models', array($this, 'ajax_layout_model')); |
| 24 | add_action('wp_ajax_nopriv_acfe/flexible/models', array($this, 'ajax_layout_model')); |
| 25 | |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * defaults_field |
| 31 | * |
| 32 | * @param $field |
| 33 | * |
| 34 | * @return mixed |
| 35 | */ |
| 36 | function defaults_field($field){ |
| 37 | |
| 38 | $field['acfe_flexible_async'] = array(); |
| 39 | return $field; |
| 40 | |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * render_field_settings |
| 46 | * |
| 47 | * @param $field |
| 48 | */ |
| 49 | function render_field_settings($field){ |
| 50 | |
| 51 | acf_render_field_setting($field, array( |
| 52 | 'label' => __('Asynchronous Settings', 'acfe'), |
| 53 | 'name' => 'acfe_flexible_async', |
| 54 | 'key' => 'acfe_flexible_async', |
| 55 | 'instructions' => '<a href="https://www.acf-extended.com/features/fields/flexible-content/advanced-settings#async-settings" target="_blank">' . __('See documentation', 'acfe') . '</a>', |
| 56 | 'type' => 'checkbox', |
| 57 | 'default_value' => '', |
| 58 | 'layout' => 'horizontal', |
| 59 | 'choices' => array( |
| 60 | 'title' => __('Disable Title Ajax', 'acfe'), |
| 61 | 'layout' => __('Asynchronous Layout', 'acfe'), |
| 62 | ), |
| 63 | 'conditional_logic' => array( |
| 64 | array( |
| 65 | array( |
| 66 | 'field' => 'acfe_flexible_advanced', |
| 67 | 'operator' => '==', |
| 68 | 'value' => '1', |
| 69 | ), |
| 70 | ) |
| 71 | ) |
| 72 | )); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * wrapper_attributes |
| 79 | * |
| 80 | * @param $wrapper |
| 81 | * @param $field |
| 82 | * |
| 83 | * @return mixed |
| 84 | */ |
| 85 | function wrapper_attributes($wrapper, $field){ |
| 86 | |
| 87 | $async = $field['acfe_flexible_async']; |
| 88 | |
| 89 | // Ajax Layout |
| 90 | if(in_array('layout', $async)){ |
| 91 | $wrapper['data-acfe-flexible-ajax'] = 1; |
| 92 | } |
| 93 | |
| 94 | // Remove ajax 'layout_title' call |
| 95 | $disable = in_array('title', $async); |
| 96 | $disable = apply_filters("acfe/flexible/remove_ajax_title", $disable, $field); |
| 97 | $disable = apply_filters("acfe/flexible/remove_ajax_title/name={$field['_name']}", $disable, $field); |
| 98 | $disable = apply_filters("acfe/flexible/remove_ajax_title/key={$field['key']}", $disable, $field); |
| 99 | |
| 100 | if($disable){ |
| 101 | $wrapper['data-acfe-flexible-remove-ajax-title'] = 1; |
| 102 | } |
| 103 | |
| 104 | return $wrapper; |
| 105 | |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * layout_model |
| 111 | * |
| 112 | * @param $return |
| 113 | * @param $field |
| 114 | * @param $layout |
| 115 | * |
| 116 | * @return bool|mixed |
| 117 | */ |
| 118 | function layout_model($return, $field, $layout){ |
| 119 | |
| 120 | if(!in_array('layout', $field['acfe_flexible_async'])){ |
| 121 | return $return; |
| 122 | } |
| 123 | |
| 124 | $i = 'acfcloneindex'; |
| 125 | $id = 'acfcloneindex'; |
| 126 | $value = array(); |
| 127 | $prefix = $field['name'] . '[' . $id . ']'; |
| 128 | $name = $field['_name']; |
| 129 | $key = $field['key']; |
| 130 | $l_name = $layout['name']; |
| 131 | |
| 132 | $div = array( |
| 133 | 'class' => 'layout acf-clone', |
| 134 | 'data-id' => 'acfcloneindex', |
| 135 | 'data-layout' => $layout['name'] |
| 136 | ); |
| 137 | |
| 138 | $div = apply_filters("acfe/flexible/layouts/div", $div, $layout, $field, $i, $value, $prefix); |
| 139 | $div = apply_filters("acfe/flexible/layouts/div/name={$name}", $div, $layout, $field, $i, $value, $prefix); |
| 140 | $div = apply_filters("acfe/flexible/layouts/div/key={$key}", $div, $layout, $field, $i, $value, $prefix); |
| 141 | $div = apply_filters("acfe/flexible/layouts/div/layout={$l_name}", $div, $layout, $field, $i, $value, $prefix); |
| 142 | $div = apply_filters("acfe/flexible/layouts/div/name={$name}&layout={$l_name}", $div, $layout, $field, $i, $value, $prefix); |
| 143 | $div = apply_filters("acfe/flexible/layouts/div/key={$key}&layout={$l_name}", $div, $layout, $field, $i, $value, $prefix); |
| 144 | |
| 145 | echo '<div ' . acf_esc_atts($div) . '></div>'; |
| 146 | |
| 147 | return true; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * ajax_layout_model |
| 154 | */ |
| 155 | function ajax_layout_model(){ |
| 156 | |
| 157 | // options |
| 158 | $options = acf_parse_args($_POST, array( |
| 159 | 'field_key' => '', |
| 160 | 'layout' => '', |
| 161 | 'nonce' => '', |
| 162 | )); |
| 163 | |
| 164 | // verify nonce |
| 165 | if(!acf_verify_ajax($options['nonce'], $options['field_key'], true)){ |
| 166 | die(); |
| 167 | } |
| 168 | |
| 169 | // get field |
| 170 | $field = acf_get_field($options['field_key']); |
| 171 | if(!$field){ |
| 172 | die(); |
| 173 | } |
| 174 | |
| 175 | // check setting |
| 176 | if(!in_array('layout', $field['acfe_flexible_async'])){ |
| 177 | die(); |
| 178 | } |
| 179 | |
| 180 | // prepare field |
| 181 | $field = acf_prepare_field($field); |
| 182 | |
| 183 | // loop available layouts |
| 184 | foreach($field['layouts'] as $k => $layout){ |
| 185 | |
| 186 | if($layout['name'] === $options['layout']){ |
| 187 | acf_get_instance('acfe_field_flexible_content')->render_layout($field, $layout, 'acfcloneindex', array()); // render layout |
| 188 | die(); |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | |
| 193 | die(); |
| 194 | |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | acf_new_instance('acfe_field_flexible_content_async'); |
| 200 | |
| 201 | endif; |