Completed
Push — 2.x ( e41392...55c530 )
by Scott Kingsley
19:59 queued 14:02
created

Pods_Templates_Auto_Template_Settings   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 362
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 362
rs 8.8
wmc 36
lcom 2
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
D options() 0 117 9
A __construct() 0 23 1
A init() 0 8 2
A tab() 0 7 1
A front_end() 0 14 4
A reset() 0 7 2
A reseter() 0 8 2
C archive_test() 0 24 7
B archive_warning() 0 25 5
A get_template_titles() 0 17 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 3.

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.

Loading history...
2
if ( class_exists( 'Pods_PFAT' ) ) {
3
	return;
4
}
5
6
/**
7
 * Class Pods_Templates_Auto_Template_Settings
8
 *
9
 * This class replaced Pods_PFAT class
10
 *
11
 * @since 2.5.5
12
 */
13
class Pods_Templates_Auto_Template_Settings {
14
15
	/**
16
	 * Front end class object
17
	 *
18
	 * @since 2.5.5
19
	 *
20
	 * @var Pods_Templates_Auto_Template_Front_End
21
	 */
22
	private $front_end_class;
23
24
	/**
25
	 * Holds instance of this class
26
	 *
27
	 * @var Pods_Templates_Auto_Template_Settings
28
	 */
29
	private  $instance;
30
31
	/**
32
	 * Constructor for the Pods_PFAT class
33
	 *
34
	 * Sets up all the appropriate hooks and actions
35
	 * within the plugin.
36
	 *
37
	 * @since 2.5.5
38
	 */
39
	public function __construct() {
40
41
42
		//Add option tab for post types
43
		add_filter( 'pods_admin_setup_edit_tabs_post_type', array( $this, 'tab' ), 11, 3 );
44
45
		//add the same tab for taxonomies
46
		add_filter( 'pods_admin_setup_edit_tabs_taxonomy', array( $this, 'tab' ), 11, 3 );
47
48
		//Add options to the new tab
49
		add_filter( 'pods_admin_setup_edit_options', array( $this, 'options' ), 12, 2 );
50
51
52
		//Include and init front-end class
53
		add_action( 'init', array( $this, 'front_end' ), 25 );
54
55
		//Delete transients when Pods settings are updated.
56
		add_action( 'update_option', array( $this, 'reset' ), 21, 3 );
57
58
		//admin notice for archives without archives
59
		add_action( 'admin_notices', array( $this, 'archive_warning' ) );
60
61
	}
62
63
	/**
64
	 * Initializes the class
65
	 *
66
	 * @since 2.5.5
67
	 */
68
	public function init() {
69
		if ( ! is_null( $this->instance ) ) {
70
			$this->instance = new self;
71
		}
72
73
		return $this->instance;
74
75
	}
76
77
78
79
	/**
80
	 * The Frontier Auto Display option tab.
81
	 *
82
	 * @param array $tabs
83
	 * @param array $pod
84
	 * @param array $addtl_args
85
	 *
86
	 * @return array
87
	 *
88
	 * @since 2.5.5
89
	 */
90
	function tab( $tabs, $pod, $addtl_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $addtl_args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
92
		$tabs[ 'pods-pfat' ] = __( 'Auto Template Options', 'pods' );
93
94
		return $tabs;
95
96
	}
97
98
	/**
99
	 * Adds options for this plugin under the Frontier Auto Template tab.
100
	 *
101
	 * @param array $options
102
	 * @param array $pod
103
	 *
104
	 * @return array
105
	 *
106
	 * @since 2.5.5
107
	 *
108
	 */
109
	function options( $options, $pod ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
		//check if it's a post type pod and add fields for that.
111
		if ( $pod['type'] === 'post_type' )  {
112
			$options[ 'pods-pfat' ] = array (
113
				'pfat_enable'  => array (
114
					'label'             => __( 'Enable Automatic Pods Templates for this Pod?', 'pods' ),
115
					'help'              => __( 'When enabled you can specify the names of Pods Templates to be used to display items in this Pod in the front-end.', 'pods' ),
116
					'type'              => 'boolean',
117
					'default'           => false,
118
					'dependency'        => true,
119
					'boolean_yes_label' => ''
120
				),
121
				'pfat_single'  => array (
122
					'label'      => __( 'Single item view template', 'pods' ),
123
					'help'       => __( 'Name of Pods template to use for single item view.', 'pods' ),
124
					'type'       => 'text',
125
					'default'    => false,
126
					'depends-on' => array ( 'pfat_enable' => true )
127
				),
128
				'pfat_append_single'  => array (
129
					'label'      => __( 'Single Template Location', 'pods' ),
130
					'help'       => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),
131
					'depends-on' => array ( 'pfat_enable' => true ),
132
				),
133
				'pfat_archive' => array (
134
					'label'      => __( 'Archive view template', 'pods' ),
135
					'help'       => __( 'Name of Pods template to use for use in this Pods archive pages.', 'pods' ),
136
					'type'       => 'text',
137
					'default'    => false,
138
					'depends-on' => array ( 'pfat_enable' => true )
139
				),
140
				'pfat_append_archive'  => array (
141
					'label'      => __( 'Archive Template Location', 'pods' ),
142
					'help'       => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),
143
					'depends-on' => array ( 'pfat_enable' => true ),
144
				),
145
			);
146
		}
147
148
		//check if it's a taxonomy Pod, if so add fields for that
149
		if ( $pod['type'] === 'taxonomy' ) {
150
			$options[ 'pods-pfat' ] = array (
151
				'pfat_enable'  => array (
152
					'label'             => __( 'Enable Automatic Pods Templates for this Pod?', 'pods' ),
153
					'help'              => __( 'When enabled you can specify the names of a Pods Template to be used to display items in this Pod in the front-end.', 'pods' ),
154
					'type'              => 'boolean',
155
					'default'           => false,
156
					'dependency'        => true,
157
					'boolean_yes_label' => ''
158
				),
159
				'pfat_archive'  => array (
160
					'label'      => __( 'Taxonomy Template', 'pods' ),
161
					'help'       => __( 'Name of Pods template to use for this taxonomy.', 'pods' ),
162
					'type'       => 'text',
163
					'default'    => false,
164
					'depends-on' => array ( 'pfat_enable' => true )
165
				),
166
				'pfat_append_archive'  => array (
167
					'label'      => __( 'Template Location', 'pods' ),
168
					'help'       => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),
169
					'depends-on' => array ( 'pfat_enable' => true ),
170
				),
171
			);
172
		}
173
174
		if ( isset( $options[ 'pods-pfat' ] ) ) {
175
176
			//field options pick values
177
			$pick = array (
178
				'type'               => 'pick',
179
				'pick_format_type'   => 'single',
180
				'pick_format_single' => 'dropdown',
181
				'default'            => 'true',
182
			);
183
184
			//get template titles
185
			$titles = $this->get_template_titles();
186
187
			if ( !empty( $titles ) ) {
188
				foreach ( $pick as $k => $v ) {
189
					$options[ 'pods-pfat' ][ 'pfat_single' ][ $k ] = $v;
190
191
					$options[ 'pods-pfat' ][ 'pfat_archive' ][ $k ] = $v;
192
193
				}
194
195
				$options[ 'pods-pfat' ][ 'pfat_archive' ][ 'data' ] = array( null => __('No Archive view template', 'pods') ) + ( array_combine( $this->get_template_titles(), $this->get_template_titles() ) );
196
				$options[ 'pods-pfat' ][ 'pfat_single' ][ 'data' ] = array_combine( $this->get_template_titles(), $this->get_template_titles() );
197
			}
198
199
			//Add data to $pick for template location
200
			unset( $pick['data']);
201
			$location_data =  array (
202
				'append'  => __( 'After', 'pods' ),
203
				'prepend' => __( 'Before', 'pods' ),
204
				'replace' => __( 'Replace', 'pods' ),
205
			);
206
			$pick['data'] = $location_data;
207
208
			//add location options to fields without type set.
209
			foreach ( $options[ 'pods-pfat' ] as $k => $option ) {
210
				if ( !isset( $option[ 'type' ] ) ) {
211
					$options[ 'pods-pfat' ][ $k ] = array_merge( $option, $pick );
212
				}
213
214
			}
215
216
			//remove single from taxonomy
217
			if( 'taxonomy' === $pod['type'] ){
218
				unset( $options[ 'pods-pfat' ][ 'pfat_single' ] );
219
			}
220
221
		}
222
223
		return $options;
224
225
	}
226
227
	/**
228
	 * Include/ init the front end class on the front end only
229
	 *
230
	 * @param bool	$load_in_admin Optional. Whether to load in admin. Default is false.
231
	 *
232
	 * @return Pods_PFAT_Frontend
233
	 *
234
	 * @since 2.5.5
235
	 */
236
	function front_end( $load_in_admin = false ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
237
238
		if ( !is_admin() || $load_in_admin ) {
239
			include_once( dirname( __FILE__ ) . '/Pods_Templates_Auto_Template_Front_End.php' );
240
241
			// Only instantiate if we haven't already
242
			if ( is_null( $this->front_end_class ) ) {
243
				$this->front_end_class = new Pods_Templates_Auto_Template_Front_End();
244
			}
245
246
			return $this->front_end_class;
247
		}
248
249
	}
250
251
	/**
252
	 * Reset the transients for front-end class when Pods are saved.
253
	 *
254
	 * @uses update_option hook
255
	 *
256
	 * @param string $option
257
	 * @param mixed $old_value
258
	 * @param mixed $value
259
	 *
260
	 * @since 2.5.5
261
	 */
262
	function reset( $option, $old_value, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $old_value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
263
264
		if ( $option === '_transient_pods_flush_rewrites' ) {
265
			$this->reseter();
266
		}
267
268
	}
269
270
271
	/**
272
	 * Delete transients that stores the settings.
273
	 *
274
	 * @since 2.5.5
275
	 */
276
	function reseter() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
277
278
		$keys = array( 'pods_pfat_the_pods', 'pods_pfat_auto_pods', 'pods_pfat_archive_test' );
279
		foreach( $keys as $key ) {
280
			pods_transient_clear( $key );
281
		}
282
283
	}
284
285
	/**
286
	 * Test if archive is set for post types that don't have archives.
287
	 *
288
	 * @return bool|mixed|null|void
289
	 *
290
	 * @since 2.4.5
291
	 */
292
	function archive_test() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
293
294
		//try to get cached results of this method
295
		$key = 'pods_pfat_archive_test';
296
		$archive_test = pods_transient_get( $key );
297
298
		if ( $archive_test === false ) {
299
			$front = $this->front_end( true );
300
			$auto_pods = $front->auto_pods();
301
302
			foreach ( $auto_pods as $name => $pod ) {
303
				if ( ! $pod[ 'has_archive' ] && $pod[ 'archive' ] && $pod[ 'type' ] !== 'taxonomy' && ! in_array( $name, array( 'post', 'page', 'attachment' ) ) ) {
304
					$archive_test[ $pod[ 'label' ] ] = 'fail';
305
				}
306
307
			}
308
309
			pods_transient_set( $key, $archive_test );
310
311
		}
312
313
		return $archive_test;
314
315
	}
316
317
	/**
318
	 * Throw admin warnings for post types that have archive templates set, but don't support archives
319
	 *
320
	 * @since 2.4.5
321
	 */
322
	function archive_warning() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
323
324
		//create $page variable to check if we are on pods admin page
325
		$page = pods_v( 'page','get', false, true );
326
327
		//check if we are on Pods Admin page
328
		if ( $page === 'pods' ) {
329
			$archive_test = $this->archive_test();
330
			if ( is_array( $archive_test ) ) {
331
				foreach ( $archive_test as $label => $test ) {
332
					if ( $test === 'fail' ) {
333
						echo sprintf( '<div id="message" class="error"><p>%s</p></div>',
334
							sprintf(
335
								__( 'The Pods post type %1$s has an archive template set to be displayed using Pods auto template, but the Pod does not have an archive. You can enable post type archives in the "Advanced Options" tab.', 'pfat' ),
336
								$label )
337
						);
338
					}
339
340
				}
341
342
			}
343
344
		}
345
346
	}
347
348
	/**
349
	 * Get titles of all Pods Templates
350
	 *
351
	 * @return string[] Array of template names
352
	 *
353
	 * @since 2.4.5
354
	 */
355
	public function get_template_titles() {
356
357
		static $template_titles;
358
359
		if ( empty( $template_titles ) ) {
360
            $all_templates = (array) pods_api()->load_templates( array() );
361
362
			$template_titles = array();
363
364
            foreach ( $all_templates as $template ) {
365
                $template_titles[] = $template['name'];
366
            }
367
		}
368
369
		return $template_titles;
370
371
	}
372
373
374
}
375