Passed
Push — master ( 68f720...5e727b )
by Paul
10:53
created

AdminController::enqueueAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Commands\EnqueueAdminAssets;
6
use GeminiLabs\SiteReviews\Commands\ExportRatings;
7
use GeminiLabs\SiteReviews\Commands\ImportRatings;
8
use GeminiLabs\SiteReviews\Commands\RegisterTinymcePopups;
9
use GeminiLabs\SiteReviews\Commands\TogglePinned;
10
use GeminiLabs\SiteReviews\Commands\ToggleStatus;
11
use GeminiLabs\SiteReviews\Database;
12
use GeminiLabs\SiteReviews\Helpers\Arr;
13
use GeminiLabs\SiteReviews\Install;
14
use GeminiLabs\SiteReviews\Modules\Html\Builder;
15
use GeminiLabs\SiteReviews\Modules\Notice;
16
use GeminiLabs\SiteReviews\Modules\Translation;
17
use GeminiLabs\SiteReviews\Request;
18
19
class AdminController extends Controller
20
{
21
    /**
22
     * @return void
23
     * @action site-reviews/export/cleanup
24
     */
25
    public function cleanupAfterExport()
26
    {
27
        glsr(Database::class)->deleteMeta(glsr()->export_key);
28
    }
29
30
    /**
31
     * @return void
32
     * @action admin_enqueue_scripts
33
     */
34
    public function enqueueAssets()
35
    {
36
        $this->execute(new EnqueueAdminAssets());
37
    }
38
39
    /**
40
     * @return array
41
     * @filter plugin_action_links_site-reviews/site-reviews.php
42
     */
43
    public function filterActionLinks(array $links)
44
    {
45
        if (glsr()->hasPermission('settings')) {
46
            $links['settings'] = glsr(Builder::class)->a([
47
                'href' => admin_url('edit.php?post_type='.glsr()->post_type.'&page=settings'),
0 ignored issues
show
Bug introduced by
The function admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

47
                'href' => /** @scrutinizer ignore-call */ admin_url('edit.php?post_type='.glsr()->post_type.'&page=settings'),
Loading history...
48
                'text' => _x('Settings', 'admin-text', 'site-reviews'),
0 ignored issues
show
Bug introduced by
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

48
                'text' => /** @scrutinizer ignore-call */ _x('Settings', 'admin-text', 'site-reviews'),
Loading history...
49
            ]);
50
        }
51
        if (glsr()->hasPermission('documentation')) {
52
            $links['documentation'] = glsr(Builder::class)->a([
53
                'href' => admin_url('edit.php?post_type='.glsr()->post_type.'&page=documentation'),
54
                'text' => _x('Help', 'admin-text', 'site-reviews'),
55
            ]);
56
        }
57
        return $links;
58
    }
59
60
    /**
61
     * @param array $capabilities
62
     * @param string $capability
63
     * @return array
64
     * @filter map_meta_cap
65
     */
66 8
    public function filterCreateCapability($capabilities, $capability)
67
    {
68 8
        if ($capability == 'create_'.glsr()->post_type) {
69
            $capabilities[] = 'do_not_allow';
70
        }
71 8
        return $capabilities;
72
    }
73
74
    /**
75
     * @param array $items
76
     * @return array
77
     * @filter dashboard_glance_items
78
     */
79
    public function filterDashboardGlanceItems($items)
80
    {
81
        $postCount = wp_count_posts(glsr()->post_type);
0 ignored issues
show
Bug introduced by
The function wp_count_posts was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

81
        $postCount = /** @scrutinizer ignore-call */ wp_count_posts(glsr()->post_type);
Loading history...
82
        if (empty($postCount->publish)) {
83
            return $items;
84
        }
85
        $text = _nx('%s Review', '%s Reviews', $postCount->publish, 'admin-text', 'site-reviews');
0 ignored issues
show
Bug introduced by
The function _nx was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

85
        $text = /** @scrutinizer ignore-call */ _nx('%s Review', '%s Reviews', $postCount->publish, 'admin-text', 'site-reviews');
Loading history...
86
        $text = sprintf($text, number_format_i18n($postCount->publish));
0 ignored issues
show
Bug introduced by
The function number_format_i18n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

86
        $text = sprintf($text, /** @scrutinizer ignore-call */ number_format_i18n($postCount->publish));
Loading history...
87
        $items = Arr::consolidate($items);
88
        if (glsr()->can('edit_posts')) {
89
            $items[] = glsr(Builder::class)->a($text, [
90
                'class' => 'glsr-review-count',
91
                'href' => 'edit.php?post_type='.glsr()->post_type,
92
            ]);
93
        } else {
94
            $items[] = glsr(Builder::class)->span($text, [
95
                'class' => 'glsr-review-count',
96
            ]);
97
        }
98
        return $items;
99
    }
100
101
    /**
102
     * @param array $args
103
     * @return array
104
     * @filter export_args
105
     */
106
    public function filterExportArgs($args)
107
    {
108
        if (in_array(Arr::get($args, 'content'), ['all', glsr()->post_type])) {
109
            $this->execute(new ExportRatings(glsr()->args($args)));
110
        }
111
        return $args;
112
    }
113
114
    /**
115
     * @param array $plugins
116
     * @return array
117
     * @filter mce_external_plugins
118
     */
119
    public function filterTinymcePlugins($plugins)
120
    {
121
        if (glsr()->can('edit_posts')) {
122
            $plugins = Arr::consolidate($plugins);
123
            $plugins['glsr_shortcode'] = glsr()->url('assets/scripts/mce-plugin.js');
124
        }
125
        return $plugins;
126
    }
127
128
    /**
129
     * @return void
130
     * @action admin_init
131
     */
132 7
    public function onActivation()
133
    {
134 7
        if (empty(get_option(glsr()->prefix.'activated'))) {
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

134
        if (empty(/** @scrutinizer ignore-call */ get_option(glsr()->prefix.'activated'))) {
Loading history...
135 7
            glsr(Install::class)->run();
136 7
            update_option(glsr()->prefix.'activated', true);
0 ignored issues
show
Bug introduced by
The function update_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

136
            /** @scrutinizer ignore-call */ 
137
            update_option(glsr()->prefix.'activated', true);
Loading history...
137
        }
138 7
    }
139
140
    /**
141
     * @return void
142
     * @action import_end
143
     */
144
    public function onImportEnd()
145
    {
146
        $this->execute(new ImportRatings());
147
    }
148
149
    /**
150
     * @return void
151
     * @action admin_head
152
     */
153
    public function printInlineStyle()
154
    {
155
        echo '<style type="text/css">a[href="edit.php?post_type=site-review&page=addons"]:not(.current):not(:hover) { color:#F6E05E!important; }</style>';
156
    }
157
158
    /**
159
     * @return void
160
     * @action admin_init
161
     */
162 7
    public function registerTinymcePopups()
163
    {
164 7
        $this->execute(new RegisterTinymcePopups([
165 7
            'site_reviews' => _x('Recent Reviews', 'admin-text', 'site-reviews'),
0 ignored issues
show
Bug introduced by
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

165
            'site_reviews' => /** @scrutinizer ignore-call */ _x('Recent Reviews', 'admin-text', 'site-reviews'),
Loading history...
166 7
            'site_reviews_form' => _x('Submit a Review', 'admin-text', 'site-reviews'),
167 7
            'site_reviews_summary' => _x('Summary of Reviews', 'admin-text', 'site-reviews'),
168
        ]));
169 7
    }
170
171
    /**
172
     * @param string $editorId
173
     * @return void|null
174
     * @action media_buttons
175
     */
176
    public function renderTinymceButton($editorId)
177
    {
178
        $allowedEditors = glsr()->filterArray('tinymce/editor-ids', ['content'], $editorId);
179
        if ('post' !== glsr_current_screen()->base || !in_array($editorId, $allowedEditors)) {
180
            return;
181
        }
182
        $shortcodes = [];
183
        foreach (glsr()->retrieve('mce', []) as $shortcode => $values) {
184
            $shortcodes[$shortcode] = $values;
185
        }
186
        if (empty($shortcodes)) {
187
            return;
188
        }
189
        glsr()->render('partials/editor/tinymce', [
190
            'shortcodes' => $shortcodes,
191
        ]);
192
    }
193
194
    /**
195
     * @return void
196
     * @action site-reviews/route/ajax/search-posts
197
     */
198
    public function searchPostsAjax(Request $request)
199
    {
200
        $results = glsr(Database::class)->searchPosts($request->search);
201
        wp_send_json_success([
0 ignored issues
show
Bug introduced by
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

201
        /** @scrutinizer ignore-call */ 
202
        wp_send_json_success([
Loading history...
202
            'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>',
0 ignored issues
show
Bug introduced by
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

202
            'empty' => '<div>'./** @scrutinizer ignore-call */ _x('Nothing found.', 'admin-text', 'site-reviews').'</div>',
Loading history...
203
            'items' => $results,
204
        ]);
205
    }
206
207
    /**
208
     * @return void
209
     * @action site-reviews/route/ajax/search-translations
210
     */
211
    public function searchTranslationsAjax(Request $request)
212
    {
213
        if (empty($request->exclude)) {
214
            $request->exclude = [];
215
        }
216
        $results = glsr(Translation::class)
217
            ->search($request->search)
218
            ->exclude()
219
            ->exclude($request->exclude)
220
            ->renderResults();
221
        wp_send_json_success([
0 ignored issues
show
Bug introduced by
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

221
        /** @scrutinizer ignore-call */ 
222
        wp_send_json_success([
Loading history...
222
            'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>',
0 ignored issues
show
Bug introduced by
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

222
            'empty' => '<div>'./** @scrutinizer ignore-call */ _x('Nothing found.', 'admin-text', 'site-reviews').'</div>',
Loading history...
223
            'items' => $results,
224
        ]);
225
    }
226
227
    /**
228
     * @return void
229
     * @action site-reviews/route/ajax/search-users
230
     */
231
    public function searchUsersAjax(Request $request)
232
    {
233
        $results = glsr(Database::class)->searchUsers($request->search);
234
        wp_send_json_success([
0 ignored issues
show
Bug introduced by
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

234
        /** @scrutinizer ignore-call */ 
235
        wp_send_json_success([
Loading history...
235
            'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>',
0 ignored issues
show
Bug introduced by
The function _x was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

235
            'empty' => '<div>'./** @scrutinizer ignore-call */ _x('Nothing found.', 'admin-text', 'site-reviews').'</div>',
Loading history...
236
            'items' => $results,
237
        ]);
238
    }
239
240
    /**
241
     * @return void
242
     * @action site-reviews/route/ajax/toggle-pinned
243
     */
244
    public function togglePinnedAjax(Request $request)
245
    {
246
        wp_send_json_success([
0 ignored issues
show
Bug introduced by
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

246
        /** @scrutinizer ignore-call */ 
247
        wp_send_json_success([
Loading history...
247
            'notices' => glsr(Notice::class)->get(),
248
            'pinned' => $this->execute(new TogglePinned($request->toArray())),
249
        ]);
250
    }
251
252
    /**
253
     * @return void
254
     * @action site-reviews/route/ajax/toggle-status
255
     */
256
    public function toggleStatusAjax(Request $request)
257
    {
258
        wp_send_json_success(
0 ignored issues
show
Bug introduced by
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

258
        /** @scrutinizer ignore-call */ 
259
        wp_send_json_success(
Loading history...
259
            $this->execute(new ToggleStatus($request->post_id, $request->status))
260
        );
261
    }
262
}
263