| 1 |
<?php |
| 2 |
|
| 3 |
namespace GeminiLabs\SiteReviews\Controllers; |
| 4 |
|
| 5 |
use GeminiLabs\SiteReviews\Commands\RegisterTinymcePopups; |
| 6 |
use GeminiLabs\SiteReviews\Database\ShortcodeOptionManager; |
| 7 |
use GeminiLabs\SiteReviews\Modules\Sanitizer; |
| 8 |
use GeminiLabs\SiteReviews\Request; |
| 9 |
|
| 10 |
class TinymceController extends AbstractController |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @filter site-reviews/enqueue/admin/localize |
| 14 |
*/ |
| 15 |
public function filterAdminVariables(array $variables): array |
| 16 |
{ |
| 17 |
$variables['shortcodes'] = []; |
| 18 |
$variables['tinymce'] = [ |
| 19 |
'glsr_shortcode' => glsr()->url('assets/scripts/mce-plugin.js'), |
| 20 |
]; |
| 21 |
if (!user_can_richedit()) { // @todo why are we checking this? |
| 22 |
return $variables; |
| 23 |
} |
| 24 |
foreach (glsr()->retrieveAs('array', 'mce', []) as $tag => $args) { |
| 25 |
if (empty($args['required'])) { |
| 26 |
continue; |
| 27 |
} |
| 28 |
$variables['shortcodes'][$tag] = $args['required']; |
| 29 |
} |
| 30 |
return $variables; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @action site-reviews/route/ajax/mce-shortcode |
| 35 |
*/ |
| 36 |
public function mceShortcodeAjax(Request $request): void |
| 37 |
{ |
| 38 |
$shortcode = glsr(Sanitizer::class)->sanitizeText($request->shortcode); |
| 39 |
$response = false; |
| 40 |
if ($data = glsr()->retrieve("mce.{$shortcode}", false)) { |
| 41 |
if (!empty($data['errors'])) { |
| 42 |
$data['btn_okay'] = [esc_attr_x('Okay', 'admin-text', 'site-reviews')]; |
| 43 |
} |
| 44 |
$response = [ |
| 45 |
'body' => $data['fields'], |
| 46 |
'close' => $data['btn_close'], |
| 47 |
'hideOptions' => glsr(ShortcodeOptionManager::class)->hide($shortcode), |
| 48 |
'ok' => $data['btn_okay'], |
| 49 |
'shortcode' => $shortcode, |
| 50 |
'title' => $data['title'], |
| 51 |
]; |
| 52 |
} |
| 53 |
wp_send_json_success($response); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @action admin_init |
| 58 |
*/ |
| 59 |
public function registerTinymcePopups(): void |
| 60 |
{ |
| 61 |
$this->execute(new RegisterTinymcePopups()); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @action media_buttons |
| 66 |
*/ |
| 67 |
public function renderTinymceButton(string $editorId): void |
| 68 |
{ |
| 69 |
$allowedEditors = glsr()->filterArray('tinymce/editor-ids', ['content'], $editorId); |
| 70 |
if ('post' !== glsr_current_screen()->base || !in_array($editorId, $allowedEditors)) { |
| 71 |
return; |
| 72 |
} |
| 73 |
$shortcodes = []; |
| 74 |
foreach (glsr()->retrieveAs('array', 'mce', []) as $shortcode => $values) { |
| 75 |
$shortcodes[$shortcode] = $values; |
| 76 |
} |
| 77 |
if (!empty($shortcodes)) { |
| 78 |
$shortcodes = wp_list_sort($shortcodes, 'label', 'ASC', true); // preserve keys |
| 79 |
glsr()->render('partials/editor/tinymce', [ |
| 80 |
'shortcodes' => $shortcodes, |
| 81 |
]); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|