From fb1789343cc4b7a7b2dad72b8de993afc9110449 Mon Sep 17 00:00:00 2001 From: Jaroslaw Rozanski Date: Thu, 30 Dec 2021 12:58:35 +0100 Subject: [PATCH] Persist individual attributes; render last --- ...ngleConfig.php => WideAngleAttributes.php} | 40 ++++------- types/WideAngleGenerator.php | 44 +++++++++++++ views/admin_settings.php | 5 +- wide-angle-analytics.php | 66 +++++++++---------- 4 files changed, 90 insertions(+), 65 deletions(-) rename types/{WideAngleConfig.php => WideAngleAttributes.php} (57%) create mode 100644 types/WideAngleGenerator.php diff --git a/types/WideAngleConfig.php b/types/WideAngleAttributes.php similarity index 57% rename from types/WideAngleConfig.php rename to types/WideAngleAttributes.php index 137d462..9be391b 100644 --- a/types/WideAngleConfig.php +++ b/types/WideAngleAttributes.php @@ -1,5 +1,5 @@ helpers = new WideAngleHelpers(); } - function generateHeaderScript() { - $script = << -EOD; - return $script; + public function generateAttributes() { + return array( + 'site_id' => $this->siteId, + 'tracker_domain' => $this->trackerDomain, + 'ignore_hash' => $this->ignoreHash, + 'exclusion_paths' => $this->generateExclusionsAttribute(), + 'include_params' => $this->generateIncludeParamsAttribute() + ); } - function generateFooterScript() { - $pathExlusionsAttribute = $this->generateExclusionsAttribute(); - $includeParamsAttribute = $this->generateIncludeParamsAttribute(); - $trackerUrlAttribute = esc_attr("https://{$this->trackerDomain}/script/{$this->siteId}.js"); - $ignoreHashAttribute = esc_attr($this->ignoreHash); - $script = << -EOD; - return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $script); - } private function generateIncludeParamsAttribute() { $params = $this->helpers->parseIncludeParamsSetting($this->includeParamsString); - if(sizeof($params) > 0) { - return "data-waa-inc-params=\"" . esc_attr(implode(",", $params)) . "\""; - } - return ""; + return implode(",", $params); } private function generateExclusionsAttribute() { @@ -53,11 +39,7 @@ EOD; $pathExlusionsAttribute = $this->generateExclusionsAttributeValue($exclusions); } - $pathExlusionsAttributeWithKey = ""; - if(trim($pathExlusionsAttribute) != "") { - $pathExlusionsAttributeWithKey = "data-waa-exc-paths=\"" . esc_attr($pathExlusionsAttribute) ."\""; - } - return $pathExlusionsAttributeWithKey; + return $pathExlusionsAttribute; } private function generateExclusionsAttributeValue($exclusions) { diff --git a/types/WideAngleGenerator.php b/types/WideAngleGenerator.php new file mode 100644 index 0000000..a0a49af --- /dev/null +++ b/types/WideAngleGenerator.php @@ -0,0 +1,44 @@ +siteId = $attributes['site_id']; + $this->trackerDomain = $attributes['tracker_domain']; + $this->ignoreHash = $attributes['ignore_hash']; + $this->exclusionPaths = $attributes['exclusion_paths']; + $this->includeParams = $attributes['include_params']; + } + + + function generateHeaderScript() { + $href = esc_attr("https://{$this->trackerDomain}/script/{$this->siteId}.js"); + $script = << +EOD; + return $script; + } + + function generateFooterScript() { + $trackerUrlAttribute = esc_attr("https://{$this->trackerDomain}/script/{$this->siteId}.js"); + $pathExlusionsAttribute = $this->exclusionPaths != '' ? "data-waa-exc-paths=\"" . esc_attr($this->exclusionPaths) . "\"": ''; + $includeParamsAttribute = $this->includeParams != '' ? "data-waa-inc-params=\"" . esc_attr($this->includeParams) . "\"": ''; + $ignoreHashAttribute = $this->ignoreHash != '' ? "data-waa-ignore-hash=\"" . esc_attr($this->ignoreHash) . "\"": 'data-waa-ignore-hash="false"'; + $script = << +EOD; + return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $script); + } + +} + +?> \ No newline at end of file diff --git a/views/admin_settings.php b/views/admin_settings.php index 86366cd..90a7c52 100644 --- a/views/admin_settings.php +++ b/views/admin_settings.php @@ -4,6 +4,7 @@ $trackerDomain = $this->settings[self::WAA_CONF_TRACKER_DOMAIN]; $ignoreHash = filter_var($this->settings[self::WAA_CONF_IGNORE_HASH], FILTER_VALIDATE_BOOLEAN); $parsedExclusions = $this->plugin->helpers->parseExclusionSetting($this->settings[self::WAA_CONF_EXC_PATHS]); $parsedIncludeParams = $this->plugin->helpers->parseIncludeParamsSetting($this->settings[self::WAA_CONF_INC_PARAMS]); +$generator = new WideAngleGenerator($this->settings[self::WAA_CONF_ATTRIBUTES]); ?>

@@ -136,11 +137,11 @@ $parsedIncludeParams = $this->plugin->helpers->parseIncludeParamsSetting($this-
 <head>
 <!-- .. -->
-settings[self::WAA_CONF_GENERATED_HEADER_SCRIPT]); ?>
+generateHeaderScript()); ?>
 
 </head>
 <!-- .. -->
-settings[self::WAA_CONF_GENERATED_FOOTER_SCRIPT]); ?>
+generateFooterScript()); ?>
 

diff --git a/wide-angle-analytics.php b/wide-angle-analytics.php index 0777b7e..3a46d7c 100644 --- a/wide-angle-analytics.php +++ b/wide-angle-analytics.php @@ -20,8 +20,7 @@ class WideAngleAnalytics { const WAA_CONF_EXC_PATHS = "waa_exc_path"; const WAA_CONF_INC_PARAMS = "waa_inc_params"; const WAA_CONF_IGNORE_HASH = "waa_ignore_hash"; - const WAA_CONF_GENERATED_HEADER_SCRIPT = "waa_header_script"; - const WAA_CONF_GENERATED_FOOTER_SCRIPT = "waa_footer_script"; + const WAA_CONF_ATTRIBUTES = "waa_attributes"; public function __construct() { $this->plugin = new stdClass; @@ -29,6 +28,7 @@ class WideAngleAnalytics { $this->plugin->displayName = 'Wide Angle Analytics'; $this->plugin->folder = plugin_dir_path( __FILE__ ); include_once( $this->plugin->folder . '/types/WideAngleHelpers.php' ); + include_once( $this->plugin->folder . '/types/WideAngleGenerator.php' ); $this->plugin->helpers = new WideAngleHelpers(); $this->plugin->exclusionTypes = array( @@ -48,7 +48,8 @@ class WideAngleAnalytics { * Inteded to be used with 'wp_head' hook. */ function renderHeaderScript() { - $this->renderSetting(self::WAA_CONF_GENERATED_HEADER_SCRIPT); + $generator = new WideAngleGenerator(get_option(self::WAA_CONF_ATTRIBUTES)); + $this->renderContent($generator->generateHeaderScript()); // The Escaping takes place in the WideAngleGenerator::generateHeaderScript method. } /** @@ -56,14 +57,16 @@ class WideAngleAnalytics { * Inteded to be used with 'wp_footer' hook. */ function renderFooterScript() { - $this->renderSetting(self::WAA_CONF_GENERATED_FOOTER_SCRIPT); + $generator = new WideAngleGenerator(get_option(self::WAA_CONF_ATTRIBUTES)); + $this->renderContent($generator->generateHeaderScript()); // The Escaping takes place in the WideAngleGenerator::generateHeaderScript method. } /** - * When called, renders content of named setting. - * Rendering is omitted if current view is: Admin, Feed, Robots or Trackback. + * Helper function to render provided content "as-is" when rendering public facing page. + * + * The $content is already escaped. */ - function renderSetting($setting) { + private function renderContent($content) { if ( is_admin() || is_feed() || @@ -71,12 +74,10 @@ class WideAngleAnalytics { is_trackback()) { return; } - - $script = get_option($setting); - if(trim($script) === "") { + if(trim($content) === "") { return; } - echo wp_unslash($script); + echo wp_unslash($content); } /** @@ -111,13 +112,13 @@ class WideAngleAnalytics { } elseif ( ! wp_verify_nonce( $_REQUEST[ $this->plugin->name . '_nonce' ], $this->plugin->name ) ) { $this->errorMessage = __( 'Invalid nonce specified. Settings NOT saved.', $this->plugin->name ); } else { - $waaSiteId = $this->plugin->helpers->validateSiteId(self::WAA_CONF_SITE_ID, $_REQUEST['waa_site_id']); - $waaTrackerDomain = $this->plugin->helpers->validateTrackerDomain(self::WAA_CONF_TRACKER_DOMAIN, $_REQUEST['waa_tracker_domain']); - $waaIgnoreHash = $this->plugin->helpers->validateIgnoreHashFlag(self::WAA_CONF_IGNORE_HASH, $_REQUEST['waa_ignore_hash']); - $waaIncParams = $this->plugin->helpers->validateIncludeParams(self::WAA_CONF_INC_PARAMS, $_REQUEST); - $waaExclusionPaths = $this->plugin->helpers->validateExclusionPathsRequest(self::WAA_CONF_EXC_PATHS, $_REQUEST); + $waaSiteId = $this->plugin->helpers->validateSiteId(self::WAA_CONF_SITE_ID, $_REQUEST['waa_site_id']); + $waaTrackerDomain = $this->plugin->helpers->validateTrackerDomain(self::WAA_CONF_TRACKER_DOMAIN, $_REQUEST['waa_tracker_domain']); + $waaIgnoreHash = $this->plugin->helpers->validateIgnoreHashFlag(self::WAA_CONF_IGNORE_HASH, $_REQUEST['waa_ignore_hash']); + $waaIncParams = $this->plugin->helpers->validateIncludeParams(self::WAA_CONF_INC_PARAMS, $_REQUEST); + $waaExclusionPaths = $this->plugin->helpers->validateExclusionPathsRequest(self::WAA_CONF_EXC_PATHS, $_REQUEST); - include_once( $this->plugin->folder . '/types/WideAngleConfig.php'); + include_once( $this->plugin->folder . '/types/WideAngleAttributes.php'); $merged = array($waaSiteId, $waaTrackerDomain, $waaIgnoreHash, $waaIncParams, $waaExclusionPaths); $errors = array(); foreach($merged as $validated) { @@ -127,15 +128,14 @@ class WideAngleAnalytics { } if(count($errors) === 0) { - $config = new WideAngleConfig($waaSiteId->get_value(), $waaTrackerDomain->get_value(), $waaIgnoreHash->get_value(), $waaExclusionPaths->get_value(), $waaIncParams->get_value()); + $attributes = new WideAngleAttributes($waaSiteId->get_value(), $waaTrackerDomain->get_value(), $waaIgnoreHash->get_value(), $waaExclusionPaths->get_value(), $waaIncParams->get_value()); - update_option(self::WAA_CONF_GENERATED_FOOTER_SCRIPT, $config->generateFooterScript()); - update_option(self::WAA_CONF_GENERATED_HEADER_SCRIPT, $config->generateHeaderScript()); - update_option(self::WAA_CONF_SITE_ID, $waaSiteId->get_value()); - update_option(self::WAA_CONF_TRACKER_DOMAIN, $waaTrackerDomain->get_value()); - update_option(self::WAA_CONF_IGNORE_HASH, $waaIgnoreHash->get_value()); - update_option(self::WAA_CONF_EXC_PATHS, $waaExclusionPaths->get_value()); - update_option(self::WAA_CONF_INC_PARAMS, $waaIncParams->get_value()); + update_option(self::WAA_CONF_SITE_ID, $waaSiteId->get_value()); + update_option(self::WAA_CONF_TRACKER_DOMAIN, $waaTrackerDomain->get_value()); + update_option(self::WAA_CONF_IGNORE_HASH, $waaIgnoreHash->get_value()); + update_option(self::WAA_CONF_EXC_PATHS, $waaExclusionPaths->get_value()); + update_option(self::WAA_CONF_INC_PARAMS, $waaIncParams->get_value()); + update_option(self::WAA_CONF_ATTRIBUTES, $attributes->generateAttributes()); $this->message = __('Settings updated', $this->plugin->name); } else { $this->errorMessage = $errors; @@ -143,13 +143,12 @@ class WideAngleAnalytics { } } $this->settings = array( - self::WAA_CONF_SITE_ID => get_option( self::WAA_CONF_SITE_ID), - self::WAA_CONF_EXC_PATHS => get_option( self::WAA_CONF_EXC_PATHS), - self::WAA_CONF_INC_PARAMS => get_option( self::WAA_CONF_INC_PARAMS), - self::WAA_CONF_TRACKER_DOMAIN => get_option( self::WAA_CONF_TRACKER_DOMAIN), - self::WAA_CONF_IGNORE_HASH => get_option( self::WAA_CONF_IGNORE_HASH), - self::WAA_CONF_GENERATED_HEADER_SCRIPT => get_option( self::WAA_CONF_GENERATED_HEADER_SCRIPT), - self::WAA_CONF_GENERATED_FOOTER_SCRIPT => get_option( self::WAA_CONF_GENERATED_FOOTER_SCRIPT), + self::WAA_CONF_SITE_ID => get_option(self::WAA_CONF_SITE_ID), + self::WAA_CONF_EXC_PATHS => get_option(self::WAA_CONF_EXC_PATHS), + self::WAA_CONF_INC_PARAMS => get_option(self::WAA_CONF_INC_PARAMS), + self::WAA_CONF_TRACKER_DOMAIN => get_option(self::WAA_CONF_TRACKER_DOMAIN), + self::WAA_CONF_IGNORE_HASH => get_option(self::WAA_CONF_IGNORE_HASH), + self::WAA_CONF_ATTRIBUTES => get_option(self::WAA_CONF_ATTRIBUTES) ); include_once( $this->plugin->folder . '/views/admin_settings.php' ); } @@ -172,8 +171,7 @@ class WideAngleAnalytics { register_setting($this->plugin->name, self::WAA_CONF_INC_PARAMS); register_setting($this->plugin->name, self::WAA_CONF_TRACKER_DOMAIN, array('default' => 'stats.wideangle.co')); register_setting($this->plugin->name, self::WAA_CONF_IGNORE_HASH, array('default' => 'false')); - register_setting($this->plugin->name, self::WAA_CONF_GENERATED_HEADER_SCRIPT); - register_setting($this->plugin->name, self::WAA_CONF_GENERATED_FOOTER_SCRIPT); + register_setting($this->plugin->name, self::WAA_CONF_ATTRIBUTES, array('type' => 'array')); } }