Controller::registerCustomizerAssets()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace GeminiLabs\Castor;
4
5
use GeminiLabs\Castor\Facades\Development;
6
use GeminiLabs\Castor\Facades\Template;
7
use GeminiLabs\Castor\Facades\Theme;
8
use GeminiLabs\Castor\Facades\Utility;
9
use WP_Customize_Manager;
10
11
class Controller
12
{
13
    /**
14
     * @return void
15
     * @action after_setup_theme
16
     */
17
    public function afterSetupTheme()
18
    {
19
        castor_app()->cssDir = trailingslashit((string) apply_filters('castor/assets/styles/dir', 'css'));
20
        castor_app()->imgDir = trailingslashit((string) apply_filters('castor/assets/images/dir', 'img'));
21
        castor_app()->jsDir = trailingslashit((string) apply_filters('castor/assets/scripts/dir', 'js'));
22
23
        add_editor_style(Theme::assetUri(castor_app()->cssDir.'editor.css'));
24
        add_theme_support('customize-selective-refresh-widgets');
25
        add_theme_support('html5', ['caption', 'comment-form', 'comment-list', 'gallery', 'search-form']);
26
        add_theme_support('post-thumbnails');
27
        add_theme_support('soil-clean-up');
28
        add_theme_support('soil-jquery-cdn');
29
        add_theme_support('soil-nav-walker');
30
        add_theme_support('soil-nice-search');
31
        add_theme_support('soil-relative-urls');
32
        add_theme_support('title-tag');
33
        load_theme_textdomain('castor', Theme::paths('dir.template').'/languages');
34
35
        $menus = apply_filters('castor/register/nav_menus', [
36
            'main_menu' => __('Main Menu', 'castor'),
37
        ]);
38
39
        foreach ($menus as $location => $description) {
40
            register_nav_menu($location, $description);
41
        }
42
    }
43
44
    /**
45
     * @return array
46
     * @filter body_class
47
     */
48
    public function filterBodyClasses(array $classes)
49
    {
50
        if (Theme::displaySidebar()) {
51
            $classes[] = 'has-sidebar';
52
        }
53
        return array_keys(array_flip($classes));
54
    }
55
56
    /**
57
     * @return string
58
     * @filter login_headertext
59
     */
60
    public function filterLoginTitle()
61
    {
62
        return get_bloginfo('name');
63
    }
64
65
    /**
66
     * @return string
67
     * @filter login_headerurl
68
     */
69
    public function filterLoginUrl()
70
    {
71
        return get_bloginfo('url');
72
    }
73
74
    /**
75
     * @return string
76
     * @filter template_include
77
     */
78
    public function filterTemplate($template)
79
    {
80
        if (is_string($template)) {
81
            $template = Template::setLayout($template);
82
            Development::storeTemplatePath($template);
83
        }
84
        return $template;
85
    }
86
87
    /**
88
     * @return array
89
     * @filter {$type}_template_hierarchy
90
     */
91
    public function filterTemplateHierarchy(array $templates)
92
    {
93
        return array_map(function ($template) {
94
            return Utility::startWith('templates/', $template);
95
        }, $templates);
96
    }
97
98
    /**
99
     * @return void
100
     * @action admin_head
101
     * @action login_head
102
     */
103
    public function loadAdminFavicon()
104
    {
105
        if (file_exists(Theme::assetPath('favicon/favicon-admin.ico'))) {
106
            printf('<link rel="shortcut icon" href="%s">', Theme::assetUri('favicon/favicon-admin.ico'));
107
        }
108
    }
109
110
    /**
111
     * @return void
112
     * @action login_head
113
     */
114
    public function login()
115
    {
116
        if (file_exists(Theme::assetPath(castor_app()->cssDir.'login.css'))) {
117
            printf('<link rel="stylesheet" href="%s">', Theme::assetUri(castor_app()->cssDir.'login.css'));
118
        }
119
    }
120
121
    /**
122
     * @return void
123
     * @action admin_enqueue_scripts
124
     */
125
    public function registerAdminAssets()
126
    {
127
        if (file_exists(Theme::assetPath(castor_app()->cssDir.'admin.css'))) {
128
            wp_enqueue_style('castor/admin.css',
129
                Theme::assetUri(castor_app()->cssDir.'admin.css'),
130
                apply_filters('castor/enqueue/admin/css/deps', []),
131
                CASTOR_FRAMEWORK_VERSION
132
            );
133
        }
134
        if (file_exists(Theme::assetPath(castor_app()->jsDir.'admin.js'))) {
135
            wp_enqueue_script('castor/admin.js',
136
                Theme::assetUri(castor_app()->jsDir.'admin.js'),
137
                apply_filters('castor/enqueue/admin/js/deps', []),
138
                CASTOR_FRAMEWORK_VERSION,
139
                true
140
            );
141
        }
142
    }
143
144
    /**
145
     * @return void
146
     * @action wp_enqueue_scripts
147
     */
148
    public function registerAssets()
149
    {
150
        wp_register_style('castor/main.css',
151
            Theme::assetUri(castor_app()->cssDir.'main.css'),
152
            apply_filters('castor/enqueue/css/deps', []),
153
            CASTOR_FRAMEWORK_VERSION
154
        );
155
        wp_register_script('castor/main.js',
156
            Theme::assetUri(castor_app()->jsDir.'main.js'),
157
            apply_filters('castor/enqueue/js/deps', []),
158
            CASTOR_FRAMEWORK_VERSION,
159
            true
160
        );
161
        wp_localize_script('castor/main.js', apply_filters('castor/enqueue/js/localize/variable', 'globals'),
162
            apply_filters('castor/enqueue/js/localize/variables', [
163
                'ajax' => admin_url('admin-ajax.php'),
164
            ])
165
        );
166
        do_action('castor/register/assets');
167
        wp_enqueue_style('castor/main.css');
168
        wp_enqueue_script('castor/main.js');
169
    }
170
171
    /**
172
     * @return void
173
     * @action customize_register
174
     */
175
    public function registerCustomizer(WP_Customize_Manager $manager)
176
    {
177
        $manager->get_setting('blogname')->transport = 'postMessage';
178
        $manager->selective_refresh->add_partial('blogname', [
179
            'selector' => '.brand',
180
            'render_callback' => function () {
181
                bloginfo('name');
182
            },
183
        ]);
184
    }
185
186
    /**
187
     * @return void
188
     * @action customize_preview_init
189
     */
190
    public function registerCustomizerAssets()
191
    {
192
        wp_enqueue_script('castor/customizer.js', Theme::assetUri(castor_app()->jsDir.'customizer.js'), ['customize-preview'], CASTOR_FRAMEWORK_VERSION, true);
193
    }
194
195
    /**
196
     * @return void
197
     * @action widgets_init
198
     */
199
    public function registerSidebars()
200
    {
201
        $defaults = apply_filters('castor/register/sidebars/defaults', [
202
            'before_widget' => '<div class="widget %1$s %2$s">',
203
            'after_widget' => '</div>',
204
            'before_title' => '<h4>',
205
            'after_title' => '</h4>',
206
        ]);
207
208
        $sidebars = apply_filters('castor/register/sidebars', [
209
            'sidebar-primary' => __('Primary Sidebar', 'castor'),
210
            'sidebar-footer' => __('Footer Widgets', 'castor'),
211
        ]);
212
213
        foreach ($sidebars as $id => $name) {
214
            register_sidebar([
215
                'id' => $id,
216
                'name' => $name,
217
            ] + $defaults);
218
        }
219
    }
220
}
221