wordpress-popup
Last commit date
popoverincludes
15 years ago
license.txt
15 years ago
popover.php
15 years ago
readme.txt
15 years ago
testheadfooter.php
15 years ago
testheadfooter.php
87 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Test Head Footer |
| 4 | Plugin URI: http://gist.github.com/378450 |
| 5 | Description: Tests for the existence and functionality of wp_head and wp_footer in the active theme |
| 6 | Author: Matt Martz |
| 7 | Author URI: http://sivel.net/ |
| 8 | Version: 3.1 |
| 9 | |
| 10 | Copyright (c) 2010 Matt Martz (http://sivel.net/) |
| 11 | Test Head Footer is released under the GNU General Public License (GPL) |
| 12 | http://www.gnu.org/licenses/gpl-2.0.txt |
| 13 | */ |
| 14 | |
| 15 | // Lets not do anything until init |
| 16 | add_action( 'init', 'test_head_footer_init' ); |
| 17 | function test_head_footer_init() { |
| 18 | // Hook in at admin_init to perform the check for wp_head and wp_footer |
| 19 | add_action( 'admin_init', 'check_head_footer' ); |
| 20 | |
| 21 | // If test-head query var exists hook into wp_head |
| 22 | if ( isset( $_GET['test-head'] ) ) |
| 23 | add_action( 'wp_head', 'test_head', 99999 ); // Some obscene priority, make sure we run last |
| 24 | |
| 25 | // If test-footer query var exists hook into wp_footer |
| 26 | if ( isset( $_GET['test-footer'] ) ) |
| 27 | add_action( 'wp_footer', 'test_footer', 99999 ); // Some obscene priority, make sure we run last |
| 28 | } |
| 29 | |
| 30 | // Echo a string that we can search for later into the head of the document |
| 31 | // This should end up appearing directly before </head> |
| 32 | function test_head() { |
| 33 | echo '<!--wp_head-->'; |
| 34 | } |
| 35 | |
| 36 | // Echo a string that we can search for later into the footer of the document |
| 37 | // This should end up appearing directly before </body> |
| 38 | function test_footer() { |
| 39 | echo '<!--wp_footer-->'; |
| 40 | } |
| 41 | |
| 42 | // Check for the existence of the strings where wp_head and wp_footer should have been called from |
| 43 | function check_head_footer() { |
| 44 | // Build the url to call, NOTE: uses home_url and thus requires WordPress 3.0 |
| 45 | $url = add_query_arg( array( 'test-head' => '', 'test-footer' => '' ), home_url() ); |
| 46 | // Perform the HTTP GET ignoring SSL errors |
| 47 | $response = wp_remote_get( $url, array( 'sslverify' => false ) ); |
| 48 | // Grab the response code and make sure the request was sucessful |
| 49 | $code = (int) wp_remote_retrieve_response_code( $response ); |
| 50 | if ( $code == 200 ) { |
| 51 | global $head_footer_errors; |
| 52 | $head_footer_errors = array(); |
| 53 | |
| 54 | // Strip all tabs, line feeds, carriage returns and spaces |
| 55 | $html = preg_replace( '/[\t\r\n\s]/', '', wp_remote_retrieve_body( $response ) ); |
| 56 | |
| 57 | // Check to see if we found the existence of wp_head |
| 58 | if ( ! strstr( $html, '<!--wp_head-->' ) ) |
| 59 | $head_footer_errors['nohead'] = 'Is missing the call to <?php wp_head(); ?> which should appear directly before </head>'; |
| 60 | // Check to see if we found the existence of wp_footer |
| 61 | if ( ! strstr( $html, '<!--wp_footer-->' ) ) |
| 62 | $head_footer_errors['nofooter'] = 'Is missing the call to <?php wp_footer(); ?> which should appear directly before </body>'; |
| 63 | |
| 64 | // Check to see if we found wp_head and if was located in the proper spot |
| 65 | if ( ! strstr( $html, '<!--wp_head--></head>' ) && ! isset( $head_footer_errors['nohead'] ) ) |
| 66 | $head_footer_errors[] = 'Has the call to <?php wp_head(); ?> but it is not called directly before </head>'; |
| 67 | // Check to see if we found wp_footer and if was located in the proper spot |
| 68 | if ( ! strstr( $html, '<!--wp_footer--></body>' ) && ! isset( $head_footer_errors['nofooter'] ) ) |
| 69 | $head_footer_errors[] = 'Has the call to <?php wp_footer(); ?> but it is not called directly before </body>'; |
| 70 | |
| 71 | // If we found errors with the existence of wp_head or wp_footer hook into admin_notices to complain about it |
| 72 | if ( ! empty( $head_footer_errors ) ) |
| 73 | add_action ( 'admin_notices', 'test_head_footer_notices' ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Output the notices |
| 78 | function test_head_footer_notices() { |
| 79 | global $head_footer_errors; |
| 80 | |
| 81 | // If we made it here it is because there were errors, lets loop through and state them all |
| 82 | echo '<div class="error"><p><strong>Your active theme:</strong></p><ul>'; |
| 83 | foreach ( $head_footer_errors as $error ) |
| 84 | echo '<li>' . esc_html( $error ) . '</li>'; |
| 85 | echo '</ul></div>'; |
| 86 | } |
| 87 | ?> |