Passed
Push — master ( 8a0962...507d84 )
by Paul
03:52
created

AdminController::routerExportSettings()   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
			if( !apply_filters( sanitize_title( $shortcode ).'_condition', true ))continue;
143
			$shortcodes[$shortcode] = $values;
144
		}
145
		if( empty( $shortcodes ))return;
146
		glsr()->render( 'partials/editor/tinymce', [
147
			'shortcodes' => $shortcodes,
148
		]);
149
	}
150
151
	/**
152
	 * @return void
153
	 */
154
	public function routerClearConsole()
155
	{
156
		glsr( Console::class )->clear();
157
		glsr( Notice::class )->addSuccess( __( 'Console cleared.', 'site-reviews' ));
158
	}
159
160
	/**
161
	 * @return void
162
	 */
163
	public function routerFetchConsole()
164
	{
165
		glsr( Notice::class )->addSuccess( __( 'Console reloaded.', 'site-reviews' ));
166
	}
167
168
	/**
169
	 * @return void
170
	 */
171
	public function routerCountReviews()
172
	{
173
		$countManager = glsr( CountsManager::class );
174
		$terms = get_terms([
175
			'hide_empty' => true,
176
			'taxonomy' => Application::TAXONOMY,
177
		]);
178
		foreach( $terms as $term ) {
179
			$countManager->setTermCounts( $term->term_id, $countManager->buildTermCounts( $term->term_id ));
180
		}
181
		$postIds = glsr( SqlQueries::class )->getReviewsMeta( 'assigned_to' );
182
		foreach( $postIds as $postId ) {
183
			$countManager->setPostCounts( $postId, $countManager->buildPostCounts( $postId ));
184
		}
185
		$countManager->setCounts( $countManager->buildCounts() );
186
		glsr( Notice::class )->addSuccess( __( 'Recalculated review counts.', 'site-reviews' ));
187
	}
188
189
	/**
190
	 * @return void
191
	 */
192
	public function routerDownloadConsole()
193
	{
194
		$this->download( Application::ID.'-console.txt', glsr( Console::class )->get() );
195
	}
196
197
	/**
198
	 * @return void
199
	 */
200
	public function routerDownloadSystemInfo()
201
	{
202
		$this->download( Application::ID.'-system-info.txt', glsr( System::class )->get() );
203
	}
204
205
	/**
206
	 * @return void
207
	 */
208
	public function routerExportSettings()
209
	{
210
		$this->download( Application::ID.'-settings.json', glsr( OptionManager::class )->json() );
211
	}
212
213
	/**
214
	 * @return void
215
	 */
216
	public function routerImportSettings()
217
	{
218
		$file = $_FILES['import-file'];
219
		if( $file['error'] !== UPLOAD_ERR_OK ) {
220
			return glsr( Notice::class )->addError( $this->getUploadError( $file['error'] ));
221
		}
222
		if( $file['type'] !== 'application/json' || !glsr( Helper::class )->endsWith( '.json', $file['name'] )) {
223
			return glsr( Notice::class )->addError( __( 'Please use a valid Site Reviews settings file.', 'site-reviews' ));
224
		}
225
		$settings = json_decode( file_get_contents( $file['tmp_name'] ), true );
226
		if( empty( $settings )) {
227
			return glsr( Notice::class )->addWarning( __( 'There were no settings found to import.', 'site-reviews' ));
228
		}
229
		glsr( OptionManager::class )->set( glsr( OptionManager::class )->normalize( $settings ));
230
		glsr( Notice::class )->addSuccess( __( 'Settings imported.', 'site-reviews' ));
231
	}
232
233
	/**
234
	 * @param int $errorCode
235
	 * @return string
236
	 */
237
	protected function getUploadError( $errorCode )
238
	{
239
		$errors = [
240
			UPLOAD_ERR_INI_SIZE => __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews' ),
241
			UPLOAD_ERR_FORM_SIZE => __( 'The uploaded file is too big.', 'site-reviews' ),
242
			UPLOAD_ERR_PARTIAL => __( 'The uploaded file was only partially uploaded.', 'site-reviews' ),
243
			UPLOAD_ERR_NO_FILE => __( 'No file was uploaded.', 'site-reviews' ),
244
			UPLOAD_ERR_NO_TMP_DIR => __( 'Missing a temporary folder.', 'site-reviews' ),
245
			UPLOAD_ERR_CANT_WRITE => __( 'Failed to write file to disk.', 'site-reviews' ),
246
			UPLOAD_ERR_EXTENSION => __( 'A PHP extension stopped the file upload.', 'site-reviews' ),
247
		];
248
		return !isset( $errors[$errorCode] )
249
			? __( 'Unknown upload error.', 'site-reviews' )
250
			: $errors[$errorCode];
251
	}
252
253
	/**
254
	 * @return bool
255
	 */
256
	protected function isReviewEditable( WP_Post $post )
257
	{
258
		return $this->isReviewPostType( $post )
259
			&& post_type_supports( Application::POST_TYPE, 'title' )
260
			&& get_post_meta( $post->ID, 'review_type', true ) == 'local';
261
	}
262
263
	/**
264
	 * @return bool
265
	 */
266
	protected function isReviewPostType( WP_Post $post )
267
	{
268
		return $post->post_type == Application::POST_TYPE;
269
	}
270
}
271