| 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 |
* register_field_group |
| 265 |
* |
| 266 |
* @author Elliot Condon |
| 267 |
* @since 3.0.6 |
| 268 |
* |
| 269 |
*-------------------------------------------------------------------------------------*/ |
| 270 |
|
| 271 |
$GLOBALS['acf_register_field_group'] = array(); |
| 272 |
|
| 273 |
function register_field_group($array) |
| 274 |
{ |
| 275 |
// add id |
| 276 |
$array['id'] = uniqid(); |
| 277 |
$GLOBALS['acf_register_field_group'][] = $array; |
| 278 |
} |
| 279 |
|
| 280 |
function acf_register_field_group($array) |
| 281 |
{ |
| 282 |
$array = array_merge($array, $GLOBALS['acf_register_field_group']); |
| 283 |
|
| 284 |
// order field groups based on menu_order |
| 285 |
// Obtain a list of columns |
| 286 |
foreach ($array as $key => $row) { |
| 287 |
$menu_order[$key] = $row['menu_order']; |
| 288 |
} |
| 289 |
|
| 290 |
// Sort the array with menu_order ascending |
| 291 |
// Add $array as the last parameter, to sort by the common key |
| 292 |
if(isset($menu_order)) |
| 293 |
{ |
| 294 |
array_multisort($menu_order, SORT_ASC, $array); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
return $array; |
| 299 |
} |
| 300 |
add_filter('acf_register_field_group', 'acf_register_field_group'); |
| 301 |
|
| 302 |
|
| 303 |
/*-------------------------------------------------------------------------------------- |
| 304 |
* |
| 305 |
* register_options_page |
| 306 |
* |
| 307 |
* @author Elliot Condon |
| 308 |
* @since 3.0.0 |
| 309 |
* |
| 310 |
*-------------------------------------------------------------------------------------*/ |
| 311 |
|
| 312 |
$GLOBALS['acf_register_options_page'] = array(); |
| 313 |
|
| 314 |
function register_options_page($title = "") |
| 315 |
{ |
| 316 |
$GLOBALS['acf_register_options_page'][] = array( |
| 317 |
'title' => $title, |
| 318 |
'slug' => 'options-' . sanitize_title_with_dashes( $title ), |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
function acf_register_options_page($array) |
| 323 |
{ |
| 324 |
$array = array_merge($array, $GLOBALS['acf_register_options_page']); |
| 325 |
|
| 326 |
return $array; |
| 327 |
} |
| 328 |
add_filter('acf_register_options_page', 'acf_register_options_page'); |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
/*-------------------------------------------------------------------------------------- |
| 333 |
* |
| 334 |
* get_sub_field |
| 335 |
* |
| 336 |
* @author Elliot Condon |
| 337 |
* @since 1.0.3 |
| 338 |
* |
| 339 |
*-------------------------------------------------------------------------------------*/ |
| 340 |
|
| 341 |
function get_row_layout() |
| 342 |
{ |
| 343 |
|
| 344 |
// vars |
| 345 |
$field = $GLOBALS['acf_field']; |
| 346 |
$i = $GLOBALS['acf_count']; |
| 347 |
|
| 348 |
// no value |
| 349 |
if(!$field) return false; |
| 350 |
|
| 351 |
if(!isset($field[$i]['acf_fc_layout'])) return false; |
| 352 |
|
| 353 |
return $field[$i]['acf_fc_layout']; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
/*-------------------------------------------------------------------------------------- |
| 358 |
* |
| 359 |
* shorcode support |
| 360 |
* |
| 361 |
* @author Elliot Condon |
| 362 |
* @since 1.1.1 |
| 363 |
* |
| 364 |
*-------------------------------------------------------------------------------------*/ |
| 365 |
|
| 366 |
function acf_shortcode( $atts ) |
| 367 |
{ |
| 368 |
// extract attributs |
| 369 |
extract( shortcode_atts( array( |
| 370 |
'field' => "" |
| 371 |
), $atts ) ); |
| 372 |
|
| 373 |
// $field is requird |
| 374 |
if(!$field || $field == "") |
| 375 |
{ |
| 376 |
return ""; |
| 377 |
} |
| 378 |
|
| 379 |
// get value and return it |
| 380 |
$value = get_field($field); |
| 381 |
|
| 382 |
if(is_array($value)) |
| 383 |
{ |
| 384 |
$value = @implode(', ',$value); |
| 385 |
} |
| 386 |
|
| 387 |
return $value; |
| 388 |
} |
| 389 |
add_shortcode( 'acf', 'acf_shortcode' ); |
| 390 |
|
| 391 |
?> |