Passed
Push — master ( a42385...9b46a7 )
by Paul
04:14
created

AdminController::getUploadError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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