*/ class Acme_Amazing_Search_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; $this->aas_options = get_option( $this->plugin_name ); } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/acme-amazing-search-public.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { /** * load custom script and WP predefined jQuery and jQueryUI */ wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/acme-amazing-search-public.js', array( 'jquery','jquery-ui-autocomplete' ), $this->version, true ); /** * Register 'ajax' url to be used in custom js * */ wp_localize_script( $this->plugin_name, 'ajax', array('url' => admin_url( 'admin-ajax.php' ))); wp_localize_script( $this->plugin_name, 'home', array('siteurl' => get_option('siteurl'))); } /** * Cleans string from crap html, blank spaces and stop-words * * @since 1.0.0 */ private function cleanupStrings($str,$haystack=false) { $result = htmlentities( str_replace( array( "-", " ", " " ), "", $str ), ENT_IGNORE ); $result = strtolower( $result ); $result = str_replace( array( "-", "/" ), "", $result ); $result = utf8_encode( str_replace( " ", "", $result ) ); if(true==$haystack) $result = "__$result"; return $result; } /** * Retrieve all the posts by 'post_type' with with no child * * @since 1.0.0 */ public function retrieve_posts($post_type, $keyword=null) { // WP_Query arguments $args = array( 'post_parent' => '0', 'post_type' => is_array( $post_type ) ? $post_type : array( $post_type ), 'post_status' => array( 'publish' ), 'orderby' => array( 'menu_order', 'post_title' ), 'cache_results' => true, 'posts_per_page' => 1, //limit results to 1 in order to access the 'request' method 'no_found_rows' => true, // counts posts, remove if pagination required 'update_post_term_cache' => false, // grabs terms, remove if terms required (category, tag...) 'update_post_meta_cache' => false, // grabs post meta, remove if post meta required ); // setup our query, replacing some option from original WP_Query $regex = array( '/\bFROM\b/', '/\bOR \(wp_posts.post_content\b(.*\%)\b(.*\%(.?)\))/', //remove search from content '/\bLIMIT.*/' ); $replace = array( ', post_title , post_excerpt FROM', '', '' ); // The Query $query = new WP_Query( $args ); //replace $QStat = preg_replace( $regex, $replace, $query->request ); //we will use $wpdb function to query the DB in order to increase preformances global $wpdb; $results = $wpdb->get_results( $QStat ); return $results; } /** * Retrieve all the posts by 'post_type' with with no child * * @since 1.0.0 */ public function retrieve_terms($taxonomy, $keyword=null, $showTax=null) { // WP_Term_Query arguments $args = array( 'taxonomy' => is_array( $taxonomy ) ? $taxonomy : array( $taxonomy ), 'order' => 'DESC', 'orderby' => 'count', 'hide_empty' => true, 'get' => 'all', 'name__like' => $keyword ); // The Term Query $term_query = new WP_Term_Query( $args ); // The Loop if ( ! empty( $term_query ) && ! is_wp_error( $term_query ) ) { // do something foreach ( $term_query->terms as $term ) { $inspect[ $term->term_id ] = $term->name; $result[ $term->term_id ] = array( 'title' => $term->name, 'parent_id' => $term->parent, 'id' => $term->term_id, 'count' => $term->count, 'taxonomy' => $showTax ? $term->taxonomy : null, 'url' => get_term_link($term->term_id) ); } foreach ( $result as $id => $row ) { if($row['parent_id']>0) $result[ $id ]['parent'] = $result[ $row['parent_id'] ]['title']; } return $result; } } /** * Create the response JSON to Ajax Call * * @since 1.0.0 */ public function do_search() { $term = $this->cleanupStrings( $_REQUEST['term'] ); $results = $this->aas_options['results']; $sep = $this->aas_options['separator'] ? " {$this->aas_options['separator']} " : " - "; /** * Let's read plugin options in order to choose the taxonomy we want to search in */ $taxonomy = array(); if ( $this->aas_options['categories'] ) { $taxonomy[] = "category"; } if ( $this->aas_options['tags'] ) { $taxonomy[] = "post_tag"; } if ( $this->aas_options['brands'] ) { $taxonomy[] = "product_brand"; } if ( $this->aas_options['product_categories'] ) { $taxonomy[] = "product_cat"; } if ( $this->aas_options['product_tags'] ) { $taxonomy[] = "product_tag"; } if ( $this->aas_options['terms'] ) { $add_terms = explode( ',', $this->aas_options['terms'] ); $taxonomy = array_merge( $taxonomy, $add_terms ); } $i = 0; if ( count( $taxonomy ) > 0 ) { $categories = $this->retrieve_terms( $taxonomy, $term, $this->aas_options['show_taxonomy'] ); if ( is_array( $categories ) ) { foreach ( $categories as $ID => $row ) { $haystack = $row['title']; if ( null != $haystack ) { $haystack = $this->cleanupStrings( $haystack, true ); if ( strpos( $haystack, $term ) ) { $title = null; $title = $this->aas_options['show_taxonomy'] ? "[{$row['taxonomy']}]" . $sep : null; $title .= $row['parent_id'] > 0 ? "{$row['parent']} => " : null; $title .= "{$row['title']} ({$row['count']})"; $caturl = $row['url']; $search_results[] = array( 'value' => $title, 'url' => $row['url'] ); $i ++; } } if ( $i == $results ) { break; } } } } /** * Now we start counting results */ $count = $i; /** * $cat_results is needed for the "Search All" button behaviour */ $cat_results = $i; /** * Let's see how many results we need before function end */ $results -= $count; /** * Let's find out which post_types we have to query */ $post_type = array(); if ( $this->aas_options['posts'] ) { $post_type[] = "post"; } if ( $this->aas_options['pages'] ) { $post_type[] = "page"; } if ( $this->aas_options['products'] ) { $post_type[] = "product"; } if ( $this->aas_options['post_type'] ) { $p_types = explode( ',', $this->aas_options['post_type'] ); $post_type = array_merge( $post_type, $p_types ); } if ( count( $post_type ) > 0 ) { $posts = $this->retrieve_posts( $post_type, $term ); }; if ( $results > 0 && is_array( $posts ) ) { /** * Let's see where we have to look for our terms (title, excerpt or both?) */ $title = $this->aas_options['title']; $excerpt = $this->aas_options['excerpt']; $i = 0; foreach ( $posts as $row ) { $haystack = $title ? $row->post_title : null; $haystack .= $excerpt ? wp_strip_all_tags( $row->post_excerpt ) : $haystack; if ( null != $haystack ) { $haystack = $this->cleanupStrings( $haystack, true ); if ( strpos( $haystack, $term ) ) { $title = $this->aas_options['show_title'] ? $row->post_title : null; if ( $title && $this->aas_options['show_excerpt'] ) { $title .= $sep; } $title .= $this->aas_options['show_excerpt'] ? wp_strip_all_tags( $row->post_excerpt ) : null; $title = $this->aas_options['trim'] ? substr( $title, 0, $this->aas_options['trim'] ) . "..." : $title; $search_results[] = array( 'value' => $title, 'url' => get_permalink( $row->ID ), ); $i ++; } } if ( $i == $results ) { break; } } } /** * Show All button behaviour */ $show_all_text = $this->aas['show_all_text'] ? $this->aas_options['show_all_text'] : __( "Show All", $this->plugin_name ); switch ( $this->aas_options['behaviour'] ) { case 1: if ( $cat_results == 1 ) { $search_results[] = array( 'value' => $show_all_text, 'url' => $caturl, ); } else { $search_results[] = array( 'value' => $show_all_text, 'url' => get_bloginfo( 'url' ) . '/?s=' . $_REQUEST['term'] ); } break; default: $search_results[] = array( 'value' => $show_all_text, 'url' => get_bloginfo( 'url' ) . '/?s=' . $_REQUEST['term'], ); break; } ob_clean(); //print_r($search_results); echo json_encode( $search_results ); wp_die(); } }