lytro.php
| 1 | <?php |
| 2 | /** |
| 3 | * Lytro.com Short Code |
| 4 | * |
| 5 | * Format: |
| 6 | * [lytro photo='202' show_arrow='true' show_border='true' show_first_time_user='true' allow_full_view='true'] |
| 7 | * [lytro username='lytroweb' photo='431119'] |
| 8 | * |
| 9 | * Legend: |
| 10 | * username: the lytro.com username for newer embed format |
| 11 | * photo: the ID or the URL of the photo on lytro.com |
| 12 | * show_arrow: set to false to force-hide the menu in the lower right (not used in v2) |
| 13 | * show_border: set to true to force-show the border |
| 14 | * show_first_time_user: set to false to force-disable the first-time user experience (not used in v2) |
| 15 | * allow_full_view: set to true to allow an external site to have a full-zoom mode (not used in v2) |
| 16 | * enable_help: set to false to hide the question mark/help popup |
| 17 | * |
| 18 | * Output: |
| 19 | * <iframe width="400" height="415" src="https://www.lytro.com/living-pictures/202/embed?showArrow=true&showBorder=true&showFTU=true" frameborder="0" allowfullscreen></iframe> |
| 20 | * <iframe width="400" height="415" src="http://pictures.lytro.com/lytroweb/pictures/431119/embed" frameborder="0" allowfullscreen="" scrolling="no"></iframe> |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * Lytro.com Short Code Attributes Definition |
| 25 | * |
| 26 | * This helper function returns an array all available |
| 27 | * shortcode attributes, their validation method, default |
| 28 | * value and more. |
| 29 | * |
| 30 | * Keys: |
| 31 | * validate: a callable function or regular expression used to validate the input |
| 32 | * default: default value for shortcode attribute |
| 33 | * query_arg: the related lytro query argument name |
| 34 | * |
| 35 | * @since 4.5.0 |
| 36 | */ |
| 37 | function jetpack_lytro_shortcode_attributes() { |
| 38 | return array( |
| 39 | 'username' => array( |
| 40 | 'default' => '', |
| 41 | ), |
| 42 | 'photo' => array( // could be ID or URL, validated separately |
| 43 | 'default' => 0, |
| 44 | ), |
| 45 | 'width' => array( |
| 46 | 'validate' => '#^\d+$#', |
| 47 | 'default' => 400, |
| 48 | ), |
| 49 | 'height' => array( |
| 50 | 'validate' => '#^\d+$#', |
| 51 | 'default' => 415, |
| 52 | ), |
| 53 | 'show_arrow' => array( |
| 54 | 'query_arg' => 'showArrow', |
| 55 | 'validate' => '#^(true|false)$#', |
| 56 | 'default' => 'true', |
| 57 | ), |
| 58 | 'show_border' => array( |
| 59 | 'query_arg' => 'showBorder', |
| 60 | 'validate' => '#^(true|false)$#', |
| 61 | 'default' => 'true', |
| 62 | ), |
| 63 | 'show_first_time_user' => array( |
| 64 | 'query_arg' => 'showFTU', |
| 65 | 'validate' => '#^(true|false)$#', |
| 66 | 'default' => 'true', |
| 67 | ), |
| 68 | 'allow_full_view' => array( |
| 69 | 'query_arg' => 'allowFullView', |
| 70 | 'validate' => '#^(true|false)$#', |
| 71 | 'default' => 'true', |
| 72 | ), |
| 73 | 'enable_help' => array( |
| 74 | 'query_arg' => 'enableHelp', |
| 75 | 'validate' => '#^(true|false)$#', |
| 76 | 'default' => 'true', |
| 77 | ), |
| 78 | 'enable_attribution' => array( |
| 79 | 'query_arg' => 'enableAttribution', |
| 80 | 'validate' => '#^(true|false)$#', |
| 81 | 'default' => 'true', |
| 82 | ), |
| 83 | 'enable_logo' => array( |
| 84 | 'query_arg' => 'enableLogo', |
| 85 | 'validate' => '#^(true|false)$#', |
| 86 | 'default' => 'true', |
| 87 | ), |
| 88 | 'enable_fullscreen' => array( |
| 89 | 'query_arg' => 'enableFullscreen', |
| 90 | 'validate' => '#^(true|false)$#', |
| 91 | 'default' => 'true', |
| 92 | ), |
| 93 | 'enable_play' => array( |
| 94 | 'query_arg' => 'enablePlay', |
| 95 | 'validate' => '#^(true|false)$#', |
| 96 | 'default' => 'true', |
| 97 | ), |
| 98 | 'bg_color' => array( |
| 99 | 'query_arg' => 'bgColor', |
| 100 | 'validate' => '/^#(?:[0-9a-fA-F]{3}){1,2}$/', |
| 101 | 'default' => '', |
| 102 | ), |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Lytro.com Shortcode |
| 108 | * |
| 109 | * Allows embedding Lytro "living pictures" using [lytro photo="200"] or |
| 110 | * [lytro photo="http://www.lytro.com/..."]. Additional attributes |
| 111 | * like show_border, show_arrow, etc have priority over the ones supplied |
| 112 | * in the URL. |
| 113 | * |
| 114 | * @since 4.5.0 |
| 115 | * |
| 116 | * @param array $atts Shortcode attributes |
| 117 | * |
| 118 | * @uses jetpack_lytro_shortcode_attributes() |
| 119 | * @return string Embed HTML or a <!-- commented out error --> |
| 120 | */ |
| 121 | function jetpack_lytro_shortcode_handler( $atts ) { |
| 122 | $defaults = array(); |
| 123 | $attributes = jetpack_lytro_shortcode_attributes(); |
| 124 | foreach ( $attributes as $key => $attribute ) { |
| 125 | if ( isset( $attribute['default'] ) ) { |
| 126 | $defaults[ $key ] = $attribute['default']; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | $atts = shortcode_atts( $defaults, $atts ); |
| 131 | |
| 132 | // There has to at least be a photo attribute. |
| 133 | if ( empty( $atts['photo'] ) ) { |
| 134 | return '<!-- Lytro Shortcode Error: No Photo ID/URL -->'; |
| 135 | } |
| 136 | |
| 137 | // The photo attribute might be a URL |
| 138 | if ( ! is_numeric( $atts['photo'] ) ) { |
| 139 | $atts = array_merge( $atts, jetpack_lytro_shortcode_url_to_atts( $atts['photo'] ) ); |
| 140 | } |
| 141 | |
| 142 | // Validate all attributes by callable function or regular expression. |
| 143 | foreach ( $atts as $key => $value ) { |
| 144 | $attribute = $attributes[ $key ]; |
| 145 | if ( isset( $attribute['validate'] ) ) { |
| 146 | $validate = $attribute['validate']; |
| 147 | $valid = is_callable( $validate ) ? call_user_func( $validate, $value ) : preg_match( $validate, $value ); |
| 148 | if ( ! $valid ) { |
| 149 | $atts[ $key ] = $defaults[ $key ]; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // The photo attribute might have changed, make sure it's still valid. |
| 155 | if ( ! is_numeric( $atts['photo'] ) || ! $atts['photo'] ) { |
| 156 | return '<!-- Lytro Shortcode Error: Invalid Photo ID/URL -->'; |
| 157 | } |
| 158 | |
| 159 | // Build a query which is then appended to the iframe src. |
| 160 | $query_args = array(); |
| 161 | foreach ( $atts as $key => $value ) { |
| 162 | $attribute = $attributes[ $key ]; |
| 163 | if ( isset( $attribute['query_arg'] ) && ! empty( $attribute['query_arg'] ) && ! empty( $value ) ) { |
| 164 | $query_args[ $attribute['query_arg'] ] = $value; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if ( ! empty( $atts['username'] ) ) { |
| 169 | $src = sprintf( 'https://pictures.lytro.com/%s/pictures/%d/embed', $atts['username'], $atts['photo'] ); |
| 170 | } else { |
| 171 | $src = sprintf( 'https://pictures.lytro.com/pictures/%d/embed', $atts['photo'] ); |
| 172 | } |
| 173 | |
| 174 | // Add query args and build the iframe. |
| 175 | $src = add_query_arg( $query_args, $src ); |
| 176 | |
| 177 | return '<iframe width="' . esc_attr( $atts['width'] ) . '" height="' . esc_attr( $atts['height'] ) . '" src="' . esc_url( $src ) . '" frameborder="0" allowfullscreen scrolling="no"></iframe>'; |
| 178 | } |
| 179 | |
| 180 | add_shortcode( 'lytro', 'jetpack_lytro_shortcode_handler' ); |
| 181 | |
| 182 | /** |
| 183 | * Lytro Shortcode URL to Shortcode Attributes |
| 184 | * |
| 185 | * This helper function parses a Lytro.com URL |
| 186 | * and returns an attributes array. |
| 187 | * |
| 188 | * @since 4.5.0 |
| 189 | * |
| 190 | * @uses jetpack_lytro_shortcode_attributes() |
| 191 | */ |
| 192 | function jetpack_lytro_shortcode_url_to_atts( $url ) { |
| 193 | $attributes = jetpack_lytro_shortcode_attributes(); |
| 194 | $atts = array(); |
| 195 | |
| 196 | $url = str_replace( '&', '&', $url ); |
| 197 | |
| 198 | if ( preg_match( '#^https?://(www\.)?lytro\.com/living-pictures/([0-9]+)/?#i', $url, $matches ) ) { |
| 199 | $atts['photo'] = $matches[2]; |
| 200 | } elseif ( preg_match( '#^https?://(www\.)?pictures\.lytro\.com/([^/]+)/pictures/([0-9]+)/?#i', $url, $matches ) ) { |
| 201 | $atts['username'] = $matches[2]; |
| 202 | $atts['photo'] = $matches[3]; |
| 203 | } |
| 204 | |
| 205 | $url = parse_url( $url ); |
| 206 | if ( isset( $url['query'] ) ) { |
| 207 | parse_str( $url['query'], $qargs ); |
| 208 | |
| 209 | // Get the attributes with query_args and fill in the $atts array |
| 210 | foreach ( $attributes as $key => $attribute ) { |
| 211 | if ( isset( $attribute['query_arg'] ) && in_array( $attribute['query_arg'], array_keys( $qargs ) ) ) { |
| 212 | $atts[ $key ] = $qargs[ $attribute['query_arg'] ]; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return $atts; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Lytro Shortcode Reversal |
| 222 | * |
| 223 | * Example |
| 224 | * <iframe width="400" height="415" src="https://www.lytro.com/living-pictures/202/embed?showBorder=true" frameborder="0" allowfullscreen></iframe> |
| 225 | * <iframe width="400" height="415" src="http://pictures.lytro.com/lytroweb/pictures/431128/embed" frameborder="0" allowfullscreen="" scrolling="no"></iframe> |
| 226 | * |
| 227 | * Converts to: |
| 228 | * [lytro photo="202" show_border="true" width="400" height="415"] |
| 229 | * |
| 230 | * @since 4.5.0 |
| 231 | * |
| 232 | * @uses jetpack_lytro_shortcode_url_to_atts() |
| 233 | * @uses wpcom_shortcodereverse_parseattr() |
| 234 | */ |
| 235 | function wpcom_shortcodereverse_lytro( $atts ) { |
| 236 | $atts = wpcom_shortcodereverse_parseattr( $atts ); |
| 237 | $shortcode_atts = array(); |
| 238 | |
| 239 | // Grab the src URL and convert to shortcode attributes |
| 240 | if ( $atts['src'] ) { |
| 241 | $shortcode_atts = jetpack_lytro_shortcode_url_to_atts( $atts['src'] ); |
| 242 | } |
| 243 | |
| 244 | // Width and height too |
| 245 | if ( $atts['width'] ) { |
| 246 | $shortcode_atts['width'] = $atts['width']; |
| 247 | } |
| 248 | if ( $atts['height'] ) { |
| 249 | $shortcode_atts['height'] = $atts['height']; |
| 250 | } |
| 251 | |
| 252 | // Generate the shortcode. |
| 253 | $shortcode = ''; |
| 254 | foreach ( $shortcode_atts as $key => $value ) { |
| 255 | $shortcode .= " $key='" . esc_attr( $value ) . "'"; |
| 256 | } |
| 257 | $shortcode = "[lytro {$shortcode}]"; |
| 258 | |
| 259 | return $shortcode; |
| 260 | } |
| 261 | |
| 262 | Filter_Embedded_HTML_Objects::register( '#^https?://(www\.)?lytro\.com/living-pictures/#i', 'wpcom_shortcodereverse_lytro', true ); |
| 263 | Filter_Embedded_HTML_Objects::register( '#^https?://(www\.)?pictures\.lytro\.com/([^/]+)/pictures/([0-9]+)/embed#i', 'wpcom_shortcodereverse_lytro', true ); |
| 264 | |
| 265 | /** |
| 266 | * Register Embed Handler |
| 267 | * |
| 268 | * Registers a WordPress Embed handler to allow embedding |
| 269 | * Lytro images by publishing the Lytro URL on a line by itself. |
| 270 | * |
| 271 | * @since 4.5.0 |
| 272 | * |
| 273 | * @uses wp_embed_register_handler |
| 274 | */ |
| 275 | function jetpack_lytro_register_embed_handler() { |
| 276 | wp_embed_register_handler( 'lytro', '#^https?://(www\.)?lytro\.com/living-pictures/([0-9]+)/?#i', 'jetpack_lytro_embed_handler' ); |
| 277 | wp_embed_register_handler( 'lytro-v2', '#^https?://(www\.)?pictures\.lytro\.com/([^/]+)/pictures/([0-9]+)/?#i', 'jetpack_lytro_embed_handler' ); |
| 278 | } |
| 279 | |
| 280 | add_action( 'init', 'jetpack_lytro_register_embed_handler' ); |
| 281 | |
| 282 | /** |
| 283 | * Lytro Embed Handler |
| 284 | * |
| 285 | * The embed handler function which converts a Lytro URL |
| 286 | * on a line by itself into an embedded Lytro image. |
| 287 | * |
| 288 | * @since 4.5.0 |
| 289 | * |
| 290 | * @see jetpack_lytro_register_embed_handler |
| 291 | * @uses jetpack_lytro_shortcode_url_to_atts |
| 292 | * @uses jetpack_lytro_shortcode_handler |
| 293 | */ |
| 294 | function jetpack_lytro_embed_handler( $matches, $attr, $url, $rawattr ) { |
| 295 | return jetpack_lytro_shortcode_handler( jetpack_lytro_shortcode_url_to_atts( $url ) ); |
| 296 | } |
| 297 |