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