*/ namespace RankMath\Search_Console; use Exception; use RankMath\Helper; use RankMath\Module; use RankMath\Admin\Admin_Helper; use RankMath\Traits\Ajax; use MyThemeShop\Admin\Page; use MyThemeShop\Helpers\Arr; use MyThemeShop\Helpers\Str; use MyThemeShop\Helpers\Param; use MyThemeShop\Helpers\Conditional; defined( 'ABSPATH' ) || exit; /** * Search_Console class. */ class Search_Console extends Module { use Ajax; /** * Hold search console api client. * * @var Client */ public $client; /** * Hold current tab id. * * @var string */ public $current_tab; /** * Hold current filters. * * @var array */ public $filters = null; /** * The Constructor */ public function __construct() { if ( Conditional::is_heartbeat() ) { return; } $directory = dirname( __FILE__ ); $this->config( [ 'id' => 'search-console', 'directory' => $directory, 'help' => [ 'title' => esc_html__( 'Search Console', 'rank-math' ), 'view' => $directory . '/views/help.php', ], ] ); parent::__construct(); $this->client = new Client; $this->crawler = new Data_Fetcher; if ( is_admin() ) { if ( Helper::has_cap( 'search_console' ) ) { $this->action( 'rank_math/dashboard/widget', 'dashboard_widget', 10 ); $this->filter( 'rank_math/settings/general', 'add_settings' ); } $this->action( 'rank_math/admin/enqueue_scripts', 'enqueue' ); // AJAX. $this->ajax( 'search_console_authentication', 'authentication' ); $this->ajax( 'search_console_deauthentication', 'deauthentication' ); $this->ajax( 'search_console_get_profiles', 'get_profiles' ); $this->ajax( 'search_console_delete_cache', 'delete_cache' ); $this->ajax( 'search_console_get_cache', 'start_background_process' ); $this->set_current_tab(); } } /** * Enqueue scripts for the metabox. */ public function enqueue() { if ( ! wp_script_is( 'moment', 'registered' ) ) { wp_register_script( 'moment', rank_math()->plugin_url() . 'assets/vendor/moment.js', [], rank_math()->version ); } } /** * Set current tab field. */ private function set_current_tab() { if ( ! $this->page->is_current_page() ) { return; } $this->get_filters(); $this->current_tab = Param::get( 'view', 'overview' ); if ( ! $this->client->is_authorized ) { return; } $class = 'RankMath\Search_Console\\' . ucfirst( $this->current_tab ); if ( class_exists( $class ) ) { $this->{$this->current_tab} = new $class( $this->client ); } } /** * Render dashboard widget. */ public function dashboard_widget() { $today = Helper::get_midnight( time() ); $week = $today - ( DAY_IN_SECONDS * 7 ); $data_info = DB::data_info( [ 'start_date' => date( 'Y-m-d', $week ), 'end_date' => date( 'Y-m-d', $today ), ] ); ?>
' . esc_html__( 'Connect Rank Math with Google Search Console to see the most important information from Google directly in your WordPress dashboard.', 'rank-math' ) . '
', ], 'search-console-analytics' => [ 'title' => esc_html__( 'Screen Content', 'rank-math' ), 'content' => '' . esc_html__( 'The Search Analytics tab will give you insights about how your site performs in search engines: you can see the top search queries to find your site and your most popular landing pages.', 'rank-math' ) . '
', ], 'search-console-sitemaps' => [ 'title' => esc_html__( 'Available Actions', 'rank-math' ), 'content' => '' . esc_html__( 'The Sitemaps tab gives you an overview of the sitemaps submitted to the Search Console.', 'rank-math' ) . '
', ], ], 'assets' => [ 'styles' => [ 'font-awesome' => rank_math()->plugin_url() . 'assets/vendor/font-awesome/css/font-awesome.min.css', 'jquery-date-range-picker' => rank_math()->plugin_url() . 'assets/vendor/date-range-picker/daterangepicker.min.css', 'rank-math-search-console' => $plugin_uri . '/assets/search-console.css', ], 'scripts' => [ 'rank-math-common' => '', 'moment' => '', 'date-picker' => rank_math()->plugin_url() . 'assets/vendor/date-range-picker/jquery.daterangepicker.min.js', 'google-charts' => '//www.gstatic.com/charts/loader.js', 'rank-math-sc' => $plugin_uri . '/assets/search-console.js', ], ], ] ); } /** * Add module settings into general optional panel. * * @param array $tabs Array of option panel tabs. * * @return array */ public function add_settings( $tabs ) { Arr::insert( $tabs, [ 'search-console' => [ 'icon' => 'fa fa-search-plus', 'title' => esc_html__( 'Search Console', 'rank-math' ), /* translators: Link to kb article */ 'desc' => sprintf( esc_html__( 'Connect Rank Math with your Google Search Console profile to see the most important information from Google directly in your WordPress dashboard. %s.', 'rank-math' ), '' . esc_html__( 'Learn more', 'rank-math' ) . '' ), 'file' => $this->directory . '/views/options.php', ], ], 9 ); return $tabs; } /** * Display tabs. */ public function display_nav() { $tabs = [ 'overview' => esc_html__( 'Overview', 'rank-math' ), 'analytics' => esc_html__( 'Search Analytics', 'rank-math' ), 'sitemaps' => esc_html__( 'Sitemaps', 'rank-math' ), 'tracker' => esc_html__( 'Keyword Tracker', 'rank-math' ), ]; $this->is_sitemap_available( $tabs ); $filters = $this->get_filters(); ?> has_cap_ajax( 'search_console' ); $code = Param::post( 'code' ); $code = $code ? trim( wp_unslash( $code ) ) : false; if ( ! $code ) { $this->error( esc_html__( 'No authentication code found.', 'rank-math' ) ); } $this->success( $this->client->get_access_token( $code ) ); } /** * Disconnect google authentication. */ public function deauthentication() { check_ajax_referer( 'rank-math-ajax-nonce', 'security' ); $this->has_cap_ajax( 'search_console' ); $this->client->disconnect(); $this->crawler->kill_process(); $this->success( 'done' ); } /** * Get profiles list. */ public function get_profiles() { check_ajax_referer( 'rank-math-ajax-nonce', 'security' ); $this->has_cap_ajax( 'search_console' ); $profiles = $this->client->get_profiles(); if ( empty( $profiles ) ) { $this->error( 'No profiles found.' ); } foreach ( $profiles as $key => $value ) { $profiles[ $key ] = str_replace( 'sc-domain:', __( 'Domain Property: ', 'rank-math' ), $value ); } $this->success( [ 'profiles' => $profiles, 'selected' => $this->select_profile( $profiles ), ] ); } /** * Select profile * * @param array $profiles Array of fetched profiles. * * @return string */ private function select_profile( $profiles ) { $home_url = home_url( '/', 'https' ); if ( in_array( $home_url, $profiles, true ) ) { return $home_url; } $home_url = home_url( '/', 'http' ); if ( in_array( $home_url, $profiles, true ) ) { return $home_url; } return ''; } /** * Delete cache. */ public function delete_cache() { check_ajax_referer( 'rank-math-ajax-nonce', 'security' ); $this->has_cap_ajax( 'search_console' ); $days = Param::get( 'days', false, FILTER_VALIDATE_INT ); if ( ! $days ) { $this->error( esc_html__( 'Not a valid settings founds to delete cache.', 'rank-math' ) ); } DB::delete( $days ); $db_info = DB::info(); $db_info['message'] = sprintf( '