'switch', 'id' => 'plugin-download-enabled', 'name' => 'adminease[plugins][plugin_download_enabled]', 'value' => ! empty( $settings['plugin_download_enabled'] ), 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Enable Plugin Download', 'adminease' ), 'description' => __( 'Adds a Download link to each plugin row in the Plugins list. Clicking it downloads a .zip archive of that plugin\'s directory. Requires the PHP ZipArchive extension. Only visible to users with the manage_options capability.', 'adminease' ), ]; return $fields; } /** * Appends a "Download" action link to a plugin row in the Plugins list. * * @param array $actions Existing plugin action links. * @param string $plugin_file Plugin basename (e.g. 'akismet/akismet.php'). * * @return array Modified action links. */ public function add_download_link( array $actions, string $plugin_file ): array { if ( ! current_user_can( 'manage_options' ) || ! Utils::is_network_admin_user() ) { return $actions; } $nonce = wp_create_nonce( 'adminease_download_plugin_' . $plugin_file ); $url = add_query_arg( [ 'action' => 'adminease_download_plugin', 'plugin' => rawurlencode( $plugin_file ), '_wpnonce' => $nonce, ], admin_url( 'admin-post.php' ) ); $actions['adminease_download'] = '' . esc_html__( 'Download', 'adminease' ) . ''; return $actions; } /** * Handles the plugin download request via admin-post.php. * * Verifies the nonce, checks capability, builds a zip of the plugin * directory, and sends it as a file download response. * * @return void */ public function handle_download(): void { $plugin_file = isset( $_GET['plugin'] ) ? sanitize_text_field( wp_unslash( $_GET['plugin'] ) ) : ''; if ( empty( $plugin_file ) ) { wp_die( esc_html__( 'No plugin specified.', 'adminease' ) ); } $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'adminease_download_plugin_' . $plugin_file ) ) { wp_die( esc_html__( 'Security check failed. Refresh the page and try again.', 'adminease' ) ); } if ( ! current_user_can( 'manage_options' ) || ! Utils::is_network_admin_user() ) { wp_die( esc_html__( 'You do not have permission to download plugins.', 'adminease' ) ); } $settings = Plugin::get_settings( 'plugins' ); if ( empty( $settings['plugin_download_enabled'] ) ) { wp_die( esc_html__( 'Plugin Download feature is not enabled.', 'adminease' ) ); } if ( false !== strpos( $plugin_file, '..' ) || false !== strpos( $plugin_file, '/' . '/' ) ) { wp_die( esc_html__( 'Invalid plugin path.', 'adminease' ) ); } $plugin_dir = WP_PLUGIN_DIR . '/' . dirname( $plugin_file ); if ( ! is_dir( $plugin_dir ) ) { wp_die( esc_html__( 'Plugin directory not found.', 'adminease' ) ); } if ( ! class_exists( 'ZipArchive' ) ) { wp_die( esc_html__( 'The PHP ZipArchive extension is not available on this server. Please contact your hosting provider to enable it.', 'adminease' ) ); } $plugin_slug = basename( dirname( $plugin_file ) ); $zip_name = $plugin_slug . '.zip'; $zip_path = trailingslashit( sys_get_temp_dir() ) . $zip_name; $zip = new ZipArchive(); if ( true !== $zip->open( $zip_path, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { wp_die( esc_html__( 'Failed to create the zip archive. Please try again.', 'adminease' ) ); } $this->add_directory_to_zip( $zip, $plugin_dir, $plugin_slug ); $zip->close(); if ( ! file_exists( $zip_path ) ) { wp_die( esc_html__( 'Zip archive could not be created.', 'adminease' ) ); } FileHandler::get_instance(); global $wp_filesystem; if ( ! isset( $wp_filesystem ) ) { wp_die( esc_html__( 'Could not initialize filesystem.', 'adminease' ) ); } header( 'Content-Type: application/zip' ); header( 'Content-Disposition: attachment; filename="' . $zip_name . '"' ); header( 'Content-Length: ' . $wp_filesystem->size( $zip_path ) ); header( 'Cache-Control: no-cache, no-store, must-revalidate' ); header( 'Pragma: no-cache' ); header( 'Expires: 0' ); if ( ob_get_level() ) { ob_end_clean(); } $zip_contents = $wp_filesystem->get_contents( $zip_path ); $wp_filesystem->delete( $zip_path ); if ( false === $zip_contents ) { wp_die( esc_html__( 'Failed to read the zip archive.', 'adminease' ) ); } echo $zip_contents; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Binary zip file data exit; } /** * Recursively adds all files in a directory to a ZipArchive. * * @param ZipArchive $zip The open ZipArchive instance. * @param string $dir Absolute path to the directory to add. * @param string $prefix Zip-internal path prefix (the plugin slug). * * @return void */ private function add_directory_to_zip( ZipArchive $zip, string $dir, string $prefix ): void { $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS ), RecursiveIteratorIterator::LEAVES_ONLY ); foreach ( $iterator as $file ) { if ( $file->isDir() ) { continue; } $file_path = $file->getRealPath(); $relative_path = $prefix . '/' . substr( $file_path, strlen( $dir ) + 1 ); $relative_path = str_replace( '\\', '/', $relative_path ); $zip->addFile( $file_path, $relative_path ); } } }