| 1 |
<?php |
| 2 |
namespace Photonic_Plugin\Modules; |
| 3 |
|
| 4 |
require_once('Core.php'); |
| 5 |
require_once('Level_One_Module.php'); |
| 6 |
|
| 7 |
/** |
| 8 |
* Processor for native WP galleries. This extends the Photonic_Plugin\Modules\Core class and defines methods local to WP. |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
class Native extends Core implements Level_One_Module { |
| 13 |
private static $instance = null; |
| 14 |
|
| 15 |
protected function __construct() { |
| 16 |
parent::__construct(); |
| 17 |
global $photonic_wp_disable_title_link; |
| 18 |
$this->provider = 'wp'; |
| 19 |
$this->link_lightbox_title = empty($photonic_wp_disable_title_link); |
| 20 |
$this->doc_links = [ |
| 21 |
'general' => 'https://aquoid.com/plugins/photonic/wp-galleries/', |
| 22 |
]; |
| 23 |
} |
| 24 |
|
| 25 |
public static function get_instance() { |
| 26 |
if (self::$instance == null) { |
| 27 |
self::$instance = new Native(); |
| 28 |
} |
| 29 |
return self::$instance; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Gets all images associated with the gallery. This method is lifted almost verbatim from the gallery short-code function provided by WP. |
| 34 |
* We will take the gallery images and do some fun stuff with styling them in other methods. We cannot use the WP function because |
| 35 |
* this code is nested within the gallery_shortcode function and we want to tweak that (there is no hook that executes after |
| 36 |
* the gallery has been retrieved.) |
| 37 |
* |
| 38 |
* @param array $attr |
| 39 |
* @param array $gallery_meta |
| 40 |
* @return array|bool |
| 41 |
*/ |
| 42 |
public function get_gallery_images($attr = [], &$gallery_meta = []) { |
| 43 |
global $post, $photonic_wp_title_caption, $photonic_alternative_shortcode; |
| 44 |
|
| 45 |
$this->gallery_index++; |
| 46 |
$this->push_to_stack('Get Gallery Images'); |
| 47 |
|
| 48 |
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement |
| 49 |
if (isset($attr['orderby'])) { |
| 50 |
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']); |
| 51 |
if (!$attr['orderby']) |
| 52 |
unset($attr['orderby']); |
| 53 |
} |
| 54 |
|
| 55 |
if (!empty($attr['ids'])) { |
| 56 |
// 'ids' is explicitly ordered, unless you specify otherwise. |
| 57 |
if (empty($attr['orderby'])) { |
| 58 |
$attr['orderby'] = 'post__in'; |
| 59 |
} |
| 60 |
$attr['include'] = $attr['ids']; |
| 61 |
} |
| 62 |
|
| 63 |
$html5 = current_theme_supports( 'html5', 'gallery' ); |
| 64 |
$attr = shortcode_atts([ |
| 65 |
'order' => 'ASC', |
| 66 |
'orderby' => 'menu_order ID', |
| 67 |
'id' => $post ? $post->ID : 0, |
| 68 |
'itemtag' => $html5 ? 'figure' : 'dl', |
| 69 |
'icontag' => $html5 ? 'div' : 'dt', |
| 70 |
'captiontag' => $html5 ? 'figcaption' : 'dd', |
| 71 |
'columns' => 3, |
| 72 |
'size' => 'thumbnail', |
| 73 |
'include' => '', |
| 74 |
'exclude' => '', |
| 75 |
'link' => '' |
| 76 |
], $attr, !empty($photonic_alternative_shortcode) ? $photonic_alternative_shortcode: 'gallery' ); |
| 77 |
|
| 78 |
$attr['layout'] = $attr['style']; |
| 79 |
$attr['main_size'] = !empty($attr['main_size']) ? $attr['main_size'] : $attr['slide_size']; |
| 80 |
$attr['caption'] = !empty($attr['caption']) ? $attr['caption'] : $photonic_wp_title_caption; |
| 81 |
|
| 82 |
$attr = array_map('trim', $attr); |
| 83 |
extract($attr); |
| 84 |
|
| 85 |
$id = intval($attr['id']); |
| 86 |
if ('RAND' == $attr['order']) |
| 87 |
$attr['orderby'] = 'none'; |
| 88 |
|
| 89 |
// All arguments can be overridden by the standard pre_get_posts filter ... |
| 90 |
$args = ['post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => ['image'], 'order' => $attr['order'], 'orderby' => $attr['orderby'], 'paged' => $attr['page']]; |
| 91 |
|
| 92 |
if (!empty($attr['include'])) { |
| 93 |
$include = preg_replace('/[^0-9,]+/', '', $attr['include']); |
| 94 |
$args['include'] = $include; |
| 95 |
$attr['count'] = -1; // 'include' always ignores the 'posts_per_page'. Having the original value here shows the "More" button even when not required. |
| 96 |
$_attachments = get_posts($args); |
| 97 |
$total_posts = count($_attachments); |
| 98 |
|
| 99 |
$attachments = []; |
| 100 |
foreach ($_attachments as $key => $val) { |
| 101 |
$attachments[$val->ID] = $_attachments[$key]; |
| 102 |
} |
| 103 |
} |
| 104 |
else { |
| 105 |
$args['post_parent'] = $id; |
| 106 |
if (!empty($attr['exclude'])) { |
| 107 |
$exclude = preg_replace('/[^0-9,]+/', '', $attr['exclude']); |
| 108 |
$args['exclude'] = $exclude; |
| 109 |
} |
| 110 |
// First get the total |
| 111 |
$attachments = get_children($args); |
| 112 |
$total_posts = count($attachments); |
| 113 |
|
| 114 |
$args['posts_per_page'] = $attr['count']; |
| 115 |
$attachments = get_children($args); |
| 116 |
} |
| 117 |
|
| 118 |
$ret = $this->process_gallery($attachments, $attr, $total_posts); |
| 119 |
$this->pop_from_stack(); |
| 120 |
if (empty($ret)) { |
| 121 |
return $ret; |
| 122 |
} |
| 123 |
return $ret.$this->get_stack_markup(); |
| 124 |
} |
| 125 |
|
| 126 |
function build_level_1_objects($images, $shortcode_attr, $module_parameters = [], $options = []) { |
| 127 |
$photo_objects = []; |
| 128 |
$thumb_size = $shortcode_attr['thumb_size']; |
| 129 |
$main_size = $shortcode_attr['main_size']; |
| 130 |
$tile_size = !empty($shortcode_attr['tile_size']) ? $shortcode_attr['tile_size'] : $main_size; |
| 131 |
$sources = []; |
| 132 |
$tiles = []; |
| 133 |
$thumbs = []; |
| 134 |
foreach ( $images as $id => $attachment ) { |
| 135 |
$wp_details = wp_prepare_attachment_for_js($id); |
| 136 |
$sources[$id] = wp_get_attachment_image_src($id, $main_size, false); |
| 137 |
$tiles[$id] = wp_get_attachment_image_src($id, $tile_size, false); |
| 138 |
$thumbs[$id] = wp_get_attachment_image_src($id, $thumb_size); |
| 139 |
|
| 140 |
if (isset($attachment->post_title)) { |
| 141 |
$title = wptexturize($attachment->post_title); |
| 142 |
} |
| 143 |
else { |
| 144 |
$title = ''; |
| 145 |
} |
| 146 |
$title = apply_filters('photonic_modify_title', $title, $attachment); |
| 147 |
|
| 148 |
if (is_array($wp_details)) { |
| 149 |
$photo_object = []; |
| 150 |
$photo_object['thumbnail'] = $thumbs[$id][0]; |
| 151 |
$photo_object['main_image'] = $sources[$id][0]; |
| 152 |
$photo_object['tile_image'] = $tiles[$id][0]; |
| 153 |
$photo_object['title'] = esc_attr($title); |
| 154 |
$photo_object['alt_title'] = $photo_object['title']; |
| 155 |
$photo_object['description'] = esc_attr($wp_details['caption']); |
| 156 |
$photo_object['main_page'] = $wp_details['link']; |
| 157 |
$photo_object['id'] = $wp_details['id']; |
| 158 |
|
| 159 |
if ($wp_details['type'] == 'video') { |
| 160 |
$photo_object['video'] = $wp_details['url']; |
| 161 |
} |
| 162 |
|
| 163 |
$photo_object['provider'] = $this->provider; |
| 164 |
|
| 165 |
$photo_objects[] = $photo_object; |
| 166 |
} |
| 167 |
} |
| 168 |
return $photo_objects; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Builds the markup for a gallery when you choose to use a specific gallery style. The following styles are allowed: |
| 173 |
* 1. strip-below: Shows thumbnails for the gallery below a larger image |
| 174 |
* 2. strip-above: Shows thumbnails for the gallery above a larger image |
| 175 |
* 3. no-strip: Doesn't show thumbnails. Useful if you are making it behave like an automatic slideshow. |
| 176 |
* 4. launch: Shows a thumbnail for the gallery, which you can click to launch a slideshow. |
| 177 |
* 5. random: Shows a random justified gallery. |
| 178 |
* |
| 179 |
* @param $attachments |
| 180 |
* @param $shortcode_attr |
| 181 |
* @param int $total_posts |
| 182 |
* @return string |
| 183 |
*/ |
| 184 |
function process_gallery($attachments, $shortcode_attr, $total_posts = -1) { |
| 185 |
global $photonic_wp_thumbnail_title_display; |
| 186 |
if ($shortcode_attr['style'] == 'default') { |
| 187 |
return ''; |
| 188 |
} |
| 189 |
$photos = $this->build_level_1_objects($attachments, $shortcode_attr); |
| 190 |
|
| 191 |
$row_constraints = [ |
| 192 |
'constraint-type' => $shortcode_attr['columns'] == 'auto' ? 'padding' : 'count', |
| 193 |
'padding' => 0, |
| 194 |
'count' => absint($shortcode_attr['columns']) ? absint($shortcode_attr['columns']) : 3 |
| 195 |
]; |
| 196 |
|
| 197 |
$ret = $this->layout_gallery($photos, |
| 198 |
[ |
| 199 |
'title_position' => $photonic_wp_thumbnail_title_display, |
| 200 |
'row_constraints' => $row_constraints, |
| 201 |
'parent' => 'stream', |
| 202 |
'level_2_meta' => [ |
| 203 |
'total' => $total_posts, |
| 204 |
'start' => $shortcode_attr['count'] < 0 ? 1 : ($shortcode_attr['page'] - 1) * $shortcode_attr['count'] + 1, |
| 205 |
'end' => ($shortcode_attr['count'] < 0 || $shortcode_attr['page'] * $shortcode_attr['count'] > $total_posts) ? $total_posts : $shortcode_attr['page'] * $shortcode_attr['count'], |
| 206 |
'per-page' => $shortcode_attr['count'], |
| 207 |
], |
| 208 |
], |
| 209 |
$shortcode_attr, |
| 210 |
1); |
| 211 |
|
| 212 |
$ret = $this->finalize_markup($ret, $shortcode_attr); |
| 213 |
return $ret; |
| 214 |
} |
| 215 |
} |
| 216 |
|