acf_field_page_link::ajax_query │ public │ ACF 5.0.0
Returns AJAX results for the Page Link field.
Метод класса: acf_field_page_link{}
Хуки из метода
Возвращает
null. Ничего (null).
Использование
$acf_field_page_link = new acf_field_page_link(); $acf_field_page_link->ajax_query();
Список изменений
| С версии 5.0.0 | Введена. |
Код acf_field_page_link::ajax_query() acf field page link::ajax query ACF 6.8.8
public function ajax_query() {
$nonce = acf_request_arg( 'nonce', '' );
$key = acf_request_arg( 'field_key', '' );
$conditional_logic = (bool) acf_request_arg( 'conditional_logic', false );
if ( $conditional_logic ) {
if ( ! acf_current_user_can_admin() ) {
die();
}
// Use the standard ACF admin nonce.
$nonce = '';
$key = '';
}
if ( ! acf_verify_ajax( $nonce, $key, ! $conditional_logic, 'page_link' ) ) {
die();
}
// defaults
$options = acf_parse_args(
$_POST,
array(
'post_id' => 0,
's' => '',
'field_key' => '',
'paged' => 1,
'include' => '',
)
);
// vars
$results = array();
$args = array();
$s = false;
$is_search = false;
// paged
$args['posts_per_page'] = 20;
$args['paged'] = (int) $options['paged'];
// search
if ( $options['s'] !== '' ) {
// strip slashes (search may be integer)
$s = wp_unslash( strval( $options['s'] ) );
// update vars
$args['s'] = $s;
$is_search = true;
}
// load field
$field = acf_get_field( $options['field_key'] );
if ( ! $field ) {
die();
}
// update $args
if ( ! empty( $field['post_type'] ) ) {
$args['post_type'] = acf_get_array( $field['post_type'] );
} else {
$args['post_type'] = acf_get_post_types();
}
// Post status - use field config only, don't accept from user input.
if ( ! empty( $field['post_status'] ) ) {
$args['post_status'] = acf_get_array( $field['post_status'] );
}
// create tax queries
if ( ! empty( $field['taxonomy'] ) ) {
// append to $args
$args['tax_query'] = array();
// decode terms
$taxonomies = acf_decode_taxonomy_terms( $field['taxonomy'] );
// now create the tax queries
foreach ( $taxonomies as $taxonomy => $terms ) {
$args['tax_query'][] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms,
);
}
}
if ( ! empty( $options['include'] ) ) {
$args['include'] = (int) $options['include'];
}
$args['perm'] = 'readable';
$args = acf_ensure_perm_readable_post_status( $args );
// filters
$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'] );
// Re-normalize in case a filter reset `post_status` to 'any' while leaving `perm=readable`.
$args = acf_ensure_perm_readable_post_status( $args );
// add archives to $results
if ( $field['allow_archives'] && $args['paged'] == 1 ) {
// Generate unique list of URLs.
$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 );
// Convert list into choices.
$children = array();
foreach ( $links as $link ) {
// Ignore if search does not match.
if ( $is_search && stripos( $link, $s ) === false ) {
continue;
}
$children[] = array(
'id' => $link,
'text' => $link,
);
}
if ( $children ) {
$results[] = array(
'text' => __( 'Archives', 'acf' ),
'children' => $children,
);
}
}
// If there is an include set, we will unset search to avoid attempting to further filter by the search term.
if ( isset( $args['include'] ) ) {
unset( $args['s'] );
}
// get posts grouped by post type
$groups = acf_get_grouped_posts( $args );
// loop
if ( ! empty( $groups ) ) {
foreach ( array_keys( $groups ) as $group_title ) {
// vars
$posts = acf_extract_var( $groups, $group_title );
// data
$data = array(
'text' => $group_title,
'children' => array(),
);
// convert post objects to post titles
foreach ( array_keys( $posts ) as $post_id ) {
$posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'], $is_search );
}
// order posts by search
if ( $is_search && empty( $args['orderby'] ) && isset( $args['s'] ) ) {
$posts = acf_order_by_search( $posts, $args['s'] );
}
// append to $data
foreach ( array_keys( $posts ) as $post_id ) {
$data['children'][] = $this->get_post_result( $post_id, $posts[ $post_id ] );
}
// append to $results
$results[] = $data;
}
}
// return
acf_send_ajax_results(
array(
'results' => $results,
'limit' => $args['posts_per_page'],
)
);
}