init(); } public function init() { $this->register_category(); // get post/page titles add_action( 'wp_ajax_aiea_hf_get_posts_by_query', array( $this, 'get_posts_by_query' ) ); // Elementor category require_once ( AIEA_DIR . 'core/header-footer/cpt.php' ); // Front-end require_once ( AIEA_DIR . 'core/header-footer/front-end.php' ); } function register_category() { add_action( 'elementor/elements/categories_registered', [ $this, 'create_header_footer_category' ], 5 ); } public function create_header_footer_category( $elements_manager ) { $elements_manager->add_category( 'ai-hf-elements', array( 'title' => esc_html__( 'AI Header Footer', 'ai-addons' ) ), 1 ); } function get_posts_by_query() { check_ajax_referer( 'ai-hf-get-posts-by-query', 'nonce' ); $search_string = isset( $_POST['q'] ) ? sanitize_text_field( $_POST['q'] ) : ''; $data = array(); $result = array(); $args = array( 'public' => true, '_builtin' => false, ); $output = 'names'; // names or objects, note names is the default. $operator = 'and'; // also supports 'or'. $post_types = get_post_types( $args, $output, $operator ); unset( $post_types['ai-hf'] ); //Exclude EHF templates. $post_types['Posts'] = 'post'; $post_types['Pages'] = 'page'; foreach ( $post_types as $key => $post_type ) { $data = array(); add_filter( 'posts_search', array( $this, 'search_only_titles' ), 10, 2 ); $query = new \WP_Query( array( 's' => $search_string, 'post_type' => $post_type, 'posts_per_page' => - 1, ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $title = get_the_title(); $title .= ( 0 != $query->post->post_parent ) ? ' (' . get_the_title( $query->post->post_parent ) . ')' : ''; $id = get_the_id(); $data[] = array( 'id' => 'post-' . $id, 'text' => $title, ); } } if ( is_array( $data ) && ! empty( $data ) ) { $result[] = array( 'text' => $key, 'children' => $data, ); } } $data = array(); wp_reset_postdata(); $args = array( 'public' => true, ); $output = 'objects'; // names or objects, note names is the default. $operator = 'and'; // also supports 'or'. $taxonomies = get_taxonomies( $args, $output, $operator ); foreach ( $taxonomies as $taxonomy ) { $terms = get_terms( $taxonomy->name, array( 'orderby' => 'count', 'hide_empty' => 0, 'name__like' => $search_string, ) ); $data = array(); $label = ucwords( $taxonomy->label ); if ( ! empty( $terms ) ) { foreach ( $terms as $term ) { $term_taxonomy_name = ucfirst( str_replace( '_', ' ', $taxonomy->name ) ); $data[] = array( 'id' => 'tax-' . $term->term_id, 'text' => $term->name . ' archive page', ); $data[] = array( 'id' => 'tax-' . $term->term_id . '-single-' . $taxonomy->name, 'text' => 'All singulars from ' . $term->name, ); } } if ( is_array( $data ) && ! empty( $data ) ) { $result[] = array( 'text' => $label, 'children' => $data, ); } } // return the result in json. wp_send_json( $result ); } function search_only_titles( $search, $wp_query ) { if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) { global $wpdb; $q = $wp_query->query_vars; $n = ! empty( $q['exact'] ) ? '' : '%'; $search = array(); foreach ( (array) $q['search_terms'] as $term ) { $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n ); } if ( ! is_user_logged_in() ) { $search[] = "$wpdb->posts.post_password = ''"; } $search = ' AND ' . implode( ' AND ', $search ); } return $search; } public static function instance() { if ( is_null( self::$_instance ) ) { self::$_instance = new self(); } return self::$_instance; } } function aiea_header_footer() { return AIEA_Header_Footer::instance(); } aiea_header_footer();