smart-youtube-embed.php
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/*
Plugin Name: Smart YouTube httpv:// URLs
Description: Parse video URLs used by the Smart YouTube plugin
Version: 1.0
Author: Janis Elsts
ModuleCategory: parser
ModuleClassName: blcSmartYouTubeURL
ModuleContext: on-demand
ModuleLazyInit: true
ModulePriority: 100
*/
if ( ! class_exists( 'blcPlaintextUrlBase' ) ) {
require_once 'plaintext-url-parser-base.php';
}
class blcSmartYouTubeURL extends blcPlaintextUrlBase {
protected function validate_url( $url ) {
//Ignore invalid URLs.
$parts = @parse_url( $url );
if ( empty( $parts ) ) {
return false;
}
//We only care about httpv[hp]:// URLs as used by the Smart YouTube plugin.
if ( stripos( $parts['scheme'], 'httpv' ) !== 0 ) {
return false;
}
//The URL should contain a domain name. AFAIK, Smart YouTube doesn't accept relative URLs.
if ( empty( $parts['host'] ) || ! strpos( $parts['host'], '.' ) ) {
return false;
}
//Replace the plugin-specific scheme with plain old http://.
$url = preg_replace( '#^httpv[^:]*?:#i', 'http:', $url );
return $url;
}
/**
* Change all occurrences of a given URLs to a new URL.
*
* @param string $content Look for URLs in this string.
* @param string $new_url Change them to this URL.
* @param string $old_url The URL to look for.
* @param string $old_raw_url The raw, not-normalized URL. Optional.
*
* @return array|WP_Error If successful, the return value will be an associative array with two
* keys : 'content' - the modified content, and 'raw_url' - the new raw, non-normalized URL used
* for the modified links. In most cases, the returned raw_url will be equal to the new_url.
*/
function edit( $content, $new_url, $old_url, $old_raw_url = '' ) {
//If the user manually prefixes the URL with "httpv://" or other Smart YouTube scheme
//then use the URL as-is. Otherwise change the scheme to the prefix from the old URL (if available).
$new_scheme = @parse_url( $new_url, PHP_URL_SCHEME );
if ( empty( $new_scheme ) || ( stripos( $new_scheme, 'httpv' ) !== 0 ) ) {
if ( ! empty( $old_raw_url ) ) {
$scheme = parse_url( $old_raw_url, PHP_URL_SCHEME );
} else {
$scheme = 'httpv';
}
if ( empty( $new_scheme ) ) {
$new_url = $scheme . '://' . $new_url;
} else {
$new_url = preg_replace(
'#^' . preg_quote( $new_scheme ) . '://#i',
$scheme . '://',
$new_url
);
}
}
return parent::edit( $content, $new_url, $old_url, $old_raw_url );
}
}