| 1 |
<?php |
| 2 |
/** |
| 3 |
* Archive.org book shortcode. |
| 4 |
* |
| 5 |
* Usage: |
| 6 |
* [archiveorg Experime1940] |
| 7 |
* [archiveorg http://archive.org/details/Experime1940 poster=http://archive.org/images/map.png] |
| 8 |
* [archiveorg id=Experime1940 width=640 height=480 autoplay=1] |
| 9 |
|
| 10 |
* <iframe src="http://archive.org/embed/Experime1940&autoplay=1&poster=http://archive.org/images/map.png" width="640" height="480" frameborder="0" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe> |
| 11 |
* |
| 12 |
* @package automattic/jetpack |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit( 0 ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Get ID of requested archive.org embed. |
| 21 |
* |
| 22 |
* @since 4.5.0 |
| 23 |
* |
| 24 |
* @param array $atts Shortcode attributes. |
| 25 |
* |
| 26 |
* @return int|string |
| 27 |
*/ |
| 28 |
function jetpack_shortcode_get_archiveorg_id( $atts ) { |
| 29 |
if ( isset( $atts[0] ) ) { |
| 30 |
$atts[0] = trim( $atts[0], '=' ); |
| 31 |
if ( preg_match( '#archive.org/(details|embed)/(.+)/?$#i', $atts[0], $match ) ) { |
| 32 |
$id = $match[2]; |
| 33 |
} else { |
| 34 |
$id = $atts[0]; |
| 35 |
} |
| 36 |
return $id; |
| 37 |
} |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Convert an archive.org shortcode into an embed code. |
| 43 |
* |
| 44 |
* @since 4.5.0 |
| 45 |
* |
| 46 |
* @param array $atts An array of shortcode attributes. |
| 47 |
* @return string The embed code for the archive.org video. |
| 48 |
*/ |
| 49 |
function jetpack_archiveorg_shortcode( $atts ) { |
| 50 |
global $content_width; |
| 51 |
|
| 52 |
if ( isset( $atts[0] ) && empty( $atts['id'] ) ) { |
| 53 |
$atts['id'] = jetpack_shortcode_get_archiveorg_id( $atts ); |
| 54 |
} |
| 55 |
|
| 56 |
$atts = shortcode_atts( |
| 57 |
array( |
| 58 |
'id' => '', |
| 59 |
'width' => 640, |
| 60 |
'height' => 480, |
| 61 |
'autoplay' => 0, |
| 62 |
'poster' => '', |
| 63 |
), |
| 64 |
$atts |
| 65 |
); |
| 66 |
|
| 67 |
if ( ! $atts['id'] ) { |
| 68 |
return '<!-- error: missing archive.org ID -->'; |
| 69 |
} |
| 70 |
|
| 71 |
$id = $atts['id']; |
| 72 |
|
| 73 |
if ( ! $atts['width'] ) { |
| 74 |
$width = absint( $content_width ); |
| 75 |
} else { |
| 76 |
$width = (int) $atts['width']; |
| 77 |
} |
| 78 |
|
| 79 |
if ( ! $atts['height'] ) { |
| 80 |
$height = round( ( $width / 640 ) * 360 ); |
| 81 |
} else { |
| 82 |
$height = (int) $atts['height']; |
| 83 |
} |
| 84 |
|
| 85 |
if ( $atts['autoplay'] ) { |
| 86 |
$autoplay = '&autoplay=1'; |
| 87 |
} else { |
| 88 |
$autoplay = ''; |
| 89 |
} |
| 90 |
|
| 91 |
if ( $atts['poster'] ) { |
| 92 |
$poster = '&poster=' . $atts['poster']; |
| 93 |
} else { |
| 94 |
$poster = ''; |
| 95 |
} |
| 96 |
|
| 97 |
return sprintf( |
| 98 |
'<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>', |
| 99 |
esc_attr__( 'Archive.org', 'jetpack' ), |
| 100 |
esc_url( "https://archive.org/embed/{$id}{$autoplay}{$poster}" ), |
| 101 |
esc_attr( $width ), |
| 102 |
esc_attr( $height ) |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
add_shortcode( 'archiveorg', 'jetpack_archiveorg_shortcode' ); |
| 107 |
|
| 108 |
/** |
| 109 |
* Compose shortcode from archive.org iframe. |
| 110 |
* |
| 111 |
* @since 4.5.0 |
| 112 |
* |
| 113 |
* @param string $content Post content. |
| 114 |
* |
| 115 |
* @return mixed |
| 116 |
*/ |
| 117 |
function jetpack_archiveorg_embed_to_shortcode( $content ) { |
| 118 |
if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/embed/' ) ) { |
| 119 |
return $content; |
| 120 |
} |
| 121 |
|
| 122 |
$regexp = '!<iframe\s+src=[\'"]https?://archive\.org/embed/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)></iframe>!i'; |
| 123 |
|
| 124 |
if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) { |
| 125 |
return $content; |
| 126 |
} |
| 127 |
|
| 128 |
foreach ( $matches as $match ) { |
| 129 |
$url = explode( '&', $match[1] ); |
| 130 |
$id = 'id=' . $url[0]; |
| 131 |
|
| 132 |
$autoplay = ''; |
| 133 |
$poster = ''; |
| 134 |
$url_count = count( $url ); |
| 135 |
|
| 136 |
for ( $ii = 1; $ii < $url_count; $ii++ ) { |
| 137 |
if ( 'autoplay=1' === $url[ $ii ] ) { |
| 138 |
$autoplay = ' autoplay="1"'; |
| 139 |
} |
| 140 |
|
| 141 |
$map_matches = array(); |
| 142 |
if ( preg_match( '/^poster=(.+)$/', $url[ $ii ], $map_matches ) ) { |
| 143 |
$poster = " poster=\"{$map_matches[1]}\""; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
$params = $match[2]; |
| 148 |
|
| 149 |
$params = wp_kses_hair( $params, array( 'http' ) ); |
| 150 |
|
| 151 |
$width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0; |
| 152 |
$height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0; |
| 153 |
|
| 154 |
$wh = ''; |
| 155 |
if ( $width && $height ) { |
| 156 |
$wh = ' width=' . $width . ' height=' . $height; |
| 157 |
} |
| 158 |
|
| 159 |
$shortcode = '[archiveorg ' . $id . $wh . $autoplay . $poster . ']'; |
| 160 |
$content = str_replace( $match[0], $shortcode, $content ); |
| 161 |
} |
| 162 |
|
| 163 |
return $content; |
| 164 |
} |
| 165 |
|
| 166 |
if ( jetpack_shortcodes_should_hook_pre_kses() ) { |
| 167 |
add_filter( 'pre_kses', 'jetpack_archiveorg_embed_to_shortcode' ); |
| 168 |
} |
| 169 |
|