mirror of
https://github.com/WenPai-org/bulk-plugin-installer.git
synced 2025-08-04 20:05:15 +08:00
升级至 1.1.8 稳定版本,添加语言包
This commit is contained in:
parent
4b16ef8556
commit
5e805c2237
10 changed files with 1283 additions and 84 deletions
|
@ -1,36 +1,38 @@
|
|||
<?php
|
||||
class BPI_Installer {
|
||||
private $wp_filesystem;
|
||||
|
||||
private $error_messages = [
|
||||
// Plugin error codes
|
||||
1001 => 'Invalid plugin slug format. Please use only lowercase letters, numbers and hyphens.',
|
||||
1004 => 'Invalid URL format. Please provide a complete URL including http:// or https://',
|
||||
1005 => 'The domain is not in the trusted list. Please add it in settings or use another source.',
|
||||
1007 => 'Only ZIP files are allowed for plugin uploads.',
|
||||
1009 => 'This appears to be a theme ZIP file. Please use the Themes tab for installation.',
|
||||
1010 => 'The uploaded file is not a valid WordPress plugin ZIP.',
|
||||
|
||||
// Theme error codes
|
||||
2001 => 'Invalid theme slug format. Please use only lowercase letters, numbers and hyphens.',
|
||||
2003 => 'Only ZIP files are allowed for theme uploads.',
|
||||
2005 => 'This appears to be a plugin ZIP file. Please use the Plugins tab for installation.',
|
||||
2006 => 'The uploaded file is not a valid WordPress theme ZIP.',
|
||||
2009 => 'Invalid URL format for theme. Please provide a complete URL including http:// or https://',
|
||||
2010 => 'The domain is not in the trusted list for themes. Please add it in settings or use another source.'
|
||||
];
|
||||
private $error_messages = []; // Initialize as empty array
|
||||
|
||||
public function __construct() {
|
||||
global $wp_filesystem;
|
||||
if (empty($wp_filesystem)) {
|
||||
require_once ABSPATH . '/wp-admin/includes/file.php';
|
||||
if (!WP_Filesystem()) {
|
||||
throw new Exception('Unable to initialize filesystem. Please check server permissions.');
|
||||
throw new Exception(__('Unable to initialize filesystem. Please check server permissions.', 'bulk-plugin-installer'));
|
||||
}
|
||||
}
|
||||
$this->wp_filesystem = $wp_filesystem;
|
||||
|
||||
// Initialize error messages with translations in the constructor
|
||||
$this->error_messages = [
|
||||
// Plugin error codes
|
||||
1001 => __('Invalid plugin slug format. Please use only lowercase letters, numbers and hyphens.', 'bulk-plugin-installer'),
|
||||
1004 => __('Invalid URL format. Please provide a complete URL including http:// or https://', 'bulk-plugin-installer'),
|
||||
1005 => __('The domain is not in the trusted list. Please add it in settings or use another source.', 'bulk-plugin-installer'),
|
||||
1007 => __('Only ZIP files are allowed for plugin uploads.', 'bulk-plugin-installer'),
|
||||
1009 => __('This appears to be a theme ZIP file. Please use the Themes tab for installation.', 'bulk-plugin-installer'),
|
||||
1010 => __('The uploaded file is not a valid WordPress plugin ZIP.', 'bulk-plugin-installer'),
|
||||
// Theme error codes
|
||||
2001 => __('Invalid theme slug format. Please use only lowercase letters, numbers and hyphens.', 'bulk-plugin-installer'),
|
||||
2003 => __('Only ZIP files are allowed for theme uploads.', 'bulk-plugin-installer'),
|
||||
2005 => __('This appears to be a plugin ZIP file. Please use the Plugins tab for installation.', 'bulk-plugin-installer'),
|
||||
2006 => __('The uploaded file is not a valid WordPress theme ZIP.', 'bulk-plugin-installer'),
|
||||
2009 => __('Invalid URL format for theme. Please provide a complete URL including http:// or https://', 'bulk-plugin-installer'),
|
||||
2010 => __('The domain is not in the trusted list for themes. Please add it in settings or use another source.', 'bulk-plugin-installer')
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function get_error_message($code, $default_message) {
|
||||
return __($this->error_messages[$code] ?? $default_message, 'bulk-plugin-installer');
|
||||
}
|
||||
|
@ -70,11 +72,11 @@ class BPI_Installer {
|
|||
public function bpi_install_plugins($items, $type) {
|
||||
$valid_types = ['repository', 'wenpai', 'url', 'upload'];
|
||||
if (!in_array($type, $valid_types)) {
|
||||
return ['error' => 'Invalid installation type'];
|
||||
return ['error' => __('Invalid installation type', 'bulk-plugin-installer')];
|
||||
}
|
||||
|
||||
if (empty($items) || !is_array($items)) {
|
||||
return ['error' => 'No items provided'];
|
||||
return ['error' => __('No items provided', 'bulk-plugin-installer')];
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
|
||||
|
@ -92,7 +94,7 @@ class BPI_Installer {
|
|||
$item = sanitize_text_field($item);
|
||||
if (!preg_match('/^[a-z0-9-]+$/', $item)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(1001, 'Invalid plugin slug'),
|
||||
$this->get_error_message(1001, 'Invalid plugin slug'),
|
||||
1001
|
||||
);
|
||||
}
|
||||
|
@ -105,7 +107,7 @@ class BPI_Installer {
|
|||
if ($plugin_key && array_key_exists($plugin_key, $installed_plugins)) {
|
||||
$results[$item] = [
|
||||
'success' => true,
|
||||
'message' => 'Plugin already installed, skipped',
|
||||
'message' => __('Plugin already installed, skipped', 'bulk-plugin-installer'),
|
||||
'skipped' => true
|
||||
];
|
||||
continue;
|
||||
|
@ -120,7 +122,7 @@ class BPI_Installer {
|
|||
]);
|
||||
if (is_wp_error($api)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(1002, 'Failed to fetch plugin info: ' . $api->get_error_message()),
|
||||
$this->get_error_message(1002, 'Failed to fetch plugin info: ' . $api->get_error_message()),
|
||||
1002
|
||||
);
|
||||
}
|
||||
|
@ -132,13 +134,13 @@ class BPI_Installer {
|
|||
if (is_wp_error($response)) {
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
throw new Exception(
|
||||
$this->get_error_message(1003, "Download failed with status code $code: " . $response->get_error_message()),
|
||||
$this->get_error_message(1003, "Download failed with status code $code: " . $response->get_error_message()),
|
||||
1003
|
||||
);
|
||||
}
|
||||
$plugin_data = json_decode(wp_remote_retrieve_body($response), true);
|
||||
$download_link = $plugin_data && !empty($plugin_data['download_link'])
|
||||
? $plugin_data['download_link']
|
||||
$download_link = $plugin_data && !empty($plugin_data['download_link'])
|
||||
? $plugin_data['download_link']
|
||||
: "https://downloads.wenpai.net/plugin/{$item}.latest-stable.zip";
|
||||
break;
|
||||
|
||||
|
@ -146,13 +148,13 @@ class BPI_Installer {
|
|||
$item = sanitize_text_field($item);
|
||||
if (!filter_var($item, FILTER_VALIDATE_URL)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(1004, 'Invalid URL format'),
|
||||
$this->get_error_message(1004, 'Invalid URL format'),
|
||||
1004
|
||||
);
|
||||
}
|
||||
if (!bpi_is_domain_allowed($item)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(1005, 'Untrusted domain'),
|
||||
$this->get_error_message(1005, 'Untrusted domain'),
|
||||
1005
|
||||
);
|
||||
}
|
||||
|
@ -163,14 +165,14 @@ class BPI_Installer {
|
|||
$file = $item;
|
||||
if ($file['error'] !== UPLOAD_ERR_OK) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(1006, 'File upload error: ' . $file['error']),
|
||||
$this->get_error_message(1006, 'File upload error: ' . $file['error']),
|
||||
1006
|
||||
);
|
||||
}
|
||||
$file_name = sanitize_file_name($file['name']);
|
||||
if (pathinfo($file_name, PATHINFO_EXTENSION) !== 'zip') {
|
||||
throw new Exception(
|
||||
$this->get_error_message(1007, 'Only ZIP files are allowed'),
|
||||
$this->get_error_message(1007, 'Only ZIP files are allowed'),
|
||||
1007
|
||||
);
|
||||
}
|
||||
|
@ -180,7 +182,7 @@ class BPI_Installer {
|
|||
|
||||
if (!move_uploaded_file($temp_file, $dest_path)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(1008, 'Failed to move uploaded file. Check server permissions.'),
|
||||
$this->get_error_message(1008, 'Failed to move uploaded file. Check server permissions.'),
|
||||
1008
|
||||
);
|
||||
}
|
||||
|
@ -189,13 +191,13 @@ class BPI_Installer {
|
|||
if ($this->is_theme_zip($dest_path)) {
|
||||
unlink($dest_path);
|
||||
throw new Exception(
|
||||
$this->get_error_message(1009, 'Theme ZIP detected'),
|
||||
$this->get_error_message(1009, 'Theme ZIP detected'),
|
||||
1009
|
||||
);
|
||||
}
|
||||
unlink($dest_path);
|
||||
throw new Exception(
|
||||
$this->get_error_message(1010, 'Invalid plugin ZIP'),
|
||||
$this->get_error_message(1010, 'Invalid plugin ZIP'),
|
||||
1010
|
||||
);
|
||||
}
|
||||
|
@ -213,12 +215,12 @@ class BPI_Installer {
|
|||
if (is_wp_error($result)) {
|
||||
$error_message = $result->get_error_message();
|
||||
throw new Exception(
|
||||
$this->get_error_message(1011, $error_message ?: 'Unknown installation error'),
|
||||
$this->get_error_message(1011, $error_message ?: 'Unknown installation error'),
|
||||
1011
|
||||
);
|
||||
} elseif ($result !== true) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(1012, 'Installation failed unexpectedly'),
|
||||
$this->get_error_message(1012, 'Installation failed unexpectedly'),
|
||||
1012
|
||||
);
|
||||
}
|
||||
|
@ -244,11 +246,11 @@ class BPI_Installer {
|
|||
public function bpi_install_themes($items, $type) {
|
||||
$valid_types = ['repository', 'wenpai', 'url', 'upload'];
|
||||
if (!in_array($type, $valid_types)) {
|
||||
return ['error' => 'Invalid installation type'];
|
||||
return ['error' => __('Invalid installation type', 'bulk-plugin-installer')];
|
||||
}
|
||||
|
||||
if (empty($items) || !is_array($items)) {
|
||||
return ['error' => 'No items provided'];
|
||||
return ['error' => __('No items provided', 'bulk-plugin-installer')];
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/theme-install.php';
|
||||
|
@ -266,7 +268,7 @@ class BPI_Installer {
|
|||
$item = sanitize_text_field($item);
|
||||
if (!preg_match('/^[a-z0-9-]+$/', $item)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(2001, 'Invalid theme slug'),
|
||||
$this->get_error_message(2001, 'Invalid theme slug'),
|
||||
2001
|
||||
);
|
||||
}
|
||||
|
@ -275,14 +277,14 @@ class BPI_Installer {
|
|||
$file = $item;
|
||||
if ($file['error'] !== UPLOAD_ERR_OK) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(2002, 'File upload error: ' . $file['error']),
|
||||
$this->get_error_message(2002, 'File upload error: ' . $file['error']),
|
||||
2002
|
||||
);
|
||||
}
|
||||
$file_name = sanitize_file_name($file['name']);
|
||||
if (pathinfo($file_name, PATHINFO_EXTENSION) !== 'zip') {
|
||||
throw new Exception(
|
||||
$this->get_error_message(2003, 'Only ZIP files are allowed'),
|
||||
$this->get_error_message(2003, 'Only ZIP files are allowed'),
|
||||
2003
|
||||
);
|
||||
}
|
||||
|
@ -292,7 +294,7 @@ class BPI_Installer {
|
|||
|
||||
if (!move_uploaded_file($temp_file, $dest_path)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(2004, 'Failed to move uploaded file. Check server permissions.'),
|
||||
$this->get_error_message(2004, 'Failed to move uploaded file. Check server permissions.'),
|
||||
2004
|
||||
);
|
||||
}
|
||||
|
@ -329,13 +331,13 @@ class BPI_Installer {
|
|||
if ($this->is_plugin_zip($dest_path)) {
|
||||
unlink($dest_path);
|
||||
throw new Exception(
|
||||
$this->get_error_message(2005, 'Plugin ZIP detected'),
|
||||
$this->get_error_message(2005, 'Plugin ZIP detected'),
|
||||
2005
|
||||
);
|
||||
}
|
||||
unlink($dest_path);
|
||||
throw new Exception(
|
||||
$this->get_error_message(2006, 'Invalid theme ZIP'),
|
||||
$this->get_error_message(2006, 'Invalid theme ZIP'),
|
||||
2006
|
||||
);
|
||||
}
|
||||
|
@ -362,7 +364,7 @@ class BPI_Installer {
|
|||
]);
|
||||
if (is_wp_error($api)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(2007, 'Failed to fetch theme info: ' . $api->get_error_message()),
|
||||
$this->get_error_message(2007, 'Failed to fetch theme info: ' . $api->get_error_message()),
|
||||
2007
|
||||
);
|
||||
}
|
||||
|
@ -374,13 +376,13 @@ class BPI_Installer {
|
|||
if (is_wp_error($response)) {
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
throw new Exception(
|
||||
$this->get_error_message(2008, "Download failed with status code $code: " . $response->get_error_message()),
|
||||
$this->get_error_message(2008, "Download failed with status code $code: " . $response->get_error_message()),
|
||||
2008
|
||||
);
|
||||
}
|
||||
$theme_data = json_decode(wp_remote_retrieve_body($response), true);
|
||||
$download_link = $theme_data && !empty($theme_data['download_link'])
|
||||
? $theme_data['download_link']
|
||||
$download_link = $theme_data && !empty($theme_data['download_link'])
|
||||
? $theme_data['download_link']
|
||||
: "https://downloads.wenpai.net/theme/{$item}.latest-stable.zip";
|
||||
break;
|
||||
|
||||
|
@ -388,13 +390,13 @@ class BPI_Installer {
|
|||
$item = sanitize_text_field($item);
|
||||
if (!filter_var($item, FILTER_VALIDATE_URL)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(2009, 'Invalid URL format'),
|
||||
$this->get_error_message(2009, 'Invalid URL format'),
|
||||
2009
|
||||
);
|
||||
}
|
||||
if (!bpi_is_domain_allowed($item)) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(2010, 'Untrusted domain'),
|
||||
$this->get_error_message(2010, 'Untrusted domain'),
|
||||
2010
|
||||
);
|
||||
}
|
||||
|
@ -414,12 +416,12 @@ class BPI_Installer {
|
|||
if (is_wp_error($result)) {
|
||||
$error_message = $result->get_error_message();
|
||||
throw new Exception(
|
||||
$this->get_error_message(2011, $error_message ?: 'Unknown installation error'),
|
||||
$this->get_error_message(2011, $error_message ?: 'Unknown installation error'),
|
||||
2011
|
||||
);
|
||||
} elseif ($result !== true) {
|
||||
throw new Exception(
|
||||
$this->get_error_message(2012, 'Installation failed unexpectedly'),
|
||||
$this->get_error_message(2012, 'Installation failed unexpectedly'),
|
||||
2012
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue