Passed
Push — master ( e9a082...4a454f )
by Paul
03:30
created

AdminController::enqueueAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 16
ccs 0
cts 8
cp 0
crap 2
rs 9.9
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\RegisterTinymcePopups;
8
use GeminiLabs\SiteReviews\Controllers\Controller;
9
use GeminiLabs\SiteReviews\Database;
10
use GeminiLabs\SiteReviews\Database\CountsManager;
11
use GeminiLabs\SiteReviews\Database\OptionManager;
12
use GeminiLabs\SiteReviews\Database\SqlQueries;
13
use GeminiLabs\SiteReviews\Helper;
14
use GeminiLabs\SiteReviews\Modules\Console;
15
use GeminiLabs\SiteReviews\Modules\Html\Builder;
16
use GeminiLabs\SiteReviews\Modules\Notice;
17
use GeminiLabs\SiteReviews\Modules\System;
18
19
class AdminController extends Controller
20
{
21
	/**
22
	 * @return void
23
	 * @action admin_enqueue_scripts
24
	 */
25
	public function enqueueAssets()
26
	{
27
		$command = new EnqueueAdminAssets([
28
			'pointers' => [[
29
				'content' => __( 'You can pin exceptional reviews so that they are always shown first.', 'site-reviews' ),
30
				'id' => 'glsr-pointer-pinned',
31
				'position' => [
32
					'edge' => 'right',
33
					'align' => 'middle',
34
				],
35
				'screen' => Application::POST_TYPE,
36
				'target' => '#misc-pub-pinned',
37
				'title' => __( 'Pin Your Reviews', 'site-reviews' ),
38
			]],
39
		]);
40
		$this->execute( $command );
41
	}
42
43
	/**
44
	 * @return array
45
	 * @filter plugin_action_links_site-reviews/site-reviews.php
46
	 */
47
	public function filterActionLinks( array $links )
48
	{
49
		$links['settings'] = glsr( Builder::class )->a( __( 'Settings', 'site-reviews' ), [
50
			'href' => admin_url( 'edit.php?post_type='.Application::POST_TYPE.'&page=settings' ),
51
		]);
52
		return $links;
53
	}
54
55
	/**
56
	 * @return array
57
	 * @filter dashboard_glance_items
58
	 */
59
	public function filterDashboardGlanceItems( array $items )
60
	{
61
		$postCount = wp_count_posts( Application::POST_TYPE );
62
		if( empty( $postCount->publish )) {
63
			return $items;
64
		}
65
		$text = _n( '%s Review', '%s Reviews', $postCount->publish, 'site-reviews' );
66
		$text = sprintf( $text, number_format_i18n( $postCount->publish ));
67
		$items[] = current_user_can( get_post_type_object( Application::POST_TYPE )->cap->edit_posts )
68
			? glsr( Builder::class )->a( $text, [
69
				'class' => 'glsr-review-count',
70
				'href' => 'edit.php?post_type='.Application::POST_TYPE,
71
			])
72
			: glsr( Builder::class )->span( $text, [
73
				'class' => 'glsr-review-count',
74
			]);
75
		return $items;
76
	}
77
78
	/**
79
	 * @return array
80
	 * @filter mce_external_plugins
81
	 */
82
	public function filterTinymcePlugins( array $plugins )
83
	{
84
		if( 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 registerTinymcePopups()
95
	{
96 1
		$command = new RegisterTinymcePopups([
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
	 * @param string $editorId
106
	 * @return null|void
107
	 * @action media_buttons
108
	 */
109
	public function renderTinymceButton( $editorId )
110
	{
111
		$allowedEditors = apply_filters( 'site-reviews/tinymce/editor-ids', ['content'], $editorId );
112
		if( glsr_current_screen()->base != 'post' || !in_array( $editorId, $allowedEditors ))return;
113
		$shortcodes = [];
114
		foreach( glsr()->mceShortcodes as $shortcode => $values ) {
115
			$shortcodes[$shortcode] = $values;
116
		}
117
		if( empty( $shortcodes ))return;
118
		glsr()->render( 'partials/editor/tinymce', [
119
			'shortcodes' => $shortcodes,
120
		]);
121
	}
122
123
	/**
124
	 * @return void
125
	 */
126
	public function routerClearConsole()
127
	{
128
		glsr( Console::class )->clear();
129
		glsr( Notice::class )->addSuccess( __( 'Console cleared.', 'site-reviews' ));
130
	}
131
132
	/**
133
	 * @return void
134
	 */
135
	public function routerFetchConsole()
136
	{
137
		glsr( Notice::class )->addSuccess( __( 'Console reloaded.', 'site-reviews' ));
138
	}
139
140
	/**
141
	 * @param bool $showNotice
142
	 * @return void
143
	 */
144 1
	public function routerCountReviews( $showNotice = true )
145
	{
146 1
		$countManager = glsr( CountsManager::class );
147 1
		$terms = glsr( Database::class )->getTerms( ['fields' => 'all'] );
148 1
		foreach( $terms as $term ) {
149
			$countManager->setTermCounts(
150
				$term->term_id,
151
				$countManager->buildTermCounts( $term->term_taxonomy_id )
152
			);
153
		}
154 1
		$postIds = glsr( SqlQueries::class )->getReviewsMeta( 'assigned_to' );
155 1
		foreach( $postIds as $postId ) {
156
			$countManager->setPostCounts( $postId, $countManager->buildPostCounts( $postId ));
157
		}
158 1
		$countManager->setCounts( $countManager->buildCounts() );
159 1
		if( $showNotice ) {
160
			glsr( Notice::class )->clear()->addSuccess( __( 'Recalculated rating counts.', 'site-reviews' ));
161
		}
162 1
		glsr( OptionManager::class )->set( 'last_review_count', current_time( 'timestamp' ));
163 1
	}
164
165
	/**
166
	 * @return void
167
	 */
168
	public function routerDownloadConsole()
169
	{
170
		$this->download( Application::ID.'-console.txt', glsr( Console::class )->get() );
171
	}
172
173
	/**
174
	 * @return void
175
	 */
176
	public function routerDownloadSystemInfo()
177
	{
178
		$this->download( Application::ID.'-system-info.txt', glsr( System::class )->get() );
179
	}
180
181
	/**
182
	 * @return void
183
	 */
184
	public function routerExportSettings()
185
	{
186
		$this->download( Application::ID.'-settings.json', glsr( OptionManager::class )->json() );
187
	}
188
189
	/**
190
	 * @return void
191
	 */
192
	public function routerImportSettings()
193
	{
194
		$file = $_FILES['import-file'];
195
		if( $file['error'] !== UPLOAD_ERR_OK ) {
196
			return glsr( Notice::class )->addError( $this->getUploadError( $file['error'] ));
197
		}
198
		if( $file['type'] !== 'application/json' || !glsr( Helper::class )->endsWith( '.json', $file['name'] )) {
199
			return glsr( Notice::class )->addError( __( 'Please use a valid Site Reviews settings file.', 'site-reviews' ));
200
		}
201
		$settings = json_decode( file_get_contents( $file['tmp_name'] ), true );
202
		if( empty( $settings )) {
203
			return glsr( Notice::class )->addWarning( __( 'There were no settings found to import.', 'site-reviews' ));
204
		}
205
		glsr( OptionManager::class )->set( glsr( OptionManager::class )->normalize( $settings ));
206
		glsr( Notice::class )->addSuccess( __( 'Settings imported.', 'site-reviews' ));
207
	}
208
209
	/**
210
	 * @param int $errorCode
211
	 * @return string
212
	 */
213
	protected function getUploadError( $errorCode )
214
	{
215
		$errors = [
216
			UPLOAD_ERR_INI_SIZE => __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews' ),
217
			UPLOAD_ERR_FORM_SIZE => __( 'The uploaded file is too big.', 'site-reviews' ),
218
			UPLOAD_ERR_PARTIAL => __( 'The uploaded file was only partially uploaded.', 'site-reviews' ),
219
			UPLOAD_ERR_NO_FILE => __( 'No file was uploaded.', 'site-reviews' ),
220
			UPLOAD_ERR_NO_TMP_DIR => __( 'Missing a temporary folder.', 'site-reviews' ),
221
			UPLOAD_ERR_CANT_WRITE => __( 'Failed to write file to disk.', 'site-reviews' ),
222
			UPLOAD_ERR_EXTENSION => __( 'A PHP extension stopped the file upload.', 'site-reviews' ),
223
		];
224
		return !isset( $errors[$errorCode] )
225
			? __( 'Unknown upload error.', 'site-reviews' )
226
			: $errors[$errorCode];
227
	}
228
}
229