DemoSites.php
1 year ago
DemoSitesHelpers.php
1 month ago
DemoSitesImportBlockMap.php
1 year ago
DemoSitesImporter.php
1 year ago
DemoSitesLogger.php
4 years ago
DemoSitesRepository.php
1 year ago
WXRExporter.php
1 year ago
WXRImporter.php
1 year ago
DemoSitesHelpers.php
341 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="kubio-notice 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 | public static function useUploadedKDSFile( $kds_file ) { |
| 164 | |
| 165 | $upload_dir = wp_upload_dir(); |
| 166 | $upload_path = trailingslashit( $upload_dir['path'] ); |
| 167 | |
| 168 | //basename($url); |
| 169 | $time = static::$import_start_time; |
| 170 | $path = "{$upload_path}/kubio-demo-import/demo-site-{$time}.wxr"; |
| 171 | |
| 172 | $content = file_get_contents( $kds_file ); |
| 173 | $file_data = unserialize( $content ); |
| 174 | $wxr_content = Arr::get( $file_data, 'content', '' ); |
| 175 | |
| 176 | unset( $file_data['content'] ); |
| 177 | $import_options = $file_data; |
| 178 | |
| 179 | $wxr_path = static::writeFile( $wxr_content, $path ); |
| 180 | |
| 181 | return array( $wxr_path, $import_options ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Helper function: get the right format of response errors. |
| 186 | * |
| 187 | * @param array|WP_Error $response Array or WP_Error or the response. |
| 188 | * |
| 189 | * @return array Error code and error message. |
| 190 | */ |
| 191 | private static function getErrorMessageFromResponse( $response ) { |
| 192 | $response_error = array(); |
| 193 | |
| 194 | if ( is_array( $response ) ) { |
| 195 | $response_error['error_code'] = $response['response']['code']; |
| 196 | $response_error['error_message'] = $response['response']['message']; |
| 197 | } else { |
| 198 | $response_error['error_code'] = $response->get_error_code(); |
| 199 | $response_error['error_message'] = $response->get_error_message(); |
| 200 | } |
| 201 | |
| 202 | return $response_error; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Write content to a file. |
| 207 | * |
| 208 | * @param string $content content to be saved to the file. |
| 209 | * @param string $file_path file path where the content should be saved. |
| 210 | * |
| 211 | * @return string|\WP_Error path to the saved file or WP_Error object with error message. |
| 212 | */ |
| 213 | public static function writeFile( $content, $file_path ) { |
| 214 | // Verify WP file-system credentials. |
| 215 | $verified_credentials = self::checkWpFileSystemCredentials(); |
| 216 | |
| 217 | if ( is_wp_error( $verified_credentials ) ) { |
| 218 | return $verified_credentials; |
| 219 | } |
| 220 | |
| 221 | if ( ! file_exists( dirname( $file_path ) ) ) { |
| 222 | wp_mkdir_p( dirname( $file_path ) ); |
| 223 | } |
| 224 | |
| 225 | if ( false === file_put_contents( $file_path, $content ) ) { |
| 226 | return new \WP_Error( |
| 227 | 'failed_writing_file_to_server', |
| 228 | sprintf( /* translators: %1$s - br HTML tag, %2$s - file path */ |
| 229 | __( 'An error occurred while writing file to your server! Tried to write a file to: %1$s%2$s.', 'kubio' ), |
| 230 | '<br>', |
| 231 | $file_path |
| 232 | ) |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | // Return the file path on successful file write. |
| 237 | return $file_path; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Helper function: check for WP file-system credentials needed for reading and writing to a file. |
| 242 | * |
| 243 | * @return boolean|\WP_Error |
| 244 | */ |
| 245 | private static function checkWpFileSystemCredentials() { |
| 246 | // Check if the file-system method is 'direct', if not display an error. |
| 247 | if ( ! ( 'direct' === get_filesystem_method() ) ) { |
| 248 | return new \WP_Error( |
| 249 | 'no_direct_file_access', |
| 250 | sprintf( /* translators: %1$s and %2$s - strong HTML tags, %3$s - HTML link to a doc page. */ |
| 251 | __( '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' ), |
| 252 | '<strong>', |
| 253 | '</strong>', |
| 254 | '<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>' |
| 255 | ) |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Write the error to the log file and send the AJAX response. |
| 264 | * |
| 265 | * @param string $error_text text to display in the log file and in the AJAX response. |
| 266 | * @param string $log_file_path path to the log file. |
| 267 | * @param string $separator title separating the old and new content. |
| 268 | */ |
| 269 | public static function logErrorAndSendAjaxResponse( $error_text, $log_file_path, $separator = '' ) { |
| 270 | // Add this error to log file. |
| 271 | $log_added = self::appendToFile( |
| 272 | $error_text, |
| 273 | $log_file_path, |
| 274 | $separator |
| 275 | ); |
| 276 | |
| 277 | // Send JSON Error response to the AJAX call. |
| 278 | static::sendAjaxError( $error_text ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Append content to the file. |
| 283 | * |
| 284 | * @param string $content content to be saved to the file. |
| 285 | * @param string $file_path file path where the content should be saved. |
| 286 | * @param string $separator_text separates the existing content of the file with the new content. |
| 287 | * |
| 288 | * @return boolean|\WP_Error, path to the saved file or WP_Error object with error message. |
| 289 | */ |
| 290 | public static function appendToFile( $content, $file_path, $separator_text = '' ) { |
| 291 | // Verify WP file-system credentials. |
| 292 | $verified_credentials = self::checkWpFileSystemCredentials(); |
| 293 | |
| 294 | if ( is_wp_error( $verified_credentials ) ) { |
| 295 | return $verified_credentials; |
| 296 | } |
| 297 | |
| 298 | $existing_data = ''; |
| 299 | if ( file_exists( $file_path ) ) { |
| 300 | $existing_data = file_get_contents( $file_path ); |
| 301 | } |
| 302 | |
| 303 | // Style separator. |
| 304 | $separator = PHP_EOL . '---' . $separator_text . '---' . PHP_EOL; |
| 305 | |
| 306 | if ( ! file_exists( dirname( $file_path ) ) ) { |
| 307 | wp_mkdir_p( dirname( $file_path ) ); |
| 308 | } |
| 309 | |
| 310 | if ( ! file_put_contents( $file_path, $existing_data . $separator . $content . PHP_EOL ) ) { |
| 311 | return new \WP_Error( |
| 312 | 'failed_writing_file_to_server', |
| 313 | sprintf( /* translators: %1$s - br HTML tag, %2$s - file path */ |
| 314 | __( 'An error occurred while writing file to your server! Tried to write a file to: %1$s%2$s.', 'kubio' ), |
| 315 | '<br>', |
| 316 | $file_path |
| 317 | ) |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | public static function sendAjaxError( $message ) { |
| 325 | |
| 326 | if ( is_wp_error( $message ) ) { |
| 327 | /** @var \WP_Error $message */ |
| 328 | |
| 329 | $data = array(); |
| 330 | |
| 331 | foreach ( $message->get_error_codes() as $code ) { |
| 332 | $data[] = "<strong>$code</strong>: " . $message->get_error_message( $code ); |
| 333 | } |
| 334 | |
| 335 | wp_send_json_error( array( 'error' => $data ) ); |
| 336 | } |
| 337 | |
| 338 | wp_send_json_error( array( 'error' => $message ) ); |
| 339 | } |
| 340 | } |
| 341 |