Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for WP 5.5 get_template args parameter. #40

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,16 @@ or:
~~~php
$meal_planner_template_loader->get_template_part( 'recipe' );
~~~
* If you want to pass data to the template, call the `set_template_data()` method with an array before calling `get_template_part()`. `set_template_data()` returns the loader object to allow for method chaining.
* If you want to pass data to the template, use the `$args` parameter like in `get_template_part` since WordPress 5.5.

~~~php
$data = array( 'foo' => 'bar', 'baz' => 'boom' );
$meal_planner_template_loader
->set_template_data( $data );
->get_template_part( 'recipe' );
->get_template_part( 'recipe', null, $data );
~~~

The value of `bar` is now available inside the recipe template as `$data->foo`.
The value of `foo` is now available inside the recipe template as `$args['foo']`.

If you wish to use a different variable name, add a second parameter to `set_template_data()`:

~~~php
$data = array( 'foo' => 'bar', 'baz' => 'boom' );
$meal_planner_template_loader
->set_template_data( $data, 'context' )
->get_template_part( 'recipe', 'ingredients' );
~~~

The value of `bar` is now available inside the recipe template as `$context->foo`.

This will try to load up `wp-content/themes/my-theme/meal-planner/recipe-ingredients.php`, or `wp-content/themes/my-theme/meal-planner/recipe.php`, then fallback to `wp-content/plugins/meal-planner/templates/recipe-ingredients.php` or `wp-content/plugins/meal-planner/templates/recipe.php`.

### Meal Planner Example Class
Expand Down
28 changes: 16 additions & 12 deletions class-gamajo-template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @link http://github.com/GaryJones/Gamajo-Template-Loader
* @copyright 2013 Gary Jones
* @license GPL-2.0-or-later
* @version 1.3.1
* @version 1.4.0
*/

if ( ! class_exists( 'Gamajo_Template_Loader' ) ) {
Expand Down Expand Up @@ -103,19 +103,20 @@ public function __destruct() {
*
* @param string $slug Template slug.
* @param string $name Optional. Template variation name. Default null.
* @param array $args Optional. Variables passed to template. Default empty array.
* @param bool $load Optional. Whether to load template. Default true.
* @return string
*/
public function get_template_part( $slug, $name = null, $load = true ) {
public function get_template_part( $slug, $name = null, $args = array(), $load = true ) {
// Execute code for this part.
do_action( 'get_template_part_' . $slug, $slug, $name );
do_action( $this->filter_prefix . '_get_template_part_' . $slug, $slug, $name );
do_action( 'get_template_part_' . $slug, $slug, $name, $args );
do_action( $this->filter_prefix . '_get_template_part_' . $slug, $slug, $name, $args );

// Get files names of templates, for given slug and name.
$templates = $this->get_template_file_names( $slug, $name );
$templates = $this->get_template_file_names( $slug, $name, $args );

// Return the part that is found.
return $this->locate_template( $templates, $load, false );
return $this->locate_template( $templates, $load, false, $args );
}

/**
Expand All @@ -135,7 +136,7 @@ public function get_template_part( $slug, $name = null, $load = true ) {
*/
public function set_template_data( $data, $var_name = 'data' ) {
global $wp_query;

_deprecated_function( 'set_template_data', '1.4.0' );
$wp_query->query_vars[ $var_name ] = (object) $data;

// Add $var_name to custom variable store if not default value.
Expand All @@ -157,7 +158,7 @@ public function set_template_data( $data, $var_name = 'data' ) {
*/
public function unset_template_data() {
global $wp_query;

_deprecated_function( 'unset_template_data', '1.4.0' );
// Remove any duplicates from the custom variable store.
$custom_var_names = array_unique( $this->template_data_var_names );

Expand All @@ -178,9 +179,10 @@ public function unset_template_data() {
*
* @param string $slug Template slug.
* @param string $name Template variation name.
* @param array $args Optional. Variables passed to template.
* @return array
*/
protected function get_template_file_names( $slug, $name ) {
protected function get_template_file_names( $slug, $name, $args ) {
$templates = array();
if ( isset( $name ) ) {
$templates[] = $slug . '-' . $name . '.php';
Expand All @@ -199,7 +201,7 @@ protected function get_template_file_names( $slug, $name ) {
* @param string $slug Template slug.
* @param string $name Template variation name.
*/
return apply_filters( $this->filter_prefix . '_get_template_part', $templates, $slug, $name );
return apply_filters( $this->filter_prefix . '_get_template_part', $templates, $slug, $name, $args );
}

/**
Expand All @@ -215,9 +217,11 @@ protected function get_template_file_names( $slug, $name ) {
* @param bool $load If true the template file will be loaded if it is found.
* @param bool $require_once Whether to require_once or require. Default true.
* Has no effect if $load is false.
* @param array $args Optional. Variables passed to template. Default empty array.
*
* @return string The template filename if one is located.
*/
public function locate_template( $template_names, $load = false, $require_once = true ) {
public function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {

// Use $template_names as a cache key - either first element of array or the variable itself if it's a string.
$cache_key = is_array( $template_names ) ? $template_names[0] : $template_names;
Expand Down Expand Up @@ -252,7 +256,7 @@ public function locate_template( $template_names, $load = false, $require_once =
}

if ( $load && $located ) {
load_template( $located, $require_once );
load_template( $located, $require_once, $args );
}

return $located;
Expand Down