PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / addons / theme-builder / ajax / posts.php

posts.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.1, at addons/theme-builder/ajax/posts.php

108 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocksThemeBuilder\Ajax;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use ABlocks\Classes\AbstractAjaxHandler;
10
11 class Posts extends AbstractAjaxHandler {
12 protected $namespace = ABLOCKS_PLUGIN_SLUG . '_theme_builder';
13 public function __construct() {
14 $this->actions = array(
15 'search_anything' => array(
16 'callback' => array( $this, 'search_anything' ),
17 'fields' => array(
18 'keyword' => 'string'
19 )
20 ),
21 );
22 }
23 public function search_anything() {
24 $search_string = isset( $_POST['keyword'] ) ? sanitize_text_field( $_POST['keyword'] ) : '';
25 $result = array();
26
27 $args = array(
28 'public' => true,
29 '_builtin' => false,
30 );
31
32 $post_types = get_post_types( $args, 'names', 'and' );
33 unset( $post_types['ablocks_tb'] );
34 $post_types['Posts'] = 'post';
35 $post_types['Pages'] = 'page';
36
37 foreach ( $post_types as $key => $post_type ) {
38 $data = array();
39 $query = new \WP_Query(array(
40 's' => $search_string,
41 'post_type' => $post_type,
42 'posts_per_page' => -1,
43 ));
44
45 if ( $query->have_posts() ) {
46 while ( $query->have_posts() ) {
47 $query->the_post();
48 $title = get_the_title();
49 $title .= ( 0 != $query->post->post_parent ) ? ' (' . get_the_title( $query->post->post_parent ) . ')' : '';
50 $id = get_the_id();
51 $data[] = [
52 'label' => $title,
53 'value' => 'post-' . $id,
54 ];
55 }
56 }
57
58 if ( ! empty( $data ) ) {
59 $result[] = [
60 'label' => $key,
61 'options' => $data,
62 ];
63 }
64 }//end foreach
65
66 wp_reset_postdata();
67
68 // Search in taxonomies
69 $taxonomies = get_taxonomies( array( 'public' => true ), 'objects', 'and' );
70 foreach ( $taxonomies as $taxonomy ) {
71 $terms = get_terms(array(
72 'taxonomy' => $taxonomy->name,
73 'orderby' => 'count',
74 'hide_empty' => 0,
75 'name__like' => $search_string,
76 ));
77
78 $data = array();
79 $label = ucwords( $taxonomy->label );
80
81 if ( ! empty( $terms ) ) {
82 foreach ( $terms as $term ) {
83 $term_taxonomy_name = ucfirst( str_replace( '_', ' ', $taxonomy->name ) );
84
85 $data[] = [
86 'label' => $term->name . ' archive page',
87 'value' => 'tax-' . $term->term_id,
88 ];
89
90 $data[] = [
91 'label' => 'All singulars from ' . $term->name,
92 'value' => 'tax-' . $term->term_id . '-single-' . $taxonomy->name,
93 ];
94 }
95 }
96
97 if ( ! empty( $data ) ) {
98 $result[] = [
99 'label' => $label,
100 'options' => $data,
101 ];
102 }
103 }//end foreach
104
105 wp_send_json_success( $result );
106 }
107 }
108