table_name = $wpdb->prefix . 'aws_index'; add_action( 'wp_loaded', array( $this, 'check_table' ) ); add_action( 'save_post', array( $this, 'update_table' ), 10, 3 ); add_action( 'wp_ajax_aws-reindex', array( $this, 'reindex_table' ) ); add_action( 'wp_ajax_nopriv_aws-reindex', array( $this, 'reindex_table' ) ); } /* * Reindex plugin table */ public function reindex_table() { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS {$this->table_name}"); $this->check_table(); } /* * Generate table for search terms */ public function check_table() { global $wpdb; if ( $wpdb->get_var( "SHOW TABLES LIKE '{$this->table_name}'" ) != $this->table_name ) { $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE {$this->table_name} ( id MEDIUMINT(20) NOT NULL DEFAULT 0, term VARCHAR(50) NOT NULL DEFAULT 0, term_source VARCHAR(20) NOT NULL DEFAULT 0, type VARCHAR(50) NOT NULL DEFAULT 0, count MEDIUMINT(20) NOT NULL DEFAULT 0 ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); $this->fill_table(); } } /* * Insert data into the index table */ private function fill_table( $post_id = 0 ) { global $wpdb; $posts = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'product', 'include' => $post_id ) ); foreach ( $posts as $post ) { $terms = array(); $id = $post->ID; $product = new WC_product( $id ); $sku = $product->get_sku(); $title = apply_filters( 'the_title', get_the_title( $id ) ); $content = apply_filters( 'the_content', get_post_field( 'post_content', $id ) ); $excerpt = apply_filters( 'get_the_excerpt', get_post_field( 'post_excerpt', $id ) ); $cat_names = $this->get_terms_names_list( $id, 'product_cat' ); $tag_names = $this->get_terms_names_list( $id, 'product_tag' ); // WP 4.2 emoji strip if ( function_exists( 'wp_encode_emoji' ) ) { $content = wp_encode_emoji( $content ); } $content = strip_shortcodes( $content ); $terms['title'] = $this->extract_terms( $title ); $terms['content'] = $this->extract_terms( $content ); $terms['excerpt'] = $this->extract_terms( $excerpt ); $terms['sku'] = $this->extract_terms( $sku ); $terms['category'] = $this->extract_terms( $cat_names ); $terms['tag'] = $this->extract_terms( $tag_names ); foreach( $terms as $source => $all_terms ) { foreach ( $all_terms as $term => $count ) { if ( ! $term ) { continue; } $wpdb->insert( $this->table_name, array( 'id' => $id, 'term' => $term, 'term_source' => $source, 'type' => 'product', 'count' => $count ) ); } } // break; } } /* * Update index table */ public function update_table( $post_id, $post, $update ) { global $wpdb; $this->check_table(); $slug = 'product'; if ( $slug != $post->post_type ) { return; } $wpdb->delete( $this->table_name, array( 'id' => $post_id ) ); $this->fill_table( $post_id ); } /* * Extract terms from content */ private function extract_terms( $str ) { $str = $this->html2txt( $str ); $str = esc_attr( $str ); $str = stripslashes( $str ); $str = str_replace( array( "\r", "\n" ), '', $str ); // Avoid single A-Z. $str = preg_replace( '/\b\w{1}\b/i', " ", $str ); $str = str_replace( array( "·", "…", "€", "­" ), "", $str ); $str = str_replace( array( ".", ",", "$", "\\", "/", "{", "^", "}", "?", "!", ";", "(", ")", ":", "[", "]", "'", "+", "ˇ", "°", "~", '"', "Ë›", "Ëť", "¸", "§", "%", "=", "¨", "`", "’", "‘", "”", "“", "„", "´", "—", "–", "Ă—", '’', " ", chr( 194 ) . chr( 160 ) ), " ", $str ); $str = str_replace( 'Ăź', 'ss', $str ); //$str = preg_replace( '/[[:punct:]]+/u', ' ', $str ); $str = preg_replace( '/[[:space:]]+/', ' ', $str ); // Most objects except unicode characters $str = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', '', $str ); // Line feeds, carriage returns, tabs $str = preg_replace( '/[\x00-\x1F\x80-\x9F]/u', '', $str ); $str = strtolower( $str ); $str = preg_replace( '/^[a-z]$/i', "", $str ); $str = trim( preg_replace( '/\s+/', ' ', $str ) ); $str_array = array_count_values( explode( ' ', $str ) ); return $str_array; } /* * Removes scripts, styles, html tags */ private function html2txt( $str ) { $search = array( '@]*?>.*?@si', '@<[\/\!]*?[^<>]*?>@si', '@]*?>.*?@siU', '@@' ); $text = preg_replace( $search, '', $str ); return $text; } /* * Get string with current product terms ids * * @return string List of terms ids */ private function get_terms_list( $id, $taxonomy ) { $terms = get_the_terms( $id, $taxonomy ); if ( is_wp_error( $terms ) ) { return ''; } if ( empty( $terms ) ) { return ''; } $cats_array_temp = array(); foreach ( $terms as $term ) { $cats_array_temp[] = $term->term_id; } return implode( ', ', $cats_array_temp ); } /* * Get string with current product terms names * * @return string List of terms names */ private function get_terms_names_list( $id, $taxonomy ) { $terms = get_the_terms( $id, $taxonomy ); if ( is_wp_error( $terms ) ) { return ''; } if ( empty( $terms ) ) { return ''; } $cats_array_temp = array(); foreach ( $terms as $term ) { $cats_array_temp[] = $term->name; } return implode( ', ', $cats_array_temp ); } } endif; new AWS_Table();