VariableProductTitle
7 years ago
.gitkeep
7 years ago
WpaeInvalidPhpException.php
7 years ago
WpaeInvalidStringException.php
7 years ago
WpaeMethodNotFoundException.php
7 years ago
WpaePhpInterpreterErrorHandler.php
7 years ago
WpaeString.php
7 years ago
WpaeTooMuchRecursionException.php
7 years ago
WpaeXmlProcessor.php
7 years ago
XmlCsvExport.php
7 years ago
XmlExportACF.php
7 years ago
XmlExportComment.php
7 years ago
XmlExportCpt.php
7 years ago
XmlExportEngine.php
7 years ago
XmlExportFiltering.php
7 years ago
XmlExportMediaGallery.php
7 years ago
XmlExportTaxonomy.php
7 years ago
XmlExportUser.php
7 years ago
XmlExportWooCommerce.php
7 years ago
XmlExportWooCommerceCoupon.php
7 years ago
XmlExportWooCommerceOrder.php
7 years ago
XmlGoogleMerchants.php
7 years ago
XmlSpec.php
7 years ago
XmlExportMediaGallery.php
367 lines
| 1 | <?php |
| 2 | |
| 3 | final class XmlExportMediaGallery |
| 4 | { |
| 5 | /** |
| 6 | * Singletone instance |
| 7 | * @var XmlExportMediaGallery |
| 8 | */ |
| 9 | protected static $instance; |
| 10 | |
| 11 | /** |
| 12 | * Return singletone instance |
| 13 | * @return XmlExportMediaGallery |
| 14 | */ |
| 15 | static public function getInstance( $pid ) { |
| 16 | if ( self::$instance == NULL or self::$pid != $pid ) { |
| 17 | self::$instance = new self( $pid ); |
| 18 | } |
| 19 | return self::$instance; |
| 20 | } |
| 21 | |
| 22 | public static $pid = false; |
| 23 | |
| 24 | public static $attachments = array(); |
| 25 | public static $images = array(); |
| 26 | public static $images_ids = array(); |
| 27 | |
| 28 | public static $featured_image = false; |
| 29 | |
| 30 | private function __construct( $pid ) |
| 31 | { |
| 32 | self::$pid = $pid; |
| 33 | } |
| 34 | |
| 35 | public static function init( $type = 'attachments', $options = false ) |
| 36 | { |
| 37 | self::$attachments = array(); |
| 38 | self::$images = array(); |
| 39 | self::$images_ids = array(); |
| 40 | self::$featured_image = false; |
| 41 | |
| 42 | switch ($type) |
| 43 | { |
| 44 | case 'attachments': |
| 45 | |
| 46 | $attachments = get_posts( array( |
| 47 | 'post_type' => 'attachment', |
| 48 | 'posts_per_page' => -1, |
| 49 | 'post_parent' => self::$pid, |
| 50 | 'orderby' => 'ID', |
| 51 | 'order' => 'ASC' |
| 52 | ) ); |
| 53 | |
| 54 | if ( ! empty($attachments)): |
| 55 | |
| 56 | foreach ($attachments as $attachment) |
| 57 | { |
| 58 | if ( ! wp_attachment_is_image( $attachment->ID ) ) |
| 59 | { |
| 60 | self::$attachments[] = $attachment; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | endif; |
| 65 | |
| 66 | break; |
| 67 | |
| 68 | case 'images': |
| 69 | |
| 70 | // prepare featured image data |
| 71 | if ( empty(self::$featured_image) ) |
| 72 | { |
| 73 | $_featured_image_id = self::get_meta(self::$pid, '_thumbnail_id', true); |
| 74 | |
| 75 | if (empty($_featured_image_id)){ |
| 76 | $_featured_image_id = self::get_meta(self::$pid, 'thumbnail_id', true); |
| 77 | } |
| 78 | |
| 79 | if ( ! empty($_featured_image_id) ) |
| 80 | { |
| 81 | $_featured_image = get_post($_featured_image_id); |
| 82 | |
| 83 | if ($_featured_image) |
| 84 | { |
| 85 | self::$featured_image = $_featured_image; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if ( ! empty(self::$featured_image) and ( empty($options) or ! empty($options['is_export_featured']) ) and ! in_array(self::$featured_image->ID, self::$images_ids)) |
| 91 | { |
| 92 | self::$images_ids[] = self::$featured_image->ID; |
| 93 | self::$images[] = self::$featured_image; |
| 94 | } |
| 95 | |
| 96 | // prepare attached images data |
| 97 | if ( empty($options) or ! empty($options['is_export_attached']) ) |
| 98 | { |
| 99 | |
| 100 | $_gallery = self::get_meta(self::$pid, '_product_image_gallery', true); |
| 101 | |
| 102 | if ( ! empty($_gallery)) |
| 103 | { |
| 104 | $gallery = explode(',', $_gallery); |
| 105 | |
| 106 | if ( ! empty($gallery) and is_array($gallery)) |
| 107 | { |
| 108 | foreach ($gallery as $aid) |
| 109 | { |
| 110 | if ( ! empty($aid) and ! in_array($aid, self::$images_ids) and ( empty(self::$featured_image) or self::$featured_image->ID != $aid ) ) |
| 111 | { |
| 112 | $_image = get_post($aid); |
| 113 | if ($_image) |
| 114 | { |
| 115 | self::$images_ids[] = $aid; |
| 116 | self::$images[] = $_image; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | $images = get_posts( array( |
| 124 | 'post_type' => 'attachment', |
| 125 | 'posts_per_page' => -1, |
| 126 | 'post_parent' => self::$pid, |
| 127 | 'orderby' => 'ID', |
| 128 | 'order' => 'ASC' |
| 129 | ) ); |
| 130 | |
| 131 | if ( ! empty($images)): |
| 132 | |
| 133 | foreach ($images as $image) |
| 134 | { |
| 135 | if ( wp_attachment_is_image( $image->ID ) and ! in_array($image->ID, self::$images_ids) and ( empty(self::$featured_image) or self::$featured_image->ID != $image->ID ) ) |
| 136 | { |
| 137 | self::$images[] = $image; |
| 138 | self::$images_ids[] = $image->ID; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | endif; |
| 143 | } |
| 144 | |
| 145 | break; |
| 146 | |
| 147 | default: |
| 148 | # code... |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | public static function get_attachments ( $field = 'attachment_url' ) |
| 154 | { |
| 155 | self::init('attachments'); |
| 156 | |
| 157 | $data = array(); |
| 158 | |
| 159 | if ( ! empty(self::$attachments) ) |
| 160 | { |
| 161 | foreach (self::$attachments as $attachment) |
| 162 | { |
| 163 | $v = self::get_media( str_replace("attachment_", "", $field), $attachment ); |
| 164 | |
| 165 | $data[] = $v; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return $data; |
| 170 | } |
| 171 | |
| 172 | public static function get_images( $field = 'image_url', $options = false ) |
| 173 | { |
| 174 | self::init('images', $options); |
| 175 | |
| 176 | $data = array(); |
| 177 | |
| 178 | switch ($field){ |
| 179 | |
| 180 | case 'image_featured': |
| 181 | $data[] = empty(self::$featured_image) ? '' : wp_get_attachment_url( self::$featured_image->ID ); |
| 182 | break; |
| 183 | default: |
| 184 | if ( ! empty(self::$images) ) |
| 185 | { |
| 186 | foreach (self::$images as $image) |
| 187 | { |
| 188 | $v = self::get_media( str_replace("image_", "", $field), $image ); |
| 189 | |
| 190 | $data[] = $v; |
| 191 | } |
| 192 | } |
| 193 | break; |
| 194 | |
| 195 | } |
| 196 | |
| 197 | return $data; |
| 198 | } |
| 199 | |
| 200 | private static function get_media( $field = 'url', $attachment = false ) |
| 201 | { |
| 202 | if ( empty($attachment)) return false; |
| 203 | |
| 204 | switch ($field) |
| 205 | { |
| 206 | case 'media': |
| 207 | case 'attachments': |
| 208 | case 'url': |
| 209 | return wp_get_attachment_url( $attachment->ID ); |
| 210 | break; |
| 211 | case 'filename': |
| 212 | return basename(wp_get_attachment_url( $attachment->ID )); |
| 213 | break; |
| 214 | case 'path': |
| 215 | return get_attached_file( $attachment->ID ); |
| 216 | break; |
| 217 | case 'id': |
| 218 | return $attachment->ID; |
| 219 | break; |
| 220 | case 'title': |
| 221 | return $attachment->post_title; |
| 222 | break; |
| 223 | case 'caption': |
| 224 | return $attachment->post_excerpt; |
| 225 | break; |
| 226 | case 'description': |
| 227 | return $attachment->post_content; |
| 228 | break; |
| 229 | case 'alt': |
| 230 | return self::get_meta($attachment->ID, '_wp_attachment_image_alt', true); |
| 231 | break; |
| 232 | |
| 233 | default: |
| 234 | # code... |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | public static $is_include_feature_meta = null; |
| 242 | public static $is_include_gallery_meta = null; |
| 243 | |
| 244 | public static function prepare_import_template( $exportOptions, &$templateOptions, $element_name, $ID) |
| 245 | { |
| 246 | $options = $exportOptions; |
| 247 | |
| 248 | $is_xml_template = $options['export_to'] == 'xml'; |
| 249 | |
| 250 | $implode_delimiter = XmlExportEngine::$implode; |
| 251 | |
| 252 | $element_type = $options['cc_type'][$ID]; |
| 253 | |
| 254 | if ( is_null(self::$is_include_feature_meta) || is_null(self::$is_include_gallery_meta)) { |
| 255 | self::$is_include_feature_meta = false; |
| 256 | self::$is_include_gallery_meta = false; |
| 257 | |
| 258 | foreach ($options['ids'] as $elID => $value) |
| 259 | { |
| 260 | if ( 'image_url' == $options['cc_type'][$elID] ) { |
| 261 | $field_options = json_decode($options['cc_options'][$elID], true); |
| 262 | if ( ! empty($field_options['is_export_featured']) ) self::$is_include_feature_meta = true; |
| 263 | if ( ! empty($field_options['is_export_attached']) ) self::$is_include_gallery_meta = true; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | switch ($element_type) |
| 269 | { |
| 270 | case 'media': |
| 271 | case 'image_url': |
| 272 | $field_options = json_decode($options['cc_options'][$ID], true); |
| 273 | $templateOptions['is_update_images'] = 1; |
| 274 | $templateOptions['update_images_logic'] = 'add_new'; |
| 275 | $templateOptions['download_featured_delim'] = (empty($field_options['image_separator'])) ? "|" : $field_options['image_separator']; |
| 276 | if ( empty($templateOptions['download_featured_image'])) { |
| 277 | $templateOptions['download_featured_image'] = '{'. $element_name .'[1]}'; |
| 278 | } |
| 279 | else { |
| 280 | $templateOptions['download_featured_image'] .= $templateOptions['download_featured_delim'] . '{'. $element_name .'[1]}'; |
| 281 | } |
| 282 | break; |
| 283 | case 'image_title': |
| 284 | $field_options = json_decode($options['cc_options'][$ID], true); |
| 285 | if ( (int) $field_options['is_export_featured'] == (int) self::$is_include_feature_meta && (int) $field_options['is_export_attached'] == (int) self::$is_include_gallery_meta ) |
| 286 | { |
| 287 | $templateOptions['set_image_meta_title'] = 1; |
| 288 | $templateOptions['image_meta_title_delim'] = (empty($field_options['image_separator'])) ? "|" : $field_options['image_separator']; |
| 289 | if ( empty($templateOptions['image_meta_title'])) { |
| 290 | $templateOptions['image_meta_title'] = '{'. $element_name .'[1]}'; |
| 291 | } |
| 292 | else { |
| 293 | $templateOptions['image_meta_title'] .= $templateOptions['image_meta_title_delim'] . '{'. $element_name .'[1]}'; |
| 294 | } |
| 295 | } |
| 296 | break; |
| 297 | case 'image_caption': |
| 298 | $field_options = json_decode($options['cc_options'][$ID], true); |
| 299 | if ( (int) $field_options['is_export_featured'] == (int) self::$is_include_feature_meta && (int) $field_options['is_export_attached'] == (int) self::$is_include_gallery_meta ) |
| 300 | { |
| 301 | $templateOptions['set_image_meta_caption'] = 1; |
| 302 | $templateOptions['image_meta_caption_delim'] = (empty($field_options['image_separator'])) ? "|" : $field_options['image_separator']; |
| 303 | if ( empty($templateOptions['image_meta_caption'])) { |
| 304 | $templateOptions['image_meta_caption'] = '{'. $element_name .'[1]}'; |
| 305 | } |
| 306 | else { |
| 307 | $templateOptions['image_meta_caption'] .= $templateOptions['image_meta_caption_delim'] . '{'. $element_name .'[1]}'; |
| 308 | } |
| 309 | } |
| 310 | break; |
| 311 | case 'image_description': |
| 312 | $field_options = json_decode($options['cc_options'][$ID], true); |
| 313 | if ( (int) $field_options['is_export_featured'] == (int) self::$is_include_feature_meta && (int) $field_options['is_export_attached'] == (int) self::$is_include_gallery_meta ) |
| 314 | { |
| 315 | $templateOptions['set_image_meta_description'] = 1; |
| 316 | $templateOptions['image_meta_description_delim'] = (empty($field_options['image_separator'])) ? "|" : $field_options['image_separator']; |
| 317 | if ( empty($templateOptions['image_meta_description'])) { |
| 318 | $templateOptions['image_meta_description'] = '{'. $element_name .'[1]}'; |
| 319 | } |
| 320 | else { |
| 321 | $templateOptions['image_meta_description'] .= $templateOptions['image_meta_description_delim'] . '{'. $element_name .'[1]}'; |
| 322 | } |
| 323 | } |
| 324 | break; |
| 325 | case 'image_alt': |
| 326 | $field_options = json_decode($options['cc_options'][$ID], true); |
| 327 | if ( (int) $field_options['is_export_featured'] == (int) self::$is_include_feature_meta && (int) $field_options['is_export_attached'] == (int) self::$is_include_gallery_meta ) |
| 328 | { |
| 329 | $templateOptions['set_image_meta_alt'] = 1; |
| 330 | $templateOptions['image_meta_alt_delim'] = (empty($field_options['image_separator'])) ? "|" : $field_options['image_separator']; |
| 331 | if ( empty($templateOptions['image_meta_alt'])) { |
| 332 | $templateOptions['image_meta_alt'] = '{'. $element_name .'[1]}'; |
| 333 | } |
| 334 | else { |
| 335 | $templateOptions['image_meta_alt'] .= $templateOptions['image_meta_alt_delim'] . '{'. $element_name .'[1]}'; |
| 336 | } |
| 337 | } |
| 338 | break; |
| 339 | case 'image_featured': |
| 340 | $templateOptions['is_featured'] = 1; |
| 341 | $templateOptions['is_featured_xpath'] = '{'. $element_name .'[1]}'; |
| 342 | break; |
| 343 | case 'attachments': |
| 344 | case 'attachment_url': |
| 345 | $templateOptions['atch_delim'] = '|'; |
| 346 | $templateOptions['is_update_attachments'] = 1; |
| 347 | if ( empty($templateOptions['attachments'])) { |
| 348 | $templateOptions['attachments'] = '{'. $element_name .'[1]}'; |
| 349 | } |
| 350 | else { |
| 351 | $templateOptions['attachments'] .= $templateOptions['atch_delim'] . '{'. $element_name .'[1]}'; |
| 352 | } |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | } |
| 357 | |
| 358 | public static function get_meta($pid, $key){ |
| 359 | if (XmlExportTaxonomy::$is_active){ |
| 360 | return get_term_meta($pid, $key, true); |
| 361 | } |
| 362 | if (XmlExportUser::$is_active){ |
| 363 | return get_user_meta($pid, $key, true); |
| 364 | } |
| 365 | return get_post_meta($pid, $key, true); |
| 366 | } |
| 367 | } |