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