W3TCExtension_Swarmify_Plugin

The W3 Total Cache W3TC Extension Swarmify Plugin class.

Defined (1)

The class is defined in the following location(s).

/Extension_Swarmify_Plugin.php  
  1. class Extension_Swarmify_Plugin { 
  2. private $reject_reason = ''; 
  3. private $_config = null; 
  4.  
  5. function __construct() { 
  6. $this->_config = Dispatcher::config(); 
  7.  
  8. /** 
  9. * Runs plugin 
  10. */ 
  11. function run() { 
  12. add_filter( 'w3tc_config_default_values', array( 
  13. $this, 'w3tc_config_default_values' ) ); 
  14.  
  15. $config = Dispatcher::config(); 
  16. // remainder only when extension is frontend-active 
  17. if ( !$config->is_extension_active_frontend( 'swarmify' ) ) 
  18. return; 
  19.  
  20. if ($this->_active()) { 
  21. Util_Bus::add_ob_callback( 'swarmify', array( 
  22. $this,  
  23. 'ob_callback' 
  24. ) ); 
  25.  
  26. add_filter( 'w3tc_footer_comment', array( 
  27. $this,  
  28. 'w3tc_footer_comment' 
  29. ) ); 
  30.  
  31. public function w3tc_config_default_values( $default_values ) { 
  32. $default_values['swarmify'] = array( 
  33. 'reject.logged' => false,  
  34. 'api_key' => '',  
  35. 'handle.htmlvideo' => true,  
  36. 'handle.jwplayer' => true 
  37. ); 
  38.  
  39. return $default_values; 
  40.  
  41. function ob_callback( $buffer ) { 
  42. $c = $this->_config; 
  43. $api_key = $c->get_string( array( 'swarmify', 'api_key' ) ); 
  44. $api_key = preg_replace( '~[^0-9a-zA-Z-]~', '', $api_key ); // make safe 
  45.  
  46. $bootstrap_required = false; 
  47.  
  48. if ( $c->get_boolean( array( 'swarmify', 'handle.htmlvideo' ) ) ) { 
  49. $count = 0; 
  50. $buffer = preg_replace( '~<video([^<>]+)>~i', '<swarmvideo\\1>',  
  51. $buffer, -1, $count ); 
  52.  
  53. if ( $count ) { 
  54. $buffer = preg_replace( '~<\\/video>~', '</swarmvideo>', $buffer ); 
  55. $bootstrap_required = true; 
  56.  
  57. if ( $c->get_boolean( array( 'swarmify', 'handle.jwplayer' ) ) ) { 
  58. $count = 0; 
  59. $buffer = preg_replace( '~jwplayer\s*\\(([^)]+)\\)\s*\\.setup\\(~', 'swarmify.jwPlayerEmbed(\\1, ',  
  60. $buffer, -1, $count ); 
  61.  
  62. if ( $count ) 
  63. $bootstrap_required = true; 
  64.  
  65. // add bootstrap swarmify script if there are really any videos on page 
  66. if ( $bootstrap_required ) { 
  67. $loader_script = '<script type="text/javascript">' . 
  68. 'var swarmoptions = {swarmcdnkey: "' . $api_key . '"};</script>' . 
  69. '<script type="text/javascript" src="//assets.swarmcdn.com/cross/swarmcdn.js"></script>'; 
  70.  
  71. $buffer = preg_replace( '~<head(\s+[^>]*)*>~Ui',  
  72. '\\0' . $loader_script, $buffer, 1 ); 
  73.  
  74. return $buffer; 
  75.  
  76.  
  77. function _active() { 
  78. $reject_reason = apply_filters( 'w3tc_swarmify_active', null ); 
  79. if ( !empty( $reject_reason ) ) { 
  80. $this->reject_reason = 
  81. __( 'rejected by filter: ', 'w3-total-cache' ) . $reject_reason; 
  82. return false; 
  83.  
  84.  
  85. /** 
  86. * Disable for AJAX so its not messed up 
  87. */ 
  88. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
  89. $this->reject_reason = __( 'DOING_AJAX constant is defined', 'w3-total-cache' ); 
  90. return false; 
  91.  
  92. if ( defined( 'WP_ADMIN' ) ) { 
  93. $this->reject_reason = __( 'WP_ADMIN page', 'w3-total-cache' ); 
  94. return false; 
  95.  
  96. /** 
  97. * Check logged users 
  98. */ 
  99. if ( $this->_config->get_boolean( array( 'swarmify', 'reject.logged' ) ) && 
  100. is_user_logged_in() ) { 
  101. $this->reject_reason = __( 'logged in user rejected',  
  102. 'w3-total-cache' ); 
  103.  
  104. return false; 
  105.  
  106. return true; 
  107.  
  108. public function w3tc_footer_comment( $strings ) { 
  109. $append = ( $this->reject_reason != '' ) ? 
  110. sprintf( ' (%s)', $this->reject_reason ) : ' active'; 
  111. $strings[] = sprintf( 
  112. __( "Swarmify%s", 'w3-total-cache' ),  
  113. $append ); 
  114.  
  115. return $strings;