'themeidol-ajaxsearch-widget', 'description' => __( 'Search form with AJAX results', 'themeidol-all-widget' ) ) ); add_action( 'init', array( $this, 'themeidol_ajaxsearch_register_script' ) ) ; // Register site styles and scripts add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_styles' ) ); // only load scripts when an instance is active if ( is_active_widget( false, false, $this->id_base ) && !is_admin() ) add_action( 'wp_footer', array( $this, 'themeidolasw_print_script' ) ); add_action( 'wp_ajax_themeidolasw', array( $this, 'themeidolasw_ajax' ) ); add_action( 'wp_ajax_nopriv_themeidolasw', array( $this, 'themeidolasw_ajax' ) ); // Refreshing the widget's cached output with each new post add_action( 'save_post', array( $this, 'flush_widget_cache' ) ); add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) ); add_action( 'delete_attachment', array( $this, 'flush_group_cache' ) ); add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) ); } /** * widget * * Output the widget * * @return void */ function widget( $args, $instance ) { // Check if there is a cached output $cache = (array) wp_cache_get( 'themeidol-ajaxsearch', 'widget' ); if(!is_array($cache)) $cache = array(); if(isset($cache[$args['widget_id']])){ echo $cache[$args['widget_id']]; return; } ob_start(); extract( $args, EXTR_SKIP ); $title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', esc_attr($instance['title']) ); $username = empty( $instance['username'] ) ? '' : esc_attr($instance['username']); $limit = empty( $instance['number'] ) ? 10 : esc_attr($instance['number']); // Adding the custom class idol-widget for default widget class if (strpos($before_widget, 'widget ') !== false) { $before_widget = preg_replace('/widget /', "idol-widget ", $before_widget, 1); } echo $before_widget; if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }; do_action( 'wpasw_before_widget', $instance ); get_search_form( true ); ?>
__( 'Search', 'themeidol-all-widget' ), 'number' => 10 ) ); $title = esc_attr( $instance['title'] ); $number = absint( $instance['number'] ); ?>

add_query_arg( array( 'action' => 'themeidolasw', '_wpnonce' => wp_create_nonce( 'themeidolasw' ) ), untrailingslashit( set_url_scheme( admin_url( 'admin-ajax.php' ) ) ) ), ) ); } /** * Registers and enqueues widget-specific styles. */ public function register_widget_styles() { wp_enqueue_style( 'themeidol-ajaxsearch', THEMEIDOL_WIDGET_CSS_URL.'search-style.css'); } // end register_widget_styles /** * wpasw_print_script * * Output JS only when widget in use on page * * @return void */ function themeidolasw_print_script() { wp_print_scripts( 'themeidolasw' ); } /** * ajax * * Handle the search request * * @return string */ function themeidolasw_ajax() { // verify the nonce if ( wp_verify_nonce( $_REQUEST['_wpnonce'], 'themeidolasw' ) ) { // clean up the query $s = trim( stripslashes( $_POST['s'] ) ); // cancel if no search term is set if ( !$s ) die(); // get the settings for this widget instance $instance = $this->get_settings(); if ( array_key_exists( $this->number, $instance ) ) { $instance = $instance[$this->number]; } do_action( 'themeidolasw_before_results' ); // set the query limit $limit = empty( $instance['number'] ) ? 10: $instance['number']; $query_args = apply_filters( 'themeidolasw_query', array( 's' => $s, 'post_status' => 'publish', 'posts_per_page' => $limit ), $s, $limit ); $search = new WP_Query( $query_args ); if ( $search->have_posts() ) : ?>