Skip to content
This repository
Browse code

reasonable compromise

  • Loading branch information...
commit 3e8dcf62850397da99a6caad0eabd4d5a65a1071 1 parent 2a7683a
Alex King authored May 27, 2012

Showing 1 changed file with 7 additions and 1 deletion. Show diff stats Hide diff stats

  1. 8  utility.php
8  utility.php
@@ -311,7 +311,13 @@ function cfct_template($dir, $keys = array()) {
311 311
 **/
312 312
 function cfct_template_file($dir, $file, $data = array()) {
313 313
 	// bring in expected globals so that templates don't need to bring them in
314  
-	global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
  314
+	// added in version 3.4 - can be overridden via filter
  315
+	$global_vars = apply_filters('cfct_template_file_globals', array('posts', 'post', 'wp_did_header', 'wp_did_template_redirect', 'wp_query', 'wp_rewrite', 'wpdb', 'wp_version', 'wp', 'id', 'comment', 'user_ID'));
  316
+	if (count($global_vars)) {
  317
+		foreach ($global_vars as $global_var) {
  318
+			global $$global_var;
  319
+		}
  320
+	}
315 321
 	$path = '';
316 322
 	if (!empty($file)) {
317 323
 		$file = basename($file, '.php');

0 notes on commit 3e8dcf6

Emyr Thomas

Nitpicking here, but I think the semantics of what you're asking here would be better implied by if (!empty($global_vars)). While probably insignificant in this case, the !empty method also has some performance benefits since it doesn't have to compute the length of the array. See this question on StackOverflow for details.

Matthew Richmond

Along those same lines, PHP doesn't throw an error or notice if you foreach over an empty array, and b/c it's a filter we should double-check that we're getting back the type we want b/c that's all that really matters. I suggest simply doing if (is_array($global_vars))

Please sign in to comment.
Something went wrong with that request. Please try again.