Advertisements

compression_test

Definition:
function compression_test() {}

Test support for compressing JavaScript from PHP
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.

Source code

function compression_test() {

?>

	<script type="text/javascript">

	/* <![CDATA[ */

	var testCompression = {

		get : function(test) {

			var x;

			if ( window.XMLHttpRequest ) {

				x = new XMLHttpRequest();

			} else {

				try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}

			}



			if (x) {

				x.onreadystatechange = function() {

					var r, h;

					if ( x.readyState == 4 ) {

						r = x.responseText.substr(0, 18);

						h = x.getResponseHeader('Content-Encoding');

						testCompression.check(r, h, test);

					}

				}



				x.open('GET', ajaxurl + '?action=wp-compression-test&test='+test+'&'+(new Date()).getTime(), true);

				x.send('');

			}

		},



		check : function(r, h, test) {

			if ( ! r && ! test )

				this.get(1);



			if ( 1 == test ) {

				if ( h && ( h.match(/deflate/i) || h.match(/gzip/i) ) )

					this.get('no');

				else

					this.get(2);



				return;

			}



			if ( 2 == test ) {

				if ( '"wpCompressionTest' == r )

					this.get('yes');

				else

					this.get('no');

			}

		}

	};

	testCompression.check();

	/* ]]> */

	</script>

<?php

}

709

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: