PageRenderTime 38ms CodeModel.GetById 27ms RepoModel.GetById 7ms app.codeStats 0ms

/theme-check/tags/20110219.1/checkbase.php

https://github.com/brandonburke/WordPress-Plugin-Baseline
PHP | 231 lines | 12 code | 4 blank | 215 comment | 0 complexity | 5bba844a5c887bd352f8bfc83f81162a MD5 | raw file
  1. <?php
  2. // main global to hold our checks
  3. global $themechecks;
  4. $themechecks = array();
  5. // counter for the checks
  6. global $checkcount;
  7. $checkcount = 0;
  8. // interface that all checks should implement
  9. interface themecheck
  10. {
  11. // should return true for good/okay/acceptable, false for bad/not-okay/unacceptable
  12. public function check( $php_files, $css_files, $other_files );
  13. // should return an array of strings explaining any problems found
  14. public function getError();
  15. }
  16. // load all the checks in the checks directory
  17. $dir = 'checks';
  18. foreach (glob(dirname(__FILE__). "/{$dir}/*.php") as $file) {
  19. include $file;
  20. }
  21. function run_themechecks($php, $css, $other) {
  22. global $themechecks;
  23. $pass = true;
  24. foreach($themechecks as $check) {
  25. if ($check instanceof themecheck) {
  26. $pass = $pass & $check->check($php, $css, $other);
  27. }
  28. }
  29. return $pass;
  30. }
  31. function display_themechecks() {
  32. $results = '';
  33. global $themechecks;
  34. $errors = array();
  35. foreach ($themechecks as $check) {
  36. if ($check instanceof themecheck) {
  37. $error = $check->getError();
  38. $error = (array) $error;
  39. if (!empty($error)) {
  40. $errors = array_unique( array_merge( $error, $errors ) );
  41. }
  42. }
  43. }
  44. if (!empty($errors)) {
  45. rsort($errors);
  46. foreach ($errors as $e) {
  47. if ( defined( 'TC_TRAC' ) ) {
  48. $results .= ( isset( $_POST['s_info'] ) && preg_match( '/INFO/', $e ) ) ? '' : '* ' . tc_trac( $e ) . "\r\n";
  49. } else {
  50. $results .= ( isset( $_POST['s_info'] ) && preg_match( '/INFO/', $e ) ) ? '' : '<li>' . tc_trac( $e ) . '</li>';
  51. }
  52. }
  53. }
  54. if ( defined( 'TC_TRAC' ) ) {
  55. if ( defined( 'TC_PRE' ) ) $results = TC_PRE . $results;
  56. $results = '<textarea cols=140 rows=20>' . strip_tags( $results );
  57. if ( defined( 'TC_POST' ) ) $results = $results . TC_POST;
  58. $results .= '</textarea>';
  59. }
  60. return $results;
  61. }
  62. function checkcount() {
  63. global $checkcount;
  64. $checkcount++;
  65. }
  66. // some functions theme checks use
  67. function tc_grep( $error, $file ) {
  68. $lines = file( $file, FILE_IGNORE_NEW_LINES ); // Read the theme file into an array
  69. $line_index = 0;
  70. $bad_lines = '';
  71. foreach( $lines as $this_line ) {
  72. if ( stristr ( $this_line, $error ) ) {
  73. $error = str_replace( '"', "'", $error );
  74. $this_line = str_replace( '"', "'", $this_line );
  75. $error = ltrim( $error );
  76. $pre = ( FALSE !== ( $pos = strpos( $this_line, $error ) ) ? substr( $this_line, 0, $pos ) : FALSE );
  77. $pre = ltrim( htmlspecialchars( $pre ) );
  78. $bad_lines .= __("<pre class='tc-grep'>Line ", "themecheck") . ( $line_index+1 ) . ": " . $pre . htmlspecialchars( substr( stristr( $this_line, $error ), 0, 75 ) ) . "</pre>";
  79. }
  80. $line_index++;
  81. }
  82. return str_replace( $error, '<span class="tc-grep">' . $error . '</span>', $bad_lines );
  83. }
  84. function tc_preg( $preg, $file ) {
  85. $lines = file( $file, FILE_IGNORE_NEW_LINES ); // Read the theme file into an array
  86. $line_index = 0;
  87. $bad_lines = '';
  88. foreach( $lines as $this_line ) {
  89. if ( preg_match( $preg, $this_line, $matches ) ) {
  90. $error = $matches[0];
  91. $this_line = str_replace( '"', "'", $this_line );
  92. $error = ltrim( $error );
  93. $pre = ( FALSE !== ( $pos = strpos( $this_line, $error ) ) ? substr( $this_line, 0, $pos ) : FALSE );
  94. $pre = ltrim( htmlspecialchars( $pre ) );
  95. $bad_lines .= __("<pre class='tc-grep'>Line ", "themecheck") . ( $line_index+1 ) . ": " . $pre . htmlspecialchars( substr( stristr( $this_line, $error ), 0, 75 ) ) . "</pre>";
  96. }
  97. $line_index++;
  98. }
  99. return str_replace( $error, '<span class="tc-grep">' . $error . '</span>', $bad_lines );
  100. }
  101. function tc_strxchr($haystack, $needle, $l_inclusive = 0, $r_inclusive = 0){
  102. if(strrpos($haystack, $needle)){
  103. //Everything before last $needle in $haystack.
  104. $left = substr($haystack, 0, strrpos($haystack, $needle) + $l_inclusive);
  105. //Switch value of $r_inclusive from 0 to 1 and viceversa.
  106. $r_inclusive = ($r_inclusive == 0) ? 1 : 0;
  107. //Everything after last $needle in $haystack.
  108. $right = substr(strrchr($haystack, $needle), $r_inclusive);
  109. //Return $left and $right into an array.
  110. return array($left, $right);
  111. } else {
  112. if(strrchr($haystack, $needle)) return array('', substr(strrchr($haystack, $needle), $r_inclusive));
  113. else return false;
  114. }
  115. }
  116. function tc_filename( $file ) {
  117. $filename = ( preg_match( '/themes\/[a-z0-9]*\/(.*)/', $file, $out ) ) ? $out[1] : basename( $file );
  118. return $filename;
  119. }
  120. function tc_trac( $e ) {
  121. $trac_left = array( '<strong>', '</strong>' );
  122. $trac_right= array( "'''", "'''" );
  123. $html_link = '/<a\s?href\s?=\s?[\'|"]([^"|\']*)[\'|"]>([^<]*)<\/a>/i';
  124. $html_new = '[$1 $2]';
  125. if ( defined( 'TC_TRAC' ) ) {
  126. $e = preg_replace( $html_link, $html_new, $e );
  127. $e = str_replace( $trac_left, $trac_right, $e );
  128. $e = preg_replace( '/<pre.*?>/', "\r\n{{{\r\n", $e );
  129. $e = str_replace( '</pre>', "\r\n}}}\r\n", $e );
  130. }
  131. return $e;
  132. }
  133. function listdir( $start_dir='.' ) {
  134. $files = array();
  135. if ( is_dir( $start_dir ) ) {
  136. $fh = opendir( $start_dir );
  137. while ( ( $file = readdir( $fh ) ) !== false ) {
  138. # loop through the files, skipping . and .., and recursing if necessary
  139. if ( strcmp( $file, '.' )==0 || strcmp( $file, '..' )==0 ) continue;
  140. $filepath = $start_dir . '/' . $file;
  141. if ( is_dir( $filepath ) )
  142. $files = array_merge( $files, listdir( $filepath ) );
  143. else
  144. array_push( $files, $filepath );
  145. }
  146. closedir( $fh );
  147. } else {
  148. # false if the function was called with an invalid non-directory argument
  149. $files = false;
  150. }
  151. return $files;
  152. }
  153. function tc_print_r( $data ) {
  154. $out = "\n<pre class='html-print-r'";
  155. $out .= " style='border: 1px solid #ccc; padding: 7px;'>\n";
  156. $out .= esc_html( print_r( $data, TRUE ) );
  157. $out .= "\n</pre>\n";
  158. echo $out;
  159. }
  160. function tc_add_headers( $extra_headers ) {
  161. $extra_headers = array( 'License', 'License URI', 'Template Version' );
  162. return $extra_headers;
  163. }
  164. function tc_intro() {
  165. _e( '<h2>About</h2>', 'themecheck' );
  166. _e( '<p>The theme check plugin is an easy way to test your theme and make sure it\'s up to spec with the latest theme review standards.<br />', 'themecheck' );
  167. _e( 'With it, you can run all the same automated testing tools on your theme that WordPress.org uses for theme submissions.</p>', 'themecheck' );
  168. _e( '<h2>Contact</h2>', 'themecheck' );
  169. _e( '<p>Theme-Check is maintained by <a href="http://profiles.wordpress.org/users/pross/">Pross</a> and <a href="http://profiles.wordpress.org/users/otto42/">Otto42</a><br />', 'themecheck' );
  170. _e( 'If you have found a bug or would like to make a suggestion or contribution why not join the <a href="http://wordpress.org/extend/themes/contact/">theme-reviewers mailing list</a><br />', 'themecheck' );
  171. _e( 'or leave a post on the <a href="http://wordpress.org/tags/theme-check?forum_id=10">WordPress forums</a>.<br />', 'themecheck' );
  172. echo '<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="2V7F4QYMWMBL6"><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"></form>';
  173. _e( '<h2>Contributors</h2>', 'themecheck' );
  174. _e( '<h3>localization</h3>', 'themecheck' );
  175. echo '<p><ul>';
  176. echo '<li><a href="http://www.onedesigns.com/">Daniel Tara</a></li>';
  177. echo '<li><a href="http://themeid.com/">Emil Uzelac</a></li>';
  178. echo '</ul></p>';
  179. _e( '<h3>Testers</h3>', 'themecheck' );
  180. _e( '<p><a href="http://make.wordpress.org/themes/">The WordPress Theme Review Team</a></p>', 'themecheck' );
  181. }
  182. function tc_success() {
  183. _e( '<div class="tc-success">Now your theme has passed the basic tests you need to check it properly using the test data before you upload to the WordPress Themes Directory.<br />', 'themecheck' );
  184. _e( '<br />Make sure to review the guidelines at <a href="http://codex.wordpress.org/Theme_Review">Theme Review</a> before uploading a Theme.', 'themecheck' );
  185. _e( '<h3>Codex Links</h3>', 'themecheck' );
  186. echo '<p><ul>';
  187. _e( '<li><a href="http://codex.wordpress.org/Theme_Development">Theme Development</a></li>', 'themecheck' );
  188. _e( '<li><a href="http://wordpress.org/support/forum/5">Themes and Templates forum</a></li>', 'themecheck' );
  189. _e( '<li><a href="http://codex.wordpress.org/Theme_Unit_Test">Theme Unit Tests</a></li>', 'themecheck' );
  190. echo '</ul></p></div>';
  191. }
  192. function tc_form() {
  193. $themes = get_themes();
  194. echo '<form action="themes.php?page=themecheck" method="POST">';
  195. echo '<select name="themename">';
  196. foreach( $themes as $name => $location ) {
  197. echo '<option ';
  198. if ( basename( STYLESHEETPATH ) === $location['Stylesheet'] ) echo 'selected ';
  199. echo 'value="' . $location['Stylesheet'] . '">' . $name . '</option>';
  200. }
  201. echo '</select>';
  202. echo '<input class="button" type="submit" value="' . __( 'Check it!', 'themecheck' ) . '" />';
  203. if ( defined( 'TC_PRE' ) || defined( 'TC_POST' ) ) echo ' <input name="trac" type="checkbox" /> ' . __( 'Output in Trac format.', 'themecheck' );
  204. echo ' <input name="s_info" type="checkbox" /> ' . __( 'Suppress INFO.', 'themecheck' );
  205. echo '</form>';
  206. }