ic function json_override_native() { $data = $this->get_request_data(); $override = \smartcrawl_get_array_value( $data, 'override' ); if ( is_null( $override ) ) { wp_send_json_error(); return; } Utils::set_sitemap_option( 'override-native', (bool) $override ); wp_send_json_success(); } /** * Invalidates sitemap cache. * * This is so the next sitemap request re-generates the caches. * Serves as performance improvement for post-based action listeners. * * On setups with large posts table, fully regenerating sitemap can take a * while. So instead, we just invalidate the cache and potentially ping the * search engines to notify them about the change. * * @param $post_id */ public function handle_post_save( $post_id ) { $post = get_post( $post_id ); if ( ! Utils::is_post_type_included( $post->post_type ) || wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { return; } $this->invalidate_sitemap_cache(); if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { // The above if condition is necessary because save_post is called twice by gutenberg: https://github.com/WordPress/gutenberg/issues/12903 // We don't want the search engines to be notified of sitemap changes twice, so as a workaround we are going to invalidate sitemap cache both times // but only prime the cache for gutenberg (and other rests requests). Utils::prime_cache( false ); } } /** * @param $post_id * * @return void */ public function handle_post_delete( $post_id ) { if ( ! Utils::is_post_included( get_post( $post_id ) ) ) { return; } $this->invalidate_sitemap_cache(); Utils::prime_cache( false ); } /** * @param $data * @param $term_id * @param $taxonomy * * @return mixed */ public function handle_term_slug_update( $data, $term_id, $taxonomy ) { $term = get_term( $term_id, $taxonomy ); $new_slug = \smartcrawl_get_array_value( $data, 'slug' ); $taxonomy_included = Utils::is_taxonomy_included( $taxonomy ); if ( $taxonomy_included && ! empty( $term->count ) && $new_slug !== $term->slug ) { $this->invalidate_sitemap_cache(); Utils::prime_cache( false ); } return $data; } /** * @param $term_id * @param $taxonomy * * @return void */ public function handle_term_deletion( $term_id, $taxonomy ) { $term = get_term( $term_id, $taxonomy ); if ( is_wp_error( $term ) ) { return; } if ( ! Utils::is_term_included( $term ) ) { return; } $this->invalidate_sitemap_cache(); Utils::prime_cache( false ); } /** * @return void */ public function json_update_sitemap() { $this->invalidate_sitemap_cache(); Utils::prime_cache( true ); die( 1 ); } /** * @return void */ public function json_update_engines() { Utils::notify_engines( 1 ); die( 1 ); } /** * @return array|mixed */ private function get_request_data() { return isset( $_POST['_wds_nonce'] ) && wp_verify_nonce( $_POST['_wds_nonce'], 'wds-nonce' ) ? stripslashes_deep( $_POST ) : array(); } /** * @return void */ public function invalidate_sitemap_cache() { Cache::get()->invalidate(); } }