Skip to content

Commit

Permalink
[gallery] => amp-carousel
Browse files Browse the repository at this point in the history
Outputs amp-carousel tag for any gallery shortcodes by overriding the
built-in gallery shortcode.

Fixes #2
  • Loading branch information
mjangda committed Nov 14, 2015
1 parent 8ed7167 commit 6dc5eb0
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions class-amp-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_once( dirname( __FILE__ ) . '/class-amp-embed-handler.php' );
require_once( dirname( __FILE__ ) . '/class-amp-twitter-embed.php' );
require_once( dirname( __FILE__ ) . '/class-amp-youtube-embed.php' );
require_once( dirname( __FILE__ ) . '/class-amp-gallery-embed.php' );

class AMP_Content {
private $original_content;
Expand All @@ -23,9 +24,11 @@ public function transform() {

$twitter_embed = new AMP_Twitter_Embed_Handler;
$youtube_embed = new AMP_YouTube_Embed_Handler;
$gallery_embed = new AMP_Gallery_Embed_Handler;
$content = apply_filters( 'the_content', $content );
$this->add_scripts( $twitter_embed->get_scripts() );
$this->add_scripts( $youtube_embed->get_scripts() );
$this->add_scripts( $gallery_embed->get_scripts() );

$content = AMP_Sanitizer::strip( $content );

Expand Down
140 changes: 140 additions & 0 deletions class-amp-gallery-embed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

require_once( dirname( __FILE__ ) . '/class-amp-embed-handler.php' );

class AMP_Gallery_Embed_Handler extends AMP_Embed_Handler {
const DEFAULT_WIDTH = 600;
const DEFAULT_HEIGHT = 480;

private static $script_slug = 'amp-carousel';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-carousel-0.1.js';

private $args;

function __construct( $args = array() ) {
$this->args = wp_parse_args( $args, array(
'width' => self::DEFAULT_WIDTH,
'height' => self::DEFAULT_HEIGHT,
'type' => 'slides',
) );

add_shortcode( 'gallery', array( $this, 'shortcode' ) );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

public function shortcode( $attr ) {
$post = get_post();

if ( ! empty( $attr['ids'] ) ) {
// 'ids' is explicitly ordered, unless you specify otherwise.
if ( empty( $attr['orderby'] ) ) {
$attr['orderby'] = 'post__in';
}
$attr['include'] = $attr['ids'];
}

$atts = shortcode_atts( array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post ? $post->ID : 0,
'include' => '',
'exclude' => '',
'size' => array( $this->args['width'], $this->args['height'] )
), $attr, 'gallery' );

$id = intval( $atts['id'] );

if ( ! empty( $atts['include'] ) ) {
$attachments = get_posts( array(
'include' => $atts['include'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby'],
'fields' => 'ids',
) );
} elseif ( ! empty( $atts['exclude'] ) ) {
$attachments = get_children( array(
'post_parent' => $id,
'exclude' => $atts['exclude'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby'],
'fields' => 'ids',
) );
} else {
$attachments = get_children( array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby'],
'fields' => 'ids',
) );
}

if ( empty( $attachments ) ) {
return '';
}

$urls = array();
foreach ( $attachments as $attachment_id ) {
list( $url, $width, $height ) = wp_get_attachment_image_src( $attachment_id, $atts['size'], true );

if ( ! $url ) {
continue;
}

$urls[] = array(
'url' => $url,
'width' => $width,
'height' => $height,
);
}

return $this->render( array(
'images' => $urls,
) );
}

public function render( $args ) {
$this->did_convert_elements = true;

$args = wp_parse_args( $args, array(
'images' => false,
) );

if ( empty( $args['images'] ) ) {
return '';
}

$images = array();
foreach ( $args['images'] as $image ) {
$images[] = AMP_HTML_Utils::build_tag(
'amp-img',
array(
'src' => $image['url'],
'width' => $image['width'],
'height' => $image['height'],
)
);
}

return AMP_HTML_Utils::build_tag(
'amp-carousel',
wp_parse_args( array(), $this->args ),
implode( PHP_EOL, $images )
);
}
}

0 comments on commit 6dc5eb0

Please sign in to comment.