*/ class Acme_Amazing_Search_Public { private $cache; /** * 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'))); } /** * Create the response JSON to Ajax Call * * @since 1.0.0 */ public function do_search() { if ( isset ( $_REQUEST['term'] ) ) { $term = aas_cleanupStrings( $_REQUEST['term'] ); } $results = $this->aas_options['results']; $sep = $this->aas_options['separator'] ? " {$this->aas_options['separator']} " : " - "; if( !isset ( $term ) ) { $this->cache = true; } //$sku = retrieve_sku(); /** * 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 = retrieve_terms( $taxonomy, $this->aas_options['show_taxonomy'],$this->cache); if ( is_object( $categories ) ) { foreach ( $categories as $ID => $row ) { echo $haystack = $row->title; if ( null != $haystack ) { $haystack = aas_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 = retrieve_posts( $post_type, $this->cache ); }; //if ( $results > 0 && is_array( $_skus ) ) { //} 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; $skus = null; if($this->aas_options['aas_sku']) $skus = retrieve_meta(); foreach ( $posts as $row ) { if ( $skus ) { $sku = $skus[ $row->ID ]; } $haystack = $title ? $row->post_title : null; $haystack .= $excerpt ? wp_strip_all_tags( $row->post_excerpt ) : null; $haystack .= $skus ? $sku : null; if ( null != $haystack ) { $haystack = aas_cleanupStrings( $haystack, true ); if ( strpos( $haystack, $term ) ) { $title = $skus ? '[' . $sku . '] ' : null; $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; } } } if ( true == $this->cache ) { return false; } /** * Show All button behaviour */ $append_post_type = null; if ( $this->aas_options['append_post_type'] ) { $append_post_type = '&post_type=' . $this->aas_options['append_post_type']; } $show_all_text = $this->aas_options['show_all_text'] ? $this->aas_options['show_all_text'] : __( "Show All", $this->plugin_name ); if ( isset ( $term ) ) { 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'] . $append_post_type ); } break; default: $search_results[] = array( 'value' => $show_all_text, 'url' => get_bloginfo( 'url' ) . '/?s=' . $_REQUEST['term'] . $append_post_type ); break; } } ob_clean(); echo json_encode( $search_results ); wp_die(); } }