Test Failed
Push — master ( 755753...a3fd07 )
by Paul
03:57
created

AdminController   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Test Coverage

Coverage 7%

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 229
ccs 7
cts 100
cp 0.07
rs 9.44
c 0
b 0
f 0
wmc 37

17 Methods

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