PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/toolset.dev/wp-content/plugins/wp-types/embedded/common/wplogger.php

https://github.com/bfay/aie-toolset
PHP | 308 lines | 151 code | 27 blank | 130 comment | 20 complexity | 6d54c1fe7f800657340e51ba8bc084eb MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /*
  3. * Modified standalone version of the WPLogger class for internal purposes.
  4. *
  5. *
  6. *
  7. Plugin Name: Wordpress Logger
  8. Plugin URI: http://www.turingtarpit.com/2009/05/wordpress-logger-a-plugin-to-display-php-log-messages-in-safari-and-firefox/
  9. Description: Displays log messages in the browser console in Safari, Firefox and Opera. Useful for plugin and theme developers to debug PHP code.
  10. Version: 0.3
  11. Author: Chandima Cumaranatunge
  12. Author URI: http://www.turingtarpit.com
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. Code to force the plugin to load before others adapted from the
  25. WordPress FirePHP plugin developed by Ivan Weiller.
  26. http://inchoo.net/wordpress/wordpress-firephp-plugin/
  27. Requirements:
  28. * PHP 5+
  29. * Wordpress 2.5+
  30. * JQuery 1.2.6
  31. * Firefox browser with firePHP plugin activated OR
  32. Safari browser with Error Console turned on
  33. Usage:
  34. $wplogger->log( mixed php_expression [, const message_type] )
  35. message_type can be: WPLOG_ERR. WPLOG_WARNING, WPLOG_INFO, WPLOG_DEBUG
  36. Example:
  37. if ($wplogger) $wplogger->log( get_option('active_plugins') );
  38. Output ( from the browser console ):
  39. [Information: from line xxx in file somefile.php] array (
  40. 0 => 'wplogger/wplogger.php',
  41. 1 => '12seconds-widget/12seconds-widget.php',
  42. 2 => 'get-the-image/get-the-image.php',
  43. )
  44. */
  45. /* Types of log messages */
  46. define( 'WPLOG_ERR', 'error' ); /* Error conditions */
  47. define( 'WPLOG_WARNING', 'warn' ); /* Warning conditions */
  48. define( 'WPLOG_INFO', 'info' ); /* Informational */
  49. define( 'WPLOG_DEBUG', 'debug' ); /* Debug-level messages */
  50. define( 'WPLOG_OFF', '' ); /* NO debug enabled */
  51. /* New Wordpress Logger instance */
  52. global $wplogger;
  53. $wplogger = new WPV_WPLogger();
  54. function wplogger( $message = '', $msgType = null )
  55. {
  56. global $wplogger;
  57. $wplogger->log( $message, $msgType );
  58. }
  59. /* Register function to add logging script */
  60. add_action( 'wp_footer', array($wplogger, 'flushLogMessages') ); // log scripts
  61. /* Ensure logging works in admin pages as well */
  62. add_action ('admin_footer', array ($wplogger, 'flushLogMessages'));
  63. /**
  64. * WPV_WPLogger Class
  65. * renamed for compatibility reasons
  66. */
  67. class WPV_WPLogger
  68. {
  69. /**
  70. * String holding the buffered output.
  71. */
  72. var $_buffer = array();
  73. /**
  74. * The default priority to use when logging an event.
  75. */
  76. var $_defaultMsgType = WPLOG_INFO;
  77. /**
  78. * Long descriptions of debug message types
  79. */
  80. var $_msgTypeLong = array(
  81. WPLOG_ERR => 'error',
  82. WPLOG_WARNING => 'warn',
  83. WPLOG_INFO => 'info',
  84. WPLOG_DEBUG => 'debug'
  85. );
  86. var $_msgStatusPriority = array(
  87. WPLOG_ERR => '50',
  88. WPLOG_WARNING => '40',
  89. WPLOG_INFO => '30',
  90. WPLOG_DEBUG => '20',
  91. WPLOG_OFF => '10'
  92. );
  93. /**
  94. * Writes JavaScript to flush all pending ("buffered") data to
  95. * the Firefox or Safari console.
  96. *
  97. * @notes requires JQuery 1.2.6 for browser detection.
  98. * browser detection is deprecated in JQuery 1.3
  99. * @see http://docs.jquery.com/Utilities/jQuery.browser
  100. */
  101. function flushLogMessages()
  102. {
  103. if ( count( $this->_buffer ) )
  104. {
  105. print '<script type="text/javascript">'."\n";
  106. print 'var $j=jQuery.noConflict();'."\n";
  107. print 'if (($j.browser.safari || $j.browser.webkit) && window.console) {'."\n";
  108. foreach ( $this->_buffer as $line )
  109. {
  110. printf( 'window.console.%s("%s");', $line[0], $line[1] );
  111. print "\n";
  112. }
  113. print '} else if ($j.browser.mozilla && (\'console\' in window)) {'."\n";
  114. foreach ( $this->_buffer as $line )
  115. {
  116. printf( 'console.%s("%s");', $line[0], $line[1] );
  117. print "\n";
  118. }
  119. print '} else if ($j.browser.opera && window.opera && opera.postError) {'."\n";
  120. foreach ( $this->_buffer as $line )
  121. {
  122. printf( 'opera.postError("%s");', $line[1] );
  123. print "\n";
  124. }
  125. print "}\n";
  126. print "</script>\n";
  127. }
  128. ;
  129. $this->_buffer = array();
  130. }
  131. /**
  132. * Buffers $message to be flushed to the Firebug or Safari console.
  133. *
  134. * Adapted from the PEAR_Log library
  135. *
  136. * @return boolean true
  137. * @param mixed $message String or object containing the message to log.
  138. * @param const $msgType[optional] type of message. Valid values are:
  139. * WPLOG_ERR. WPLOG_WARNING, WPLOG_INFO, WPLOG_DEBUG
  140. */
  141. function log( $message, $msgType = null )
  142. {
  143. /* backtrace */
  144. $bTrace = debug_backtrace(); // assoc array
  145. /* If a log message type hasn't been specified, use the default value. */
  146. if ( $msgType === null )
  147. {
  148. $msgType = $this->_defaultMsgType;
  149. }
  150. // verify the status type and output only priority messages (based on wp-config setup)
  151. if(!$this->isMsgVisible($msgType)) {
  152. return false;
  153. }
  154. /* Extract the string representation of the message. */
  155. $message = $this->_extractMessage( $message );
  156. /* normalize line breaks */
  157. $message = str_replace( "\r\n", "\n", $message );
  158. /* escape line breaks */
  159. $message = str_replace( "\n", "\\n\\\n", $message );
  160. /* escape quotes */
  161. $message = str_replace( '"', '\\"', $message );
  162. /* Build the string containing the complete log line. */
  163. $line = sprintf('[%s: from line %d in file %s] %s',
  164. $this->_msgTypeLong[ $msgType ],
  165. $bTrace[0]['line'],
  166. basename($bTrace[0]['file']),
  167. $message );
  168. // buffer method and line
  169. $this->_buffer[] = array($msgType, $line);
  170. return true;
  171. }
  172. /**
  173. * Returns the string representation of the message data (from the PEAR_Log library).
  174. *
  175. * If $message is an object, _extractMessage() will attempt to extract
  176. * the message text using a known method (such as a PEAR_Error object's
  177. * getMessage() method). If a known method, cannot be found, the
  178. * serialized representation of the object will be returned.
  179. *
  180. * If the message data is already a string, it will be returned unchanged.
  181. *
  182. * Adapted from the PEAR_Log library
  183. *
  184. * @param mixed $message The original message data. This may be a
  185. * string or any object.
  186. *
  187. * @return string The string representation of the message.
  188. *
  189. */
  190. function _extractMessage( $message )
  191. {
  192. /*
  193. * If we've been given an object, attempt to extract the message using
  194. * a known method. If we can't find such a method, default to the
  195. * "human-readable" version of the object.
  196. *
  197. * We also use the human-readable format for arrays.
  198. */
  199. if ( is_object( $message ) )
  200. {
  201. if ( method_exists( $message, 'getmessage' ) )
  202. {
  203. $message = $message->getMessage();
  204. }
  205. else if ( method_exists( $message, 'tostring' ) )
  206. {
  207. $message = $message->toString();
  208. }
  209. else if ( method_exists( $message, '__tostring' ) )
  210. {
  211. if ( version_compare( PHP_VERSION, '5.0.0', 'ge' ) )
  212. {
  213. $message = (string) $message;
  214. }
  215. else
  216. {
  217. $message = $message->__toString();
  218. }
  219. }
  220. else
  221. {
  222. $message = var_export( $message, true );
  223. }
  224. }
  225. else if ( is_array( $message ) )
  226. {
  227. if ( isset($message['message']) )
  228. {
  229. if ( is_scalar( $message['message'] ) )
  230. {
  231. $message = $message['message'];
  232. }
  233. else
  234. {
  235. $message = var_export( $message['message'], true );
  236. }
  237. }
  238. else
  239. {
  240. $message = var_export( $message, true );
  241. }
  242. }
  243. else if ( is_bool( $message ) || $message === NULL )
  244. {
  245. $message = var_export( $message, true );
  246. }
  247. /* Otherwise, we assume the message is a string. */
  248. return $message;
  249. }
  250. /**
  251. *
  252. * Is the message for the logger visible, i.e. is the status approved for output in the config
  253. *
  254. * @param status_type $msg_status the status level
  255. */
  256. function isMsgVisible($msg_status) {
  257. // verify that status for logging is set
  258. if(!defined('WPV_LOGGING_STATUS')) {
  259. return false;
  260. }
  261. // use default off status if status not in the list
  262. if(!in_array(WPV_LOGGING_STATUS, $this->_msgTypeLong) ||
  263. !in_array($msg_status, $this->_msgTypeLong)) {
  264. return false;
  265. }
  266. // verify priorities
  267. if( $this->_msgStatusPriority[$msg_status] >= $this->_msgStatusPriority[WPV_LOGGING_STATUS] ) {
  268. return true;
  269. }
  270. return false;
  271. }
  272. }
  273. ?>