This repository has been archived by the owner on Feb 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetter-embed.php
177 lines (122 loc) · 4.6 KB
/
better-embed.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
Plugin Name: Better Embed
Version: 1.0
Plugin URI: http://wordpress.pressible.org/plugin-embed-code-field
Description: Adds a field in which to paste embed codes. Built to be XSS safe. Adapted from the custom fields code of Steve Taylor (http://sltaylor.co.uk/blog/control-your-own-wordpress-custom-fields/).
Author: EdLab Publishing
Author URI: http://edlab.tc.columbia.edu
*/
# functions
add_action( 'admin_menu', 'createEmbedField' );
function createEmbedField() {
if ( function_exists( 'add_meta_box' ) )
add_meta_box( 'embed-field', 'Embed Code (for featured media)', 'displayEmbedField', 'post', 'normal', 'high' );
}
add_action( 'save_post', 'saveEmbedField', 1, 2 );
function displayEmbedField() {
global $post;
print '<div>';
wp_nonce_field( 'embed-field', 'embed-field_wpnonce', false, true );
# check if this is a post and the user can edit it
if (
(basename( $_SERVER['SCRIPT_FILENAME'] )=="post-new.php" || $post->post_type=="post") and
current_user_can( 'edit_post', $post->ID )
){
# print out the embed field
?>
<label for="_embed" class="screen-reader-text"><b>Paste Embed Code</b></label>
<textarea style="margin:0;height:4em;width:98%;color:#999;font-size:.8em;overflow:hidden" name="_embed" id="_embed" columns="40" rows="1"><?php print htmlspecialchars( get_post_meta( $post->ID, '_embed', true ) ); ?></textarea>
<p>
Paste <em>one</em> embed code from any service to be included with your post.
Broken, incomplete, or duplicate codes will be removed. Please note that
only <a href="http://codex.wordpress.org/Embeds#Okay.2C_So_What_Sites_Can_I_Embed_From.3F">services that support oEmbed</a> are allowed in the main content area. <a href="http://pressible.org/header/faq#Images">Learn
more</a> about embedding media.
</p>
<?php
}
print '</div>';
}
function saveEmbedField( $post_id, $post ) {
if (
wp_verify_nonce( $_POST[ 'embed-field_wpnonce' ], 'embed-field' ) and
current_user_can( 'edit_post', $post_id ) and
$post->post_type == 'post'
){
if ( isset( $_POST[ '_embed' ] ) and trim( $_POST[ '_embed' ] ) ) {
# allowed tags and attributes
$filter = array(
'object' => array(
'width' => array(),
'height' => array()
),
'param' => array(
'name' => array(),
'value' => array()
),
'embed' => array(
'width' => array(),
'height' => array(),
'src' => array(),
'type' => array(),
'allowscriptaccess' => array(),
'allowfullscreen' => array(),
'allownetworking' => array(),
'pluginspace' => array(),
'wmode' => array(),
'flashvars' => array()
),
);
$filtered = $_POST[ '_embed' ];
$filtered = preg_replace("/=(.)'([^\']*)(.)'/", '=$1"$2$3"', $filtered);
$filtered = wp_kses($filtered, $filter, array('http'));
$filtered = preg_replace('/>[^<]+</','><', $filtered);
$filtered = preg_replace('/^[^<]+/','', $filtered);
$filtered = preg_replace('/[^>]+$/','', $filtered);
if(
preg_match("/\<(object|embed)[^>]*>/", $filtered, $type) and
$end = stripos($filtered, '</' . $type[1] . '>')
)
update_post_meta( $post_id, '_embed', substr($filtered, 0, $end + strlen($type[1]) + 3));
else
delete_post_meta( $post_id, '_embed' );
}
else
delete_post_meta( $post_id, '_embed' );
}
}
# template tag
function the_embed($new_total_width=400, $print=TRUE){
global $post;
# get the embed meta data
$embed = get_post_meta( $post->ID, '_embed', true );
# check if dimensions are specified
if(preg_match("/width=\"(\d+)/", $embed, $width) and preg_match("/height=\"(\d+)/", $embed, $height)){
$original_width = $width[1];
$embed = preg_replace("/(width=\")\d+/",'${1}' . $new_total_width, $embed);
$embed = preg_replace("/(height=\")\d+/",'${1}' . ceil($height[1] * $new_total_width / $original_width), $embed);
# pairs that need to scale proportionally without changing margins
$pairs = array(
'(vw=)(\d+)' => '(vh=)(\d+)',
'(=)(\d+)(?=x)' => '(=\d+x)(\d+)',
);
foreach($pairs as $w => $h){
if(preg_match("/$w/", $embed, $width) and $width[2] and preg_match("/$h/", $embed, $height)){
$new_width = $new_total_width - $original_width + $width[2];
$new_height = ceil($height[2] * $new_width / $width[2]);
$embed = preg_replace("/$w/", '${1}' . $new_width, $embed);
$embed = preg_replace("/$h/", '${1}' . $new_height, $embed);
}
}
}
if($print)
print $embed;
return $embed;
}
function embed_content($content){
if($embed = the_embed(600, FALSE))
$content = "<p>$embed</p>\n$content";
return $content;
}
add_filter('the_content', 'embed_content');
?>