Guest User

Untitled

a guest
Jul 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Edge Side Includes (ESI) Shortcode
  5. */
  6. function esi_shortcode($attrs){
  7. $attrs = shortcode_atts(array(
  8. 'src' => '',
  9. 'ttl' => '1h',
  10. 'dca' => 'none',
  11. 'onerror' => 'continue'
  12. ), $attrs);
  13.  
  14. // Serialize $attrs into HTML attributes
  15. $html_attrs = array();
  16. foreach($attrs as $name => $value){
  17. $html_attrs[] = $name . '="' . esc_attr($value) . '"';
  18. }
  19.  
  20. // Yes, we return the esi shortcode back with the updated attributes because
  21. // WordPress will strip out <esi:include> at this point. The content filter
  22. // below will do the final replacement just in time.
  23. return sprintf('[esi %s ]', join(' ', $html_attrs));
  24. }
  25. add_shortcode( 'esi', 'esi_shortcode' );
  26.  
  27.  
  28. /**
  29. * Since <esi:include> tags don't make it through the WordPress content filter
  30. * when output by shortcode, do the replacement at the end.
  31. */
  32. function esi_shortcode_content_filter( $content ){
  33. return preg_replace_callback( '/\[esi(.+?)\]/', 'esi_shortcode_content_filter_callback', $content );
  34. }
  35. add_filter( 'the_content', 'esi_shortcode_content_filter', 100 );
  36.  
  37.  
  38. /**
  39. * This is because Akamai b0rks on & appearing in esi:include attributes
  40. */
  41. function esi_shortcode_content_filter_callback( $matches ){
  42. $attrs = str_replace( '&', '&', $matches[1] );
  43. return "<esi:include $attrs />";
  44. }
Add Comment
Please, Sign In to add comment