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