|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Controllers\ListTableColumns\ColumnValueType; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Database\Query; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Defaults\UpdatedMessageDefaults; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
|
12
|
|
|
use GeminiLabs\SiteReviews\Modules\Notice; |
|
13
|
|
|
use GeminiLabs\SiteReviews\Modules\Sanitizer; |
|
14
|
|
|
use GeminiLabs\SiteReviews\Request; |
|
15
|
|
|
use GeminiLabs\SiteReviews\Review; |
|
16
|
|
|
use WP_Post; |
|
17
|
|
|
|
|
18
|
|
|
class EditorController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param array $settings |
|
22
|
|
|
* @return array |
|
23
|
|
|
* @filter wp_editor_settings |
|
24
|
|
|
*/ |
|
25
|
|
|
public function filterEditorSettings($settings) |
|
26
|
|
|
{ |
|
27
|
|
|
if ($this->isReviewEditor()) { |
|
28
|
|
|
$settings = [ |
|
29
|
|
|
'media_buttons' => false, |
|
30
|
|
|
'quicktags' => false, |
|
31
|
|
|
'textarea_rows' => 12, |
|
32
|
|
|
'tinymce' => false, |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
return Arr::consolidate($settings); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Modify the WP_Editor html to allow autosizing without breaking the `editor-expand` script. |
|
40
|
|
|
* @param string $html |
|
41
|
|
|
* @return string |
|
42
|
|
|
* @filter the_editor |
|
43
|
|
|
*/ |
|
44
|
|
|
public function filterEditorTextarea($html) |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->isReviewEditor()) { |
|
47
|
|
|
$html = str_replace('<textarea', '<div id="ed_toolbar"></div><textarea', $html); |
|
48
|
|
|
} |
|
49
|
|
|
return $html; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param bool $protected |
|
54
|
|
|
* @param string $metaKey |
|
55
|
|
|
* @param string $metaType |
|
56
|
|
|
* @return bool |
|
57
|
|
|
* @filter is_protected_meta |
|
58
|
|
|
*/ |
|
59
|
|
|
public function filterIsProtectedMeta($protected, $metaKey, $metaType) |
|
60
|
|
|
{ |
|
61
|
|
|
if ('post' == $metaType && Str::startsWith('_custom_,_'.glsr()->prefix, $metaKey)) { |
|
62
|
|
|
if ('delete-meta' === filter_input(INPUT_POST, 'action')) { |
|
63
|
|
|
return false; // allow delete but not update |
|
64
|
|
|
} |
|
65
|
|
|
if (glsr()->post_type === get_post_type()) { |
|
66
|
|
|
return false; // display the field in the Custom Fields metabox |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
return $protected; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $messages |
|
74
|
|
|
* @return array |
|
75
|
|
|
* @filter post_updated_messages |
|
76
|
|
|
*/ |
|
77
|
|
|
public function filterUpdateMessages($messages) |
|
78
|
|
|
{ |
|
79
|
|
|
$post = get_post(); |
|
80
|
|
|
if (!$post instanceof WP_Post) { |
|
81
|
|
|
return $messages; |
|
82
|
|
|
} |
|
83
|
|
|
$strings = glsr(UpdatedMessageDefaults::class)->defaults(); |
|
84
|
|
|
$restored = filter_input(INPUT_GET, 'revision'); |
|
85
|
|
|
if ($revisionTitle = wp_post_revision_title(intval($restored), false)) { |
|
86
|
|
|
$restored = sprintf($strings['restored'], $revisionTitle); |
|
87
|
|
|
} |
|
88
|
|
|
$scheduled_date = date_i18n('M j, Y @ H:i', strtotime($post->post_date)); |
|
89
|
|
|
$messages = Arr::consolidate($messages); |
|
90
|
|
|
$messages[glsr()->post_type] = [ |
|
91
|
|
|
1 => $strings['updated'], |
|
92
|
|
|
4 => $strings['updated'], |
|
93
|
|
|
5 => $restored, |
|
94
|
|
|
6 => $strings['published'], |
|
95
|
|
|
7 => $strings['saved'], |
|
96
|
|
|
8 => $strings['submitted'], |
|
97
|
|
|
9 => sprintf($strings['scheduled'], '<strong>'.$scheduled_date.'</strong>'), |
|
98
|
|
|
10 => $strings['draft_updated'], |
|
99
|
|
|
50 => $strings['approved'], |
|
100
|
|
|
51 => $strings['unapproved'], |
|
101
|
|
|
52 => $strings['reverted'], |
|
102
|
|
|
]; |
|
103
|
|
|
return $messages; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return void |
|
108
|
|
|
* @action site-reviews/route/ajax/mce-shortcode |
|
109
|
|
|
*/ |
|
110
|
|
|
public function mceShortcodeAjax(Request $request) |
|
111
|
|
|
{ |
|
112
|
|
|
$shortcode = glsr(Sanitizer::class)->sanitizeText($request->shortcode); |
|
113
|
|
|
$response = false; |
|
114
|
|
|
if ($data = glsr()->retrieve('mce.'.$shortcode, false)) { |
|
115
|
|
|
if (!empty($data['errors'])) { |
|
116
|
|
|
$data['btn_okay'] = [esc_attr_x('Okay', 'admin-text', 'site-reviews')]; |
|
117
|
|
|
} |
|
118
|
|
|
$response = [ |
|
119
|
|
|
'body' => $data['fields'], |
|
120
|
|
|
'close' => $data['btn_close'], |
|
121
|
|
|
'ok' => $data['btn_okay'], |
|
122
|
|
|
'shortcode' => $shortcode, |
|
123
|
|
|
'title' => $data['title'], |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
wp_send_json_success($response); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param WP_Post $post |
|
131
|
|
|
* @return void |
|
132
|
|
|
* @action edit_form_top |
|
133
|
|
|
*/ |
|
134
|
|
|
public function renderReviewNotice($post) |
|
135
|
|
|
{ |
|
136
|
|
|
if (!$this->isReviewEditor()) { |
|
137
|
|
|
return; |
|
138
|
|
|
} |
|
139
|
|
|
if (Review::isReview($post) && !Review::isEditable($post)) { |
|
140
|
|
|
glsr(Notice::class)->addWarning(sprintf( |
|
141
|
|
|
_x('Publicly responding to third-party %s reviews is disabled.', 'admin-text', 'site-reviews'), |
|
142
|
|
|
glsr(ColumnValueType::class)->handle(glsr(Query::class)->review($post->ID)) |
|
143
|
|
|
)); |
|
144
|
|
|
glsr(Template::class)->render('partials/editor/notice', [ |
|
145
|
|
|
'context' => [ |
|
146
|
|
|
'notices' => glsr(Notice::class)->get(), |
|
147
|
|
|
], |
|
148
|
|
|
]); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param int $postId |
|
154
|
|
|
* @param int $messageIndex |
|
155
|
|
|
* @return void |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function redirect($postId, $messageIndex) |
|
158
|
|
|
{ |
|
159
|
|
|
$referer = wp_get_referer(); |
|
160
|
|
|
$hasReferer = !$referer |
|
161
|
|
|
|| Str::contains('post.php', $referer) |
|
162
|
|
|
|| Str::contains('post-new.php', $referer); |
|
163
|
|
|
$redirectUri = $hasReferer |
|
164
|
|
|
? remove_query_arg(['deleted', 'ids', 'trashed', 'untrashed'], $referer) |
|
165
|
|
|
: get_edit_post_link($postId); |
|
166
|
|
|
wp_safe_redirect(add_query_arg(['message' => $messageIndex], $redirectUri)); |
|
167
|
|
|
exit; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|