Test Failed
Push — hotfix/fix-counts ( 1fe4ce...872cd6 )
by Paul
03:14
created

AdminController::filterCreateCapability()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Commands\EnqueueAdminAssets;
7
use GeminiLabs\SiteReviews\Commands\RegisterTinymcePopups;
8
use GeminiLabs\SiteReviews\Controllers\Controller;
9
use GeminiLabs\SiteReviews\Database;
10
use GeminiLabs\SiteReviews\Database\CountsManager;
11
use GeminiLabs\SiteReviews\Database\OptionManager;
12
use GeminiLabs\SiteReviews\Database\SqlQueries;
13
use GeminiLabs\SiteReviews\Helper;
14
use GeminiLabs\SiteReviews\Modules\Console;
15
use GeminiLabs\SiteReviews\Modules\Html\Builder;
16
use GeminiLabs\SiteReviews\Modules\Notice;
17
use GeminiLabs\SiteReviews\Modules\System;
18
19
class AdminController extends Controller
20
{
21
	/**
22
	 * @return void
23
	 * @action admin_enqueue_scripts
24
	 */
25
	public function enqueueAssets()
26
	{
27
		$command = new EnqueueAdminAssets([
28
			'pointers' => [[
29
				'content' => __( 'You can pin exceptional reviews so that they are always shown first.', 'site-reviews' ),
30
				'id' => 'glsr-pointer-pinned',
31
				'position' => [
32
					'edge' => 'right',
33
					'align' => 'middle',
34
				],
35
				'screen' => Application::POST_TYPE,
36
				'target' => '#misc-pub-pinned',
37
				'title' => __( 'Pin Your Reviews', 'site-reviews' ),
38
			]],
39
		]);
40
		$this->execute( $command );
41
	}
42
43
	/**
44
	 * @return array
45
	 * @filter plugin_action_links_site-reviews/site-reviews.php
46
	 */
47
	public function filterActionLinks( array $links )
48
	{
49
		$links['settings'] = glsr( Builder::class )->a( __( 'Settings', 'site-reviews' ), [
50
			'href' => admin_url( 'edit.php?post_type='.Application::POST_TYPE.'&page=settings' ),
51
		]);
52
		return $links;
53
	}
54
55
	/**
56
	 * @param array $capabilities
57
	 * @param string $capability
58
	 * @return array
59
	 * @filter map_meta_cap
60
	 */
61 1
	public function filterCreateCapability( $capabilities, $capability )
62
	{
63 1
		if( $capability == 'create_'.Application::POST_TYPE ) {
64
			$capabilities[] = 'do_not_allow';
65
		}
66 1
		return $capabilities;
67
	}
68
69
	/**
70
	 * @param array $items
71
	 * @return array
72
	 * @filter dashboard_glance_items
73
	 */
74
	public function filterDashboardGlanceItems( $items )
75
	{
76
		$postCount = wp_count_posts( Application::POST_TYPE );
77
		if( empty( $postCount->publish )) {
78
			return $items;
79
		}
80
		$text = _n( '%s Review', '%s Reviews', $postCount->publish, 'site-reviews' );
81
		$text = sprintf( $text, number_format_i18n( $postCount->publish ));
82
		$items = glsr( Helper::class )->consolidateArray( $items );
83
		$items[] = current_user_can( get_post_type_object( Application::POST_TYPE )->cap->edit_posts )
84
			? glsr( Builder::class )->a( $text, [
85
				'class' => 'glsr-review-count',
86
				'href' => 'edit.php?post_type='.Application::POST_TYPE,
87
			])
88
			: glsr( Builder::class )->span( $text, [
89
				'class' => 'glsr-review-count',
90
			]);
91
		return $items;
92
	}
93
94
	/**
95
	 * @param array $plugins
96
	 * @return array
97
	 * @filter mce_external_plugins
98
	 */
99
	public function filterTinymcePlugins( $plugins )
100
	{
101
		if( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' )) {
102
			$plugins = glsr( Helper::class )->consolidateArray( $plugins );
103
			$plugins['glsr_shortcode'] = glsr()->url( 'assets/scripts/mce-plugin.js' );
104
		}
105
		return $plugins;
106
	}
107
108
	/**
109
	 * @return void
110
	 * @action admin_init
111
	 */
112 1
	public function registerTinymcePopups()
113
	{
114 1
		$command = new RegisterTinymcePopups([
115 1
			'site_reviews' => esc_html__( 'Recent Reviews', 'site-reviews' ),
116 1
			'site_reviews_form' => esc_html__( 'Submit a Review', 'site-reviews' ),
117 1
			'site_reviews_summary' => esc_html__( 'Summary of Reviews', 'site-reviews' ),
118
		]);
119 1
		$this->execute( $command );
120 1
	}
121
122
	/**
123
	 * @param string $editorId
124
	 * @return null|void
125
	 * @action media_buttons
126
	 */
127
	public function renderTinymceButton( $editorId )
128
	{
129
		$allowedEditors = apply_filters( 'site-reviews/tinymce/editor-ids', ['content'], $editorId );
130
		if( glsr_current_screen()->base != 'post' || !in_array( $editorId, $allowedEditors ))return;
131
		$shortcodes = [];
132
		foreach( glsr()->mceShortcodes as $shortcode => $values ) {
133
			$shortcodes[$shortcode] = $values;
134
		}
135
		if( empty( $shortcodes ))return;
136
		glsr()->render( 'partials/editor/tinymce', [
137
			'shortcodes' => $shortcodes,
138
		]);
139
	}
140
141
	/**
142
	 * @return void
143
	 */
144
	public function routerClearConsole()
145
	{
146
		glsr( Console::class )->clear();
147
		glsr( Notice::class )->addSuccess( __( 'Console cleared.', 'site-reviews' ));
148
	}
149
150
	/**
151
	 * @return void
152
	 */
153
	public function routerFetchConsole()
154
	{
155
		glsr( Notice::class )->addSuccess( __( 'Console reloaded.', 'site-reviews' ));
156
	}
157
158
	/**
159
	 * @param bool $showNotice
160
	 * @return void
161
	 */
162 1
	public function routerCountReviews( $showNotice = true )
163
	{
164 1
		glsr( CountsManager::class )->countAll();
165 1
		glsr( OptionManager::class )->set( 'last_review_count', current_time( 'timestamp' ));
166 1
		if( $showNotice ) {
167
			glsr( Notice::class )->clear()->addSuccess( __( 'Recalculated rating counts.', 'site-reviews' ));
168
		}
169 1
	}
170
171
	/**
172
	 * @return void
173
	 */
174
	public function routerDownloadConsole()
175
	{
176
		$this->download( Application::ID.'-console.txt', glsr( Console::class )->get() );
177
	}
178
179
	/**
180
	 * @return void
181
	 */
182
	public function routerDownloadSystemInfo()
183
	{
184
		$this->download( Application::ID.'-system-info.txt', glsr( System::class )->get() );
185
	}
186
187
	/**
188
	 * @return void
189
	 */
190
	public function routerExportSettings()
191
	{
192
		$this->download( Application::ID.'-settings.json', glsr( OptionManager::class )->json() );
193
	}
194
195
	/**
196
	 * @return void
197
	 */
198
	public function routerImportSettings()
199
	{
200
		$file = $_FILES['import-file'];
201
		if( $file['error'] !== UPLOAD_ERR_OK ) {
202
			return glsr( Notice::class )->addError( $this->getUploadError( $file['error'] ));
203
		}
204
		if( $file['type'] !== 'application/json' || !glsr( Helper::class )->endsWith( '.json', $file['name'] )) {
205
			return glsr( Notice::class )->addError( __( 'Please use a valid Site Reviews settings file.', 'site-reviews' ));
206
		}
207
		$settings = json_decode( file_get_contents( $file['tmp_name'] ), true );
208
		if( empty( $settings )) {
209
			return glsr( Notice::class )->addWarning( __( 'There were no settings found to import.', 'site-reviews' ));
210
		}
211
		glsr( OptionManager::class )->set( glsr( OptionManager::class )->normalize( $settings ));
212
		glsr( Notice::class )->addSuccess( __( 'Settings imported.', 'site-reviews' ));
213
	}
214
215
	/**
216
	 * @param int $errorCode
217
	 * @return string
218
	 */
219
	protected function getUploadError( $errorCode )
220
	{
221
		$errors = [
222
			UPLOAD_ERR_INI_SIZE => __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews' ),
223
			UPLOAD_ERR_FORM_SIZE => __( 'The uploaded file is too big.', 'site-reviews' ),
224
			UPLOAD_ERR_PARTIAL => __( 'The uploaded file was only partially uploaded.', 'site-reviews' ),
225
			UPLOAD_ERR_NO_FILE => __( 'No file was uploaded.', 'site-reviews' ),
226
			UPLOAD_ERR_NO_TMP_DIR => __( 'Missing a temporary folder.', 'site-reviews' ),
227
			UPLOAD_ERR_CANT_WRITE => __( 'Failed to write file to disk.', 'site-reviews' ),
228
			UPLOAD_ERR_EXTENSION => __( 'A PHP extension stopped the file upload.', 'site-reviews' ),
229
		];
230
		return glsr_get( $errors, $errorCode, __( 'Unknown upload error.', 'site-reviews' ));
231
	}
232
}
233