Skip to content

Instantly share code, notes, and snippets.

@nuancedesignstudio
Last active April 27, 2018 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuancedesignstudio/b872d11dddf9e8077d63453a99dc2683 to your computer and use it in GitHub Desktop.
Save nuancedesignstudio/b872d11dddf9e8077d63453a99dc2683 to your computer and use it in GitHub Desktop.
Changes to methods in class-common.php to add Post Thumbnail to the Advanced Search cache
<?php
/**
* Register the JavaScript for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_scripts() {
$transient_name = $this->plugin_transients['autosuggest_transient'];
$cached_posts_titles = array();
$cached_posts_data = array();
// check if cached posts are available, and make the data available to the JavaScript.
$cached_posts = get_transient( $transient_name );
// $cached_posts will contain an object with Post ID, Title and the Thumbnail URI.
if ( $cached_posts ) {
foreach ( $cached_posts as $index => $post ) {
$cached_posts_titles[ $index ] = $post['title'];
$cached_posts_data[$index] = array(
'id' => $post['id'],
'title' => $post['title'],
'thumbnail_uri' => $post['post_thumbnail_uri'],
);
}
}
$params = array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'cached_post_titles' => $cached_posts_titles,
'cached_posts_data' => $cached_posts_data, // make the cached posts data available in the JavaScript.
);
wp_enqueue_script( 'nds_advanced_search', plugin_dir_url( __FILE__ ) . 'js/nds-advanced-search.js', array( 'jquery', 'jquery-ui-autocomplete' ), $this->version, true );
wp_localize_script( 'nds_advanced_search', 'params', $params );
}
/**
* Cache WordPress posts for post types that are specified in the
* plugin setting to be included in the custom search.
*
* @since 1.0.0
*/
public function cache_posts_in_post_types() {
$transient_name = $this->plugin_transients['autosuggest_transient'];
$transient_expiration = $this->plugin_transients['autosuggest_transient_expiration'];
// retrieve the selected post types from the plugin settings to include in the custom search.
$plugin_options = get_option( $this->plugin_name );
$post_types = array_keys( $plugin_options, 1, true );
// check the transient for existing cached data.
$cached_posts = get_transient( $transient_name );
if ( false === $cached_posts ) {
$args = array(
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => -1,
'no_found_rows' => true, // true by default.
'suppress_filters' => false, // true by default.
'ignore_sticky_posts' => true, // true by default.
);
// get_posts() to retrieve posts belonging to the required post types.
$posts_in_required_post_types = get_posts( $args );
// Check if posts were found.
if ( $posts_in_required_post_types ) {
foreach ( $posts_in_required_post_types as $key => $post ) {
// get the URL for featured image.
$featured_img_url = get_the_post_thumbnail_url( $post->ID );
// cache the post titles, post ids, and the image url.
$cached_post = array(
'id' => $post->ID,
'title' => esc_html( $post->post_title ),
'post_thumbnail_uri' => $featured_img_url ? esc_html( $featured_img_url ) : false,
);
$cached_posts[] = $cached_post;
}
/**
* Save the post data in a transient.
* For better performance cache only the post ids, titles
* instead of the entire WP Query.
*/
set_transient( $transient_name, $cached_posts, $transient_expiration );
}
}
return $cached_posts;
}
/**
* AJAX handler for the auto-suggest.
*
* Callback for the "wp_ajax_nds_advanced_search_autosuggest" and
* "wp_ajax_nopriv_nds_advanced_search_autosuggest" hooks in "class-init.php"
*
* @since 1.0.0
*/
public function advanced_search_autosuggest_handler() {
$transient_name = $this->plugin_transients['autosuggest_transient'];
// check if cached posts are available.
$cached_posts = get_transient( $transient_name );
if ( false === $cached_posts ) {
// retrieve posts by running a new query and cache the posts in the transient as well.
$cached_posts = $this->cache_posts_in_post_types();
}
$cached_post_titles = array();
$cached_posts_data = array();
foreach ( $cached_posts as $index => $post ) {
$cached_post_titles[ $index ] = $post['title'];
$cached_posts_data[$index] = array(
'id' => $post['id'],
'title' => $post['title'],
'thumbnail_uri' => $post['post_thumbnail_uri'], // also pass in the post_thumbnail_uri
);
}
// Echo the response to the AJAX request.
// the response is the $cached_posts_data object which contains titles as well as post thumbail.
// wp_send_json( $cached_post_titles ); // Note: earlier this was an array
wp_send_json( $cached_posts_data ); // Note: this is an object.
// wp_send_json will also die().
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment