PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.4.3
Kubio AI Page Builder v2.4.3
2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / DemoSites / DemoSitesHelpers.php
kubio / lib / src / DemoSites Last commit date
DemoSites.php 2 years ago DemoSitesHelpers.php 1 year ago DemoSitesImportBlockMap.php 4 years ago DemoSitesImporter.php 1 year ago DemoSitesLogger.php 4 years ago DemoSitesRepository.php 2 years ago WXRExporter.php 4 years ago WXRImporter.php 2 years ago
DemoSitesHelpers.php
343 lines
1 <?php
2
3 namespace Kubio\DemoSites;
4
5 use IlluminateAgnostic\Arr\Support\Arr;
6
7 class DemoSitesHelpers {
8
9 const IMPORT_TRANSIENT = 'kubio_importer_data';
10 private static $import_start_time = 0;
11
12 /**
13 * Check if the AJAX call is valid.
14 */
15 public static function verifyAjaxCall() {
16 check_ajax_referer( 'kubio-ajax-demo-site-verification', 'nonce' );
17
18 // Check if user has the WP capability to import data.
19 if ( ! current_user_can( 'import' ) ) {
20 wp_die(
21 wp_kses_post(
22 sprintf(
23 /* translators: %1$s - opening div and paragraph HTML tags, %2$s - closing div and paragraph HTML tags. */
24 __( '%1$sYour user role isn\'t high enough. You don\'t have permission to import demo data.%2$s', 'kubio' ),
25 '<div class="notice notice-error"><p>',
26 '</p></div>'
27 )
28 )
29 );
30 }
31 }
32
33 /**
34 * Set the transient with the current importer data.
35 *
36 * @param array $data Data to be saved to the transient.
37 */
38 public static function setImportDataTransient( $data ) {
39 set_transient( DemoSitesHelpers::IMPORT_TRANSIENT, $data, 0.1 * HOUR_IN_SECONDS );
40 }
41
42 public static function setImportStartTime() {
43 self::$import_start_time = time();
44 }
45
46 /**
47 * Get log file path
48 *
49 * @return string, path to the log file
50 */
51 public static function getLogFile() {
52 $upload_dir = wp_upload_dir();
53 $upload_path = trailingslashit( $upload_dir['path'] );
54
55 $time = static::$import_start_time;
56 $log_path = "{$upload_path}kubio-demo-import/log_file_{$time}.txt";
57
58 self::registerFileAsMediaAttachment( $log_path );
59
60 return $log_path;
61 }
62
63 /**
64 * Register file as attachment to the Media page.
65 *
66 * @param string $file_path log file path.
67 *
68 * @return void
69 */
70 public static function registerFileAsMediaAttachment( $file_path ) {
71 // Check the type of file.
72 $log_mimes = array( 'txt' => 'text/plain' );
73 $filetype = wp_check_filetype( basename( $file_path ), $log_mimes );
74
75 $upload_dir = wp_upload_dir();
76 $upload_url = trailingslashit( $upload_dir['url'] );
77
78 // Prepare an array of post data for the attachment.
79 $attachment = array(
80 'guid' => $upload_url . basename( $file_path ),
81 'post_mime_type' => $filetype['type'],
82 'post_title' => esc_html__( 'Kubio Demo Import - ', 'kubio' ) . preg_replace( '/\.[^.]+$/', '', basename( $file_path ) ),
83 'post_content' => '',
84 'post_status' => 'inherit',
85 );
86
87 // Insert the file as attachment in Media page.
88 wp_insert_attachment( $attachment, $file_path );
89 }
90
91
92 public static function extractImporterFilesFromSource( $source ) {
93
94 if ( strpos( $source, 's3.us-west-2.amazonaws.com/static-assets.kubiobuilder.com' ) !== false ) {
95 $source = str_replace( 's3.us-west-2.amazonaws.com/static-assets.kubiobuilder.com', 'static-assets.kubiobuilder.com', $source );
96 }
97
98 if ( empty( $source ) ) {
99 return new \WP_Error(
100 'missing_url',
101 __( 'Missing URL for downloading a file!', 'kubio' )
102 );
103 }
104
105 if ( ! filter_var( $source, FILTER_VALIDATE_URL ) && file_exists( $source ) ) {
106 $content = file_get_contents( $source );
107 } else {
108 if ( ! filter_var( $source, FILTER_VALIDATE_URL ) ) {
109 return new \WP_Error(
110 'invalid_url',
111 __( 'Invalid URL for downloading a file!', 'kubio' )
112 );
113 }
114 // Get file content from the server.
115 $response = wp_remote_get(
116 $source,
117 array(
118 'timeout' => 30,
119 )
120 );
121
122 // Test if the get request was not successful.
123 if ( is_wp_error( $response ) || 200 !== $response['response']['code'] ) {
124 // Collect the right format of error data (array or WP_Error).
125 $response_error = static::getErrorMessageFromResponse( $response );
126
127 return new \WP_Error(
128 'download_error',
129 sprintf( /* translators: %1$s and %3$s - strong HTML tags, %2$s - file URL, %4$s - br HTML tag, %5$s - error code, %6$s - error message. */
130 __( 'An error occurred while fetching file from: %1$s%2$s%3$s!%4$sReason: %5$s - %6$s.', 'kubio' ),
131 '<strong>',
132 $source,
133 '</strong>',
134 '<br>',
135 $response_error['error_code'],
136 $response_error['error_message']
137 ) . '<br>'
138 );
139 }
140
141 $content = wp_remote_retrieve_body( $response );
142
143 }
144
145 $upload_dir = wp_upload_dir();
146 $upload_path = trailingslashit( $upload_dir['path'] );
147
148 //basename($url);
149 $time = static::$import_start_time;
150 $path = "{$upload_path}/kubio-demo-import/demo-site-{$time}.wxr";
151
152 $file_data = unserialize( $content );
153 $wxr_content = Arr::get( $file_data, 'content', '' );
154
155 unset( $file_data['content'] );
156 $import_options = $file_data;
157
158 $wxr_path = static::writeFile( $wxr_content, $path );
159
160 return array( $wxr_path, $import_options );
161
162 }
163
164 public static function useUploadedKDSFile( $kds_file ) {
165
166 $upload_dir = wp_upload_dir();
167 $upload_path = trailingslashit( $upload_dir['path'] );
168
169 //basename($url);
170 $time = static::$import_start_time;
171 $path = "{$upload_path}/kubio-demo-import/demo-site-{$time}.wxr";
172
173 $content = file_get_contents( $kds_file );
174 $file_data = unserialize( $content );
175 $wxr_content = Arr::get( $file_data, 'content', '' );
176
177 unset( $file_data['content'] );
178 $import_options = $file_data;
179
180 $wxr_path = static::writeFile( $wxr_content, $path );
181
182 return array( $wxr_path, $import_options );
183
184 }
185
186 /**
187 * Helper function: get the right format of response errors.
188 *
189 * @param array|WP_Error $response Array or WP_Error or the response.
190 *
191 * @return array Error code and error message.
192 */
193 private static function getErrorMessageFromResponse( $response ) {
194 $response_error = array();
195
196 if ( is_array( $response ) ) {
197 $response_error['error_code'] = $response['response']['code'];
198 $response_error['error_message'] = $response['response']['message'];
199 } else {
200 $response_error['error_code'] = $response->get_error_code();
201 $response_error['error_message'] = $response->get_error_message();
202 }
203
204 return $response_error;
205 }
206
207 /**
208 * Write content to a file.
209 *
210 * @param string $content content to be saved to the file.
211 * @param string $file_path file path where the content should be saved.
212 *
213 * @return string|\WP_Error path to the saved file or WP_Error object with error message.
214 */
215 public static function writeFile( $content, $file_path ) {
216 // Verify WP file-system credentials.
217 $verified_credentials = self::checkWpFileSystemCredentials();
218
219 if ( is_wp_error( $verified_credentials ) ) {
220 return $verified_credentials;
221 }
222
223 if ( ! file_exists( dirname( $file_path ) ) ) {
224 mkdir( dirname( $file_path ), 0777, true );
225 }
226
227 if ( false === file_put_contents( $file_path, $content ) ) {
228 return new \WP_Error(
229 'failed_writing_file_to_server',
230 sprintf( /* translators: %1$s - br HTML tag, %2$s - file path */
231 __( 'An error occurred while writing file to your server! Tried to write a file to: %1$s%2$s.', 'kubio' ),
232 '<br>',
233 $file_path
234 )
235 );
236 }
237
238 // Return the file path on successful file write.
239 return $file_path;
240 }
241
242 /**
243 * Helper function: check for WP file-system credentials needed for reading and writing to a file.
244 *
245 * @return boolean|\WP_Error
246 */
247 private static function checkWpFileSystemCredentials() {
248 // Check if the file-system method is 'direct', if not display an error.
249 if ( ! ( 'direct' === get_filesystem_method() ) ) {
250 return new \WP_Error(
251 'no_direct_file_access',
252 sprintf( /* translators: %1$s and %2$s - strong HTML tags, %3$s - HTML link to a doc page. */
253 __( 'This WordPress site does not have %1$sdirect%2$s write file access. This plugin needs it in order to save the demo import files to the upload directory of your site. You can change this setting with these instructions: %3$s.', 'kubio' ),
254 '<strong>',
255 '</strong>',
256 '<a href="https://wordpress.org/support/article/editing-wp-config-php/#override-of-default-file-permissions" target="_blank">How to set <strong>direct</strong> filesystem method</a>'
257 )
258 );
259 }
260
261 return true;
262 }
263
264 /**
265 * Write the error to the log file and send the AJAX response.
266 *
267 * @param string $error_text text to display in the log file and in the AJAX response.
268 * @param string $log_file_path path to the log file.
269 * @param string $separator title separating the old and new content.
270 */
271 public static function logErrorAndSendAjaxResponse( $error_text, $log_file_path, $separator = '' ) {
272 // Add this error to log file.
273 $log_added = self::appendToFile(
274 $error_text,
275 $log_file_path,
276 $separator
277 );
278
279 // Send JSON Error response to the AJAX call.
280 static::sendAjaxError( $error_text );
281 }
282
283 /**
284 * Append content to the file.
285 *
286 * @param string $content content to be saved to the file.
287 * @param string $file_path file path where the content should be saved.
288 * @param string $separator_text separates the existing content of the file with the new content.
289 *
290 * @return boolean|\WP_Error, path to the saved file or WP_Error object with error message.
291 */
292 public static function appendToFile( $content, $file_path, $separator_text = '' ) {
293 // Verify WP file-system credentials.
294 $verified_credentials = self::checkWpFileSystemCredentials();
295
296 if ( is_wp_error( $verified_credentials ) ) {
297 return $verified_credentials;
298 }
299
300 $existing_data = '';
301 if ( file_exists( $file_path ) ) {
302 $existing_data = file_get_contents( $file_path );
303 }
304
305 // Style separator.
306 $separator = PHP_EOL . '---' . $separator_text . '---' . PHP_EOL;
307
308 if ( ! file_exists( dirname( $file_path ) ) ) {
309 mkdir( dirname( $file_path ), 0777, true );
310 }
311
312 if ( ! file_put_contents( $file_path, $existing_data . $separator . $content . PHP_EOL ) ) {
313 return new \WP_Error(
314 'failed_writing_file_to_server',
315 sprintf( /* translators: %1$s - br HTML tag, %2$s - file path */
316 __( 'An error occurred while writing file to your server! Tried to write a file to: %1$s%2$s.', 'one-click-demo-import' ),
317 '<br>',
318 $file_path
319 )
320 );
321 }
322
323 return true;
324 }
325
326 public static function sendAjaxError( $message ) {
327
328 if ( is_wp_error( $message ) ) {
329 /** @var \WP_Error $message */
330
331 $data = array();
332
333 foreach ( $message->get_error_codes() as $code ) {
334 $data[] = "<strong>$code</strong>: " . $message->get_error_message( $code );
335 }
336
337 wp_send_json_error( array( 'error' => $data ) );
338 }
339
340 wp_send_json_error( array( 'error' => $message ) );
341 }
342 }
343