Last active
November 10, 2022 22:34
-
-
Save gaswirth/b77950ee51a7c2182176d018283d063c to your computer and use it in GitHub Desktop.
WordPress manual header/footer embeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Embeds, Pixels, and Codes | |
* Description: Manual header and footer embeds for ads, tracking, CRMs, etc. | |
* Author: Roundhouse Designs | |
* Author URI: https://roundhouse-designs.com | |
* Version: 0.0.1 | |
* | |
* @package rhd | |
*/ | |
/** | |
* Handles embedding inline header and footer scripts. | |
*/ | |
class RHD_Embeds { | |
protected $header; | |
protected $footer; | |
private $is_multisite; | |
private $current_site; | |
/** | |
* Class constructor. | |
* | |
* @param array $header An array of embeds. | |
* @param string $header[]['label'] The snippet label. | |
* @param string $header[]['content'] The embed content. | |
* @param array $footer An array of embeds. | |
* @param string $footer[]['label'] The snippet label. | |
* @param string $footer[]['content'] The embed content. | |
*/ | |
public function __construct( $header, $footer ) { | |
$this->header = $header; | |
$this->footer = $footer; | |
$this->is_multisite = is_multisite(); | |
$this->current_site = get_current_blog_id(); | |
add_action( 'wp_head', array( $this, 'rhd_embed_head' ), 10, 1 ); | |
add_action( 'wp_footer', array( $this, 'rhd_embed_footer' ) ); | |
} | |
/** | |
* Strips whitespace and newlines from text. | |
* | |
* @param string $content The content to minimize. | |
* @return string The minimized content. | |
*/ | |
private function minimize( $content ) { | |
$pattern = array( '/^\s+|\s+$/m' ); | |
$replace = ''; | |
$trimmed = preg_replace( $pattern, $replace, $content ); | |
$flattened = str_replace( PHP_EOL, '', $trimmed ); | |
return $flattened; | |
} | |
/** | |
* Prints the embed. | |
* | |
* @param array $embed The embed data. | |
* @param string $embed[]['label'] The snippet label. | |
* @param string $embed[]['content'] The embed content. | |
* @param int $embed[]['site_id'] The site ID, if multisite (All sites if omitted). | |
* @param boolean $embed[]['minimize'] (default: true) Trim the embed content whitespace before output. | |
* @return void | |
*/ | |
private function embed( $embed ) { | |
printf( '<!-- %s -->', $embed['label'] ); | |
if ( isset( $embed['minimize'] ) && $embed['minimize'] ) { | |
echo $this->minimize( $embed['content'] ); | |
} else { | |
echo $embed['content']; | |
} | |
echo "\n"; | |
} | |
/** | |
* Embed scripts into the <head> element. | |
* | |
* @param array $embeds | |
* @param string $embeds[]['label'] The snippet label. | |
* @param string $embeds[]['content'] The embed content. | |
* @param int $embed[]['site_id'] The site ID, if multisite (All sites if omitted). | |
* @param boolean $embeds[]['minimize'] The embed content. | |
* @return void | |
*/ | |
public function rhd_embed_head() { | |
// TODO omit header if no embeds (or not on right multisite) | |
echo "\n<!-- RHD Header embeds -->\n"; | |
foreach ( $this->header as $embed ) { | |
// Multisite: do not embed if a `site_id` is set and it doesn't match. | |
if ( $this->is_multisite && $embed['site_id'] !== $this->current_site ) { | |
return; | |
} | |
$this->embed( $embed ); | |
} | |
echo "\n"; | |
} | |
/** | |
* Embed scripts after the closing </body> tag. | |
* | |
* @return void | |
*/ | |
public function rhd_embed_footer() { | |
// TODO omit header if no embeds (or not on right multisite) | |
echo "\n<!-- RHD Footer embeds -->\n"; | |
foreach ( $this->footer as $embed ) { | |
// Multisite: do not embed if a `site_id` is set and it doesn't match. | |
if ( $this->is_multisite && $embed['site_id'] !== $this->current_site ) { | |
return; | |
} | |
$this->embed( $embed ); | |
} | |
echo "\n"; | |
} | |
} | |
/** | |
* Initialize embeds. | |
*/ | |
$embeds = new RHD_Embeds( | |
// Header embeds. | |
array( | |
array( | |
'label' => '', | |
'content' => '', | |
'site_id' => null, | |
'minimize' => true, | |
), | |
), | |
// Footer embeds. | |
array( | |
array( | |
'label' => '', | |
'content' => '', | |
'site_id' => null, | |
'minimize' => true, | |
), | |
), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment