PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.0
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.0, at addons/theme-builder/ajax/posts.php

109 lines 2.6 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 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
25 $search_string = isset( $_POST['keyword'] ) ? sanitize_text_field( $_POST['keyword'] ) : '';
26 $result = array();
27
28 $args = array(
29 'public' => true,
30 '_builtin' => false,
31 );
32
33 $post_types = get_post_types( $args, 'names', 'and' );
34 unset( $post_types['ablocks_tb'] );
35 $post_types['Posts'] = 'post';
36 $post_types['Pages'] = 'page';
37
38 foreach ( $post_types as $key => $post_type ) {
39 $data = array();
40 $query = new \WP_Query(array(
41 's' => $search_string,
42 'post_type' => $post_type,
43 'posts_per_page' => -1,
44 ));
45
46 if ( $query->have_posts() ) {
47 while ( $query->have_posts() ) {
48 $query->the_post();
49 $title = get_the_title();
50 $title .= ( 0 != $query->post->post_parent ) ? ' (' . get_the_title( $query->post->post_parent ) . ')' : '';
51 $id = get_the_id();
52 $data[] = [
53 'label' => $title,
54 'value' => 'post-' . $id,
55 ];
56 }
57 }
58
59 if ( ! empty( $data ) ) {
60 $result[] = [
61 'label' => $key,
62 'options' => $data,
63 ];
64 }
65 }//end foreach
66
67 wp_reset_postdata();
68
69 // Search in taxonomies
70 $taxonomies = get_taxonomies( array( 'public' => true ), 'objects', 'and' );
71 foreach ( $taxonomies as $taxonomy ) {
72 $terms = get_terms(array(
73 'taxonomy' => $taxonomy->name,
74 'orderby' => 'count',
75 'hide_empty' => 0,
76 'name__like' => $search_string,
77 ));
78
79 $data = array();
80 $label = ucwords( $taxonomy->label );
81
82 if ( ! empty( $terms ) ) {
83 foreach ( $terms as $term ) {
84 $term_taxonomy_name = ucfirst( str_replace( '_', ' ', $taxonomy->name ) );
85
86 $data[] = [
87 'label' => $term->name . ' archive page',
88 'value' => 'tax-' . $term->term_id,
89 ];
90
91 $data[] = [
92 'label' => 'All singulars from ' . $term->name,
93 'value' => 'tax-' . $term->term_id . '-single-' . $taxonomy->name,
94 ];
95 }
96 }
97
98 if ( ! empty( $data ) ) {
99 $result[] = [
100 'label' => $label,
101 'options' => $data,
102 ];
103 }
104 }//end foreach
105
106 wp_send_json_success( $result );
107 }
108 }
109