PluginProbe
WP-Stateless – Google Cloud Storage / trunk
WP-Stateless – Google Cloud Storage vtrunk
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / status / class-info-google_cloud.php

class-info-google_cloud.php in WP-Stateless – Google Cloud Storage trunk, at lib/classes/status/class-info-google_cloud.php

136 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * System Info (Google Cloud section) class
4 * https://cloud.google.com/storage/docs/json_api/v1/buckets#resource-representations
5 *
6 * @since 4.1.0
7 */
8
9 namespace wpCloud\StatelessMedia\Status;
10
11 use wpCloud\StatelessMedia\Singleton;
12 use wpCloud\StatelessMedia\Helper;
13
14 class GoogleCloudInfo {
15 use Singleton;
16
17 protected function __construct() {
18 $this->_init_hooks();
19 }
20
21 private function _init_hooks() {
22 add_filter('wp_stateless_status_info_values_google_cloud', [$this, 'get_bucket_info'], 10);
23
24 add_filter('wp_stateless_status_info_values_google_cloud', [$this, 'format_values'], 99);
25
26 add_filter('wp_stateless_status_info_values_google_cloud_public_access', [$this, 'get_public_access_value'], 10);
27 add_filter('wp_stateless_status_info_values_google_cloud_access_control', [$this, 'get_access_control'], 10);
28 add_filter('wp_stateless_status_info_values_google_cloud_versioning', [$this, 'get_versioning'], 10);
29 add_filter('wp_stateless_status_info_values_google_cloud_soft_delete', [$this, 'get_soft_delete'], 10);
30 }
31
32 /**
33 * Format values to human-readable
34 */
35 public function format_values($values) {
36 foreach ($values as $key => $value) {
37 $values[$key]['value'] = apply_filters("wp_stateless_status_info_values_google_cloud_$key", $values[$key]['value']);
38 }
39
40 return $values;
41 }
42
43 /**
44 * Format 'public_access' value
45 */
46 public function get_public_access_value($value) {
47 switch ($value) {
48 case 'enforced':
49 return __('Enforced', ud_get_stateless_media()->domain);
50 case 'inherited':
51 return __('Inherited', ud_get_stateless_media()->domain);
52 }
53
54 return $value;
55 }
56
57 /**
58 * Format 'versioning' value
59 */
60 public function get_versioning($value) {
61 return $value ? __('Enabled', ud_get_stateless_media()->domain) : __('Disabled', ud_get_stateless_media()->domain);
62 }
63
64 /**
65 * Get 'Access Control' value
66 */
67 public function get_access_control($value) {
68 return $value ? __('Uniform', ud_get_stateless_media()->domain) : __('Fine-grained', ud_get_stateless_media()->domain);
69 }
70
71 /**
72 * Get 'Soft Delete' value
73 */
74 public function get_soft_delete($value) {
75 return $value > 0 ? __('Enabled', ud_get_stateless_media()->domain) : __('Disabled', ud_get_stateless_media()->domain);
76 }
77
78 /**
79 * Get the values related to GCS bucket configuration
80 */
81 public function get_bucket_info($values) {
82 $rows = [];
83 $client = ud_get_stateless_media()->get_client();
84 $bucket_name = ud_get_stateless_media()->get('sm.bucket');
85
86 if ( empty($client) || empty($bucket_name) ) {
87 $rows = [
88 'gcs_error' => [
89 'label' => __('Error', ud_get_stateless_media()->domain),
90 'value' => __('Google Cloud info not accessible', ud_get_stateless_media()->domain),
91 ],
92 ];
93
94 return $values + $rows;
95 }
96
97 // Get bucket info
98 try {
99 $info = $client->service->buckets->get($bucket_name);
100
101 $rows = [
102 'storage_class' => [
103 'label' => __('Storage Class', ud_get_stateless_media()->domain),
104 'value' => $info->storageClass,
105 ],
106 'public_access' => [
107 'label' => __('Public Access Prevention', ud_get_stateless_media()->domain),
108 'value' => $info->iamConfiguration->publicAccessPrevention,
109 ],
110 'access_control' => [
111 'label' => __('Access Control', ud_get_stateless_media()->domain),
112 'value' => $info->iamConfiguration->uniformBucketLevelAccess->enabled,
113 ],
114 'versioning' => [
115 'label' => __('Versioning', ud_get_stateless_media()->domain),
116 'value' => isset($info->versioning) && isset($info->versioning->enabled) ? $info->versioning->enabled : false,
117 ],
118 'soft_delete' => [
119 'label' => __('Soft Delete', ud_get_stateless_media()->domain),
120 'value' => isset($info->softDeletePolicy) && isset($info->softDeletePolicy->retentionDurationSeconds) ? $info->softDeletePolicy->retentionDurationSeconds : 0,
121 ],
122 ];
123 } catch ( \Throwable $e ) {
124 $rows = [
125 'gcs_error' => [
126 'label' => __('Error', ud_get_stateless_media()->domain),
127 'value' => __('Google Cloud Storage Bucket info not available', ud_get_stateless_media()->domain),
128 ],
129 ];
130 }
131
132 return $values + $rows;
133 }
134
135 }
136