*/
namespace RankMath\Analytics;
use RankMath\Helper;
use RankMath\Helpers\Str;
use RankMath\Helpers\DB as DB_Helper;
use RankMath\Traits\Hooker;
use RankMath\Google\Console;
use RankMath\Google\Authentication;
use RankMath\Analytics\Workflow\Jobs;
use RankMath\Analytics\Workflow\Workflow;
defined( 'ABSPATH' ) || exit;
/**
 * Analytics class.
 */
class Analytics_Common {
	use Hooker;
	/**
	 * The Constructor
	 */
	public function __construct() {
		if ( Helper::is_heartbeat() ) {
			return;
		}
		if ( Helper::has_cap( 'analytics' ) ) {
			$this->action( 'rank_math/admin_bar/items', 'admin_bar_items', 11 );
		}
		// Show Analytics block in the Dashboard widget only if account is connected or user has permissions.
		if ( Helper::has_cap( 'analytics' ) && Authentication::is_authorized() ) {
			$this->action( 'rank_math/dashboard/widget', 'dashboard_widget' );
		}
		new GTag();
		new Analytics_Stats();
		$this->action( 'plugins_loaded', 'maybe_init_email_reports', 15 );
		$this->action( 'init', 'maybe_enable_email_reports', 20 );
		$this->action( 'cmb2_save_options-page_fields_rank-math-options-general_options', 'maybe_update_report_schedule', 20, 3 );
		Jobs::get();
		Workflow::get();
		$this->action( 'rest_api_init', 'init_rest_api' );
		$this->filter( 'rank_math/webmaster/google_verify', 'add_site_verification' );
		$this->filter( 'rank_math/tools/analytics_clear_caches', 'analytics_clear_caches' );
		$this->filter( 'rank_math/tools/analytics_reindex_posts', 'analytics_reindex_posts' );
		$this->filter( 'rank_math/tools/analytics_fix_collations', 'analytics_fix_collations' );
		$this->filter( 'wp_helpers_notifications_render', 'replace_notice_link', 10, 3 );
	}
	/**
	 * Add stats widget into admin dashboard.
	 */
	public function dashboard_widget() {
		?>
		
			
			
			
				
			
		
		
			get_dashboard_widget_items();
			foreach ( $items as $label => $item ) {
				if ( ! $item['value'] ) {
					continue;
				}
				?>
				
					
						
						
							
							
								
							
						
					
					get_analytic_block( $item['data'], ! empty( $item['revert'] ) ); ?>
				
			
		 
		register_routes();
		}
	}
	/**
	 * Add admin bar item.
	 *
	 * @param Admin_Bar_Menu $menu Menu class instance.
	 */
	public function admin_bar_items( $menu ) {
		$dot_color = '#ed5e5e';
		if ( Console::is_console_connected() ) {
			$dot_color = '#11ac84';
		}
		$menu->add_sub_menu(
			'analytics',
			[
				'title'    => esc_html__( 'Analytics', 'rank-math' ) . '',
				'href'     => Helper::get_admin_url( 'analytics' ),
				'meta'     => [ 'title' => esc_html__( 'Review analytics and sitemaps', 'rank-math' ) ],
				'priority' => 20,
			]
		);
	}
	/**
	 * Purge cache.
	 *
	 * @return string
	 */
	public function analytics_clear_caches() {
		DB::purge_cache();
		return __( 'Analytics cache cleared.', 'rank-math' );
	}
	/**
	 * ReIndex posts.
	 *
	 * @return string
	 */
	public function analytics_reindex_posts() {
		// Clear all objects data.
		DB::objects()
			->truncate();
		// Clear all metadata related to object.
		DB::table( 'postmeta' )
			->where( 'meta_key', 'rank_math_analytic_object_id' )
			->delete();
		// Start reindexing posts.
		( new \RankMath\Analytics\Workflow\Objects() )->flat_posts();
		return __( 'Post re-index in progress.', 'rank-math' );
	}
	/**
	 * Fix table & column collations.
	 *
	 * @return string
	 */
	public function analytics_fix_collations() {
		$tables = [
			'rank_math_analytics_ga',
			'rank_math_analytics_gsc',
			'rank_math_analytics_keyword_manager',
			'rank_math_analytics_inspections',
		];
		$objects_coll = DB_Helper::get_table_collation( 'rank_math_analytics_objects' );
		$changed      = 0;
		foreach ( $tables as $table ) {
			$changed += (int) DB_Helper::check_collation( $table, 'all', $objects_coll );
		}
		return $changed ? sprintf(
			/* translators: %1$d: number of changes, %2$s: new collation. */
			_n( '%1$d collation changed to %2$s.', '%1$d collations changed to %2$s.', $changed, 'rank-math' ),
			$changed,
			'`' . $objects_coll . '`'
		) : __( 'No collation mismatch to fix.', 'rank-math' );
	}
	/**
	 * Init Email Reports class if the option is enabled.
	 *
	 * @return void
	 */
	public function maybe_init_email_reports() {
		if ( Helper::get_settings( 'general.console_email_reports' ) ) {
			new Email_Reports();
		}
	}
	/**
	 * Enable the email reports option if the `enable_email_reports` param is set.
	 *
	 * @return void
	 */
	public function maybe_enable_email_reports() {
		if ( ! Helper::has_cap( 'analytics' ) ) {
			return;
		}
		if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'enable_email_reports' ) ) {
			return;
		}
		if ( ! empty( $_GET['enable_email_reports'] ) ) {
			$all_opts = rank_math()->settings->all_raw();
			$general  = $all_opts['general'];
			$general['console_email_reports'] = 'on';
			Helper::update_all_settings( $general, null, null );
			rank_math()->settings->reset();
			$this->schedule_email_reporting();
			Helper::remove_notification( 'rank_math_analytics_new_email_reports' );
			Helper::redirect( remove_query_arg( 'enable_email_reports' ) );
			die();
		}
	}
	/**
	 * Add/remove/change scheduled action when the report on/off or the frequency options are changed.
	 *
	 * @param int    $object_id The ID of the current object.
	 * @param array  $updated   Array of field IDs that were updated.
	 *                          Will only include field IDs that had values change.
	 * @param object $cmb       CMB object.
	 */
	public function maybe_update_report_schedule( $object_id, $updated, $cmb ) {
		// Early bail if our options are not changed.
		if ( ! in_array( 'console_email_reports', $updated, true ) && ! in_array( 'console_email_frequency', $updated, true ) ) {
			return;
		}
		as_unschedule_all_actions( 'rank_math/analytics/email_report_event', [], 'rank-math' );
		$values = $cmb->get_sanitized_values( $_POST ); // phpcs:ignore
		if ( 'off' === $values['console_email_reports'] ) {
			return;
		}
		$frequency = isset( $values['console_email_frequency'] ) ? $values['console_email_frequency'] : 'monthly';
		$this->schedule_email_reporting( $frequency );
	}
	/**
	 * Replace link inside notice dynamically to avoid issues with the nonce.
	 *
	 * @param string $output  Notice output.
	 * @param string $message Notice message.
	 * @param array  $options Notice options.
	 *
	 * @return string
	 */
	public function replace_notice_link( $output, $message, $options ) {
		$url    = wp_nonce_url( Helper::get_admin_url( 'options-general&enable_email_reports=1#setting-panel-analytics' ), 'enable_email_reports' );
		$output = str_replace( '###ENABLE_EMAIL_REPORTS###', $url, $output );
		return $output;
	}
	/**
	 * Get Dashboard Widget items.
	 */
	private function get_dashboard_widget_items() {
		// Get stats info within last 30 days.
		Stats::get()->set_date_range( '-30 days' );
		$data         = Stats::get()->get_widget();
		$analytics    = get_option( 'rank_math_google_analytic_options' );
		$is_connected = ! empty( $analytics ) && ! empty( $analytics['view_id'] );
		return [
			'search-traffic'    => [
				'label' => __( 'Search Traffic', 'rank-math' ),
				'desc'  => __( 'This is the number of pageviews carried out by visitors from Search Engines.', 'rank-math' ),
				'value' => $is_connected && defined( 'RANK_MATH_PRO_FILE' ),
				'data'  => isset( $data->pageviews ) ? $data->pageviews : '',
			],
			'total-impressions' => [
				'label' => __( 'Total Impressions', 'rank-math' ),
				'desc'  => __( 'How many times your site showed up in the search results.', 'rank-math' ),
				'value' => true,
				'data'  => $data->impressions,
			],
			'total-clicks'      => [
				'label' => __( 'Total Clicks', 'rank-math' ),
				'desc'  => __( 'This is the number of pageviews carried out by visitors from Google.', 'rank-math' ),
				'value' => ! $is_connected || ( $is_connected && ! defined( 'RANK_MATH_PRO_FILE' ) ),
				'data'  => $data->clicks,
			],
			'total-keywords'    => [
				'label' => __( 'Total Keywords', 'rank-math' ),
				'desc'  => __( 'Total number of keywords your site ranking below 100 position.', 'rank-math' ),
				'value' => true,
				'data'  => $data->keywords,
			],
			'average-position'  => [
				'label'  => __( 'Average Position', 'rank-math' ),
				'desc'   => __( 'Average position of all the ranking keywords below 100 position.', 'rank-math' ),
				'value'  => true,
				'revert' => true,
				'data'   => $data->position,
			],
		];
	}
	/**
	 * Get analytic block
	 *
	 * @param object  $item   Item.
	 * @param boolean $revert Flag whether to revert difference icon or not.
	 */
	private function get_analytic_block( $item, $revert = false ) {
		$total       = isset( $item['total'] ) ? abs( $item['total'] ) : 0;
		$difference  = isset( $item['difference'] ) ? abs( $item['difference'] ) : 0;
		$is_negative = isset( $item['difference'] ) && abs( $item['difference'] ) !== $item['difference'];
		$diff_class  = 'up';
		if ( ( ! $revert && $is_negative ) || ( $revert && ! $is_negative && $item['difference'] > 0 ) ) {
			$diff_class = 'down';
		}
		?>