$types, 'post_status' => 'publish', 'has_password' => false, 'posts_per_page' => 500, 'offset' => $page * 500, 'fields' => 'ids', 'orderby' => 'ID', 'order' => 'ASC', 'no_found_rows' => true, 'update_post_term_cache' => false, 'suppress_filters' => false, ) ); foreach ( $ids as $id ) { $urls[] = get_permalink( $id ); } $page++; } while ( count( $ids ) === 500 ); } if ( CFSP_Settings::crawler( 'taxonomies' ) ) { foreach ( get_taxonomies( array( 'public' => true ), 'names' ) as $tax ) { if ( 'post_format' === $tax ) { continue; } $terms = get_terms( array( 'taxonomy' => $tax, 'hide_empty' => true ) ); if ( is_wp_error( $terms ) ) { continue; } foreach ( $terms as $term ) { $link = get_term_link( $term ); if ( ! is_wp_error( $link ) ) { $urls[] = $link; } } } } if ( CFSP_Settings::crawler( 'authors' ) && $types ) { foreach ( get_users( array( 'has_published_posts' => $types, 'fields' => 'ID' ) ) as $user_id ) { $urls[] = get_author_posts_url( $user_id ); } } if ( CFSP_Settings::crawler( 'date_archives' ) ) { global $wpdb; $months = wp_cache_get( 'cfsp_months', 'cfsp' ); if ( false === $months ) { // The months that have posts, as wp_get_archives() lists them (it has no raw-data API). $months = $wpdb->get_results( "SELECT DISTINCT YEAR(post_date) AS y, MONTH(post_date) AS m FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Same query core's wp_get_archives() runs; cached below. wp_cache_set( 'cfsp_months', $months, 'cfsp', HOUR_IN_SECONDS ); } $years = array(); foreach ( $months as $row ) { $urls[] = get_month_link( $row->y, $row->m ); $years[ $row->y ] = true; } foreach ( array_keys( $years ) as $year ) { $urls[] = get_year_link( $year ); } } if ( CFSP_Settings::crawler( 'feeds' ) ) { $urls[] = get_feed_link(); } $urls = array_merge( $urls, self::sitemap_urls(), self::public_files(), self::extra(), self::extra_files() ); if ( CFSP_Settings::crawler( 'uploads' ) && ! CFSP_Media::exclude_uploads() ) { $urls = array_merge( $urls, self::uploads() ); } return self::clean( apply_filters( 'cfsp_full_urls', $urls ) ); } /** URLs affected when a single post is published / updated / removed. */ public static function for_post( $post_id ) { $post = get_post( $post_id ); if ( ! $post ) { return array(); } $urls = array( home_url( '/' ) ); if ( CFSP_Settings::crawler( 'feeds' ) ) { $urls[] = get_feed_link(); } if ( self::is_public_post( $post ) ) { $urls[] = get_permalink( $post ); $urls = array_merge( $urls, self::paged_urls( $post ) ); } if ( get_option( 'page_for_posts' ) ) { $urls[] = get_permalink( get_option( 'page_for_posts' ) ); } $archive = get_post_type_archive_link( $post->post_type ); if ( $archive ) { $urls[] = $archive; } foreach ( get_object_taxonomies( $post->post_type, 'objects' ) as $tax ) { if ( ! $tax->public || 'post_format' === $tax->name ) { continue; } $terms = get_the_terms( $post, $tax->name ); if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $link = get_term_link( $term ); if ( ! is_wp_error( $link ) ) { $urls[] = $link; if ( CFSP_Settings::crawler( 'feeds' ) ) { $urls[] = get_term_feed_link( $term->term_id, $term->taxonomy ); } } } } } if ( CFSP_Settings::crawler( 'authors' ) && post_type_supports( $post->post_type, 'author' ) ) { $urls[] = get_author_posts_url( $post->post_author ); } if ( 'post' === $post->post_type && CFSP_Settings::crawler( 'date_archives' ) ) { $time = strtotime( $post->post_date ); $urls[] = get_year_link( gmdate( 'Y', $time ) ); $urls[] = get_month_link( gmdate( 'Y', $time ), gmdate( 'm', $time ) ); } if ( $post->post_parent && self::is_public_post( $post->post_parent ) ) { $urls[] = get_permalink( $post->post_parent ); } // Adjacent posts show "previous / next" links to this post. if ( 'post' === $post->post_type && 'publish' === $post->post_status ) { $GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride setup_postdata( $post ); foreach ( array( true, false ) as $previous ) { $adjacent = get_adjacent_post( false, '', $previous ); if ( $adjacent ) { $urls[] = get_permalink( $adjacent ); } } wp_reset_postdata(); } // Posts that show this one as related (build-time related posts). $urls = array_merge( $urls, CFSP_Related::affected_urls( $post->ID ) ); $urls = array_merge( $urls, self::sitemap_urls(), self::extra() ); return self::clean( apply_filters( 'cfsp_post_urls', $urls, $post ) ); } /** * Page 2, 3, … of a post split with . Built the same way as WordPress's * own page links (_wp_link_page), so they match the URLs the crawler finds on a full export. */ public static function paged_urls( $post ) { $post = get_post( $post ); if ( ! $post ) { return array(); } $pages = preg_match_all( '//i', $post->post_content ) + 1; if ( $pages < 2 ) { return array(); } global $wp_rewrite; $permalink = get_permalink( $post ); $is_front = 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === (int) $post->ID; $urls = array(); for ( $i = 2; $i <= $pages; $i++ ) { if ( ! get_option( 'permalink_structure' ) ) { $urls[] = add_query_arg( 'page', $i, $permalink ); } elseif ( $is_front ) { $urls[] = trailingslashit( $permalink ) . user_trailingslashit( $wp_rewrite->pagination_base . '/' . $i, 'single_paged' ); } else { $urls[] = trailingslashit( $permalink ) . user_trailingslashit( (string) $i, 'single_paged' ); } } return $urls; } /** Post types selected in Export → Content types. */ public static function post_types() { $public = get_post_types( array( 'public' => true ), 'names' ); $selected = array_values( array_intersect( (array) CFSP_Settings::get( 'post_types' ), $public ) ); return array_values( apply_filters( 'cfsp_post_types', array_diff( $selected, array( 'attachment' ) ) ) ); } public static function is_public_post( $post ) { $post = get_post( $post ); return $post && 'publish' === $post->post_status && '' === $post->post_password && in_array( $post->post_type, self::post_types(), true ); } /** Sitemap index URLs (core, plus Yoast / Rank Math / AIOSEO when the integration is on). */ public static function sitemap_urls() { if ( ! CFSP_Settings::crawler( 'sitemaps' ) ) { return array(); } $urls = array(); if ( function_exists( 'wp_sitemaps_get_server' ) && wp_sitemaps_get_server()->sitemaps_enabled() ) { $urls[] = get_sitemap_url( 'index' ); } if ( CFSP_Settings::get( 'int_seo_sitemaps' ) ) { foreach ( CFSP_Integrations::seo_sitemaps() as $path ) { $urls[] = home_url( $path ); } } return $urls; } private static function public_files() { if ( ! CFSP_Settings::crawler( 'public_files' ) ) { return array(); } return array_map( 'home_url', self::PUBLIC_FILES ); } private static function extra() { $urls = array(); foreach ( CFSP_Settings::lines( 'extra_urls' ) as $line ) { $urls[] = preg_match( '#^https?://#i', $line ) ? $line : home_url( '/' . ltrim( $line, '/' ) ); } return $urls; } /** * Export → Additional files and directories. * Paths are absolute or relative to the WordPress root; directories are included recursively. * Only paths inside the site folder, uploads, themes or plugins are allowed. */ public static function extra_files() { $roots = array(); foreach ( CFSP_Paths::roots() as $root ) { $real_root = realpath( $root[1] ); if ( $real_root ) { $roots[] = array( $real_root, $root[0] ); } } $urls = array(); $limit = (int) apply_filters( 'cfsp_extra_files_limit', 20000 ); foreach ( CFSP_Settings::lines( 'extra_files' ) as $line ) { $line = str_replace( '\\', '/', $line ); $path = $line; // A site address ("https://example.com/font/") means that path under the WordPress root. if ( preg_match( '#^https?://#i', $path ) ) { $path = rawurldecode( (string) wp_parse_url( $path, PHP_URL_PATH ) ); $home = rtrim( (string) wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); if ( '' !== $home && 0 === strpos( $path, $home . '/' ) ) { $path = substr( $path, strlen( $home ) ); } $path = ltrim( $path, '/' ); } $real = preg_match( '#^(/|[a-zA-Z]:/)#', $path ) ? realpath( $path ) : false; if ( ! $real ) { // "/font/" is usually meant as the site's /font/ folder, not the server's root. $real = realpath( CFSP_Paths::home_dir() . ltrim( $path, '/' ) ); } if ( ! $real ) { CFSP_Logger::error( 'Additional file not found: ' . $line ); continue; } $base = null; foreach ( $roots as $root ) { if ( $real === $root[0] || 0 === strpos( $real, rtrim( $root[0], '/\\' ) . DIRECTORY_SEPARATOR ) || 0 === strpos( $real, rtrim( $root[0], '/\\' ) . '/' ) ) { $base = $root; break; } } if ( ! $base ) { CFSP_Logger::error( 'Additional file is outside the WordPress install and was skipped: ' . $line ); continue; } $files = array(); if ( is_dir( $real ) ) { $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $real, FilesystemIterator::SKIP_DOTS ) ); foreach ( $it as $file ) { if ( $file->isFile() ) { $files[] = $file->getPathname(); } } } elseif ( is_file( $real ) ) { $files[] = $real; } foreach ( $files as $path ) { $rel = ltrim( str_replace( '\\', '/', substr( $path, strlen( $base[0] ) ) ), '/' ); // Never publish PHP, dotfiles or the plugin's own working folders. if ( preg_match( '#(^|/)\.|\.(php\d?|phtml|phar|sql|log|ini)$#i', $rel ) || false !== strpos( $rel, 'cfsp-' ) || 0 === strpos( $rel, CFSP_Settings::DATA_DIR . '/' ) ) { continue; } $urls[] = untrailingslashit( $base[1] ) . '/' . implode( '/', array_map( 'rawurlencode', explode( '/', $rel ) ) ); if ( count( $urls ) >= $limit ) { CFSP_Logger::error( sprintf( 'Additional files: stopped after %d files.', $limit ) ); return $urls; } } } return $urls; } private static function uploads() { $uploads = wp_upload_dir( null, false ); $base = $uploads['basedir']; $urls = array(); if ( ! is_dir( $base ) ) { return $urls; } $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base, FilesystemIterator::SKIP_DOTS ) ); foreach ( $it as $file ) { $rel = str_replace( '\\', '/', substr( $file->getPathname(), strlen( $base ) ) ); if ( 0 === strpos( ltrim( $rel, '/' ), 'cfsp-' ) || 0 === strpos( ltrim( $rel, '/' ), CFSP_Settings::DATA_DIR . '/' ) || ! $file->isFile() ) { continue; } $url = $uploads['baseurl'] . '/' . implode( '/', array_map( 'rawurlencode', explode( '/', ltrim( $rel, '/' ) ) ) ); if ( CFSP_Exporter::is_asset( $url ) ) { $urls[] = $url; } } return $urls; } private static function clean( $urls ) { $out = array(); foreach ( $urls as $url ) { if ( ! is_string( $url ) || '' === $url ) { continue; } $normalized = CFSP_Exporter::normalize( $url ); if ( $normalized && ! CFSP_Exporter::is_excluded( $normalized ) ) { $out[ CFSP_Exporter::key( $normalized ) ] = true; } } return array_keys( $out ); } }