From 4dff290de94477b2432e4bdddf31e9ef8041a90b Mon Sep 17 00:00:00 2001 From: Erick Hitter <services@ethitter.com> Date: Sun, 21 Feb 2016 20:25:05 -0800 Subject: [PATCH] Utility method for retrieving options --- eth-amp-analytics-wp.php | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/eth-amp-analytics-wp.php b/eth-amp-analytics-wp.php index 0ecbec0..dae97f7 100644 --- a/eth-amp-analytics-wp.php +++ b/eth-amp-analytics-wp.php @@ -89,16 +89,10 @@ class ETH_AMP_Analytics_WP { * Conditionally load front-end hooks */ public function action_wp_loaded() { - $ga_options = get_option( $this->plugin_option_name ); - - if ( is_array( $ga_options ) ) { - $this->options = wp_parse_args( $ga_options, $this->plugin_option_defaults ); - + if ( ! empty( $this->get_option( 'property_id' ) ) ) { add_filter( 'amp_component_scripts', array( $this, 'filter_amp_component_scripts' ) ); add_action( 'amp_post_template_footer', array( $this, 'action_amp_post_template_footer' ) ); } - - unset( $ga_options ); } /** @@ -168,6 +162,35 @@ class ETH_AMP_Analytics_WP { public function sanitize_options( $options ) { return $options; } + + /** + * UTILITY FUNCTIONS + */ + + /** + * + */ + private function get_option( $name ) { + // Prepare options if this is the first request + if ( is_null( $this->options ) ) { + $ga_options = get_option( $this->plugin_option_name ); + + if ( is_array( $ga_options ) ) { + $this->options = wp_parse_args( $ga_options, $this->plugin_option_defaults ); + } else { + return false; + } + + unset( $ga_options ); + } + + // Does the key exist? + if ( isset( $this->options[ $name ] ) ) { + return $this->options[ $name ]; + } else { + return false; + } + } } ETH_AMP_Analytics_WP::get_instance(); \ No newline at end of file -- GitLab