PluginProbe
Code Embed / 2.2.2
Code Embed v2.2.2
2.6.4 2.6.3 2.6.2 2.6.1 trunk 1.0 1.1 1.2 1.3 1.4.1 1.5.1 1.6.1 2.0.2 2.1.2 2.2.2 2.3.9 2.4 2.5.1 2.5.2 2.6
simple-embed-code / includes / add-embeds.php

add-embeds.php in Code Embed 2.2.2, at includes/add-embeds.php

317 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Add Embed to Posts
4 *
5 * Functions to add embed code to posts
6 *
7 * @package simple-embed-code
8 */
9
10 /**
11 * Add filter to add embed code
12 *
13 * Filter to add embed to any posts
14 *
15 * @since 1.5
16 *
17 * @uses ce_get_embed_code Get embed code from other posts
18 *
19 * @param string $content Post content without embedded code
20 * @return string Post content with embedded code
21 */
22
23 function ce_filter( $content ) {
24
25 global $post;
26
27 // Set initial values
28
29 $options = get_option( 'artiss_code_embed' );
30 $found_pos = strpos( $content, $options[ 'opening_ident' ] . $options[ 'keyword_ident' ], 0 );
31 $prefix_len = strlen( $options[ 'opening_ident' ] . $options[ 'keyword_ident' ] );
32
33 // Loop around the post content looking for all requests for code embeds
34
35 while ( $found_pos !== false ) {
36
37 // Get the position of the closing identifier - ignore if one is not found
38
39 $end_pos = strpos( $content, $options[ 'closing_ident' ], $found_pos + $prefix_len );
40 if ( $end_pos !== false ) {
41
42 // Extract the suffix
43
44 $suffix_len = $end_pos - ( $found_pos + $prefix_len );
45 if ( $suffix_len == 0 ) {
46 $suffix = '';
47 } else {
48 $suffix = substr( $content, $found_pos + $prefix_len, $suffix_len );
49 }
50 $full_suffix = $suffix;
51
52 // Check for responsive part of suffix
53
54 $responsive = false;
55 $max_width = false;
56 $res_pos = false;
57 if ( strlen( $suffix ) > 1 ) { $res_pos = strpos( $suffix, '_RES', 1 ); }
58
59 if ( $res_pos !== false ) {
60
61 // If responsive section found, check it's at the end of has an underscore afterwards
62 // Otherwise it may be part of something else
63
64 if ( $res_pos == strlen( $suffix) - 4 ) {
65 $responsive = true;
66 } else {
67 if ( substr( $suffix, $res_pos+4, 1 ) == '_' ) {
68 $responsive = true;
69 $max_width = substr( $suffix, $res_pos + 5 );
70 if ( !is_numeric( $max_width ) ) { $max_width = ''; }
71 }
72 }
73
74 if ( $responsive ) { $suffix = substr( $suffix, 0, $res_pos ); }
75 }
76
77 // Get the custom field data and replace in the post
78
79 $search = $options[ 'opening_ident' ] . $options[ 'keyword_ident' ] . $suffix . $options[ 'closing_ident' ];
80
81 // Get the meta for the current post
82
83 $post_meta = get_post_meta( $post -> ID, $options[ 'keyword_ident' ].$suffix, false );
84 if ( isset( $post_meta[ 0 ] ) ) {
85 $html = $post_meta[ 0 ];
86 } else {
87 // No meta found, so look for it elsewhere
88 $html = ce_get_embed_code( $options[ 'keyword_ident' ], $suffix );
89 }
90
91 // Build the string to search for
92
93 $search = $options[ 'opening_ident' ] . $options[ 'keyword_ident' ] . $full_suffix . $options[ 'closing_ident' ];
94
95 // Build the string of code to replace with
96
97 $replace = ce_generate_code( $html, 'Code Embed', $responsive, $max_width, $options[ 'debug' ] );
98
99 // Now modify all references
100
101 $content = str_replace( $search , $replace, $content );
102
103 }
104 $found_pos = strpos( $content, $options[ 'opening_ident' ] . $options[ 'keyword_ident' ], $found_pos + 1 );
105 }
106
107 // Loop around the post content looking for HTTP addresses
108
109 $content = ce_quick_replace( $content, $options, 'http://' );
110
111 // Loop around the post content looking for HTTPS addresses
112
113 $content = ce_quick_replace( $content, $options, 'https://' );
114
115 return $content;
116 }
117
118 add_filter( 'the_content', 'ce_filter' );
119 add_filter( 'widget_content', 'ce_filter' );
120
121 /**
122 * Quick Replace
123 *
124 * Function to do a quick search/replace of the content
125 *
126 * @since 2.0
127 *
128 * @param $content string The content
129 * @param $options string The options array
130 * @param $search string The string to search for
131 * @return string The updated content
132 */
133
134 function ce_quick_replace( $content = '', $options = '', $search = '' ) {
135
136 $start_pos = strpos( $content, $options[ 'opening_ident' ] . $search, 0 );
137 $prefix_len = strlen( $options[ 'opening_ident' ] );
138
139 while ( $start_pos !== false ) {
140
141 $start_pos = $start_pos + strlen( $options[ 'opening_ident' ] );
142 $end_pos = strpos( $content, $options[ 'closing_ident' ], $start_pos );
143 if ( $end_pos !== false ) {
144 $url = substr( $content, $start_pos, $end_pos - $start_pos );
145 $file = ce_get_file( $url );
146 $content = str_replace ( $options[ 'opening_ident' ] . $url . $options[ 'closing_ident' ], $file[ 'file' ], $content );
147 }
148 $start_pos = strpos( $content, $options[ 'opening_ident' ] . $search, $found_pos );
149
150 }
151
152 return $content;
153 }
154
155 /**
156 * Generate Embed Code
157 *
158 * Function to generate the embed code that will be output
159 *
160 * @since 2.0
161 *
162 * @param $html string The embed code (required)
163 * @param $plugin_name string The name of the plugin (required)
164 * @param $responsive string Responsive output required? (optional)
165 * @param $max_width string Maximum width of responsive output (optional)
166 * @param $debug boolean Whether to suppress debug output (1) or not
167 * @return string The embed code
168 */
169
170 function ce_generate_code( $html, $plugin_name, $responsive = '', $max_width = '', $debug = '' ) {
171
172 $code = "\n";
173 if ( $debug != 1 ) { $code .= '<!-- ' . $plugin_name . ' v' . code_embed_version . " -->\n"; }
174
175 if ( $max_width !== false ) { $code .= '<div style="width: ' . $max_width . 'px; max-width: 100%">'; }
176
177 if ( $responsive ) { $code .= '<div class="ce-video-container">'; }
178
179 $code .= $html;
180
181 if ( $responsive ) { $code .= '</div>'; }
182
183 if ( $max_width !== false ) { $code .= '</div>'; }
184
185 $code .= "\n";
186 if ( $debug != 1 ) { $code .= '<!-- End of ' . $plugin_name . " code -->\n"; }
187
188 return $code;
189 }
190
191 /**
192 * Get the Global Embed Code
193 *
194 * Function to look for and, if available, return the global embed code
195 *
196 * @since 1.6
197 *
198 * @uses ce_report_error Generate an error message
199 *
200 * @param $ident string The embed code opening identifier
201 * @param $suffix string The embed code suffix
202 * @return string The embed code (or error)
203 */
204
205 function ce_get_embed_code( $ident, $suffix ) {
206
207 // Meta was not found in current post so look across meta table - find the number of distinct code results
208
209 $meta_name = $ident . $suffix;
210 global $wpdb;
211 $unique_records = $wpdb -> get_results( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = '" . $meta_name . "'" );
212 $records = $wpdb -> num_rows;
213
214 if ( $records > 0 ) {
215
216 // Results were found
217
218 $meta = $wpdb -> get_results( "SELECT meta_value, post_title, ID FROM $wpdb->postmeta, $wpdb->posts WHERE meta_key = '" . $meta_name . "' AND post_id = ID" );
219 $total_records = $wpdb -> num_rows;
220
221 if ( $records == 1 ) {
222
223 // Only one unique code result returned so assume this is the global embed
224
225 foreach ( $meta as $meta_data ) {
226 $html = $meta_data -> meta_value;
227 }
228
229 } else {
230
231 // More than one unique code result returned, so output the list of posts
232
233 $error = sprintf( __( 'Cannot use %s as a global code as it is being used to store %d unique pieces of code in %d posts - <a href="%s">click here</a> for more details', 'simple-embed-code' ), $meta_name, $records, $total_records, get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=ce-search&amp;suffix=' . $suffix );
234 $html = ce_report_error( $error, 'Code Embed', false );
235 }
236 } else {
237
238 // No meta code was found so write out an error
239
240 $html = ce_report_error( sprintf( __( 'No embed code was found for %s', 'simple-embed-code' ), $meta_name ), 'Code Embed', false );
241
242 }
243 return $html;
244 }
245
246 /**
247 * Fetch a file (1.6)
248 *
249 * Use WordPress API to fetch a file and check results
250 * RC is 0 to indicate success, -1 a failure
251 *
252 * @since 2.0
253 *
254 * @param string $filein File name to fetch
255 * @param string $header Only get headers?
256 * @return string Array containing file contents and response
257 */
258
259 function ce_get_file( $filein, $header = false ) {
260
261 $rc = 0;
262 $error = '';
263 if ( $header ) {
264 $fileout = wp_remote_head( $filein );
265 if ( is_wp_error( $fileout ) ) {
266 $error = 'Header: ' . $fileout -> get_error_message();
267 $rc = -1;
268 }
269 } else {
270 $fileout = wp_remote_get( $filein );
271 if ( is_wp_error( $fileout ) ) {
272 $error = 'Body: ' . $fileout -> get_error_message();
273 $rc = -1;
274 } else {
275 if ( isset( $fileout[ 'body' ] ) ) {
276 $file_return[ 'file' ] = $fileout[ 'body' ];
277 }
278 }
279 }
280
281 $file_return[ 'error' ] = $error;
282 $file_return[ 'rc' ] = $rc;
283 if ( !is_wp_error( $fileout ) ) {
284 if ( isset( $fileout[ 'response' ][ 'code' ] ) ) {
285 $file_return[ 'response' ] = $fileout[ 'response' ][ 'code' ];
286 }
287 }
288
289 return $file_return;
290 }
291
292 /**
293 * Report an error (1.4)
294 *
295 * Function to report an error
296 *
297 * @since 1.6
298 *
299 * @param $error string Error message
300 * @param $plugin_name string The name of the plugin
301 * @param $echo string True or false, depending on whether you wish to return or echo the results
302 * @return string True or the output text
303 */
304
305 function ce_report_error( $error, $plugin_name, $echo = true ) {
306
307 $output = '<p style="color: #f00; font-weight: bold;">' . $plugin_name . ': ' . $error . "</p>\n";
308
309 if ( $echo ) {
310 echo $output;
311 return true;
312 } else {
313 return $output;
314 }
315
316 }
317 ?>