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