compression_test

Test support for compressing JavaScript from PHP.

Description

compression_test(); 

Outputs JavaScript that tests if compression from PHP works as expected and sets an option with the result. Has no effect when the current user is not an administrator. To run the test again the option can_compress_scripts has to be deleted.


Usage

  1. if ( !function_exists( 'compression_test' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/template.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = compression_test(); 
  7.  

Defined (1)

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

/wp-admin/includes/template.php  
  1. function compression_test() { 
  2. ?> 
  3. <script type="text/javascript"> 
  4. var compressionNonce = <?php echo wp_json_encode( wp_create_nonce( 'update_can_compress_scripts' ) ); ?>; 
  5. var testCompression = { 
  6. get : function(test) { 
  7. var x; 
  8. if ( window.XMLHttpRequest ) { 
  9. x = new XMLHttpRequest(); 
  10. } else { 
  11. try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e) {try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e) {};} 
  12.  
  13. if (x) { 
  14. x.onreadystatechange = function() { 
  15. var r, h; 
  16. if ( x.readyState == 4 ) { 
  17. r = x.responseText.substr(0, 18); 
  18. h = x.getResponseHeader('Content-Encoding'); 
  19. testCompression.check(r, h, test); 
  20. }; 
  21.  
  22. x.open('GET', ajaxurl + '?action=wp-compression-test&test='+test+'&_ajax_nonce='+compressionNonce+'&'+(new Date()).getTime(), true); 
  23. x.send(''); 
  24. },  
  25.  
  26. check : function(r, h, test) { 
  27. if ( ! r && ! test ) 
  28. this.get(1); 
  29.  
  30. if ( 1 == test ) { 
  31. if ( h && ( h.match(/deflate/i) || h.match(/gzip/i) ) ) 
  32. this.get('no'); 
  33. else 
  34. this.get(2); 
  35.  
  36. return; 
  37.  
  38. if ( 2 == test ) { 
  39. if ( '"wpCompressionTest' == r ) 
  40. this.get('yes'); 
  41. else 
  42. this.get('no'); 
  43. }; 
  44. testCompression.check(); 
  45. </script> 
  46. <?php