Skip to content

Instantly share code, notes, and snippets.

@fongreecss
Last active April 1, 2016 11:09
Show Gist options
  • Save fongreecss/35b37629641d45d5d482474cb6aa70b0 to your computer and use it in GitHub Desktop.
Save fongreecss/35b37629641d45d5d482474cb6aa70b0 to your computer and use it in GitHub Desktop.
Helper for Wordpress
<?php
class GalleryInfo {
private $gallery = null;
private $ids = array();
private $gallerySize = 'thumbnail';
private $imagesMetadata = array();
private function setIds() {
$this->ids = array_map('intval', explode(",", $this->gallery['ids']));
}
public function setGallerySize() {
$this->gallerySize = $this->gallery['size'];
}
private function setImagesMetadata() {
$this->imagesMetadata = array();
foreach ($this->ids as $key => $id) {
$imagesrc = wp_get_attachment_image_src($id, $this->gallerySize);
$image = get_post($id);
$this->imagesMetadata[$id] = array(
'id' => $id,
'src' => $imagesrc[0], //thumbnail size (or that one that is stated trough gallery shortcut)
'width' => $imagesrc[1],
'height' => $imagesrc[2],
'caption' => sanitize_text_field($image->post_excerpt),
'description' => wp_kses($image->post_content, array(
'a' => array(),
'br' => array(),
'em' => array(),
'strong' => array(),
'i' => array()
)),
'title' => sanitize_text_field($image->post_title),
'osrc' => $image->guid, //original image src
'href' => get_permalink($image->ID), //original attachment page
);
}
}
public function getGallery() {
return $this->gallery;
}
public function getGallerySize() {
return $this->gallerySize;
}
public function getIds() {
return $this->ids;
}
public function getImagesMetadata() {
return $this->imagesMetadata;
}
//gallery has to exist in post
public function __construct($post) {
if(! has_shortcode($post->post_content, 'gallery')) {
return;
}
$this->gallery = get_post_gallery( $post, false );
$this->setIds($this->gallery);
$this->setGallerySize();
$this->setImagesMetadata();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment