class-foogallery-export-view-helper.php
7 months ago
class-foogallery-import-export-extension.php
7 months ago
class-foogallery-import-export.php
7 months ago
class-foogallery-import-view-helper.php
7 months ago
functions.php
7 months ago
view-import-export.php
7 months ago
functions.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Generate the JSON export for a gallery |
| 5 | * |
| 6 | * @param int[] $foogallery_ids The IDs of the gallery to export. |
| 7 | * |
| 8 | * @return string |
| 9 | */ |
| 10 | function foogallery_generate_export_json( $foogallery_ids ) { |
| 11 | global $current_foogallery; |
| 12 | |
| 13 | $exported_galleries = array(); |
| 14 | |
| 15 | if ( ! is_array( $foogallery_ids ) && intval( $foogallery_ids ) > 0 ) { |
| 16 | $foogallery_ids = array( $foogallery_ids ); |
| 17 | } |
| 18 | |
| 19 | foreach ( $foogallery_ids as $foogallery_id ) { |
| 20 | |
| 21 | $current_foogallery = FooGallery::get_by_id( $foogallery_id ); |
| 22 | do_action( 'foogallery_located_template', $current_foogallery ); |
| 23 | |
| 24 | $source_settings = get_post_meta( $foogallery_id, FOOGALLERY_META_SETTINGS, true ); |
| 25 | $source_sorting = get_post_meta( $foogallery_id, FOOGALLERY_META_SORT, true ); |
| 26 | $source_retina = get_post_meta( $foogallery_id, FOOGALLERY_META_RETINA, true ); |
| 27 | $source_custom_css = get_post_meta( $foogallery_id, FOOGALLERY_META_CUSTOM_CSS, true ); |
| 28 | |
| 29 | $export = array( |
| 30 | 'ID' => $foogallery_id, |
| 31 | 'template' => $current_foogallery->gallery_template, |
| 32 | 'name' => $current_foogallery->name, |
| 33 | 'datasource_name' => $current_foogallery->datasource_name, |
| 34 | ); |
| 35 | |
| 36 | if ( 'media_library' === $current_foogallery->datasource_name ) { |
| 37 | $export['attachment_ids'] = $current_foogallery->attachment_ids; |
| 38 | $attachments = array(); |
| 39 | foreach ( $current_foogallery->attachments() as $attachment ) { |
| 40 | |
| 41 | $attachment_object = array( |
| 42 | 'url' => $attachment->url, |
| 43 | 'title' => $attachment->title, |
| 44 | 'caption' => $attachment->caption, |
| 45 | 'description' => $attachment->description, |
| 46 | 'alt' => $attachment->alt, |
| 47 | ); |
| 48 | |
| 49 | if ( ! empty( $attachment->custom_url ) ) { |
| 50 | $attachment_object['custom_url'] = $attachment->custom_url; |
| 51 | } |
| 52 | if ( ! empty( $attachment->custom_target ) ) { |
| 53 | $attachment_object['custom_target'] = $attachment->custom_target; |
| 54 | } |
| 55 | |
| 56 | if ( defined( 'FOOGALLERY_ATTACHMENT_TAXONOMY_TAG' ) ) { |
| 57 | $tags = wp_get_post_terms( $attachment->ID, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, array( 'fields' => 'names' ) ); |
| 58 | if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) { |
| 59 | $attachment_object['tags'] = $tags; |
| 60 | } |
| 61 | |
| 62 | $categories = wp_get_post_terms( $attachment->ID, FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, array( 'fields' => 'names' ) ); |
| 63 | if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) { |
| 64 | $attachment_object['categories'] = $categories; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | $attachments[ $attachment->ID ] = $attachment_object; |
| 69 | } |
| 70 | } else { |
| 71 | $export['datasource_value'] = $current_foogallery->datasource_value; |
| 72 | } |
| 73 | |
| 74 | $export['settings'] = $source_settings; |
| 75 | $export['sorting'] = $source_sorting; |
| 76 | $export['retina'] = $source_retina; |
| 77 | $export['custom_css'] = $source_custom_css; |
| 78 | if ( isset( $attachments ) ) { |
| 79 | $export['attachments'] = $attachments; |
| 80 | } |
| 81 | |
| 82 | $exported_galleries[] = $export; |
| 83 | } |
| 84 | |
| 85 | return foogallery_json_encode( $exported_galleries ); |
| 86 | } |
| 87 |