'grid', 'showdotnav' => true, 'order' => 'ASC', 'orderby' => 'menu_order ID' ); /** * @var Attributes for the gallery */ private $settings; /** * @var The current post */ private $post; /** * @var The images */ private $attachments; /** * Renders a gallery based on the attributes. * Prepares content before passing rendering to specific function. * * @param $attributes * * @return mixed */ public function render($attributes) { $this->post = get_post(); $this->settings = array_merge($this->defaults, $attributes); $arguments = array( 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $this->settings['order'], 'orderby' => $this->settings['orderby'] ); if (!empty($this->settings['ids'])) { $arguments['include'] = $this->settings['ids']; } else { $arguments['include'] = $this->post ? $this->post->ID : 0; } $this->attachments = get_posts($arguments); // If no images were found... if (empty($this->attachments)) { return; } // Render the gallery if ($this->settings['type'] == 'slideshow') { return $this->renderSlideshow(); } else if ($this->settings['type'] == 'grid') { return $this->renderGrid(); } } /** * Renders the gallery like a slideshow */ private function renderSlideshow() { $description = trim($attachment->post_content) ? esc_attr($attachment->post_content) : esc_attr(trim($attachment->post_excerpt)); $output = array(); $output[] = '
'; $output[] = ''; $output[] = ''; $output[] = ''; if ($this->settings['showdotnav']) { $output[] = ''; } $output[] = '
'; return implode(" ", $output); } /** * Renders the gallery like a grid with lightbox */ private function renderGrid() { $output = array(); $output[] = '
'; foreach ($this->attachments as $attachment) { $small = $this->getImageByAttachment($attachment, 'thumbnail'); $big = $this->getImageByAttachment($attachment); $description = trim($attachment->post_content) ? esc_attr($attachment->post_content) : esc_attr(trim($attachment->post_excerpt)); $output[] = '
'; $output[] = ''; $output[] = ''; $output[] = ''; $output[] = '
'; $output[] = '
'; } $output[] = '
'; return implode(" ", $output); } /** * Gets the image out of an attachment * * @param $attachment * * @return array|bool */ private function getImageByAttachment($attachment, $size = 'large') { return $this->getImageByAttachmentID($attachment->ID, $size); } /** * Gets the image from an id * * @param $id * @param string $size * * @return array|bool */ private function getImageByAttachmentID($id, $size = 'large') { return wp_get_attachment_image_src($id, $size); } }