Passed
Push — master ( a78f9f...203c84 )
by Paul
08:17
created

AdminController::isReviewEditable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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