Skip to content

Commit

Permalink
Refactor init code output and move it closer to the head
Browse files Browse the repository at this point in the history
  • Loading branch information
katzgrau committed Dec 12, 2022
1 parent b0aad8c commit bf1d827
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
26 changes: 16 additions & 10 deletions Broadstreet/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,14 @@ private function _registerHooks()
add_action('admin_menu', array($this, 'adminCallback' ));
add_action('admin_enqueue_scripts', array($this, 'adminStyles'));
add_action('admin_init', array($this, 'adminInitCallback' ));
add_action('wp_enqueue_scripts', array($this, 'addZoneTag' ));
add_action('wp_enqueue_scripts', array($this, 'addCDNScript' ));
add_filter('script_loader_tag', array($this, 'finalizeZoneTag' ));
add_action('init', array($this, 'businessIndexSidebar' ));
add_action('admin_notices', array($this, 'adminWarningCallback'));
add_action('widgets_init', array($this, 'registerWidget'));
add_shortcode('broadstreet', array($this, 'shortcode'));
add_filter('image_size_names_choose', array($this, 'addImageSizes'));
add_action('wp_footer', array($this, 'addPoweredBy'));
add_action('wp_head', array($this, 'setWhitelabel'));
# -- Ad injection
add_action('wp_body_open', array($this, 'addAdsPageTop' ));
add_filter('the_content', array($this, 'addAdsContent'), 20);
Expand Down Expand Up @@ -526,12 +525,15 @@ public function addPoweredBy()
$placement_settings = Broadstreet_Utility::getPlacementSettings();
if (!property_exists($placement_settings, 'load_in_head')
|| !$placement_settings->load_in_head) {
Broadstreet_Utility::writeInitCode();
$code = Broadstreet_Utility::getInitCode();
echo "<script data-cfasync='false'>$code</script>";
}
}

public function setWhitelabel()
public function writeInitCode()
{
$code = '';

# while we're in the post, capture the disabled status of the ads
if (is_singular()) {
self::$_disableAds = Broadstreet_Utility::getPostMeta(get_queried_object_id(), 'bs_ads_disabled') == '1';
Expand All @@ -540,7 +542,7 @@ public function setWhitelabel()
$placement_settings = Broadstreet_Utility::getPlacementSettings();
if (property_exists($placement_settings, 'use_old_tags') && $placement_settings->use_old_tags) {
if (property_exists($placement_settings, 'cdn_whitelabel') && strlen($placement_settings->adserver_whitelabel) > 0) {
echo "<script data-cfasync='false'>broadstreet.setWhitelabel('//{$placement_settings->adserver_whitelabel}/')</script>";
$code .= "broadstreet.setWhitelabel('//{$placement_settings->adserver_whitelabel}/');";
}
}

Expand All @@ -550,8 +552,10 @@ public function setWhitelabel()

if (property_exists($placement_settings, 'load_in_head')
&& $placement_settings->load_in_head) {
Broadstreet_Utility::writeInitCode();
$code .= Broadstreet_Utility::getInitCode();
}

wp_add_inline_script('broadstreet-init', "<script data-cfasync='false'>$code</script>", 'after');
}

/**
Expand All @@ -569,14 +573,14 @@ public function finalizeZoneTag($tag, $handle = '', $src = false)
}

// add cloudflare attrs. but seriously, f cloudflare
if (strstr($tag, 'init-2.min.js') || strstr($tag, 'init.js')) {
$tag = str_replace('src', "data-cfasync='false' src", $tag);
if (strstr($tag, 'broadstreet-init')) {
$tag = str_replace('<script', "<script data-cfasync='false'", $tag);
}

return $tag;
}

public function addZoneTag()
public function addCDNScript()
{
$placement_settings = Broadstreet_Utility::getPlacementSettings();

Expand All @@ -603,7 +607,9 @@ public function addZoneTag()
# $host = 'street-production.s3.amazonaws.com';
# }

wp_enqueue_script('broadstreet-cdn', "//$host/$file");
wp_register_script('broadstreet-init', "//$host/$file");
$this->writeInitCode();
wp_enqueue_script('broadstreet-init');
}
}

Expand Down
20 changes: 11 additions & 9 deletions Broadstreet/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class Broadstreet_Utility
protected static $_placementSettingsCache = NULL;

/**
* Write the initialization code (init.js)
* Get the initialization code (init.js)
*/
public static function writeInitCode() {
public static function getInitCode() {
if (Broadstreet_Utility::isAMPEndpoint()) return;

$placement_settings = Broadstreet_Utility::getPlacementSettings();
Expand All @@ -53,20 +53,22 @@ public static function writeInitCode() {
$args = json_encode($args);
}

echo "<script data-cfasync='false'>window.broadstreetKeywords = [" . Broadstreet_Utility::getAllAdKeywordsString() . "]</script>\n";
echo "<script data-cfasync='false'>window.broadstreetTargets = " . json_encode(Broadstreet_Utility::getTargets()) . ";</script>\n";
$code = "window.broadstreetKeywords = [" . Broadstreet_Utility::getAllAdKeywordsString() . "]\n";
$code .= "window.broadstreetTargets = " . json_encode(Broadstreet_Utility::getTargets()) . ";\n";

echo "<script data-cfasync='false'>\nwindow.broadstreet = window.broadstreet || { run: [] };window.broadstreet.run.push(function () {\n";
$code .= "\nwindow.broadstreet = window.broadstreet || { run: [] };window.broadstreet.run.push(function () {\n";
if (property_exists($placement_settings, 'defer_configuration') && strlen($placement_settings->defer_configuration)) {
if (property_exists($placement_settings, 'cdn_whitelabel') && strlen($placement_settings->adserver_whitelabel) > 0) {
echo "window.broadstreet.loadNetworkJS($network_id, { domain: '$placement_settings->adserver_whitelabel'});\n";
$code.= "window.broadstreet.loadNetworkJS($network_id, { domain: '$placement_settings->adserver_whitelabel'});\n";
} else {
echo "window.broadstreet.loadNetworkJS($network_id);\n";
$code.= "window.broadstreet.loadNetworkJS($network_id, $args);\n";
}
} else {
echo "window.broadstreet.watch($args);\n";
$code .= "window.broadstreet.watch($args);\n";
}
echo " });</script>";
$code .= " });";

return $code;
}

public static function getTrackerCode() {
Expand Down

0 comments on commit bf1d827

Please sign in to comment.