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\RegisterShortcodeButtons; |
8
|
|
|
use GeminiLabs\SiteReviews\Controllers\Controller; |
9
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
10
|
|
|
use GeminiLabs\SiteReviews\Helper; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Console; |
12
|
|
|
use GeminiLabs\SiteReviews\Modules\Html; |
13
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
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
|
|
|
$command = new EnqueueAdminAssets([ |
27
|
|
|
'pointers' => [[ |
28
|
|
|
'content' => __( 'You can pin exceptional reviews so that they are always shown first.', 'site-reviews' ), |
29
|
|
|
'id' => 'glsr-pointer-pinned', |
30
|
|
|
'position' => [ |
31
|
|
|
'edge' => 'right', |
32
|
|
|
'align' => 'middle', |
33
|
|
|
], |
34
|
|
|
'screen' => Application::POST_TYPE, |
35
|
|
|
'target' => '#misc-pub-pinned', |
36
|
|
|
'title' => __( 'Pin Your Reviews', 'site-reviews' ), |
37
|
|
|
]], |
38
|
|
|
]); |
39
|
|
|
$this->execute( $command ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
* @filter plugin_action_links_site-reviews/site-reviews.php |
45
|
|
|
*/ |
46
|
|
|
public function filterActionLinks( array $links ) |
47
|
|
|
{ |
48
|
|
|
$links[] = glsr( Builder::class )->a( __( 'Settings', 'site-reviews' ), [ |
49
|
|
|
'href' => admin_url( 'edit.php?post_type='.Application::POST_TYPE.'&page=settings' ), |
50
|
|
|
]); |
51
|
|
|
return $links; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array |
56
|
|
|
* @filter dashboard_glance_items |
57
|
|
|
*/ |
58
|
|
|
public function filterDashboardGlanceItems( array $items ) |
59
|
|
|
{ |
60
|
|
|
$postCount = wp_count_posts( Application::POST_TYPE ); |
61
|
|
|
if( empty( $postCount->publish )) { |
62
|
|
|
return $items; |
63
|
|
|
} |
64
|
|
|
$text = _n( '%s Review', '%s Reviews', $postCount->publish, 'site-reviews' ); |
65
|
|
|
$text = sprintf( $text, number_format_i18n( $postCount->publish )); |
66
|
|
|
$items[] = current_user_can( get_post_type_object( Application::POST_TYPE )->cap->edit_posts ) |
67
|
|
|
? glsr( Builder::class )->a( $text, [ |
68
|
|
|
'class' => 'glsr-review-count', |
69
|
|
|
'href' => 'edit.php?post_type='.Application::POST_TYPE, |
70
|
|
|
]) |
71
|
|
|
: glsr( Builder::class )->span( $text, [ |
72
|
|
|
'class' => 'glsr-review-count', |
73
|
|
|
]); |
74
|
|
|
return $items; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
* @filter mce_external_plugins |
80
|
|
|
*/ |
81
|
|
|
public function filterTinymcePlugins( array $plugins ) |
82
|
|
|
{ |
83
|
|
|
if( user_can_richedit() |
84
|
|
|
&& ( 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 registerShortcodeButtons() |
95
|
|
|
{ |
96
|
1 |
|
$command = new RegisterShortcodeButtons([ |
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
|
|
|
* @return void |
106
|
|
|
* @action edit_form_after_title |
107
|
|
|
*/ |
108
|
|
|
public function renderReviewEditor( WP_Post $post ) |
109
|
|
|
{ |
110
|
|
|
if( !$this->isReviewPostType( $post ) || $this->isReviewEditable( $post ))return; |
111
|
|
|
glsr()->render( 'partials/editor/review', [ |
112
|
|
|
'post' => $post, |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return void |
118
|
|
|
* @action edit_form_top |
119
|
|
|
*/ |
120
|
|
|
public function renderReviewNotice( WP_Post $post ) |
121
|
|
|
{ |
122
|
|
|
if( !$this->isReviewPostType( $post ) || $this->isReviewEditable( $post ))return; |
123
|
|
|
glsr( Notice::class )->addWarning( __( 'This review is read-only.', 'site-reviews' )); |
124
|
|
|
glsr( Html::class )->renderTemplate( 'partials/editor/notice', [ |
125
|
|
|
'context' => [ |
126
|
|
|
'notices' => glsr( Notice::class )->get(), |
127
|
|
|
], |
128
|
|
|
]); |
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 routerClearConsole() |
153
|
|
|
{ |
154
|
|
|
glsr( Console::class )->clear(); |
155
|
|
|
glsr( Notice::class )->addSuccess( __( 'Console cleared.', 'site-reviews' )); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
public function routerDownloadLog() |
162
|
|
|
{ |
163
|
|
|
$this->download( Application::ID.'-log.txt', glsr( Console::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() |
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 $this->isReviewPostType( $post ) |
228
|
|
|
&& post_type_supports( Application::POST_TYPE, 'title' ) |
229
|
|
|
&& get_post_meta( $post->ID, 'review_type', true ) == 'local'; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
|
|
protected function isReviewPostType( WP_Post $post ) |
236
|
|
|
{ |
237
|
|
|
return $post->post_type == Application::POST_TYPE; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|