ID ) ) { return $content; } // Set initial values. static $options = null; if ( null === $options ) { $options = get_option( 'artiss_code_embed' ); } if ( ! is_array( $options ) ) { return $content; } $found_pos = strpos( $content, $options['opening_ident'] . $options['keyword_ident'], 0 ); $prefix_len = strlen( $options['opening_ident'] . $options['keyword_ident'] ); // Loop around the post content looking for all requests for code embeds. while ( false !== $found_pos ) { // Get the position of the closing identifier - ignore if one is not found! $end_pos = strpos( $content, $options['closing_ident'], $found_pos + $prefix_len ); if ( false !== $end_pos ) { // Extract the suffix. $suffix_len = $end_pos - ( $found_pos + $prefix_len ); if ( 0 === $suffix_len ) { $suffix = ''; } else { $suffix = substr( $content, $found_pos + $prefix_len, $suffix_len ); } $full_suffix = $suffix; // Check for responsive part of suffix. $responsive = false; $max_width = false; $res_pos = false; if ( 1 < strlen( $suffix ) ) { $res_pos = strpos( $suffix, '_RES', 1 ); } if ( false !== $res_pos ) { // If responsive section found, check it's at the end or has an underscore afterwards. // Otherwise it may be part of something else. if ( strlen( $suffix ) - 4 === $res_pos ) { $responsive = true; } elseif ( '_' === substr( $suffix, $res_pos + 4, 1 ) ) { $responsive = true; $max_width = substr( $suffix, $res_pos + 5 ); if ( ! is_numeric( $max_width ) ) { $max_width = ''; } } if ( $responsive ) { $suffix = substr( $suffix, 0, $res_pos ); } } // Get the meta for the current post. $post_meta = get_post_meta( $post->ID, $options['keyword_ident'] . $suffix, false ); if ( isset( $post_meta[0] ) ) { $html = $post_meta[0]; } else { // No meta found, so look for it elsewhere. $html = ce_get_embed_code( $options['keyword_ident'], $suffix ); } // Build the string to search for. $search = $options['opening_ident'] . $options['keyword_ident'] . $full_suffix . $options['closing_ident']; // Build the string of code to replace with. $replace = ce_generate_code( $html, $responsive, $max_width ); // Now modify all references. $content = str_replace( $search, $replace, $content ); } $found_pos = strpos( $content, $options['opening_ident'] . $options['keyword_ident'], $found_pos + 1 ); } // Loop around the post content looking for HTTP addresses. if ( '1' === get_post_meta( $post->ID, '_ce_allow_url_embeds', true ) ) { $content = ce_quick_replace( $content, $options, 'http://' ); // Loop around the post content looking for HTTPS addresses. $content = ce_quick_replace( $content, $options, 'https://' ); } return $content; } add_filter( 'the_content', 'ce_filter' ); add_filter( 'widget_text_content', 'ce_filter' ); add_action( 'save_post', 'ce_save_url_embed_permission' ); /** * Save URL Embed Permission * * On post save, records whether the post author has the unfiltered_html * capability, so that URL embed tokens are only expanded at render time * for content authored by trusted users. * * @param string $post_id The post ID. */ function ce_save_url_embed_permission( $post_id ) { if ( wp_is_post_revision( $post_id ) ) { return; } $post = get_post( $post_id ); $allowed = user_can( $post->post_author, 'unfiltered_html' ) ? '1' : '0'; update_post_meta( $post_id, '_ce_allow_url_embeds', $allowed ); } /** * Quick Replace * * Function to do a quick search/replace of the content. * * @param string $content The content. * @param array $options The options array. * @param string $search The string to search for. * @return string The updated content. */ function ce_quick_replace( $content = '', $options = '', $search = '' ) { $start_pos = strpos( $content, $options['opening_ident'] . $search, 0 ); while ( false !== $start_pos ) { $end_pos = strpos( $content, $options['closing_ident'], $start_pos + 1 ); if ( false !== $end_pos ) { $url = substr( $content, $start_pos + strlen( $options['opening_ident'] ), $end_pos - ( $start_pos + strlen( $options['opening_ident'] ) ) ); $file = ce_get_file( $url ); if ( false === $file ) { $file = ce_report_error( __( 'File could not be fetched', 'simple-embed-code' ), 'Code Embed', false ); } $content = str_replace( $options['opening_ident'] . $url . $options['closing_ident'], wp_kses_post( $file ), $content ); $start_pos = strpos( $content, $options['opening_ident'] . $search, 0 ); } else { $start_pos = strpos( $content, $options['opening_ident'] . $search, $start_pos + 1 ); } } return $content; } /** * Generate Embed Code * * Function to generate the embed code that will be output. * * @param string $html The embed code (required). * @param bool $responsive Responsive output required? (optional). * @param string|bool $max_width Maximum width of responsive output (optional). * @return string The embed code. */ function ce_generate_code( $html, $responsive = false, $max_width = false ) { $code = ''; if ( false !== $max_width && '' !== $max_width ) { $code .= '
' . esc_html( $plugin_name ) . ': ' . esc_html( $error ) . "
\n"; if ( $echo_out || ! current_user_can( 'edit_posts' ) ) { if ( current_user_can( 'edit_posts' ) ) { echo $output; // @codingStandardsIgnoreLine -- being escaped above so this is a false positive } return true; } else { return $output; } }