Last active
August 29, 2015 14:16
-
-
Save rachelbaker/a46241df5f87173d1289 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
/** | |
* Register and load JavaScript files. | |
* | |
* Includes 'settings' array based on query parameters. | |
* | |
* @since 1.0. | |
* | |
* @see wp_enqueue_script() | |
* @see wp_localize_script() | |
*/ | |
function parbaked_enqueue_scripts() { | |
$queried_object = get_queried_object(); | |
$query_settings = parbaked_get_query_settings( $queried_object ); | |
$version = current_time( 'timestamp' ); | |
wp_register_script( | |
'rbbb', | |
get_template_directory_uri() . '/assets/js/src/app.js', | |
array( | |
'backbone' | |
'underscore', | |
'jquery', | |
), | |
$version, | |
true | |
); | |
wp_localize_script( 'rbbb', 'settings', $query_settings ); | |
wp_enqueue_script( 'rbbb' ); | |
} | |
add_action( 'wp_enqueue_scripts', 'parbaked_enqueue_scripts' ); | |
/** | |
* Set the localized settings array based on the request. | |
* | |
* @return array $query_settings | |
*/ | |
function parbaked_get_query_settings( $queried_object ) { | |
global $wp_rewrite; | |
$query_settings = array( | |
'query_type' => 'home', | |
'query_object' => $queried_object, | |
'query_path' => array( | |
'author_permastruct' => $wp_rewrite->get_author_permastruct(), | |
'host' => preg_replace( '#^http(s)?://#i', '', untrailingslashit( get_option( 'home' ) ) ), | |
'path' => parbaked_get_request_path(), | |
'use_trailing_slashes' => $wp_rewrite->use_trailing_slashes, | |
'parameters' => parbaked_get_request_parameters(), | |
), | |
); | |
/** | |
* Conditionally change the $query_settings array based on the current request. | |
*/ | |
if ( is_singular() ) { | |
$query_settings['query_type'] = 'single'; | |
} | |
if ( is_category() || is_tag() || is_tax() ) { | |
$query_settings['query_type'] = 'archive'; | |
$query_settings['taxonomy'] = get_taxonomy( $queried_object->taxonomy ); | |
} elseif ( is_date() ) { | |
$query_settings['query_type'] = 'archive'; | |
} elseif ( is_search() ) { | |
$query_settings['query_type'] = 'search'; | |
$query_settings['searchQuery'] = get_search_query(); | |
} elseif ( is_author() ) { | |
$query_settings['query_type'] = 'author'; | |
} | |
if ( is_paged() ) { | |
$query_settings['page'] = absint( get_query_var( 'paged' ) ) + 1; | |
} | |
return $query_settings; | |
} | |
/** | |
* Build path data for current request. | |
* | |
* @return string|bool | |
*/ | |
function parbaked_get_request_path() { | |
global $wp_rewrite; | |
if ( $wp_rewrite->using_permalinks() ) { | |
global $wp; | |
// If called too early, bail | |
if ( ! isset( $wp->request ) ) { | |
return false; | |
} | |
// Determine path for paginated version of current request | |
if ( false != preg_match( '#' . $wp_rewrite->pagination_base . '/\d+/?$#i', $wp->request ) ) { | |
$path = preg_replace( '#' . $wp_rewrite->pagination_base . '/\d+$#i', $wp_rewrite->pagination_base . '/%d', $wp->request ); | |
} elseif ( is_paged() ) { | |
$path = $wp->request . '/' . $wp_rewrite->pagination_base . '/%d'; | |
} else { | |
$path = $wp->request . '/'; | |
} | |
// Slashes everywhere we need them | |
if ( 0 !== strpos( $path, '/' ) ) { | |
$path = '/' . $path; | |
} | |
$path = user_trailingslashit( $path ); | |
} else { | |
// Clean up raw $_REQUEST input | |
$path = array_map( 'sanitize_text_field', $_REQUEST ); | |
$path = array_filter( $path ); | |
$path['paged'] = '%d'; | |
$path = add_query_arg( $path, '/' ); | |
} | |
return empty( $path ) ? false : $path; | |
} | |
/** | |
* Return query string for current request, prefixed with '?'. | |
* | |
* @return string | |
*/ | |
function parbaked_get_request_parameters() { | |
$uri = $_SERVER[ 'REQUEST_URI' ]; | |
$uri = preg_replace( '/^[^?]*(\?.*$)/', '$1', $uri, 1, $count ); | |
if ( 1 != $count ) { | |
return ''; | |
} | |
return $uri; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment