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-post-object.php
297 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_field_post_object')): |
| 8 | |
| 9 | class acfe_field_post_object extends acfe_field_extend{ |
| 10 | |
| 11 | /** |
| 12 | * initialize |
| 13 | */ |
| 14 | function initialize(){ |
| 15 | |
| 16 | $this->name = 'post_object'; |
| 17 | $this->defaults = array( |
| 18 | 'save_custom' => 0, |
| 19 | 'save_post_type' => '', |
| 20 | 'save_post_status' => '', |
| 21 | ); |
| 22 | |
| 23 | // hooks |
| 24 | $this->add_field_filter('acf/update_value', array($this, '_update_value'), 5, 3); |
| 25 | |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * render_field_settings |
| 31 | * |
| 32 | * @param $field |
| 33 | */ |
| 34 | function render_field_settings($field){ |
| 35 | |
| 36 | // save custom value |
| 37 | acf_render_field_setting($field, array( |
| 38 | 'label' => __('Allow & Save Custom value', 'acf'), |
| 39 | 'instructions' => '', |
| 40 | 'name' => 'save_custom', |
| 41 | 'type' => 'true_false', |
| 42 | 'ui' => 1, |
| 43 | )); |
| 44 | |
| 45 | // save post_type |
| 46 | acf_render_field_setting($field, array( |
| 47 | 'label' => __('New Post Arguments', 'acf'), |
| 48 | 'instructions' => __('See available hooks in the <a href="https://www.acf-extended.com/features/fields/post-object#custom-value-hooks" target="_blank">documentation</a>.', 'acfe'), |
| 49 | 'name' => 'save_post_type', |
| 50 | 'type' => 'acfe_post_types', |
| 51 | 'field_type' => 'select', |
| 52 | 'conditional_logic' => array( |
| 53 | 'field' => 'save_custom', |
| 54 | 'operator' => '==', |
| 55 | 'value' => 1 |
| 56 | ) |
| 57 | )); |
| 58 | |
| 59 | // save post_status |
| 60 | acf_render_field_setting($field, array( |
| 61 | 'label' => '', |
| 62 | 'instructions' => '', |
| 63 | 'name' => 'save_post_status', |
| 64 | 'type' => 'acfe_post_statuses', |
| 65 | 'field_type' => 'select', |
| 66 | 'conditional_logic' => array( |
| 67 | 'field' => 'save_custom', |
| 68 | 'operator' => '==', |
| 69 | 'value' => 1 |
| 70 | ), |
| 71 | '_append' => 'save_post_type' |
| 72 | )); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * field_wrapper_attributes |
| 79 | * |
| 80 | * @param $wrapper |
| 81 | * @param $field |
| 82 | * |
| 83 | * @return mixed |
| 84 | */ |
| 85 | function field_wrapper_attributes($wrapper, $field){ |
| 86 | |
| 87 | if($field['save_custom']){ |
| 88 | $wrapper['data-acfe-allow-custom'] = 1; |
| 89 | } |
| 90 | |
| 91 | return $wrapper; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * _update_value |
| 98 | * |
| 99 | * acf/update_value:5 |
| 100 | * |
| 101 | * @param $value |
| 102 | * @param $post_id |
| 103 | * @param $field |
| 104 | * |
| 105 | * @return array|false|mixed|string[] |
| 106 | */ |
| 107 | function _update_value($value, $post_id, $field){ |
| 108 | |
| 109 | // bail early if empty |
| 110 | if(empty($value)){ |
| 111 | return $value; |
| 112 | } |
| 113 | |
| 114 | // bail early if no save custom setting |
| 115 | if(!$field['save_custom']){ |
| 116 | return $value; |
| 117 | } |
| 118 | |
| 119 | // bail early when local meta |
| 120 | if(acfe_is_local_post_id($post_id)){ |
| 121 | return $value; |
| 122 | } |
| 123 | |
| 124 | // new post args |
| 125 | $post_type = acfe_get($field, 'save_post_type', 'post'); |
| 126 | $post_status = acfe_get($field, 'save_post_status', 'publish'); |
| 127 | |
| 128 | // vars |
| 129 | $is_array = is_array($value); |
| 130 | $value = acfe_as_array($value); |
| 131 | |
| 132 | // loop |
| 133 | foreach($value as $k => $v){ |
| 134 | |
| 135 | // has to be words |
| 136 | // (post id are selected posts) |
| 137 | if(is_numeric($v)){ |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | // vars |
| 142 | $title = $v; |
| 143 | |
| 144 | // args |
| 145 | $args = array( |
| 146 | 'post_title' => $title, |
| 147 | 'post_type' => $post_type, |
| 148 | 'post_status' => $post_status, |
| 149 | ); |
| 150 | |
| 151 | // filters |
| 152 | $args = apply_filters("acfe/fields/post_object/custom_save_args", $args, $title, $post_id, $field); |
| 153 | $args = apply_filters("acfe/fields/post_object/custom_save_args/name={$field['name']}", $args, $title, $post_id, $field); |
| 154 | $args = apply_filters("acfe/fields/post_object/custom_save_args/key={$field['key']}", $args, $title, $post_id, $field); |
| 155 | |
| 156 | // do not create post |
| 157 | if($args === false){ |
| 158 | |
| 159 | unset($value[ $k ]); |
| 160 | continue; |
| 161 | |
| 162 | } |
| 163 | |
| 164 | // insert post |
| 165 | $_post_id = wp_insert_post($args); |
| 166 | |
| 167 | // error during creation |
| 168 | if(empty($_post_id) || is_wp_error($_post_id)){ |
| 169 | |
| 170 | unset($value[ $k ]); |
| 171 | continue; |
| 172 | |
| 173 | } |
| 174 | |
| 175 | // actions after create |
| 176 | do_action("acfe/fields/post_object/custom_save", $_post_id, $title, $post_id, $field); |
| 177 | do_action("acfe/fields/post_object/custom_save/name={$field['name']}", $_post_id, $title, $post_id, $field); |
| 178 | do_action("acfe/fields/post_object/custom_save/key={$field['key']}", $_post_id, $title, $post_id, $field); |
| 179 | |
| 180 | // assign new post id as selected |
| 181 | $value[ $k ] = $_post_id; |
| 182 | |
| 183 | } |
| 184 | |
| 185 | // check array |
| 186 | if(!$is_array){ |
| 187 | $value = acfe_unarray($value); |
| 188 | } |
| 189 | |
| 190 | // return |
| 191 | return $value; |
| 192 | |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /** |
| 197 | * format_front_value |
| 198 | * |
| 199 | * @param $formatted |
| 200 | * @param $unformatted |
| 201 | * @param $post_id |
| 202 | * @param $field |
| 203 | * @param $form |
| 204 | * |
| 205 | * @return string |
| 206 | */ |
| 207 | function format_front_value($formatted, $unformatted, $post_id, $field, $form){ |
| 208 | |
| 209 | // vars |
| 210 | $value = acfe_as_array($unformatted); |
| 211 | $array = array(); |
| 212 | |
| 213 | // loop values |
| 214 | foreach($value as $p_id){ |
| 215 | |
| 216 | // get post |
| 217 | $post = get_post($p_id); |
| 218 | |
| 219 | // validate |
| 220 | if($post && !is_wp_error($post)){ |
| 221 | $array[] = get_the_title($post->ID); |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
| 226 | // merge |
| 227 | return implode(', ', $array); |
| 228 | |
| 229 | } |
| 230 | |
| 231 | |
| 232 | /** |
| 233 | * validate_front_value |
| 234 | * |
| 235 | * @param $valid |
| 236 | * @param $value |
| 237 | * @param $field |
| 238 | * @param $input |
| 239 | * @param $form |
| 240 | * |
| 241 | * @return false |
| 242 | */ |
| 243 | function validate_front_value($valid, $value, $field, $input, $form){ |
| 244 | |
| 245 | // bail early |
| 246 | if(!$this->pre_validate_front_value($valid, $value, $field, $form)){ |
| 247 | return $valid; |
| 248 | } |
| 249 | |
| 250 | // custom value allowed |
| 251 | if(!empty($field['save_custom'])){ |
| 252 | return $valid; |
| 253 | } |
| 254 | |
| 255 | // vars |
| 256 | $value = acfe_as_array($value); |
| 257 | |
| 258 | // loop values |
| 259 | foreach($value as $v){ |
| 260 | |
| 261 | // get post |
| 262 | $post = get_post($v); |
| 263 | |
| 264 | // check post exists |
| 265 | if(!$post || is_wp_error($post)){ |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | // check query method exists |
| 270 | if(method_exists($this->instance, 'get_ajax_query')){ |
| 271 | |
| 272 | // query post object ajax query |
| 273 | $query = $this->instance->get_ajax_query(array( |
| 274 | 'field_key' => $field['key'], |
| 275 | 'post_id' => $form['post_id'], |
| 276 | 'include' => $v, |
| 277 | )); |
| 278 | |
| 279 | // return false if no results |
| 280 | if(empty($query)){ |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | } |
| 287 | |
| 288 | // return |
| 289 | return $valid; |
| 290 | |
| 291 | } |
| 292 | |
| 293 | } |
| 294 | |
| 295 | acf_new_instance('acfe_field_post_object'); |
| 296 | |
| 297 | endif; |