PluginProbe
Code Embed / 2.1.2
Code Embed v2.1.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 / ace-filter.php

ace-filter.php in Code Embed 2.1.2, at includes/ace-filter.php

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