Passed
Push — master ( 9b46a7...ed8579 )
by Paul
03:56
created

AdminController::enqueueAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 16
ccs 0
cts 8
cp 0
crap 2
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\RegisterTinymcePopups;
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( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' )) {
86
			$plugins['glsr_shortcode'] = glsr()->url( 'assets/scripts/mce-plugin.js' );
87
		}
88
		return $plugins;
89
	}
90
91
	/**
92
	 * @return void
93
	 * @action admin_init
94
	 */
95 1
	public function registerTinymcePopups()
96
	{
97 1
		$command = new RegisterTinymcePopups([
98 1
			'site_reviews' => esc_html__( 'Recent Reviews', 'site-reviews' ),
99 1
			'site_reviews_form' => esc_html__( 'Submit a Review', 'site-reviews' ),
100 1
			'site_reviews_summary' => esc_html__( 'Summary of Reviews', 'site-reviews' ),
101
		]);
102 1
		$this->execute( $command );
103 1
	}
104
105
	/**
106
	 * @return void
107
	 * @action edit_form_after_title
108
	 */
109
	public function renderReviewEditor( WP_Post $post )
110
	{
111
		if( !$this->isReviewPostType( $post ) || $this->isReviewEditable( $post ))return;
112
		glsr()->render( 'partials/editor/review', [
113
			'post' => $post,
114
		]);
115
	}
116
117
	/**
118
	 * @return void
119
	 * @action edit_form_top
120
	 */
121
	public function renderReviewNotice( WP_Post $post )
122
	{
123
		if( !$this->isReviewPostType( $post ) || $this->isReviewEditable( $post ))return;
124
		glsr( Notice::class )->addWarning( __( 'This review is read-only.', 'site-reviews' ));
125
		glsr( Template::class )->render( 'partials/editor/notice', [
126
			'context' => [
127
				'notices' => glsr( Notice::class )->get(),
128
			],
129
		]);
130
	}
131
132
	/**
133
	 * @return null|void
134
	 * @action media_buttons
135
	 */
136
	public function renderTinymceButton()
137
	{
138
		if( glsr_current_screen()->base != 'post' )return;
139
		$shortcodes = [];
140
		foreach( glsr()->mceShortcodes as $shortcode => $values ) {
141
			$shortcodes[$shortcode] = $values;
142
		}
143
		if( empty( $shortcodes ))return;
144
		glsr()->render( 'partials/editor/tinymce', [
145
			'shortcodes' => $shortcodes,
146
		]);
147
	}
148
149
	/**
150
	 * @return void
151
	 */
152
	public function routerClearConsole()
153
	{
154
		glsr( Console::class )->clear();
155
		glsr( Notice::class )->addSuccess( __( 'Console cleared.', 'site-reviews' ));
156
	}
157
158
	/**
159
	 * @return void
160
	 */
161
	public function routerFetchConsole()
162
	{
163
		glsr( Notice::class )->addSuccess( __( 'Console reloaded.', 'site-reviews' ));
164
	}
165
166
	/**
167
	 * @param bool $showNotice
168
	 * @return void
169
	 */
170 1
	public function routerCountReviews( $showNotice = true )
171
	{
172 1
		$countManager = glsr( CountsManager::class );
173 1
		$terms = get_terms([
174 1
			'hide_empty' => true,
175
			'taxonomy' => Application::TAXONOMY,
176
		]);
177 1
		foreach( $terms as $term ) {
178
			$countManager->setTermCounts( $term->term_id, $countManager->buildTermCounts( $term->term_id ));
179
		}
180 1
		$postIds = glsr( SqlQueries::class )->getReviewsMeta( 'assigned_to' );
181 1
		foreach( $postIds as $postId ) {
182
			$countManager->setPostCounts( $postId, $countManager->buildPostCounts( $postId ));
183
		}
184 1
		$countManager->setCounts( $countManager->buildCounts() );
185 1
		if( $showNotice ) {
186
			glsr( Notice::class )->clear()->addSuccess( __( 'Recalculated rating counts.', 'site-reviews' ));
187
		}
188 1
	}
189
190
	/**
191
	 * @return void
192
	 */
193
	public function routerDownloadConsole()
194
	{
195
		$this->download( Application::ID.'-console.txt', glsr( Console::class )->get() );
196
	}
197
198
	/**
199
	 * @return void
200
	 */
201
	public function routerDownloadSystemInfo()
202
	{
203
		$this->download( Application::ID.'-system-info.txt', glsr( System::class )->get() );
204
	}
205
206
	/**
207
	 * @return void
208
	 */
209
	public function routerExportSettings()
210
	{
211
		$this->download( Application::ID.'-settings.json', glsr( OptionManager::class )->json() );
212
	}
213
214
	/**
215
	 * @return void
216
	 */
217
	public function routerImportSettings()
218
	{
219
		$file = $_FILES['import-file'];
220
		if( $file['error'] !== UPLOAD_ERR_OK ) {
221
			return glsr( Notice::class )->addError( $this->getUploadError( $file['error'] ));
222
		}
223
		if( $file['type'] !== 'application/json' || !glsr( Helper::class )->endsWith( '.json', $file['name'] )) {
224
			return glsr( Notice::class )->addError( __( 'Please use a valid Site Reviews settings file.', 'site-reviews' ));
225
		}
226
		$settings = json_decode( file_get_contents( $file['tmp_name'] ), true );
227
		if( empty( $settings )) {
228
			return glsr( Notice::class )->addWarning( __( 'There were no settings found to import.', 'site-reviews' ));
229
		}
230
		glsr( OptionManager::class )->set( glsr( OptionManager::class )->normalize( $settings ));
231
		glsr( Notice::class )->addSuccess( __( 'Settings imported.', 'site-reviews' ));
232
	}
233
234
	/**
235
	 * @param int $errorCode
236
	 * @return string
237
	 */
238
	protected function getUploadError( $errorCode )
239
	{
240
		$errors = [
241
			UPLOAD_ERR_INI_SIZE => __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews' ),
242
			UPLOAD_ERR_FORM_SIZE => __( 'The uploaded file is too big.', 'site-reviews' ),
243
			UPLOAD_ERR_PARTIAL => __( 'The uploaded file was only partially uploaded.', 'site-reviews' ),
244
			UPLOAD_ERR_NO_FILE => __( 'No file was uploaded.', 'site-reviews' ),
245
			UPLOAD_ERR_NO_TMP_DIR => __( 'Missing a temporary folder.', 'site-reviews' ),
246
			UPLOAD_ERR_CANT_WRITE => __( 'Failed to write file to disk.', 'site-reviews' ),
247
			UPLOAD_ERR_EXTENSION => __( 'A PHP extension stopped the file upload.', 'site-reviews' ),
248
		];
249
		return !isset( $errors[$errorCode] )
250
			? __( 'Unknown upload error.', 'site-reviews' )
251
			: $errors[$errorCode];
252
	}
253
254
	/**
255
	 * @return bool
256
	 */
257
	protected function isReviewEditable( WP_Post $post )
258
	{
259
		return $this->isReviewPostType( $post )
260
			&& post_type_supports( Application::POST_TYPE, 'title' )
261
			&& get_post_meta( $post->ID, 'review_type', true ) == 'local';
262
	}
263
264
	/**
265
	 * @return bool
266
	 */
267
	protected function isReviewPostType( WP_Post $post )
268
	{
269
		return $post->post_type == Application::POST_TYPE;
270
	}
271
}
272