'Home & blog page', 'post_types' => 'Posts & pages', 'taxonomies' => 'Categories & tags', 'authors' => 'Author archives', 'pagination' => 'Page 2, 3, …', 'date_archives' => 'Monthly archives', 'feeds' => 'RSS feeds', 'sitemaps' => 'XML sitemaps', 'public_files' => 'robots.txt & ads.txt', 'not_found' => 'Not-found page', 'uploads' => 'Every uploaded file', ); } public static function post_type_options() { $out = array(); foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $type ) { if ( 'attachment' !== $type->name ) { $out[ $type->name ] = $type->labels->name; } } return $out; } /** * type: bool|int|enum|list|string|url|email|text|raw|secret */ public static function schema() { $b = function ( $default ) { return array( 'type' => 'bool', 'default' => $default ); }; return array( // General. 'url_mode' => array( 'type' => 'enum', 'default' => 'absolute', 'options' => array( 'absolute', 'relative' ) ), 'destination_url' => array( 'type' => 'url', 'default' => '' ), 'relative_path' => array( 'type' => 'string', 'default' => '/' ), 'force_replace' => $b( 1 ), 'enhanced_crawl' => $b( 1 ), 'crawlers' => array( 'type' => 'list', 'default' => array( 'home', 'post_types', 'taxonomies', 'authors', 'pagination', 'feeds', 'sitemaps', 'public_files', 'not_found' ), 'options' => 'crawlers' ), 'post_types' => array( 'type' => 'list', 'default' => array( 'post', 'page' ) ), 'follow_links' => $b( 1 ), 'comment_feeds' => $b( 0 ), 'extra_urls' => array( 'type' => 'text', 'default' => '' ), 'extra_files' => array( 'type' => 'text', 'default' => '' ), 'feed_xml' => $b( 1 ), 'feed_style' => $b( 1 ), 'feed_no_footer' => $b( 1 ), 'feed_css' => array( 'type' => 'raw', 'default' => '' ), 'sitemap_redirect' => $b( 1 ), 'sitemap_style' => $b( 1 ), 'sitemap_css' => array( 'type' => 'raw', 'default' => '' ), 'exclude_patterns' => array( 'type' => 'text', 'default' => "/wp-admin/\n/wp-login.php\n/wp-json/\n/xmlrpc.php\n/feed/atom/\n/feed/rdf/\n/comments/feed/" ), // Deploy. 'target' => array( 'type' => 'enum', 'default' => 'github', 'options' => array( 'github', 'cloudflare', 'local' ) ), 'gh_repo' => array( 'type' => 'string', 'default' => '' ), 'gh_branch' => array( 'type' => 'string', 'default' => 'main' ), 'gh_path' => array( 'type' => 'string', 'default' => '' ), 'gh_commit_message' => array( 'type' => 'string', 'default' => '{reason} ({count})' ), 'gh_author_name' => array( 'type' => 'string', 'default' => '' ), 'gh_author_email' => array( 'type' => 'email', 'default' => '' ), 'gh_token' => array( 'type' => 'secret', 'default' => '' ), // Legacy fallback. 'account_id' => array( 'type' => 'string', 'default' => '' ), 'api_token' => array( 'type' => 'secret', 'default' => '' ), 'project_name' => array( 'type' => 'string', 'default' => '' ), 'branch' => array( 'type' => 'string', 'default' => 'main' ), 'redirects' => array( 'type' => 'raw', 'default' => '' ), 'redirect_renamed' => $b( 1 ), 'redirect_grace' => array( 'type' => 'int', 'default' => 30, 'min' => 0, 'max' => 1440 ), 'headers' => array( 'type' => 'raw', 'default' => '' ), // Media offload (Cloudflare R2). 'r2_enabled' => $b( 0 ), 'r2_account_id' => array( 'type' => 'string', 'default' => '' ), 'r2_access_key' => array( 'type' => 'string', 'default' => '' ), 'r2_secret_key' => array( 'type' => 'secret', 'default' => '' ), 'r2_bucket' => array( 'type' => 'string', 'default' => '' ), 'r2_endpoint' => array( 'type' => 'url', 'default' => '' ), 'r2_public_url' => array( 'type' => 'url', 'default' => '' ), 'r2_prefix' => array( 'type' => 'string', 'default' => 'wp-content/uploads' ), 'r2_cache_control' => array( 'type' => 'string', 'default' => 'public, max-age=31536000' ), 'r2_exclude_dirs' => array( 'type' => 'text', 'default' => "cache\nwc-logs\nwpo-cache\nbackups\nbackup\nai1wm-backups\nupdraft\nwpforms/cache" ), 'r2_exclude_names' => array( 'type' => 'text', 'default' => '' ), 'r2_auto_upload' => $b( 1 ), 'r2_delete' => $b( 0 ), 'r2_resync' => array( 'type' => 'enum', 'default' => 'daily', 'options' => array( 'off', 'hourly', 'twicedaily', 'daily', 'weekly' ) ), 'r2_rewrite' => $b( 1 ), 'r2_exclude_export' => $b( 1 ), // Workflow. 'auto_push' => $b( 1 ), 'trigger_publish' => $b( 1 ), 'trigger_update' => $b( 1 ), 'trigger_remove' => $b( 1 ), 'trigger_comments' => $b( 1 ), 'trigger_terms' => $b( 1 ), 'trigger_sitewide' => $b( 1 ), 'push_delay' => array( 'type' => 'int', 'default' => 120, 'min' => 0, 'max' => 3600 ), 'page_depth' => array( 'type' => 'int', 'default' => 0, 'min' => 0, 'max' => 9999 ), 'rescue_missed' => $b( 1 ), 'schedule' => array( 'type' => 'enum', 'default' => 'off', 'options' => array( 'off', 'hourly', 'twicedaily', 'daily', 'weekly' ) ), 'notify_failure' => $b( 0 ), 'notify_success' => $b( 0 ), 'notify_email' => array( 'type' => 'email', 'default' => '' ), // Optimize. 'opt_html_comments' => $b( 1 ), 'opt_sourceurl' => $b( 1 ), 'opt_img_auto_sizes' => $b( 1 ), 'opt_admin_bar_css' => $b( 1 ), 'opt_global_styles' => $b( 1 ), 'opt_dashicons' => $b( 1 ), 'opt_yoast_schema' => $b( 1 ), 'opt_emoji' => $b( 1 ), 'opt_classic_styles' => $b( 0 ), 'opt_c0_preconnect' => $b( 1 ), 'opt_speculation' => $b( 1 ), 'opt_jetpack_rp_js' => $b( 1 ), 'opt_site_icon' => $b( 0 ), 'opt_home_next' => $b( 0 ), 'a11y_main' => $b( 0 ), 'a11y_main_selector' => array( 'type' => 'string', 'default' => '' ), 'opt_minify_html' => $b( 0 ), 'hints_media' => $b( 1 ), 'hints_origins' => array( 'type' => 'text', 'default' => '' ), 'cleanup_custom' => array( 'type' => 'raw', 'default' => '' ), // Hide WP. 'private_mode' => $b( 0 ), 'private_action' => array( 'type' => 'enum', 'default' => 'login', 'options' => array( 'login', 'notfound' ) ), 'hide_generator' => $b( 1 ), 'hide_wp_links' => $b( 1 ), 'hide_ver' => $b( 0 ), // Forms. 'forms_remove_comments' => $b( 0 ), 'forms_endpoint_on' => $b( 0 ), 'forms_endpoint' => array( 'type' => 'url', 'default' => '' ), // Search. 'llms_enabled' => $b( 0 ), 'llms_summary' => array( 'type' => 'string', 'default' => '' ), 'llms_details' => array( 'type' => 'text', 'default' => '' ), 'llms_sections' => array( 'type' => 'int', 'default' => 20, 'min' => 0, 'max' => 100 ), 'llms_pages' => $b( 1 ), 'llms_posts' => array( 'type' => 'int', 'default' => 30, 'min' => 0, 'max' => 200 ), 'search_enabled' => $b( 0 ), 'search_excerpt' => $b( 1 ), 'search_post_types' => array( 'type' => 'list', 'default' => array( 'post', 'page' ) ), 'search_fuzziness' => array( 'type' => 'int', 'default' => 3, 'min' => 0, 'max' => 10 ), 'search_min_chars' => array( 'type' => 'int', 'default' => 2, 'min' => 1, 'max' => 5 ), 'search_limit' => array( 'type' => 'int', 'default' => 20, 'min' => 5, 'max' => 100 ), // Related posts (build time). 'related_enabled' => $b( 0 ), 'related_post_types' => array( 'type' => 'list', 'default' => array( 'post' ) ), 'related_match' => array( 'type' => 'enum', 'default' => 'both', 'options' => array( 'both', 'tags', 'categories' ) ), 'related_count' => array( 'type' => 'int', 'default' => 3, 'min' => 1, 'max' => 12 ), 'related_skip_default_cat' => $b( 1 ), 'related_fill' => array( 'type' => 'enum', 'default' => 'latest', 'options' => array( 'latest', 'none' ) ), 'related_refresh' => array( 'type' => 'int', 'default' => 10, 'min' => 0, 'max' => 50 ), 'related_design' => array( 'type' => 'enum', 'default' => 'auto', 'options' => array( 'auto', 'jetpack', 'simple', 'custom' ) ), 'related_headline' => array( 'type' => 'string', 'default' => 'Related' ), 'related_show_image' => $b( 1 ), 'related_image_size' => array( 'type' => 'string', 'default' => 'full' ), 'related_show_date' => $b( 1 ), 'related_show_category' => $b( 1 ), 'related_show_excerpt' => $b( 0 ), 'related_item_template' => array( 'type' => 'raw', 'default' => '' ), 'related_wrapper_template' => array( 'type' => 'raw', 'default' => '' ), // Integrations. 'int_seo_sitemaps' => $b( 1 ), 'int_redirection' => $b( 1 ), // Debug. 'debug_log' => $b( 0 ), 'delete_data' => $b( 0 ), 'batch_seconds' => array( 'type' => 'int', 'default' => 20, 'min' => 5, 'max' => 120 ), 'http_timeout' => array( 'type' => 'int', 'default' => 30, 'min' => 5, 'max' => 120 ), 'sslverify' => $b( 0 ), 'fetch_concurrency' => array( 'type' => 'int', 'default' => 4, 'min' => 1, 'max' => 10 ), 'direct_fetch' => $b( 1 ), 'direct_ip' => array( 'type' => 'string', 'default' => '' ), 'r2_parallel' => array( 'type' => 'int', 'default' => 8, 'min' => 1, 'max' => 32 ), ); } public static function defaults() { return wp_list_pluck( self::schema(), 'default' ); } public static function all() { if ( null !== self::$cache ) { return self::$cache; } $saved = get_option( self::OPTION, array() ); $saved = is_array( $saved ) ? self::migrate( $saved ) : array(); $all = array_merge( self::defaults(), array_intersect_key( $saved, self::schema() ) ); if ( defined( 'CFSP_GITHUB_TOKEN' ) && CFSP_GITHUB_TOKEN ) { $all['gh_token'] = CFSP_GITHUB_TOKEN; } elseif ( class_exists( 'CFSP_GitHub_Auth' ) && CFSP_GitHub_Auth::token() ) { $all['gh_token'] = CFSP_GitHub_Auth::token(); } if ( defined( 'CFSP_API_TOKEN' ) && CFSP_API_TOKEN ) { $all['api_token'] = CFSP_API_TOKEN; } if ( defined( 'CFSP_ACCOUNT_ID' ) && CFSP_ACCOUNT_ID ) { $all['account_id'] = CFSP_ACCOUNT_ID; } if ( defined( 'CFSP_R2_ACCESS_KEY' ) && CFSP_R2_ACCESS_KEY ) { $all['r2_access_key'] = CFSP_R2_ACCESS_KEY; } if ( defined( 'CFSP_R2_SECRET_KEY' ) && CFSP_R2_SECRET_KEY ) { $all['r2_secret_key'] = CFSP_R2_SECRET_KEY; } if ( ! $all['r2_account_id'] && $all['account_id'] ) { $all['r2_account_id'] = $all['account_id']; // Same Cloudflare account as Pages. } self::$cache = $all; return $all; } public static function get( $key ) { $all = self::all(); return $all[ $key ] ?? null; } /** Settings from v1.x used different keys. */ private static function migrate( array $saved ) { if ( isset( $saved['cleanup'] ) && ! isset( $saved['opt_html_comments'] ) ) { foreach ( array( 'html_comments', 'sourceurl', 'img_auto_sizes', 'admin_bar_css', 'global_styles', 'dashicons', 'yoast_schema' ) as $key ) { $saved[ 'opt_' . $key ] = in_array( $key, (array) $saved['cleanup'], true ) ? 1 : 0; } } if ( isset( $saved['auto_full_rebuild'] ) && ! isset( $saved['trigger_sitewide'] ) ) { $saved['trigger_sitewide'] = (int) $saved['auto_full_rebuild']; } if ( ! empty( $saved['include_uploads'] ) && ! isset( $saved['crawlers'] ) ) { $saved['crawlers'] = self::defaults()['crawlers']; $saved['crawlers'][] = 'uploads'; } return $saved; } /** * Merge $changes into the saved settings, sanitize and store. * * @return array The stored settings. */ public static function update( array $changes ) { $old = get_option( self::OPTION, array() ); $old = is_array( $old ) ? self::migrate( $old ) : array(); $new = self::sanitize( array_merge( self::defaults(), $old, $changes ), $old ); update_option( self::OPTION, $new, false ); self::$cache = null; do_action( 'cfsp_settings_updated', $new, $old ); return $new; } public static function sanitize( array $input, array $old = array() ) { $out = array(); foreach ( self::schema() as $key => $field ) { $value = $input[ $key ] ?? $field['default']; switch ( $field['type'] ) { case 'bool': $out[ $key ] = ( true === $value || 1 === $value || '1' === $value || 'true' === $value || 'on' === $value ) ? 1 : 0; break; case 'int': $out[ $key ] = max( $field['min'], min( $field['max'], (int) $value ) ); break; case 'enum': $out[ $key ] = in_array( $value, $field['options'], true ) ? $value : $field['default']; break; case 'list': $list = array_values( array_unique( array_filter( array_map( 'sanitize_key', (array) $value ) ) ) ); if ( 'crawlers' === ( $field['options'] ?? '' ) ) { $list = array_values( array_intersect( $list, array_keys( self::crawler_options() ) ) ); } $out[ $key ] = $list; break; case 'url': $out[ $key ] = untrailingslashit( esc_url_raw( trim( (string) $value ) ) ); break; case 'email': $out[ $key ] = sanitize_email( (string) $value ); break; case 'text': $out[ $key ] = sanitize_textarea_field( (string) $value ); break; case 'raw': $out[ $key ] = trim( str_replace( "\r\n", "\n", (string) $value ) ); break; case 'slug': $out[ $key ] = sanitize_title( (string) $value ) ?: $field['default']; break; case 'secret': // Empty means "keep what's saved" so secrets never have to be sent back to the browser. $value = trim( (string) $value ); $out[ $key ] = '' !== $value ? sanitize_text_field( $value ) : ( $old[ $key ] ?? '' ); break; default: $out[ $key ] = sanitize_text_field( (string) $value ); } } $out['gh_repo'] = trim( preg_replace( '#^(https?://github\.com/|git@github\.com:)|\.git$#i', '', $out['gh_repo'] ), '/' ); $out['gh_branch'] = $out['gh_branch'] ? $out['gh_branch'] : 'main'; $out['gh_path'] = trim( str_replace( array( '\\', '..' ), array( '/', '' ), $out['gh_path'] ), '/' ); $out['branch'] = $out['branch'] ? $out['branch'] : 'main'; $out['project_name'] = sanitize_title( $out['project_name'] ); $out['relative_path'] = '/' . trim( str_replace( array( '\\', '..' ), array( '/', '' ), $out['relative_path'] ), '/' ); $out['r2_prefix'] = trim( str_replace( array( '\\', '..' ), array( '/', '' ), $out['r2_prefix'] ), '/' ); $out['r2_bucket'] = strtolower( preg_replace( '/[^a-zA-Z0-9-]/', '', $out['r2_bucket'] ) ); return $out; } /** Values safe to send to the browser (secrets replaced by a flag). */ public static function for_js() { $all = self::all(); foreach ( self::schema() as $key => $field ) { if ( 'secret' === $field['type'] ) { $all[ $key ] = ''; } } return $all; } public static function is_configured() { $s = self::all(); if ( 'local' === $s['target'] ) { return true; } if ( 'cloudflare' === $s['target'] ) { return $s['account_id'] && $s['api_token'] && $s['project_name']; } return $s['gh_token'] && preg_match( '#^[\w.-]+/[\w.-]+$#', $s['gh_repo'] ); } /** True when finished exports should be pushed somewhere. */ public static function has_remote() { return 'local' !== self::get( 'target' ) && self::is_configured(); } /** Folder inside the repository for the static files: '' (root) or 'path/'. */ public static function github_prefix() { $path = trim( (string) self::get( 'gh_path' ), '/' ); return '' === $path ? '' : $path . '/'; } /** Name of the plugin's folder in uploads. */ const DATA_DIR = 'staticpush'; /** Folder names used before 2.16.6, moved into DATA_DIR by migrate_dirs(). */ const LEGACY_DIRS = array( 'export' => 'cfsp-static', 'work' => 'cfsp-work', ); /** * The plugin's own folder in uploads (uploads/staticpush). Deny rules for Apache and IIS plus * random sub-folder names keep it from direct web access on any server. * The generated site and the work files are only read back by the plugin, to send them to * GitHub or Cloudflare or into a ZIP; WordPress never serves or runs them. */ public static function data_dir() { $uploads = wp_upload_dir( null, false ); $dir = trailingslashit( $uploads['basedir'] ) . self::DATA_DIR; if ( ! is_file( $dir . '/.htaccess' ) ) { self::protect_dir( $dir ); } return $dir; } /** Write deny rules (Apache, IIS) and an empty index into $dir. */ private static function protect_dir( $dir ) { if ( ! wp_mkdir_p( $dir ) ) { return; } $files = array( '.htaccess' => "# StaticPush working files. Not for direct web access.\n\n\tRequire all denied\n\n\n\tOrder deny,allow\n\tDeny from all\n\n", 'web.config' => "\n\n\t\n\t\t\n\t\t\t\n\t\t\n\t\n\n", 'index.php' => " $body ) { if ( ! is_file( $dir . '/' . $name ) ) { file_put_contents( $dir . '/' . $name, $body ); } } } /** Export and work folder paths for this request, filled by sub_dir(). */ private static $dirs = array(); /** Version of the folder layout; migrate_dirs() runs until the stored value matches. */ const DIRS_VERSION = 2; /** * Random part of the folder names, made once per site. Many servers (nginx) ignore * .htaccess, so the names themselves keep the folders from being found from the web. */ private static function dir_key() { $key = (string) get_option( 'cfsp_dir_key' ); if ( ! preg_match( '/^[a-z0-9]{20}$/', $key ) ) { $key = strtolower( wp_generate_password( 20, false, false ) ); update_option( 'cfsp_dir_key', $key, true ); } return $key; } /** Path of a sub-folder of data_dir(), e.g. uploads/staticpush/export-. */ private static function target_dir( $key ) { $uploads = wp_upload_dir( null, false ); return trailingslashit( $uploads['basedir'] ) . self::DATA_DIR . '/' . $key . '-' . self::dir_key(); } /** * Earlier locations of a sub-folder, oldest layout first: uploads/cfsp-static (before 2.16.6), * uploads/staticpush/export (2.16.6) and export- (a key that was reset). */ private static function legacy_dirs( $key ) { $uploads = wp_upload_dir( null, false ); $base = trailingslashit( $uploads['basedir'] ); $data = $base . self::DATA_DIR . '/'; $target = self::target_dir( $key ); $list = array( $base . self::LEGACY_DIRS[ $key ], $data . $key ); $others = glob( $data . $key . '-*', GLOB_ONLYDIR ); foreach ( is_array( $others ) ? $others : array() as $dir ) { if ( wp_normalize_path( $dir ) !== wp_normalize_path( $target ) ) { $list[] = $dir; } } return $list; } /** The first earlier location that still holds files, or ''. */ private static function legacy_dir( $key ) { foreach ( self::legacy_dirs( $key ) as $dir ) { if ( ! self::is_empty_dir( $dir ) ) { return $dir; } } return ''; } /** * Current path of a sub-folder. Until migrate_dirs() has moved an older folder, the old * location keeps being used so a running job never loses its files. */ private static function sub_dir( $key ) { if ( isset( self::$dirs[ $key ] ) ) { return self::$dirs[ $key ]; } $new = self::target_dir( $key ); if ( self::is_empty_dir( $new ) ) { $old = self::legacy_dir( $key ); if ( '' !== $old ) { self::$dirs[ $key ] = $old; return $old; } } self::data_dir(); self::$dirs[ $key ] = $new; return $new; } /** True when $dir is missing or has no entries. */ private static function is_empty_dir( $dir ) { if ( ! is_dir( $dir ) ) { return true; } $handle = opendir( $dir ); if ( ! $handle ) { return true; } $empty = true; while ( false !== ( $entry = readdir( $handle ) ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition -- Stops at the first entry instead of listing a folder that can hold thousands. if ( '.' !== $entry && '..' !== $entry ) { $empty = false; break; } } closedir( $handle ); return $empty; } /** * Move older export and work folders to their current place in uploads/staticpush. * Runs only while no job is active; a rename keeps every file and its modified time, * so the hash caches stay valid and nothing is uploaded again. */ public static function migrate_dirs() { if ( (int) get_option( 'cfsp_dirs_migrated' ) >= self::DIRS_VERSION || ( class_exists( 'CFSP_Job' ) && CFSP_Job::is_active() ) ) { return; } $done = true; foreach ( array_keys( self::LEGACY_DIRS ) as $key ) { $new = self::target_dir( $key ); $old = self::legacy_dir( $key ); if ( '' === $old || ! self::is_empty_dir( $new ) ) { continue; // Nothing to move, or the current folder already holds files (older ones are left alone). } if ( is_dir( $new ) ) { rmdir( $new ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- Removes the plugin's own empty folder so the old one can take its place. } self::data_dir(); // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- Moves the plugin's own folder within uploads in one step; WP_Filesystem::move() would copy file by file. if ( ! @rename( $old, $new ) ) { $done = false; continue; } foreach ( array( '.htaccess', 'index.html' ) as $stale ) { if ( 'work' === $key && is_file( $new . '/' . $stale ) ) { wp_delete_file( $new . '/' . $stale ); } } } self::$dirs = array(); delete_transient( 'cfsp_export_count' ); if ( $done ) { update_option( 'cfsp_dirs_migrated', self::DIRS_VERSION, true ); } } /** Directory holding the generated static site. */ public static function export_dir() { return self::sub_dir( 'export' ); } /** Directory for internal state files (manifest, hash cache). */ public static function work_dir() { $dir = self::sub_dir( 'work' ); if ( ! is_dir( $dir ) ) { wp_mkdir_p( $dir ); } return $dir; } /** The WordPress origin that gets replaced in exported files, e.g. https://wp.example.com */ public static function source_url() { return untrailingslashit( home_url() ); } /** Absolute URL of the static site ('' if unknown). */ public static function destination_url() { $dest = self::get( 'destination_url' ); if ( ! $dest && 'cloudflare' === self::get( 'target' ) && self::get( 'project_name' ) ) { $dest = 'https://' . self::get( 'project_name' ) . '.pages.dev'; } return untrailingslashit( (string) $dest ); } /** * What WordPress URLs are replaced with: an absolute origin ("https://example.com") * or, in relative mode, a path prefix ("" for the root, "/sub" for a subdirectory). */ public static function url_base() { if ( 'relative' === self::get( 'url_mode' ) ) { return rtrim( (string) self::get( 'relative_path' ), '/' ); } return self::destination_url(); } public static function lines( $key ) { $lines = preg_split( '/\r\n|\r|\n/', (string) self::get( $key ) ); return array_values( array_filter( array_map( 'trim', $lines ), 'strlen' ) ); } public static function crawler( $name ) { return in_array( $name, (array) self::get( 'crawlers' ), true ); } }