options = $this->get_options(); $this->api = Yoast_Api_Libs::load_api_libraries( array( 'google', 'googleanalytics' ) ); $dashboards = Yoast_GA_Dashboards::get_instance(); // Listener for reconnecting with google analytics $this->google_analytics_listener(); if ( is_null( $this->get_tracking_code() ) && $this->show_admin_warning() ) { add_action( 'admin_notices', array( 'Yoast_Google_Analytics_Notice', 'config_warning' ) ); } // Check if something has went wrong with GA-api calls $has_tracking_code = ( ! is_null( $this->get_tracking_code() ) && empty( $this->options['manual_ua_code_field'] ) ); if ( $has_tracking_code && $this->show_admin_dashboard_warning() ) { Yoast_Google_Analytics::get_instance()->check_for_ga_issues(); } if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { $this->handle_ga_post_request( $dashboards ); } /** * Show the notifications if we have one */ $this->show_notification( 'ga_notifications' ); // Load the Google Analytics Dashboards functionality $dashboards->init_dashboards( $this->get_current_profile() ); } /** * This function saves the settings in the option field and returns a wp success message on success * * @param array $data */ public function save_settings( $data ) { unset( $data['google_auth_code'] ); foreach ( $data as $key => $value ) { if ( $key != 'return_tab' ) { if ( $key != 'custom_code' && is_string( $value ) ) { $value = strip_tags( $value ); } $this->options[ $key ] = $value; } } // Check checkboxes, on a uncheck they won't be posted to this function $defaults = $this->default_ga_values(); foreach ( $defaults[ $this->option_prefix ] as $option_name => $value ) { $this->handle_default_setting( $data, $option_name, $value ); } if ( ! empty( $this->options['analytics_profile'] ) ) { $this->options['analytics_profile_code'] = $this->get_ua_code_from_profile( $this->options['analytics_profile'] ); } if ( ! empty( $this->options['manual_ua_code_field'] ) ) { $this->options['manual_ua_code_field'] = trim( $this->options['manual_ua_code_field'] ); // en dash to minus, prevents issue with code copied from web with "fancy" dash $this->options['manual_ua_code_field'] = str_replace( '–', '-', $this->options['manual_ua_code_field'] ); if ( ! preg_match( '|^UA-\d{4,}-\d+$|', $this->options['manual_ua_code_field'] ) ) { $this->add_notification( 'ga_notifications', array( 'type' => 'error', 'description' => __( 'The UA code needs to follow UA-XXXXXXXX-X format.', 'google-analytics-for-wordpress' ), ) ); wp_redirect( admin_url( 'admin.php' ) . '?page=yst_ga_settings#top#' . $data['return_tab'], 301 ); exit; } } if ( $this->update_option( $this->options ) ) { // Success, add a new notification $this->add_notification( 'ga_notifications', array( 'type' => 'success', 'description' => __( 'Settings saved.', 'google-analytics-for-wordpress' ), ) ); } else { // Fail, add a new notification $this->add_notification( 'ga_notifications', array( 'type' => 'error', 'description' => __( 'There were no changes to save, please try again.', 'google-analytics-for-wordpress' ), ) ); } // redirect wp_redirect( admin_url( 'admin.php' ) . '?page=yst_ga_settings#top#' . $data['return_tab'], 301 ); exit; } /** * Run a this deactivation hook on deactivation of GA. When this happens we'll * remove the options for the profiles and the refresh token. */ public static function ga_deactivation_hook() { // Remove the refresh token and other API settings self::analytics_api_clean_up(); } /** * Handle a default setting in GA * * @param array $data * @param string $option_name * @param mixed $value */ private function handle_default_setting( $data, $option_name, $value ) { if ( ! isset( $data[ $option_name ] ) ) { // If no data was passed in, set it to the default. if ( $value === 1 ) { // Disable the checkbox for now, use value 0 $this->options[ $option_name ] = 0; } else { $this->options[ $option_name ] = $value; } } } /** * Handle the post requests in the admin form of the GA plugin * * @param Yoast_GA_Dashboards $dashboards */ private function handle_ga_post_request( $dashboards ) { if ( ! function_exists( 'wp_verify_nonce' ) ) { require_once( ABSPATH . 'wp-includes/pluggable.php' ); } if ( isset( $_POST['ga-form-settings'] ) && wp_verify_nonce( $_POST['yoast_ga_nonce'], 'save_settings' ) ) { if ( ! isset ( $_POST['ignore_users'] ) ) { $_POST['ignore_users'] = array(); } $dashboards_disabled = Yoast_GA_Settings::get_instance()->dashboards_disabled(); if ( ( $dashboards_disabled == false && isset( $_POST['dashboards_disabled'] ) ) || $this->ga_profile_changed( $_POST ) ) { $dashboards->reset_dashboards_data(); } // Post submitted and verified with our nonce $this->save_settings( $_POST ); } } /** * Is there selected an other property in the settings post? Returns true or false. * * @param array $post * * @return bool */ private function ga_profile_changed( $post ) { if ( isset( $post['analytics_profile'] ) && isset( $this->options['analytics_profile'] ) ) { if ( $post['analytics_profile'] != $this->options['analytics_profile'] ) { return true; } } return false; } /** * Are we allowed to show a warning message? returns true if it's allowed * * @return bool */ private function show_admin_warning() { return ( current_user_can( 'manage_options' ) && ( ! isset( $_GET['page'] ) || ( isset( $_GET['page'] ) && $_GET['page'] !== 'yst_ga_settings' ) ) ); } /** * Are we allowed to show a warning message? returns true if it's allowed ( this is meant to be only for dashboard ) * * @return bool */ private function show_admin_dashboard_warning() { return ( current_user_can( 'manage_options' ) && isset( $_GET['page'] ) && $_GET['page'] === 'yst_ga_dashboard' ); } /** * Transform the Profile ID into an helpful UA code * * @param integer $profile_id * * @return null */ private function get_ua_code_from_profile( $profile_id ) { $profiles = $this->get_profiles(); $ua_code = null; foreach ( $profiles as $account ) { foreach ( $account['items'] as $profile ) { foreach ( $profile['items'] as $subprofile ) { if ( isset( $subprofile['id'] ) && $subprofile['id'] === $profile_id ) { return $subprofile['ua_code']; } } } } return $ua_code; } /** * Add a link to the settings page to the plugins list * * @param array $links array of links for the plugins, adapted when the current plugin is found. * * @return array $links */ public function add_action_links( $links ) { // add link to knowledgebase // @todo UTM link fix $faq_link = '' . __( 'FAQ', 'google-analytics-for-wordpress' ) . ''; array_unshift( $links, $faq_link ); $settings_link = '' . __( 'Settings', 'google-analytics-for-wordpress' ) . ''; array_unshift( $links, $settings_link ); return $links; } /** * Adds some promo text for the premium plugin on the custom dimensions tab. */ public function premium_promo() { echo '
'; printf( __( 'If you want to track custom dimensions like page views per author or post type, you should upgrade to the %1$spremium version of Google Analytics by Yoast%2$s.', 'google-analytics-for-wordpress' ), '', '' ); echo ' '; _e( 'This will also give you email access to the support team at Yoast, who will provide support on the plugin 24/7.', 'google-analytics-for-wordpress' ); echo '
'; echo ''; var_dump( $this->options ); echo '