GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-template.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * Load up the Gamajo Template Loader.
11  *
12  * @see https://github.com/GaryJones/Gamajo-Template-Loader
13  */
14 if ( ! class_exists( '\GV\Gamajo_Template_Loader' ) ) {
15  require gravityview()->plugin->dir( 'future/lib/class-gamajo-template-loader.php' );
16 }
17 
18 /**
19  * The Template abstract class.
20  *
21  * Stores information on where a template to render an object is,
22  * and other metadata.
23  */
24 abstract class Template extends \GV\Gamajo_Template_Loader {
25 
26  /**
27  * @var array The template data stack.
28  */
29  private static $data_stack = array();
30 
31  /**
32  * @var string The located template.
33  */
34  public $located_template = '';
35 
36  /**
37  * General template initialization.
38  *
39  * Sets the $plugin_directory field.
40  */
41  public function __construct() {
42  /** Set plugin directory. */
43  $this->plugin_directory = gravityview()->plugin->dir();
44  }
45 
46  /**
47  * Disallow any cleanup for fear of loss of global data.
48  *
49  * The destructor in Gamajo 1.3.0 destroys all of `$wp_query`.
50  * This has the chance of inappropriately destroying valid
51  * data that's been stored under the same key.
52  *
53  * Disallow this.
54  */
55  public function __destruct() {
56  }
57 
58  /**
59  * Get a directory part and a full slug+name (file) components.
60  *
61  * @param string $slug The slug, template base.
62  * @param string $name The name, template part. Default: null
63  *
64  * @return array containing slug directory and slug+name.
65  */
66  public static function split_slug( $slug, $name = null ) {
67 
68  $dir_name = ( dirname( $slug ) != '.' ) ? trailingslashit( dirname( $slug ) ) : '';
69  $slug_name = basename( $slug ) . ( $name ? "-$name" : '' );
70 
71  return array( $dir_name, $slug_name );
72  }
73 
74  /**
75  * Push the current template data down the stack and set.
76  *
77  * This allows us to use the same variable in the template scope
78  * without destroying data under the same variable in a nested
79  * or parallel template.
80  *
81  * @param mixed $data The data to set.
82  * @param string $var_name The data variable identifier (Default: "data")
83  *
84  * @see \Gamajo_Template_Loader::set_template_data
85  * @see \GV\Template::pop_template_data
86  *
87  * @return \GV\Gamajo_Template_Loader The current instance.
88  */
89  public function push_template_data( $data, $var_name = 'data' ) {
90  if ( ! isset( self::$data_stack[ $var_name ] ) ) {
91  self::$data_stack[ $var_name ] = array();
92  }
93 
94  global $wp_query;
95 
96  if ( isset( $wp_query->query_vars[ $var_name ] ) ) {
97  array_push( self::$data_stack[ $var_name ], $wp_query->query_vars[ $var_name ] );
98  }
99 
100  $this->set_template_data( $data, $var_name );
101 
102  return $this;
103  }
104 
105  /**
106  * Restore the template data from the stack.
107  *
108  * @param string $var_name The data variable identifier (Default: "data")
109  *
110  * @see \Gamajo_Template_Loader::set_template_data
111  * @see \GV\Template::pop_template_data
112  *
113  * @return $this;
114  */
115  public function pop_template_data( $var_name = 'data' ) {
116  if ( ! empty( self::$data_stack[ $var_name ] ) ) {
117  $this->set_template_data( array_pop( self::$data_stack[ $var_name ] ), $var_name );
118  }
119 
120  return $this;
121  }
122 }
__destruct()
Disallow any cleanup for fear of loss of global data.
static split_slug( $slug, $name=null)
Get a directory part and a full slug+name (file) components.
push_template_data( $data, $var_name='data')
Push the current template data down the stack and set.
If this file is called directly, abort.
__construct()
General template initialization.
gravityview()
The main GravityView wrapper function.
pop_template_data( $var_name='data')
Restore the template data from the stack.