Passed
Push — master ( 796b78...de3336 )
by Paul
05:32
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\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
		$postType = Application::POST_TYPE;
48
		$postCount = wp_count_posts( $postType );
49
		if( !isset( $postCount->publish ) || !$postCount->publish ) {
50
			return $items;
51
		}
52
		$postTypeObject = get_post_type_object( $postType );
53
		$text = _n( '%s Review', '%s Reviews', $postCount->publish, 'site-reviews' );
54
		$text = sprintf( $text, number_format_i18n( $postCount->publish ));
55
		$items[] = $postTypeObject && current_user_can( $postTypeObject->cap->edit_posts )
56
			? sprintf( '<a class="glsr-review-count" href="edit.php?post_type=%s">%s</a>', $postType, $text )
57
			: sprintf( '<span class="glsr-review-count">%s</span>', $text );
58
		return $items;
59
	}
60
61
	/**
62
	 * @return array
63
	 * @filter mce_external_plugins
64
	 */
65
	public function filterTinymcePlugins( array $plugins )
66
	{
67
		if( user_can_richedit()
68
			&& ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ))) {
69
			$plugins['glsr_shortcode'] = glsr()->url( 'assets/scripts/mce-plugin.js' );
70
		}
71
		return $plugins;
72
	}
73
74
	/**
75
	 * @return void
76
	 * @action admin_enqueue_scripts
77
	 */
78
	public function registerPointers()
79
	{
80
		$command = new RegisterPointers([[
81
			'content' => __( 'You can pin exceptional reviews so that they are always shown first.', 'site-reviews' ),
82
			'id' => 'glsr-pointer-pinned',
83
			'position' => [
84
				'edge' => 'right',
85
				'align' => 'middle',
86
			],
87
			'screen' => Application::POST_TYPE,
88
			'target' => '#misc-pub-pinned',
89
			'title' => __( 'Pin Your Reviews', 'site-reviews' ),
90
		]]);
91
		$this->execute( $command );
92
	}
93
94
	/**
95
	 * @return void
96
	 * @action admin_init
97
	 */
98
	public function registerShortcodeButtons()
99
	{
100
		$command = new RegisterShortcodeButtons([
101
			'site_reviews' => esc_html__( 'Recent Reviews', 'site-reviews' ),
102
			'site_reviews_form' => esc_html__( 'Submit a Review', 'site-reviews' ),
103
			'site_reviews_summary' => esc_html__( 'Summary of Reviews', 'site-reviews' ),
104
		]);
105
		$this->execute( $command );
106
	}
107
108
	/**
109
	 * @return void
110
	 * @action edit_form_after_title
111
	 */
112
	public function renderReviewEditor( WP_Post $post )
113
	{
114
		if( !$this->isReviewEditable( $post ) )return;
115
		glsr()->render( 'partials/editor/review', [
116
			'post' => $post,
117
		]);
118
	}
119
120
	/**
121
	 * @return void
122
	 * @action edit_form_top
123
	 */
124
	public function renderReviewNotice( WP_Post $post )
125
	{
126
		if( !$this->isReviewEditable( $post ) )return;
127
		glsr( Notice::class )->addWarning( __( 'This review is read-only.', 'site-reviews' ));
128
		glsr()->render( 'partials/editor/notice' );
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 routerClearLog()
153
	{
154
		glsr( Logger::class )->clear();
155
		glsr( Notice::class )->addSuccess( __( 'Log cleared.', 'site-reviews' ));
156
	}
157
158
	/**
159
	 * @return void
160
	 */
161
	public function routerDownloadLog()
162
	{
163
		$this->download( Application::ID.'-log.txt', glsr( Logger::class )->get() );
164
	}
165
166
	/**
167
	 * @return void
168
	 */
169
	public function routerDownloadSystemInfo()
170
	{
171
		$this->download( Application::ID.'-system-info.txt', glsr( System::class )->get() );
172
	}
173
174
	/**
175
	 * @return void
176
	 */
177
	public function routerExportSettings()
178
	{
179
		$this->download( Application::ID.'-settings.json', glsr( OptionManager::class )->json() );
180
	}
181
182
	/**
183
	 * @return void
184
	 */
185
	public function routerImportSettings( array $request )
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

185
	public function routerImportSettings( /** @scrutinizer ignore-unused */ array $request )

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
	{
187
		$file = $_FILES['import-file'];
188
		if( $file['error'] !== UPLOAD_ERR_OK ) {
189
			return glsr( Notice::class )->addError( $this->getUploadError( $file['error'] ));
190
		}
191
		if( $file['type'] !== 'application/json' || !glsr( Helper::class )->endsWith( '.json', $file['name'] )) {
192
			return glsr( Notice::class )->addError( __( 'Please use a valid Site Reviews settings file.', 'site-reviews' ));
193
		}
194
		$settings = json_decode( file_get_contents( $file['tmp_name'] ), true );
195
		if( empty( $settings )) {
196
			return glsr( Notice::class )->addWarning( __( 'There were no settings found to import.', 'site-reviews' ));
197
		}
198
		glsr( OptionManager::class )->set( glsr( OptionManager::class )->normalize( $settings ));
199
		glsr( Notice::class )->addSuccess( __( 'Settings imported.', 'site-reviews' ));
200
	}
201
202
	/**
203
	 * @param int $errorCode
204
	 * @return string
205
	 */
206
	protected function getUploadError( $errorCode )
207
	{
208
		$errors = [
209
			UPLOAD_ERR_INI_SIZE => __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews' ),
210
			UPLOAD_ERR_FORM_SIZE => __( 'The uploaded file is too big.', 'site-reviews' ),
211
			UPLOAD_ERR_PARTIAL => __( 'The uploaded file was only partially uploaded.', 'site-reviews' ),
212
			UPLOAD_ERR_NO_FILE => __( 'No file was uploaded.', 'site-reviews' ),
213
			UPLOAD_ERR_NO_TMP_DIR => __( 'Missing a temporary folder.', 'site-reviews' ),
214
			UPLOAD_ERR_CANT_WRITE => __( 'Failed to write file to disk.', 'site-reviews' ),
215
			UPLOAD_ERR_EXTENSION => __( 'A PHP extension stopped the file upload.', 'site-reviews' ),
216
		];
217
		return !isset( $errors[$errorCode] )
218
			? __( 'Unknown upload error.', 'site-reviews' )
219
			: $errors[$errorCode];
220
	}
221
222
	/**
223
	 * @return bool
224
	 */
225
	protected function isReviewEditable( WP_Post $post )
226
	{
227
		return $post->post_type == Application::POST_TYPE
228
			&& post_type_supports( Application::POST_TYPE, 'title' )
229
			&& get_post_meta( $post->ID, 'review_type', true ) == 'local';
230
	}
231
}
232