| @@ -1,7 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace Booktics\Assets; |
| 3 | 4 | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) exit; | |
| 6 | + | |
| 4 | 7 | /** |
| 5 | 8 | * Manage all admin scripts and styles |
| 6 | 9 | */ |
| 7 | 10 | class Admin_Assets extends Base_Assets { |
| @@ -10,10 +13,14 @@ | ||
| 10 | 13 | * |
| 11 | 14 | * @return void |
| 12 | 15 | */ |
| 13 | 16 | public function register() { |
| 14 | - add_action( 'admin_enqueue_scripts', [$this, 'register_styles_scripts'] ); | |
| 15 | - add_action( 'admin_enqueue_scripts', [$this, 'enqueue'] ); | |
| 17 | + add_action( 'admin_enqueue_scripts', [ | |
| 18 | + $this, | |
| 19 | + 'register_styles_scripts' | |
| 20 | + ] ); | |
| 21 | + add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] ); | |
| 22 | + add_action( 'admin_footer', [ $this, 'admin_helpscout_beacon' ] ); | |
| 16 | 23 | } |
| 17 | 24 | |
| 18 | 25 | /** |
| 19 | 26 | * Enqueue scripts and styles |
| @@ -20,8 +27,13 @@ | ||
| 20 | 27 | * |
| 21 | 28 | * @return void |
| 22 | 29 | */ |
| 23 | 30 | public function enqueue() { |
| 31 | + $screen = get_current_screen(); | |
| 32 | + if ( $screen->base !== 'toplevel_page_booktics') { | |
| 33 | + return; | |
| 34 | + } | |
| 35 | + $this->enqueue_i18n_loader(); | |
| 24 | 36 | wp_enqueue_style( 'wp-color-picker' ); |
| 25 | 37 | wp_enqueue_style( 'booktics-admin-style' ); |
| 26 | 38 | wp_enqueue_style( 'booktics-vendor' ); |
| 27 | 39 | |
| @@ -30,8 +42,14 @@ | ||
| 30 | 42 | wp_enqueue_script( 'booktics-packages' ); |
| 31 | 43 | wp_enqueue_script( 'booktics-dashboard-scripts' ); |
| 32 | 44 | |
| 33 | 45 | wp_localize_script( 'booktics-packages', 'booktics', Localize::get_admin() ); |
| 46 | + wp_localize_script( 'booktics-packages', 'booktics_enabled_modules', booktics_get_enabled_modules() ); | |
| 47 | + | |
| 48 | + if ( function_exists( 'wp_set_script_translations' ) ) { | |
| 49 | + wp_set_script_translations( 'booktics-dashboard-scripts', 'booktics' ); | |
| 50 | + } | |
| 51 | + | |
| 34 | 52 | } |
| 35 | 53 | |
| 36 | 54 | /** |
| 37 | 55 | * Get all scripts |
| @@ -39,21 +57,29 @@ | ||
| 39 | 57 | * @return array List register scripts |
| 40 | 58 | */ |
| 41 | 59 | public function get_scripts(): array { |
| 42 | 60 | $scripts = [ |
| 43 | - 'booktics-dashboard-scripts' => [ | |
| 61 | + 'booktics-dashboard-scripts' => [ | |
| 44 | 62 | 'src' => booktics()->assets_url . '/build/js/dashboard.js', |
| 45 | - 'deps' => ['booktics-packages'], | |
| 63 | + 'deps' => [ 'booktics-packages' ], | |
| 46 | 64 | 'in_footer' => true, |
| 47 | 65 | ], |
| 48 | - 'booktics-flatpickr-scripts' => [ | |
| 66 | + 'booktics-flatpickr-scripts' => [ | |
| 49 | 67 | 'src' => booktics()->assets_url . '/js/vendors/flatpickr.js', |
| 50 | 68 | 'deps' => [], |
| 51 | 69 | 'in_footer' => true, |
| 52 | 70 | ], |
| 53 | - 'booktics-packages' => [ | |
| 71 | + 'booktics-packages' => [ | |
| 54 | 72 | 'src' => booktics()->assets_url . '/build/js/packages.js', |
| 55 | - 'deps' => [], | |
| 73 | + 'deps' => [ | |
| 74 | + 'wp-api-fetch', | |
| 75 | + 'wp-components', | |
| 76 | + 'wp-compose', | |
| 77 | + 'wp-i18n', | |
| 78 | + 'wp-element', | |
| 79 | + 'wp-data', | |
| 80 | + 'wp-hooks', | |
| 81 | + ], | |
| 56 | 82 | 'in_footer' => true, |
| 57 | 83 | ], |
| 58 | 84 | ]; |
| 59 | 85 | |
| @@ -66,15 +92,71 @@ | ||
| 66 | 92 | * @return array |
| 67 | 93 | */ |
| 68 | 94 | public function get_styles(): array { |
| 69 | 95 | $styles = [ |
| 70 | - 'booktics-admin-style' => [ | |
| 96 | + 'booktics-admin-style' => [ | |
| 71 | 97 | 'src' => booktics()->assets_url . '/build/css/admin.css', |
| 72 | 98 | ], |
| 73 | - 'booktics-vendor' => [ | |
| 99 | + 'booktics-vendor' => [ | |
| 74 | 100 | 'src' => booktics()->assets_url . '/build/css/vendor.css', |
| 75 | 101 | ], |
| 76 | 102 | ]; |
| 77 | 103 | |
| 78 | 104 | return apply_filters( 'booktics_admin_styles', $styles ); |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Add HelpScout script to admin footer | |
| 109 | + * | |
| 110 | + * @return void | |
| 111 | + */ | |
| 112 | + public function admin_helpscout_beacon() { | |
| 113 | + $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; | |
| 114 | + $is_booktics_screen = false; | |
| 115 | + | |
| 116 | + if ( $screen ) { | |
| 117 | + $screen_id = isset( $screen->id ) ? $screen->id : ''; | |
| 118 | + | |
| 119 | + // Check: only load on Booktics top-level admin page | |
| 120 | + if ( 'toplevel_page_booktics' === $screen_id ) { | |
| 121 | + $is_booktics_screen = true; | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + // Allow overriding detection via filter. | |
| 126 | + $is_booktics_screen = apply_filters( 'booktics_is_admin_screen', $is_booktics_screen, $screen ); | |
| 127 | + | |
| 128 | + if ( ! $is_booktics_screen ) { | |
| 129 | + return; | |
| 130 | + } | |
| 131 | + ?> | |
| 132 | +<script type="text/javascript"> | |
| 133 | +! function(e, t, n) { | |
| 134 | + function a() { | |
| 135 | + var e = t.getElementsByTagName("script")[0], | |
| 136 | + n = t.createElement("script"); | |
| 137 | + n.type = "text/javascript", n.async = !0, n.src = "https://beacon-v2.helpscout.net", e.parentNode.insertBefore( | |
| 138 | + n, e) | |
| 139 | + } | |
| 140 | + if (e.Beacon = n = function(t, n, a) { | |
| 141 | + e.Beacon.readyQueue.push({ | |
| 142 | + method: t, | |
| 143 | + options: n, | |
| 144 | + data: a | |
| 145 | + }) | |
| 146 | + }, n.readyQueue = [], "complete" === t.readyState) a(); | |
| 147 | + else if (e.attachEvent) e.attachEvent("onload", a); | |
| 148 | + else e.addEventListener("load", a, !1) | |
| 149 | +}(window, document, window.Beacon || function() {}); | |
| 150 | +</script> | |
| 151 | +<script type="text/javascript"> | |
| 152 | +window.Beacon('config', { | |
| 153 | + color: "#22B855", | |
| 154 | +}); | |
| 155 | +window.Beacon('init', '0f1a82db-0aa9-4bac-accc-ee1bb81d4940'); | |
| 156 | +window.Beacon('on', 'ready', function() { | |
| 157 | + window.Beacon('close'); | |
| 158 | +}); | |
| 159 | +</script> | |
| 160 | +<?php | |
| 79 | 161 | } |
| 80 | 162 | } |