Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Youtube functions: add jetpack namespace and move into the lib/ folder #126

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions functions.compat.php → _inc/lib/youtube.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
<?php

if ( !function_exists( 'rawurlencode_deep' ) ) :
/**
* Navigates through an array and raw encodes the values to be used in a URL.
*
* @since WordPress 3.4.0
*
* @param array|string $value The array or string to be encoded.
* @return array|string $value The encoded array (or string from the callback).
*/
function rawurlencode_deep( $value ) {
return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode( $value );
}
endif;

if ( !function_exists( 'get_youtube_id' ) ) :
if ( !function_exists( 'jetpack_get_youtube_id' ) ) :
/**
* @param $url Can be just the $url or the whole $atts array
* @return bool|mixed The Youtube video ID
*/
function get_youtube_id( $url ) {
function jetpack_get_youtube_id( $url ) {
// Do we have an $atts array? Get first att
if ( is_array( $url ) )
$url = $url[0];

$url = youtube_sanitize_url( $url );
$url = jetpack_youtube_sanitize_url( $url );
$url = parse_url( $url );
$id = false;

Expand All @@ -46,14 +32,14 @@ function get_youtube_id( $url ) {
}
endif;

if ( !function_exists( 'youtube_sanitize_url' ) ) :
if ( !function_exists( 'jetpack_youtube_sanitize_url' ) ) :
/**
* Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
*
* @param string $url
* @return string The normalized URL
*/
function youtube_sanitize_url( $url ) {
function jetpack_youtube_sanitize_url( $url ) {
$url = trim( $url, ' "' );
$url = trim( $url );
$url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&amp;', '&#038;', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );
Expand All @@ -67,4 +53,4 @@ function youtube_sanitize_url( $url ) {

return $url;
}
endif;
endif;
4 changes: 3 additions & 1 deletion class.media-summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ static function get( $post_id, $blog_id = 0, $args = array() ) {

$extract = Jetpack_Media_Meta_Extractor::extract( $blog_id, $post_id, Jetpack_Media_Meta_Extractor::ALL );

jetpack_require_lib( 'youtube' );

if ( empty( $extract['has'] ) )
return $return;

Expand Down Expand Up @@ -97,7 +99,7 @@ static function get( $post_id, $blog_id = 0, $args = array() ) {
$return['video'] = 'http://' . $embed;
$return['secure']['video'] = self::https( $return['video'] );
if ( strstr( $embed, 'youtube' ) ) {
$return['image'] = self::get_video_poster( 'youtube', get_youtube_id( $return['video'] ) );
$return['image'] = self::get_video_poster( 'youtube', jetpack_get_youtube_id( $return['video'] ) );
$return['secure']['image'] = self::https( $return['image'] );
} else if ( strstr( $embed, 'vimeo' ) ) {
$poster_image = get_post_meta( $post_id, 'vimeo_poster_image', true );
Expand Down
1 change: 0 additions & 1 deletion jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-heartbeat.php' );
require_once( JETPACK__PLUGIN_DIR . 'class.photon.php' );
require_once( JETPACK__PLUGIN_DIR . 'functions.photon.php' );
require_once( JETPACK__PLUGIN_DIR . 'functions.compat.php' );
require_once( JETPACK__PLUGIN_DIR . 'functions.gallery.php' );
require_once( JETPACK__PLUGIN_DIR . 'require-lib.php' );

Expand Down
5 changes: 4 additions & 1 deletion modules/publicize/enhanced-open-graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
if ( ! class_exists( 'Jetpack_Media_Summary' ) && defined('IS_WPCOM') && IS_WPCOM )
include WP_CONTENT_DIR . '/lib/class.wpcom-media-summary.php';

// Add Youtube utilities
jetpack_require_lib( 'youtube' );

/**
* Better OG Image Tags for Image Post Formats
*/
Expand Down Expand Up @@ -90,7 +93,7 @@ function enhanced_og_video( $tags ) {

if ( preg_match( '/((youtube|vimeo)\.com|youtu.be)/', $video_url ) ) {
if ( strstr( $video_url, 'youtube' ) ) {
$id = get_youtube_id( $video_url );
$id = jetpack_get_youtube_id( $video_url );
$video_url = 'http://www.youtube.com/v/' . $id . '?version=3&autohide=1';
$secure_video_url = 'https://www.youtube.com/v/' . $id . '?version=3&autohide=1';
} else if ( strstr( $video_url, 'vimeo' ) ) {
Expand Down
70 changes: 5 additions & 65 deletions modules/shortcodes/youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* @example [youtube=http://www.youtube.com/watch?v=wq0rXGLs0YM&amp;fs=1&amp;hl=bg_BG]
*/

// Add Youtube utilities
jetpack_require_lib( 'youtube' );

/**
* Replaces YouTube embeds with YouTube shortcodes.
*
Expand Down Expand Up @@ -110,29 +113,6 @@ function youtube_link_callback( $matches ) {
return "\n" . youtube_id( $matches[0] ) . "\n";
}

/**
* Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
*
* @param string $url
* @return string The normalized URL
*/
if ( !function_exists( 'youtube_sanitize_url' ) ) :
function youtube_sanitize_url( $url ) {
$url = trim( $url, ' "' );
$url = trim( $url );
$url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&amp;', '&#038;', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );

// Replace any extra question marks with ampersands - the result of a URL like "http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US" being passed in.
$query_string_start = strpos( $url, "?" );

if ( false !== $query_string_start ) {
$url = substr( $url, 0, $query_string_start + 1 ) . str_replace( "?", "&", substr( $url, $query_string_start + 1 ) );
}

return $url;
}
endif;

/*
* url can be:
* http://www.youtube.com/embed/videoseries?list=PL94269DA08231042B&amp;hl=en_US
Expand All @@ -144,57 +124,17 @@ function youtube_sanitize_url( $url ) {
* http://youtu.be/Rrohlqeir5E
*/

/**
* Same as get_youtube_id(), but with the prefix that function should've had.
*/
function jetpack_shortcode_get_youtube_id( $url ) {
return get_youtube_id( $url );
}

/**
* @param $url Can be just the $url or the whole $atts array
* @return bool|mixed The Youtube video ID
*/
if ( !function_exists( 'get_youtube_id' ) ) :
function get_youtube_id( $url ) {

// Do we have an $atts array? Get first att
if ( is_array( $url ) )
$url = $url[0];

$url = youtube_sanitize_url( $url );
$url = parse_url( $url );
$id = false;

if ( ! isset( $url['query'] ) )
return false;

parse_str( $url['query'], $qargs );

if ( ! isset( $qargs['v'] ) && ! isset( $qargs['list'] ) )
return false;

if ( isset( $qargs['list'] ) )
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['list'] );

if ( empty( $id ) )
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['v'] );

return $id;
}
endif;

/**
* Converts a YouTube URL into an embedded YouTube video.
*/
function youtube_id( $url ) {
if ( apply_filters( 'jetpack_bail_on_shortcode', false, 'youtube' ) )
return '';

if ( ! $id = get_youtube_id( $url ) )
if ( ! $id = jetpack_get_youtube_id( $url ) )
return '<!--YouTube Error: bad URL entered-->';

$url = youtube_sanitize_url( $url );
$url = jetpack_youtube_sanitize_url( $url );
$url = parse_url( $url );

if ( ! isset( $url['query'] ) )
Expand Down