PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.2.0
Kubio AI Page Builder v2.2.0
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 3 years ago DemoSitesImportBlockMap.php 4 years ago DemoSitesImporter.php 2 years ago DemoSitesLogger.php 4 years ago DemoSitesRepository.php 2 years ago WXRExporter.php 4 years ago WXRImporter.php 2 years ago
DemoSitesHelpers.php
333 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 if ( empty( $source ) ) {
94 return new \WP_Error(
95 'missing_url',
96 __( 'Missing URL for downloading a file!', 'kubio' )
97 );
98 }
99
100 if ( ! filter_var( $source, FILTER_VALIDATE_URL ) && file_exists( $source ) ) {
101 $content = file_get_contents( $source );
102 } else {
103 if ( ! filter_var( $source, FILTER_VALIDATE_URL ) ) {
104 return new \WP_Error(
105 'invalid_url',
106 __( 'Invalid URL for downloading a file!', 'kubio' )
107 );
108 }
109 // Get file content from the server.
110 $response = wp_remote_get( $source );
111
112 // Test if the get request was not successful.
113 if ( is_wp_error( $response ) || 200 !== $response['response']['code'] ) {
114 // Collect the right format of error data (array or WP_Error).
115 $response_error = static::getErrorMessageFromResponse( $response );
116
117 return new \WP_Error(
118 'download_error',
119 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. */
120 __( 'An error occurred while fetching file from: %1$s%2$s%3$s!%4$sReason: %5$s - %6$s.', 'kubio' ),
121 '<strong>',
122 $source,
123 '</strong>',
124 '<br>',
125 $response_error['error_code'],
126 $response_error['error_message']
127 ) . '<br>'
128 );
129 }
130
131 $content = wp_remote_retrieve_body( $response );
132
133 }
134
135 $upload_dir = wp_upload_dir();
136 $upload_path = trailingslashit( $upload_dir['path'] );
137
138 //basename($url);
139 $time = static::$import_start_time;
140 $path = "{$upload_path}/kubio-demo-import/demo-site-{$time}.wxr";
141
142 $file_data = unserialize( $content );
143 $wxr_content = Arr::get( $file_data, 'content', '' );
144
145 unset( $file_data['content'] );
146 $import_options = $file_data;
147
148 $wxr_path = static::writeFile( $wxr_content, $path );
149
150 return array( $wxr_path, $import_options );
151
152 }
153
154 public static function useUploadedKDSFile( $kds_file ) {
155
156 $upload_dir = wp_upload_dir();
157 $upload_path = trailingslashit( $upload_dir['path'] );
158
159 //basename($url);
160 $time = static::$import_start_time;
161 $path = "{$upload_path}/kubio-demo-import/demo-site-{$time}.wxr";
162
163 $content = file_get_contents( $kds_file );
164 $file_data = unserialize( $content );
165 $wxr_content = Arr::get( $file_data, 'content', '' );
166
167 unset( $file_data['content'] );
168 $import_options = $file_data;
169
170 $wxr_path = static::writeFile( $wxr_content, $path );
171
172 return array( $wxr_path, $import_options );
173
174 }
175
176 /**
177 * Helper function: get the right format of response errors.
178 *
179 * @param array|WP_Error $response Array or WP_Error or the response.
180 *
181 * @return array Error code and error message.
182 */
183 private static function getErrorMessageFromResponse( $response ) {
184 $response_error = array();
185
186 if ( is_array( $response ) ) {
187 $response_error['error_code'] = $response['response']['code'];
188 $response_error['error_message'] = $response['response']['message'];
189 } else {
190 $response_error['error_code'] = $response->get_error_code();
191 $response_error['error_message'] = $response->get_error_message();
192 }
193
194 return $response_error;
195 }
196
197 /**
198 * Write content to a file.
199 *
200 * @param string $content content to be saved to the file.
201 * @param string $file_path file path where the content should be saved.
202 *
203 * @return string|\WP_Error path to the saved file or WP_Error object with error message.
204 */
205 public static function writeFile( $content, $file_path ) {
206 // Verify WP file-system credentials.
207 $verified_credentials = self::checkWpFileSystemCredentials();
208
209 if ( is_wp_error( $verified_credentials ) ) {
210 return $verified_credentials;
211 }
212
213 if ( ! file_exists( dirname( $file_path ) ) ) {
214 mkdir( dirname( $file_path ), 0777, true );
215 }
216
217 if ( false === file_put_contents( $file_path, $content ) ) {
218 return new \WP_Error(
219 'failed_writing_file_to_server',
220 sprintf( /* translators: %1$s - br HTML tag, %2$s - file path */
221 __( 'An error occurred while writing file to your server! Tried to write a file to: %1$s%2$s.', 'kubio' ),
222 '<br>',
223 $file_path
224 )
225 );
226 }
227
228 // Return the file path on successful file write.
229 return $file_path;
230 }
231
232 /**
233 * Helper function: check for WP file-system credentials needed for reading and writing to a file.
234 *
235 * @return boolean|\WP_Error
236 */
237 private static function checkWpFileSystemCredentials() {
238 // Check if the file-system method is 'direct', if not display an error.
239 if ( ! ( 'direct' === get_filesystem_method() ) ) {
240 return new \WP_Error(
241 'no_direct_file_access',
242 sprintf( /* translators: %1$s and %2$s - strong HTML tags, %3$s - HTML link to a doc page. */
243 __( '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' ),
244 '<strong>',
245 '</strong>',
246 '<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>'
247 )
248 );
249 }
250
251 return true;
252 }
253
254 /**
255 * Write the error to the log file and send the AJAX response.
256 *
257 * @param string $error_text text to display in the log file and in the AJAX response.
258 * @param string $log_file_path path to the log file.
259 * @param string $separator title separating the old and new content.
260 */
261 public static function logErrorAndSendAjaxResponse( $error_text, $log_file_path, $separator = '' ) {
262 // Add this error to log file.
263 $log_added = self::appendToFile(
264 $error_text,
265 $log_file_path,
266 $separator
267 );
268
269 // Send JSON Error response to the AJAX call.
270 static::sendAjaxError( $error_text );
271 }
272
273 /**
274 * Append content to the file.
275 *
276 * @param string $content content to be saved to the file.
277 * @param string $file_path file path where the content should be saved.
278 * @param string $separator_text separates the existing content of the file with the new content.
279 *
280 * @return boolean|\WP_Error, path to the saved file or WP_Error object with error message.
281 */
282 public static function appendToFile( $content, $file_path, $separator_text = '' ) {
283 // Verify WP file-system credentials.
284 $verified_credentials = self::checkWpFileSystemCredentials();
285
286 if ( is_wp_error( $verified_credentials ) ) {
287 return $verified_credentials;
288 }
289
290 $existing_data = '';
291 if ( file_exists( $file_path ) ) {
292 $existing_data = file_get_contents( $file_path );
293 }
294
295 // Style separator.
296 $separator = PHP_EOL . '---' . $separator_text . '---' . PHP_EOL;
297
298 if ( ! file_exists( dirname( $file_path ) ) ) {
299 mkdir( dirname( $file_path ), 0777, true );
300 }
301
302 if ( ! file_put_contents( $file_path, $existing_data . $separator . $content . PHP_EOL ) ) {
303 return new \WP_Error(
304 'failed_writing_file_to_server',
305 sprintf( /* translators: %1$s - br HTML tag, %2$s - file path */
306 __( 'An error occurred while writing file to your server! Tried to write a file to: %1$s%2$s.', 'one-click-demo-import' ),
307 '<br>',
308 $file_path
309 )
310 );
311 }
312
313 return true;
314 }
315
316 public static function sendAjaxError( $message ) {
317
318 if ( is_wp_error( $message ) ) {
319 /** @var \WP_Error $message */
320
321 $data = array();
322
323 foreach ( $message->get_error_codes() as $code ) {
324 $data[] = "<strong>$code</strong>: " . $message->get_error_message( $code );
325 }
326
327 wp_send_json_error( array( 'error' => $data ) );
328 }
329
330 wp_send_json_error( array( 'error' => $message ) );
331 }
332 }
333