PluginProbe
Code Embed / 2.4
Code Embed v2.4
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.4, at includes/add-embeds.php

300 lines 8.4 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 * @uses ce_get_embed_code Get embed code from other posts
16 *
17 * @param string $content Post content without embedded code.
18 * @return string Post content with embedded code.
19 */
20 function ce_filter( $content ) {
21
22 global $post;
23
24 // Set initial values.
25
26 $options = get_option( 'artiss_code_embed' );
27 $found_pos = strpos( $content, $options['opening_ident'] . $options['keyword_ident'], 0 );
28 $prefix_len = strlen( $options['opening_ident'] . $options['keyword_ident'] );
29
30 // Loop around the post content looking for all requests for code embeds.
31
32 while ( false !== $found_pos ) {
33
34 // Get the position of the closing identifier - ignore if one is not found!
35
36 $end_pos = strpos( $content, $options['closing_ident'], $found_pos + $prefix_len );
37 if ( false !== $end_pos ) {
38
39 // Extract the suffix.
40
41 $suffix_len = $end_pos - ( $found_pos + $prefix_len );
42 if ( 0 === $suffix_len ) {
43 $suffix = '';
44 } else {
45 $suffix = substr( $content, $found_pos + $prefix_len, $suffix_len );
46 }
47 $full_suffix = $suffix;
48
49 // Check for responsive part of suffix.
50
51 $responsive = false;
52 $max_width = false;
53 $res_pos = false;
54 if ( 1 < strlen( $suffix ) ) {
55 $res_pos = strpos( $suffix, '_RES', 1 );
56 }
57
58 if ( false !== $res_pos ) {
59
60 // If responsive section found, check it's at the end of has an underscore afterwards.
61 // Otherwise it may be part of something else.
62
63 if ( strlen( $suffix ) - 4 === $res_pos ) {
64 $responsive = true;
65 } elseif ( '_' === substr( $suffix, $res_pos + 4, 1 ) ) {
66 $responsive = true;
67 $max_width = substr( $suffix, $res_pos + 5 );
68 if ( ! is_numeric( $max_width ) ) {
69 $max_width = '';
70 }
71 }
72
73 if ( $responsive ) {
74 $suffix = substr( $suffix, 0, $res_pos );
75 }
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 = ce_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 = ce_generate_code( $html, $responsive, $max_width );
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 = ce_quick_replace( $content, $options, 'http://' );
111
112 // Loop around the post content looking for HTTPS addresses.
113
114 $content = ce_quick_replace( $content, $options, 'https://' );
115
116 return $content;
117 }
118
119 add_filter( 'the_content', 'ce_filter' );
120 add_filter( 'widget_content', 'ce_filter' );
121
122 /**
123 * Quick Replace
124 *
125 * Function to do a quick search/replace of the content
126 *
127 * @param string $content The content.
128 * @param string $options The options array.
129 * @param string $search The string to search for.
130 * @return string The updated content
131 */
132 function ce_quick_replace( $content = '', $options = '', $search = '' ) {
133
134 $start_pos = strpos( $content, $options['opening_ident'] . $search, 0 );
135
136 $open_len = strlen( $options['opening_ident'] );
137 $close_len = strlen( $options['closing_ident'] );
138
139 while ( false !== $start_pos ) {
140
141 $end_pos = strpos( $content, $options['closing_ident'], $start_pos + 1 );
142
143 if ( false !== $end_pos ) {
144 $url = substr( $content, $start_pos + $open_len, $end_pos - $start_pos - $close_len );
145 $file = ce_get_file( $url );
146 if ( false !== $file ) {
147 $content = str_replace( $options['opening_ident'] . $url . $options['closing_ident'], $file, $content );
148 } else {
149 ce_report_error( __( 'File could not be fetched', 'simple-embed-code' ), 'Code Embed', false );
150 }
151 }
152
153 $start_pos = strpos( $content, $options['opening_ident'] . $search, 0 );
154
155 }
156
157 return $content;
158 }
159
160 /**
161 * Generate Embed Code
162 *
163 * Function to generate the embed code that will be output
164 *
165 * @param string $html The embed code (required).
166 * @param string $responsive Responsive output required? (optional).
167 * @param string $max_width Maximum width of responsive output (optional).
168 * @return string The embed code
169 */
170 function ce_generate_code( $html, $responsive = '', $max_width = '' ) {
171
172 $code = '';
173
174 if ( false !== $max_width ) {
175 $code .= '<div style="width: ' . $max_width . 'px; max-width: 100%">';
176 }
177
178 if ( $responsive ) {
179 $code .= '<div class="ce-video-container">';
180 }
181
182 $code .= $html;
183
184 if ( $responsive ) {
185 $code .= '</div>';
186 }
187
188 if ( false !== $max_width ) {
189 $code .= '</div>';
190 }
191
192 return $code;
193 }
194
195 /**
196 * Get the Global Embed Code
197 *
198 * Function to look for and, if available, return the global embed code
199 *
200 * @uses ce_report_error Generate an error message
201 *
202 * @param string $ident The embed code opening identifier.
203 * @param string $suffix The embed code suffix.
204 * @return string The embed code (or error)
205 */
206 function ce_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( $wpdb->prepare( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_name ) ); // @codingStandardsIgnoreLine -- requires the latest data when called, so caching is inappropriate
213 $records = $wpdb->num_rows;
214
215 if ( 0 < $records ) {
216
217 // Results were found.
218
219 $meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_value, post_title, ID FROM $wpdb->postmeta, $wpdb->posts WHERE meta_key = %s AND post_id = ID", $meta_name ) ); // @codingStandardsIgnoreLine -- requires the latest data when called, so caching is inappropriate
220 $total_records = $wpdb->num_rows;
221
222 if ( 1 === $records ) {
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 } else {
230
231 // More than one unique code result returned, so output the list of posts.
232
233 /* translators: %1$s: embed name, %2$d: number of pieces of code being stored with that name, %3$d: number of posts using that embed name */
234 $error = sprintf( __( 'Cannot use %1$s as a global code as it is being used to store %2$d unique pieces of code in %3$d posts', 'simple-embed-code' ), $meta_name, $records, $total_records );
235 $html = ce_report_error( $error, 'Code Embed', false );
236 }
237 } else {
238
239 // No meta code was found so write out an error.
240
241 /* translators: %s: the embed name */
242 $html = ce_report_error( sprintf( __( 'No embed code was found for %s', 'simple-embed-code' ), $meta_name ), 'Code Embed', false );
243
244 }
245 return $html;
246 }
247
248 /**
249 * Fetch a file
250 *
251 * Use WordPress API to fetch a file and check results
252 *
253 * @param string $filein File name to fetch.
254 * @return string Array containing file contents or false to indicate a failure
255 */
256 function ce_get_file( $filein ) {
257
258 if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
259
260 $response = vip_safe_wp_remote_get( $filein, '', 3, 3 );
261
262 } else {
263
264 $response = wp_remote_get( $filein, array( 'timeout' => 3 ) ); // @codingStandardsIgnoreLine -- for non-VIP environments
265
266 }
267
268 if ( is_array( $response ) ) {
269
270 return $response['body'];
271
272 } else {
273
274 return false;
275
276 }
277 }
278
279 /**
280 * Report an error
281 *
282 * Function to report an error
283 *
284 * @param string $error Error message.
285 * @param string $plugin_name The name of the plugin.
286 * @param string $echo_out True or false, depending on whether you wish to return or echo the results.
287 * @return string True or the output text
288 */
289 function ce_report_error( $error, $plugin_name, $echo_out = true ) {
290
291 $output = '<p style="color: #f00; font-weight: bold;">' . $plugin_name . ': ' . $error . "</p>\n";
292
293 if ( $echo_out ) {
294 echo esc_html( $output );
295 return true;
296 } else {
297 return $output;
298 }
299 }
300