|
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
|
68 |
|
public function __construct() { |
|
42
|
|
|
/** Set plugin directory. */ |
|
43
|
68 |
|
$this->plugin_directory = gravityview()->plugin->dir(); |
|
44
|
68 |
|
} |
|
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
|
4 |
|
public function __destruct() { |
|
56
|
4 |
|
} |
|
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
|
69 |
|
public static function split_slug( $slug, $name = null ) { |
|
67
|
|
|
|
|
68
|
69 |
|
$dir_name = ( dirname( $slug ) != '.' ) ? trailingslashit( dirname( $slug ) ) : ''; |
|
69
|
69 |
|
$slug_name = basename( $slug ) . ( $name ? "-$name" : '' ); |
|
70
|
|
|
|
|
71
|
69 |
|
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 \Gamajo_Template_Loader The current instance. |
|
88
|
|
|
*/ |
|
89
|
69 |
|
public function push_template_data( $data, $var_name = 'data' ) { |
|
90
|
69 |
|
if ( ! isset( self::$data_stack[ $var_name ] ) ) { |
|
91
|
2 |
|
self::$data_stack[ $var_name ] = array(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
69 |
|
global $wp_query; |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
69 |
|
if ( isset( $wp_query->query_vars[ $var_name ] ) ) { |
|
97
|
60 |
|
array_push( self::$data_stack[ $var_name ], $wp_query->query_vars[ $var_name ] ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
69 |
|
$this->set_template_data( $data, $var_name ); |
|
101
|
|
|
|
|
102
|
69 |
|
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
|
69 |
|
public function pop_template_data( $var_name = 'data' ) { |
|
116
|
69 |
|
if ( ! empty( self::$data_stack[ $var_name ] ) ) { |
|
117
|
60 |
|
$this->set_template_data( array_pop( self::$data_stack[ $var_name ] ), $var_name ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
69 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.