PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Staging / Traits / WithStagingRequirementLogs.php
wp-staging / Staging / Traits Last commit date
StagingDatabaseDtoTrait.php 1 year ago StagingNetworkDtoTrait.php 2 months ago StagingOperationDtoTrait.php 2 weeks ago StagingSiteGetterTrait.php 2 months ago WithAdvanceStagingOptions.php 5 months ago WithDataAdjustmentTasks.php 9 months ago WithStagingDatabase.php 2 months ago WithStagingEnginePreference.php 2 weeks ago WithStagingRequirementLogs.php 2 weeks ago WithStagingSiteDto.php 1 year ago
WithStagingRequirementLogs.php
288 lines
1 <?php
2
3 namespace WPStaging\Staging\Traits;
4
5 use WPStaging\Core\Utils\Logger;
6 use WPStaging\Pro\License\Licensing;
7 use WPStaging\Staging\Service\StagingSetup;
8
9 /**
10 * Adds common staging requirement log sections.
11 */
12 trait WithStagingRequirementLogs
13 {
14 /**
15 * @return void
16 */
17 protected function writeAdvancedSettingsToLogs()
18 {
19 $settingsToLog = $this->getAdvancedSettingsToLog();
20 if (empty($settingsToLog)) {
21 return;
22 }
23
24 $this->logger->add('Advanced Settings', Logger::TYPE_INFO);
25
26 foreach ($settingsToLog as $setting) {
27 $this->writeAdvancedSettingToLogs($setting);
28 }
29 }
30
31 /**
32 * @return array<int, array<string, mixed>>
33 */
34 private function getAdvancedSettingsToLog(): array
35 {
36 if (!$this->isProRequirementLog() || !method_exists($this->jobDataDto, 'getJobType')) {
37 return [];
38 }
39
40 switch ($this->jobDataDto->getJobType()) {
41 case StagingSetup::JOB_NEW_STAGING_SITE:
42 return $this->getCreateAdvancedSettingsToLog();
43 case StagingSetup::JOB_UPDATE:
44 return $this->getUpdateAdvancedSettingsToLog();
45 case StagingSetup::JOB_PUSH:
46 return $this->getPushAdvancedSettingsToLog();
47 }
48
49 return [];
50 }
51
52 /**
53 * @return array<int, array<string, mixed>>
54 */
55 private function getCreateAdvancedSettingsToLog(): array
56 {
57 $settingsToLog = [
58 $this->booleanSetting('New Admin Account', 'getUseNewAdminAccount'),
59 $this->stringSetting('Email', 'getAdminEmail'),
60 $this->sensitiveStringSetting('Password', 'getAdminPassword'),
61 $this->stringSetting('Database Server', 'getDatabaseServer'),
62 $this->stringSetting('Database User', 'getDatabaseUser'),
63 $this->sensitiveStringSetting('Database Password', 'getDatabasePassword'),
64 $this->stringSetting('Database', 'getDatabaseName'),
65 $this->stringSetting('Database Prefix', 'getDatabasePrefix'),
66 $this->booleanSetting('Database SSL', 'getDatabaseSsl'),
67 $this->stringSetting('Clone Directory', 'getCustomPath'),
68 $this->stringSetting('Clone Host', 'getCustomUrl'),
69 $this->booleanSetting('Symlink Uploads Folder', 'getIsUploadsSymlinked'),
70 $this->booleanSetting('WP CRON Enabled', 'getIsCronEnabled'),
71 $this->booleanSetting('Emails Sending Allowed', 'getIsEmailsAllowed'),
72 $this->booleanSetting('Email Reminder Enabled', 'getIsEmailsReminderEnabled'),
73 $this->booleanSetting('Auto Update Plugins Enabled', 'getIsAutoUpdatePlugins'),
74 ];
75
76 if ($this->isWooSchedulerSettingRendered()) {
77 $settingsToLog[] = $this->booleanSetting(
78 'WooCommerce Scheduler Enabled',
79 'getIsWooSchedulerEnabled'
80 );
81 }
82
83 return $settingsToLog;
84 }
85
86 /**
87 * @return array<int, array<string, mixed>>
88 */
89 private function getUpdateAdvancedSettingsToLog(): array
90 {
91 return [
92 $this->booleanSetting('Emails Sending Allowed', 'getIsEmailsAllowed'),
93 $this->booleanSetting('Email Reminder Enabled', 'getIsEmailsReminderEnabled'),
94 $this->booleanSetting('Auto Update Plugins Enabled', 'getIsAutoUpdatePlugins'),
95 $this->booleanSetting('Clean Plugins/Themes', 'getIsCleanPluginsThemes'),
96 $this->booleanSetting('Clean Uploads', 'getIsCleanUploads'),
97 ];
98 }
99
100 /**
101 * @return array<int, array<string, mixed>>
102 */
103 private function getPushAdvancedSettingsToLog(): array
104 {
105 return [
106 $this->booleanSetting('Clean Plugins/Themes', 'getIsCleanPluginsThemes'),
107 $this->booleanSetting('Clean Uploads', 'getIsCleanUploads'),
108 $this->booleanSetting('Backup Uploads', 'getIsBackupUploads'),
109 $this->booleanSetting('Create Database Backup', 'getIsCreateDatabaseBackup'),
110 ];
111 }
112
113 /**
114 * @param array<string, mixed> $setting
115 * @return void
116 */
117 private function writeAdvancedSettingToLogs(array $setting)
118 {
119 switch ($setting['type']) {
120 case 'boolean':
121 $this->writeBooleanSettingToLogs($setting['label'], $setting['methods']);
122 break;
123 case 'sensitive':
124 $this->writeSensitiveStringSettingToLogs($setting['label'], $setting['methods']);
125 break;
126 default:
127 $this->writeStringSettingToLogs($setting['label'], $setting['methods']);
128 break;
129 }
130 }
131
132 /**
133 * @param string $label
134 * @param string|string[] $methods
135 * @return array<string, mixed>
136 */
137 private function stringSetting(string $label, $methods): array
138 {
139 return [
140 'type' => 'string',
141 'label' => $label,
142 'methods' => $methods,
143 ];
144 }
145
146 /**
147 * @param string $label
148 * @param string|string[] $methods
149 * @return array<string, mixed>
150 */
151 private function sensitiveStringSetting(string $label, $methods): array
152 {
153 return [
154 'type' => 'sensitive',
155 'label' => $label,
156 'methods' => $methods,
157 ];
158 }
159
160 /**
161 * @param string $label
162 * @param string|string[] $methods
163 * @return array<string, mixed>
164 */
165 private function booleanSetting(string $label, $methods): array
166 {
167 return [
168 'type' => 'boolean',
169 'label' => $label,
170 'methods' => $methods,
171 ];
172 }
173
174 /**
175 * @return bool
176 */
177 private function isProRequirementLog(): bool
178 {
179 return strpos(static::class, '\\Pro\\') !== false;
180 }
181
182 /**
183 * @return bool
184 */
185 private function isWooSchedulerSettingRendered(): bool
186 {
187 if (!class_exists(Licensing::class)) {
188 return false;
189 }
190
191 $licenseData = get_option('wpstg_license_status');
192 $licensePriceId = !empty($licenseData->price_id) ? $licenseData->price_id : '';
193 $acceptablePlans = [
194 Licensing::AGENCY_LICENSE_PLAN_KEY,
195 Licensing::DEVELOPER_LICENSE_PLAN_KEY,
196 Licensing::DEVELOPER_LEGACY_LICENSE_PLAN_KEY,
197 Licensing::DEVELOPER_30_SITES_LICENSE_PLAN_KEY,
198 Licensing::DEVELOPER_NON_RECURRING_LICENSE_PLAN_KEY,
199 Licensing::AGENCY_NON_RECURRING_LICENSE_PLAN_KEY,
200 Licensing::DEVELOPER_UNLIMITED_SITES_LICENSE_PLAN_KEY,
201 ];
202
203 return in_array($licensePriceId, $acceptablePlans, true);
204 }
205
206 /**
207 * @param string $label
208 * @param string|string[] $methods
209 * @return void
210 */
211 private function writeStringSettingToLogs(string $label, $methods)
212 {
213 $value = $this->getLogSettingValue((array)$methods);
214 if ($value === null) {
215 return;
216 }
217
218 $this->logger->add(sprintf('- %s : %s', $label, $value !== '' ? $value : 'Not Set'), Logger::TYPE_INFO_SUB);
219 }
220
221 /**
222 * @param string $label
223 * @param string|string[] $methods
224 * @return void
225 */
226 private function writeSensitiveStringSettingToLogs(string $label, $methods)
227 {
228 $value = $this->getLogSettingValue((array)$methods);
229 if ($value === null) {
230 return;
231 }
232
233 $value = $value !== '' ? '**************' : 'Not Set';
234
235 $this->logger->add(sprintf('- %s : %s', $label, $value), Logger::TYPE_INFO_SUB);
236 }
237
238 /**
239 * @param string $label
240 * @param string|string[] $methods
241 * @return void
242 */
243 private function writeBooleanSettingToLogs(string $label, $methods)
244 {
245 $value = $this->getLogSettingValue((array)$methods);
246 if ($value === null) {
247 return;
248 }
249
250 $value = $value ? 'True' : 'False';
251
252 $this->logger->add(sprintf('- %s : %s', $label, $value), Logger::TYPE_INFO_SUB);
253 }
254
255 /**
256 * @param string[] $methods
257 * @return mixed|null
258 */
259 private function getLogSettingValue(array $methods)
260 {
261 $fallbackValue = null;
262 foreach ($this->getLogSettingSources() as $source) {
263 foreach ($methods as $method) {
264 if (!method_exists($source, $method)) {
265 continue;
266 }
267
268 $value = $source->{$method}();
269 if ($value !== '' && $value !== null) {
270 return $value;
271 }
272
273 $fallbackValue = $value;
274 }
275 }
276
277 return $fallbackValue;
278 }
279
280 /**
281 * @return object[]
282 */
283 private function getLogSettingSources(): array
284 {
285 return [$this->jobDataDto];
286 }
287 }
288