Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Archive.org Shortcode |
||
| 4 | * |
||
| 5 | * Usage: |
||
| 6 | * [archiveorg-book goodytwoshoes00newyiala] |
||
| 7 | * [archiveorg-book http://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 | |||
| 13 | /** |
||
| 14 | * Get ID of requested archive.org book embed. |
||
| 15 | * |
||
| 16 | * @since 4.5.0 |
||
| 17 | * |
||
| 18 | * @param $atts |
||
| 19 | * |
||
| 20 | * @return int|string |
||
| 21 | */ |
||
| 22 | View Code Duplication | function jetpack_shortcode_get_archiveorg_book_id( $atts ) { |
|
|
0 ignored issues
–
show
|
|||
| 23 | if ( isset( $atts[0] ) ) { |
||
| 24 | $atts[0] = trim( $atts[0] , '=' ); |
||
| 25 | if ( preg_match( '#archive.org/stream/(.+)/?$#i', $atts[0], $match ) ) { |
||
| 26 | $id = $match[1]; |
||
| 27 | } else { |
||
| 28 | $id = $atts[0]; |
||
| 29 | } |
||
| 30 | return $id; |
||
| 31 | } |
||
| 32 | return 0; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Convert an archive.org book shortcode into an embed code. |
||
| 37 | * |
||
| 38 | * @since 4.5.0 |
||
| 39 | * |
||
| 40 | * @param array $atts An array of shortcode attributes. |
||
| 41 | * @return string The embed code for the Archive.org book |
||
| 42 | */ |
||
| 43 | function jetpack_archiveorg_book_shortcode( $atts ) { |
||
| 44 | global $content_width; |
||
| 45 | |||
| 46 | if ( isset( $atts[0] ) && empty( $atts['id'] ) ) { |
||
| 47 | $atts['id'] = jetpack_shortcode_get_archiveorg_book_id( $atts ); |
||
| 48 | } |
||
| 49 | |||
| 50 | $atts = shortcode_atts( array( |
||
| 51 | 'id' => '', |
||
| 52 | 'width' => 480, |
||
| 53 | 'height' => 430, |
||
| 54 | ), $atts ); |
||
| 55 | |||
| 56 | if ( ! $atts['id'] ) { |
||
| 57 | return '<!-- error: missing archive.org book ID -->'; |
||
| 58 | } |
||
| 59 | |||
| 60 | $id = $atts['id']; |
||
| 61 | |||
| 62 | View Code Duplication | if ( ! $atts['width'] ) { |
|
| 63 | $width = absint( $content_width ); |
||
| 64 | } else { |
||
| 65 | $width = intval( $atts['width'] ); |
||
| 66 | } |
||
| 67 | |||
| 68 | View Code Duplication | if ( ! $atts['height'] ) { |
|
| 69 | $height = round( ( $width / 640 ) * 360 ); |
||
| 70 | } else { |
||
| 71 | $height = intval( $atts['height'] ); |
||
| 72 | } |
||
| 73 | |||
| 74 | $url = esc_url( set_url_scheme( "http://archive.org/stream/{$id}?ui=embed#mode/1up" ) ); |
||
| 75 | |||
| 76 | $html = "<div class='embed-archiveorg-book' style='text-align:center;'><iframe src='$url' width='$width' height='$height' style='border:0;' webkitallowfullscreen='true' mozallowfullscreen='true' allowfullscreen></iframe></div>"; |
||
| 77 | return $html; |
||
| 78 | } |
||
| 79 | |||
| 80 | add_shortcode( 'archiveorg-book', 'jetpack_archiveorg_book_shortcode' ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Compose shortcode from archive.org book iframe. |
||
| 84 | * |
||
| 85 | * @since 4.5.0 |
||
| 86 | * |
||
| 87 | * @param string $content |
||
| 88 | * |
||
| 89 | * @return mixed |
||
| 90 | */ |
||
| 91 | function jetpack_archiveorg_book_embed_to_shortcode( $content ) { |
||
| 92 | if ( ! is_string( $content ) || false === stripos( $content, 'archive.org/stream/' ) ) { |
||
| 93 | return $content; |
||
| 94 | } |
||
| 95 | |||
| 96 | $regexp = '!<iframe\s+src=[\'"](http|https)://(www.archive|archive)\.org/stream/([^\'"]+)[\'"]((?:\s+\w+(=[\'"][^\'"]*[\'"])?)*)\s></iframe>!i'; |
||
| 97 | |||
| 98 | if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) { |
||
| 99 | return $content; |
||
| 100 | } |
||
| 101 | |||
| 102 | foreach ( $matches as $match ) { |
||
| 103 | $url = explode( '?', $match[3] ); |
||
| 104 | $id = $url[0]; |
||
| 105 | |||
| 106 | $params = $match[4]; |
||
| 107 | |||
| 108 | $params = wp_kses_hair( $params, array( 'http' ) ); |
||
| 109 | |||
| 110 | $width = isset( $params['width'] ) ? absint( $params['width']['value'] ) : 0; |
||
| 111 | $height = isset( $params['height'] ) ? absint( $params['height']['value'] ) : 0; |
||
| 112 | |||
| 113 | $wh = ''; |
||
| 114 | if ( $width && $height ) { |
||
| 115 | $wh = ' width=' . $width . ' height=' . $height; |
||
| 116 | } |
||
| 117 | |||
| 118 | $shortcode = '[archiveorg-book ' . $id . $wh . ']'; |
||
| 119 | $content = str_replace( $match[0], $shortcode, $content ); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $content; |
||
| 123 | } |
||
| 124 | |||
| 125 | add_filter( 'pre_kses', 'jetpack_archiveorg_book_embed_to_shortcode' ); |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.