| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Utils; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Utils\Base; |
| 7 |
|
| 8 |
class Utils extends Base { |
| 9 |
/** |
| 10 |
* @throws Exception |
| 11 |
*/ |
| 12 |
public static function read_json_file( $path ) { |
| 13 |
if ( ! file_exists( $path ) ) { |
| 14 |
throw new Exception( __( 'JSON file not exists. ' . basename( $path ), 'templately' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
$file_content = self::file_get_contents( $path ); |
| 18 |
|
| 19 |
return $file_content ? json_decode( $file_content, true ) : []; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @param $file |
| 24 |
* @param mixed ...$args |
| 25 |
* |
| 26 |
* @return false|string |
| 27 |
*/ |
| 28 |
public static function file_get_contents( $file, ...$args ) { |
| 29 |
if ( ! is_file( $file ) || ! is_readable( $file ) ) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
return file_get_contents( $file, ...$args ); |
| 34 |
} |
| 35 |
|
| 36 |
public static function get_builtin_wp_post_types(): array { |
| 37 |
$post_type_args = [ |
| 38 |
'show_in_nav_menus' => true, |
| 39 |
'public' => true |
| 40 |
]; |
| 41 |
$_post_types = get_post_types( $post_type_args, 'objects' ); |
| 42 |
|
| 43 |
return array_merge( array_keys( $_post_types ), [ 'nav_menu_item', 'wp_navigation' ] ); |
| 44 |
} |
| 45 |
|
| 46 |
public static function map_old_new_post_ids( array $imported_data ) { |
| 47 |
$result = []; |
| 48 |
|
| 49 |
$result += $imported_data['templates']['succeed'] ?? []; |
| 50 |
|
| 51 |
if ( isset( $imported_data['content'] ) ) { |
| 52 |
foreach ( $imported_data['content'] as $post_type ) { |
| 53 |
$result += $post_type['succeed'] ?? []; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if ( isset( $imported_data['wp-content'] ) ) { |
| 58 |
foreach ( $imported_data['wp-content'] as $post_type ) { |
| 59 |
$result += $post_type['succeed'] ?? []; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return $result; |
| 64 |
} |
| 65 |
|
| 66 |
public static function map_old_new_term_ids( array $imported_data ) { |
| 67 |
$result = []; |
| 68 |
|
| 69 |
if ( isset( $imported_data['terms'] ) ) { |
| 70 |
foreach ( $imported_data['terms'] as $post_type ) { |
| 71 |
$result += $post_type['succeed'] ?? []; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return $result; |
| 76 |
} |
| 77 |
|
| 78 |
public static function map_old_new_term_ids_el( array $imported_data ): array { |
| 79 |
$result = []; |
| 80 |
|
| 81 |
if ( ! isset( $imported_data['taxonomies'] ) ) { |
| 82 |
return $result; |
| 83 |
} |
| 84 |
|
| 85 |
foreach ( $imported_data['taxonomies'] as $post_type_taxonomies ) { |
| 86 |
foreach ( $post_type_taxonomies as $taxonomy ) { |
| 87 |
foreach ( $taxonomy as $term ) { |
| 88 |
$result[ $term['old_id'] ] = $term['new_id']; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return $result; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @param string $platform |
| 98 |
* |
| 99 |
* @return ImportHelper |
| 100 |
*/ |
| 101 |
public static function get_json_helper( string $platform ) { |
| 102 |
return $platform === 'elementor' ? new ElementorHelper() : new GutenbergHelper(); |
| 103 |
} |
| 104 |
|
| 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 ); |
| 110 |
} |
| 111 |
return update_option( $key, $value, $autoload ); |
| 112 |
} |
| 113 |
|
| 114 |
public static function import_page_settings( $id, $settings ) { |
| 115 |
$extra_settings = [ |
| 116 |
'page_on_front' => [ |
| 117 |
'show_on_front' => 'page' |
| 118 |
] |
| 119 |
]; |
| 120 |
if ( isset( $settings['page_for_posts'] ) && $settings['page_for_posts'] ) { |
| 121 |
self::update_option( 'page_for_posts', $id ); |
| 122 |
} |
| 123 |
if ( isset( $settings['show_on_front'] ) && $settings['show_on_front'] ) { |
| 124 |
self::update_option( 'page_on_front', $id ); |
| 125 |
self::update_option( 'show_on_front', 'page' ); |
| 126 |
} |
| 127 |
if ( ! empty( $settings['page_settings'] ) ) { |
| 128 |
foreach ( $settings['page_settings'] as $option_name => $val ) { |
| 129 |
self::update_option( $option_name, $id ); |
| 130 |
if ( array_key_exists( $option_name, $extra_settings ) ) { |
| 131 |
foreach ( $extra_settings[ $option_name ] as $name => $value ) { |
| 132 |
self::update_option( $name, $value ); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |