rtrim( (string) $uploads['basedir'], '/' ), 'label' => 'uploads', ); case self::TYPE_MU_PLUGINS: if ( ! defined( 'WPMU_PLUGIN_DIR' ) ) { return new \WP_Error( 'mu_plugins_unavailable', __( 'WPMU_PLUGIN_DIR is not defined on this install.', 'acrossai-abilities-manager' ) ); } $mu_dir = rtrim( (string) WPMU_PLUGIN_DIR, '/' ); if ( $for_destination && ! is_dir( $mu_dir ) ) { if ( ! wp_mkdir_p( $mu_dir ) ) { return new \WP_Error( 'mu_plugins_create_failed', __( 'Could not create the mu-plugins directory.', 'acrossai-abilities-manager' ) ); } } return array( 'abs_path' => $mu_dir, 'label' => 'mu-plugins', ); case self::TYPE_PATH: default: return self::resolve_abspath_relative( $target, $for_destination ); } } /** * Resolve a plugin slug / file / name to its containing directory. * * @return array{abs_path: string, label: string}|\WP_Error */ private static function resolve_plugin_dir( string $target ) { if ( '' === $target ) { return new \WP_Error( 'invalid_target', __( 'A plugin slug is required when target_type is "plugin".', 'acrossai-abilities-manager' ) ); } if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugin_file = null; if ( function_exists( '\get_plugins' ) ) { $installed = get_plugins(); if ( isset( $installed[ $target ] ) ) { $plugin_file = $target; } } if ( null === $plugin_file ) { $resolved = Plugin_Helpers::resolve_plugin( $target ); if ( ! empty( $resolved['plugin_file'] ) && $resolved['certainty'] >= 8.0 ) { $plugin_file = (string) $resolved['plugin_file']; } } if ( null === $plugin_file ) { $plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? rtrim( (string) WP_PLUGIN_DIR, '/' ) : rtrim( WP_CONTENT_DIR, '/' ) . '/plugins'; $candidate = $plugins_dir . '/' . ltrim( $target, '/' ); $candidate = rtrim( $candidate, '/' ); if ( is_dir( $candidate ) ) { return array( 'abs_path' => $candidate, 'label' => 'plugin:' . basename( $candidate ), ); } return new \WP_Error( 'plugin_not_found', sprintf( /* translators: %s: submitted plugin slug */ __( 'Could not resolve plugin "%s".', 'acrossai-abilities-manager' ), $target ) ); } $plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? rtrim( (string) WP_PLUGIN_DIR, '/' ) : rtrim( WP_CONTENT_DIR, '/' ) . '/plugins'; // A plugin file like "hello.php" (single-file plugin) has no dir of its own — zip the file itself would be surprising. // For a plugin file like "hello-dolly/hello.php" the directory is "hello-dolly". $slug_dir = strtok( $plugin_file, '/' ); if ( false === $slug_dir || '' === $slug_dir || strpos( $plugin_file, '/' ) === false ) { return new \WP_Error( 'plugin_single_file', sprintf( /* translators: %s: plugin file path */ __( 'Plugin "%s" is a single file with no directory to archive.', 'acrossai-abilities-manager' ), $plugin_file ) ); } $abs = $plugins_dir . '/' . $slug_dir; if ( ! is_dir( $abs ) ) { return new \WP_Error( 'plugin_dir_missing', sprintf( /* translators: %s: plugin directory path */ __( 'Resolved plugin directory does not exist: %s', 'acrossai-abilities-manager' ), $abs ) ); } return array( 'abs_path' => $abs, 'label' => 'plugin:' . $slug_dir, ); } /** * Resolve a theme stylesheet / name to its directory. * * @return array{abs_path: string, label: string}|\WP_Error */ private static function resolve_theme_dir( string $target ) { if ( '' === $target ) { return new \WP_Error( 'invalid_target', __( 'A theme stylesheet is required when target_type is "theme".', 'acrossai-abilities-manager' ) ); } $stylesheet = null; if ( function_exists( '\wp_get_theme' ) ) { $theme = wp_get_theme( $target ); if ( $theme && $theme->exists() ) { $stylesheet = (string) $theme->get_stylesheet(); } } if ( null === $stylesheet ) { $resolved = Theme_Helpers::resolve_theme( $target ); if ( ! empty( $resolved['stylesheet'] ) && $resolved['certainty'] >= 8.0 ) { $stylesheet = (string) $resolved['stylesheet']; } } if ( null === $stylesheet ) { return new \WP_Error( 'theme_not_found', sprintf( /* translators: %s: submitted theme identifier */ __( 'Could not resolve theme "%s".', 'acrossai-abilities-manager' ), $target ) ); } $themes_root = get_theme_root( $stylesheet ); if ( ! is_string( $themes_root ) || '' === $themes_root ) { $themes_root = rtrim( WP_CONTENT_DIR, '/' ) . '/themes'; } $abs = rtrim( $themes_root, '/' ) . '/' . $stylesheet; if ( ! is_dir( $abs ) ) { return new \WP_Error( 'theme_dir_missing', sprintf( /* translators: %s: theme directory */ __( 'Resolved theme directory does not exist: %s', 'acrossai-abilities-manager' ), $abs ) ); } return array( 'abs_path' => $abs, 'label' => 'theme:' . $stylesheet, ); } /** * Resolve an ABSPATH-relative path with a realpath boundary check. * * @return array{abs_path: string, label: string}|\WP_Error */ private static function resolve_abspath_relative( string $rel_path, bool $for_destination ) { if ( '' === $rel_path ) { return new \WP_Error( 'invalid_target', __( 'A path is required when target_type is "path".', 'acrossai-abilities-manager' ) ); } if ( false !== strpos( $rel_path, "\0" ) ) { return new \WP_Error( 'invalid_path', __( 'Path contains disallowed characters.', 'acrossai-abilities-manager' ) ); } // Feature 135: resolve against every root WordPress uses. A zip source or destination under // a WP_CONTENT_DIR that lives outside ABSPATH is the common case on managed hosts, and the // old ABSPATH-only bound made backing up the live content directory impossible. $resolved = Fs_Roots::resolve( $rel_path, ! $for_destination ); if ( is_wp_error( $resolved ) ) { return new \WP_Error( 'path_out_of_bounds', $resolved->get_error_message() ); } if ( ! $for_destination ) { return array( 'abs_path' => $resolved['path'], 'label' => 'path:' . $rel_path, ); } // For a destination the leaf may not exist yet; resolve() has already contained its parent. return array( 'abs_path' => $resolved['path'], 'label' => 'path:' . $rel_path, ); } }