page_link.php
| 1 | <?php |
| 2 | |
| 3 | class acf_Page_link extends acf_Field |
| 4 | { |
| 5 | |
| 6 | /*-------------------------------------------------------------------------------------- |
| 7 | * |
| 8 | * Constructor |
| 9 | * |
| 10 | * @author Elliot Condon |
| 11 | * @since 1.0.0 |
| 12 | * @updated 2.2.0 |
| 13 | * |
| 14 | *-------------------------------------------------------------------------------------*/ |
| 15 | |
| 16 | function __construct($parent) |
| 17 | { |
| 18 | parent::__construct($parent); |
| 19 | |
| 20 | $this->name = 'page_link'; |
| 21 | $this->title = __('Page Link','acf'); |
| 22 | |
| 23 | } |
| 24 | |
| 25 | |
| 26 | /*-------------------------------------------------------------------------------------- |
| 27 | * |
| 28 | * create_field |
| 29 | * |
| 30 | * @author Elliot Condon |
| 31 | * @since 2.0.5 |
| 32 | * @updated 2.2.0 |
| 33 | * |
| 34 | *-------------------------------------------------------------------------------------*/ |
| 35 | |
| 36 | function create_field($field) |
| 37 | { |
| 38 | // vars |
| 39 | $field['multiple'] = isset($field['multiple']) ? $field['multiple'] : false; |
| 40 | $field['post_type'] = isset($field['post_type']) ? $field['post_type'] : false; |
| 41 | //$field['meta_key'] = isset($field['meta_key']) ? $field['meta_key'] : false; |
| 42 | //$field['meta_value'] = isset($field['meta_value']) ? $field['meta_value'] : false; |
| 43 | |
| 44 | |
| 45 | if(!$field['post_type'] || !is_array($field['post_type']) || $field['post_type'][0] == "") |
| 46 | { |
| 47 | $field['post_type'] = get_post_types(array('public' => true)); |
| 48 | foreach($field['post_type'] as $key => $value) |
| 49 | { |
| 50 | if($value == 'attachment') |
| 51 | { |
| 52 | unset($field['post_type'][$key]); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // multiple select |
| 58 | $multiple = ''; |
| 59 | if($field['multiple'] == '1') |
| 60 | { |
| 61 | $multiple = ' multiple="multiple" size="5" '; |
| 62 | $field['name'] .= '[]'; |
| 63 | } |
| 64 | |
| 65 | // html |
| 66 | echo '<select id="' . $field['name'] . '" class="' . $field['class'] . '" name="' . $field['name'] . '" ' . $multiple . ' >'; |
| 67 | |
| 68 | // null |
| 69 | if($field['allow_null'] == '1') |
| 70 | { |
| 71 | echo '<option value="null"> - Select - </option>'; |
| 72 | } |
| 73 | |
| 74 | foreach($field['post_type'] as $post_type) |
| 75 | { |
| 76 | // get posts |
| 77 | $posts = false; |
| 78 | |
| 79 | if(is_post_type_hierarchical($post_type)) |
| 80 | { |
| 81 | // get pages |
| 82 | $posts = get_pages(array( |
| 83 | 'numberposts' => -1, |
| 84 | 'post_type' => $post_type, |
| 85 | 'sort_column' => 'menu_order', |
| 86 | 'order' => 'ASC', |
| 87 | 'post_status' => array('publish', 'private', 'draft'), |
| 88 | //'meta_key' => $field['meta_key'], |
| 89 | //'meta_value' => $field['meta_value'], |
| 90 | )); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | // get posts |
| 95 | $posts = get_posts(array( |
| 96 | 'numberposts' => -1, |
| 97 | 'post_type' => $post_type, |
| 98 | 'orderby' => 'title', |
| 99 | 'order' => 'ASC', |
| 100 | 'post_status' => array('publish', 'private', 'draft'), |
| 101 | //'meta_key' => $field['meta_key'], |
| 102 | //'meta_value' => $field['meta_value'], |
| 103 | )); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | // if posts, make a group for them |
| 108 | if($posts) |
| 109 | { |
| 110 | $post_type_object = get_post_type_object($post_type); |
| 111 | $post_type_name = $post_type_object->labels->name; |
| 112 | |
| 113 | echo '<optgroup label="'.$post_type_name.'">'; |
| 114 | |
| 115 | foreach($posts as $post) |
| 116 | { |
| 117 | $key = $post->ID; |
| 118 | |
| 119 | $value = ''; |
| 120 | $ancestors = get_ancestors($post->ID, $post_type); |
| 121 | if($ancestors) |
| 122 | { |
| 123 | foreach($ancestors as $a) |
| 124 | { |
| 125 | $value .= '– '; |
| 126 | } |
| 127 | } |
| 128 | $value .= get_the_title($post->ID); |
| 129 | |
| 130 | // status |
| 131 | if($post->post_status == "private" || $post->post_status == "draft") |
| 132 | { |
| 133 | $value .= " ($post->post_status)"; |
| 134 | } |
| 135 | |
| 136 | $selected = ''; |
| 137 | |
| 138 | |
| 139 | if(is_array($field['value'])) |
| 140 | { |
| 141 | // 2. If the value is an array (multiple select), loop through values and check if it is selected |
| 142 | if(in_array($key, $field['value'])) |
| 143 | { |
| 144 | $selected = 'selected="selected"'; |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | // 3. this is not a multiple select, just check normaly |
| 150 | if($key == $field['value']) |
| 151 | { |
| 152 | $selected = 'selected="selected"'; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>'; |
| 158 | |
| 159 | |
| 160 | } |
| 161 | |
| 162 | echo '</optgroup>'; |
| 163 | |
| 164 | }// endif |
| 165 | |
| 166 | }// endforeach |
| 167 | |
| 168 | |
| 169 | echo '</select>'; |
| 170 | |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /*-------------------------------------------------------------------------------------- |
| 175 | * |
| 176 | * create_options |
| 177 | * |
| 178 | * @author Elliot Condon |
| 179 | * @since 2.0.6 |
| 180 | * @updated 2.2.0 |
| 181 | * |
| 182 | *-------------------------------------------------------------------------------------*/ |
| 183 | |
| 184 | function create_options($key, $field) |
| 185 | { |
| 186 | // defaults |
| 187 | $field['post_type'] = isset($field['post_type']) ? $field['post_type'] : ''; |
| 188 | $field['multiple'] = isset($field['multiple']) ? $field['multiple'] : '0'; |
| 189 | $field['allow_null'] = isset($field['allow_null']) ? $field['allow_null'] : '0'; |
| 190 | |
| 191 | ?> |
| 192 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 193 | <td class="label"> |
| 194 | <label for=""><?php _e("Post Type",'acf'); ?></label> |
| 195 | <p class="description"><?php _e("Filter posts by selecting a post type<br /> |
| 196 | Tip: deselect all post types to show all post type's posts",'acf'); ?></p> |
| 197 | </td> |
| 198 | <td> |
| 199 | <?php |
| 200 | $post_types = array('' => '-All-'); |
| 201 | |
| 202 | foreach (get_post_types() as $post_type ) { |
| 203 | $post_types[$post_type] = $post_type; |
| 204 | } |
| 205 | |
| 206 | unset($post_types['attachment']); |
| 207 | unset($post_types['nav_menu_item']); |
| 208 | unset($post_types['revision']); |
| 209 | unset($post_types['acf']); |
| 210 | |
| 211 | $this->parent->create_field(array( |
| 212 | 'type' => 'select', |
| 213 | 'name' => 'fields['.$key.'][post_type]', |
| 214 | 'value' => $field['post_type'], |
| 215 | 'choices' => $post_types, |
| 216 | 'multiple' => '1', |
| 217 | )); |
| 218 | ?> |
| 219 | </td> |
| 220 | </tr> |
| 221 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 222 | <td class="label"> |
| 223 | <label><?php _e("Allow Null?",'acf'); ?></label> |
| 224 | </td> |
| 225 | <td> |
| 226 | <?php |
| 227 | $this->parent->create_field(array( |
| 228 | 'type' => 'radio', |
| 229 | 'name' => 'fields['.$key.'][allow_null]', |
| 230 | 'value' => $field['allow_null'], |
| 231 | 'choices' => array( |
| 232 | '1' => 'Yes', |
| 233 | '0' => 'No', |
| 234 | ), |
| 235 | 'layout' => 'horizontal', |
| 236 | )); |
| 237 | ?> |
| 238 | </td> |
| 239 | </tr> |
| 240 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 241 | <td class="label"> |
| 242 | <label><?php _e("Select multiple values?",'acf'); ?></label> |
| 243 | </td> |
| 244 | <td> |
| 245 | <?php |
| 246 | $this->parent->create_field(array( |
| 247 | 'type' => 'radio', |
| 248 | 'name' => 'fields['.$key.'][multiple]', |
| 249 | 'value' => $field['multiple'], |
| 250 | 'choices' => array( |
| 251 | '1' => 'Yes', |
| 252 | '0' => 'No', |
| 253 | ), |
| 254 | 'layout' => 'horizontal', |
| 255 | )); |
| 256 | ?> |
| 257 | </td> |
| 258 | </tr> |
| 259 | <?php |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /*-------------------------------------------------------------------------------------- |
| 264 | * |
| 265 | * get_value_for_api |
| 266 | * |
| 267 | * @author Elliot Condon |
| 268 | * @since 3.0.0 |
| 269 | * |
| 270 | *-------------------------------------------------------------------------------------*/ |
| 271 | |
| 272 | function get_value_for_api($post_id, $field) |
| 273 | { |
| 274 | // get value |
| 275 | $value = parent::get_value($post_id, $field); |
| 276 | |
| 277 | if(!$value) |
| 278 | { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | if($value == 'null') |
| 283 | { |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | if(is_array($value)) |
| 288 | { |
| 289 | foreach($value as $k => $v) |
| 290 | { |
| 291 | $value[$k] = get_permalink($v); |
| 292 | } |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | $value = get_permalink($value); |
| 297 | } |
| 298 | |
| 299 | return $value; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | } |
| 305 | |
| 306 | ?> |