api.php
| 1 | <?php |
| 2 | |
| 3 | // set some globals |
| 4 | reset_the_repeater_field(); |
| 5 | |
| 6 | |
| 7 | /*-------------------------------------------------------------------------------------- |
| 8 | * |
| 9 | * get_fields |
| 10 | * |
| 11 | * @author Elliot Condon |
| 12 | * @since 1.0.3 |
| 13 | * |
| 14 | *-------------------------------------------------------------------------------------*/ |
| 15 | |
| 16 | function get_fields($post_id = false) |
| 17 | { |
| 18 | // vars |
| 19 | global $post; |
| 20 | |
| 21 | if(!$post_id) |
| 22 | { |
| 23 | $post_id = $post->ID; |
| 24 | } |
| 25 | elseif($post_id == "options") |
| 26 | { |
| 27 | $post_id = 999999999; |
| 28 | } |
| 29 | |
| 30 | // default |
| 31 | $value = array(); |
| 32 | |
| 33 | $keys = get_post_custom_keys($post_id); |
| 34 | |
| 35 | if($keys) |
| 36 | { |
| 37 | foreach($keys as $key) |
| 38 | { |
| 39 | if(substr($key, 0, 1) != "_") |
| 40 | { |
| 41 | $value[$key] = get_field($key, $post_id); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // no value |
| 47 | if(empty($value)) |
| 48 | { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | return $value; |
| 53 | |
| 54 | } |
| 55 | |
| 56 | |
| 57 | |
| 58 | /*-------------------------------------------------------------------------------------- |
| 59 | * |
| 60 | * get_field |
| 61 | * |
| 62 | * @author Elliot Condon |
| 63 | * @since 1.0.3 |
| 64 | * |
| 65 | *-------------------------------------------------------------------------------------*/ |
| 66 | |
| 67 | function get_field($field_name, $post_id = false) |
| 68 | { |
| 69 | global $post, $acf; |
| 70 | |
| 71 | if(!$post_id) |
| 72 | { |
| 73 | $post_id = $post->ID; |
| 74 | } |
| 75 | elseif($post_id == "options") |
| 76 | { |
| 77 | $post_id = 999999999; |
| 78 | } |
| 79 | |
| 80 | // default |
| 81 | $value = ""; |
| 82 | |
| 83 | // get value |
| 84 | $field_key = get_post_meta($post_id, '_' . $field_name, true); |
| 85 | |
| 86 | if($field_key != "") |
| 87 | { |
| 88 | // we can load the field properly! |
| 89 | $field = $acf->get_acf_field($field_key); |
| 90 | $value = $acf->get_value_for_api($post_id, $field); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | // just load the text version |
| 95 | $value = get_post_meta($post_id, $field_name, true); |
| 96 | } |
| 97 | |
| 98 | // no value? |
| 99 | if($value == "") $value = false; |
| 100 | |
| 101 | return $value; |
| 102 | |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /*-------------------------------------------------------------------------------------- |
| 107 | * |
| 108 | * the_field |
| 109 | * |
| 110 | * @author Elliot Condon |
| 111 | * @since 1.0.3 |
| 112 | * |
| 113 | *-------------------------------------------------------------------------------------*/ |
| 114 | |
| 115 | function the_field($field_name, $post_id = false) |
| 116 | { |
| 117 | $value = get_field($field_name, $post_id); |
| 118 | |
| 119 | if(is_array($value)) |
| 120 | { |
| 121 | $value = @implode(', ',$value); |
| 122 | } |
| 123 | |
| 124 | echo $value; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /*-------------------------------------------------------------------------------------- |
| 129 | * |
| 130 | * the_repeater_field |
| 131 | * |
| 132 | * @author Elliot Condon |
| 133 | * @since 1.0.3 |
| 134 | * |
| 135 | *-------------------------------------------------------------------------------------*/ |
| 136 | |
| 137 | function the_repeater_field($field_name, $post_id = false) |
| 138 | { |
| 139 | |
| 140 | // if no field, create field + reset count |
| 141 | if(!$GLOBALS['acf_field']) |
| 142 | { |
| 143 | reset_the_repeater_field(); |
| 144 | $GLOBALS['acf_field'] = get_field($field_name, $post_id); |
| 145 | } |
| 146 | |
| 147 | // increase order_no |
| 148 | $GLOBALS['acf_count']++; |
| 149 | |
| 150 | // vars |
| 151 | $field = $GLOBALS['acf_field']; |
| 152 | $i = $GLOBALS['acf_count']; |
| 153 | |
| 154 | if(isset($field[$i])) |
| 155 | { |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | // no row, reset the global values |
| 160 | reset_the_repeater_field(); |
| 161 | return false; |
| 162 | |
| 163 | } |
| 164 | |
| 165 | function the_flexible_field($field_name, $post_id = false) |
| 166 | { |
| 167 | return the_repeater_field($field_name, $post_id); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /*-------------------------------------------------------------------------------------- |
| 172 | * |
| 173 | * reset_the_repeater_field |
| 174 | * |
| 175 | * @author Elliot Condon |
| 176 | * @since 1.0.3 |
| 177 | * |
| 178 | *-------------------------------------------------------------------------------------*/ |
| 179 | |
| 180 | function reset_the_repeater_field() |
| 181 | { |
| 182 | $GLOBALS['acf_field'] = false; |
| 183 | $GLOBALS['acf_count'] = -1; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /*-------------------------------------------------------------------------------------- |
| 188 | * |
| 189 | * get_sub_field |
| 190 | * |
| 191 | * @author Elliot Condon |
| 192 | * @since 1.0.3 |
| 193 | * |
| 194 | *-------------------------------------------------------------------------------------*/ |
| 195 | |
| 196 | function get_sub_field($field_name) |
| 197 | { |
| 198 | |
| 199 | // vars |
| 200 | $field = $GLOBALS['acf_field']; |
| 201 | $i = $GLOBALS['acf_count']; |
| 202 | |
| 203 | // no value |
| 204 | if(!$field) return false; |
| 205 | |
| 206 | if(!isset($field[$i][$field_name])) return false; |
| 207 | |
| 208 | return $field[$i][$field_name]; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /*-------------------------------------------------------------------------------------- |
| 213 | * |
| 214 | * the_sub_field |
| 215 | * |
| 216 | * @author Elliot Condon |
| 217 | * @since 1.0.3 |
| 218 | * |
| 219 | *-------------------------------------------------------------------------------------*/ |
| 220 | |
| 221 | function the_sub_field($field_name, $field = false) |
| 222 | { |
| 223 | $value = get_sub_field($field_name, $field); |
| 224 | |
| 225 | if(is_array($value)) |
| 226 | { |
| 227 | $value = implode(', ',$value); |
| 228 | } |
| 229 | |
| 230 | echo $value; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /*-------------------------------------------------------------------------------------- |
| 235 | * |
| 236 | * register_field |
| 237 | * |
| 238 | * @author Elliot Condon |
| 239 | * @since 3.0.0 |
| 240 | * |
| 241 | *-------------------------------------------------------------------------------------*/ |
| 242 | |
| 243 | $GLOBALS['acf_register_field'] = array(); |
| 244 | |
| 245 | function register_field($class = "", $url = "") |
| 246 | { |
| 247 | $GLOBALS['acf_register_field'][] = array( |
| 248 | 'url' => $url, |
| 249 | 'class' => $class, |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | function acf_register_field($array) |
| 254 | { |
| 255 | $array = array_merge($array, $GLOBALS['acf_register_field']); |
| 256 | |
| 257 | return $array; |
| 258 | } |
| 259 | add_filter('acf_register_field', 'acf_register_field'); |
| 260 | |
| 261 | |
| 262 | |
| 263 | /*-------------------------------------------------------------------------------------- |
| 264 | * |
| 265 | * register_options_page |
| 266 | * |
| 267 | * @author Elliot Condon |
| 268 | * @since 3.0.0 |
| 269 | * |
| 270 | *-------------------------------------------------------------------------------------*/ |
| 271 | |
| 272 | $GLOBALS['acf_register_options_page'] = array(); |
| 273 | |
| 274 | function register_options_page($title = "") |
| 275 | { |
| 276 | $GLOBALS['acf_register_options_page'][] = array( |
| 277 | 'title' => $title, |
| 278 | 'slug' => 'options-' . sanitize_title_with_dashes( $title ), |
| 279 | ); |
| 280 | } |
| 281 | |
| 282 | function acf_register_options_page($array) |
| 283 | { |
| 284 | $array = array_merge($array, $GLOBALS['acf_register_options_page']); |
| 285 | |
| 286 | return $array; |
| 287 | } |
| 288 | add_filter('acf_register_options_page', 'acf_register_options_page'); |
| 289 | |
| 290 | |
| 291 | |
| 292 | /*-------------------------------------------------------------------------------------- |
| 293 | * |
| 294 | * get_sub_field |
| 295 | * |
| 296 | * @author Elliot Condon |
| 297 | * @since 1.0.3 |
| 298 | * |
| 299 | *-------------------------------------------------------------------------------------*/ |
| 300 | |
| 301 | function get_row_layout() |
| 302 | { |
| 303 | |
| 304 | // vars |
| 305 | $field = $GLOBALS['acf_field']; |
| 306 | $i = $GLOBALS['acf_count']; |
| 307 | |
| 308 | // no value |
| 309 | if(!$field) return false; |
| 310 | |
| 311 | if(!isset($field[$i]['acf_fc_layout'])) return false; |
| 312 | |
| 313 | return $field[$i]['acf_fc_layout']; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | ?> |