GetPaid_Template   B
last analyzed

Complexity

Total Complexity 47

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
c 0
b 0
f 0
dl 0
loc 268
rs 8.64
wmc 47

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B is_preview() 0 8 7
A display_template() 0 12 2
A is_cornerstone_preview() 0 2 2
A is_divi_preview() 0 2 5
A oxygen_override_template() 0 8 2
A locate_template() 0 18 4
A get_template() 0 4 1
A is_elementor_preview() 0 2 6
A is_siteorigin_preview() 0 2 1
A is_beaver_preview() 0 2 1
A is_oxygen_preview() 0 2 4
A load_template() 0 14 2
A get_theme_template_path() 0 5 1
A oxygen_locate_template() 0 25 5
A is_fusion_preview() 0 2 2

How to fix   Complexity   

Complex Class

Complex classes like GetPaid_Template often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GetPaid_Template, and based on these observations, apply Extract Interface, too.

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

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