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