ButtonService.php
103 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @copyright © TMS-Plugins. All rights reserved. |
| 4 | * @licence See LICENCE.md for license details. |
| 5 | */ |
| 6 | |
| 7 | namespace AmeliaBooking\Infrastructure\WP\ButtonService; |
| 8 | |
| 9 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 10 | use AmeliaBooking\Infrastructure\WP\HelperService\HelperService; |
| 11 | use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage; |
| 12 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 13 | |
| 14 | /** |
| 15 | * Class ButtonService |
| 16 | * |
| 17 | * @package AmeliaBooking\Infrastructure\WP\ShortcodeService |
| 18 | */ |
| 19 | class ButtonService |
| 20 | { |
| 21 | /** |
| 22 | * Function that adds shortcode button to WordPress TinyMCE editor on Page and Post pages |
| 23 | */ |
| 24 | public static function renderButton() |
| 25 | { |
| 26 | if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | if (get_user_option('rich_editing') !== 'true') { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | add_filter('mce_external_plugins', [self::class, 'addButton']); |
| 35 | add_filter('mce_buttons', [self::class, 'registerButton']); |
| 36 | // Export JS variables for classic editor |
| 37 | add_action('after_wp_tiny_mce', [self::class, 'exportVars']); |
| 38 | // Export JS variables for Gutenberg editor |
| 39 | add_action('admin_enqueue_scripts', [self::class, 'exportVars']); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Function that add buttons for MCE editor |
| 44 | * |
| 45 | * @param $pluginArray |
| 46 | * |
| 47 | * @return mixed |
| 48 | */ |
| 49 | public static function addButton($pluginArray) |
| 50 | { |
| 51 | $pluginArray['ameliaBookingPlugin'] = AMELIA_URL . 'public/js/tinymce/amelia-mce.js'; |
| 52 | |
| 53 | return $pluginArray; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Function that register buttons for MCE editor |
| 58 | * |
| 59 | * @param $buttons |
| 60 | * |
| 61 | * @return mixed |
| 62 | */ |
| 63 | public static function registerButton($buttons) |
| 64 | { |
| 65 | $buttons[] = 'ameliaButton'; |
| 66 | |
| 67 | return $buttons; |
| 68 | } |
| 69 | |
| 70 | public static function exportVars() |
| 71 | { |
| 72 | HelperService::exportJSVar('wpAmeliaPluginURL', AMELIA_URL); |
| 73 | |
| 74 | wp_enqueue_script('wp-deactivation-amelia', AMELIA_URL . 'public/js/plugins/delete-plugin.js', []); |
| 75 | |
| 76 | wp_enqueue_style('amelia_booking_tinymce_styles', AMELIA_URL . 'public/js/tinymce/amelia-tinymce-styles.css', array(), AMELIA_VERSION); |
| 77 | |
| 78 | $settingsService = new SettingsService(new SettingsStorage()); |
| 79 | |
| 80 | HelperService::exportJSVar( |
| 81 | 'wpAmeliaDeleteSettings', |
| 82 | $settingsService->getSetting('activation', 'deleteTables') ? 1 : 0 |
| 83 | ); |
| 84 | |
| 85 | HelperService::exportJSVar( |
| 86 | 'wpAmeliaNonce', |
| 87 | wp_create_nonce('ajax-nonce') |
| 88 | ); |
| 89 | |
| 90 | HelperService::exportJSVar('wpAmeliaActionURL', AMELIA_ACTION_URL); |
| 91 | |
| 92 | HelperService::exportJSVar( |
| 93 | 'wpAmeliaLabels', |
| 94 | array_merge( |
| 95 | BackendStrings::getCommonStrings(), |
| 96 | BackendStrings::getWordPressStrings() |
| 97 | ) |
| 98 | ); |
| 99 | |
| 100 | HelperService::printJSVars(); |
| 101 | } |
| 102 | } |
| 103 |