backups_dir = rtrim( str_replace( '\\', '/', $backups_dir ), '/' ); $map = array( 'skip_media' => 'uploads', 'skip_themes' => 'themes', 'skip_plugins' => 'plugins', 'skip_mu_plugins' => 'mu-plugins', 'skip_other' => 'other', ); foreach ( $map as $key => $group ) { if ( ! empty( $options[ $key ] ) ) { $this->skip_groups[] = $group; } } if ( ! empty( $options['active_theme_only'] ) ) { $this->keep_themes = self::active_themes(); } if ( ! empty( $options['active_plugins_only'] ) ) { $this->keep_plugins = self::active_plugins(); } } /** * Folder names of the active theme and its parent. * * @return array */ public static function active_themes() { return array_values( array_unique( array_filter( array( (string) get_option( 'stylesheet' ), (string) get_option( 'template' ) ) ) ) ); } /** * Folder (or single-file) names of the active plugins. * * @return array */ public static function active_plugins() { $keep = array( 'index.php' ); foreach ( (array) get_option( 'active_plugins', array() ) as $plugin ) { $parts = explode( '/', (string) $plugin ); $keep[] = $parts[0]; } return array_values( array_unique( $keep ) ); } /** * Which group a path relative to wp-content belongs to. * * @param string $rel Relative path. * @return string uploads, themes, plugins, mu-plugins or other. */ public static function group( $rel ) { $top = explode( '/', $rel, 2 ); return in_array( $top[0], self::GROUPS, true ) ? $top[0] : 'other'; } /** * Public form of the exclusion check, for size scans. * * @param string $rel Path relative to wp-content. * @param string $path Absolute path. * @return bool */ public function excluded( $rel, $path ) { return $this->is_excluded( $rel, $path ); } /** * Label for the UI. * * @return string */ public function label() { return __( 'Files', 'sitezip' ); } /** * Add files until the deadline. * * @param array $state Job state. * @param ZipWriter $writer Archive writer. * @param float $deadline Stop-by timestamp. * @return bool|\WP_Error */ public function step( array &$state, ZipWriter $writer, $deadline ) { $root = rtrim( str_replace( '\\', '/', WP_CONTENT_DIR ), '/' ); if ( ! isset( $state['files'] ) ) { $state['files'] = array( 'stack' => array( '' ), 'cur' => null, 'idx' => 0, 'count' => 0, 'bytes' => 0, ); } $files = &$state['files']; while ( microtime( true ) < $deadline ) { if ( null === $files['cur'] ) { if ( ! $files['stack'] ) { $state['file_count'] = $files['count']; $state['file_bytes'] = $files['bytes']; unset( $state['files'] ); return true; } $files['cur'] = array_shift( $files['stack'] ); $files['idx'] = 0; } $abs = '' === $files['cur'] ? $root : $root . '/' . $files['cur']; $entries = @scandir( $abs ); if ( false === $entries ) { // Unreadable directory: skip it rather than fail the backup. $files['cur'] = null; continue; } sort( $entries, SORT_STRING ); $total = count( $entries ); for ( $i = (int) $files['idx']; $i < $total; $i++ ) { $name = $entries[ $i ]; if ( '.' === $name || '..' === $name ) { continue; } $rel = '' === $files['cur'] ? $name : $files['cur'] . '/' . $name; $path = $abs . '/' . $name; if ( $this->is_excluded( $rel, $path ) || ! PathGuard::is_not_symlink( $path ) ) { $files['idx'] = $i + 1; continue; } if ( is_dir( $path ) ) { $files['stack'][] = $rel; $files['idx'] = $i + 1; continue; } if ( ! is_file( $path ) || ! is_readable( $path ) ) { $files['idx'] = $i + 1; continue; } $added = $writer->add_file( $path, 'wp-content/' . $rel ); if ( is_wp_error( $added ) ) { return $added; } // Advance only once the entry is really in the archive, so a // failure before this point retries the same file. $files['idx'] = $i + 1; if ( true === $added ) { $files['count']++; $files['bytes'] += (int) @filesize( $path ); } if ( microtime( true ) >= $deadline ) { return false; } } $files['cur'] = null; } return false; } /** * Whether a path should be left out of the archive. * * @param string $rel Path relative to wp-content. * @param string $path Absolute path. * @return bool */ private function is_excluded( $rel, $path ) { return '' !== $this->exclusion_reason( $rel, $path ); } /** * Why a path is left out of the archive. * * @param string $rel Path relative to wp-content. * @param string $path Absolute path. * @return string '' when it is backed up, else backups, server, option, * dev, cache or logs. */ public function exclusion_reason( $rel, $path ) { $normalised = str_replace( '\\', '/', $path ); if ( $normalised === $this->backups_dir || 0 === strpos( $normalised, $this->backups_dir . '/' ) ) { return 'backups'; } $depth = substr_count( $rel, '/' ); if ( $depth <= 1 && '' !== self::host_specific( $rel ) ) { return 'server'; } if ( 0 === $depth && $this->skip_groups && in_array( self::group( $rel ), $this->skip_groups, true ) ) { return 'option'; } if ( 1 === $depth ) { list( $top, $name ) = explode( '/', $rel, 2 ); if ( 'themes' === $top && null !== $this->keep_themes && is_dir( $path ) && ! in_array( $name, $this->keep_themes, true ) ) { return 'option'; } if ( 'plugins' === $top && null !== $this->keep_plugins && ! in_array( $name, $this->keep_plugins, true ) ) { return 'option'; } } $base = basename( $rel ); if ( in_array( $base, self::SKIP_ANYWHERE, true ) ) { return 'dev'; } if ( false === strpos( $rel, '/' ) ) { if ( in_array( $rel, self::SKIP_CACHE, true ) ) { return 'cache'; } if ( in_array( $rel, self::SKIP_TOP_LEVEL, true ) || preg_match( PathGuard::BACKUPS_FOLDER_PATTERN, $rel ) ) { return 'backups'; } } // Logs are large, regenerable, and often hold request data. return preg_match( '/\.(log|tmp|swp)$/i', $base ) ? 'logs' : ''; } }