Created
April 1, 2011 00:05
-
-
Save anthonycole/897512 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: WPEC Product Search History | |
| Version: 1.0 | |
| Plugin URI: http://anthonycole.me/wp-search-history/ | |
| Description: Allows a user to see products that match results from their search history | |
| Author: Anthony Cole | |
| Author URI: http://anthonycole.me/ | |
| Copyright 2011 (email: [email protected] ) | |
| This program is free software; you can redistribute it and/or modify | |
| it under the terms of the GNU General Public License as published by | |
| the Free Software Foundation; either version 2 of the License, or | |
| (at your option) any later version. | |
| This program is distributed in the hope that it will be useful, | |
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| GNU General Public License for more details. | |
| You should have received a copy of the GNU General Public License | |
| along with this program; if not, write to the Free Software | |
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| */ | |
| /* | |
| * @function wpecsh_register_pt() | |
| * @description Registers a Post Type for WordPress Search History | |
| * @package wpecsh | |
| * @since 1.0 | |
| */ | |
| function wpecsh_register_pt() { | |
| $args = array( | |
| 'public' => false, | |
| 'publicly_queryable' => true, | |
| 'show_in_menu' => false, | |
| 'query_var' => false, | |
| 'rewrite' => false, | |
| 'capability_type' => 'post', | |
| 'has_archive' => false, | |
| 'hierarchical' => false, | |
| 'supports' => array('author'), | |
| ); | |
| register_post_type( 'wpecsh_log', $args ); | |
| } | |
| add_action( 'init', 'wpecsh_register_pt' ); | |
| /* | |
| * @function wpecsh_search_log | |
| * @description logs a users' search history | |
| * @package wpecsh | |
| * @since 1.0 | |
| */ | |
| function wpecsh_search_log() { | |
| if( is_user_logged_in() && is_search() ) { | |
| $user = wp_get_current_user(); | |
| $args = array( | |
| 'post_author' => $user->id, | |
| 'post_title' => '', | |
| 'post_content' => get_query_var( 'wpsc_live_search' ), | |
| 'post_type' => 'wpecsh_log', | |
| 'post_status' => 'publish' | |
| ); | |
| $post = wp_update_post( $args ); | |
| } | |
| } | |
| add_action( 'template_redirect', 'wpecsh_search_log' ); | |
| class WPECsh_Widget extends WP_Widget { | |
| function wpecsh_Widget() { | |
| $widget_ops = array( 'classname' => 'wpecsh_widget', 'description' => 'Displays a users\'s product search history' ); | |
| $control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => 'wpecsh' ); | |
| $this->WP_Widget( 'wpecsh_widget', __('Product Search History'), $widget_ops, $control_ops ); | |
| } | |
| function widget( $args, $instance ) { | |
| $user = wp_get_current_user( ); | |
| $args = array( | |
| 'post_type' => 'wpecsh_log', | |
| 'post_author' => $user->id, | |
| 'showposts' => 30 | |
| ); | |
| $results = get_posts( $args ); | |
| if( 0 == count( $results ) ) { | |
| extract( $args ); | |
| echo $before_widget; | |
| $title = apply_filters('widget_title', $instance['title'] ); | |
| if ( $title ) | |
| echo $before_title . $title . $after_title; | |
| foreach( $results as $search_result ) { | |
| $args = array( | |
| 's' => $search_results->post_content, | |
| 'post_type' => 'wpsc-product', | |
| 'showposts' => 1, | |
| ); | |
| $products = get_posts( $args ); | |
| foreach( $product as $product ) { | |
| ?> | |
| <li> | |
| <?php if ( has_post_thumbnail() ) : ?> | |
| <?php post_thumbnail() ?> | |
| <?php endif; ?> | |
| <a href="<?php echo get_permalink( $product->id ); ?>"><?php echo $product->post_title; ?></a> | |
| </li> | |
| <?php | |
| } // second foreach | |
| } // first foreach | |
| echo $after_widget; | |
| } // if more than 0 results | |
| } | |
| function update( $new_instance, $old_instance ) { | |
| $instance = $old_instance; | |
| foreach ( array('title', 'results_show') as $val ) { | |
| $instance[$val] = strip_tags( $new_instance[$val] ); | |
| } | |
| return $instance; | |
| } | |
| function form( $instance ) { | |
| $defaults = array( | |
| 'title' => 'Product Search History', | |
| 'results_show' => 5, | |
| ); | |
| $instance = wp_parse_args( (array) $instance, $defaults ); ?> | |
| <p> | |
| <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e("Title"); ?>:</label> | |
| <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" /> | |
| </p> | |
| <p> | |
| <label for="<?php echo $this->get_field_id( 'results_show' ); ?>"><?php _e("Number of results to show"); ?>:</label> | |
| <input type="text" id="<?php echo $this->get_field_id( 'results_show' ); ?>" name="<?php echo $this->get_field_name( 'results_show' ); ?>" value="<?php echo $instance['results_show']; ?>" size="5" /> | |
| </p> | |
| <p><strong>Note:</strong> This widget will not display if there are no search history results for the user.</p> | |
| <?php | |
| } | |
| } | |
| function _widget_func() { | |
| register_widget( 'wpecsh_Widget' ); | |
| } | |
| add_action( 'widgets_init', '_widget_func' ); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment