Test Failed
Push — develop ( c4a2cb...83a5b5 )
by Paul
07:40
created

MainController::logOnce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Commands\RegisterPostMeta;
6
use GeminiLabs\SiteReviews\Commands\RegisterPostType;
7
use GeminiLabs\SiteReviews\Commands\RegisterShortcodes;
8
use GeminiLabs\SiteReviews\Commands\RegisterTaxonomy;
9
use GeminiLabs\SiteReviews\Commands\RegisterWidgets;
10
use GeminiLabs\SiteReviews\Database\OptionManager;
11
use GeminiLabs\SiteReviews\Database\Tables;
12
use GeminiLabs\SiteReviews\Helpers\Arr;
13
use GeminiLabs\SiteReviews\Install;
14
15
class MainController extends AbstractController
16
{
17
    /**
18
     * switch_to_blog() has run before this hook is triggered.
19
     *
20
     * @see http://developer.wordpress.org/reference/functions/wp_uninitialize_site/
21
     *
22
     * @param string[] $tables
23
     *
24
     * @return string[]
25
     *
26
     * @filter wpmu_drop_tables:999
27
     */
28
    public function filterDropTables(array $tables): array
29
    {
30
        // Custom tables have foreign indexes so they must be removed first!
31
        foreach (glsr(Tables::class)->tables() as $classname) {
32
            $table = glsr($classname);
33
            $tables = Arr::prepend($tables, $table->tablename, $table->name($prefixName = true));
34
        }
35
        return $tables;
36
    }
37
38
    /**
39
     * @action wp_initialize_site:999
40
     */
41
    public function installOnNewSite(\WP_Site $site): void
42
    {
43
        if (is_plugin_active_for_network(glsr()->basename)) {
44
            glsr(Install::class)->runOnSite($site->blog_id);
45
        }
46
    }
47
48
    /**
49
     * @param ?string $data We are not enforcing the type because the "wp_footer" hook does not have a parameter
50
     *
51
     * @action admin_footer
52
     * @action wp_footer
53
     */
54
    public function logOnce($data = ''): void
55
    {
56
        if ('update.php' !== $data) {
57
            glsr_log()->logOnce();
58
        }
59
    }
60
61
    /**
62
     * Initialize the Application settings config and defaults.
63
     *
64
     * @action init:5
65
     */
66
    public function onInit(): void
67
    {
68
        $defaults = glsr()->defaults();
69
        glsr(OptionManager::class)->mergeDefaults($defaults);
70
        glsr(OptionManager::class)->updateVersion();
71
    }
72
73
    /**
74
     * @action site-reviews/migration/end
75
     */
76
    public function onMigrationEnd(): void
77
    {
78
        $settings = glsr(OptionManager::class)->wp(OptionManager::databaseKey(), []);
79
        $settings = glsr(OptionManager::class)->clean($settings);
80
        update_option(OptionManager::databaseKey(), $settings, true);
81
    }
82
83
    /**
84
     * @action parse_query
85
     */
86
    public function parseAssignedPostTypesInQuery(\WP_Query $query): void
87
    {
88
        if (glsr()->prefix.'assigned_posts' !== $query->get('post_type')) {
89
            return;
90
        }
91
        $postTypes = get_post_types([
92
            '_builtin' => false,
93
            'public' => true,
94
            'show_in_rest' => true,
95
            'show_ui' => true,
96
        ]);
97
        $postTypes[] = 'post';
98
        $postTypes[] = 'page';
99
        $query->is_archive = false;
100
        $query->is_post_type_archive = false;
101
        $query->set('post_type', array_map('sanitize_key', array_values($postTypes)));
102
    }
103
104
    /**
105
     * @action plugins_loaded
106
     */
107
    public function registerAddons(): void
108
    {
109
        glsr()->action('addon/register', glsr());
110
    }
111
112
    /**
113
     * Languages are loaded before "init" because the setting config uses translated strings.
114
     *
115
     * @action after_setup_theme
116
     */
117
    public function registerLanguages(): void
118
    {
119
        load_plugin_textdomain(glsr()->id, false,
120
            trailingslashit(plugin_basename(glsr()->path()).'/'.glsr()->languages)
121
        );
122
    }
123
124
    /**
125
     * @action init
126
     */
127
    public function registerPostMeta(): void
128
    {
129
        $this->execute(new RegisterPostMeta());
130
    }
131
132
    /**
133
     * @action init
134
     */
135
    public function registerPostType(): void
136
    {
137
        $this->execute(new RegisterPostType());
138
    }
139
140
    /**
141
     * @action init
142
     */
143
    public function registerReviewTypes(): void
144
    {
145
        $types = glsr()->filterArray('review/types', []);
146
        $types = wp_parse_args($types, [
147
            'local' => _x('Local Review', 'admin-text', 'site-reviews'),
148
        ]);
149
        glsr()->store('review_types', $types);
150
    }
151
152
    /**
153
     * @action init
154
     */
155
    public function registerShortcodes(): void
156
    {
157
        $this->execute(new RegisterShortcodes());
158
    }
159
160
    /**
161
     * @action init
162
     */
163
    public function registerTaxonomy(): void
164
    {
165
        $this->execute(new RegisterTaxonomy());
166
    }
167
168
    /**
169
     * @action widgets_init
170
     */
171
    public function registerWidgets(): void
172
    {
173
        $this->execute(new RegisterWidgets());
174
    }
175
}
176