| @@ -101,43 +101,17 @@ | ||
| 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 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); | |
| 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 ); | |
| 120 | 110 | } |
| 121 | - | |
| 122 | - return $templately_options; | |
| 111 | + return update_option( $key, $value, $autoload ); | |
| 123 | 112 | } |
| 124 | 113 | |
| 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 | - | |
| 140 | 114 | public static function import_page_settings( $id, $settings ) { |
| 141 | 115 | $extra_settings = [ |
| 142 | 116 | 'page_on_front' => [ |
| 143 | 117 | 'show_on_front' => 'page' |
| @@ -160,102 +134,5 @@ | ||
| 160 | 134 | } |
| 161 | 135 | } |
| 162 | 136 | } |
| 163 | 137 | } |
| 164 | - | |
| 165 | - public static function upload_logo($url) { | |
| 166 | - if(empty($url)) { | |
| 167 | - return ['error' => __('URL is empty', 'templately')]; | |
| 168 | - } | |
| 169 | - | |
| 170 | - // Validate URL and ensure scheme is present | |
| 171 | - if ( ! wp_http_validate_url( $url ) || !parse_url( $url, PHP_URL_SCHEME ) ) { | |
| 172 | - return ['error' => __('Invalid URL', 'templately')]; | |
| 173 | - } | |
| 174 | - | |
| 175 | - // Download image and get MIME type | |
| 176 | - $temp_file = download_url( $url ); | |
| 177 | - if ( is_wp_error( $temp_file ) ) { | |
| 178 | - return ['error' => __('Failed to download image', 'templately')]; | |
| 179 | - } | |
| 180 | - | |
| 181 | - // Validate image type using wp_get_image_mime | |
| 182 | - $mime_type = wp_get_image_mime( $temp_file ); | |
| 183 | - if ( ! $mime_type || !in_array( $mime_type, get_allowed_mime_types() ) ) { | |
| 184 | - unlink( $temp_file ); | |
| 185 | - return ['error' => __('Invalid image type', 'templately')]; | |
| 186 | - } | |
| 187 | - | |
| 188 | - $file_info = wp_check_filetype_and_ext( $temp_file, basename( $url ), get_allowed_mime_types() ); | |
| 189 | - if ( ! $file_info['ext'] || $file_info['type'] !== $mime_type ) { | |
| 190 | - unlink( $temp_file ); | |
| 191 | - return ['error' => __('File type and extension check failed', 'templately')]; | |
| 192 | - } | |
| 193 | - | |
| 194 | - // Prepare the file for sideloading | |
| 195 | - $file = array( | |
| 196 | - 'name' => basename($url), | |
| 197 | - 'type' => $mime_type, | |
| 198 | - 'tmp_name' => $temp_file, | |
| 199 | - 'error' => 0, | |
| 200 | - 'size' => filesize($temp_file), | |
| 201 | - ); | |
| 202 | - | |
| 203 | - // Load the WordPress media handler | |
| 204 | - require_once(ABSPATH . 'wp-admin/includes/media.php'); | |
| 205 | - require_once(ABSPATH . 'wp-admin/includes/file.php'); | |
| 206 | - require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
| 207 | - | |
| 208 | - // Handle the sideload | |
| 209 | - $id = media_handle_sideload($file, 0); | |
| 210 | - | |
| 211 | - if (is_wp_error($id)) { | |
| 212 | - @unlink($file['tmp_name']); | |
| 213 | - return ['error' => __('Failed to sideload image', 'templately')]; | |
| 214 | - } | |
| 215 | - | |
| 216 | - return [ | |
| 217 | - 'id' => $id, | |
| 218 | - 'url' => esc_url_raw(wp_get_attachment_url($id)), | |
| 219 | - ]; | |
| 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 | - } | |
| 261 | 138 | } |