| 1 |
<?php |
| 2 |
class MaxGalleriaNextGen { |
| 3 |
public $nonce_nextgen_importer = array( |
| 4 |
'action' => 'nextgen_importer', |
| 5 |
'name' => 'maxgalleria_nextgen_importer' |
| 6 |
); |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
// Ajax call for importing a NextGEN gallery |
| 10 |
add_action('wp_ajax_import_nextgen_gallery', array($this, 'import_nextgen_gallery')); |
| 11 |
add_action('wp_ajax_nopriv_import_nextgen_gallery', array($this, 'import_nextgen_gallery')); |
| 12 |
|
| 13 |
// Ajax call for getting the import percent of a NextGEN gallery |
| 14 |
add_action('wp_ajax_get_nextgen_import_percent', array($this, 'get_nextgen_import_percent')); |
| 15 |
add_action('wp_ajax_nopriv_get_nextgen_import_percent', array($this, 'get_nextgen_import_percent')); |
| 16 |
|
| 17 |
// Ajax call for resetting the import percent and count of a NextGEN gallery |
| 18 |
add_action('wp_ajax_reset_nextgen_import', array($this, 'reset_nextgen_import')); |
| 19 |
add_action('wp_ajax_nopriv_reset_nextgen_import', array($this, 'reset_nextgen_import')); |
| 20 |
} |
| 21 |
|
| 22 |
public function import_nextgen_gallery() { |
| 23 |
global $maxgalleria; |
| 24 |
$common = $maxgalleria->common; |
| 25 |
|
| 26 |
if (isset($_POST) && check_admin_referer($this->nonce_nextgen_importer['action'], $this->nonce_nextgen_importer['name'])) { |
| 27 |
$nextgen_import_count = 0; |
| 28 |
$maxgalleria_gallery = null; |
| 29 |
|
| 30 |
$nextgen_gallery_id = $_POST['nextgen_gallery_id']; |
| 31 |
$nextgen_gallery = $this->get_nextgen_gallery($nextgen_gallery_id); |
| 32 |
$nextgen_gallery_pics = $this->get_nextgen_gallery_pictures($nextgen_gallery_id); |
| 33 |
$nextgen_gallery_pics_count = $this->get_nextgen_gallery_picture_count($nextgen_gallery_id); |
| 34 |
|
| 35 |
do_action(MAXGALLERIA_ACTION_BEFORE_NEXTGEN_IMPORT, $nextgen_gallery, $nextgen_gallery_pics, $nextgen_gallery_pics_count); |
| 36 |
|
| 37 |
switch ($_POST['maxgalleria_gallery_where']) { |
| 38 |
case 'existing': |
| 39 |
$maxgalleria_gallery = get_post($_POST['maxgalleria_gallery_id']); |
| 40 |
break; |
| 41 |
case 'new': |
| 42 |
// First create the gallery post itself |
| 43 |
$new_id = wp_insert_post(array('post_title' => stripslashes($_POST['maxgalleria_gallery_title']), 'post_type' => MAXGALLERIA_POST_TYPE)); |
| 44 |
|
| 45 |
// Then save the gallery type and set template to Image Tiles |
| 46 |
$options = new MaxGalleryOptions($new_id); |
| 47 |
add_post_meta($new_id, $options->type_key, 'image', true); |
| 48 |
add_post_meta($new_id, $options->template_key, 'image-tiles', true); |
| 49 |
|
| 50 |
// And finally get the full gallery post back |
| 51 |
$maxgalleria_gallery = get_post($new_id); |
| 52 |
break; |
| 53 |
} |
| 54 |
|
| 55 |
if (isset($maxgalleria_gallery)) { |
| 56 |
// Turn number of galleries to process into chunks for progress bar |
| 57 |
$chunks = ceil(100 / $nextgen_gallery_pics_count) + 1; |
| 58 |
$chunk = 0; |
| 59 |
|
| 60 |
foreach ($nextgen_gallery_pics as $picture) { |
| 61 |
do_action(MAXGALLERIA_ACTION_BEFORE_NEXTGEN_IMPORT_PICTURE, $picture); |
| 62 |
|
| 63 |
$url = trailingslashit(site_url()) . trailingslashit($nextgen_gallery->path) . $picture->filename; |
| 64 |
|
| 65 |
// Get the contents of the picture |
| 66 |
$response = wp_remote_get($url); |
| 67 |
$contents = wp_remote_retrieve_body($response); |
| 68 |
|
| 69 |
// Upload and get file data |
| 70 |
$upload = wp_upload_bits(basename($url), null, $contents); |
| 71 |
$guid = $upload['url']; |
| 72 |
$file = $upload['file']; |
| 73 |
$file_type = wp_check_filetype(basename($file), null); |
| 74 |
|
| 75 |
// Get menu order |
| 76 |
$menu_order = $common->get_next_menu_order($maxgalleria_gallery->ID); |
| 77 |
|
| 78 |
// Create attachment |
| 79 |
$attachment = array( |
| 80 |
'ID' => 0, |
| 81 |
'guid' => $upload['url'], |
| 82 |
'post_title' => $picture->alttext != '' ? $picture->alttext : $picture->image_slug, |
| 83 |
'post_excerpt' => $picture->description, |
| 84 |
'post_content' => $picture->description, |
| 85 |
'post_date' => '', // Ensures it gets today's date |
| 86 |
'post_parent' => $maxgalleria_gallery->ID, |
| 87 |
'post_mime_type' => $file_type['type'], |
| 88 |
'ancestors' => array(), |
| 89 |
'menu_order' => $menu_order |
| 90 |
); |
| 91 |
|
| 92 |
// Include image.php so we can call wp_generate_attachment_metadata() |
| 93 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 94 |
|
| 95 |
// Insert the attachment |
| 96 |
$attachment_id = wp_insert_attachment($attachment, $file, $maxgalleria_gallery->ID); |
| 97 |
$attachment_data = wp_generate_attachment_metadata($attachment_id, $file); |
| 98 |
wp_update_attachment_metadata($attachment_id, $attachment_data); |
| 99 |
|
| 100 |
// Save alt text in the post meta and increment counter |
| 101 |
update_post_meta($attachment_id, '_wp_attachment_image_alt', $picture->alttext); |
| 102 |
$nextgen_import_count++; |
| 103 |
|
| 104 |
// Increment chunks for progress bar |
| 105 |
$chunk++; |
| 106 |
update_option('maxgalleria_nextgen_import_percent', $chunk * $chunks); |
| 107 |
|
| 108 |
do_action(MAXGALLERIA_ACTION_AFTER_NEXTGEN_IMPORT_PICTURE, $picture); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
do_action(MAXGALLERIA_ACTION_AFTER_NEXTGEN_IMPORT, $nextgen_gallery, $nextgen_gallery_pics, $nextgen_gallery_pics_count); |
| 113 |
|
| 114 |
$gallery_edit_url = admin_url('post.php?post=' . $maxgalleria_gallery->ID . '&action=edit'); |
| 115 |
$message = sprintf(__('%sSuccessfully imported %d of %d images from NextGEN into the "%s%s%s" gallery.%s', 'maxgalleria'), '<h4>', $nextgen_import_count, $nextgen_gallery_pics_count, '<a href="' . $gallery_edit_url . '">', $maxgalleria_gallery->post_title, '</a>', '</h4>'); |
| 116 |
echo $message; |
| 117 |
die(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public function get_nextgen_import_percent() { |
| 122 |
$import_percent = get_option('maxgalleria_nextgen_import_percent'); |
| 123 |
$percentage = ($import_percent > 100) ? 100 : $import_percent; |
| 124 |
|
| 125 |
echo $percentage; |
| 126 |
die(); |
| 127 |
} |
| 128 |
|
| 129 |
public function reset_nextgen_import() { |
| 130 |
update_option('maxgalleria_nextgen_import_count', 0); |
| 131 |
update_option('maxgalleria_nextgen_import_percent', 0); |
| 132 |
|
| 133 |
// No need to echo anything for a return |
| 134 |
die(); |
| 135 |
} |
| 136 |
|
| 137 |
public function get_nextgen_gallery($id) { |
| 138 |
global $wpdb; |
| 139 |
return $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $this->get_nextgen_gallery_table() . " WHERE gid = %d", $id)); |
| 140 |
} |
| 141 |
|
| 142 |
public function get_nextgen_galleries() { |
| 143 |
global $wpdb; |
| 144 |
return $wpdb->get_results("SELECT * FROM " . $this->get_nextgen_gallery_table()); |
| 145 |
} |
| 146 |
|
| 147 |
public function get_nextgen_gallery_picture_count($id) { |
| 148 |
global $wpdb; |
| 149 |
return $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM " . $this->get_nextgen_pictures_table() . " WHERE galleryid = %d", $id)); |
| 150 |
} |
| 151 |
|
| 152 |
public function get_nextgen_gallery_pictures($id) { |
| 153 |
global $wpdb; |
| 154 |
return $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $this->get_nextgen_pictures_table() . " WHERE galleryid = %d", $id)); |
| 155 |
} |
| 156 |
|
| 157 |
public function get_nextgen_gallery_table() { |
| 158 |
global $wpdb; |
| 159 |
return $wpdb->prefix . 'ngg_gallery'; |
| 160 |
} |
| 161 |
|
| 162 |
public function get_nextgen_pictures_table() { |
| 163 |
global $wpdb; |
| 164 |
return $wpdb->prefix . 'ngg_pictures'; |
| 165 |
} |
| 166 |
|
| 167 |
public function is_nextgen_installed() { |
| 168 |
$plugins = get_plugins(); |
| 169 |
|
| 170 |
foreach ($plugins as $plugin_path => $plugin) { |
| 171 |
if ($plugin['Name'] == 'NextGEN Gallery' || $plugin['Name'] == 'NextGEN Gallery by Photocrati') { |
| 172 |
return true; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return false; |
| 177 |
} |
| 178 |
} |
| 179 |
?> |