urn; } if ( ! Helper::is_post_indexable( $post_id ) ) { return; } // Check if it's a hidden product. if ( 'product' === $post->post_type && Helper::is_woocommerce_active() ) { $product = wc_get_product( $post_id ); if ( $product && ! $product->is_visible() ) { return; } } $url = get_permalink( $post ); if ( 'trash' === $post->post_status ) { $url = $this->previous_post_permalinks[ $post_id ]; } /** * Filter the URL to be submitted to IndexNow. * Returning false will prevent the URL from being submitted. * * @param string $url URL to be submitted. * @param WP_POST $post Post object. */ $send_url = $this->do_filter( 'instant_indexing/publish_url', $url, $post ); // Early exit if filter is set to false. if ( ! $send_url ) { return; } $this->api_submit( $send_url, false ); $this->submitted[] = $post_id; } /** * Is module configured. * * @return boolean */ private function is_configured() { return (bool) Helper::get_settings( 'instant_indexing.indexnow_api_key' ); } /** * Enqueue CSS & JS. * * @param string $hook Page hook name. * @return void */ public function enqueue( $hook ) { if ( 'rank-math_page_rank-math-options-instant-indexing' !== $hook && 'rank-math_page_instant-indexing' !== $hook ) { return; } $uri = untrailingslashit( plugin_dir_url( __FILE__ ) ); wp_enqueue_script( 'rank-math-instant-indexing', $uri . '/assets/js/instant-indexing.js', [ 'jquery' ], rank_math()->version, true ); Helper::add_json( 'indexNow', [ 'restUrl' => rest_url( \RankMath\Rest\Rest_Helper::BASE . '/in' ), 'refreshHistoryInterval' => 30000, 'i18n' => [ 'submitError' => esc_html__( 'An error occurred while submitting the URL.', 'rank-math' ), 'clearHistoryError' => esc_html__( 'Error: could not clear history.', 'rank-math' ), 'getHistoryError' => esc_html__( 'Error: could not get history.', 'rank-math' ), 'noHistory' => esc_html__( 'No submissions yet.', 'rank-math' ), ], ] ); } /** * Serve API key for search engines. */ public function serve_api_key() { global $wp; $api = Api::get(); $key = $api->get_key(); $key_location = $api->get_key_location( 'serve_api_key' ); $current_url = home_url( $wp->request ); if ( isset( $current_url ) && $key_location === $current_url ) { header( 'Content-Type: text/plain' ); header( 'X-Robots-Tag: noindex' ); status_header( 200 ); echo esc_html( $key ); exit(); } } /** * Submit URL to IndexNow API. * * @param string $url URL to be submitted. * @param bool $is_manual_submission Whether the URL is submitted manually by the user. * * @return bool */ private function api_submit( $url, $is_manual_submission ) { $api = Api::get(); /** * Filter the URL to be submitted to IndexNow. * Returning false will prevent the URL from being submitted. * * @param bool $is_manual_submission Whether the URL is submitted manually by the user. */ $url = $this->do_filter( 'instant_indexing/submit_url', $url, $is_manual_submission ); if ( ! $url ) { return false; } $api_logs = $api->get_log(); if ( ! $is_manual_submission && ! empty( $api_logs ) ) { $logs = array_values( array_reverse( $api_logs ) ); if ( ! empty( $logs[0] ) && $logs[0]['url'] === $url && time() - $logs[0]['time'] < self::THROTTLE_LIMIT ) { return false; } } $submitted = $api->submit( $url, $is_manual_submission ); if ( ! $is_manual_submission ) { return $submitted; } $count = is_array( $url ) ? count( $url ) : 1; $this->add_submit_message_notice( $submitted, $count ); return $submitted; } /** * Add notice after submitting one or more URLs. * * @param bool $success Whether the submission was successful. * @param int $count Number of submitted URLs. * * @return void */ private function add_submit_message_notice( $success, $count ) { $notification_type = 'error'; $notification_message = __( 'Error submitting page to IndexNow.', 'rank-math' ); if ( $success ) { $notification_type = 'success'; $notification_message = sprintf( /* translators: %s: Number of pages submitted. */ _n( '%s page submitted to IndexNow.', '%s pages submitted to IndexNow.', $count, 'rank-math' ), $count ); } Helper::add_notification( $notification_message, [ 'type' => $notification_type ] ); } /** * Get post types where auto-submit is enabled. * * @return array */ private function get_auto_submit_post_types() { $post_types = Helper::get_settings( 'instant_indexing.bing_post_types', [] ); return $post_types; } }