Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
41.07% |
23 / 56 |
|
0.00% |
0 / 3 |
CRAP | n/a |
0 / 0 |
|
| jetpack_shortcode_get_archiveorg_book_id | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| jetpack_archiveorg_book_shortcode | |
88.46% |
23 / 26 |
|
0.00% |
0 / 1 |
6.06 | |||
| jetpack_archiveorg_book_embed_to_shortcode | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
90 | |||
| 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 automattic/jetpack |
| 13 | */ |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit( 0 ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Get ID of requested archive.org book 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_book_id( $atts ) { |
| 29 | if ( isset( $atts[0] ) ) { |
| 30 | $atts[0] = trim( $atts[0], '=' ); |
| 31 | if ( preg_match( '#archive.org/stream/(.+)/?$#i', $atts[0], $match ) ) { |
| 32 | $id = $match[1]; |
| 33 | } else { |
| 34 | $id = $atts[0]; |
| 35 | } |
| 36 | return $id; |
| 37 | } |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Convert an archive.org book 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 book |
| 48 | */ |
| 49 | function jetpack_archiveorg_book_shortcode( $atts ) { |
| 50 | global $content_width; |
| 51 | |
| 52 | if ( isset( $atts[0] ) && empty( $atts['id'] ) ) { |
| 53 | $atts['id'] = jetpack_shortcode_get_archiveorg_book_id( $atts ); |
| 54 | } |
| 55 | |
| 56 | $atts = shortcode_atts( |
| 57 | array( |
| 58 | 'id' => '', |
| 59 | 'width' => 480, |
| 60 | 'height' => 430, |
| 61 | ), |
| 62 | $atts |
| 63 | ); |
| 64 | |
| 65 | if ( ! $atts['id'] ) { |
| 66 | return '<!-- error: missing archive.org book ID -->'; |
| 67 | } |
| 68 | |
| 69 | $id = $atts['id']; |
| 70 | |
| 71 | if ( ! $atts['width'] ) { |
| 72 | $width = absint( $content_width ); |
| 73 | } else { |
| 74 | $width = (int) $atts['width']; |
| 75 | } |
| 76 | |
| 77 | if ( ! $atts['height'] ) { |
| 78 | $height = round( ( $width / 640 ) * 360 ); |
| 79 | } else { |
| 80 | $height = (int) $atts['height']; |
| 81 | } |
| 82 | |
| 83 | return sprintf( |
| 84 | '<div class="embed-archiveorg-book" style="text-align:center;"><iframe title="%s" src="%s" width="%s" height="%s" style="border:0;" webkitallowfullscreen="true" mozallowfullscreen="true" allowfullscreen></iframe></div>', |
| 85 | esc_attr__( 'Archive.org Book', 'jetpack' ), |
| 86 | esc_url( "https://archive.org/stream/{$id}?ui=embed#mode/1up" ), |
| 87 | esc_attr( $width ), |
| 88 | esc_attr( $height ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | add_shortcode( 'archiveorg-book', 'jetpack_archiveorg_book_shortcode' ); |
| 93 | |
| 94 | /** |
| 95 | * Compose shortcode from archive.org book iframe. |
| 96 | * |
| 97 | * @since 4.5.0 |
| 98 | * |
| 99 | * @param string $content Post content. |
| 100 | * |
| 101 | * @return mixed |
| 102 | */ |
| 103 | function jetpack_archiveorg_book_embed_to_shortcode( $content ) { |
| 104 | if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/stream/' ) ) { |
| 105 | return $content; |
| 106 | } |
| 107 | |
| 108 | $regexp = '!<iframe\s+src=[\'"](http|https)://(www.archive|archive)\.org/stream/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)\s></iframe>!i'; |
| 109 | |
| 110 | if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) { |
| 111 | return $content; |
| 112 | } |
| 113 | |
| 114 | foreach ( $matches as $match ) { |
| 115 | $url = explode( '?', $match[3] ); |
| 116 | $id = $url[0]; |
| 117 | |
| 118 | $params = $match[4]; |
| 119 | |
| 120 | $params = wp_kses_hair( $params, array( 'http' ) ); |
| 121 | |
| 122 | $width = isset( $params['width'] ) ? absint( $params['width']['value'] ) : 0; |
| 123 | $height = isset( $params['height'] ) ? absint( $params['height']['value'] ) : 0; |
| 124 | |
| 125 | $wh = ''; |
| 126 | if ( $width && $height ) { |
| 127 | $wh = ' width=' . $width . ' height=' . $height; |
| 128 | } |
| 129 | |
| 130 | $shortcode = '[archiveorg-book ' . $id . $wh . ']'; |
| 131 | $content = str_replace( $match[0], $shortcode, $content ); |
| 132 | } |
| 133 | |
| 134 | return $content; |
| 135 | } |
| 136 | |
| 137 | if ( jetpack_shortcodes_should_hook_pre_kses() ) { |
| 138 | add_filter( 'pre_kses', 'jetpack_archiveorg_book_embed_to_shortcode' ); |
| 139 | } |