<?php
/**
 * This class is for the backend, extendable for all child classes
 */

if ( ! class_exists( 'Yoast_GA_Admin' ) ) {

	class Yoast_GA_Admin extends Yoast_GA_Options {

		/**
		 * Store the API instance
		 *
		 * @var
		 */
		public $api;

		public function __construct() {
			parent::__construct();

			add_action( 'plugins_loaded', array( $this, 'init_ga' ) );
			add_action( 'admin_init', array( $this, 'init_settings' ) );
		}

		/**
		 * Init function when the plugin is loaded
		 */
		public function init_ga() {

			new Yoast_GA_Admin_Menu( $this );

			add_filter( 'plugin_action_links_' . plugin_basename( GAWP_FILE ), array( $this, 'add_action_links' ) );

		}

		/**
		 * Init function for the settings of GA
		 */
		public function init_settings() {
			$this->options = $this->get_options();
			$this->api     = Yoast_Api_Libs::load_api_libraries( array( 'google', 'googleanalytics' ) );


			// 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( $this, 'config_warning' ) );
			}

			$last_run = get_option( 'yst_ga_last_wp_run' );
			if ( $last_run === false || Yoast_GA_Utils::hours_between( strtotime( $last_run ), time() ) >= 48 ) {
				// Show error, something went wrong
				if ( ! is_null( $this->get_tracking_code() ) && empty( $this->options['manual_ua_code_field'] ) && $this->show_admin_warning() ) {
					add_action( 'admin_notices', array( $this, 'warning_fetching_data' ) );
				}
			}

			if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
				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();
					}

					// Post submitted and verified with our nonce
					$this->save_settings( $_POST );
				}
			}

			/**
			 * Show the notifications if we have one
			 */
			$this->show_notification( 'ga_notifications' );

			// Load the Google Analytics Dashboards functionality
			$dashboards = Yoast_GA_Dashboards::get_instance();
			$dashboards->init_dashboards( $this->get_current_profile() );
		}

		/**
		 * Throw a warning if no UA code is set.
		 */
		public function config_warning() {
			echo '<div class="error"><p>' . sprintf( __( 'Please configure your %sGoogle Analytics settings%s!', 'google-analytics-for-wordpress' ), '<a href="' . admin_url( 'admin.php?page=yst_ga_settings' ) . '">', '</a>' ) . '</p></div>';
		}

		/**
		 * Throw a warning when the fetching failed
		 */
		public function warning_fetching_data() {
			echo '<div class="error"><p>' . sprintf( __( 'Failed to fetch the new data from Google Analytics. Please %sreauthenticate on the settings page%s!', 'google-analytics-for-wordpress' ), '<a href="' . admin_url( 'admin.php?page=yst_ga_settings' ) . '">', '</a>' ) . '</p></div>';
		}

		/**
		 * This function saves the settings in the option field and returns a wp success message on success
		 *
		 * @param $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 $key => $value ) {
				if ( ! isset( $data[$key] ) ) {
					// If no data was passed in, set it to the default.
					$this->options[$key] = $value;
				}
			}

			if ( ! empty( $this->options['analytics_profile'] ) ) {
				$this->options['analytics_profile_code'] = $this->get_ua_code_from_profile( $this->options['analytics_profile'] );
			}

			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
			delete_option( 'yoast-ga-refresh_token' );

			// Remove the ga accounts and response
			delete_option( 'yst_ga_accounts' );
			delete_option( 'yst_ga_response' );

		}

		/**
		 * Are we allowed to show an warning message? returns true if it's allowed
		 *
		 * @return bool
		 */
		private function show_admin_warning() {
			if ( current_user_can( 'manage_options' ) ) {
				if ( ! isset( $_GET['page'] ) || ( isset( $_GET['page'] ) && $_GET['page'] !== 'yst_ga_settings' ) ) {
					return true;
				}
			}

			return false;
		}

		/**
		 * Transform the Profile ID into an helpful UA code
		 *
		 * @param $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 = '<a title="Yoast Knowledge Base" href="http://kb.yoast.com/category/43-google-analytics-for-wordpress">' . __( 'FAQ', 'google-analytics-for-wordpress' ) . '</a>';
			array_unshift( $links, $faq_link );

			$settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=yst_ga_settings' ) ) . '">' . __( 'Settings', 'google-analytics-for-wordpress' ) . '</a>';
			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 '<div class="ga-promote">';
			echo '<p>';
			printf( __( 'If you want to track custom dimensions, to for instance track 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' ), '<a href="https://yoast.com/wordpress/plugins/google-analytics/#utm_medium=text-link&utm_source=gawp-config&utm_campaign=wpgaplugin&utm_content=custom_dimensions_tab">', '</a>' );
			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 '</p>';
			echo '</div>';
		}

		/**
		 * Initialize the promo class for our translate site
		 *
		 * @return yoast_i18n
		 */
		public function translate_promo() {
			$yoast_ga_i18n = new yoast_i18n(
				array(
					'textdomain'     => 'google-analytics-for-wordpress',
					'project_slug'   => 'google-analytics-for-wordpress',
					'plugin_name'    => 'Google Analytics by Yoast',
					'hook'           => 'yoast_ga_admin_footer',
					'glotpress_url'  => 'http://translate.yoast.com',
					'glotpress_name' => 'Yoast Translate',
					'glotpress_logo' => 'https://cdn.yoast.com/wp-content/uploads/i18n-images/Yoast_Translate.svg',
					'register_url '  => 'http://translate.yoast.com/projects#utm_source=plugin&utm_medium=promo-box&utm_campaign=yoast-ga-i18n-promo',
				)
			);

			return $yoast_ga_i18n;
		}

		/**
		 * Load the page of a menu item in the GA plugin
		 */
		public function load_page() {
			global $yoast_ga_admin_ga_js;
			$yoast_ga_admin_ga_js = new Yoast_GA_Admin_GA_JS;

			$this->translate_promo();

			if ( ! has_action( 'yst_ga_custom_dimensions_tab-content' ) ) {
				add_action( 'yst_ga_custom_dimensions_tab-content', array( $this, 'premium_promo' ) );
			}

			if ( ! has_action( 'yst_ga_custom_dimension_add-dashboards-tab' ) ) {
				add_action( 'yst_ga_custom_dimension_add-dashboards-tab', array( $this, 'premium_promo' ) );
			}

			if ( isset( $_GET['page'] ) ) {
				switch ( $_GET['page'] ) {
					case 'yst_ga_settings':
						require_once( $this->plugin_path . 'admin/pages/settings.php' );
						break;
					case 'yst_ga_extensions':
						require_once( $this->plugin_path . 'admin/pages/extensions.php' );
						break;
					case 'yst_ga_dashboard':
					default:
						require_once( $this->plugin_path . 'admin/pages/dashboard.php' );
						break;
				}
			}
		}


		/**
		 * Get the Google Analytics profiles which are in this google account
		 *
		 * @return array
		 */
		public function get_profiles() {
			$return = Yoast_Google_Analytics::get_instance()->get_profiles();

			return $return;
		}

		/**
		 * Checks if there is a callback or reauth to get token from Google Analytics api
		 */
		private function google_analytics_listener() {

			if ( ! empty( $_POST['google_auth_code'] ) ) {
				Yoast_Google_Analytics::get_instance()->authenticate( trim( $_POST['google_auth_code'] ) );
			}


			if ( ! empty ( $_GET['reauth'] ) ) {

				delete_option( 'yst_ga_accounts' );
				delete_option( 'yst_ga_response' );

				Yoast_Google_Analytics::get_instance()->authenticate();
			}

		}

		/**
		 * Get the current GA profile
		 *
		 * @return null
		 */
		private function get_current_profile() {
			if ( ! empty( $this->options['analytics_profile'] ) ) {
				return $this->options['analytics_profile'];
			} else {
				return null;
			}
		}

		/**
		 * Get the user roles of this WordPress blog
		 *
		 * @return array
		 */
		public function get_userroles() {
			global $wp_roles;

			$all_roles = $wp_roles->roles;
			$roles     = array();

			/**
			 * Filter: 'editable_roles' - Allows filtering of the roles shown within the plugin (and elsewhere in WP as it's a WP filter)
			 *
			 * @api array $all_roles
			 */
			$editable_roles = apply_filters( 'editable_roles', $all_roles );
			foreach ( $editable_roles as $id => $name ) {
				$roles[] = array(
					'id'   => $id,
					'name' => $name['name'],
				);
			}

			return $roles;
		}

		/**
		 * Get types of how we can track downloads
		 *
		 * @return array
		 */
		public function track_download_types() {
			return array(
				0 => array( 'id' => 'event', 'name' => 'Event' ),
				1 => array( 'id' => 'pageview', 'name' => 'Pageview' ),
			);
		}

		/**
		 * Get options for the track full url or links setting
		 *
		 * @return array
		 */
		public function get_track_full_url() {
			return array(
				0 => array( 'id' => 'domain', 'name' => 'Just the domain' ),
				1 => array( 'id' => 'full_links', 'name' => 'Full links' ),
			);
		}

		/**
		 * Render the admin page head for the GA Plugin
		 */
		public function content_head() {
			require 'views/content_head.php';
		}

		/**
		 * Render the admin page footer with sidebar for the GA Plugin
		 */
		public function content_footer() {

			do_action( 'yoast_ga_admin_footer' );

			if ( true == WP_DEBUG ) {
				// Show the debug information if debug is enabled in the wp_config file
				echo '<div id="ga-debug-info" class="postbox"><h3 class="hndle"><span>' . __( 'Debug information', 'google-analytics-for-wordpress' ) . '</span></h3><div class="inside"><pre>';
				var_dump( $this->options );
				echo '</pre></div></div>';
			}

			if ( class_exists( 'Yoast_Product_GA_Premium' ) ) {
				$license_manager = new Yoast_Plugin_License_Manager( new Yoast_Product_GA_Premium() );
				if ( $license_manager->license_is_valid() ) {
					return;
				}
			}

			$banners   = array();
			$banners[] = array(
				'url'    => 'https://yoast.com/hire-us/website-review/#utm_medium=banner&utm_source=gawp-config&utm_campaign=wpgaplugin',
				'banner' => $this->plugin_url . 'assets/img/banner-website-review.png',
				'title'  => 'Get a website review by Yoast',
			);
			$banners[] = array(
				'url'    => 'https://yoast.com/wordpress/plugins/google-analytics/#utm_medium=banner&utm_source=gawp-config&utm_campaign=wpgaplugin',
				'banner' => $this->plugin_url . 'assets/img/banner-premium-ga.png',
				'title'  => 'Get the premium version of Google Analytics by Yoast!',
			);
			$banners[] = array(
				'url'    => 'https://yoast.com/ebook-optimize-wordpress-site/#utm_medium=banner&utm_source=gawp-config&utm_campaign=wpgaplugin',
				'banner' => $this->plugin_url . 'assets/img/eBook_261x130.png',
				'title'  => 'Get the Yoast ebook!',
			);
			$banners[] = array(
				'url'    => 'https://yoast.com/wordpress/plugins/ga-ecommerce/#utm_medium=banner&utm_source=gawp-config&utm_campaign=wpgaplugin',
				'banner' => $this->plugin_url . 'assets/img/banner-ga-ecommerce.png',
				'title'  => 'Get advanced eCommerce tracking for WooCommerce and Easy Digital Downloads!',
			);

			shuffle( $banners );

			require 'views/content-footer.php';

		}

		/**
		 * Returns a list of all available extensions
		 *
		 * @return array
		 */
		public function get_extensions() {
			$extensions = array(
				'ga_premium' => (object) array(
					'url'    => 'https://yoast.com/wordpress/plugins/google-analytics/',
					'title'  => __( 'Google Analytics by Yoast Premium', 'google-analytics-for-wordpress' ),
					'desc'   => __( 'The premium version of Google Analytics for WordPress with more features &amp; support.', 'google-analytics-for-wordpress' ),
					'status' => 'uninstalled',
				),
				'ecommerce'  => (object) array(
					'url'    => 'https://yoast.com/wordpress/plugins/ga-ecommerce/',
					'title'  => __( 'Google Analytics', 'google-analytics-for-wordpress' ) . '<br />' . __( 'E-Commerce tracking', 'google-analytics-for-wordpress' ),
					'desc'   => __( 'Track your E-Commerce data and transactions with this E-Commerce extension for Google Analytics.', 'google-analytics-for-wordpress' ),
					'status' => 'uninstalled',
				),
			);

			$extensions = apply_filters( 'yst_ga_extension_status', $extensions );

			return $extensions;
		}

		/**
		 * Add a notification to the notification transient
		 *
		 * @param $transient_name
		 * @param $settings
		 */
		private function add_notification( $transient_name, $settings ) {
			set_transient( $transient_name, $settings, MINUTE_IN_SECONDS );
		}

		/**
		 * Show the notification that should be set, after showing the notification this function unset the transient
		 *
		 * @param string $transient_name The name of the transient which contains the notification
		 */
		public function show_notification( $transient_name ) {
			$transient = get_transient( $transient_name );

			if ( isset( $transient['type'] ) && isset( $transient['description'] ) ) {
				if ( $transient['type'] == 'success' ) {
					add_settings_error(
						'yoast_google_analytics',
						'yoast_google_analytics',
						$transient['description'],
						'updated'
					);
				} else {
					add_settings_error(
						'yoast_google_analytics',
						'yoast_google_analytics',
						$transient['description'],
						'error'
					);
				}

				delete_transient( $transient_name );
			}
		}

	}

}