| 1 |
<?php |
| 2 |
/** |
| 3 |
* Archive.org Shortcode |
| 4 |
* |
| 5 |
* Usage: |
| 6 |
* [archiveorg-book goodytwoshoes00newyiala] |
| 7 |
* [archiveorg-book https://www.archive.org/stream/goodytwoshoes00newyiala] |
| 8 |
* [archiveorg id=goodytwoshoes00newyiala width=480 height=430] |
| 9 |
|
| 10 |
* <iframe src='https://www.archive.org/stream/goodytwoshoes00newyiala?ui=embed#mode/1up' width='480px' height='430px' frameborder='0' ></iframe> |
| 11 |
* |
| 12 |
* @package Jetpack |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* Get ID of requested archive.org book embed. |
| 17 |
* |
| 18 |
* @since 4.5.0 |
| 19 |
* |
| 20 |
* @param array $atts Shortcode attributes. |
| 21 |
* |
| 22 |
* @return int|string |
| 23 |
*/ |
| 24 |
function jetpack_shortcode_get_archiveorg_book_id( $atts ) { |
| 25 |
if ( isset( $atts[0] ) ) { |
| 26 |
$atts[0] = trim( $atts[0], '=' ); |
| 27 |
if ( preg_match( '#archive.org/stream/(.+)/?$#i', $atts[0], $match ) ) { |
| 28 |
$id = $match[1]; |
| 29 |
} else { |
| 30 |
$id = $atts[0]; |
| 31 |
} |
| 32 |
return $id; |
| 33 |
} |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Convert an archive.org book shortcode into an embed code. |
| 39 |
* |
| 40 |
* @since 4.5.0 |
| 41 |
* |
| 42 |
* @param array $atts An array of shortcode attributes. |
| 43 |
* @return string The embed code for the Archive.org book |
| 44 |
*/ |
| 45 |
function jetpack_archiveorg_book_shortcode( $atts ) { |
| 46 |
global $content_width; |
| 47 |
|
| 48 |
if ( isset( $atts[0] ) && empty( $atts['id'] ) ) { |
| 49 |
$atts['id'] = jetpack_shortcode_get_archiveorg_book_id( $atts ); |
| 50 |
} |
| 51 |
|
| 52 |
$atts = shortcode_atts( |
| 53 |
array( |
| 54 |
'id' => '', |
| 55 |
'width' => 480, |
| 56 |
'height' => 430, |
| 57 |
), |
| 58 |
$atts |
| 59 |
); |
| 60 |
|
| 61 |
if ( ! $atts['id'] ) { |
| 62 |
return '<!-- error: missing archive.org book ID -->'; |
| 63 |
} |
| 64 |
|
| 65 |
$id = $atts['id']; |
| 66 |
|
| 67 |
if ( ! $atts['width'] ) { |
| 68 |
$width = absint( $content_width ); |
| 69 |
} else { |
| 70 |
$width = intval( $atts['width'] ); |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! $atts['height'] ) { |
| 74 |
$height = round( ( $width / 640 ) * 360 ); |
| 75 |
} else { |
| 76 |
$height = intval( $atts['height'] ); |
| 77 |
} |
| 78 |
|
| 79 |
$url = esc_url( "https://archive.org/stream/{$id}?ui=embed#mode/1up" ); |
| 80 |
|
| 81 |
$title = esc_html__( 'Archive.org Book', 'jetpack' ); |
| 82 |
|
| 83 |
$html = "<div class='embed-archiveorg-book' style='text-align:center;'><iframe title='$title' src='$url' width='$width' height='$height' style='border:0;' webkitallowfullscreen='true' mozallowfullscreen='true' allowfullscreen></iframe></div>"; |
| 84 |
return $html; |
| 85 |
} |
| 86 |
|
| 87 |
add_shortcode( 'archiveorg-book', 'jetpack_archiveorg_book_shortcode' ); |
| 88 |
|
| 89 |
/** |
| 90 |
* Compose shortcode from archive.org book iframe. |
| 91 |
* |
| 92 |
* @since 4.5.0 |
| 93 |
* |
| 94 |
* @param string $content Post content. |
| 95 |
* |
| 96 |
* @return mixed |
| 97 |
*/ |
| 98 |
function jetpack_archiveorg_book_embed_to_shortcode( $content ) { |
| 99 |
if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/stream/' ) ) { |
| 100 |
return $content; |
| 101 |
} |
| 102 |
|
| 103 |
$regexp = '!<iframe\s+src=[\'"](http|https)://(www.archive|archive)\.org/stream/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)\s></iframe>!i'; |
| 104 |
|
| 105 |
if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) { |
| 106 |
return $content; |
| 107 |
} |
| 108 |
|
| 109 |
foreach ( $matches as $match ) { |
| 110 |
$url = explode( '?', $match[3] ); |
| 111 |
$id = $url[0]; |
| 112 |
|
| 113 |
$params = $match[4]; |
| 114 |
|
| 115 |
$params = wp_kses_hair( $params, array( 'http' ) ); |
| 116 |
|
| 117 |
$width = isset( $params['width'] ) ? absint( $params['width']['value'] ) : 0; |
| 118 |
$height = isset( $params['height'] ) ? absint( $params['height']['value'] ) : 0; |
| 119 |
|
| 120 |
$wh = ''; |
| 121 |
if ( $width && $height ) { |
| 122 |
$wh = ' width=' . $width . ' height=' . $height; |
| 123 |
} |
| 124 |
|
| 125 |
$shortcode = '[archiveorg-book ' . $id . $wh . ']'; |
| 126 |
$content = str_replace( $match[0], $shortcode, $content ); |
| 127 |
} |
| 128 |
|
| 129 |
return $content; |
| 130 |
} |
| 131 |
|
| 132 |
add_filter( 'pre_kses', 'jetpack_archiveorg_book_embed_to_shortcode' ); |
| 133 |
|