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
|
|
|
* @param array $items |
57
|
|
|
* @return array |
58
|
|
|
* @filter dashboard_glance_items |
59
|
|
|
*/ |
60
|
|
|
public function filterDashboardGlanceItems( $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 = glsr( Helper::class )->consolidateArray( $items ); |
69
|
|
|
$items[] = current_user_can( get_post_type_object( Application::POST_TYPE )->cap->edit_posts ) |
70
|
|
|
? glsr( Builder::class )->a( $text, [ |
71
|
|
|
'class' => 'glsr-review-count', |
72
|
|
|
'href' => 'edit.php?post_type='.Application::POST_TYPE, |
73
|
|
|
]) |
74
|
|
|
: glsr( Builder::class )->span( $text, [ |
75
|
|
|
'class' => 'glsr-review-count', |
76
|
|
|
]); |
77
|
|
|
return $items; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $plugins |
82
|
|
|
* @return array |
83
|
|
|
* @filter mce_external_plugins |
84
|
|
|
*/ |
85
|
|
|
public function filterTinymcePlugins( $plugins ) |
86
|
|
|
{ |
87
|
|
|
if( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' )) { |
88
|
|
|
$plugins = glsr( Helper::class )->consolidateArray( $plugins ); |
89
|
|
|
$plugins['glsr_shortcode'] = glsr()->url( 'assets/scripts/mce-plugin.js' ); |
90
|
|
|
} |
91
|
|
|
return $plugins; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return void |
96
|
|
|
* @action admin_init |
97
|
|
|
*/ |
98
|
1 |
|
public function registerTinymcePopups() |
99
|
|
|
{ |
100
|
1 |
|
$command = new RegisterTinymcePopups([ |
101
|
1 |
|
'site_reviews' => esc_html__( 'Recent Reviews', 'site-reviews' ), |
102
|
1 |
|
'site_reviews_form' => esc_html__( 'Submit a Review', 'site-reviews' ), |
103
|
1 |
|
'site_reviews_summary' => esc_html__( 'Summary of Reviews', 'site-reviews' ), |
104
|
|
|
]); |
105
|
1 |
|
$this->execute( $command ); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $editorId |
110
|
|
|
* @return null|void |
111
|
|
|
* @action media_buttons |
112
|
|
|
*/ |
113
|
|
|
public function renderTinymceButton( $editorId ) |
114
|
|
|
{ |
115
|
|
|
$allowedEditors = apply_filters( 'site-reviews/tinymce/editor-ids', ['content'], $editorId ); |
116
|
|
|
if( glsr_current_screen()->base != 'post' || !in_array( $editorId, $allowedEditors ))return; |
117
|
|
|
$shortcodes = []; |
118
|
|
|
foreach( glsr()->mceShortcodes as $shortcode => $values ) { |
119
|
|
|
$shortcodes[$shortcode] = $values; |
120
|
|
|
} |
121
|
|
|
if( empty( $shortcodes ))return; |
122
|
|
|
glsr()->render( 'partials/editor/tinymce', [ |
123
|
|
|
'shortcodes' => $shortcodes, |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function routerClearConsole() |
131
|
|
|
{ |
132
|
|
|
glsr( Console::class )->clear(); |
133
|
|
|
glsr( Notice::class )->addSuccess( __( 'Console cleared.', 'site-reviews' )); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function routerFetchConsole() |
140
|
|
|
{ |
141
|
|
|
glsr( Notice::class )->addSuccess( __( 'Console reloaded.', 'site-reviews' )); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param bool $showNotice |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
1 |
|
public function routerCountReviews( $showNotice = true ) |
149
|
|
|
{ |
150
|
1 |
|
$countManager = glsr( CountsManager::class ); |
151
|
1 |
|
$terms = glsr( Database::class )->getTerms( ['fields' => 'all'] ); |
152
|
1 |
|
foreach( $terms as $term ) { |
153
|
|
|
$countManager->setTermCounts( |
154
|
|
|
$term->term_id, |
155
|
|
|
$countManager->buildTermCounts( $term->term_taxonomy_id ) |
156
|
|
|
); |
157
|
|
|
} |
158
|
1 |
|
$postIds = glsr( SqlQueries::class )->getReviewsMeta( 'assigned_to' ); |
159
|
1 |
|
foreach( $postIds as $postId ) { |
160
|
|
|
$countManager->setPostCounts( $postId, $countManager->buildPostCounts( $postId )); |
161
|
|
|
} |
162
|
1 |
|
$countManager->setCounts( $countManager->buildCounts() ); |
163
|
1 |
|
if( $showNotice ) { |
164
|
|
|
glsr( Notice::class )->clear()->addSuccess( __( 'Recalculated rating counts.', 'site-reviews' )); |
165
|
|
|
} |
166
|
1 |
|
glsr( OptionManager::class )->set( 'last_review_count', current_time( 'timestamp' )); |
167
|
1 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
public function routerDownloadConsole() |
173
|
|
|
{ |
174
|
|
|
$this->download( Application::ID.'-console.txt', glsr( Console::class )->get() ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
public function routerDownloadSystemInfo() |
181
|
|
|
{ |
182
|
|
|
$this->download( Application::ID.'-system-info.txt', glsr( System::class )->get() ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
public function routerExportSettings() |
189
|
|
|
{ |
190
|
|
|
$this->download( Application::ID.'-settings.json', glsr( OptionManager::class )->json() ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
public function routerImportSettings() |
197
|
|
|
{ |
198
|
|
|
$file = $_FILES['import-file']; |
199
|
|
|
if( $file['error'] !== UPLOAD_ERR_OK ) { |
200
|
|
|
return glsr( Notice::class )->addError( $this->getUploadError( $file['error'] )); |
201
|
|
|
} |
202
|
|
|
if( $file['type'] !== 'application/json' || !glsr( Helper::class )->endsWith( '.json', $file['name'] )) { |
203
|
|
|
return glsr( Notice::class )->addError( __( 'Please use a valid Site Reviews settings file.', 'site-reviews' )); |
204
|
|
|
} |
205
|
|
|
$settings = json_decode( file_get_contents( $file['tmp_name'] ), true ); |
206
|
|
|
if( empty( $settings )) { |
207
|
|
|
return glsr( Notice::class )->addWarning( __( 'There were no settings found to import.', 'site-reviews' )); |
208
|
|
|
} |
209
|
|
|
glsr( OptionManager::class )->set( glsr( OptionManager::class )->normalize( $settings )); |
210
|
|
|
glsr( Notice::class )->addSuccess( __( 'Settings imported.', 'site-reviews' )); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param int $errorCode |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
protected function getUploadError( $errorCode ) |
218
|
|
|
{ |
219
|
|
|
$errors = [ |
220
|
|
|
UPLOAD_ERR_INI_SIZE => __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews' ), |
221
|
|
|
UPLOAD_ERR_FORM_SIZE => __( 'The uploaded file is too big.', 'site-reviews' ), |
222
|
|
|
UPLOAD_ERR_PARTIAL => __( 'The uploaded file was only partially uploaded.', 'site-reviews' ), |
223
|
|
|
UPLOAD_ERR_NO_FILE => __( 'No file was uploaded.', 'site-reviews' ), |
224
|
|
|
UPLOAD_ERR_NO_TMP_DIR => __( 'Missing a temporary folder.', 'site-reviews' ), |
225
|
|
|
UPLOAD_ERR_CANT_WRITE => __( 'Failed to write file to disk.', 'site-reviews' ), |
226
|
|
|
UPLOAD_ERR_EXTENSION => __( 'A PHP extension stopped the file upload.', 'site-reviews' ), |
227
|
|
|
]; |
228
|
|
|
return glsr_get( $errors, $errorCode, __( 'Unknown upload error.', 'site-reviews' )); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|