09/14/2014 at 10:46 am
#303979
Keymaster
@ Christopher
Ok, all fixed up!
For reference for anyone else who has implemented this or wants to. Here’s the updated code snippet to get it to work again.
function cn_initial_search_filter( $posts, $WP_Query ) {
global $wp_query;
if ( ! class_exists('cnTemplatePart') ) return $posts;
$shortcode = 'connections';
$pattern = get_shortcode_regex();
// Grab the array containing all query vars registered by Connections.
$registeredQueryVars = cnRewrite::queryVars( array() );
foreach ( $posts as $post ) {
// If we're in the main query, proceed!
if ( isset( $WP_Query->queried_object_id ) && $WP_Query->queried_object_id == $post->ID ) {
/*
* $matches[0] == An array of all shortcode that were found with its options.
* $matches[1] == Unknown.
* $matches[2] == An array of all shortcode tags that were found.
* $matches[3] == An array of the shortcode options that were found.
* $matches[4] == Unknown.
* $matches[5] == Unknown.
* $matches[6] == Unknown.
*/
if ( preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
&& array_key_exists( 2, $matches )
&& in_array( $shortcode, $matches[2] ) )
{
// Parse the shortcode atts.
$atts = shortcode_parse_atts( $matches[3][ array_search( $shortcode, $matches[2] ) ] );
// Remove the cn-image query vars.
$wp_query->query_vars = array_diff_key( (array) $wp_query->query_vars, array_flip( array( 'src', 'w', 'h', 'q', 'a', 'zc', 'f', 's', 'o', 'cc', 'ct' ) ) );
// Show the just the search form w/o showing the intial results?
// If a Connections query var is set, show the results instead.
if ( isset( $atts['initial_results'] )
&& strtolower( $atts['initial_results'] ) == 'false'
&& ! (bool) array_intersect( $registeredQueryVars, array_keys( (array) $wp_query->query_vars ) )
)
{
// Add return = true to $atts so template part are returned instead of echoed.
$atts['return'] = true;
// Enqueue the CSS and JS. We need to use the action because we're hooked
// way to early in WP to use wp_enqueue_style() and wp_enqueue_script().
add_action( 'wp_enqueue_scripts', array( 'cnScript', 'enqueueStyles' ) );
// Add action to enqueue Chosen.
add_action( 'wp_enqueue_scripts', 'cn_enqueue_chosen' );
add_action( 'wp_print_footer_scripts', 'cn_init_chosen', 999 );
// Passing the shortcode $atts incase there are any template part specific shortcode options set.
$category = cnTemplatePart::category( $atts );
$search = cnTemplatePart::search( $atts );
$formOpen = cnTemplatePart::formOpen( $atts );
$formClose = cnTemplatePart::formClose( $atts );
// Create the final output.
$replace = $formOpen . $category . $search . $formClose;
// All returns and tabs should be removed so wpautop() doesn't insert <p> and <br> tags in the form output.
//$replace = str_replace( array( "rn", "r", "n", "t" ), array( ' ', ' ', ' ', ' ' ), $replace );
// This needs to be wrapped in a div with an id of #cn-list so the CSS styles will be applied.
$replace = '<div id="cn-list">' . $replace . '</div>';
} else {
// Rewrite the $atts array to prep it to be imploded.
array_walk( $atts, create_function( '&$i,$k','$i="$k=\"$i\"";' ) );
$replace = '[' . $shortcode . ' ' . implode( ' ', $atts ) . ']';
}
// Replace the shortcode in the post with something a new one based on you changes to $atts.
$post->post_content = str_replace( $matches[0][ array_search( $shortcode, $matches[2] ) ], $replace, $post->post_content );
}
}
}
return $posts;
}
function cn_enqueue_chosen() {
// If SCRIPT_DEBUG is set and TRUE load the non-minified JS files, otherwise, load the minified files.
$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script( 'jquery-chosen', CN_URL . "vendor/chosen/chosen.jquery$min.js", array( 'jquery' ), '1.1.0', true );
}
function cn_init_chosen() {
?>
<script type="text/javascript">/* <![CDATA[ */
jQuery('select[name^=cn-cat]').chosen();
/* ]]> */</script>
<?php
}
add_filter( 'the_posts', 'cn_initial_search_filter', 1000, 2 );