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