\n" ); } $htaccess = wp_normalize_path( $dir . '/.htaccess' ); if ( ! is_file( $htaccess ) ) { $rules = "\nRequire all denied\n\n\nDeny from all\n\n"; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents file_put_contents( $htaccess, $rules ); } $web_config = wp_normalize_path( $dir . '/web.config' ); if ( ! is_file( $web_config ) ) { $xml = "\n\n \n \n \n \n \n \n\n"; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents file_put_contents( $web_config, $xml ); } } /** * Delete a directory tree that is strictly inside the given root. * * @param string $dir Absolute directory to remove. * @param string $root Absolute ancestor that must contain $dir. * @return void */ public static function delete_tree( $dir, $root ) { $dir_real = realpath( $dir ); $root_real = realpath( $root ); if ( false === $dir_real || false === $root_real ) { return; } $dir_real = self::normalize( $dir_real ); $root_real = self::normalize( $root_real ); if ( $dir_real === $root_real || ! self::is_inside( $dir_real, $root_real ) ) { return; } self::delete_tree_normalized( $dir_real, trailingslashit( $root_real ) ); } /** * @param string $dir Normalized real directory. * @param string $root_slash Normalized real root with a trailing slash. * @return void */ private static function delete_tree_normalized( $dir, $root_slash ) { if ( ! self::is_inside( $dir, rtrim( $root_slash, '/' ) ) ) { return; } $items = scandir( $dir ); if ( ! is_array( $items ) ) { return; } foreach ( $items as $item ) { if ( '.' === $item || '..' === $item ) { continue; } $path = $dir . '/' . $item; if ( is_link( $path ) ) { continue; } if ( is_dir( $path ) ) { self::delete_tree_normalized( self::normalize( $path ), $root_slash ); continue; } if ( is_file( $path ) ) { wp_delete_file( $path ); } } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- Directory was constrained to a plugin folder inside uploads. rmdir( $dir ); } }