PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/www/wp-content/plugins/ithemes-exchange/lib/classes/it-error/class-it-classes-fatal-error-parser.php

https://github.com/ArzuA/gitwordpress
PHP | 722 lines | 550 code | 154 blank | 18 comment | 114 complexity | a4749964c34ad86f02b707ed5111928b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. Written by Chris Jean for iThemes.com
  4. Version 1.0.0
  5. Version History
  6. 0.0.1 - 2012-07-01 - Chris Jean
  7. Initial development version
  8. 0.0.2 - 2012-07-06 - Chris Jean
  9. Added handler for add_help_sidebar() error
  10. 0.0.3 - 2012-07-06 - Chris Jean
  11. Added handler for builder_register_module_style() error
  12. 0.0.4 - 2012-07-06 - Chris Jean
  13. Streamlined message creation code
  14. Added handler for maximum execution time errors
  15. 1.0.0 - 2012-07-17 - Chris Jean
  16. Release ready
  17. */
  18. class IT_Classes_Fatal_Error_Parser {
  19. var $data = array();
  20. var $descriptions = array();
  21. var $solutions = array();
  22. var $message = '';
  23. var $solutions_complete = false;
  24. function IT_Classes_Fatal_Error_Parser( $error ) {
  25. $this->add_data( $error, 'error_' );
  26. $this->parse();
  27. }
  28. function add_data( $data, $prefix = '' ) {
  29. foreach ( $data as $key => $val )
  30. $this->data["{$prefix}$key"] = $val;
  31. }
  32. function parse() {
  33. $this->add_error_type_details();
  34. $this->add_source_data();
  35. $this->add_source_details();
  36. $this->add_software_details();
  37. $this->add_other_software_details();
  38. $this->build_descriptions();
  39. if ( ! $this->solutions_complete )
  40. $this->build_solutions();
  41. $this->generate_message();
  42. }
  43. function get_message() {
  44. return $this->message;
  45. }
  46. function generate_message() {
  47. $message = '';
  48. $message .= $this->get_message_block( $this->descriptions );
  49. $message .= $this->get_message_block( array( $this->solutions ), 'Solutions' );
  50. $message .= $this->get_message_table( $this->data['software_details'], 'Error Source Details' );
  51. $message .= $this->get_message_table( $this->data['other_software_details'], 'Other Software Details' );
  52. $error_data = array(
  53. 'Type' => "{$this->data['error_name']} ({$this->data['error_type']})",
  54. 'Message' => $this->data['error_message'],
  55. 'File' => $this->data['error_file'],
  56. 'Line' => $this->data['error_line'],
  57. 'Type Description' => $this->data['error_description'],
  58. );
  59. $message .= $this->get_message_table( $error_data, 'Full Error Details' );
  60. $this->message = $message;
  61. }
  62. function get_message_block( $data, $header = '', $depth = 0 ) {
  63. if ( empty( $data ) )
  64. return '';
  65. $message = '';
  66. if ( ! empty( $header ) )
  67. $message .= "<h2>$header</h2>\n";
  68. if ( is_string( $data ) ) {
  69. if ( $depth > 1 )
  70. $message .= "<li>$data</li>\n";
  71. else
  72. $message .= "<p>$data</p>\n";
  73. }
  74. else if ( is_array( $data ) ) {
  75. if ( $depth > 0 )
  76. $message .= "<ul>\n";
  77. foreach ( $data as $item )
  78. $message .= $this->get_message_block( $item, '', $depth + 1 );
  79. if ( $depth > 0 )
  80. $message .= "</ul>\n";
  81. }
  82. return $message;
  83. }
  84. function get_message_table( $data, $header = '' ) {
  85. if ( empty( $data ) || ! is_array( $data ) )
  86. return '';
  87. $message = '';
  88. if ( ! empty( $header ) )
  89. $message .= "<h2>$header</h2>\n";
  90. $message .= "<table>\n";
  91. foreach ( $data as $var => $val )
  92. $message .= "<tr><th scope='row'>$var</th><td><code>$val</code></td></tr>\n";
  93. $message .= "</table>\n";
  94. return $message;
  95. }
  96. function add_error_type_details() {
  97. $error_types = array(
  98. E_ERROR => array(
  99. 'E_ERROR',
  100. 'Fatal Error',
  101. 'This type of error indicates that PHP cannot continue to run the code. Typical causes of this type of error are code bugs that have typos, missing or incomplete files (such as a file that was only partially uploaded), and the code using more memory than it is allowed.',
  102. ),
  103. E_PARSE => array(
  104. 'E_PARSE',
  105. 'Parse Error',
  106. 'This type of error indicates that the PHP code is invalid and is typically referred to as a syntax error. It basically means that the code was written incorrectly and is preventing PHP from being able to read the code properly.',
  107. ),
  108. E_CORE_ERROR => array(
  109. 'E_CORE_ERROR',
  110. 'Core Fatal Error',
  111. 'This type of error indicates a fatal error occurred as PHP started up. This typically a faulty PHP configuration on the server.',
  112. ),
  113. E_COMPILE_ERROR => array(
  114. 'E_COMPILE_ERROR',
  115. 'Zend Compile-Time Error',
  116. 'This type of error indicates that the PHP code is violating a restriction enforced by PHP. An example of such a violation is trying to use a reserved word for a class or function name.',
  117. ),
  118. E_USER_ERROR => array(
  119. 'E_USER_ERROR',
  120. 'Code-Generated Fatal Error via trigger_error()',
  121. 'Code running on the site purposefully triggered an error. This typically indicates that some unrecoverable problem happened in the code that could only be properly handled by forcing an error so that the code would no longer run.',
  122. ),
  123. E_STRICT => array(
  124. 'E_STRICT',
  125. 'Strict Fatal Error',
  126. 'Strict errors only occur when E_STRICT <a href="http://php.net/manual/en/function.error-reporting.php">error reporting</a> is enabled. These types of errors indicate that the best-practices in PHP development are not followed.',
  127. ),
  128. );
  129. if ( ! isset( $error_types[$this->data['error_type']] ) )
  130. return false;
  131. $details = array(
  132. 'code' => $this->data['error_type'],
  133. 'type' => $error_types[$this->data['error_type']][0],
  134. 'name' => $error_types[$this->data['error_type']][1],
  135. 'description' => $error_types[$this->data['error_type']][2],
  136. );
  137. $this->add_data( $details, 'error_' );
  138. }
  139. function add_description( $description ) {
  140. if ( ! empty( $description ) )
  141. $this->descriptions[] = $description;
  142. }
  143. function add_solution( $solution ) {
  144. if ( ! empty( $solution ) )
  145. $this->solutions[] = $solution;
  146. }
  147. function build_descriptions() {
  148. $this->add_source_specific_descriptions();
  149. $this->add_basic_error_message_descriptions();
  150. }
  151. function add_basic_error_message_descriptions() {
  152. $match = false;
  153. if ( preg_match( '/Maximum execution time of (\d+)( seconds?)/', $this->data['error_message'], $match ) ) {
  154. $max = $match[1];
  155. $this->add_description( "Your server is set to limit PHP execution to a maximum of <code>{$match[1]}</code>{$match[2]}. The code running on your site is exceeding this time limit." );
  156. $this->add_description( 'This can be caused by a number of different situations:' );
  157. $causes = array(
  158. 'Code that requires more time than the server will allow.',
  159. 'Code that attempts to contact a remote server but is unable to do so.',
  160. 'The server could be overloaded, causing each page request to take too long to process.',
  161. );
  162. $this->add_description( $causes );
  163. if ( is_callable( 'sys_getloadavg' ) ) {
  164. $load = sys_getloadavg();
  165. $load[0] = 4;
  166. if ( $load[0] > 10 ) {
  167. $this->add_description( "The load on your server is <code>{$load[0]}</code>. This is extremely high and indicates that your server is overloaded." );
  168. $this->add_solution( 'Contact your hosting provider for help in finding out why the load is so high.' );
  169. }
  170. else if ( $load[0] > 3 ) {
  171. $this->add_description( "The load on your server is <code>{$load[0]}</code>. Depending on the server hardware, this could be a very high load and could indicate performance issues due to the server being overloaded." );
  172. $this->add_solution( 'Contact your hosting provider for help in finding out why the load is so high.' );
  173. }
  174. else if ( $load[0] > 1 ) {
  175. $this->add_description( "The load on your server is <code>{$load[0]}</code>. Depending on the server hardware, this could be a high load and could indicate performance issues due to the server being overloaded." );
  176. $this->add_solution( 'Contact your hosting provider for help in finding out if there are any server issues.' );
  177. }
  178. else {
  179. $this->add_description( "The load on your server is <code>{$load[0]}</code>. Your server should not be overloaded. This means that the code simply took more time than allowed to process." );
  180. }
  181. }
  182. else {
  183. $this->add_solution( 'If all of your site\'s page requests are failing, your server may be overloaded. Contact your hosting provider for help in determining if the server is overloaded and what can be done to fix the issue.' );
  184. }
  185. if ( '0' == $max ) {
  186. $this->add_description( "The <code><a href='http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'>max_execution_time</a></code> is set to <code>$max</code>. This should allow an unlimited amount of time for script execution." );
  187. $this->add_solution( 'Contact your hosting provider and ask why setting the <code><a href="http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time">max_execution_time</a></code> to <code>0</code> isn\'t allowing for unlimited execution time.' );
  188. }
  189. else if ( $max <= 10 ) {
  190. $this->add_description( "The <code><a href='http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'>max_execution_time</a></code> is set to <code>$max</code>. This is very low (the default is <code>30</code>) and when combined with complex code and/or a slower server, could easily cause page load failures." );
  191. $this->add_solution( 'Contact your hosting provider and ask about increasing the <code><a href="http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time">max_execution_time</a></code> on your site to at least <code>30</code>.' );
  192. }
  193. else {
  194. $this->add_description( "The <code><a href='http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'>max_execution_time</a></code> is set to <code>$max</code>. This should be more than enough time to process most page requests." );
  195. $this->add_solution( 'If a complex task is running on your site, it needs more time to finish succesfully. Contact your hosting provider and ask about temporarily increasing the <code><a href="http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time">max_execution_time</a></code> on your site so that the process can finish.' );
  196. $message = 'If this is a normal page request, too much time is being used to render the page.';
  197. if ( 'WordPress core' == $this->data['source_type'] )
  198. $this->add_solution( "$message Create a support request on the <a href='http://wordpress.org/support/'>official WordPress support forum</a> and post these error details to get help with fixing this issue." );
  199. else if ( 'theme' == $this->data['source_type'] )
  200. $this->add_solution( "$message Contact the {$this->data['source_name']} {$this->data['source_type']} author to find a solution to the problem. A temporary solution is to switch to another theme so that site functionality is restored." );
  201. else if ( preg_match( '/plugin/i', $this->data['source_type'] ) )
  202. $this->add_solution( "$message Contact the {$this->data['source_name']} {$this->data['source_type']} author to find a solution to the problem. A temporary solution is to disable the plugin so that site functionality is restored." );
  203. }
  204. $this->solutions_complete = true;
  205. return;
  206. }
  207. if ( preg_match( '/Call to undefined method WP_Screen::add_help_sidebar/', $this->data['error_message'] ) ) {
  208. $this->add_description( "The <code>WP_Screen::add_help_sidebar()</code> function existed in the development version of WordPress 3.3. Before the release of WordPress 3.3, it was renamed to <code>WP_Screen::set_help_sidebar()</code>." );
  209. if ( preg_match( '|builder-core/admin-functions\.php|', $this->data['error_file'] ) )
  210. $this->add_solution( "Upgrading the Builder core theme to a current version will fix this issue." );
  211. $this->add_solution( "Changing the function name in the code from <code>add_help_sidebar()</code> to <code>set_help_sidebar()</code> will fix this issue." );
  212. return;
  213. }
  214. if ( preg_match( '/Call to undefined function builder_register_module_style/', $this->data['error_message'] ) ) {
  215. $builder_data = array();
  216. if ( ! empty( $this->data['source_parent_data'] ) && ! empty( $this->data['source_parent_data']['name'] ) && ( 'Builder' == $this->data['source_parent_data']['name'] ) )
  217. $builder_data = $this->data['source_parent_data'];
  218. else if ( ! empty( $this->data['source_data']['name'] ) && ( 'Builder' == $this->data['source_data']['name'] ) )
  219. $builder_data = $this->data['source_data'];
  220. if ( ! empty( $builder_data ) ) {
  221. if ( ! empty( $builder_data['version'] ) && version_compare( $builder_data['version'], '2.7.0', '<' ) ) {
  222. $this->add_description( "The <code>builder_register_module_style()</code> function was added in Builder version 2.7.0. This site is running Builder version {$builder_data['version']} which does not have this feature." );
  223. $this->add_solution( "Upgrading the Builder core theme to a current version will fix this issue." );
  224. }
  225. else {
  226. $this->add_description( "The <code>builder_register_module_style()</code> function must be called after the <code>it_libraries_loaded</code> action in order for it to function properly. <a href='http://ithemes.com/codex/page/Builder_Features#Custom_Module_Styles'>This tutorial</a> has information on how to properly add Alternate Module Styles." );
  227. $this->add_solution( "Read <a href='http://ithemes.com/codex/page/Builder_Features#Custom_Module_Styles'>this tutorial</a> for information on how to properly add Alternate Module Styles and make the necessary modifications to your code." );
  228. }
  229. }
  230. else {
  231. $this->add_description( "The <code>builder_register_module_style()</code> is provided by the <a href='http://ithemes.com/purchase/builder-theme/'>Builder theme</a>. Your site does not appear to be running the Builder theme." );
  232. $this->add_solution( "Switch to the Builder theme or remove the customizations that call the <code>builder_register_module_style()</code> function." );
  233. }
  234. return;
  235. }
  236. if ( preg_match( '/Cannot redeclare ([^\(]+)/', $this->data['error_message'], $match ) ) {
  237. $this->add_description( "The code tried to create a function that already existed (<code>{$match[1]}()</code>). This error typically occurs when the PHP file that defines the function is loaded more than once, which could indicate a bug in the code or that some modification is incorrectly trying to load the file again. This type of error can sometimes indicate a plugin conflict which can occur if two plugins try to use the same function name." );
  238. return;
  239. }
  240. if ( preg_match( '/Call to undefined (function|method) ([^\(]+)/', $this->data['error_message'], $match ) ) {
  241. $this->add_description( "The code tried to run a function that doesn't exist (<code>{$match[2]}()</code>). This type of error is typically caused by a simple typo (<code>is_hom()</code> rather than <code>is_home()</code>), by calling a function provided by a plugin when that plugin is not activated, by calling a function before it is ready (such as calling a function before the code that creates it has run), or by using a function that no longer exists (the function may have been removed from WordPress core or the plugin/theme that previously supplied it)." );
  242. return;
  243. }
  244. if ( preg_match( '/Class \'([^\']+)\' not found/', $this->data['error_message'], $match ) ) {
  245. $this->add_description( "The code tried to use a PHP class that doesn't exist (<code>{$match[1]}</code>). This type of error is typically caused by a simple typo (<code>WP_error</code> rather than <code>WP_Error</code>), by trying to use a class that is provided by a plugin when that plugin is not activated, by trying to use a class before it is ready (such as trying to use a class before the code that creates it has run), or by trying to use a class that no longer exists (the class may have been removed from WordPress core or the plugin/theme that previously supplied it)." );
  246. return;
  247. }
  248. if ( preg_match( '/([^\(]+)\(\)( \[[^\]]+\])?: Failed opening required \'([^\']+)\'/', $this->data['error_message'], $match ) ) {
  249. if ( in_array( $this->data['source_type'], array( 'theme', 'plugin' ) ) )
  250. $update = " Try uploading the {$this->data['source_type']} again to see if that fixes the problem.";
  251. else if ( 'WordPress core' == $this->data['source_type'] )
  252. $update = " If the missing file is part of WordPress, you may have to <a href=\"http://codex.wordpress.org/Updating_WordPress#Manual_Update\">manually update the WordPress files</a>.";
  253. $this->add_description( "A required file (<code>{$match[3]}</code>) was unable to be loaded since it does not exist. This error typically indicates that the code has a typo (<code>{$match[1]}('hom.php')</code> rather than <code>{$match[1]}('home.php')</code>) or that the file is actually missing.$update" );
  254. return;
  255. }
  256. if ( preg_match( '/syntax error, unexpected \$end/', $this->data['error_message'] ) ) {
  257. $this->add_description( "The file indicated in the error message is unable to be processed as a valid PHP file. This can be due to a bug such as a missing close curly brace (<code>}</code>) or because the file was not completely uploaded." );
  258. return;
  259. }
  260. if ( preg_match( '/syntax error/', $this->data['error_message'] ) ) {
  261. $this->add_description( "This type error is called a \"syntax error.\" A syntax error means that the format of the code is invalid. Examples of syntax errors: a missing semicolon after a line of code, having mismatched parentheses, forgetting to put a dollar sign (<code>$</code>) in front of a variable's name, and forgetting an operator &mdash; such as a plus sign (<code>+</code>) &mdash; between two variables. There are many other possible causes of syntax errors, the preceding are just a few examples." );
  262. return;
  263. }
  264. if ( preg_match( '/Call-time pass-by-reference has been removed/', $this->data['error_message'] ) ) {
  265. $this->add_description( "This error is due to your server running PHP version <code>" . phpversion() . "</code>. Starting in 5.4.0, PHP no longer permits passing function arguments by reference. The {$this->data['source_type']} code is violating this rule and is causing the code to fail." );
  266. $this->add_solution( "If you are able, switching your PHP version to 5.2.4+ or 5.3.0+ will allow your site to function properly again." );
  267. $this->add_solution( "Contact the {$this->data['source_type']} author and notify them of the issue. It could be possible to have the problem solved quickly." );
  268. }
  269. }
  270. function add_source_specific_descriptions() {
  271. if ( 'WordPress core' == $this->data['source_type'] ) {
  272. $this->add_description( 'The WordPress code created an error that caused PHP execution to fail. This typically happens when the WordPress code has been modified.' );
  273. }
  274. else {
  275. $this->add_description( "The {$this->data['details_name']} {$this->data['source_type']} code created an error that caused PHP execution to fail." );
  276. if ( 'must-use plugin' == $this->data['source_type'] )
  277. $this->add_description( "<strong>Note:</strong> Must-use plugins are not the same as regular plugins. Must-use plugins exist in a different directory than the regular plugins, and must-use plugins do not require activation since they always run on every page load." );
  278. else if ( 'drop-in plugin' == $this->data['source_type'] )
  279. $this->add_description( "<strong>Note:</strong> Drop-in plugins are not the same as regular plugins. Drop-in plugins exist in the <kbd>wp-content</kbd> directory, and drop-in plugins do not require activation since they always run when needed (such as some drop-in plugins only running in multisite installations)." );
  280. }
  281. }
  282. function build_solutions() {
  283. $solutions = array();
  284. if ( 'theme' == $this->data['source_type'] ) {
  285. $theme_solutions = array(
  286. 'modification',
  287. 'out_of_date',
  288. 'plugin_conflict',
  289. 'reupload',
  290. );
  291. if ( ! empty( $this->data['source_parent_data'] ) )
  292. $theme_solutions[] = 'parent_theme_conflict';
  293. $solutions = array_merge( $solutions, $theme_solutions );
  294. }
  295. else if ( in_array( $this->data['source_type'], array( 'plugin', 'must-use plugin', 'drop-in plugin' ) ) ) {
  296. $plugin_solutions = array( 'modification', 'out_of_date', 'plugin_conflict_for_plugins', 'reupload' );
  297. $solutions = array_merge( $solutions, $plugin_solutions );
  298. }
  299. else if ( 'sunrise.php' == $this->data['source_type'] ) {
  300. $sunrise_solutions = array( 'modification', 'plugin_conflict' );
  301. $solutions = array_merge( $solutions, $sunrise_solutions );
  302. }
  303. else if ( 'WordPress core' == $this->data['source_type'] ) {
  304. $wordpress_solutions = array( 'modification', 'reupload_wordpress_core' );
  305. $solutions = array_merge( $solutions, $wordpress_solutions );
  306. }
  307. $this->add_standard_solution( array_unique( $solutions ) );
  308. }
  309. function add_source_data() {
  310. if ( preg_match( '|^' . preg_quote( WP_PLUGIN_DIR, '|' ) . '/|', $this->data['error_file'] ) )
  311. $this->add_plugin_source_data();
  312. else if ( preg_match( '|^' . preg_quote( PLUGINDIR, '|' ) . '/|', $this->data['error_file'] ) )
  313. $this->add_plugin_source_data( PLUGINDIR );
  314. else if ( preg_match( '|^' . preg_quote( WPMU_PLUGIN_DIR, '|' ) . '/|', $this->data['error_file'] ) )
  315. $this->add_plugin_source_data( WPMU_PLUGIN_DIR, 'must-use plugin' );
  316. else if ( preg_match( '|^' . preg_quote( MUPLUGINDIR, '|' ) . '/|', $this->data['error_file'] ) )
  317. $this->add_plugin_source_data( MUPLUGIN_DIR, 'must-use plugin' );
  318. else if ( preg_match( '#^(' . preg_quote( ABSPATH . WPINC, '#' ) . '|'. preg_quote( ABSPATH . 'wp-admin', '#' ) . ')#', $this->data['error_file'] ) )
  319. $this->add_wordpress_core_source_data();
  320. else if ( preg_match( '#^' . preg_quote( WP_CONTENT_DIR, '#' ) . '/[^/]+\.php$#', $this->data['error_file'] ) )
  321. $this->add_plugin_source_data( WP_CONTENT_DIR, 'drop-in plugin' );
  322. else
  323. $this->add_theme_source_data();
  324. if ( empty( $this->data['source_type'] ) )
  325. $this->add_wordpress_core_source_data();
  326. if ( empty( $this->data['source_type'] ) ) {
  327. $source = array(
  328. 'type' => 'Unknown',
  329. 'path' => $this->data['error_file'],
  330. 'file' => basename( $this->data['error_file'] ),
  331. );
  332. $this->add_data( $source, 'source_' );
  333. }
  334. if ( empty( $this->data['source_pretty_type'] ) )
  335. $this->data['source_pretty_type'] = ucwords( $this->data['source_type'] );
  336. }
  337. function add_wordpress_core_source_data() {
  338. $source = array(
  339. 'type' => 'WordPress core',
  340. 'pretty_type' => 'WordPress Core',
  341. 'path' => ABSPATH,
  342. 'file' => preg_replace( '/^' . preg_quote( ABSPATH, '/' ) . '/', '', $this->data['error_file'] ),
  343. );
  344. $this->add_data( $source, 'source_' );
  345. }
  346. function add_theme_source_data() {
  347. $source = false;
  348. if ( ! empty( $GLOBALS['wp_theme_directories'] ) )
  349. $directories = (array) $GLOBALS['wp_theme_directories'];
  350. else if ( function_exists( 'get_theme_root' ) )
  351. $directories = array( get_theme_root() );
  352. else
  353. $directories = array( WP_CONTENT_DIR . '/themes' );
  354. foreach ( $directories as $directory ) {
  355. if ( preg_match( '|^' . preg_quote( $directory, '|' ) . '/|', $this->data['error_file'] ) )
  356. break;
  357. $directory = null;
  358. }
  359. if ( is_null( $directory ) )
  360. return false;
  361. $source = array(
  362. 'type' => 'theme',
  363. 'path' => preg_replace( '/^(' . preg_quote( $directory . DIRECTORY_SEPARATOR, '/' ) . "[^" . preg_quote( DIRECTORY_SEPARATOR, '/' ) . ']+).*/', '$1', $this->data['error_file'] ),
  364. );
  365. $source['slug'] = basename( $source['path'] );
  366. $source['file'] = preg_replace( '/^' . preg_quote( $source['path'] . DIRECTORY_SEPARATOR, '/' ) . '/', '', $this->data['error_file'] );
  367. $source['data'] = $this->get_file_data( "{$source['path']}/style.css", $this->get_theme_headers() );
  368. if ( ! empty( $source['data']['template'] ) ) {
  369. foreach ( $directories as $directory ) {
  370. if ( file_exists( "$directory/{$source['data']['template']}/style.css" ) )
  371. break;
  372. $directory = null;
  373. }
  374. if ( ! is_null( $directory ) )
  375. $source['parent_data'] = $this->get_file_data( "$directory/{$source['data']['template']}/style.css", $this->get_theme_headers() );
  376. }
  377. $this->add_data( $source, 'source_' );
  378. }
  379. function add_plugin_source_data( $base_path = WP_PLUGIN_DIR, $type = 'plugin' ) {
  380. $source = array();
  381. $source['type'] = $type;
  382. $source['path'] = preg_replace( '/^(' . preg_quote( $base_path . DIRECTORY_SEPARATOR, '/' ) . "[^" . preg_quote( DIRECTORY_SEPARATOR, '/' ) . ']+).*/', '$1', $this->data['error_file'] );
  383. $source['slug'] = basename( $source['path'] );
  384. if ( is_file( $source['path'] ) ) {
  385. $source['file'] = basename( $source['path'] );
  386. $source['plugin_type'] = 'file';
  387. }
  388. else {
  389. $source['file'] = preg_replace( '/^' . preg_quote( $source['path'] . DIRECTORY_SEPARATOR, '/' ) . '/', '', $this->data['error_file'] );
  390. $source['plugin_type'] = 'directory';
  391. }
  392. if ( is_dir( $source['path'] ) && ( false !== ( $dir = @opendir( $source['path'] ) ) ) ) {
  393. while ( false !== ( $file = readdir( $dir ) ) ) {
  394. if ( '.' == substr( $file, 0, 1 ) )
  395. continue;
  396. if ( '.php' == substr( $file, -4 ) ) {
  397. $data = $this->get_file_data( "{$source['path']}/$file", $this->get_plugin_headers() );
  398. if ( ! empty( $data['name'] ) ) {
  399. $source['data'] = $data;
  400. break;
  401. }
  402. }
  403. }
  404. closedir( $dir );
  405. }
  406. $this->add_data( $source, 'source_' );
  407. }
  408. function add_standard_solution( $types ) {
  409. if ( ! isset( $this->standard_solutions ) ) {
  410. $this->standard_solutions = array(
  411. 'modification' => sprintf( 'Invalid code modifications can cause this problem. If you have made any modifications to the %1$s, remove them and try to load the site again.', $this->data['source_type'] ),
  412. 'out_of_date' => sprintf( 'It is possible that this %1$s\'s code is out of date and that an upgrade is available. Check with the %2$s %1$s\'s author%3$s to see if an upgrade is available.', $this->data['source_type'], $this->get_details( 'name' ), $this->get_details( 'author', ' (', ')' ) ),
  413. 'plugin_conflict' => 'There may be a conflict with a plugin running on the site. Try upgrading all the plugins on the site. A plugin conflict can be ruled out by deactivating all the active plugins on the site and checking to see if the error still occurs.',
  414. 'plugin_conflict_for_plugins' => 'There may be a conflict with another plugin running on the site. Try upgrading the other plugins on the site. A plugin conflict can be ruled out by deactivating all the other active plugins on the site and checking to see if the error still occurs.',
  415. 'reupload' => sprintf( 'The %1$s %2$s may not have been fully uploaded. Uploading the %2$s again could fix the issue. <strong>Important:</strong> If you do this, you will lose any modifications made to the %2$s.', $this->data['details_name'], $this->data['source_type'] ),
  416. 'reupload_wordpress_core' => sprintf( 'The %1$s %2$s may not have been fully uploaded. This can happen with bad or incomplete upgrades. Uploading the %2$s files again could fix the issue. You can find details on how to manually update WordPress\'s files in <a href="http://codex.wordpress.org/Updating_WordPress#Manual_Update">these instructions</a>.', $this->data['details_name'], $this->data['source_type'] ),
  417. );
  418. if ( ! empty( $this->data['source_parent_data'] ) ) {
  419. $this->standard_solutions['parent_theme_conflict'] = sprintf( 'The theme that triggered the error has a parent theme (%1$s version %2$s). The parent theme may be out of date. Check with the %1$s theme\'s author%3$s to see if an upgrade is available. If the parent theme was recently upgraded, a change in the parent theme may cause any modifications present in the child theme to no longer function. Remove your child theme modifications and load the site again to see if this resolves the error.', $this->get_details( 'parent_name' ), $this->get_details( 'parent_version' ), $this->get_details( 'parent_author' ) );
  420. }
  421. }
  422. foreach ( (array) $types as $type ) {
  423. if ( ! empty( $this->standard_solutions[$type] ) )
  424. $this->add_solution( $this->standard_solutions[$type] );
  425. }
  426. }
  427. function add_software_details() {
  428. $details = array();
  429. $details['Type'] = ucfirst( $this->data['source_type'] );
  430. $details_vars = array(
  431. 'details_name' => 'Name',
  432. 'details_version' => 'Version',
  433. 'details_author' => 'Author',
  434. );
  435. foreach ( $details_vars as $var => $name ) {
  436. if ( ! empty( $this->data[$var] ) )
  437. $details[$name] = $this->data[$var];
  438. }
  439. if ( ! empty( $this->data['source_path'] ) ) {
  440. $details['Path'] = $this->data['source_path'];
  441. if ( empty( $this->data['source_plugin_type'] ) || ( 'file' != $this->data['source_plugin_type'] ) )
  442. $details['File'] = $this->data['source_file'];
  443. }
  444. $this->add_data( array( 'software_details' => $details ) );
  445. }
  446. function add_other_software_details() {
  447. $details = array();
  448. $details_vars = array(
  449. 'details_parent_name' => 'Parent Theme Name',
  450. 'details_parent_author' => 'Parent Theme Author',
  451. 'details_parent_version' => 'Parent Theme Version',
  452. );
  453. foreach ( $details_vars as $var => $name ) {
  454. if ( ! empty( $this->data[$var] ) )
  455. $details[$name] = $this->data[$var];
  456. }
  457. $details['WordPress Version'] = $GLOBALS['wp_version'];
  458. $details['PHP Version'] = phpversion();
  459. $this->add_data( array( 'other_software_details' => $details ) );
  460. }
  461. function get_details( $type, $prefix = '', $suffix = '' ) {
  462. if ( empty( $this->details[$type] ) )
  463. return '';
  464. return "$prefix{$this->details[$type]}$suffix";
  465. }
  466. function add_source_details() {
  467. $details = array(
  468. 'name' => '',
  469. 'author' => '',
  470. 'version' => '',
  471. 'parent_name' => '',
  472. 'parent_author' => '',
  473. 'parent_version' => '',
  474. );
  475. $types = array(
  476. 'data' => '',
  477. 'parent_data' => 'parent_',
  478. );
  479. foreach ( $types as $type => $prefix ) {
  480. if ( ! empty( $this->data["source_$type"] ) ) {
  481. if ( ! empty( $this->data["source_$type"]['name'] ) ) {
  482. if ( ! empty( $this->data["source_$type"]['uri'] ) )
  483. $details[$prefix . 'name'] = "<a href='{$this->data["source_$type"]['uri']}'>{$this->data["source_$type"]['name']}</a>";
  484. else
  485. $details[$prefix . 'name'] = $this->data["source_$type"]['name'];
  486. }
  487. else if ( ( 'data' != $type ) && ! empty( $this->data['source_slug'] ) ) {
  488. $details[$prefix . 'name'] = $this->data['source_slug'];
  489. }
  490. if ( ! empty( $this->data["source_$type"]['author'] ) ) {
  491. if ( ! empty( $this->data["source_$type"]['author_uri'] ) )
  492. $details[$prefix . 'author'] = "<a href='{$this->data["source_$type"]['author_uri']}'>{$this->data["source_$type"]['author']}</a>";
  493. else
  494. $details[$prefix . 'author'] = $this->data["source_$type"]['author'];
  495. }
  496. if ( ! empty( $this->data["source_$type"]['version'] ) ) {
  497. $details[$prefix . 'version'] = $this->data["source_$type"]['version'];
  498. }
  499. }
  500. }
  501. $this->add_data( $details, 'details_' );
  502. }
  503. function get_theme_headers() {
  504. $headers = array(
  505. 'name' => 'Theme Name',
  506. 'version' => 'Version',
  507. 'uri' => 'Theme URI',
  508. 'description' => 'Description',
  509. 'author' => 'Author',
  510. 'author_uri' => 'Author URI',
  511. 'text_domain' => 'Text Domain',
  512. 'domain_path' => 'Domain Path',
  513. 'template' => 'Template',
  514. 'status' => 'Status',
  515. 'tags' => 'Tags',
  516. );
  517. return $headers;
  518. }
  519. function get_plugin_headers() {
  520. $headers = array(
  521. 'name' => 'Plugin Name',
  522. 'version' => 'Version',
  523. 'uri' => 'Plugin URI',
  524. 'description' => 'Description',
  525. 'author' => 'Author',
  526. 'author_uri' => 'Author URI',
  527. 'text_domain' => 'Text Domain',
  528. 'domain_path' => 'Domain Path',
  529. 'network' => 'Network',
  530. );
  531. return $headers;
  532. }
  533. // Modified from the WP core get_file_data function from WP 3.5-alpha
  534. function get_file_data( $file, $headers, $context = '' ) {
  535. if ( ! is_file( $file ) || ! is_readable( $file ) )
  536. return array();
  537. $fp = fopen( $file, 'r' );
  538. $file_data = fread( $fp, 8192 );
  539. fclose( $fp );
  540. $file_data = str_replace( "\r", "\n", $file_data );
  541. foreach ( $headers as $field => $regex ) {
  542. if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )
  543. $headers[$field] = trim( preg_replace( "/\s*(?:\*\/|\?>).*/", '', $match[1] ) );
  544. else
  545. $headers[$field] = '';
  546. }
  547. return $headers;
  548. }
  549. }