| @@ -11,8 +11,12 @@ | ||
| 11 | 11 | * |
| 12 | 12 | * @package automattic/jetpack |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 16 | + exit( 0 ); | |
| 17 | +} | |
| 18 | + | |
| 15 | 19 | /** |
| 16 | 20 | * Get ID of requested archive.org embed. |
| 17 | 21 | * |
| 18 | 22 | * @since 4.5.0 |
| @@ -55,8 +59,9 @@ | ||
| 55 | 59 | 'width' => 640, |
| 56 | 60 | 'height' => 480, |
| 57 | 61 | 'autoplay' => 0, |
| 58 | 62 | 'poster' => '', |
| 63 | + 'playlist' => 0, | |
| 59 | 64 | ), |
| 60 | 65 | $atts |
| 61 | 66 | ); |
| 62 | 67 | |
| @@ -65,8 +70,25 @@ | ||
| 65 | 70 | } |
| 66 | 71 | |
| 67 | 72 | $id = $atts['id']; |
| 68 | 73 | |
| 74 | + // Allow extra query parameters to be baked into the identifier, e.g. "myitem&playlist=1" or "myitem?playlist=1". | |
| 75 | + // In some environments a the_content filter encodes "&" to "&" before do_shortcode runs, so the | |
| 76 | + // parser receives "myitem&playlist=1" — normalize that back before splitting. The sibling function | |
| 77 | + // jetpack_archiveorg_embed_to_shortcode() splits on "&" for the same reason. | |
| 78 | + $id = str_replace( '&', '&', $id ); | |
| 79 | + $id_extra_args = array(); | |
| 80 | + if ( preg_match( '/^([^?&]*)[?&](.*)$/', $id, $id_match ) ) { | |
| 81 | + $id = $id_match[1]; | |
| 82 | + wp_parse_str( $id_match[2], $id_extra_args ); | |
| 83 | + } | |
| 84 | + | |
| 85 | + // Re-check after the split — an identifier that's only a query string (e.g. "?playlist=1") leaves $id empty | |
| 86 | + // and would otherwise produce an item-less embed URL. | |
| 87 | + if ( '' === $id ) { | |
| 88 | + return '<!-- error: missing archive.org ID -->'; | |
| 89 | + } | |
| 90 | + | |
| 69 | 91 | if ( ! $atts['width'] ) { |
| 70 | 92 | $width = absint( $content_width ); |
| 71 | 93 | } else { |
| 72 | 94 | $width = (int) $atts['width']; |
| @@ -77,24 +99,31 @@ | ||
| 77 | 99 | } else { |
| 78 | 100 | $height = (int) $atts['height']; |
| 79 | 101 | } |
| 80 | 102 | |
| 103 | + $query_args = array(); | |
| 81 | 104 | if ( $atts['autoplay'] ) { |
| 82 | - $autoplay = '&autoplay=1'; | |
| 83 | - } else { | |
| 84 | - $autoplay = ''; | |
| 105 | + $query_args['autoplay'] = 1; | |
| 85 | 106 | } |
| 107 | + if ( $atts['poster'] ) { | |
| 108 | + $query_args['poster'] = $atts['poster']; | |
| 109 | + } | |
| 110 | + if ( $atts['playlist'] ) { | |
| 111 | + $query_args['playlist'] = 1; | |
| 112 | + } | |
| 86 | 113 | |
| 87 | - if ( $atts['poster'] ) { | |
| 88 | - $poster = '&poster=' . $atts['poster']; | |
| 89 | - } else { | |
| 90 | - $poster = ''; | |
| 114 | + // Explicit shortcode attributes take precedence over query parameters baked into the identifier. | |
| 115 | + $query_args = array_merge( $id_extra_args, $query_args ); | |
| 116 | + | |
| 117 | + $url = 'https://archive.org/embed/' . $id; | |
| 118 | + if ( ! empty( $query_args ) ) { | |
| 119 | + $url = add_query_arg( $query_args, $url ); | |
| 91 | 120 | } |
| 92 | 121 | |
| 93 | 122 | return sprintf( |
| 94 | 123 | '<div class="embed-archiveorg" style="text-align:center;"><iframe title="%s" src="%s" width="%s" height="%s" style="border:0;" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe></div>', |
| 95 | 124 | esc_attr__( 'Archive.org', 'jetpack' ), |
| 96 | - esc_url( "https://archive.org/embed/{$id}{$autoplay}{$poster}" ), | |
| 125 | + esc_url( $url ), | |
| 97 | 126 | esc_attr( $width ), |
| 98 | 127 | esc_attr( $height ) |
| 99 | 128 | ); |
| 100 | 129 | } |
| @@ -158,5 +187,7 @@ | ||
| 158 | 187 | |
| 159 | 188 | return $content; |
| 160 | 189 | } |
| 161 | 190 | |
| 162 | -add_filter( 'pre_kses', 'jetpack_archiveorg_embed_to_shortcode' ); | |
| 191 | +if ( jetpack_shortcodes_should_hook_pre_kses() ) { | |
| 192 | + add_filter( 'pre_kses', 'jetpack_archiveorg_embed_to_shortcode' ); | |
| 193 | +} | |