/core/fields/post_object.php

  1. <?php 
  2.  
  3. class acf_field_post_object extends acf_field 
  4. /** 
  5. * __construct 
  6. * 
  7. * Set name / label needed for actions / filters 
  8. * 
  9. * @since 3.6 
  10. * @date 23/01/13 
  11. */ 
  12.  
  13. function __construct() 
  14. // vars 
  15. $this->name = 'post_object'; 
  16. $this->label = __("Post Object", 'acf'); 
  17. $this->category = __("Relational", 'acf'); 
  18. $this->defaults = array( 
  19. 'post_type' => array('all'),  
  20. 'taxonomy' => array('all'),  
  21. 'multiple' => 0,  
  22. 'allow_null' => 0,  
  23. ); 
  24.  
  25.  
  26. // do not delete! 
  27. parent::__construct(); 
  28.  
  29.  
  30.  
  31. /** 
  32. * load_field() 
  33. *  
  34. * This filter is appied to the $field after it is loaded from the database 
  35. *  
  36. * @type filter 
  37. * @since 3.6 
  38. * @date 23/01/13 
  39. *  
  40. * @param $field - the field array holding all the field options 
  41. *  
  42. * @return $field - the field array holding all the field options 
  43. */ 
  44.  
  45. function load_field( $field ) 
  46. // validate post_type 
  47. if( !$field['post_type'] || !is_array($field['post_type']) || in_array('', $field['post_type']) ) 
  48. $field['post_type'] = array( 'all' ); 
  49.  
  50.  
  51. // validate taxonomy 
  52. if( !$field['taxonomy'] || !is_array($field['taxonomy']) || in_array('', $field['taxonomy']) ) 
  53. $field['taxonomy'] = array( 'all' ); 
  54.  
  55.  
  56. // return 
  57. return $field; 
  58.  
  59.  
  60. /** 
  61. * create_field() 
  62. * 
  63. * Create the HTML interface for your field 
  64. * 
  65. * @param $field - an array holding all the field's data 
  66. * 
  67. * @type action 
  68. * @since 3.6 
  69. * @date 23/01/13 
  70. */ 
  71.  
  72. function create_field( $field ) 
  73. // global 
  74. global $post; 
  75.  
  76.  
  77. // vars 
  78. $args = array( 
  79. 'numberposts' => -1,  
  80. 'post_type' => null,  
  81. 'orderby' => 'title',  
  82. 'order' => 'ASC',  
  83. 'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'),  
  84. 'suppress_filters' => false,  
  85. ); 
  86.  
  87.  
  88. // load all post types by default 
  89. if( in_array('all', $field['post_type']) ) 
  90. $field['post_type'] = apply_filters('acf/get_post_types', array()); 
  91.  
  92.  
  93. // create tax queries 
  94. if( ! in_array('all', $field['taxonomy']) ) 
  95. // vars 
  96. $taxonomies = array(); 
  97. $args['tax_query'] = array(); 
  98.  
  99. foreach( $field['taxonomy'] as $v ) 
  100.  
  101. // find term (find taxonomy!) 
  102. // $term = array( 0 => $taxonomy, 1 => $term_id ) 
  103. $term = explode(':', $v);  
  104.  
  105.  
  106. // validate 
  107. if( !is_array($term) || !isset($term[1]) ) 
  108. continue; 
  109.  
  110.  
  111. // add to tax array 
  112. $taxonomies[ $term[0] ][] = $term[1]; 
  113.  
  114.  
  115.  
  116. // now create the tax queries 
  117. foreach( $taxonomies as $k => $v ) 
  118. $args['tax_query'][] = array( 
  119. 'taxonomy' => $k,  
  120. 'field' => 'id',  
  121. 'terms' => $v,  
  122. ); 
  123.  
  124.  
  125. // Change Field into a select 
  126. $field['type'] = 'select'; 
  127. $field['choices'] = array(); 
  128.  
  129.  
  130. foreach( $field['post_type'] as $post_type ) 
  131. // set post_type 
  132. $args['post_type'] = $post_type; 
  133.  
  134.  
  135. // set order 
  136. $get_pages = false; 
  137. if( is_post_type_hierarchical($post_type) && !isset($args['tax_query']) ) 
  138. $args['sort_column'] = 'menu_order, post_title'; 
  139. $args['sort_order'] = 'ASC'; 
  140.  
  141. $get_pages = true; 
  142.  
  143.  
  144. // filters 
  145. $args = apply_filters('acf/fields/post_object/query', $args, $field, $post); 
  146. $args = apply_filters('acf/fields/post_object/query/name=' . $field['_name'], $args, $field, $post ); 
  147. $args = apply_filters('acf/fields/post_object/query/key=' . $field['key'], $args, $field, $post ); 
  148.  
  149.  
  150. if( $get_pages ) 
  151. $posts = get_pages( $args ); 
  152. else 
  153. $posts = get_posts( $args ); 
  154.  
  155.  
  156. if($posts) { 
  157.  
  158. foreach( $posts as $p ) { 
  159.  
  160. // title 
  161. $title = get_the_title( $p->ID ); 
  162.  
  163.  
  164. // empty 
  165. if( $title === '' ) { 
  166.  
  167. $title = __('(no title)', 'acf'); 
  168.  
  169.  
  170.  
  171. // ancestors 
  172. if( $p->post_type != 'attachment' ) { 
  173.  
  174. $ancestors = get_ancestors( $p->ID, $p->post_type ); 
  175.  
  176. $title = str_repeat('- ', count($ancestors)) . $title; 
  177.  
  178.  
  179.  
  180. // status 
  181. if( get_post_status( $p->ID ) != "publish" ) { 
  182.  
  183. $title .= ' (' . get_post_status( $p->ID ) . ')'; 
  184.  
  185.  
  186.  
  187. // WPML 
  188. if( defined('ICL_LANGUAGE_CODE') ) { 
  189.  
  190. $title .= ' (' . ICL_LANGUAGE_CODE . ')'; 
  191.  
  192.  
  193.  
  194. // filters 
  195. $title = apply_filters('acf/fields/post_object/result', $title, $p, $field, $post); 
  196. $title = apply_filters('acf/fields/post_object/result/name=' . $field['_name'] , $title, $p, $field, $post); 
  197. $title = apply_filters('acf/fields/post_object/result/key=' . $field['key'], $title, $p, $field, $post); 
  198.  
  199.  
  200. // add to choices 
  201. if( count($field['post_type']) == 1 ) 
  202. $field['choices'][ $p->ID ] = $title; 
  203. else 
  204. // group by post type 
  205. $post_type_object = get_post_type_object( $p->post_type ); 
  206. $post_type_name = $post_type_object->labels->name; 
  207.  
  208. $field['choices'][ $post_type_name ][ $p->ID ] = $title; 
  209.  
  210.  
  211. // foreach( $posts as $post ) 
  212. // if($posts) 
  213. // foreach( $field['post_type'] as $post_type ) 
  214.  
  215.  
  216. // create field 
  217. do_action('acf/create_field', $field ); 
  218.  
  219.  
  220. /** 
  221. * create_options() 
  222. * 
  223. * Create extra options for your field. This is rendered when editing a field. 
  224. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  225. * 
  226. * @type action 
  227. * @since 3.6 
  228. * @date 23/01/13 
  229. * 
  230. * @param $field - an array holding all the field's data 
  231. */ 
  232.  
  233. function create_options( $field ) 
  234. // vars 
  235. $key = $field['name']; 
  236.  
  237. ?> 
  238. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  239. <td class="label"> 
  240. <label for=""><?php _e("Post Type", 'acf'); ?></label> 
  241. </td> 
  242. <td> 
  243. <?php  
  244.  
  245. $choices = array( 
  246. 'all' => __("All", 'acf') 
  247. ); 
  248. $choices = apply_filters('acf/get_post_types', $choices); 
  249.  
  250.  
  251. do_action('acf/create_field', array( 
  252. 'type' => 'select',  
  253. 'name' => 'fields['.$key.'][post_type]',  
  254. 'value' => $field['post_type'],  
  255. 'choices' => $choices,  
  256. 'multiple' => 1,  
  257. )); 
  258.  
  259. ?> 
  260. </td> 
  261. </tr> 
  262. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  263. <td class="label"> 
  264. <label><?php _e("Filter from Taxonomy", 'acf'); ?></label> 
  265. </td> 
  266. <td> 
  267. <?php  
  268. $choices = array( 
  269. '' => array( 
  270. 'all' => __("All", 'acf') 
  271. ); 
  272. $simple_value = false; 
  273. $choices = apply_filters('acf/get_taxonomies_for_select', $choices, $simple_value); 
  274.  
  275. do_action('acf/create_field', array( 
  276. 'type' => 'select',  
  277. 'name' => 'fields['.$key.'][taxonomy]',  
  278. 'value' => $field['taxonomy'],  
  279. 'choices' => $choices,  
  280. 'multiple' => 1,  
  281. )); 
  282.  
  283. ?> 
  284. </td> 
  285. </tr> 
  286. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  287. <td class="label"> 
  288. <label><?php _e("Allow Null?", 'acf'); ?></label> 
  289. </td> 
  290. <td> 
  291. <?php 
  292.  
  293. do_action('acf/create_field', array( 
  294. 'type' => 'radio',  
  295. 'name' => 'fields['.$key.'][allow_null]',  
  296. 'value' => $field['allow_null'],  
  297. 'choices' => array( 
  298. 1 => __("Yes", 'acf'),  
  299. 0 => __("No", 'acf'),  
  300. ),  
  301. 'layout' => 'horizontal',  
  302. )); 
  303.  
  304. ?> 
  305. </td> 
  306. </tr> 
  307. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  308. <td class="label"> 
  309. <label><?php _e("Select multiple values?", 'acf'); ?></label> 
  310. </td> 
  311. <td> 
  312. <?php 
  313.  
  314. do_action('acf/create_field', array( 
  315. 'type' => 'radio',  
  316. 'name' => 'fields['.$key.'][multiple]',  
  317. 'value' => $field['multiple'],  
  318. 'choices' => array( 
  319. 1 => __("Yes", 'acf'),  
  320. 0 => __("No", 'acf'),  
  321. ),  
  322. 'layout' => 'horizontal',  
  323. )); 
  324.  
  325. ?> 
  326. </td> 
  327. </tr> 
  328. <?php 
  329.  
  330.  
  331.  
  332. /** 
  333. * format_value() 
  334. * 
  335. * This filter is appied to the $value after it is loaded from the db and before it is passed to the create_field action 
  336. * 
  337. * @type filter 
  338. * @since 3.6 
  339. * @date 23/01/13 
  340. * 
  341. * @param $value - the value which was loaded from the database 
  342. * @param $post_id - the $post_id from which the value was loaded 
  343. * @param $field - the field array holding all the field options 
  344. * 
  345. * @return $value - the modified value 
  346. */ 
  347.  
  348. function format_value( $value, $post_id, $field ) 
  349. // empty
  350. if( !empty($value) ) 
  351. // convert to integers 
  352. if( is_array($value) ) 
  353. $value = array_map('intval', $value); 
  354. else 
  355. $value = intval($value); 
  356.  
  357.  
  358. // return value 
  359. return $value;  
  360.  
  361.  
  362. /** 
  363. * format_value_for_api() 
  364. * 
  365. * This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field 
  366. * 
  367. * @type filter 
  368. * @since 3.6 
  369. * @date 23/01/13 
  370. * 
  371. * @param $value - the value which was loaded from the database 
  372. * @param $post_id - the $post_id from which the value was loaded 
  373. * @param $field - the field array holding all the field options 
  374. * 
  375. * @return $value - the modified value 
  376. */ 
  377.  
  378. function format_value_for_api( $value, $post_id, $field ) 
  379. // no value? 
  380. if( !$value ) 
  381. return false; 
  382.  
  383.  
  384. // null? 
  385. if( $value == 'null' ) 
  386. return false; 
  387.  
  388.  
  389. // multiple / single 
  390. if( is_array($value) ) 
  391. // find posts (DISTINCT POSTS) 
  392. $posts = get_posts(array( 
  393. 'numberposts' => -1,  
  394. 'post__in' => $value,  
  395. 'post_type' => apply_filters('acf/get_post_types', array()),  
  396. 'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'),  
  397. )); 
  398.  
  399.  
  400. $ordered_posts = array(); 
  401. foreach( $posts as $post ) 
  402. // create array to hold value data 
  403. $ordered_posts[ $post->ID ] = $post; 
  404.  
  405.  
  406. // override value array with attachments 
  407. foreach( $value as $k => $v) 
  408. // check that post exists (my have been trashed) 
  409. if( !isset($ordered_posts[ $v ]) ) 
  410. unset( $value[ $k ] ); 
  411. else 
  412. $value[ $k ] = $ordered_posts[ $v ]; 
  413.  
  414. else 
  415. $value = get_post($value); 
  416.  
  417.  
  418. // return the value 
  419. return $value; 
  420.  
  421.  
  422. /** 
  423. * update_value() 
  424. * 
  425. * This filter is appied to the $value before it is updated in the db 
  426. * 
  427. * @type filter 
  428. * @since 3.6 
  429. * @date 23/01/13 
  430. * 
  431. * @param $value - the value which will be saved in the database 
  432. * @param $post_id - the $post_id of which the value will be saved 
  433. * @param $field - the field array holding all the field options 
  434. * 
  435. * @return $value - the modified value 
  436. */ 
  437.  
  438. function update_value( $value, $post_id, $field ) 
  439. // validate 
  440. if( empty($value) ) 
  441. return $value; 
  442.  
  443.  
  444. if( is_object($value) && isset($value->ID) ) 
  445. // object 
  446. $value = $value->ID; 
  447.  
  448. elseif( is_array($value) ) 
  449. // array 
  450. foreach( $value as $k => $v ) { 
  451.  
  452. // object
  453. if( is_object($v) && isset($v->ID) ) 
  454. $value[ $k ] = $v->ID; 
  455.  
  456. // save value as strings, so we can clearly search for them in SQL LIKE statements 
  457. $value = array_map('strval', $value); 
  458.  
  459.  
  460. return $value; 
  461.  
  462.  
  463. new acf_field_post_object(); 
  464.  
  465. ?> 
.