| @@ -101,17 +101,43 @@ | ||
| 101 | 101 | public static function get_json_helper( string $platform ) { |
| 102 | 102 | return $platform === 'elementor' ? new ElementorHelper() : new GutenbergHelper(); |
| 103 | 103 | } |
| 104 | 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 ); | |
| 105 | + public static function get_backup_options() { | |
| 106 | + global $wpdb; | |
| 107 | + | |
| 108 | + $prefix = '__templately_'; | |
| 109 | + $table_name = $wpdb->options; // Assuming default options table name | |
| 110 | + | |
| 111 | + $sql = "SELECT option_name, option_value FROM {$table_name} WHERE option_name LIKE %s"; | |
| 112 | + $prepared_sql = $wpdb->prepare($sql, array("$prefix%")); // Escape wildcard for security | |
| 113 | + | |
| 114 | + $results = $wpdb->get_results($prepared_sql); | |
| 115 | + | |
| 116 | + $templately_options = array(); | |
| 117 | + foreach ($results as $row) { | |
| 118 | + $name = str_replace($prefix, '', $row->option_name); | |
| 119 | + $templately_options[$name] = maybe_unserialize($row->option_value); | |
| 110 | 120 | } |
| 111 | - return update_option( $key, $value, $autoload ); | |
| 121 | + | |
| 122 | + return $templately_options; | |
| 112 | 123 | } |
| 113 | 124 | |
| 125 | + public static function backup_option_value($key, $autoload = 'no') { | |
| 126 | + $old_value = get_option($key); | |
| 127 | + if ($old_value) { | |
| 128 | + update_option("__templately_$key", $old_value, $autoload); | |
| 129 | + } | |
| 130 | + else { | |
| 131 | + add_option("__templately_$key", $old_value, '', $autoload); | |
| 132 | + } | |
| 133 | + } | |
| 134 | + | |
| 135 | + public static function update_option($key, $value, $autoload = 'no') { | |
| 136 | + self::backup_option_value($key, $autoload); | |
| 137 | + return update_option($key, $value, $autoload); | |
| 138 | + } | |
| 139 | + | |
| 114 | 140 | public static function import_page_settings( $id, $settings ) { |
| 115 | 141 | $extra_settings = [ |
| 116 | 142 | 'page_on_front' => [ |
| 117 | 143 | 'show_on_front' => 'page' |
| @@ -191,5 +217,45 @@ | ||
| 191 | 217 | 'id' => $id, |
| 192 | 218 | 'url' => esc_url_raw(wp_get_attachment_url($id)), |
| 193 | 219 | ]; |
| 194 | 220 | } |
| 221 | + | |
| 222 | + /** | |
| 223 | + * Inserts a template into the Gutenberg editor. | |
| 224 | + * | |
| 225 | + * @param mixed $data | |
| 226 | + * @param int $postId | |
| 227 | + * @return array | |
| 228 | + */ | |
| 229 | + public static function import_and_replace_attachments($content, $postId = 0) { | |
| 230 | + // Instantiate GutenbergHelper | |
| 231 | + $helper = new GutenbergHelper(); | |
| 232 | + | |
| 233 | + $data = [ | |
| 234 | + 'content' => $content, | |
| 235 | + ]; | |
| 236 | + | |
| 237 | + // Organize URLs from the content | |
| 238 | + $organizedUrls = $helper->parse_images($data['content']); | |
| 239 | + if(empty($organizedUrls)){ | |
| 240 | + return $content; | |
| 241 | + } | |
| 242 | + | |
| 243 | + // Define template settings | |
| 244 | + $template_settings = [ | |
| 245 | + 'post_id' => $postId, | |
| 246 | + '__attachments' => $organizedUrls, | |
| 247 | + ]; | |
| 248 | + | |
| 249 | + // Map post IDs and disable logging | |
| 250 | + $helper->map_post_ids[$postId] = $postId; | |
| 251 | + $helper->shouldLog = false; | |
| 252 | + | |
| 253 | + // Prepare the helper with the data and settings | |
| 254 | + $helper->prepare($data, $template_settings); | |
| 255 | + | |
| 256 | + // Update the content in the data array | |
| 257 | + $content = wp_unslash($helper->get_content()); | |
| 258 | + | |
| 259 | + return $content; | |
| 260 | + } | |
| 195 | 261 | } |