Theme::assetUri()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
class Theme
6
{
7
    public $archiveMeta;
8
    public $postMeta;
9
10
    public function __construct(ArchiveMeta $archiveMeta, PostMeta $postMeta)
11
    {
12
        $this->archiveMeta = $archiveMeta;
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 string
38
     */
39
    public function copyright()
40
    {
41
        return __('Copyright', 'castor').' &copy; '.date('Y').', '.get_bloginfo('name');
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function displaySidebar()
48
    {
49
        $conditions = [
50
            is_archive(),
51
            is_home(),
52
            is_single(),
53
        ];
54
55
        $display = in_array(true, $conditions);
56
57
        return apply_filters('castor/display/sidebar', $display);
58
    }
59
60
    /**
61
     * @param string $asset
62
     *
63
     * @return string
64
     */
65
    public function imagePath($asset)
66
    {
67
        return $this->assetPath(castor_app()->imgDir.$asset);
68
    }
69
70
    /**
71
     * @param string $asset
72
     *
73
     * @return string
74
     */
75
    public function imageUri($asset)
76
    {
77
        return $this->assetUri(castor_app()->imgDir.$asset);
78
    }
79
80
    public function pageTitle()
81
    {
82
        foreach (['is_404', 'is_archive', 'is_home', 'is_page', 'is_search'] as $bool) {
83
            if (!$bool()) {
84
                continue;
85
            }
86
            $method = sprintf('get%sTitle', ucfirst(str_replace('is_', '', $bool)));
87
            return $this->$method();
88
        }
89
90
        return get_the_title();
91
    }
92
93
    /**
94
     * @param string|null $path
95
     *
96
     * @return array|string
97
     */
98
    public function paths($path = null)
99
    {
100
        $paths = [
101
            'dir.stylesheet' => get_stylesheet_directory(),
102
            'dir.template' => get_template_directory(),
103
            'dir.upload' => wp_upload_dir()['basedir'],
104
            'uri.stylesheet' => get_stylesheet_directory_uri(),
105
            'uri.template' => get_template_directory_uri(),
106
        ];
107
108
        if (is_null($path)) {
109
            return $paths;
110
        }
111
112
        return array_key_exists($path, $paths)
113
            ? trailingslashit($paths[$path])
114
            : '';
115
    }
116
117
    /**
118
     * @param string|null $path
119
     * @param string $class
120
     * @return string|null
121
     */
122
    public function svg($path = null, $class = '')
123
    {
124
        if (!file_exists($this->imagePath($path))) {
125
            return;
126
        }
127
        $svg = file_get_contents($this->imagePath($path));
128
        $pattern = '/(<svg.+)( class=[\'\"](|[^\'\"]+)[\'\"])(>.+)/i';
129
        if (1 === preg_match($pattern, $svg, $matches)) {
130
            $class .= ' '.$matches[3];
131
            $svg = preg_filter($pattern, '$1$4', $svg);
132
        }
133
        return str_replace('<svg', '<svg class="'.trim($class).'"', $svg);
134
    }
135
136
    protected function get404Title()
137
    {
138
        return __('Not Found', 'castor');
139
    }
140
141
    protected function getArchiveTitle()
142
    {
143
        return $this->archiveMeta->get('title', get_the_archive_title(), get_query_var('post_type'));
144
    }
145
146
    protected function getHomeTitle()
147
    {
148
        return ($home = (string) get_option('page_for_posts'))
149
            ? get_the_title($home)
0 ignored issues
show
Bug introduced by
$home of type string is incompatible with the type WP_Post|integer expected by parameter $post of get_the_title(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            ? get_the_title(/** @scrutinizer ignore-type */ $home)
Loading history...
150
            : get_the_archive_title();
151
    }
152
153
    protected function getPageTitle()
154
    {
155
        return $this->postMeta->get('title', [
156
            'fallback' => get_the_title(),
157
        ]);
158
    }
159
160
    protected function getSearchTitle()
161
    {
162
        return sprintf(__('Search Results for %s', 'castor'), get_search_query());
163
    }
164
}
165