PageRenderTime 186ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/advanced-custom-fields-pro/fields/page_link.php

https://gitlab.com/Svyrydov/test-project
PHP | 697 lines | 264 code | 237 blank | 196 comment | 35 complexity | a65cb6df586341476c0066f52388def8 MD5 | raw file
  1. <?php
  2. /*
  3. * ACF Page Link Field Class
  4. *
  5. * All the logic for this field type
  6. *
  7. * @class acf_field_page_link
  8. * @extends acf_field
  9. * @package ACF
  10. * @subpackage Fields
  11. */
  12. if( ! class_exists('acf_field_page_link') ) :
  13. class acf_field_page_link extends acf_field {
  14. /*
  15. * __construct
  16. *
  17. * This function will setup the field type data
  18. *
  19. * @type function
  20. * @date 5/03/2014
  21. * @since 5.0.0
  22. *
  23. * @param n/a
  24. * @return n/a
  25. */
  26. function __construct() {
  27. // vars
  28. $this->name = 'page_link';
  29. $this->label = __("Page Link",'acf');
  30. $this->category = 'relational';
  31. $this->defaults = array(
  32. 'post_type' => array(),
  33. 'taxonomy' => array(),
  34. 'allow_null' => 0,
  35. 'multiple' => 0,
  36. );
  37. // extra
  38. add_action('wp_ajax_acf/fields/page_link/query', array($this, 'ajax_query'));
  39. add_action('wp_ajax_nopriv_acf/fields/page_link/query', array($this, 'ajax_query'));
  40. // do not delete!
  41. parent::__construct();
  42. }
  43. /*
  44. * get_choices
  45. *
  46. * This function will return an array of data formatted for use in a select2 AJAX response
  47. *
  48. * @type function
  49. * @date 15/10/2014
  50. * @since 5.0.9
  51. *
  52. * @param $options (array)
  53. * @return (array)
  54. */
  55. function get_choices( $options = array() ) {
  56. // defaults
  57. $options = acf_parse_args($options, array(
  58. 'post_id' => 0,
  59. 's' => '',
  60. 'lang' => false,
  61. 'field_key' => '',
  62. 'paged' => 1
  63. ));
  64. // vars
  65. $r = array();
  66. $args = array();
  67. // paged
  68. $args['posts_per_page'] = 20;
  69. $args['paged'] = $options['paged'];
  70. // load field
  71. $field = acf_get_field( $options['field_key'] );
  72. if( !$field ) return false;
  73. // update $args
  74. if( !empty($field['post_type']) ) {
  75. $args['post_type'] = acf_get_array( $field['post_type'] );
  76. } else {
  77. $args['post_type'] = acf_get_post_types();
  78. }
  79. // create tax queries
  80. if( !empty($field['taxonomy']) ) {
  81. // append to $args
  82. $args['tax_query'] = array();
  83. // decode terms
  84. $taxonomies = acf_decode_taxonomy_terms( $field['taxonomy'] );
  85. // now create the tax queries
  86. foreach( $taxonomies as $taxonomy => $terms ) {
  87. $args['tax_query'][] = array(
  88. 'taxonomy' => $taxonomy,
  89. 'field' => 'slug',
  90. 'terms' => $terms,
  91. );
  92. }
  93. }
  94. // search
  95. if( $options['s'] ) {
  96. $args['s'] = $options['s'];
  97. }
  98. // filters
  99. $args = apply_filters('acf/fields/page_link/query', $args, $field, $options['post_id']);
  100. $args = apply_filters('acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id'] );
  101. $args = apply_filters('acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id'] );
  102. // is search
  103. $is_search = !empty( $args['s'] );
  104. // add archives to $r
  105. if( $args['paged'] == 1 ) {
  106. $archives = array();
  107. $archives[] = array(
  108. 'id' => home_url(),
  109. 'text' => home_url()
  110. );
  111. foreach( $args['post_type'] as $post_type ) {
  112. $archive_link = get_post_type_archive_link( $post_type );
  113. if( $archive_link ) {
  114. $archives[] = array(
  115. 'id' => $archive_link,
  116. 'text' => $archive_link
  117. );
  118. }
  119. }
  120. // search
  121. if( $is_search ) {
  122. foreach( array_keys($archives) as $i ) {
  123. if( strpos( $archives[$i]['text'], $args['s'] ) === false ) {
  124. unset($archives[$i]);
  125. }
  126. }
  127. $archives = array_values($archives);
  128. }
  129. if( !empty($archives) ) {
  130. $r[] = array(
  131. 'text' => __('Archives', 'acf'),
  132. 'children' => $archives
  133. );
  134. }
  135. }
  136. // get posts grouped by post type
  137. $groups = acf_get_grouped_posts( $args );
  138. if( !empty($groups) ) {
  139. foreach( array_keys($groups) as $group_title ) {
  140. // vars
  141. $posts = acf_extract_var( $groups, $group_title );
  142. $titles = array();
  143. // data
  144. $data = array(
  145. 'text' => $group_title,
  146. 'children' => array()
  147. );
  148. foreach( array_keys($posts) as $post_id ) {
  149. // override data
  150. $posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'] );
  151. };
  152. // order by search
  153. if( $is_search ) {
  154. $posts = acf_order_by_search( $posts, $args['s'] );
  155. }
  156. // append to $data
  157. foreach( array_keys($posts) as $post_id ) {
  158. $data['children'][] = array(
  159. 'id' => $post_id,
  160. 'text' => $posts[ $post_id ]
  161. );
  162. }
  163. // append to $r
  164. $r[] = $data;
  165. }
  166. }
  167. // return
  168. return $r;
  169. }
  170. /*
  171. * ajax_query
  172. *
  173. * description
  174. *
  175. * @type function
  176. * @date 24/10/13
  177. * @since 5.0.0
  178. *
  179. * @param $post_id (int)
  180. * @return $post_id (int)
  181. */
  182. function ajax_query() {
  183. // validate
  184. if( !acf_verify_ajax() ) {
  185. die();
  186. }
  187. // get choices
  188. $choices = $this->get_choices( $_POST );
  189. // validate
  190. if( !$choices ) {
  191. die();
  192. }
  193. // return JSON
  194. echo json_encode( $choices );
  195. die();
  196. }
  197. /*
  198. * get_post_title
  199. *
  200. * This function returns the HTML for a result
  201. *
  202. * @type function
  203. * @date 1/11/2013
  204. * @since 5.0.0
  205. *
  206. * @param $post (object)
  207. * @param $field (array)
  208. * @param $post_id (int) the post_id to which this value is saved to
  209. * @return (string)
  210. */
  211. function get_post_title( $post, $field, $post_id = 0, $is_search = 0 ) {
  212. // get post_id
  213. if( !$post_id ) $post_id = acf_get_form_data('post_id');
  214. // vars
  215. $title = acf_get_post_title( $post, $is_search );
  216. // filters
  217. $title = apply_filters('acf/fields/page_link/result', $title, $post, $field, $post_id);
  218. $title = apply_filters('acf/fields/page_link/result/name=' . $field['_name'], $title, $post, $field, $post_id);
  219. $title = apply_filters('acf/fields/page_link/result/key=' . $field['key'], $title, $post, $field, $post_id);
  220. // return
  221. return $title;
  222. }
  223. /*
  224. * get_posts
  225. *
  226. * This function will return an array of posts for a given field value
  227. *
  228. * @type function
  229. * @date 13/06/2014
  230. * @since 5.0.0
  231. *
  232. * @param $value (array)
  233. * @return $value
  234. */
  235. function get_posts( $value, $field ) {
  236. // force value to array
  237. $value = acf_get_array( $value );
  238. // get selected post ID's
  239. $post__in = array();
  240. foreach( $value as $k => $v ) {
  241. if( is_numeric($v) ) {
  242. // append to $post__in
  243. $post__in[] = (int) $v;
  244. }
  245. }
  246. // bail early if no posts
  247. if( empty($post__in) ) {
  248. return $value;
  249. }
  250. // get posts
  251. $posts = acf_get_posts(array(
  252. 'post__in' => $post__in,
  253. 'post_type' => $field['post_type']
  254. ));
  255. // override value with post
  256. $return = array();
  257. // append to $return
  258. foreach( $value as $k => $v ) {
  259. if( is_numeric($v) ) {
  260. // extract first post
  261. $post = array_shift( $posts );
  262. // append
  263. if( $post ) {
  264. $return[] = $post;
  265. }
  266. } else {
  267. $return[] = $v;
  268. }
  269. }
  270. // return
  271. return $return;
  272. }
  273. /*
  274. * render_field()
  275. *
  276. * Create the HTML interface for your field
  277. *
  278. * @param $field - an array holding all the field's data
  279. *
  280. * @type action
  281. * @since 3.6
  282. * @date 23/01/13
  283. */
  284. function render_field( $field ){
  285. // Change Field into a select
  286. $field['type'] = 'select';
  287. $field['ui'] = 1;
  288. $field['ajax'] = 1;
  289. $field['choices'] = array();
  290. // populate choices if value exists
  291. if( !empty($field['value']) ) {
  292. // get posts
  293. $posts = $this->get_posts( $field['value'], $field );
  294. // set choices
  295. if( !empty($posts) ) {
  296. foreach( array_keys($posts) as $i ) {
  297. // vars
  298. $post = acf_extract_var( $posts, $i );
  299. if( is_object($post) ) {
  300. // append to choices
  301. $field['choices'][ $post->ID ] = $this->get_post_title( $post, $field );
  302. } else {
  303. // append to choices
  304. $field['choices'][ $post ] = $post;
  305. }
  306. }
  307. }
  308. }
  309. // render
  310. acf_render_field( $field );
  311. }
  312. /*
  313. * render_field_settings()
  314. *
  315. * Create extra options for your field. This is rendered when editing a field.
  316. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  317. *
  318. * @type action
  319. * @since 3.6
  320. * @date 23/01/13
  321. *
  322. * @param $field - an array holding all the field's data
  323. */
  324. function render_field_settings( $field ) {
  325. // post_type
  326. acf_render_field_setting( $field, array(
  327. 'label' => __('Filter by Post Type','acf'),
  328. 'instructions' => '',
  329. 'type' => 'select',
  330. 'name' => 'post_type',
  331. 'choices' => acf_get_pretty_post_types(),
  332. 'multiple' => 1,
  333. 'ui' => 1,
  334. 'allow_null' => 1,
  335. 'placeholder' => __("All post types",'acf'),
  336. ));
  337. // taxonomy
  338. acf_render_field_setting( $field, array(
  339. 'label' => __('Filter by Taxonomy','acf'),
  340. 'instructions' => '',
  341. 'type' => 'select',
  342. 'name' => 'taxonomy',
  343. 'choices' => acf_get_taxonomy_terms(),
  344. 'multiple' => 1,
  345. 'ui' => 1,
  346. 'allow_null' => 1,
  347. 'placeholder' => __("All taxonomies",'acf'),
  348. ));
  349. // allow_null
  350. acf_render_field_setting( $field, array(
  351. 'label' => __('Allow Null?','acf'),
  352. 'instructions' => '',
  353. 'type' => 'radio',
  354. 'name' => 'allow_null',
  355. 'choices' => array(
  356. 1 => __("Yes",'acf'),
  357. 0 => __("No",'acf'),
  358. ),
  359. 'layout' => 'horizontal',
  360. ));
  361. // multiple
  362. acf_render_field_setting( $field, array(
  363. 'label' => __('Select multiple values?','acf'),
  364. 'instructions' => '',
  365. 'type' => 'radio',
  366. 'name' => 'multiple',
  367. 'choices' => array(
  368. 1 => __("Yes",'acf'),
  369. 0 => __("No",'acf'),
  370. ),
  371. 'layout' => 'horizontal',
  372. ));
  373. }
  374. /*
  375. * format_value()
  376. *
  377. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  378. *
  379. * @type filter
  380. * @since 3.6
  381. * @date 23/01/13
  382. *
  383. * @param $value (mixed) the value which was loaded from the database
  384. * @param $post_id (mixed) the $post_id from which the value was loaded
  385. * @param $field (array) the field array holding all the field options
  386. *
  387. * @return $value (mixed) the modified value
  388. */
  389. function format_value( $value, $post_id, $field ) {
  390. // ACF4 null
  391. if( $value === 'null' ) {
  392. return false;
  393. }
  394. // bail early if no value
  395. if( empty($value) ) {
  396. return $value;
  397. }
  398. // get posts
  399. $value = $this->get_posts( $value, $field );
  400. // set choices
  401. foreach( array_keys($value) as $i ) {
  402. // vars
  403. $post = acf_extract_var( $value, $i );
  404. // convert $post to permalink
  405. if( is_object($post) ) {
  406. $post = get_permalink( $post );
  407. }
  408. // append back to $value
  409. $value[ $i ] = $post;
  410. }
  411. // convert back from array if neccessary
  412. if( !$field['multiple'] ) {
  413. $value = array_shift($value);
  414. }
  415. // return value
  416. return $value;
  417. }
  418. /*
  419. * update_value()
  420. *
  421. * This filter is appied to the $value before it is updated in the db
  422. *
  423. * @type filter
  424. * @since 3.6
  425. * @date 23/01/13
  426. *
  427. * @param $value - the value which will be saved in the database
  428. * @param $post_id - the $post_id of which the value will be saved
  429. * @param $field - the field array holding all the field options
  430. *
  431. * @return $value - the modified value
  432. */
  433. function update_value( $value, $post_id, $field ) {
  434. // validate
  435. if( empty($value) ) {
  436. return $value;
  437. }
  438. // format
  439. if( is_array($value) ) {
  440. // array
  441. foreach( $value as $k => $v ){
  442. // object?
  443. if( is_object($v) && isset($v->ID) )
  444. {
  445. $value[ $k ] = $v->ID;
  446. }
  447. }
  448. // save value as strings, so we can clearly search for them in SQL LIKE statements
  449. $value = array_map('strval', $value);
  450. } elseif( is_object($value) && isset($value->ID) ) {
  451. // object
  452. $value = $value->ID;
  453. }
  454. // return
  455. return $value;
  456. }
  457. }
  458. new acf_field_page_link();
  459. endif;
  460. ?>