TimberTheme::path()   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 0
1
<?php
2
3
/**
4
 * Need to display info about your theme? Well you've come to the right place. By default info on the current theme comes for free with what's fetched by `Timber::get_context()` in which case you can access it your theme like so:
5
 * @example
6
 * ```php
7
 * <?php
8
 * $context = Timber::get_context();
9
 * Timber::render('index.twig', $context);
10
 * ?>
11
 * ```
12
 * ```twig
13
 * <script src="{{theme.link}}/static/js/all.js"></script>
14
 * ```
15
 * ```html
16
 * <script src="http://example.org/wp-content/themes/my-theme/static/js/all.js"></script>
17
 * ```
18
 * @package Timber
19
 */
20
class TimberTheme extends TimberCore {
21
22
	/**
23
	 * @api
24
	 * @var string the human-friendly name of the theme (ex: `My Timber Starter Theme`)
25
	 */
26
	public $name;
27
28
	/**
29
	 * @api
30
	 * @var TimberTheme|bool the TimberTheme object for the parent theme (if it exists), false otherwise
31
	 */
32
	public $parent = false;
33
34
	/**
35
	 * @api
36
	 * @var string the slug of the parent theme (ex: `_s`)
37
	 */
38
	public $parent_slug;
39
40
	/**
41
	 * @api
42
	 * @var string the slug of the theme (ex: `my-super-theme`)
43
	 */
44
	public $slug;
45
	public $uri;
46
47
	/**
48
	 * Constructs a new TimberTheme object. NOTE the TimberTheme object of the current theme comes in the default `Timber::get_context()` call. You can access this in your twig template via `{{site.theme}}.
49
	 * @param string $slug
0 ignored issues
show
Documentation introduced by
Should the type for parameter $slug not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
50
	 * @example
51
	 * ```php
52
	 * <?php
53
	 *     $theme = new TimberTheme("my-theme");
54
	 *     $context['theme_stuff'] = $theme;
55
	 *     Timber::render('single.')
56
	 * ?>
57
	 * ```
58
	 * ```twig
59
	 * We are currently using the {{ theme_stuff.name }} theme.
60
	 * ```
61
	 * ```html
62
	 * We are currently using the My Theme theme.
63
	 * ```
64
	 */
65
	function __construct($slug = null) {
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...
66
		$this->init($slug);
67
	}
68
69
	/**
70
	 * @internal
71
	 * @param string $slug
0 ignored issues
show
Documentation introduced by
Should the type for parameter $slug not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
72
	 */
73
	protected function init($slug = null) {
74
		$data = wp_get_theme($slug);
75
		$this->name = $data->get('Name');
76
		$ss = $data->get_stylesheet();
77
		$this->slug = $ss;
78
79
		if ( ! function_exists( 'get_home_path' ) ) {
80
			require_once(ABSPATH . 'wp-admin/includes/file.php');
81
		}
82
83
		$this->uri = get_stylesheet_directory_uri();
84
		$this->parent_slug = $data->get('Template');
85
		if ( !$this->parent_slug ) {
86
			$this->uri = get_template_directory_uri();
87
		}
88
		if ( $this->parent_slug && $this->parent_slug != $this->slug ) {
89
			$this->parent = new TimberTheme($this->parent_slug);
90
		}
91
	}
92
93
	/**
94
	 * @api
95
	 * @return string the absolute path to the theme (ex: `http://example.org/wp-content/themes/my-timber-theme`)
96
	 */
97
	public function link() {
98
		return $this->uri;
99
	}
100
101
	/**
102
	 * @api
103
	 * @return  string the relative path to the theme (ex: `/wp-content/themes/my-timber-theme`)
104
	 */
105
	public function path() {
106
		return TimberURLHelper::get_rel_url( $this->link() );
107
	}
108
109
	/**
110
	 * @param string $name
111
	 * @param bool $default
112
	 * @return string
113
	 */
114
	public function theme_mod($name, $default = false) {
115
		return get_theme_mod($name, $default);
116
	}
117
118
	/**
119
	 * @return array
120
	 */
121
	public function theme_mods() {
122
		return get_theme_mods();
123
	}
124
125
}
126