123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
- <?php
- if ( ! class_exists( 'acf_field_page_link' ) ) :
- class acf_field_page_link extends acf_field {
-
- function initialize() {
-
- $this->name = 'page_link';
- $this->label = __( 'Page Link', 'acf' );
- $this->category = 'relational';
- $this->defaults = array(
- 'post_type' => array(),
- 'taxonomy' => array(),
- 'allow_null' => 0,
- 'multiple' => 0,
- 'allow_archives' => 1,
- );
-
- add_action( 'wp_ajax_acf/fields/page_link/query', array( $this, 'ajax_query' ) );
- add_action( 'wp_ajax_nopriv_acf/fields/page_link/query', array( $this, 'ajax_query' ) );
- }
-
- function ajax_query() {
-
- if ( ! acf_verify_ajax() ) {
- die();
- }
-
- $options = acf_parse_args(
- $_POST,
- array(
- 'post_id' => 0,
- 's' => '',
- 'field_key' => '',
- 'paged' => 1,
- )
- );
-
- $results = array();
- $args = array();
- $s = false;
- $is_search = false;
-
- $args['posts_per_page'] = 20;
- $args['paged'] = $options['paged'];
-
- if ( $options['s'] !== '' ) {
-
- $s = wp_unslash( strval( $options['s'] ) );
-
- $args['s'] = $s;
- $is_search = true;
- }
-
- $field = acf_get_field( $options['field_key'] );
- if ( ! $field ) {
- die();
- }
-
- if ( ! empty( $field['post_type'] ) ) {
- $args['post_type'] = acf_get_array( $field['post_type'] );
- } else {
- $args['post_type'] = acf_get_post_types();
- }
-
- if ( ! empty( $field['taxonomy'] ) ) {
-
- $args['tax_query'] = array();
-
- $taxonomies = acf_decode_taxonomy_terms( $field['taxonomy'] );
-
- foreach ( $taxonomies as $taxonomy => $terms ) {
- $args['tax_query'][] = array(
- 'taxonomy' => $taxonomy,
- 'field' => 'slug',
- 'terms' => $terms,
- );
- }
- }
-
- $args = apply_filters( 'acf/fields/page_link/query', $args, $field, $options['post_id'] );
- $args = apply_filters( 'acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id'] );
- $args = apply_filters( 'acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id'] );
-
- if ( $field['allow_archives'] && $args['paged'] == 1 ) {
-
- $links = array();
- $links[] = home_url();
- foreach ( $args['post_type'] as $post_type ) {
- $links[] = get_post_type_archive_link( $post_type );
- }
- $links = array_filter( $links );
- $links = array_unique( $links );
-
- $children = array();
- foreach ( $links as $link ) {
-
- if ( $is_search && stripos( $link, $s ) === false ) {
- continue;
- }
- $children[] = array(
- 'id' => $link,
- 'text' => $link,
- );
- }
- if ( $children ) {
- $results[] = array(
- 'text' => __( 'Archives', 'acf' ),
- 'children' => $children,
- );
- }
- }
-
- $groups = acf_get_grouped_posts( $args );
-
- if ( ! empty( $groups ) ) {
- foreach ( array_keys( $groups ) as $group_title ) {
-
- $posts = acf_extract_var( $groups, $group_title );
-
- $data = array(
- 'text' => $group_title,
- 'children' => array(),
- );
-
- foreach ( array_keys( $posts ) as $post_id ) {
- $posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'], $is_search );
- }
-
- if ( $is_search && empty( $args['orderby'] ) && isset( $args['s'] ) ) {
- $posts = acf_order_by_search( $posts, $args['s'] );
- }
-
- foreach ( array_keys( $posts ) as $post_id ) {
- $data['children'][] = $this->get_post_result( $post_id, $posts[ $post_id ] );
- }
-
- $results[] = $data;
- }
- }
-
- acf_send_ajax_results(
- array(
- 'results' => $results,
- 'limit' => $args['posts_per_page'],
- )
- );
- }
-
- function get_post_result( $id, $text ) {
-
- $result = array(
- 'id' => $id,
- 'text' => $text,
- );
-
- $search = '| ' . __( 'Parent', 'acf' ) . ':';
- $pos = strpos( $text, $search );
- if ( $pos !== false ) {
- $result['description'] = substr( $text, $pos + 2 );
- $result['text'] = substr( $text, 0, $pos );
- }
-
- return $result;
- }
-
- function get_post_title( $post, $field, $post_id = 0, $is_search = 0 ) {
-
- if ( ! $post_id ) {
- $post_id = acf_get_form_data( 'post_id' );
- }
-
- $title = acf_get_post_title( $post, $is_search );
-
- $title = apply_filters( 'acf/fields/page_link/result', $title, $post, $field, $post_id );
- $title = apply_filters( 'acf/fields/page_link/result/name=' . $field['_name'], $title, $post, $field, $post_id );
- $title = apply_filters( 'acf/fields/page_link/result/key=' . $field['key'], $title, $post, $field, $post_id );
-
- return $title;
- }
-
- function get_posts( $value, $field ) {
-
- $value = acf_get_array( $value );
-
- $post__in = array();
- foreach ( $value as $k => $v ) {
- if ( is_numeric( $v ) ) {
-
- $post__in[] = (int) $v;
- }
- }
-
- if ( empty( $post__in ) ) {
- return $value;
- }
-
- $posts = acf_get_posts(
- array(
- 'post__in' => $post__in,
- 'post_type' => $field['post_type'],
- )
- );
-
- $return = array();
-
- foreach ( $value as $k => $v ) {
- if ( is_numeric( $v ) ) {
-
- $post = array_shift( $posts );
-
- if ( $post ) {
- $return[] = $post;
- }
- } else {
- $return[] = $v;
- }
- }
-
- return $return;
- }
-
- function render_field( $field ) {
-
- $field['type'] = 'select';
- $field['ui'] = 1;
- $field['ajax'] = 1;
- $field['choices'] = array();
-
- if ( ! empty( $field['value'] ) ) {
-
- $posts = $this->get_posts( $field['value'], $field );
-
- if ( ! empty( $posts ) ) {
- foreach ( array_keys( $posts ) as $i ) {
-
- $post = acf_extract_var( $posts, $i );
- if ( is_object( $post ) ) {
-
- $field['choices'][ $post->ID ] = $this->get_post_title( $post, $field );
- } else {
-
- $field['choices'][ $post ] = $post;
- }
- }
- }
- }
-
- acf_render_field( $field );
- }
-
- function render_field_settings( $field ) {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Filter by Post Type', 'acf' ),
- 'instructions' => '',
- 'type' => 'select',
- 'name' => 'post_type',
- 'choices' => acf_get_pretty_post_types(),
- 'multiple' => 1,
- 'ui' => 1,
- 'allow_null' => 1,
- 'placeholder' => __( 'All post types', 'acf' ),
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Filter by Taxonomy', 'acf' ),
- 'instructions' => '',
- 'type' => 'select',
- 'name' => 'taxonomy',
- 'choices' => acf_get_taxonomy_terms(),
- 'multiple' => 1,
- 'ui' => 1,
- 'allow_null' => 1,
- 'placeholder' => __( 'All taxonomies', 'acf' ),
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Allow Archives URLs', 'acf' ),
- 'instructions' => '',
- 'name' => 'allow_archives',
- 'type' => 'true_false',
- 'ui' => 1,
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Select multiple values?', 'acf' ),
- 'instructions' => '',
- 'name' => 'multiple',
- 'type' => 'true_false',
- 'ui' => 1,
- )
- );
- }
-
- function render_field_validation_settings( $field ) {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Allow Null?', 'acf' ),
- 'instructions' => '',
- 'name' => 'allow_null',
- 'type' => 'true_false',
- 'ui' => 1,
- )
- );
- }
-
- function format_value( $value, $post_id, $field ) {
-
- if ( $value === 'null' ) {
- return false;
- }
-
- if ( empty( $value ) ) {
- return $value;
- }
-
- $value = $this->get_posts( $value, $field );
-
- foreach ( array_keys( $value ) as $i ) {
-
- $post = acf_extract_var( $value, $i );
-
- if ( is_object( $post ) ) {
- $post = get_permalink( $post );
- }
-
- $value[ $i ] = $post;
- }
-
- if ( ! $field['multiple'] ) {
- $value = array_shift( $value );
- }
-
- return $value;
- }
-
- function update_value( $value, $post_id, $field ) {
-
- if ( empty( $value ) ) {
- return $value;
- }
-
-
-
- if ( acf_is_sequential_array( $value ) ) {
- $value = array_map( 'acf_maybe_idval', $value );
- $value = array_map( 'strval', $value );
-
- } else {
- $value = acf_maybe_idval( $value );
- }
-
- return $value;
- }
-
- public function validate_rest_value( $valid, $value, $field ) {
- return acf_get_field_type( 'post_object' )->validate_rest_value( $valid, $value, $field );
- }
-
- public function get_rest_schema( array $field ) {
- $schema = array(
- 'type' => array( 'integer', 'array', 'null' ),
- 'required' => ! empty( $field['required'] ),
- 'items' => array(
- 'type' => array( 'integer' ),
- ),
- );
- if ( empty( $field['allow_null'] ) ) {
- $schema['minItems'] = 1;
- }
- if ( ! empty( $field['allow_archives'] ) ) {
- $schema['type'][] = 'string';
- $schema['items']['type'][] = 'string';
- }
- if ( empty( $field['multiple'] ) ) {
- $schema['maxItems'] = 1;
- }
- return $schema;
- }
-
- public function get_rest_links( $value, $post_id, array $field ) {
- $links = array();
- if ( empty( $value ) ) {
- return $links;
- }
- foreach ( (array) $value as $object_id ) {
- if ( ! $post_type = get_post_type( $object_id ) or ! $post_type = get_post_type_object( $post_type ) ) {
- continue;
- }
- $rest_base = acf_get_object_type_rest_base( $post_type );
- $links[] = array(
- 'rel' => $post_type->name === 'attachment' ? 'acf:attachment' : 'acf:post',
- 'href' => rest_url( sprintf( '/wp/v2/%s/%s', $rest_base, $object_id ) ),
- 'embeddable' => true,
- );
- }
- return $links;
- }
-
- public function format_value_for_rest( $value, $post_id, array $field ) {
- return acf_format_numerics( $value );
- }
- }
-
- acf_register_field_type( 'acf_field_page_link' );
- endif;
|