Completed
Push — develop ( 13bd87...d494ed )
by Paul
02:08
created

Theme::svg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
use GeminiLabs\Castor\Helpers\PostMeta;
6
7
class Theme
8
{
9
	public $postmeta;
10
11
	public function __construct( PostMeta $postmeta )
12
	{
13
		$this->postmeta = $postmeta;
14
	}
15
16
	/**
17
	 * @param string $asset
18
	 *
19
	 * @return string
20
	 */
21
	public function assetPath( $asset )
22
	{
23
		return $this->paths( 'dir.stylesheet' ) . 'assets/' . $asset;
24
	}
25
26
	/**
27
	 * @param string $asset
28
	 *
29
	 * @return string
30
	 */
31
	public function assetUri( $asset )
32
	{
33
		return $this->paths( 'uri.stylesheet' ) . 'assets/' . $asset;
34
	}
35
36
	/**
37
	 * @return bool
38
	 */
39
	public function displaySidebar()
40
	{
41
		$conditions = [
42
			is_archive(),
43
			is_home(),
44
			is_single(),
45
		];
46
47
		$display = in_array( true, $conditions );
48
49
		return apply_filters( 'castor/display/sidebar', $display );
50
	}
51
52
	/**
53
	 * @param string $asset
54
	 *
55
	 * @return string
56
	 */
57
	public function imagePath( $asset )
58
	{
59
		return $this->paths( 'dir.stylesheet' ) . 'assets/img/' . $asset;
60
	}
61
62
	/**
63
	 * @param string $asset
64
	 *
65
	 * @return string
66
	 */
67
	public function imageUri( $asset )
68
	{
69
		return $this->paths( 'uri.stylesheet' ) . 'assets/img/' . $asset;
70
	}
71
72
	public function pageTitle()
73
	{
74
		foreach( ['is_404', 'is_archive', 'is_home', 'is_page', 'is_search'] as $bool ) {
75
			if( !$bool() )continue;
76
			$method = sprintf( 'get%sTitle', ucfirst( str_replace( 'is_', '', $bool )));
77
			return $this->$method();
78
		}
79
80
		return get_the_title();
81
	}
82
83
	/**
84
	 * @param null|string $path
85
	 *
86
	 * @return array|string
87
	 */
88
	public function paths( $path = null )
89
	{
90
		$paths = [
91
			'dir.stylesheet' => get_stylesheet_directory(),
92
			'dir.template'   => get_template_directory(),
93
			'dir.upload'     => wp_upload_dir()['basedir'],
94
			'uri.stylesheet' => get_stylesheet_directory_uri(),
95
			'uri.template'   => get_template_directory_uri(),
96
		];
97
98
		if( is_null( $path )) {
99
			return $paths;
100
		}
101
102
		return array_key_exists( $path, $paths )
103
			? trailingslashit( $paths[$path] )
104
			: '';
105
	}
106
107
	/**
108
	 * @param null|string $path
109
	 *
110
	 * @return string|null
111
	 */
112
	public function svg( $path = null )
113
	{
114
		if( $svg = file_get_contents( $this->imageUri( $path ))) {
115
			return $svg;
116
		}
117
	}
118
119
	protected function get404Title()
120
	{
121
		return __( 'Not Found', 'castor' );
122
	}
123
124
	protected function getArchiveTitle()
125
	{
126
		return get_the_archive_title();
127
	}
128
129
	protected function getHomeTitle()
130
	{
131
		return ( $home = get_option( 'page_for_posts', true ))
132
			? get_the_title( $home )
133
			: __( 'Latest Posts', 'castor' );
134
	}
135
136
	protected function getPageTitle()
137
	{
138
		return ($title = $this->postmeta->get( 'title' ))
139
			? $title
140
			: get_the_title();
141
	}
142
143
	protected function getSearchTitle()
144
	{
145
		return sprintf( __( 'Search Results for %s', 'castor' ), get_search_query() );
146
	}
147
}
148