data. * * @param string $date Date to fetch data for. */ public function get_console_data( $date ) { set_time_limit( 300 ); $rows = Api::get()->get_search_analytics( [ 'start_date' => $date, 'end_date' => $date, 'dimensions' => [ 'query', 'page' ], ] ); if ( empty( $rows ) || is_wp_error( $rows ) ) { return; } $rows = \array_map( [ $this, 'normalize_query_page_data' ], $rows ); try { DB::add_query_page_bulk( $date, $rows ); // Clear the cache here. $this->cache_flush_group( 'rank_math_rest_keywords_rows' ); $this->cache_flush_group( 'rank_math_posts_rows_by_objects' ); $this->cache_flush_group( 'rank_math_analytics_summary' ); return $rows; } catch ( Exception $e ) {} // phpcs:ignore } /** * Handlle console response. * * @param array $data API request and response data. */ public function handle_console_response( $data = [] ) { if ( 200 !== $data['code'] ) { return; } if ( isset( $data['formatted_response']['rows'] ) && ! empty( $data['formatted_response']['rows'] ) ) { return; } if ( ! isset( $data['args']['startDate'] ) ) { return; } $dates = get_option( 'rank_math_console_empty_dates', [] ); if ( ! $dates ) { $dates = []; } $dates[] = $data['args']['startDate']; $dates[] = $data['args']['endDate']; $dates = array_unique( $dates ); update_option( 'rank_math_console_empty_dates', $dates ); } /** * Get inspection results from the API and store them in the database. * * @param string $page URI to fetch data for. */ public function get_inspections_data( $page ) { // If the option is disabled, don't fetch data. if ( ! \RankMath\Analytics\Url_Inspection::is_enabled() ) { return; } $inspection = Url_Inspection::get()->get_inspection_data( $page ); if ( empty( $inspection ) ) { return; } try { DB::store_inspection( $inspection ); } catch ( Exception $e ) {} // phpcs:ignore } /** * Check for missing dates. * * @param string $action Action to perform. */ public function check_for_missing_dates( $action ) { $days = Helper::get_settings( 'general.console_caching_control', 90 ); Workflow::do_workflow( $action, $days, null, null ); } /** * Calculate stats. */ private function calculate_stats() { $ranges = [ '-7 days', '-15 days', '-30 days', '-3 months', '-6 months', '-1 year', ]; foreach ( $ranges as $range ) { Stats::get()->set_date_range( $range ); Stats::get()->get_top_keywords(); } } /** * Normalize console data. * * @param array $row Single row item. * * @return array */ protected function normalize_query_page_data( $row ) { $row = $this->normalize_data( $row ); $row['query'] = $row['keys'][0]; $row['page'] = $row['keys'][1]; unset( $row['keys'] ); return $row; } /** * Normalize console data. * * @param array $row Single row item. * * @return array */ private function normalize_data( $row ) { $row['ctr'] = round( $row['ctr'] * 100, 2 ); $row['position'] = round( $row['position'], 2 ); return $row; } }