Passed
Push — master ( 57234e...a7a8b8 )
by Brian
05:17
created

GetPaid_Template::get_theme_template_path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
        // Oxygen plugin
33
		if ( defined( 'CT_VERSION' ) ) {
34
			add_filter( 'wpinv_locate_template', array( $this, 'oxygen_override_template' ), 11, 4 );
35
		}
36
37
    }
38
39
    /**
40
	 * Checks if this is a preview page
41
	 *
42
	 * @since 1.0.19
43
	 * @return bool
44
	 */
45
	public function is_preview() {
46
        return 
47
            $this->is_divi_preview() ||
48
            $this->is_elementor_preview() ||
49
            $this->is_beaver_preview() ||
50
            $this->is_siteorigin_preview() ||
51
            $this->is_cornerstone_preview() ||
52
            $this->is_fusion_preview() ||
53
            $this->is_oxygen_preview();
54
    }
55
56
    /**
57
	 * Checks if this is an elementor preview page
58
	 *
59
	 * @since 1.0.19
60
	 * @return bool
61
	 */
62
	public function is_elementor_preview() {
63
		return isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' );
64
	}
65
66
	/**
67
	 * Checks if this is a DIVI preview page
68
	 *
69
	 * @since 1.0.19
70
	 * @return bool
71
	 */
72
	public function is_divi_preview() {
73
		return isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'et_pb' );
74
	}
75
76
	/**
77
	 * Checks if this is a beaver builder preview page
78
	 *
79
	 * @since 1.0.19
80
	 * @return bool
81
	 */
82
	public function is_beaver_preview() {
83
		return isset( $_REQUEST['fl_builder'] );
84
	}
85
86
	/**
87
	 * Checks if this is a siteorigin builder preview page
88
	 *
89
	 * @since 1.0.19
90
	 * @return bool
91
	 */
92
	public function is_siteorigin_preview() {
93
		return ! empty( $_REQUEST['siteorigin_panels_live_editor'] );
94
	}
95
96
	/**
97
	 * Checks if this is a cornerstone builder preview page
98
	 *
99
	 * @since 1.0.19
100
	 * @return bool
101
	 */
102
	public function is_cornerstone_preview() {
103
		return ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint';
104
	}
105
106
	/**
107
	 * Checks if this is a fusion builder preview page
108
	 *
109
	 * @since 1.0.19
110
	 * @return bool
111
	 */
112
	public function is_fusion_preview() {
113
		return ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] );
114
	}
115
116
	/**
117
	 * Checks if this is an oxygen builder preview page
118
	 *
119
	 * @since 1.0.19
120
	 * @return bool
121
	 */
122
	public function is_oxygen_preview() {
123
		return ! empty( $_REQUEST['ct_builder'] ) || ( ! empty( $_REQUEST['action'] ) && ( substr( $_REQUEST['action'], 0, 11 ) === "oxy_render_" || substr( $_REQUEST['action'], 0, 10 ) === "ct_render_" ) );
124
    }
125
126
    /**
127
     * Locates a template path.
128
     * 
129
     * @param string $template_name e.g payment-forms/cart.php The template to locate.
130
     * @param string $template_path The template path relative to the theme's root dir. Defaults to 'invoicing'.
131
     * @param string $default_path The root path to the default template. Defaults to invoicing/templates
132
     */
133
	public function locate_template( $template_name, $template_path = '', $default_path = '' ) {
134
135
        // Load the defaults for the template path and default path.
136
        $template_path = empty( $template_path ) ? 'invoicing' : $template_path;
137
        $default_path  = empty( $default_path ) ? $this->templates_dir : $default_path;
138
        $default_path  = apply_filters( 'getpaid_template_default_template_path', $default_path, $template_name );
139
140
        // Is it overidden?
141
        $template = locate_template(
142
            array( trailingslashit( $template_path ) . $template_name, 'wpinv-' . $template_name )
143
        );
144
145
        // If not, load the default template.
146
        if ( empty( $template ) ) {
147
            $template = trailingslashit( $default_path ) . $template_name;
148
        }
149
150
        return apply_filters( 'wpinv_locate_template', $template, $template_name, $template_path, $default_path );
0 ignored issues
show
Unused Code introduced by
The call to oxygen_override_template() has too many arguments starting with $template_path. ( Ignorable by Annotation )

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

150
        return /** @scrutinizer ignore-call */ apply_filters( 'wpinv_locate_template', $template, $template_name, $template_path, $default_path );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
151
    }
152
    
153
    /**
154
	 * Loads a template
155
	 *
156
	 * @since 1.0.19
157
	 * @return bool
158
	 */
159
	protected function load_template( $template_name, $template_path, $args ) {
160
161
        if ( is_array( $args ) ){
162
            extract( $args );
163
        }
164
165
        // Fires before loading a template.
166
	    do_action( 'wpinv_before_template_part', $template_name, $template_path, $args );
167
168
        // Load the template.
169
	    include( $template_path );
170
171
        // Fires after loading a template.
172
        do_action( 'wpinv_after_template_part', $template_name, $template_path, $args );
173
174
    }
175
176
    /**
177
     * Displays a template.
178
     * 
179
     * First checks if there is a template overide, if not it loads the default template.
180
     * 
181
     * @param string $template_name e.g payment-forms/cart.php The template to locate.
182
     * @param array $args An array of args to pass to the template.
183
     * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'.
184
     * @param string $default_path The root path to the default template. Defaults to invoicing/templates
185
     */
186
	public function display_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
187
188
        // Locate the template.
189
        $located = $this->locate_template( $template_name, $template_path, $default_path );
190
191
        // Abort if the file does not exist.
192
        if ( ! file_exists( $located ) ) {
193
            getpaid_doing_it_wrong( __CLASS__ . '::' .__METHOD__, sprintf( '<code>%s</code> does not exist.', $located ), '1.0.19' );
194
            return;
195
        }
196
197
        $this->load_template( $template_name, $located, $args );
198
199
    }
200
    
201
    /**
202
     * Retrieves a template.
203
     * 
204
     * First checks if there is a template overide, if not it loads the default template.
205
     * 
206
     * @param string $template_name e.g payment-forms/cart.php The template to locate.
207
     * @param array $args An array of args to pass to the template.
208
     * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'.
209
     * @param string $default_path The root path to the default template. Defaults to invoicing/templates
210
     */
211
	public function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
212
        ob_start();
213
        $this->display_template( $template_name, $args, $template_path, $default_path );
214
        return ob_get_clean();
215
    }
216
217
    /**
218
	 * Get the geodirectory templates theme path.
219
	 *
220
	 *
221
	 * @return string Template path.
222
	 */
223
	public static function get_theme_template_path() {
224
		$template   = get_template();
225
		$theme_root = get_theme_root( $template );
226
227
		return $theme_root . '/' . $template . '/' . untrailingslashit( wpinv_get_theme_template_dir_name() );
228
229
	}
230
231
	/**
232
	 * Oxygen locate theme template.
233
	 *
234
	 * @param string $template The template.
235
	 * @return string The theme template.
236
	 */
237
	public static function oxygen_locate_template( $template ) {
238
239
		if ( empty( $template ) ) {
240
			return '';
241
		}
242
243
		$has_filter = has_filter( 'template', 'ct_oxygen_template_name' );
244
245
		// Remove template filter
246
		if ( $has_filter ) {
247
			remove_filter( 'template', 'ct_oxygen_template_name' );
248
		}
249
250
		$template = self::get_theme_template_path() . '/' . $template;
251
252
		if ( file_exists( $template ) ) {
253
			$template = '';
254
		}
255
256
		// Add template filter
257
		if ( $has_filter ) {
258
			add_filter( 'template', 'ct_oxygen_template_name' );
259
		}
260
261
		return $template;
262
	}
263
264
	/**
265
	 * Oxygen override theme template.
266
	 *
267
	 * @param string $located Located template.
268
	 * @param string $template_name Template name.
269
	 * @return string Located template.
270
	 */
271
	public function oxygen_override_template( $located, $template_name ) {
272
273
        $oxygen_overide = self::oxygen_locate_template( $template_name );
274
		if ( ! empty( $oxygen_overide ) ) {
275
			return $oxygen_overide;
276
		}
277
278
		return $located;
279
	}
280
281
}
282