Stencil_Page_Hook_Custom::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Custom page hook
4
 *
5
 * @package Stencil\PageHook
6
 */
7
8
/**
9
 * Class Stencil_Page_Hook_Custom
10
 */
11
class Stencil_Page_Hook_Custom extends Stencil_Abstract_Page_Type {
12
13
	/**
14
	 * Exection hook
15
	 *
16
	 * @param Stencil_Interface $controller Controller that initiated this class.
17
	 */
18
	public function execute( Stencil_Interface $controller ) {
19
		add_filter( Stencil_Environment::format_filter( 'views-custom' ), array( $this, 'set_template_as_first_option' ) );
20
	}
21
22
	/**
23
	 * Prepend {page_template} on the view stack for custom page
24
	 *
25
	 * @param array $options Current options.
26
	 *
27
	 * @return mixed
28
	 */
29
	public function set_template_as_first_option( $options ) {
30
		$page_template = get_page_template();
31
32
		if ( $page_template ) {
33
			$page_template_base = basename( $page_template );
34
			$page_template_pure = str_replace( '.php', '', $page_template_base );
35
36
			// Reversed order; first look for 'custom', then 'single'.
37
			array_unshift( $options, 'single/' . $page_template_pure );
38
			array_unshift( $options, 'custom/' . $page_template_pure );
39
		}
40
41
		return $options;
42
	}
43
}
44