Permalink
Browse files

New filters for wfx_get_template_part() + refactor

New filters to allow alterning of file to include - allows re-use of template parts. Instead of including a file with the right filename and then get_template_file() inside that to point to your custom file, just use filters instead, it’s what the cool kids do (and saves your sanity!)

New filter: 'wflux_template_part_main' - string containing first part of filename to get (slug), eg 'loop-content' (filename example loop-content-archive.php).

New filter: 'wflux_template_part_fragment' - string containing second part of filename to get (name) eg 'archive' (filename example loop-content-archive.php).

New filter: 'wflux_template_part_array' - array of values used to build filename to get (tip - most useful for advanced filtering!), eg array(0 => 'loop-content', 1 => 'archive') (filename example loop-content-archive.php).

NOTE: 'wflux_template_part_array' filter is probably most useful, here is a code example:

<?php

/**
 *
 * Filter template parts so we re-use certain files for consistency.
 *
 * This will make 'my-taxonomy' and date based archives use the file
 * 'loop-content-archive-special.php' in your child theme instead of
 * 'loop-content-archive.php' which is default behaviour in Wonderflux.
 *
 */
function mywfx_file_parts_filter( $val ) {

	if ( is_tax( 'my-taxonomy' ) || is_date() ) {

		if ( is_array( $val ) && ( array_key_exists( 0, $val ) && array_key_exists( 1, $val ) ) ) {

			// Check we just going to filter loop-content-archive.php
			if ( $val[0] == 'loop-content' && $val[1] == 'archive' ) {

				$val[1] = 'archive-special';

			}

		}

	}

	return $val;

}
add_filter( 'wflux_template_part_array', 'mywfx_file_parts_filter' );

?>
  • Loading branch information...
Jonnyauk committed Nov 28, 2017
1 parent aa3593c commit 7780fb622a50cbb826b932527fe79cb3f551b82a
Showing with 67 additions and 45 deletions.
  1. +8 −1 functions.php
  2. +59 −44 wf-includes/wf-helper-functions.php
View
@@ -296,8 +296,15 @@
* 1 loop-content-404.php
* 2 loop-content.php
*
* @filter wflux_template_part_main - string containing first part of filename to get (slug),
* eg 'loop-content' (filename example loop-content-archive.php)
* @filter wflux_template_part_fragment - string containing second part of filename to get (name)
* eg 'archive' (filename example loop-content-archive.php)
* @filter wflux_template_part_array - array of values used to build filename to get (tip - most useful for advanced filtering!),
* eg array(0 => 'loop-content', 1 => 'archive') (filename example loop-content-archive.php)
*
* @since 0.881
* @version 2.3
* @version 2.6
*
* @param [string] $part REQUIRED The slug name for the generic template
*
@@ -514,8 +514,15 @@ function wf_info_single() {
* 1 loop-content-404.php
* 2 loop-content.php
*
* @filter wflux_template_part_main - string containing first part of filename to get (slug),
* eg 'loop-content' (filename example loop-content-archive.php)
* @filter wflux_template_part_fragment - string containing second part of filename to get (name)
* eg 'archive' (filename example loop-content-archive.php)
* @filter wflux_template_part_array - array of values used to build filename to get (tip - most useful for advanced filtering!),
* eg array(0 => 'loop-content', 1 => 'archive') (filename example loop-content-archive.php)
*
* @since 0.881
* @version 2.3
* @version 2.6
*
* @param [string] $part REQUIRED The slug name for the generic template
*
@@ -524,7 +531,7 @@ function wf_info_single() {
function wf_get_template_part( $args ) {
$defaults = array (
'part' => false,
'part' => false
);
$args = wp_parse_args( $args, $defaults );
@@ -542,13 +549,13 @@ function wf_get_template_part( $args ) {
$slug = get_query_var( 'post_type' );
$slug_depth_1 = ( isset($slug) ) ? $this_location . '-' . $slug : false;
if ( $this->wfx_is_small_screen == true ){
if ( $this->wfx_is_small_screen == true ) {
if ( locate_template( $part . '-' . $slug_depth_1 . '-mobile.php', false ) !='' ):
$part_get = $slug_depth_1 . '-mobile';
endif;
}
if ( empty($part_get) ){
if ( empty( $part_get ) ) {
if ( locate_template( $part . '-' . $slug_depth_1 . '.php', false ) !='' ):
$part_get = $slug_depth_1;
endif;
@@ -559,16 +566,16 @@ function wf_get_template_part( $args ) {
// Category archive
case ( 'category' ):
$slug = get_category( get_query_var('cat') )->slug;
$slug_depth_1 = ( isset($slug) ) ? $this_location . '-' . $slug : false;
$slug = get_category( get_query_var( 'cat' ) )->slug;
$slug_depth_1 = ( isset( $slug ) ) ? $this_location . '-' . $slug : false;
if ( $this->wfx_is_small_screen == true ){
if ( $this->wfx_is_small_screen == true ) {
if ( locate_template( $part . '-' . $slug_depth_1 . '-mobile.php', false ) !='' ):
$part_get = $slug_depth_1 . '-mobile';
endif;
}
if ( empty($part_get) ){
if ( empty( $part_get ) ) {
if ( locate_template( $part . '-' . $slug_depth_1 . '.php', false ) !='' ):
$part_get = $slug_depth_1;
endif;
@@ -579,16 +586,16 @@ function wf_get_template_part( $args ) {
// Tag archive
case ( 'tag' ):
$slug = get_query_var('tag');
$slug_depth_1 = ( isset($slug) ) ? $this_location . '-' . $slug : false;
$slug = get_query_var( 'tag' );
$slug_depth_1 = ( isset( $slug ) ) ? $this_location . '-' . $slug : false;
if ( $this->wfx_is_small_screen == true ){
if ( $this->wfx_is_small_screen == true ) {
if ( locate_template( $part . '-' . $slug_depth_1 . '-mobile.php', false ) !='' ):
$part_get = $slug_depth_1 . '-mobile';
endif;
}
if ( empty($part_get) ){
if ( empty($part_get) ) {
if ( locate_template( $part . '-' . $slug_depth_1 . '.php', false ) !='' ):
$part_get = $slug_depth_1;
endif;
@@ -598,23 +605,24 @@ function wf_get_template_part( $args ) {
// Taxonomy archive
case ('taxonomy'):
//NOTE: No get_query_var / $wp_query in taxonomy archive view - not populated
$this_q = get_queried_object();
$slug_depth_1 = ( isset($this_q->taxonomy) ) ? $this_location . '-' . $this_q->taxonomy : false;
$slug_depth_2 = ( isset($this_q->slug) ) ? $this_location . '-' . $this_q->taxonomy . '-' . $this_q->slug : false;
$slug_depth_1 = ( isset( $this_q->taxonomy ) ) ? $this_location . '-' . $this_q->taxonomy : false;
$slug_depth_2 = ( isset( $this_q->slug ) ) ? $this_location . '-' . $this_q->taxonomy . '-' . $this_q->slug : false;
if ( $this->wfx_is_small_screen == true ){
if ( locate_template($part . '-' . $slug_depth_2 . '-mobile.php', false) !='' ):
if ( $this->wfx_is_small_screen == true ) {
if ( locate_template( $part . '-' . $slug_depth_2 . '-mobile.php', false ) !='' ):
$part_get = $slug_depth_2 . '-mobile';
elseif ( locate_template($part . '-' . $slug_depth_1 . '-mobile.php', false) !='' ):
elseif ( locate_template( $part . '-' . $slug_depth_1 . '-mobile.php', false ) !='' ):
$part_get = $slug_depth_1 . '-mobile';
endif;
}
if ( empty($part_get) ){
if ( locate_template($part . '-' . $slug_depth_2 . '.php', false) !='' ):
if ( empty($part_get) ) {
if ( locate_template( $part . '-' . $slug_depth_2 . '.php', false ) !='' ):
$part_get = $slug_depth_2;
elseif ( locate_template($part . '-' . $slug_depth_1 . '.php', false) !='' ):
elseif ( locate_template( $part . '-' . $slug_depth_1 . '.php', false ) !='' ):
$part_get = $slug_depth_1;
endif;
}
@@ -623,20 +631,21 @@ function wf_get_template_part( $args ) {
// Date archive
case ('date'):
$month = get_query_var( 'monthnum' );
$year = get_query_var( 'year' );
$slug_1 = ( !empty($year) ) ? '-' . $year : false;
$slug_2 = ( !empty($month) ) ? ($month < 10) ? sprintf( '-%02d', $month ) : '-' . $month : false;
$slug_1 = ( !empty( $year ) ) ? '-' . $year : false;
$slug_2 = ( !empty( $month ) ) ? ( $month < 10 ) ? sprintf( '-%02d', $month ) : '-' . $month : false;
if ( $this->wfx_is_small_screen == true ){
if ( locate_template( $part . '-' . $this_location . $slug_1 . $slug_2 . '-mobile.php', false) !='' ):
if ( $this->wfx_is_small_screen == true ) {
if ( locate_template( $part . '-' . $this_location . $slug_1 . $slug_2 . '-mobile.php', false ) !='' ):
$part_get = $this_location . $slug_1 . $slug_2 . '-mobile';
elseif ( locate_template($part . '-' . $this_location . $slug_1 . '-mobile.php', false) !='' ):
elseif ( locate_template( $part . '-' . $this_location . $slug_1 . '-mobile.php', false ) !='' ):
$part_get = $this_location . $slug_1 . '-mobile';
endif;
}
if ( empty($part_get) ){
if ( empty($part_get) ) {
if ( locate_template( $part . '-' . $this_location . $slug_1 . $slug_2 . '.php', false) !='' ):
$part_get = $this_location . $slug_1 . $slug_2;
elseif ( locate_template($part . '-' . $this_location . $slug_1 . '.php', false) !='' ):
@@ -647,18 +656,18 @@ function wf_get_template_part( $args ) {
break;
// Archive/custom post type archive
case ('archive'):
case ( 'archive' ):
$slug = get_query_var( 'post_type' );
$slug_depth_1 = (isset($slug)) ? $this_location . '-' . $slug : false;
$slug_depth_1 = ( isset( $slug ) ) ? $this_location . '-' . $slug : false;
if ( $this->wfx_is_small_screen == true ){
if ( $this->wfx_is_small_screen == true ) {
if ( locate_template( $part . '-' . $slug_depth_1 . '-mobile.php', false ) !='' ):
$part_get = $slug_depth_1 . '-mobile';
endif;
}
if ( empty($part_get) ){
if ( empty( $part_get ) ) {
if ( locate_template( $part . '-' . $slug_depth_1 . '.php', false ) !='' ):
$part_get = $slug_depth_1;
endif;
@@ -667,7 +676,9 @@ function wf_get_template_part( $args ) {
break;
default:
$part_get = $this_location;
break;
}
@@ -682,28 +693,32 @@ function wf_get_template_part( $args ) {
'search'
);
if ( in_array($this_location, $archive_views) ) {
if ( in_array( $this_location, $archive_views ) ) {
if ( $this->wfx_is_small_screen == true ) {
$part_get = ( locate_template( $part . '-' . $this_location . '-mobile.php', false ) !='' ) ? $this_location . '-mobile' : $part_get;
if ( $this->wfx_is_small_screen == true ){
if ( locate_template( $part . '-' . $this_location . '-mobile.php', false ) !='' ):
$part_get = $this_location . '-mobile';
endif;
}
if ( empty($part_get) || $part_get == 'search' ){
if ( locate_template( $part . '-' . $this_location . '.php', false ) !='' ):
$part_get = $this_location;
else:
$part_get = 'archive';
endif;
if ( empty( $part_get ) || $part_get == 'search' ) {
$part_get = ( locate_template( $part . '-' . $this_location . '.php', false ) !='' ) ? $this_location : 'archive';
}
}
// Covers all other eventualities
$part_get = ( empty($part_get) ) ? $this_location : $part_get;
$part_get = ( empty( $part_get ) ) ? $this_location : $part_get;
// Allow filtering so theme files can be re-purposed
$part_get_main = apply_filters( 'wflux_template_part_main', $part );
$part_get_fragment = apply_filters( 'wflux_template_part_fragment', $part_get );
$part_get_array = apply_filters( 'wflux_template_part_array', array( $part_get_main, $part_get_fragment ) );
get_template_part( $part, sanitize_html_class($part_get) );
// Now scoot off and get out template part!
get_template_part( sanitize_html_class( $part_get_array[0] ), sanitize_html_class( $part_get_array[1] ) );
}
@@ -712,7 +727,7 @@ function wf_get_template_part( $args ) {
* Gets user role of logged-in user.
* IMPORTANT - Used internally by Wonderflux.
*
* BACKPAT: When using WordPress 4.5 or above wp_get_current_user()
* BACKPAT: When using WordPress 4.5 or above wp_get_current_user()
* is used instead of get_currentuserinfo() (function deprecated)
*
* @since 0.62

0 comments on commit 7780fb6

Please sign in to comment.