ButtonService.php
109 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 | global $pagenow; |
| 27 | |
| 28 | if (is_admin() && isset($pagenow) && 'customize.php' === $pagenow) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | if (get_user_option('rich_editing') !== 'true') { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | add_filter('mce_external_plugins', [self::class, 'addButton']); |
| 41 | add_filter('mce_buttons', [self::class, 'registerButton']); |
| 42 | // Export JS variables for classic editor |
| 43 | add_action('after_wp_tiny_mce', [self::class, 'exportVars']); |
| 44 | // Export JS variables for Gutenberg editor |
| 45 | add_action('admin_enqueue_scripts', [self::class, 'exportVars']); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Function that add buttons for MCE editor |
| 50 | * |
| 51 | * @param $pluginArray |
| 52 | * |
| 53 | * @return mixed |
| 54 | */ |
| 55 | public static function addButton($pluginArray) |
| 56 | { |
| 57 | $pluginArray['ameliaBookingPlugin'] = AMELIA_URL . 'public/js/tinymce/amelia-mce.js'; |
| 58 | |
| 59 | return $pluginArray; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Function that register buttons for MCE editor |
| 64 | * |
| 65 | * @param $buttons |
| 66 | * |
| 67 | * @return mixed |
| 68 | */ |
| 69 | public static function registerButton($buttons) |
| 70 | { |
| 71 | $buttons[] = 'ameliaButton'; |
| 72 | |
| 73 | return $buttons; |
| 74 | } |
| 75 | |
| 76 | public static function exportVars() |
| 77 | { |
| 78 | HelperService::exportJSVar('wpAmeliaPluginURL', AMELIA_URL); |
| 79 | |
| 80 | wp_enqueue_script('wp-deactivation-amelia', AMELIA_URL . 'public/js/plugins/delete-plugin.js', []); |
| 81 | |
| 82 | wp_enqueue_style('amelia_booking_tinymce_styles', AMELIA_URL . 'public/js/tinymce/amelia-tinymce-styles.css', array(), AMELIA_VERSION); |
| 83 | |
| 84 | $settingsService = new SettingsService(new SettingsStorage()); |
| 85 | |
| 86 | HelperService::exportJSVar( |
| 87 | 'wpAmeliaDeleteSettings', |
| 88 | $settingsService->getSetting('activation', 'deleteTables') ? 1 : 0 |
| 89 | ); |
| 90 | |
| 91 | HelperService::exportJSVar( |
| 92 | 'wpAmeliaNonce', |
| 93 | wp_create_nonce('ajax-nonce') |
| 94 | ); |
| 95 | |
| 96 | HelperService::exportJSVar('wpAmeliaActionURL', AMELIA_ACTION_URL); |
| 97 | |
| 98 | HelperService::exportJSVar( |
| 99 | 'wpAmeliaLabels', |
| 100 | array_merge( |
| 101 | BackendStrings::getCommonStrings(), |
| 102 | BackendStrings::getWordPressStrings() |
| 103 | ) |
| 104 | ); |
| 105 | |
| 106 | HelperService::printJSVars(); |
| 107 | } |
| 108 | } |
| 109 |