| @@ -2,36 +2,12 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Templately\Core\Importer\Utils; |
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | -use Templately\Builder\PageTemplates; | |
| 7 | -use Templately\Builder\Factory\TemplateFactory; | |
| 8 | -use Templately\Core\Importer\FullSiteImport; | |
| 9 | -use Templately\Core\Importer\WPImport; | |
| 10 | 6 | use Templately\Utils\Base; |
| 11 | -use Templately\Utils\Helper; | |
| 12 | 7 | |
| 13 | 8 | class Utils extends Base { |
| 14 | - | |
| 15 | 9 | /** |
| 16 | - * Filter callback to prefer the GD image editor over Imagick during imports. | |
| 17 | - * Imagick can fail on certain server configurations; GD is more reliable for | |
| 18 | - * the resize/crop operations triggered by wp_generate_attachment_metadata(). | |
| 19 | - */ | |
| 20 | - public static function prefer_gd_editor( $editors ) { | |
| 21 | - if ( is_callable( [ 'WP_Image_Editor_GD', 'test' ] ) && call_user_func( [ 'WP_Image_Editor_GD', 'test' ] ) ) { | |
| 22 | - return [ 'WP_Image_Editor_GD' ]; | |
| 23 | - } | |
| 24 | - return $editors; | |
| 25 | - } | |
| 26 | - | |
| 27 | - public static function add_gd_editor_filter() { | |
| 28 | - if ( ! has_filter( 'wp_image_editors', [ self::class, 'prefer_gd_editor' ] ) ) { | |
| 29 | - add_filter( 'wp_image_editors', [ self::class, 'prefer_gd_editor' ] ); | |
| 30 | - } | |
| 31 | - } | |
| 32 | - | |
| 33 | - /** | |
| 34 | 10 | * @throws Exception |
| 35 | 11 | */ |
| 36 | 12 | public static function read_json_file( $path ) { |
| 37 | 13 | if ( ! file_exists( $path ) ) { |
| @@ -83,14 +59,8 @@ | ||
| 83 | 59 | $result += $post_type['succeed'] ?? []; |
| 84 | 60 | } |
| 85 | 61 | } |
| 86 | 62 | |
| 87 | - // add attachments data | |
| 88 | - if ( !empty( $imported_data['attachments']['succeed'] ) ) { | |
| 89 | - $result += $imported_data['attachments']['succeed'] ?? []; | |
| 90 | - } | |
| 91 | - | |
| 92 | - | |
| 93 | 63 | return $result; |
| 94 | 64 | } |
| 95 | 65 | |
| 96 | 66 | public static function map_old_new_term_ids( array $imported_data ) { |
| @@ -131,172 +101,17 @@ | ||
| 131 | 101 | public static function get_json_helper( string $platform ) { |
| 132 | 102 | return $platform === 'elementor' ? new ElementorHelper() : new GutenbergHelper(); |
| 133 | 103 | } |
| 134 | 104 | |
| 135 | - public static function get_backup_options() { | |
| 136 | - global $wpdb; | |
| 137 | - | |
| 138 | - $prefix = '__templately_'; | |
| 139 | - $table_name = $wpdb->options; // Assuming default options table name | |
| 140 | - | |
| 141 | - $sql = "SELECT option_name, option_value FROM {$table_name} WHERE option_name LIKE %s"; | |
| 142 | - $prepared_sql = $wpdb->prepare($sql, array("$prefix%")); // Escape wildcard for security | |
| 143 | - | |
| 144 | - $results = $wpdb->get_results($prepared_sql); | |
| 145 | - | |
| 146 | - $templately_options = array(); | |
| 147 | - foreach ($results as $row) { | |
| 148 | - $name = str_replace($prefix, '', $row->option_name); | |
| 149 | - $templately_options[$name] = maybe_unserialize($row->option_value); | |
| 105 | + public static function update_option( $key, $value, $autoload = 'no' ){ | |
| 106 | + $bk_value = get_option("__templately_$key"); | |
| 107 | + if($bk_value === false){ | |
| 108 | + $old_value = get_option($key); | |
| 109 | + add_option( "__templately_$key", $old_value, '', $autoload ); | |
| 150 | 110 | } |
| 151 | - | |
| 152 | - return $templately_options; | |
| 111 | + return update_option( $key, $value, $autoload ); | |
| 153 | 112 | } |
| 154 | 113 | |
| 155 | - public static function backup_option_value($key, $autoload = 'no') { | |
| 156 | - $old_value = get_option($key); | |
| 157 | - if ($old_value) { | |
| 158 | - update_option("__templately_$key", $old_value, $autoload); | |
| 159 | - } | |
| 160 | - else { | |
| 161 | - add_option("__templately_$key", $old_value, '', $autoload); | |
| 162 | - } | |
| 163 | - } | |
| 164 | - | |
| 165 | - public static function update_option($key, $value, $autoload = 'no') { | |
| 166 | - self::backup_option_value($key, $autoload); | |
| 167 | - return update_option($key, $value, $autoload); | |
| 168 | - } | |
| 169 | - | |
| 170 | - /** | |
| 171 | - * Create the WordPress page an archive-type template attaches to, and wire up | |
| 172 | - * the matching WP/WooCommerce/LearnDash setting. | |
| 173 | - * | |
| 174 | - * Shared by the FSI Templates runner and single-template Library imports so both | |
| 175 | - * produce the same companion page + settings (page_for_posts, | |
| 176 | - * woocommerce_shop_page_id, LearnDash courses slug). | |
| 177 | - * | |
| 178 | - * @param array $template_settings Needs 'type'; optional 'title' and 'page_settings.archive_page_id'. | |
| 179 | - * @param string $platform 'elementor' | 'gutenberg'. | |
| 180 | - * @param array $manifest FSI manifest (optional). Only used to skip creation when the | |
| 181 | - * archive page is already part of the imported content. | |
| 182 | - * | |
| 183 | - * @return false|int New page ID, or false on skip/failure. | |
| 184 | - */ | |
| 185 | - public static function create_archive_page( $template_settings, $platform, $manifest = [] ) { | |
| 186 | - try { | |
| 187 | - $archive_page_id = $template_settings['page_settings']['archive_page_id'] ?? null; | |
| 188 | - if ( $archive_page_id && ! empty( $manifest['content']['page'][ $archive_page_id ] ) ) { | |
| 189 | - return false; | |
| 190 | - } | |
| 191 | - | |
| 192 | - $type = $template_settings['type']; | |
| 193 | - | |
| 194 | - $archive_page = wp_insert_post( [ | |
| 195 | - 'post_title' => $template_settings['title'] ?? ucfirst( $type ) . ' - (by Templately)', | |
| 196 | - 'post_status' => 'publish', | |
| 197 | - 'post_type' => 'page', | |
| 198 | - 'post_content' => '', | |
| 199 | - 'page_template' => $platform === 'elementor' ? 'elementor_header_footer' : PageTemplates::TEMPLATE_HEADER_FOOTER, | |
| 200 | - ] ); | |
| 201 | - | |
| 202 | - if ( is_wp_error( $archive_page ) ) { | |
| 203 | - return false; | |
| 204 | - } | |
| 205 | - | |
| 206 | - if ( $type === 'archive' ) { | |
| 207 | - self::update_option( 'page_for_posts', $archive_page ); | |
| 208 | - } | |
| 209 | - | |
| 210 | - if ( $type === 'product_archive' ) { | |
| 211 | - self::update_option( 'woocommerce_shop_page_id', $archive_page ); | |
| 212 | - } | |
| 213 | - | |
| 214 | - if ( $type === 'course_archive' ) { | |
| 215 | - // get page slug from $archive_page id and update learndash_settings_permalinks option to courses. | |
| 216 | - $post_name = get_post_field( 'post_name', $archive_page ); | |
| 217 | - | |
| 218 | - if ( class_exists( '\LearnDash_Settings_Section' ) ) { | |
| 219 | - $section = \LearnDash_Settings_Section::get_section_instance( 'LearnDash_Settings_Section_Permalinks' ); | |
| 220 | - if ( ! empty( $section ) ) { | |
| 221 | - $section->set_setting( 'courses', $post_name ); | |
| 222 | - if ( function_exists( 'learndash_setup_rewrite_flush' ) ) { | |
| 223 | - learndash_setup_rewrite_flush(); | |
| 224 | - } | |
| 225 | - } | |
| 226 | - } | |
| 227 | - } | |
| 228 | - | |
| 229 | - return $archive_page; | |
| 230 | - } catch ( \Exception $e ) { | |
| 231 | - return false; | |
| 232 | - } | |
| 233 | - } | |
| 234 | - | |
| 235 | - /** | |
| 236 | - * Create a single-page Library template carrying a post-content widget/block, | |
| 237 | - * so a fluent_product_single import has a page template to render into. | |
| 238 | - * | |
| 239 | - * Shared by the FSI Templates runner and single-template Library imports. | |
| 240 | - * | |
| 241 | - * @param string $platform 'elementor' | 'gutenberg'. | |
| 242 | - * | |
| 243 | - * @return void | |
| 244 | - */ | |
| 245 | - public static function create_page_template( $platform ) { | |
| 246 | - try { | |
| 247 | - $meta = []; | |
| 248 | - $data = []; | |
| 249 | - | |
| 250 | - $post_data = [ | |
| 251 | - 'post_title' => 'Single Page - (by Templately)', | |
| 252 | - 'post_status' => 'publish', | |
| 253 | - 'post_type' => 'templately_library', | |
| 254 | - ]; | |
| 255 | - | |
| 256 | - if ( $platform === 'elementor' ) { | |
| 257 | - $meta['_wp_page_template'] = 'elementor_header_footer'; | |
| 258 | - $data = json_decode( '{"content":[{"id":"4a86515d","settings":[],"elements":[{"id":"30a46db1","settings":{"content_width":"full"},"elements":[],"isInner":false,"widgetType":"tl-post-content","elType":"widget"}],"isInner":false,"elType":"container"}],"settings":{"template":"elementor_header_footer"},"metadata":[]}', true ); | |
| 259 | - } elseif ( $platform === 'gutenberg' ) { | |
| 260 | - $meta['_wp_page_template'] = PageTemplates::TEMPLATE_HEADER_FOOTER; | |
| 261 | - $data = [ 'content' => '<!-- wp:post-content /-->' ]; | |
| 262 | - } | |
| 263 | - | |
| 264 | - $factory = new TemplateFactory( $platform ); | |
| 265 | - $template = $factory->create( 'page_single', $post_data, $meta ); | |
| 266 | - $template->import( $data ); | |
| 267 | - } catch ( \Exception $e ) { | |
| 268 | - return; | |
| 269 | - } | |
| 270 | - } | |
| 271 | - | |
| 272 | - /** | |
| 273 | - * Run the companion-content side effects for a freshly imported Library template. | |
| 274 | - * | |
| 275 | - * Dispatches by resolved Builder type: | |
| 276 | - * - archive / product_archive / course_archive → attached WP page + settings | |
| 277 | - * - fluent_product_single → single-page template | |
| 278 | - * | |
| 279 | - * Convenience wrapper for single-template imports, which have no FSI manifest. | |
| 280 | - * | |
| 281 | - * @param string $type Resolved Builder type key. | |
| 282 | - * @param string $title Template title (used for the created page). | |
| 283 | - * @param string $platform 'elementor' | 'gutenberg'. | |
| 284 | - * | |
| 285 | - * @return false|int|null Archive page ID for archive types, null otherwise. | |
| 286 | - */ | |
| 287 | - public static function create_companion_content( $type, $title, $platform ) { | |
| 288 | - if ( in_array( $type, [ 'archive', 'product_archive', 'course_archive' ], true ) ) { | |
| 289 | - return self::create_archive_page( [ 'title' => $title, 'type' => $type ], $platform ); | |
| 290 | - } | |
| 291 | - | |
| 292 | - if ( $type === 'fluent_product_single' ) { | |
| 293 | - self::create_page_template( $platform ); | |
| 294 | - } | |
| 295 | - | |
| 296 | - return null; | |
| 297 | - } | |
| 298 | - | |
| 299 | 114 | public static function import_page_settings( $id, $settings ) { |
| 300 | 115 | $extra_settings = [ |
| 301 | 116 | 'page_on_front' => [ |
| 302 | 117 | 'show_on_front' => 'page' |
| @@ -310,13 +125,9 @@ | ||
| 310 | 125 | self::update_option( 'show_on_front', 'page' ); |
| 311 | 126 | } |
| 312 | 127 | if ( ! empty( $settings['page_settings'] ) ) { |
| 313 | 128 | foreach ( $settings['page_settings'] as $option_name => $val ) { |
| 314 | - $__val = $id; | |
| 315 | - if($option_name === 'fluent_cart_store_settings'){ | |
| 316 | - $__val = $val; | |
| 317 | - } | |
| 318 | - self::update_option( $option_name, $__val ); | |
| 129 | + self::update_option( $option_name, $id ); | |
| 319 | 130 | if ( array_key_exists( $option_name, $extra_settings ) ) { |
| 320 | 131 | foreach ( $extra_settings[ $option_name ] as $name => $value ) { |
| 321 | 132 | self::update_option( $name, $value ); |
| 322 | 133 | } |
| @@ -324,9 +135,9 @@ | ||
| 324 | 135 | } |
| 325 | 136 | } |
| 326 | 137 | } |
| 327 | 138 | |
| 328 | - public static function upload_logo($url, $session_id) { | |
| 139 | + public static function upload_logo($url) { | |
| 329 | 140 | if(empty($url)) { |
| 330 | 141 | return ['error' => __('URL is empty', 'templately')]; |
| 331 | 142 | } |
| 332 | 143 | |
| @@ -334,349 +145,51 @@ | ||
| 334 | 145 | if ( ! wp_http_validate_url( $url ) || !parse_url( $url, PHP_URL_SCHEME ) ) { |
| 335 | 146 | return ['error' => __('Invalid URL', 'templately')]; |
| 336 | 147 | } |
| 337 | 148 | |
| 338 | - $post_data = self::prepare_post_data($url); | |
| 339 | - $wp_importer = new WPImport( null, ['fetch_attachments' => true, 'session_id' => $session_id] ); | |
| 340 | - $attachment_id = $wp_importer->process_attachment($post_data, $url); | |
| 341 | - | |
| 342 | - if(is_wp_error($attachment_id)){ | |
| 343 | - return ['error' => $attachment_id->get_error_message()]; | |
| 149 | + // Download image and get MIME type | |
| 150 | + $temp_file = download_url( $url ); | |
| 151 | + if ( is_wp_error( $temp_file ) ) { | |
| 152 | + return ['error' => __('Failed to download image', 'templately')]; | |
| 344 | 153 | } |
| 345 | 154 | |
| 346 | - return [ | |
| 347 | - 'id' => (int) $attachment_id, | |
| 348 | - 'url' => esc_url_raw(wp_get_attachment_url($attachment_id)), | |
| 349 | - ]; | |
| 350 | - } | |
| 351 | - | |
| 352 | - /** | |
| 353 | - * Get a remote logo URL from the supported import payload shapes. | |
| 354 | - * | |
| 355 | - * @param mixed $logo A URL string or an array containing a URL. | |
| 356 | - * @return string | |
| 357 | - */ | |
| 358 | - public static function get_logo_url( $logo ) { | |
| 359 | - if ( is_string( $logo ) ) { | |
| 360 | - return $logo; | |
| 155 | + // Validate image type using wp_get_image_mime | |
| 156 | + $mime_type = wp_get_image_mime( $temp_file ); | |
| 157 | + if ( ! $mime_type || !in_array( $mime_type, get_allowed_mime_types() ) ) { | |
| 158 | + unlink( $temp_file ); | |
| 159 | + return ['error' => __('Invalid image type', 'templately')]; | |
| 361 | 160 | } |
| 362 | 161 | |
| 363 | - if ( is_array( $logo ) && isset( $logo['url'] ) && is_string( $logo['url'] ) ) { | |
| 364 | - return $logo['url']; | |
| 162 | + $file_info = wp_check_filetype_and_ext( $temp_file, basename( $url ), get_allowed_mime_types() ); | |
| 163 | + if ( ! $file_info['ext'] || $file_info['type'] !== $mime_type ) { | |
| 164 | + unlink( $temp_file ); | |
| 165 | + return ['error' => __('File type and extension check failed', 'templately')]; | |
| 365 | 166 | } |
| 366 | 167 | |
| 367 | - return ''; | |
| 368 | - } | |
| 168 | + // Prepare the file for sideloading | |
| 169 | + $file = array( | |
| 170 | + 'name' => basename($url), | |
| 171 | + 'type' => $mime_type, | |
| 172 | + 'tmp_name' => $temp_file, | |
| 173 | + 'error' => 0, | |
| 174 | + 'size' => filesize($temp_file), | |
| 175 | + ); | |
| 369 | 176 | |
| 370 | - /** | |
| 371 | - * Upload base64 encoded image to media library | |
| 372 | - * | |
| 373 | - * @param string $base64 Base64 encoded image data (with or without data URI scheme) | |
| 374 | - * @param string $session_id Session ID for tracking (reserved for future use) | |
| 375 | - * @return array Array with 'id' and 'url' on success, or ['error' => message] on failure | |
| 376 | - */ | |
| 377 | - public static function upload_logo_base64($base64, $session_id = null) { | |
| 378 | - if(empty($base64)) { | |
| 379 | - return ['error' => __('Base64 is empty', 'templately')]; | |
| 380 | - } | |
| 177 | + // Load the WordPress media handler | |
| 178 | + require_once(ABSPATH . 'wp-admin/includes/media.php'); | |
| 179 | + require_once(ABSPATH . 'wp-admin/includes/file.php'); | |
| 180 | + require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
| 381 | 181 | |
| 382 | - // Upload the base64 image | |
| 383 | - $attachment_id = self::upload_base64_image($base64); | |
| 182 | + // Handle the sideload | |
| 183 | + $id = media_handle_sideload($file, 0); | |
| 384 | 184 | |
| 385 | - if(is_wp_error($attachment_id)){ | |
| 386 | - return ['error' => $attachment_id->get_error_message()]; | |
| 185 | + if (is_wp_error($id)) { | |
| 186 | + @unlink($file['tmp_name']); | |
| 187 | + return ['error' => __('Failed to sideload image', 'templately')]; | |
| 387 | 188 | } |
| 388 | 189 | |
| 389 | 190 | return [ |
| 390 | - 'id' => (int) $attachment_id, | |
| 391 | - 'url' => esc_url_raw(wp_get_attachment_url($attachment_id)), | |
| 191 | + 'id' => $id, | |
| 192 | + 'url' => esc_url_raw(wp_get_attachment_url($id)), | |
| 392 | 193 | ]; |
| 393 | 194 | } |
| 394 | - | |
| 395 | - /** | |
| 396 | - * Upload base64 encoded image without dependency on prepare_post_data | |
| 397 | - * Handles MIME type detection and proper file extension assignment | |
| 398 | - * | |
| 399 | - * @param string $base64 Base64 encoded image data (with or without data URI scheme) | |
| 400 | - * @return int|\WP_Error Attachment ID on success, WP_Error on failure | |
| 401 | - */ | |
| 402 | - public static function upload_base64_image($base64) { | |
| 403 | - // Strip data URL prefix if present (e.g., "data:image/png;base64,") | |
| 404 | - if (strpos($base64, 'data:image/') === 0) { | |
| 405 | - $base64_parts = explode(',', $base64, 2); | |
| 406 | - if (isset($base64_parts[1])) { | |
| 407 | - $base64 = $base64_parts[1]; | |
| 408 | - } | |
| 409 | - } | |
| 410 | - | |
| 411 | - // Decode base64 string | |
| 412 | - $decoded_image = base64_decode($base64, true); | |
| 413 | - if ($decoded_image === false) { | |
| 414 | - return new \WP_Error('invalid_base64', __('Invalid base64 data.', 'templately')); | |
| 415 | - } | |
| 416 | - | |
| 417 | - // Detect MIME type from decoded image data | |
| 418 | - $mime_type = self::detect_mime_type_from_data($decoded_image); | |
| 419 | - if (empty($mime_type)) { | |
| 420 | - return new \WP_Error('unknown_mime_type', __('Unable to determine image MIME type.', 'templately')); | |
| 421 | - } | |
| 422 | - | |
| 423 | - // Get file extension from MIME type | |
| 424 | - $extension = self::get_file_extension_by_mime_type($mime_type); | |
| 425 | - if (empty($extension)) { | |
| 426 | - return new \WP_Error('unsupported_mime_type', __('Unsupported image MIME type.', 'templately')); | |
| 427 | - } | |
| 428 | - | |
| 429 | - // Generate unique filename | |
| 430 | - $filename = 'templately-logo-' . \wp_generate_uuid4() . '.' . $extension; | |
| 431 | - | |
| 432 | - // Get upload directory | |
| 433 | - $upload_dir = \wp_upload_dir(); | |
| 434 | - if (!$upload_dir['error']) { | |
| 435 | - $upload_path = $upload_dir['path'] . '/' . $filename; | |
| 436 | - } else { | |
| 437 | - return new \WP_Error('upload_dir_error', __('Unable to access upload directory.', 'templately')); | |
| 438 | - } | |
| 439 | - | |
| 440 | - // Write decoded image to file | |
| 441 | - if (file_put_contents($upload_path, $decoded_image) === false) { | |
| 442 | - return new \WP_Error('upload_error', __('Error uploading image.', 'templately')); | |
| 443 | - } | |
| 444 | - | |
| 445 | - // Create attachment post | |
| 446 | - $attachment_data = [ | |
| 447 | - 'post_mime_type' => $mime_type, | |
| 448 | - 'post_title' => \sanitize_file_name(pathinfo($filename, PATHINFO_FILENAME)), | |
| 449 | - 'post_content' => '', | |
| 450 | - 'post_status' => 'inherit', | |
| 451 | - ]; | |
| 452 | - | |
| 453 | - $attachment_id = \wp_insert_attachment($attachment_data, $upload_path); | |
| 454 | - if (is_wp_error($attachment_id)) { | |
| 455 | - return $attachment_id; | |
| 456 | - } | |
| 457 | - | |
| 458 | - // Ensure WordPress image functions are available | |
| 459 | - // These functions are defined in wp-admin/includes/image.php which is not always loaded | |
| 460 | - if (!function_exists('wp_generate_attachment_metadata')) { | |
| 461 | - require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
| 462 | - } | |
| 463 | - | |
| 464 | - // Generate and update attachment metadata | |
| 465 | - $metadata = \wp_generate_attachment_metadata($attachment_id, $upload_path); | |
| 466 | - \wp_update_attachment_metadata($attachment_id, $metadata); | |
| 467 | - | |
| 468 | - return $attachment_id; | |
| 469 | - } | |
| 470 | - | |
| 471 | - /** | |
| 472 | - * Detect MIME type from image data | |
| 473 | - * Uses finfo_buffer if available, otherwise falls back to getimagesizefromstring | |
| 474 | - * | |
| 475 | - * @param string $image_data Raw image data | |
| 476 | - * @return string|null MIME type or null if unable to detect | |
| 477 | - */ | |
| 478 | - private static function detect_mime_type_from_data($image_data) { | |
| 479 | - // Try using finfo_buffer first (most reliable) | |
| 480 | - if (function_exists('finfo_buffer')) { | |
| 481 | - $finfo = finfo_open(FILEINFO_MIME_TYPE); | |
| 482 | - if ($finfo) { | |
| 483 | - $mime_type = finfo_buffer($finfo, $image_data); | |
| 484 | - finfo_close($finfo); | |
| 485 | - if ($mime_type && strpos($mime_type, 'image/') === 0) { | |
| 486 | - return $mime_type; | |
| 487 | - } | |
| 488 | - } | |
| 489 | - } | |
| 490 | - | |
| 491 | - // Fallback: use getimagesizefromstring | |
| 492 | - if (function_exists('getimagesizefromstring')) { | |
| 493 | - $image_info = @getimagesizefromstring($image_data); | |
| 494 | - if ($image_info && isset($image_info['mime'])) { | |
| 495 | - return $image_info['mime']; | |
| 496 | - } | |
| 497 | - } | |
| 498 | - | |
| 499 | - return null; | |
| 500 | - } | |
| 501 | - | |
| 502 | - /** | |
| 503 | - * Get file extension by MIME type | |
| 504 | - * Uses WordPress built-in functions for MIME type to extension conversion | |
| 505 | - * | |
| 506 | - * @since 3.4.5 | |
| 507 | - * @param string $mime_type MIME type (e.g., 'image/png') | |
| 508 | - * @return string|null File extension without dot, or null if not found | |
| 509 | - */ | |
| 510 | - private static function get_file_extension_by_mime_type($mime_type) { | |
| 511 | - // Use WordPress core function if available (WordPress 5.8.1+) | |
| 512 | - // wp_get_default_extension_for_mime_type() returns the default file extension for a given MIME type | |
| 513 | - if (function_exists('wp_get_default_extension_for_mime_type')) { | |
| 514 | - return \wp_get_default_extension_for_mime_type($mime_type); | |
| 515 | - } | |
| 516 | - | |
| 517 | - // Fallback for WordPress < 5.8.1 | |
| 518 | - // Use wp_get_mime_types() which returns array with extensions as keys and MIME types as values | |
| 519 | - // Example: ['jpg|jpeg|jpe' => 'image/jpeg', 'png' => 'image/png', ...] | |
| 520 | - $wp_mime_types = \wp_get_mime_types(); | |
| 521 | - | |
| 522 | - // Flip the array to get MIME type as key and extensions as value | |
| 523 | - $mime_map = array_flip($wp_mime_types); | |
| 524 | - | |
| 525 | - if (isset($mime_map[$mime_type])) { | |
| 526 | - $extensions = $mime_map[$mime_type]; | |
| 527 | - // Get first extension if multiple are available (e.g., 'jpg|jpeg|jpe' -> 'jpg') | |
| 528 | - return strtok($extensions, '|'); | |
| 529 | - } | |
| 530 | - | |
| 531 | - return null; | |
| 532 | - } | |
| 533 | - | |
| 534 | - /** | |
| 535 | - * Inserts a template into the Gutenberg editor. | |
| 536 | - * | |
| 537 | - * @param mixed $data | |
| 538 | - * @param int $postId | |
| 539 | - * @return array | |
| 540 | - */ | |
| 541 | - public static function import_and_replace_attachments($content, $postId = 0, $attachment_options = []) { | |
| 542 | - // Instantiate GutenbergHelper | |
| 543 | - $helper = new GutenbergHelper(); | |
| 544 | - | |
| 545 | - if ( ! empty( $attachment_options ) ) { | |
| 546 | - $helper->set_attachment_options( $attachment_options ); | |
| 547 | - } | |
| 548 | - | |
| 549 | - $data = [ | |
| 550 | - 'content' => $content, | |
| 551 | - ]; | |
| 552 | - | |
| 553 | - // Organize URLs from the content | |
| 554 | - $organizedUrls = $helper->parse_images($data['content']); | |
| 555 | - if(empty($organizedUrls)){ | |
| 556 | - return $content; | |
| 557 | - } | |
| 558 | - | |
| 559 | - // Define template settings | |
| 560 | - $template_settings = [ | |
| 561 | - 'post_id' => $postId, | |
| 562 | - '__attachments' => $organizedUrls, | |
| 563 | - ]; | |
| 564 | - | |
| 565 | - // Map post IDs and disable logging | |
| 566 | - $helper->map_post_ids[$postId] = $postId; | |
| 567 | - $helper->shouldLog = false; | |
| 568 | - | |
| 569 | - // Prepare the helper with the data and settings | |
| 570 | - $helper->prepare($data, $template_settings); | |
| 571 | - | |
| 572 | - // Update the content in the data array | |
| 573 | - $content = wp_unslash($helper->get_content()); | |
| 574 | - | |
| 575 | - return $content; | |
| 576 | - } | |
| 577 | - | |
| 578 | - public static function prepare_post_data($image_url, $post_parent = null, $logger = null) { | |
| 579 | - $filetype = wp_check_filetype(basename($image_url)); | |
| 580 | - if (!$filetype['type']) { | |
| 581 | - if(is_callable($logger)){ | |
| 582 | - // call the logger function | |
| 583 | - call_user_func($logger, 'prepare', 'Error: Unable to determine the file type.', -1, 'eventLog'); | |
| 584 | - } | |
| 585 | - return null; | |
| 586 | - } | |
| 587 | - | |
| 588 | - $post_data = array( | |
| 589 | - 'post_title' => basename($image_url), | |
| 590 | - 'post_content' => '', | |
| 591 | - 'post_status' => 'inherit', | |
| 592 | - 'post_mime_type' => $filetype['type'], | |
| 593 | - 'guid' => $image_url, | |
| 594 | - ); | |
| 595 | - | |
| 596 | - if($post_parent){ | |
| 597 | - $post_data['post_parent'] = $post_parent; // Set the parent post | |
| 598 | - } | |
| 599 | - | |
| 600 | - if (preg_match('%wp-content/uploads/([0-9]{4}/[0-9]{2})%', $image_url, $matches)) { | |
| 601 | - $post_data['upload_date'] = $matches[1]; | |
| 602 | - } | |
| 603 | - else{ | |
| 604 | - $post_data['upload_date'] = date('Y/m'); | |
| 605 | - } | |
| 606 | - | |
| 607 | - return $post_data; | |
| 608 | - } | |
| 609 | - | |
| 610 | - // ============================================================================ | |
| 611 | - // Session Data Functions - DEPRECATED: Use SessionData class instead | |
| 612 | - // ============================================================================ | |
| 613 | - | |
| 614 | - /** | |
| 615 | - * @deprecated 3.4.7 Use SessionData::get_session_id() instead | |
| 616 | - */ | |
| 617 | - public static function get_session_id(){ | |
| 618 | - _deprecated_function(__METHOD__, '3.4.7', 'SessionData::get_session_id()'); | |
| 619 | - return SessionData::get_session_id(); | |
| 620 | - } | |
| 621 | - | |
| 622 | - /** | |
| 623 | - * @deprecated 3.4.7 Use SessionData::save() or SessionData::set() instead | |
| 624 | - */ | |
| 625 | - public static function update_session_data_by_id($data): bool { | |
| 626 | - _deprecated_function(__METHOD__, '3.4.7', 'SessionData::save() or SessionData::set()'); | |
| 627 | - if($session_id = SessionData::get_session_id()){ | |
| 628 | - $existing = SessionData::get_data($session_id); | |
| 629 | - return SessionData::save($session_id, array_merge($existing, $data)); | |
| 630 | - } | |
| 631 | - return false; | |
| 632 | - } | |
| 633 | - | |
| 634 | - /** | |
| 635 | - * @deprecated 3.4.7 Use SessionData::clean_by_pack_id() instead | |
| 636 | - */ | |
| 637 | - public static function clean_session_data_by_pack_id($pack_id, $current_session_id) { | |
| 638 | - _deprecated_function(__METHOD__, '3.4.7', 'SessionData::clean_by_pack_id()'); | |
| 639 | - return SessionData::clean_by_pack_id($pack_id, $current_session_id); | |
| 640 | - } | |
| 641 | - | |
| 642 | - /** | |
| 643 | - * @deprecated 3.4.7 Use SessionData::cleanup_expired() instead | |
| 644 | - */ | |
| 645 | - public static function cleanup_expired_sessions($max_age_days = 7) { | |
| 646 | - _deprecated_function(__METHOD__, '3.4.7', 'SessionData::cleanup_expired()'); | |
| 647 | - return SessionData::cleanup_expired($max_age_days); | |
| 648 | - } | |
| 649 | - | |
| 650 | - | |
| 651 | - | |
| 652 | - /** | |
| 653 | - * Clean up directory using RecursiveIteratorIterator approach | |
| 654 | - * This method handles directory cleanup with proper validation | |
| 655 | - * | |
| 656 | - * @param string $dir_path The directory path to clean up | |
| 657 | - * @return bool True on success, false on failure | |
| 658 | - */ | |
| 659 | - public static function cleanup_directory($dir_path) { | |
| 660 | - if (empty($dir_path) || !file_exists($dir_path) || !is_dir($dir_path)) { | |
| 661 | - return false; | |
| 662 | - } | |
| 663 | - | |
| 664 | - try { | |
| 665 | - $files = new \RecursiveIteratorIterator( | |
| 666 | - new \RecursiveDirectoryIterator($dir_path, \RecursiveDirectoryIterator::SKIP_DOTS), | |
| 667 | - \RecursiveIteratorIterator::CHILD_FIRST | |
| 668 | - ); | |
| 669 | - | |
| 670 | - foreach ($files as $fileinfo) { | |
| 671 | - $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); | |
| 672 | - $todo($fileinfo->getRealPath()); | |
| 673 | - } | |
| 674 | - | |
| 675 | - rmdir($dir_path); | |
| 676 | - return true; | |
| 677 | - } catch (Exception $e) { | |
| 678 | - return false; | |
| 679 | - } | |
| 680 | - } | |
| 681 | - | |
| 682 | -} | |
| 195 | +} | |