// // It is ENQUEUED like any script, so WordPress decides where it goes and a caching or consent // plugin can see and hold it. The script_loader_tag filter then rebuilds our tag with // wp_get_script_tag() from exactly three attributes, because WordPress's own would add an id the // button does not need. wp_get_script_tag() escapes every value and passes the attributes through // `wp_script_attributes`, where a security plugin adds its nonce. WordPress chooses their order // (7.1 writes async first), which HTML ignores, and an older theme without HTML5 script support // gets WordPress's older spelling (`type="text/javascript"`, `async="async"`). // The plugin prints no credit and no link of its own. if (!defined('ABSPATH')) { exit; } define('ODROP_SUPPORT_LAUNCHER_HANDLE', 'odrop-support-launcher'); define('ODROP_SUPPORT_LAUNCHER_FILE', 'w/odrop.js'); /** The button's script tag attributes for a card: the script's address on this site, the card, async. */ function odrop_support_launcher_attributes(string $src, string $card): array { return [ 'src' => esc_url($src), 'data-card' => $card, 'async' => true, ]; } /** Enqueue the button's script on the front end, only when a card is saved. */ function odrop_support_enqueue_launcher(): void { if (odrop_support_saved_card() === '') { return; } $src = plugins_url(ODROP_SUPPORT_LAUNCHER_FILE, ODROP_SUPPORT_PLUGIN_FILE); wp_enqueue_script(ODROP_SUPPORT_LAUNCHER_HANDLE, $src, [], ODROP_SUPPORT_VERSION, true); } add_action('wp_enqueue_scripts', 'odrop_support_enqueue_launcher'); /** Write our enqueued tag with the card. Every other script's tag is left alone. */ function odrop_support_launcher_tag($tag, $handle, $src = '') { if ($handle !== ODROP_SUPPORT_LAUNCHER_HANDLE) { return $tag; } $card = odrop_support_saved_card(); return ($card === '' || $src === '') ? '' : wp_get_script_tag(odrop_support_launcher_attributes((string) $src, $card)); } add_filter('script_loader_tag', 'odrop_support_launcher_tag', 10, 3);