| 1 |
<?php |
| 2 |
|
| 3 |
namespace GeminiLabs\SiteReviews\Controllers; |
| 4 |
|
| 5 |
use GeminiLabs\SiteReviews\Controllers\ListTableColumns\ColumnValueType; |
| 6 |
use GeminiLabs\SiteReviews\Database\ReviewManager; |
| 7 |
use GeminiLabs\SiteReviews\Defaults\UpdatedMessageDefaults; |
| 8 |
use GeminiLabs\SiteReviews\Helpers\Str; |
| 9 |
use GeminiLabs\SiteReviews\Modules\Html\Template; |
| 10 |
use GeminiLabs\SiteReviews\Modules\Notice; |
| 11 |
use GeminiLabs\SiteReviews\Modules\Sanitizer; |
| 12 |
use GeminiLabs\SiteReviews\Request; |
| 13 |
use GeminiLabs\SiteReviews\Review; |
| 14 |
|
| 15 |
class EditorController extends AbstractController |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @filter wp_editor_settings |
| 19 |
*/ |
| 20 |
public function filterEditorSettings(array $settings): array |
| 21 |
{ |
| 22 |
if ($this->isReviewEditor()) { |
| 23 |
$settings = [ |
| 24 |
'media_buttons' => false, |
| 25 |
'quicktags' => false, |
| 26 |
'textarea_rows' => 12, |
| 27 |
'tinymce' => false, |
| 28 |
]; |
| 29 |
} |
| 30 |
return $settings; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Modify the WP_Editor html to allow autosizing without breaking the `editor-expand` script. |
| 35 |
* |
| 36 |
* @filter the_editor |
| 37 |
*/ |
| 38 |
public function filterEditorTextarea(?string $output): string |
| 39 |
{ |
| 40 |
$output = (string) $output; |
| 41 |
if ($this->isReviewEditor()) { |
| 42 |
$output = str_replace('<textarea', '<div id="ed_toolbar"></div><textarea', $output); |
| 43 |
} |
| 44 |
return $output; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @filter is_protected_meta |
| 49 |
*/ |
| 50 |
public function filterIsProtectedMeta(bool $protected, string $metaKey, ?string $metaType): bool |
| 51 |
{ |
| 52 |
if ('post' === $metaType && Str::startsWith((string) $metaKey, ['_custom_', '_'.glsr()->prefix])) { |
| 53 |
if ('delete-meta' === filter_input(\INPUT_POST, 'action')) { |
| 54 |
return false; // allow delete but not update |
| 55 |
} |
| 56 |
if (glsr()->post_type === get_post_type()) { |
| 57 |
return false; // display the field in the Custom Fields metabox |
| 58 |
} |
| 59 |
} |
| 60 |
return $protected; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @param array[] $messages |
| 65 |
* |
| 66 |
* @filter post_updated_messages |
| 67 |
*/ |
| 68 |
public function filterUpdateMessages(array $messages): array |
| 69 |
{ |
| 70 |
$post = get_post(); |
| 71 |
if (!$post instanceof \WP_Post) { |
| 72 |
return $messages; |
| 73 |
} |
| 74 |
$strings = glsr(UpdatedMessageDefaults::class)->defaults(); |
| 75 |
$restored = filter_input(\INPUT_GET, 'revision'); |
| 76 |
if ($revisionTitle = wp_post_revision_title(intval($restored), false)) { |
| 77 |
$restored = sprintf($strings['restored'], $revisionTitle); |
| 78 |
} |
| 79 |
$scheduled_date = date_i18n('M j, Y @ H:i', strtotime($post->post_date)); |
| 80 |
$messages[glsr()->post_type] = [ |
| 81 |
1 => $strings['updated'], |
| 82 |
4 => $strings['updated'], |
| 83 |
5 => $restored, |
| 84 |
6 => $strings['published'], |
| 85 |
7 => $strings['saved'], |
| 86 |
8 => $strings['submitted'], |
| 87 |
9 => sprintf($strings['scheduled'], "<strong>{$scheduled_date}</strong>"), |
| 88 |
10 => $strings['draft_updated'], |
| 89 |
50 => $strings['approved'], |
| 90 |
51 => $strings['unapproved'], |
| 91 |
52 => $strings['reverted'], |
| 92 |
]; |
| 93 |
return $messages; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @action edit_form_top |
| 98 |
*/ |
| 99 |
public function renderReviewNotice(\WP_Post $post): void |
| 100 |
{ |
| 101 |
if (!$this->isReviewEditor()) { |
| 102 |
return; |
| 103 |
} |
| 104 |
if (Review::isReview($post) && !Review::isEditable($post)) { |
| 105 |
$review = glsr(ReviewManager::class)->get($post->ID); |
| 106 |
glsr(Notice::class)->addWarning(sprintf( |
| 107 |
/* translators: %s: the review type */ |
| 108 |
_x('Publicly responding to third-party %s reviews is disabled.', 'admin-text', 'site-reviews'), |
| 109 |
$review->type() |
| 110 |
)); |
| 111 |
glsr(Template::class)->render('partials/editor/notice', [ |
| 112 |
'context' => [ |
| 113 |
'notices' => glsr(Notice::class)->get(), |
| 114 |
], |
| 115 |
]); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|