Passed
Push — master ( 790631...6c357f )
by Jip
03:07
created

Stencil_Flow::add_to_options()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4286
cc 3
eloc 10
nc 3
nop 2
1
<?php
2
/**
3
 * Flow controller class
4
 *
5
 * Builds all the actions that need to be executed on a certain page request.
6
 * Builds the view hierarchy of a certain page.
7
 *
8
 * @package Stencil
9
 */
10
11
/**
12
 * Class Flow
13
 */
14
class Stencil_Flow implements Stencil_Flow_Interface {
15
16
	/**
17
	 * Pages that are considered of the 'archive' type
18
	 *
19
	 * Allows for view to fall through to 'archive'
20
	 * Allows for listing posts
21
	 *
22
	 * @var array
23
	 */
24
	protected $archive_pages = array(
25
		'author',
26
		'category',
27
		'taxonomy',
28
		'date',
29
		'tag',
30
		'archive',
31
		'post-type-archive',
32
	);
33
34
	/**
35
	 * Create a list of actions for the specified page
36
	 *
37
	 * @param string $page Page to get Actions for.
38
	 *
39
	 * @return array of actions
40
	 */
41
	public function get_page_actions( $page ) {
42
		/**
43
		 * Actions to be executed
44
		 */
45
		$actions = array(
46
			'Always'
47
		);
48
49
		// Singular = single item (post, attachment, page, etc).
50
		if ( is_singular() ) {
51
			$actions[] = 'Singular';
52
		}
53
54
		// Paged page (pagination).
55
		if ( is_paged() ) {
56
			$actions[] = 'Paged';
57
		}
58
59
		if ( is_sticky() ) {
60
			$actions[] = 'Sticky';
61
		}
62
63
		if ( is_user_logged_in() ) {
64
			$actions[] = 'LoggedIn';
65
		}
66
67
		if ( in_array( $page, $this->archive_pages, true ) || 'home' === $page ) {
68
			$actions[] = 'Archived';
69
		}
70
71
		// Add page action.
72
		$actions[] = $page;
73
74
		// Apply filters.
75
		$actions = Stencil_Environment::filter( 'actions-' . $page, $actions );
76
		$actions = Stencil_Environment::filter( 'actions', $actions );
77
78
		return $actions;
79
	}
80
81
	/**
82
	 * Determine which view is available for loading
83
	 *
84
	 * @param string $page Page to get hierarchy of.
85
	 *
86
	 * @return array
87
	 * @throws Exception When handler doesn't return expected type.
88
	 */
89
	public function get_view_hierarchy( $page ) {
90
91
		/**
92
		 * Get the possible views for specified page:
93
		 */
94
		$options = Stencil_Handler_Factory::get_hierarchy_handler( $page );
95
		if ( ! is_array( $options ) && ! ( $options instanceof Traversable ) ) {
96
			throw new Exception( 'Expected array got ' . gettype( $options ) );
97
		}
98
99
		if ( is_array( $options ) ) {
100
			$options = new ArrayIterator( $options );
101
		}
102
103
		/**
104
		 * Add archive option for archive pages:
105
		 */
106
		if ( 'archived' !== $page && in_array( $page, $this->archive_pages, true ) ) {
107
			$this->add_to_options( 'archived', $options );
108
		}
109
110
		/**
111
		 * Add paged option for paged pages:
112
		 */
113
		if ( 'paged' !== $page && is_paged() ) {
114
			$this->add_to_options( 'paged', $options );
115
		}
116
117
		/**
118
		 * Convert to array for filtering and return ouput
119
		 */
120
		$options = iterator_to_array( $options );
121
122
		// Apply filter.
123
		$options = Stencil_Environment::filter( 'views-' . $page, $options );
124
125
		return $options;
126
	}
127
128
	/**
129
	 * Add additional options to the existing array
130
	 *
131
	 * @param string         $type Type to get options for.
132
	 * @param array|Iterator $options Reference. Existing options.
133
	 *
134
	 * @throws Exception
135
	 */
136
	private function add_to_options( $type, & $options ) {
137
		$additive_options = $this->get_view_hierarchy( $type );
138
139
		if ( array() !== $additive_options ) {
140
			if ( ! ( $options instanceof AppendIterator ) ) {
141
				$all_options = new AppendIterator();
142
				$all_options->append( $options );
143
			} else {
144
				$all_options = $options;
145
			}
146
147
			$all_options->append( new ArrayIterator( $additive_options ) );
148
149
			$options = $all_options;
150
		}
151
	}
152
}
153