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