wpbridge/includes/Commercial/Vendors/BridgeApiVendor.php
wenpai e9d28817fe style: phpcbf 自动格式化 — 全量 WPCS 3.x 规范对齐
74 文件 14,082 处自动修复:空格→Tab 缩进、括号间距、
函数声明空格、前置自增、尾逗号等纯格式化改动。
零逻辑变更,php -l + token 级对比验证通过。

新增 phpcs.xml.dist / phpstan.neon.dist 项目配置。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 00:43:46 +08:00

175 lines
4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Bridge API 供应商
*
* 连接另一个 WPBridge 站点的 Bridge APIhub-spoke 架构)
*
* @package WPBridge
* @since 1.1.0
*/
declare(strict_types=1);
namespace WPBridge\Commercial\Vendors;
use WPBridge\Core\Logger;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class BridgeApiVendor extends AbstractVendor {
protected string $vendor_id;
protected string $vendor_name;
public function __construct( string $vendor_id, string $vendor_name, array $config = [] ) {
$this->vendor_id = $vendor_id;
$this->vendor_name = $vendor_name;
parent::__construct( $config );
}
protected function get_default_config(): array {
return array_merge(
parent::get_default_config(),
[
'api_key' => '',
]
);
}
public function get_id(): string {
return $this->vendor_id;
}
public function get_info(): array {
return [
'id' => $this->vendor_id,
'name' => $this->vendor_name,
'url' => $this->config['api_url'],
'api_type' => 'bridge_api',
'requires_key' => true,
];
}
protected function get_request_headers(): array {
$headers = parent::get_request_headers();
if ( ! empty( $this->config['api_key'] ) ) {
$headers['X-WPBridge-API-Key'] = $this->config['api_key'];
}
return $headers;
}
public function verify_credentials(): bool {
$cache_key = 'credentials_valid';
$cached = $this->get_cache( $cache_key );
if ( $cached !== null ) {
return (bool) $cached;
}
$response = $this->api_request( 'wp-json/bridge/v1/sources' );
$valid = $response !== null;
// 失败短缓存,避免临时网络问题长期阻塞
$this->set_cache( $cache_key, $valid, $valid ? 3600 : 300 );
return $valid;
}
public function get_plugins( int $page = 1, int $limit = 100 ): array {
$cache_key = "plugins_page_{$page}_limit_{$limit}";
$cached = $this->get_cache( $cache_key );
if ( $cached !== null ) {
return $cached;
}
// 获取所有源,筛选 plugin 类型
$sources = $this->api_request( 'wp-json/bridge/v1/sources' );
if ( $sources === null ) {
return [
'plugins' => [],
'total' => 0,
'pages' => 0,
];
}
$plugins = [];
foreach ( $sources as $source ) {
$item_type = $source['item_type'] ?? $source['type'] ?? '';
if ( $item_type !== 'plugin' ) {
continue;
}
$slug = $source['slug'] ?? '';
if ( empty( $slug ) ) {
continue;
}
// 直接用 source 数据标准化,避免 N+1 HTTP 请求
// 详细信息在 get_plugin() 时按需懒加载
$plugins[] = $this->normalize_plugin( $source );
}
$result = [
'plugins' => $plugins,
'total' => count( $plugins ),
'pages' => 1,
];
// 空结果短缓存 5 分钟
$this->set_cache( $cache_key, $result, empty( $plugins ) ? 300 : 0 );
return $result;
}
public function get_plugin( string $slug ): ?array {
$cache_key = 'plugin_' . $slug;
$cached = $this->get_cache( $cache_key );
if ( $cached !== null ) {
return $cached;
}
$plugin_info = $this->api_request( 'wp-json/bridge/v1/plugins/' . $slug . '/info' );
if ( $plugin_info === null ) {
return null;
}
$normalized = $this->normalize_plugin( $plugin_info );
$this->set_cache( $cache_key, $normalized );
return $normalized;
}
public function check_update( string $slug, string $current_version ): ?array {
$plugin = $this->get_plugin( $slug );
if ( $plugin === null ) {
return null;
}
$latest_version = $plugin['version'] ?? '';
if ( empty( $latest_version ) ) {
return null;
}
if ( version_compare( $latest_version, $current_version, '<=' ) ) {
return null;
}
return [
'version' => $latest_version,
'download_url' => $plugin['download_url'] ?? '',
'changelog' => '',
'tested' => $plugin['tested'] ?? '',
'requires' => $plugin['requires'] ?? '',
'requires_php' => $plugin['requires_php'] ?? '',
];
}
public function get_download_url( string $slug, string $version = '' ): ?string {
$plugin = $this->get_plugin( $slug );
if ( $plugin === null ) {
return null;
}
return $plugin['download_url'] ?? null;
}
}