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

https://github.com/brandonburke/WordPress-Plugin-Baseline · PHP · 144 lines · 12 code · 4 blank · 128 comment · 0 complexity · af270ebeabe1229e17bf168951584c57 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. $results .= '<li>' . tc_trac( $e ) . '</li>';
  48. }
  49. }
  50. if ( defined( 'REVIEWER' ) ) {
  51. if ( defined( 'TC_PRE' ) ) $results = TC_PRE . $results;
  52. if ( defined( 'TC_POST' ) ) $results = $results . TC_POST;
  53. }
  54. return $results;
  55. }
  56. function checkcount() {
  57. global $checkcount;
  58. $checkcount++;
  59. }
  60. // some functions theme checks use
  61. function tc_grep( $error, $file ) {
  62. $lines = file( $file, FILE_IGNORE_NEW_LINES ); // Read the theme file into an array
  63. $line_index = 0;
  64. $bad_lines = '';
  65. foreach( $lines as $this_line )
  66. {
  67. if ( stristr ( $this_line, $error ) ) {
  68. $error = str_replace( '"', "'", $error );
  69. $this_line = str_replace( '"', "'", $this_line );
  70. $error = ltrim( $error );
  71. $pre = ( FALSE !== ( $pos = strpos( $this_line, $error ) ) ? substr( $this_line, 0, $pos ) : FALSE );
  72. $pre = ltrim( htmlspecialchars( $pre ) );
  73. $bad_lines .= __("<pre class='tc-grep'>Line ", "theme-check") . ( $line_index+1 ) . ": " . $pre . htmlspecialchars( substr( stristr( $this_line, $error ), 0, 75 ) ) . "</pre>";
  74. }
  75. $line_index++;
  76. }
  77. return str_replace( $error, '<span class="tc-grep">' . $error . '</span>', $bad_lines );
  78. }
  79. function tc_preg( $preg, $file ) {
  80. $lines = file( $file, FILE_IGNORE_NEW_LINES ); // Read the theme file into an array
  81. $line_index = 0;
  82. $bad_lines = '';
  83. foreach( $lines as $this_line )
  84. {
  85. if ( preg_match( $preg, $this_line, $matches ) ) {
  86. $error = $matches[0];
  87. $this_line = str_replace( '"', "'", $this_line );
  88. $error = ltrim( $error );
  89. $pre = ( FALSE !== ( $pos = strpos( $this_line, $error ) ) ? substr( $this_line, 0, $pos ) : FALSE );
  90. $pre = ltrim( htmlspecialchars( $pre ) );
  91. $bad_lines .= __("<pre class='tc-grep'>Line ", "theme-check") . ( $line_index+1 ) . ": " . $pre . htmlspecialchars( substr( stristr( $this_line, $error ), 0, 75 ) ) . "</pre>";
  92. }
  93. $line_index++;
  94. }
  95. return str_replace( $error, '<span class="tc-grep">' . $error . '</span>', $bad_lines );
  96. }
  97. function tc_strxchr($haystack, $needle, $l_inclusive = 0, $r_inclusive = 0){
  98. if(strrpos($haystack, $needle)){
  99. //Everything before last $needle in $haystack.
  100. $left = substr($haystack, 0, strrpos($haystack, $needle) + $l_inclusive);
  101. //Switch value of $r_inclusive from 0 to 1 and viceversa.
  102. $r_inclusive = ($r_inclusive == 0) ? 1 : 0;
  103. //Everything after last $needle in $haystack.
  104. $right = substr(strrchr($haystack, $needle), $r_inclusive);
  105. //Return $left and $right into an array.
  106. return array($left, $right);
  107. } else {
  108. if(strrchr($haystack, $needle)) return array('', substr(strrchr($haystack, $needle), $r_inclusive));
  109. else return false;
  110. }
  111. }
  112. function tc_filename( $file ) {
  113. $filename = tc_strxchr($file, '/themes/');
  114. $filename = str_replace( $filename, '', $file );
  115. $filename = str_replace( '/themes/', '', $filename );
  116. $filename .= basename($file);
  117. $remove = explode( '/', $filename );
  118. return ltrim( str_replace( $remove[0], '', $filename ), '/' );
  119. }
  120. function tc_trac( $e ) {
  121. $trac_left = array( '<strong>', '</strong>' );
  122. $trac_right= array( "'''", "'''" );
  123. $html_link = '/\<a href=\"(.*?)\"(.*?)\>(.*?)\<\/a\>/i';
  124. $html_new = '[$1 $3]';
  125. if ( defined( 'REVIEWER' ) ) {
  126. $e = preg_replace( $html_link, $html_new, $e);
  127. $e = str_replace($trac_left, $trac_right, $e);
  128. $e = preg_replace( '/<pre.*?>/', '<br />{{{<br />', $e);
  129. $e = str_replace( '</pre>', '<br />}}}', $e);
  130. }
  131. return $e;
  132. }