diff --git a/inc/shortcodes/class-abc-news.php b/inc/shortcodes/class-abc-news.php index cec377c..432e773 100644 --- a/inc/shortcodes/class-abc-news.php +++ b/inc/shortcodes/class-abc-news.php @@ -11,6 +11,16 @@ public static function get_shortcode_ui_args() { 'label' => esc_html__( 'ABC News', 'shortcake-bakery' ), 'listItemImage' => '', 'attrs' => array( + array( + 'label' => esc_html__( 'Type', 'shortcake-bakery' ), + 'attr' => 'type', + 'type' => 'select', + 'options' => array( + 'iframe' => esc_html__( 'Iframe', 'shortcake-bakery' ), + 'script' => esc_html__( 'Script tag', 'shortcake-bakery' ), + ), + 'description' => esc_html__( 'URL to a ABC News video', 'shortcake-bakery' ), + ), array( 'label' => esc_html__( 'URL', 'shortcake-bakery' ), 'attr' => 'url', @@ -29,7 +39,18 @@ public static function reversal( $content ) { if ( ! in_array( self::parse_url( $iframe->attrs['src'], PHP_URL_HOST ), self::$valid_hosts ) ) { continue; } - $replacements[ $iframe->original ] = '[' . self::get_shortcode_tag() . ' url="' . esc_url_raw( $iframe->attrs['src'] ) . '"]'; + $replacements[ $iframe->original ] = '[' . self::get_shortcode_tag() . ' type="iframe" url="' . esc_url_raw( $iframe->attrs['src'] ) . '"]'; + } + $content = self::make_replacements_to_content( $content, $replacements ); + } + + if ( $scripts = self::parse_scripts( $content ) ) { + $replacements = array(); + foreach ( $scripts as $script ) { + if ( ! in_array( self::parse_url( $script->attrs['src'], PHP_URL_HOST ), self::$valid_hosts ) ) { + continue; + } + $replacements[ $script->original ] = '[' . self::get_shortcode_tag() . ' type="script" url="' . esc_url_raw( $script->attrs['src'] ) . '"]'; } $content = self::make_replacements_to_content( $content, $replacements ); } @@ -38,12 +59,18 @@ public static function reversal( $content ) { } public static function callback( $attrs, $content = '' ) { + $attrs = wp_parse_args( $attrs, array( 'type' => 'iframe' ) ); if ( empty( $attrs['url'] ) || ! in_array( self::parse_url( $attrs['url'], PHP_URL_HOST ), self::$valid_hosts ) ) { return ''; } - return sprintf( '', esc_url( $attrs['url'] ) ); + switch ( $attrs['type'] ) { + case 'iframe': + return sprintf( '', esc_url( $attrs['url'] ) ); + case 'script': + return sprintf( '', esc_url( $attrs['url'] ) ); + } } } diff --git a/tests/test-abc-news-shortcode.php b/tests/test-abc-news-shortcode.php index 1c6e76f..6b0b7f9 100644 --- a/tests/test-abc-news-shortcode.php +++ b/tests/test-abc-news-shortcode.php @@ -2,13 +2,19 @@ class Test_ABC_News_Shortcode extends WP_UnitTestCase { - public function test_post_display() { + public function test_post_display_with_iframe() { $post_id = $this->factory->post->create( array( 'post_content' => '[abc-news url="http://abcnews.go.com/video/embed?id=33317297"]' ) ); $post = get_post( $post_id ); $this->assertContains( '', apply_filters( 'the_content', $post->post_content ) ); } - public function test_embed_reversal() { + public function test_post_display_with_script() { + $post_id = $this->factory->post->create( array( 'post_content' => '[abc-news type="script" url="http://abcnews.go.com/javascript/portableplayer?id=14476486&autoStart=true&size=inpage&affil=true"]' ) ); + $post = get_post( $post_id ); + $this->assertContains( '', apply_filters( 'the_content', $post->post_content ) ); + } + + public function test_iframe_embed_reversal() { $old_content = <<assertEquals( $expected_content, $transformed_content ); } + public function test_script_embed_reversal() { + $old_content = << + + bananas after +EOT; + + $expected_content = <<assertEquals( $expected_content, $transformed_content ); + } }