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
class-foogallery-import-export.php
240 lines
| 1 | <?php |
| 2 | /** |
| 3 | * FooGallery - Import Export Class |
| 4 | */ |
| 5 | |
| 6 | if ( ! class_exists( 'FooGallery_Import_Export' ) ) { |
| 7 | |
| 8 | require_once plugin_dir_path( __FILE__ ) . 'functions.php'; |
| 9 | |
| 10 | /** |
| 11 | * Class FooGallery_Import_Export |
| 12 | */ |
| 13 | class FooGallery_Import_Export { |
| 14 | |
| 15 | /** |
| 16 | * Wire up everything we need to run the extension |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | if ( is_admin() ) { |
| 20 | //add_action( 'add_meta_boxes_foogallery', array( $this, 'add_export_metabox' ) ); |
| 21 | add_action( 'foogallery_admin_menu_after', array( $this, 'add_import_export_menu' ) ); |
| 22 | add_action( 'wp_ajax_foogallery_gallery_export', array( $this, 'ajax_generate_export' ) ); |
| 23 | add_action( 'wp_ajax_foogallery_gallery_import', array( $this, 'ajax_import_galleries' ) ); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Output a log message |
| 29 | * |
| 30 | * @param string $message The message to show. |
| 31 | * @param bool $line_break If a line break should also be output. |
| 32 | */ |
| 33 | public function log_message( $message, $line_break = true ) { |
| 34 | echo esc_html( $message ); |
| 35 | if ( $line_break ) { |
| 36 | echo '<br />'; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Import galleries |
| 42 | */ |
| 43 | public function ajax_import_galleries() { |
| 44 | if ( check_admin_referer( 'foogallery_gallery_import' ) ) { |
| 45 | if ( isset( $_POST['data'] ) ) { |
| 46 | if ( empty( $_POST['data'] ) ) { |
| 47 | echo esc_html( __( 'No import data provided!', 'foogallery' ) ); |
| 48 | } else { |
| 49 | |
| 50 | $galleries = json_decode( wp_unslash( $_POST['data'] ), true ); |
| 51 | |
| 52 | if ( null === $galleries ) { |
| 53 | echo esc_html( __( 'The import data could not be interpreted.', 'foogallery' ) ); |
| 54 | } else { |
| 55 | $gallery_count = 0; |
| 56 | $attachment_count = 0; |
| 57 | $new_attachments = array(); |
| 58 | foreach ( $galleries as $gallery ) { |
| 59 | $datasource_name = $gallery['datasource_name']; |
| 60 | $gallery_id = $gallery['ID']; |
| 61 | $name = $gallery['name']; |
| 62 | $template = $gallery['template']; |
| 63 | |
| 64 | $this->log_message( sprintf( __( 'Import started for gallery ID : %1$s; Name : "%2$s"; Template : %3$s.', 'foogallery' ), $gallery_id, $name, $template ) ); |
| 65 | |
| 66 | // Check if we have already imported the gallery. |
| 67 | $search_gallery = FooGallery::get_by_id( $gallery_id ); |
| 68 | // A match is made if the ID, name and template are the same, and also it's published! |
| 69 | if ( $search_gallery->ID === intval( $gallery_id ) && $search_gallery->gallery_template === $template && $search_gallery->name === $name && 'publish' === $search_gallery->post_status ) { |
| 70 | $this->log_message( __( 'Gallery already exists so skipping.', 'foogallery' ) ); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | $media_library_datasource = 'media_library' === $datasource_name; |
| 75 | |
| 76 | if ( $media_library_datasource ) { |
| 77 | // We need to import attachments! |
| 78 | foreach ( $gallery['attachments'] as $attachment_id => $attachment ) { |
| 79 | $this->log_message( sprintf( __( 'Importing attachment from URL : %s ... ', 'foogallery' ), $attachment['url'] ), false ); |
| 80 | // Try to find the image. |
| 81 | $imported_attachment_id = $this->find_attachment( $attachment['url'] ); |
| 82 | if ( 0 === $imported_attachment_id ) { |
| 83 | // Import the attachment into the media library. |
| 84 | |
| 85 | $imported_attachment_id = foogallery_import_attachment( $attachment ); |
| 86 | if ( is_wp_error( $imported_attachment_id ) ) { |
| 87 | $this->log_message( sprintf( __( 'error : %s.', 'foogallery' ), $imported_attachment_id->get_error_message() ) ); |
| 88 | $imported_attachment_id = 0; |
| 89 | } else { |
| 90 | $this->log_message( sprintf( __( 'success! ID : %s.', 'foogallery' ), $imported_attachment_id ) ); |
| 91 | $attachment_count ++; |
| 92 | } |
| 93 | } else { |
| 94 | $this->log_message( __( 'already exists so skipping.', 'foogallery' ) ); |
| 95 | } |
| 96 | if ( !is_wp_error( $imported_attachment_id ) && intval( $imported_attachment_id ) > 0 ) { |
| 97 | $new_attachments[ $attachment_id ] = $imported_attachment_id; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | $gallery_data = array( |
| 103 | 'post_title' => $name, |
| 104 | 'post_status' => 'publish', |
| 105 | 'post_type' => FOOGALLERY_CPT_GALLERY, |
| 106 | 'meta_input' => array( |
| 107 | FOOGALLERY_META_TEMPLATE => $template, |
| 108 | FOOGALLERY_META_SETTINGS => $gallery['settings'], |
| 109 | FOOGALLERY_META_SORT => $gallery['sorting'], |
| 110 | FOOGALLERY_META_DATASOURCE => $datasource_name, |
| 111 | FOOGALLERY_META_RETINA => $gallery['retina'], |
| 112 | FOOGALLERY_META_CUSTOM_CSS => $gallery['custom_css'], |
| 113 | ), |
| 114 | ); |
| 115 | |
| 116 | if ( $media_library_datasource ) { |
| 117 | $new_attachment_ids = array(); |
| 118 | foreach ( $gallery['attachment_ids'] as $old_attachment_id ) { |
| 119 | if ( array_key_exists( $old_attachment_id, $new_attachments ) ) { |
| 120 | $new_attachment_ids[] = $new_attachments[ $old_attachment_id ]; |
| 121 | } |
| 122 | } |
| 123 | $gallery_data['meta_input'][ FOOGALLERY_META_ATTACHMENTS ] = $new_attachment_ids; |
| 124 | } else { |
| 125 | $gallery_data['meta_input'][ FOOGALLERY_META_DATASOURCE_VALUE ] = $gallery['datasource_value']; |
| 126 | } |
| 127 | |
| 128 | $new_gallery_id = wp_insert_post( $gallery_data, true ); |
| 129 | |
| 130 | if ( ! is_wp_error( $new_gallery_id ) ) { |
| 131 | $gallery_count ++; |
| 132 | $this->log_message( sprintf( __( 'Success! Gallery successfully created. Gallery ID : %s.', 'foogallery' ), $new_gallery_id ) ); |
| 133 | } else { |
| 134 | $this->log_message( sprintf( __( 'Error! Could not create gallery. Error : %s.', 'foogallery' ), $new_gallery_id->get_error_message() ) ); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | wp_die(); |
| 142 | } |
| 143 | |
| 144 | public function find_attachment( $url ) { |
| 145 | $imported_attachment_id = attachment_url_to_postid( $url ); |
| 146 | if ( $imported_attachment_id === 0 ) { |
| 147 | $found_attachments = get_posts( array( |
| 148 | 'post_type' => 'attachment', |
| 149 | 'meta_key' => '_foogallery_imported_from', |
| 150 | 'meta_value' => $url, |
| 151 | ) ); |
| 152 | |
| 153 | if ( is_array( $found_attachments ) && count( $found_attachments ) > 0 ) { |
| 154 | $imported_attachment_id = $found_attachments[0]->ID; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return $imported_attachment_id; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Generate the export data |
| 163 | */ |
| 164 | public function ajax_generate_export() { |
| 165 | if ( check_admin_referer( 'foogallery_gallery_export' ) ) { |
| 166 | if ( isset( $_POST['galleries'] ) ) { |
| 167 | $galleries = array_map( 'sanitize_text_field', wp_unslash( $_POST['galleries'] ) ); |
| 168 | echo foogallery_generate_export_json( $galleries ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON output |
| 169 | } |
| 170 | } |
| 171 | die(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Registers the test menu and page |
| 176 | */ |
| 177 | public function add_import_export_menu() { |
| 178 | foogallery_add_submenu_page( |
| 179 | __( 'Import / Export', 'foogallery' ), |
| 180 | 'manage_options', |
| 181 | 'foogallery_import_export', |
| 182 | array( $this, 'render_import_export_page' ) |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Renders Import / Export page |
| 188 | */ |
| 189 | public function render_import_export_page() { |
| 190 | require_once 'class-foogallery-export-view-helper.php'; |
| 191 | require_once 'class-foogallery-import-view-helper.php'; |
| 192 | require_once 'view-import-export.php'; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Add a metabox to the gallery for exporting |
| 197 | * |
| 198 | * @param WP_Post $post the post we are dealing with. |
| 199 | */ |
| 200 | public function add_export_metabox( $post ) { |
| 201 | add_meta_box( |
| 202 | 'foogallery_export', |
| 203 | __( 'Export', 'foogallery' ), |
| 204 | array( $this, 'render_export_metabox' ), |
| 205 | FOOGALLERY_CPT_GALLERY, |
| 206 | 'normal', |
| 207 | 'low' |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Render the export metabox on the gallery edit page |
| 213 | * |
| 214 | * @param WP_Post $post the post we are dealing with. |
| 215 | */ |
| 216 | public function render_export_metabox( $post ) { |
| 217 | $export = foogallery_generate_export_json( $post->ID ); |
| 218 | ?> |
| 219 | <style> |
| 220 | .foogallery_metabox_export { |
| 221 | width: 100%; |
| 222 | height: 50em; |
| 223 | } |
| 224 | </style> |
| 225 | <p> |
| 226 | <?php echo esc_html( __( 'Below is a JSON export of your gallery.', 'foogallery' ) ); ?> |
| 227 | </p> |
| 228 | <table id="table_styling" class="form-table"> |
| 229 | <tbody> |
| 230 | <tr> |
| 231 | <td> |
| 232 | <textarea class="foogallery_metabox_export" type="text"><?php echo esc_html( $export ); ?></textarea> |
| 233 | </td> |
| 234 | </tr> |
| 235 | </tbody> |
| 236 | </table> |
| 237 | <?php |
| 238 | } |
| 239 | } |
| 240 | } |