Passed
Pull Request — master (#398)
by Brian
05:25
created

GetPaid_Template::locate_template()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 18
rs 10
cc 4
nc 8
nop 3
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Template Class
8
 *
9
 */
10
class GetPaid_Template {
11
12
    /**
13
     * @param string
14
     */
15
    public $templates_dir;
16
17
    /**
18
     * @param string
19
     */
20
    public $templates_url;
21
22
    /**
23
	 * Class constructor.
24
	 *
25
	 * @since 1.0.19
26
	 */
27
	public function __construct() {
28
29
        $this->templates_dir = apply_filters( 'getpaid_default_templates_dir', WPINV_PLUGIN_DIR . 'templates' );
30
        $this->templates_url = apply_filters( 'getpaid_default_templates_url', WPINV_PLUGIN_URL . 'templates' );
31
    }
32
33
    /**
34
	 * Checks if this is a preview page
35
	 *
36
	 * @since 1.0.19
37
	 * @return bool
38
	 */
39
	public function is_preview() {
40
        return 
41
            $this->is_divi_preview() ||
42
            $this->is_elementor_preview() ||
43
            $this->is_beaver_preview() ||
44
            $this->is_siteorigin_preview() ||
45
            $this->is_cornerstone_preview() ||
46
            $this->is_fusion_preview() ||
47
            $this->is_oxygen_preview();
48
    }
49
50
    /**
51
	 * Checks if this is an elementor preview page
52
	 *
53
	 * @since 1.0.19
54
	 * @return bool
55
	 */
56
	public function is_elementor_preview() {
57
		return isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' );
58
	}
59
60
	/**
61
	 * Checks if this is a DIVI preview page
62
	 *
63
	 * @since 1.0.19
64
	 * @return bool
65
	 */
66
	public function is_divi_preview() {
67
		return isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'et_pb' );
68
	}
69
70
	/**
71
	 * Checks if this is a beaver builder preview page
72
	 *
73
	 * @since 1.0.19
74
	 * @return bool
75
	 */
76
	public function is_beaver_preview() {
77
		return isset( $_REQUEST['fl_builder'] );
78
	}
79
80
	/**
81
	 * Checks if this is a siteorigin builder preview page
82
	 *
83
	 * @since 1.0.19
84
	 * @return bool
85
	 */
86
	public function is_siteorigin_preview() {
87
		return ! empty( $_REQUEST['siteorigin_panels_live_editor'] );
88
	}
89
90
	/**
91
	 * Checks if this is a cornerstone builder preview page
92
	 *
93
	 * @since 1.0.19
94
	 * @return bool
95
	 */
96
	public function is_cornerstone_preview() {
97
		return ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint';
98
	}
99
100
	/**
101
	 * Checks if this is a fusion builder preview page
102
	 *
103
	 * @since 1.0.19
104
	 * @return bool
105
	 */
106
	public function is_fusion_preview() {
107
		return ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] );
108
	}
109
110
	/**
111
	 * Checks if this is an oxygen builder preview page
112
	 *
113
	 * @since 1.0.19
114
	 * @return bool
115
	 */
116
	public function is_oxygen_preview() {
117
		return ! empty( $_REQUEST['ct_builder'] ) || ( ! empty( $_REQUEST['action'] ) && ( substr( $_REQUEST['action'], 0, 11 ) === "oxy_render_" || substr( $_REQUEST['action'], 0, 10 ) === "ct_render_" ) );
118
    }
119
120
    /**
121
     * Locates a template path.
122
     * 
123
     * @param string $template_name e.g payment-forms/cart.php The template to locate.
124
     * @param string $template_path The template path relative to the theme's root dir. Defaults to 'invoicing'.
125
     * @param string $default_path The root path to the default template. Defaults to invoicing/templates
126
     */
127
	public function locate_template( $template_name, $template_path = '', $default_path = '' ) {
128
129
        // Load the defaults for the template path and default path.
130
        $template_path = empty( $template_path ) ? 'invoicing' : $template_path;
131
        $default_path  = empty( $default_path ) ? $this->templates_dir : $default_path;
132
133
         // Is it overidden?
134
        $template = locate_template(
135
            array( trailingslashit( $template_path ) . $template_name ),
136
            array( 'wpinv-' . $template_name )
0 ignored issues
show
Bug introduced by
array('wpinv-' . $template_name) of type array<integer,string> is incompatible with the type boolean expected by parameter $load of locate_template(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
            /** @scrutinizer ignore-type */ array( 'wpinv-' . $template_name )
Loading history...
137
        );
138
139
        // If not, load the default template.
140
        if ( empty( $template ) ) {
141
            $template = trailingslashit( $default_path ) . $template_name;
142
        }
143
144
        return apply_filters( 'wpinv_locate_template', $template, $template_name, $template_path, $default_path );
145
    }
146
    
147
    /**
148
	 * Loads a template
149
	 *
150
	 * @since 1.0.19
151
	 * @return bool
152
	 */
153
	protected function load_template( $template_name, $template_path, $args ) {
154
155
        if ( is_array( $args ) ){
156
            extract( $args );
157
        }
158
159
        // Fires before loading a template.
160
	    do_action( 'wpinv_before_template_part', $template_name, $template_path, $args );
161
162
        // Load the template.
163
	    include( $template_path );
164
165
        // Fires after loading a template.
166
        do_action( 'wpinv_after_template_part', $template_name, $template_path, $args );
167
168
    }
169
170
    /**
171
     * Displays a template.
172
     * 
173
     * First checks if there is a template overide, if not it loads the default template.
174
     * 
175
     * @param string $template_name e.g payment-forms/cart.php The template to locate.
176
     * @param array $args An array of args to pass to the template.
177
     * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'.
178
     * @param string $default_path The root path to the default template. Defaults to invoicing/templates
179
     */
180
	public function display_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
181
182
        // Locate the template.
183
        $located = wpinv_locate_template( $template_name, $template_path, $default_path );
184
185
        // Abort if the file does not exist.
186
        if ( ! file_exists( $located ) ) {
187
            getpaid_doing_it_wrong( __CLASS__ . '::' .__METHOD__, sprintf( '<code>%s</code> does not exist.', $located ), '1.0.19' );
188
            return;
189
        }
190
191
        $this->load_template( $template_name, $located, $args );
192
193
    }
194
    
195
    /**
196
     * Retrieves a template.
197
     * 
198
     * First checks if there is a template overide, if not it loads the default template.
199
     * 
200
     * @param string $template_name e.g payment-forms/cart.php The template to locate.
201
     * @param array $args An array of args to pass to the template.
202
     * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'.
203
     * @param string $default_path The root path to the default template. Defaults to invoicing/templates
204
     */
205
	public function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
206
        ob_start();
207
        $this->display_template( $template_name, $args, $template_path, $default_path );
208
        return ob_get_clean();
209
    }
210
211
}
212