Passed
Push — master ( dfcc60...d308ca )
by Paul
03:56
created

AdminController::routerDownloadConsole()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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;
15
use GeminiLabs\SiteReviews\Modules\Html\Builder;
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( Html::class )->renderTemplate( '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
	 * @return void
169
	 */
170
	public function routerCountReviews()
171
	{
172
		$countManager = glsr( CountsManager::class );
173
		$terms = get_terms([
174
			'hide_empty' => true,
175
			'taxonomy' => Application::TAXONOMY,
176
		]);
177
		foreach( $terms as $term ) {
178
			$countManager->setTermCounts( $term->term_id, $countManager->buildTermCounts( $term->term_id ));
179
		}
180
		$postIds = glsr( SqlQueries::class )->getReviewsMeta( 'assigned_to' );
181
		foreach( $postIds as $postId ) {
182
			$countManager->setPostCounts( $postId, $countManager->buildPostCounts( $postId ));
183
		}
184
		$countManager->setCounts( $countManager->buildCounts() );
185
		glsr( Notice::class )->addSuccess( __( 'Recalculated rating counts.', 'site-reviews' ));
186
	}
187
188
	/**
189
	 * @return void
190
	 */
191
	public function routerDownloadConsole()
192
	{
193
		$this->download( Application::ID.'-console.txt', glsr( Console::class )->get() );
194
	}
195
196
	/**
197
	 * @return void
198
	 */
199
	public function routerDownloadSystemInfo()
200
	{
201
		$this->download( Application::ID.'-system-info.txt', glsr( System::class )->get() );
202
	}
203
204
	/**
205
	 * @return void
206
	 */
207
	public function routerExportSettings()
208
	{
209
		$this->download( Application::ID.'-settings.json', glsr( OptionManager::class )->json() );
210
	}
211
212
	/**
213
	 * @return void
214
	 */
215
	public function routerImportSettings()
216
	{
217
		$file = $_FILES['import-file'];
218
		if( $file['error'] !== UPLOAD_ERR_OK ) {
219
			return glsr( Notice::class )->addError( $this->getUploadError( $file['error'] ));
220
		}
221
		if( $file['type'] !== 'application/json' || !glsr( Helper::class )->endsWith( '.json', $file['name'] )) {
222
			return glsr( Notice::class )->addError( __( 'Please use a valid Site Reviews settings file.', 'site-reviews' ));
223
		}
224
		$settings = json_decode( file_get_contents( $file['tmp_name'] ), true );
225
		if( empty( $settings )) {
226
			return glsr( Notice::class )->addWarning( __( 'There were no settings found to import.', 'site-reviews' ));
227
		}
228
		glsr( OptionManager::class )->set( glsr( OptionManager::class )->normalize( $settings ));
229
		glsr( Notice::class )->addSuccess( __( 'Settings imported.', 'site-reviews' ));
230
	}
231
232
	/**
233
	 * @param int $errorCode
234
	 * @return string
235
	 */
236
	protected function getUploadError( $errorCode )
237
	{
238
		$errors = [
239
			UPLOAD_ERR_INI_SIZE => __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews' ),
240
			UPLOAD_ERR_FORM_SIZE => __( 'The uploaded file is too big.', 'site-reviews' ),
241
			UPLOAD_ERR_PARTIAL => __( 'The uploaded file was only partially uploaded.', 'site-reviews' ),
242
			UPLOAD_ERR_NO_FILE => __( 'No file was uploaded.', 'site-reviews' ),
243
			UPLOAD_ERR_NO_TMP_DIR => __( 'Missing a temporary folder.', 'site-reviews' ),
244
			UPLOAD_ERR_CANT_WRITE => __( 'Failed to write file to disk.', 'site-reviews' ),
245
			UPLOAD_ERR_EXTENSION => __( 'A PHP extension stopped the file upload.', 'site-reviews' ),
246
		];
247
		return !isset( $errors[$errorCode] )
248
			? __( 'Unknown upload error.', 'site-reviews' )
249
			: $errors[$errorCode];
250
	}
251
252
	/**
253
	 * @return bool
254
	 */
255
	protected function isReviewEditable( WP_Post $post )
256
	{
257
		return $this->isReviewPostType( $post )
258
			&& post_type_supports( Application::POST_TYPE, 'title' )
259
			&& get_post_meta( $post->ID, 'review_type', true ) == 'local';
260
	}
261
262
	/**
263
	 * @return bool
264
	 */
265
	protected function isReviewPostType( WP_Post $post )
266
	{
267
		return $post->post_type == Application::POST_TYPE;
268
	}
269
}
270