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