| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Utils; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Core\Importer\FullSiteImport; |
| 7 |
use Templately\Core\Importer\WPImport; |
| 8 |
use Templately\Utils\Base; |
| 9 |
use Templately\Utils\Helper; |
| 10 |
|
| 11 |
class Utils extends Base { |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* @throws Exception |
| 16 |
*/ |
| 17 |
public static function read_json_file( $path ) { |
| 18 |
if ( ! file_exists( $path ) ) { |
| 19 |
throw new Exception( __( 'JSON file not exists. ' . basename( $path ), 'templately' ) ); |
| 20 |
} |
| 21 |
|
| 22 |
$file_content = self::file_get_contents( $path ); |
| 23 |
|
| 24 |
return $file_content ? json_decode( $file_content, true ) : []; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @param $file |
| 29 |
* @param mixed ...$args |
| 30 |
* |
| 31 |
* @return false|string |
| 32 |
*/ |
| 33 |
public static function file_get_contents( $file, ...$args ) { |
| 34 |
if ( ! is_file( $file ) || ! is_readable( $file ) ) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
return file_get_contents( $file, ...$args ); |
| 39 |
} |
| 40 |
|
| 41 |
public static function get_builtin_wp_post_types(): array { |
| 42 |
$post_type_args = [ |
| 43 |
'show_in_nav_menus' => true, |
| 44 |
'public' => true |
| 45 |
]; |
| 46 |
$_post_types = get_post_types( $post_type_args, 'objects' ); |
| 47 |
|
| 48 |
return array_merge( array_keys( $_post_types ), [ 'nav_menu_item', 'wp_navigation' ] ); |
| 49 |
} |
| 50 |
|
| 51 |
public static function map_old_new_post_ids( array $imported_data ) { |
| 52 |
$result = []; |
| 53 |
|
| 54 |
$result += $imported_data['templates']['succeed'] ?? []; |
| 55 |
|
| 56 |
if ( isset( $imported_data['content'] ) ) { |
| 57 |
foreach ( $imported_data['content'] as $post_type ) { |
| 58 |
$result += $post_type['succeed'] ?? []; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if ( isset( $imported_data['wp-content'] ) ) { |
| 63 |
foreach ( $imported_data['wp-content'] as $post_type ) { |
| 64 |
$result += $post_type['succeed'] ?? []; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// add attachments data |
| 69 |
if ( !empty( $imported_data['attachments']['succeed'] ) ) { |
| 70 |
$result += $imported_data['attachments']['succeed'] ?? []; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
return $result; |
| 75 |
} |
| 76 |
|
| 77 |
public static function map_old_new_term_ids( array $imported_data ) { |
| 78 |
$result = []; |
| 79 |
|
| 80 |
if ( isset( $imported_data['terms'] ) ) { |
| 81 |
foreach ( $imported_data['terms'] as $post_type ) { |
| 82 |
$result += $post_type['succeed'] ?? []; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $result; |
| 87 |
} |
| 88 |
|
| 89 |
public static function map_old_new_term_ids_el( array $imported_data ): array { |
| 90 |
$result = []; |
| 91 |
|
| 92 |
if ( ! isset( $imported_data['taxonomies'] ) ) { |
| 93 |
return $result; |
| 94 |
} |
| 95 |
|
| 96 |
foreach ( $imported_data['taxonomies'] as $post_type_taxonomies ) { |
| 97 |
foreach ( $post_type_taxonomies as $taxonomy ) { |
| 98 |
foreach ( $taxonomy as $term ) { |
| 99 |
$result[ $term['old_id'] ] = $term['new_id']; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return $result; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @param string $platform |
| 109 |
* |
| 110 |
* @return ImportHelper |
| 111 |
*/ |
| 112 |
public static function get_json_helper( string $platform ) { |
| 113 |
return $platform === 'elementor' ? new ElementorHelper() : new GutenbergHelper(); |
| 114 |
} |
| 115 |
|
| 116 |
public static function get_backup_options() { |
| 117 |
global $wpdb; |
| 118 |
|
| 119 |
$prefix = '__templately_'; |
| 120 |
$table_name = $wpdb->options; // Assuming default options table name |
| 121 |
|
| 122 |
$sql = "SELECT option_name, option_value FROM {$table_name} WHERE option_name LIKE %s"; |
| 123 |
$prepared_sql = $wpdb->prepare($sql, array("$prefix%")); // Escape wildcard for security |
| 124 |
|
| 125 |
$results = $wpdb->get_results($prepared_sql); |
| 126 |
|
| 127 |
$templately_options = array(); |
| 128 |
foreach ($results as $row) { |
| 129 |
$name = str_replace($prefix, '', $row->option_name); |
| 130 |
$templately_options[$name] = maybe_unserialize($row->option_value); |
| 131 |
} |
| 132 |
|
| 133 |
return $templately_options; |
| 134 |
} |
| 135 |
|
| 136 |
public static function backup_option_value($key, $autoload = 'no') { |
| 137 |
$old_value = get_option($key); |
| 138 |
if ($old_value) { |
| 139 |
update_option("__templately_$key", $old_value, $autoload); |
| 140 |
} |
| 141 |
else { |
| 142 |
add_option("__templately_$key", $old_value, '', $autoload); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
public static function update_option($key, $value, $autoload = 'no') { |
| 147 |
self::backup_option_value($key, $autoload); |
| 148 |
return update_option($key, $value, $autoload); |
| 149 |
} |
| 150 |
|
| 151 |
public static function import_page_settings( $id, $settings ) { |
| 152 |
$extra_settings = [ |
| 153 |
'page_on_front' => [ |
| 154 |
'show_on_front' => 'page' |
| 155 |
] |
| 156 |
]; |
| 157 |
if ( isset( $settings['page_for_posts'] ) && $settings['page_for_posts'] ) { |
| 158 |
self::update_option( 'page_for_posts', $id ); |
| 159 |
} |
| 160 |
if ( isset( $settings['show_on_front'] ) && $settings['show_on_front'] ) { |
| 161 |
self::update_option( 'page_on_front', $id ); |
| 162 |
self::update_option( 'show_on_front', 'page' ); |
| 163 |
} |
| 164 |
if ( ! empty( $settings['page_settings'] ) ) { |
| 165 |
foreach ( $settings['page_settings'] as $option_name => $val ) { |
| 166 |
self::update_option( $option_name, $id ); |
| 167 |
if ( array_key_exists( $option_name, $extra_settings ) ) { |
| 168 |
foreach ( $extra_settings[ $option_name ] as $name => $value ) { |
| 169 |
self::update_option( $name, $value ); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
public static function upload_logo($url, $session_id) { |
| 177 |
if(empty($url)) { |
| 178 |
return ['error' => __('URL is empty', 'templately')]; |
| 179 |
} |
| 180 |
|
| 181 |
// Validate URL and ensure scheme is present |
| 182 |
if ( ! wp_http_validate_url( $url ) || !parse_url( $url, PHP_URL_SCHEME ) ) { |
| 183 |
return ['error' => __('Invalid URL', 'templately')]; |
| 184 |
} |
| 185 |
|
| 186 |
$post_data = self::prepare_post_data($url); |
| 187 |
$wp_importer = new WPImport( null, ['fetch_attachments' => true, 'session_id' => $session_id] ); |
| 188 |
$attachment_id = $wp_importer->process_attachment($post_data, $url); |
| 189 |
|
| 190 |
if(is_wp_error($attachment_id)){ |
| 191 |
return ['error' => $attachment_id->get_error_message()]; |
| 192 |
} |
| 193 |
|
| 194 |
return [ |
| 195 |
'id' => (int) $attachment_id, |
| 196 |
'url' => esc_url_raw(wp_get_attachment_url($attachment_id)), |
| 197 |
]; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Inserts a template into the Gutenberg editor. |
| 202 |
* |
| 203 |
* @param mixed $data |
| 204 |
* @param int $postId |
| 205 |
* @return array |
| 206 |
*/ |
| 207 |
public static function import_and_replace_attachments($content, $postId = 0) { |
| 208 |
// Instantiate GutenbergHelper |
| 209 |
$helper = new GutenbergHelper(); |
| 210 |
|
| 211 |
$data = [ |
| 212 |
'content' => $content, |
| 213 |
]; |
| 214 |
|
| 215 |
// Organize URLs from the content |
| 216 |
$organizedUrls = $helper->parse_images($data['content']); |
| 217 |
if(empty($organizedUrls)){ |
| 218 |
return $content; |
| 219 |
} |
| 220 |
|
| 221 |
// Define template settings |
| 222 |
$template_settings = [ |
| 223 |
'post_id' => $postId, |
| 224 |
'__attachments' => $organizedUrls, |
| 225 |
]; |
| 226 |
|
| 227 |
// Map post IDs and disable logging |
| 228 |
$helper->map_post_ids[$postId] = $postId; |
| 229 |
$helper->shouldLog = false; |
| 230 |
|
| 231 |
// Prepare the helper with the data and settings |
| 232 |
$helper->prepare($data, $template_settings); |
| 233 |
|
| 234 |
// Update the content in the data array |
| 235 |
$content = wp_unslash($helper->get_content()); |
| 236 |
|
| 237 |
return $content; |
| 238 |
} |
| 239 |
|
| 240 |
public static function prepare_post_data($image_url, $post_parent = null, $logger = null) { |
| 241 |
$filetype = wp_check_filetype(basename($image_url)); |
| 242 |
if (!$filetype['type']) { |
| 243 |
if(is_callable($logger)){ |
| 244 |
// call the logger function |
| 245 |
call_user_func($logger, 'prepare', 'Error: Unable to determine the file type.', -1, 'eventLog'); |
| 246 |
} |
| 247 |
return null; |
| 248 |
} |
| 249 |
|
| 250 |
$post_data = array( |
| 251 |
'post_title' => basename($image_url), |
| 252 |
'post_content' => '', |
| 253 |
'post_status' => 'inherit', |
| 254 |
'post_mime_type' => $filetype['type'], |
| 255 |
'guid' => $image_url, |
| 256 |
); |
| 257 |
|
| 258 |
if($post_parent){ |
| 259 |
$post_data['post_parent'] = $post_parent; // Set the parent post |
| 260 |
} |
| 261 |
|
| 262 |
if (preg_match('%wp-content/uploads/([0-9]{4}/[0-9]{2})%', $image_url, $matches)) { |
| 263 |
$post_data['upload_date'] = $matches[1]; |
| 264 |
} |
| 265 |
else{ |
| 266 |
$post_data['upload_date'] = date('Y/m'); |
| 267 |
} |
| 268 |
|
| 269 |
return $post_data; |
| 270 |
} |
| 271 |
|
| 272 |
// Static version of get_session_data |
| 273 |
public static function get_all_session_data(): array { |
| 274 |
$data = get_option(FullSiteImport::SESSION_OPTION_KEY, []); |
| 275 |
return $data ?? []; |
| 276 |
} |
| 277 |
|
| 278 |
// Static version of get_session_data |
| 279 |
public static function get_session_data($session_id): array { |
| 280 |
$data = get_option(FullSiteImport::SESSION_OPTION_KEY, []); |
| 281 |
return $data[$session_id] ?? []; |
| 282 |
} |
| 283 |
|
| 284 |
// Static version of update_session_data |
| 285 |
public static function update_session_data($session_id, $data): bool { |
| 286 |
$old_data = get_option(FullSiteImport::SESSION_OPTION_KEY, []); |
| 287 |
return update_option(FullSiteImport::SESSION_OPTION_KEY, Helper::recursive_wp_parse_args([$session_id => $data], $old_data)); |
| 288 |
} |
| 289 |
|
| 290 |
// Delete specific session data by ID |
| 291 |
public static function delete_session_data($session_id): bool { |
| 292 |
$all_data = get_option(FullSiteImport::SESSION_OPTION_KEY, []); |
| 293 |
|
| 294 |
if (!isset($all_data[$session_id])) { |
| 295 |
return false; // Session ID doesn't exist |
| 296 |
} |
| 297 |
|
| 298 |
unset($all_data[$session_id]); |
| 299 |
return update_option(FullSiteImport::SESSION_OPTION_KEY, $all_data); |
| 300 |
} |
| 301 |
|
| 302 |
public static function get_session_id(){ |
| 303 |
$session_id = null; |
| 304 |
if(!empty($_REQUEST['session_id'])){ |
| 305 |
$session_id = sanitize_text_field($_REQUEST['session_id']); |
| 306 |
} |
| 307 |
return $session_id; |
| 308 |
} |
| 309 |
|
| 310 |
public static function get_session_data_by_id(): array { |
| 311 |
if($session_id = self::get_session_id()){ |
| 312 |
return self::get_session_data($session_id); |
| 313 |
} |
| 314 |
throw new Exception(__('Invalid Session ID.', 'templately')); |
| 315 |
} |
| 316 |
|
| 317 |
public static function update_session_data_by_id($data): array { |
| 318 |
if($session_id = self::get_session_id()){ |
| 319 |
$session_id = self::update_session_data($session_id, $data); |
| 320 |
return $data[$session_id] ?? []; |
| 321 |
} |
| 322 |
throw new Exception(__('Invalid Session ID.', 'templately')); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
/** |
| 328 |
* Clean up directory using RecursiveIteratorIterator approach |
| 329 |
* This method handles directory cleanup with proper validation |
| 330 |
* |
| 331 |
* @param string $dir_path The directory path to clean up |
| 332 |
* @return bool True on success, false on failure |
| 333 |
*/ |
| 334 |
public static function cleanup_directory($dir_path) { |
| 335 |
if (empty($dir_path) || !file_exists($dir_path) || !is_dir($dir_path)) { |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
try { |
| 340 |
$files = new \RecursiveIteratorIterator( |
| 341 |
new \RecursiveDirectoryIterator($dir_path, \RecursiveDirectoryIterator::SKIP_DOTS), |
| 342 |
\RecursiveIteratorIterator::CHILD_FIRST |
| 343 |
); |
| 344 |
|
| 345 |
foreach ($files as $fileinfo) { |
| 346 |
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); |
| 347 |
$todo($fileinfo->getRealPath()); |
| 348 |
} |
| 349 |
|
| 350 |
rmdir($dir_path); |
| 351 |
return true; |
| 352 |
} catch (Exception $e) { |
| 353 |
return false; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
/** |
| 366 |
* Clean session data by pack ID, keeping only the current session |
| 367 |
* Removes all session entries with the same pack_id except the current session |
| 368 |
* |
| 369 |
* @param string $pack_id The pack ID to match for cleanup |
| 370 |
* @param string $current_session_id The current session ID to preserve |
| 371 |
* @return array Array of removed session IDs |
| 372 |
*/ |
| 373 |
public static function clean_session_data_by_pack_id($pack_id, $current_session_id) { |
| 374 |
if (empty($pack_id) || empty($current_session_id)) { |
| 375 |
return []; |
| 376 |
} |
| 377 |
|
| 378 |
$all_session_data = self::get_all_session_data(); |
| 379 |
$removed_session_ids = []; |
| 380 |
|
| 381 |
|
| 382 |
foreach ($all_session_data as $session_id => $session_data) { |
| 383 |
// Skip the current session |
| 384 |
if ($session_id === $current_session_id) { |
| 385 |
continue; |
| 386 |
} |
| 387 |
|
| 388 |
// Remove sessions that have the same pack_id |
| 389 |
if (isset($session_data['id']) && $session_data['id'] === $pack_id) { |
| 390 |
unset($all_session_data[$session_id]); |
| 391 |
$removed_session_ids[] = $session_id; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
// Update the option with cleaned data if any sessions were removed |
| 396 |
if (!empty($removed_session_ids)) { |
| 397 |
update_option(FullSiteImport::SESSION_OPTION_KEY, $all_session_data); |
| 398 |
} |
| 399 |
|
| 400 |
return $removed_session_ids; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
} |
| 406 |
|