Completed
Pull Request — 2.x (#4768)
by Scott Kingsley
10:16
created

Pods_Templates_Auto_Template_Settings   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 404
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 404
rs 9
c 0
b 0
f 0
wmc 35
lcom 2
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A init() 0 9 2
A tab() 0 7 1
C options() 0 167 8
A front_end() 0 14 4
A reset() 0 7 2
A reseter() 0 8 2
C archive_test() 0 29 7
B archive_warning() 0 18 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
		// Add option tab for post types
42
		add_filter( 'pods_admin_setup_edit_tabs_post_type', array( $this, 'tab' ), 11, 3 );
43
44
		// add the same tab for taxonomies
45
		add_filter( 'pods_admin_setup_edit_tabs_taxonomy', array( $this, 'tab' ), 11, 3 );
46
47
		// Add options to the new tab
48
		add_filter( 'pods_admin_setup_edit_options', array( $this, 'options' ), 12, 2 );
49
50
		// Include and init front-end class
51
		add_action( 'init', array( $this, 'front_end' ), 25 );
52
53
		// Delete transients when Pods settings are updated.
54
		add_action( 'update_option', array( $this, 'reset' ), 21, 3 );
55
56
		// admin notice for archives without archives
57
		add_action( 'admin_notices', array( $this, 'archive_warning' ) );
58
59
	}
60
61
	/**
62
	 * Initializes the class
63
	 *
64
	 * @since 2.5.5
65
	 */
66
	public function init() {
67
68
		if ( ! is_null( $this->instance ) ) {
69
			$this->instance = new self();
70
		}
71
72
		return $this->instance;
73
74
	}
75
76
	/**
77
	 * The Frontier Auto Display option tab.
78
	 *
79
	 * @param array $tabs
80
	 * @param array $pod
81
	 * @param array $addtl_args
82
	 *
83
	 * @return array
84
	 *
85
	 * @since 2.5.5
86
	 */
87
	public function tab( $tabs, $pod, $addtl_args ) {
88
89
		$tabs['pods-pfat'] = __( 'Auto Template Options', 'pods' );
90
91
		return $tabs;
92
93
	}
94
95
	/**
96
	 * Adds options for this plugin under the Frontier Auto Template tab.
97
	 *
98
	 * @param array $options Tab options.
99
	 * @param array $pod     Pod options.
100
	 *
101
	 * @return array
102
	 *
103
	 * @since 2.5.5
104
	 */
105
	public function options( $options, $pod ) {
106
107
		// check if it's a post type pod and add fields for that.
108
		if ( $pod['type'] === 'post_type' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
109
			$options['pods-pfat'] = array(
110
				'pfat_enable'           => array(
111
					'label'             => __( 'Enable Automatic Pods Templates for this Pod?', 'pods' ),
112
					'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' ),
113
					'type'              => 'boolean',
114
					'default'           => false,
115
					'dependency'        => true,
116
					'boolean_yes_label' => '',
117
				),
118
				'pfat_run_outside_loop' => array(
119
					'label'             => __( 'Execute Auto Template outside of the WordPress loop? (advanced)', 'pods' ),
120
					'help'              => __( 'When enabled, the template will be executed whenever the specified filter is called.', 'pods' ),
121
					'type'              => 'boolean',
122
					'default'           => false,
123
					'depends-on'        => array( 'pfat_enable' => true ),
124
					'boolean_yes_label' => '',
125
				),
126
				'pfat_single'           => array(
127
					'label'      => __( 'Single item view template', 'pods' ),
128
					'help'       => __( 'Name of Pods template to use for single item view.', 'pods' ),
129
					'type'       => 'text',
130
					'default'    => false,
131
					'depends-on' => array( 'pfat_enable' => true ),
132
				),
133
				'pfat_append_single'    => array(
134
					'label'      => __( 'Single Template Location', 'pods' ),
135
					'help'       => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),
136
					'depends-on' => array( 'pfat_enable' => true ),
137
				),
138
				'pfat_filter_single'    => array(
139
					'label'      => __( 'Single Template Filter', 'pods' ),
140
					'help'       => __( 'Which filter to use for single views.', 'pods' ),
141
					'default'    => 'the_content',
142
					'type'       => 'text',
143
					'depends-on' => array( 'pfat_enable' => true ),
144
				),
145
				'pfat_archive'          => array(
146
					'label'      => __( 'Archive view template', 'pods' ),
147
					'help'       => __( 'Name of Pods template to use for use in this Pods archive pages.', 'pods' ),
148
					'type'       => 'text',
149
					'default'    => false,
150
					'depends-on' => array( 'pfat_enable' => true ),
151
				),
152
				'pfat_append_archive'   => array(
153
					'label'      => __( 'Archive Template Location', 'pods' ),
154
					'help'       => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),
155
					'depends-on' => array( 'pfat_enable' => true ),
156
				),
157
				'pfat_filter_archive'   => array(
158
					'label'      => __( 'Archive Template Filter', 'pods' ),
159
					'help'       => __( 'Which filter to use for archives.', 'pods' ),
160
					'default'    => 'the_content',
161
					'type'       => 'text',
162
					'depends-on' => array( 'pfat_enable' => true ),
163
				),
164
			);
165
		}//end if
166
167
		// check if it's a taxonomy Pod, if so add fields for that
168
		if ( $pod['type'] === 'taxonomy' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
169
			$options['pods-pfat'] = array(
170
				'pfat_enable'           => array(
171
					'label'             => __( 'Enable Automatic Pods Templates for this Taxonomy Pod?', 'pods' ),
172
					'help'              => __( 'When enabled you can specify the names of a Pods Template to be used to display information about this taxonomy and/or posts in this taxonomy in the front-end.', 'pods' ),
173
					'type'              => 'boolean',
174
					'default'           => false,
175
					'dependency'        => true,
176
					'boolean_yes_label' => '',
177
				),
178
				'pfat_run_outside_loop' => array(
179
					'label'             => __( 'Execute Auto Template outside of the WordPress loop? (advanced)', 'pods' ),
180
					'help'              => __( 'When enabled, the template will be executed whenever the specified filter is called.', 'pods' ),
181
					'type'              => 'boolean',
182
					'default'           => false,
183
					'depends-on'        => array( 'pfat_enable' => true ),
184
					'boolean_yes_label' => '',
185
				),
186
				'pfat_single'           => array(
187
					'label'      => __( 'Taxonomy Template', 'pods' ),
188
					'help'       => __( 'Name of Pods template to use to present this taxonomy object itself.', 'pods' ),
189
					'type'       => 'text',
190
					'default'    => false,
191
					'depends-on' => array( 'pfat_enable' => true ),
192
				),
193
				'pfat_append_single'    => array(
194
					'label'      => __( 'Template Location', 'pods' ),
195
					'help'       => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),
196
					'depends-on' => array( 'pfat_enable' => true ),
197
				),
198
				'pfat_filter_single'    => array(
199
					'label'      => __( 'Taxonomy Template Filter', 'pods' ),
200
					'help'       => __( 'Which filter to use for taxonomy view.', 'pods' ),
201
					'default'    => 'get_the_archive_description',
202
					'type'       => 'text',
203
					'depends-on' => array( 'pfat_enable' => true ),
204
				),
205
				'pfat_archive'          => array(
206
					'label'      => __( 'Taxonomy Archive Template', 'pods' ),
207
					'help'       => __( 'Name of Pods template to use for posts in this taxonomy.', 'pods' ),
208
					'type'       => 'text',
209
					'default'    => false,
210
					'depends-on' => array( 'pfat_enable' => true ),
211
				),
212
				'pfat_append_archive'   => array(
213
					'label'      => __( 'Archive Template Location', 'pods' ),
214
					'help'       => __( 'Whether the template will go before, after or in place of the post content.', 'pods' ),
215
					'depends-on' => array( 'pfat_enable' => true ),
216
				),
217
				'pfat_filter_archive'   => array(
218
					'label'      => __( 'Archive Template Filter', 'pods' ),
219
					'help'       => __( 'Which filter to use for archives.', 'pods' ),
220
					'default'    => 'the_content',
221
					'type'       => 'text',
222
					'depends-on' => array( 'pfat_enable' => true ),
223
				),
224
			);
225
		}//end if
226
227
		if ( isset( $options['pods-pfat'] ) ) {
228
229
			// field options pick values
230
			$pick = array(
231
				'type'               => 'pick',
232
				'pick_format_type'   => 'single',
233
				'pick_format_single' => 'dropdown',
234
				'default'            => 'true',
235
			);
236
237
			// get template titles
238
			$titles = $this->get_template_titles();
239
240
			if ( ! empty( $titles ) ) {
241
				foreach ( $pick as $k => $v ) {
242
					$options['pods-pfat']['pfat_single'][ $k ] = $v;
243
244
					$options['pods-pfat']['pfat_archive'][ $k ] = $v;
245
246
				}
247
248
				$options['pods-pfat']['pfat_archive']['data'] = array( null => __( 'No Archive view template', 'pods' ) ) + ( array_combine( $this->get_template_titles(), $this->get_template_titles() ) );
249
				$options['pods-pfat']['pfat_single']['data']  = array( null => __( 'No view template', 'pods' ) ) + array_combine( $this->get_template_titles(), $this->get_template_titles() );
250
			}
251
252
			// Add data to $pick for template location
253
			unset( $pick['data'] );
254
			$location_data = array(
255
				'append'  => __( 'After', 'pods' ),
256
				'prepend' => __( 'Before', 'pods' ),
257
				'replace' => __( 'Replace', 'pods' ),
258
			);
259
			$pick['data']  = $location_data;
260
261
			// add location options to fields without type set.
262
			foreach ( $options['pods-pfat'] as $k => $option ) {
263
				if ( ! isset( $option['type'] ) ) {
264
					$options['pods-pfat'][ $k ] = array_merge( $option, $pick );
265
				}
266
			}
267
		}//end if
268
269
		return $options;
270
271
	}
272
273
	/**
274
	 * Include/ init the front end class on the front end only
275
	 *
276
	 * @param bool $load_in_admin Optional. Whether to load in admin. Default is false.
277
	 *
278
	 * @return Pods_PFAT_Frontend
0 ignored issues
show
Documentation introduced by
Should the return type not be Pods_Templates_Auto_Template_Front_End|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
279
	 *
280
	 * @since 2.5.5
281
	 */
282
	public function front_end( $load_in_admin = false ) {
283
284
		if ( ! is_admin() || $load_in_admin ) {
285
			include_once dirname( __FILE__ ) . '/Pods_Templates_Auto_Template_Front_End.php';
286
287
			// Only instantiate if we haven't already
288
			if ( is_null( $this->front_end_class ) ) {
289
				$this->front_end_class = new Pods_Templates_Auto_Template_Front_End();
290
			}
291
292
			return $this->front_end_class;
293
		}
294
295
	}
296
297
	/**
298
	 * Reset the transients for front-end class when Pods are saved.
299
	 *
300
	 * @uses  update_option hook
301
	 *
302
	 * @param string $option
303
	 * @param mixed  $old_value
304
	 * @param mixed  $value
305
	 *
306
	 * @since 2.5.5
307
	 */
308
	public function reset( $option, $old_value, $value ) {
309
310
		if ( $option === '_transient_pods_flush_rewrites' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
311
			$this->reseter();
312
		}
313
314
	}
315
316
	/**
317
	 * Delete transients that stores the settings.
318
	 *
319
	 * @since 2.5.5
320
	 */
321
	public function reseter() {
322
323
		$keys = array( 'pods_pfat_the_pods', 'pods_pfat_auto_pods', 'pods_pfat_archive_test' );
324
		foreach ( $keys as $key ) {
325
			pods_transient_clear( $key );
326
		}
327
328
	}
329
330
	/**
331
	 * Test if archive is set for post types that don't have archives.
332
	 *
333
	 * @return bool|mixed|null|void
334
	 *
335
	 * @since 2.4.5
336
	 */
337
	public function archive_test() {
338
339
		// try to get cached results of this method
340
		$key          = 'pods_pfat_archive_test';
341
		$archive_test = pods_transient_get( $key );
342
343
		if ( $archive_test === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
344
			$front     = $this->front_end( true );
345
			$auto_pods = $front->auto_pods();
346
347
			foreach ( $auto_pods as $name => $pod ) {
348
				if ( ! $pod['has_archive'] && $pod['archive'] && $pod['type'] !== 'taxonomy' && ! in_array(
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
349
					$name, array(
350
						'post',
351
						'page',
352
						'attachment',
353
					), true
354
				) ) {
355
					$archive_test[ $pod['label'] ] = 'fail';
356
				}
357
			}
358
359
			pods_transient_set( $key, $archive_test );
360
361
		}
362
363
		return $archive_test;
364
365
	}
366
367
	/**
368
	 * Throw admin warnings for post types that have archive templates set, but don't support archives
369
	 *
370
	 * @since 2.4.5
371
	 */
372
	public function archive_warning() {
373
374
		// create $page variable to check if we are on pods admin page
375
		$page = pods_v( 'page', 'get', false, true );
376
377
		// check if we are on Pods Admin page
378
		if ( $page === 'pods' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
379
			$archive_test = $this->archive_test();
380
			if ( is_array( $archive_test ) ) {
381
				foreach ( $archive_test as $label => $test ) {
382
					if ( $test === 'fail' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
383
						echo sprintf( '<div id="message" class="error"><p>%s</p></div>', sprintf( __( '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' ), $label ) );
384
					}
385
				}
386
			}
387
		}
388
389
	}
390
391
	/**
392
	 * Get titles of all Pods Templates
393
	 *
394
	 * @return string[] Array of template names
395
	 *
396
	 * @since 2.4.5
397
	 */
398
	public function get_template_titles() {
399
400
		static $template_titles;
401
402
		if ( empty( $template_titles ) ) {
403
			$all_templates = (array) pods_api()->load_templates( array() );
404
405
			$template_titles = array();
406
407
			foreach ( $all_templates as $template ) {
408
				$template_titles[] = $template['name'];
409
			}
410
		}
411
412
		return $template_titles;
413
414
	}
415
416
}
417