| 1 |
<?php |
| 2 |
class MaxGalleriaShortcode { |
| 3 |
public function __construct() { |
| 4 |
add_shortcode('maxgallery', array($this, 'maxgallery_shortcode')); |
| 5 |
} |
| 6 |
|
| 7 |
public function maxgallery_shortcode($atts) { |
| 8 |
extract(shortcode_atts(array( |
| 9 |
'id' => '', |
| 10 |
'name' => '' |
| 11 |
), $atts)); |
| 12 |
|
| 13 |
$gallery_id = sanitize_text_field("{$id}"); |
| 14 |
$gallery_name = sanitize_text_field("{$name}"); |
| 15 |
|
| 16 |
$output = ''; |
| 17 |
$gallery = null; |
| 18 |
|
| 19 |
if ($gallery_id != '' && $gallery_name != '') { |
| 20 |
// If both given, the id wins |
| 21 |
$gallery = get_post($gallery_id); |
| 22 |
} |
| 23 |
|
| 24 |
if ($gallery_id != '' && $gallery_name == '') { |
| 25 |
// Get the gallery by id |
| 26 |
$gallery = get_post($gallery_id); |
| 27 |
} |
| 28 |
|
| 29 |
if ($gallery_id == '' && $gallery_name != '') { |
| 30 |
// Get the gallery by name |
| 31 |
$query = new WP_Query(array('name' => $gallery_name, 'post_type' => MAXGALLERIA_POST_TYPE)); |
| 32 |
$gallery = $query->get_queried_object(); |
| 33 |
} |
| 34 |
|
| 35 |
if (isset($gallery) && $gallery->post_status == 'publish') { |
| 36 |
|
| 37 |
global $wpdb; |
| 38 |
|
| 39 |
$options = new MaxGalleryOptions($gallery->ID); |
| 40 |
|
| 41 |
if($options->is_video_gallery()) |
| 42 |
$items_per_page = get_post_meta( $gallery->ID, 'maxgallery_videos_per_page', true ); |
| 43 |
else |
| 44 |
$items_per_page = get_post_meta( $gallery->ID, 'maxgallery_images_per_page', true ); |
| 45 |
|
| 46 |
if(($items_per_page == '') || ($items_per_page == 0)) { |
| 47 |
// if blank, get all attachments by setting items_per_page to -1 |
| 48 |
$items_per_page = -1; |
| 49 |
} |
| 50 |
else { |
| 51 |
// calculate total number of pages; for this we need the total number of attachments |
| 52 |
$sql = "select SQL_CALC_FOUND_ROWS ID from " . $wpdb->prefix . "posts where post_parent = $gallery->ID and post_type = 'attachment'"; |
| 53 |
$rows = $wpdb->get_results($sql); |
| 54 |
$count = $wpdb->get_row("select FOUND_ROWS()", ARRAY_A); |
| 55 |
$total_posts = $count['FOUND_ROWS()']; |
| 56 |
} |
| 57 |
|
| 58 |
$paged = (get_query_var('page')) ? get_query_var('page') : 1; |
| 59 |
$args = array( |
| 60 |
'post_parent' => $gallery->ID, |
| 61 |
'post_type' => 'attachment', |
| 62 |
'orderby' => 'menu_order', |
| 63 |
'order' => 'asc', |
| 64 |
'numberposts' => -1, // All of them |
| 65 |
'posts_per_page' => $items_per_page, |
| 66 |
'paged' => $paged |
| 67 |
); |
| 68 |
|
| 69 |
$attachments = get_posts($args); |
| 70 |
|
| 71 |
$total_number_pages = ceil($total_posts / $items_per_page); |
| 72 |
|
| 73 |
foreach ($attachments as $attachment) { |
| 74 |
setup_postdata($attachment); |
| 75 |
|
| 76 |
if (count($attachments) > 0) { |
| 77 |
//$options = new MaxGalleryOptions($gallery->ID); |
| 78 |
|
| 79 |
global $maxgalleria; |
| 80 |
$templates = $maxgalleria->get_template_addons(); |
| 81 |
|
| 82 |
foreach ($templates as $template) { |
| 83 |
if ($template['key'] == $options->get_template()) { |
| 84 |
$output = call_user_func($template['output'], $gallery, $attachments); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
// display page links if we have pages |
| 94 |
if($items_per_page != -1) { |
| 95 |
$link = get_the_permalink(); |
| 96 |
$prev = $paged-1; |
| 97 |
$next = $paged+1; |
| 98 |
$page_links = "<div style='clear:both'></div><div class='mg-pagination'>"; |
| 99 |
if($prev != 0) { |
| 100 |
$page_links .= '<a class="mg-page-previous" href="'.$link.($paged-1).'">« Previous</a>'; |
| 101 |
} |
| 102 |
if($next <= $total_number_pages) { |
| 103 |
$page_links .= '<a class="mg-page-next" href="'.$link.($paged+1).'">Next »</a>'; |
| 104 |
} |
| 105 |
$page_links .= "</div>"; |
| 106 |
} |
| 107 |
else |
| 108 |
$page_links = ""; |
| 109 |
|
| 110 |
return $output . $page_links; |
| 111 |
} |
| 112 |
} |
| 113 |
?> |