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

/core/includes/helper-functions.php

https://bitbucket.org/esilverman/ph-salesforce
PHP | 345 lines | 213 code | 86 blank | 46 comment | 37 complexity | 8c79622f42e752a3de16e8d1a15c47fe MD5 | raw file
  1. <?php
  2. // Print arrays to JS console immediately inline
  3. // Different from console_log which collects logs (see below)
  4. if( !function_exists( 'console_json' ) ) {
  5. function console_json($array, $title="json array") {
  6. echo "<script type='text/javascript'>";
  7. if ($title !== 0) {
  8. echo "console.log('$title');";
  9. }
  10. echo "console.log(".json_encode($array).");
  11. </script>";
  12. }
  13. }
  14. // Alias for console_log
  15. if( !function_exists( 'jslog' ) ) {
  16. function jslog($array, $title="logged var") {
  17. console_json($array, $title);
  18. }
  19. }
  20. /**
  21. * Different from console_json:
  22. * This function collects logs throughout the PHP process
  23. * And prints them as inline JS in the footer
  24. * if the LOG_TO_CONSOLE var == TRUE
  25. */
  26. if( !function_exists( 'console_log' ) ) {
  27. function console_log($array, $title="logged var") {
  28. global $logs;
  29. if ( count($logs) == 0 ) {
  30. $logs = array(
  31. "<script type='text/javascript'>",
  32. "</script>"
  33. );
  34. }
  35. $backtrace = debug_backtrace();
  36. $fullArray = array(
  37. "\"$title\"" => $array
  38. , "From" => $backtrace[0]['file']
  39. );
  40. array_splice($logs, count($logs) - 1, 0,
  41. "console.log(".json_encode($fullArray).");"
  42. );
  43. }
  44. }
  45. /**
  46. * This function sits in the footer and
  47. * prints the logs collected to the JS console
  48. */
  49. if( !function_exists( 'print_logs' ) ) {
  50. function print_logs() {
  51. global $logs;
  52. if (LOG_TO_CONSOLE && $logs !== null )
  53. echo implode("\n\t",$logs) . "\n";
  54. }
  55. }
  56. /**
  57. * Used for PHP console logging
  58. *
  59. * If configured properly can be used with
  60. * Log Viewer WP plugin : http://wordpress.org/plugins/log-viewer/
  61. */
  62. if( !function_exists( '_log' ) ) {
  63. function _log( $data , $label = null , $backtrace = false ) {
  64. // Prevent logging if we're not in debug mode
  65. if( !defined('PH_SF_DEBUG') || !PH_SF_DEBUG )
  66. return;
  67. // Stop execution and log to ChromePhp if the class exists
  68. if( class_exists('ChromePhp') ) {
  69. if( $label ) {
  70. $r = array(
  71. 'label' => $label,
  72. 'data' => $data
  73. );
  74. return ChromePhp::log( $r );
  75. }
  76. return ChromePhp::log( $data );
  77. }
  78. $output = '';
  79. switch (gettype($data)) {
  80. case "array":
  81. case "object":
  82. $output .= print_r($data, TRUE);
  83. if ( $label !== null )
  84. $output .= "\n\n---^^^^^^^ END $label ^^^^^^^---";
  85. break;
  86. case "integer":
  87. case "float":
  88. case "string":
  89. $output .= $data;
  90. break;
  91. case "boolean":
  92. $converted_data = ($data) ? 'true' : 'false';
  93. $output .= $converted_data;
  94. break;
  95. default:
  96. $output .= "ERROR: tried to log bad type [".gettype($data)."]";
  97. break;
  98. }
  99. if ( $backtrace )
  100. _backtrace( $label );
  101. if ( !empty( $label ) ) {
  102. $output = "\t--- [ $label ] ---\n\n" . $output . "\n";
  103. }
  104. error_log($output);
  105. } // end _log()
  106. } // end if exists _log()
  107. if( !function_exists( '_log_v' ) ) {
  108. // "Verbose-only" alias of _log
  109. function _log_v( $data , $label = null , $backtrace = false ) {
  110. global $color_formats;
  111. $verbose = getopt( "", array('verbose') );
  112. if( $verbose ) {
  113. if ( !in_array( gettype( $data ) , array( 'object', 'stdObject', 'array' ) ) ) {
  114. $data = sprintf( $color_formats['green'], $data );
  115. } else {
  116. $label = sprintf( $color_formats['green'], $label );
  117. }
  118. _log( $data, $label, $backtrace );
  119. }
  120. } // end _log_v()
  121. } // end if exists _log_v()
  122. if( !function_exists( '_backtrace' ) ) {
  123. function _backtrace( $label = '' ) {
  124. ob_start();
  125. if ( phpversion() >= 5.4 ) {
  126. debug_print_backtrace( 0 , 5);
  127. } else {
  128. debug_print_backtrace();
  129. }
  130. $trace = ob_get_contents();
  131. ob_end_clean();
  132. _log( $trace, 'backtrace '. $label );
  133. _log("\t---^^^^^^^ END backtrace ^^^^^^^---");
  134. }
  135. }
  136. if( !function_exists( '_backtrace_files' ) ) {
  137. function _backtrace_files() {
  138. $backtrace = debug_backtrace();
  139. foreach( $backtrace as $key => $trace ) {
  140. if( isset( $trace['file'] ) )
  141. _log($trace['file']);
  142. }
  143. }
  144. }
  145. /**
  146. * Shorthand for custom post type query
  147. *
  148. * Accepts the following params:
  149. * Custom Post Type - (string) $cptName
  150. * Limit for number of posts - (int) $posts_per_page
  151. * [Optional] Array of args used in WP_Query
  152. */
  153. if( !function_exists( 'cpt_query' ) ) {
  154. function cpt_query($cptName, $posts_per_page=-1, $args=NULL) {
  155. $defaults = array(
  156. 'post_type' => $cptName,
  157. 'posts_per_page' => $posts_per_page
  158. );
  159. $args = wp_parse_args($args, $defaults);
  160. $cptQuery = new WP_Query($args);
  161. return $cptQuery;
  162. }
  163. }
  164. /*$
  165. * Return or echo post permalink by title
  166. */
  167. if( !function_exists( 'permalink_by_title' ) ) {
  168. function permalink_by_title($title, $echo=true) {
  169. $id = get_page_by_title( $title );
  170. $link = get_permalink( $id );
  171. if (!$echo)
  172. return $link;
  173. echo $link;
  174. }
  175. }
  176. function isset_or_false( $val ) {
  177. return isset( $val ) ? $val : false;
  178. } // END isset_or_false()
  179. function not_empty_or_false( $val ) {
  180. return !empty( $val ) ? $val : false;
  181. } // END not_empty_or_false()
  182. /*========== Memory Usage / Tracking ==========*/
  183. if( !function_exists( '_mem' ) ) {
  184. function _mem( $input, $color_format = 0 ){
  185. global $mem_usage;
  186. $new_usage = memory_get_peak_usage();
  187. $diff = $new_usage - $mem_usage ;
  188. $mem_usage = memory_get_peak_usage();
  189. /* if( $diff > 0 ) */
  190. $log = "+".( number_format( ($diff/1024) / 1024 , 2) ). " M [ ".( number_format( ($mem_usage/1024) / 1024 , 2) )." M total ] $input ( limit : ".ini_get('memory_limit'). " ) ";
  191. if( $color_format ) {
  192. $log = sprintf( $color_format , $log );
  193. }
  194. _log( $log );
  195. /*
  196. if( $diff > .005 )
  197. _log( "+".( $diff/1024/1024 ). " mb [ ".($mem_usage/1024/1024)." total mb ] $input " );
  198. */
  199. /* if( $diff > 10000 ) _backtrace(); */
  200. }
  201. }
  202. if( !function_exists( '_mem_v' ) ) {
  203. // Alias of _mem that only echo's with --verbose flag on
  204. function _mem_v( $input ){
  205. global $color_formats;
  206. $verbose = getopt( "", array('verbose') );
  207. if( $verbose ) {
  208. _mem( $input, $color_formats['cyan'] );
  209. }
  210. } // end _mem_v
  211. } // end fn exists _mem_v
  212. if( !function_exists( '_memstart' ) ) {
  213. function _memstart() {
  214. if( !ENABLE_MEM )
  215. return;
  216. _log( 'START MEMORY PROFILE' );
  217. $trace = debug_backtrace();
  218. _mem( $trace[1]['function'] );
  219. }
  220. function _memend() {
  221. if( !ENABLE_MEM )
  222. return;
  223. $trace = debug_backtrace();
  224. _mem( $trace[1]['function'] );
  225. _log( 'END MEMORY PROFILE' );
  226. }
  227. }
  228. /*========== END : Memory Usage / Tracking ==========*/
  229. global $color_formats;
  230. $color_formats = array(
  231. // styles
  232. // italic and blink may not work depending of your terminal
  233. 'bold' => "\033[1m%s\033[0m",
  234. 'dark' => "\033[2m%s\033[0m",
  235. 'italic' => "\033[3m%s\033[0m",
  236. 'underline' => "\033[4m%s\033[0m",
  237. 'blink' => "\033[5m%s\033[0m",
  238. 'reverse' => "\033[7m%s\033[0m",
  239. 'concealed' => "\033[8m%s\033[0m",
  240. // foreground colors
  241. 'black' => "\033[30m%s\033[0m",
  242. 'red' => "\033[31m%s\033[0m",
  243. 'green' => "\033[32m%s\033[0m",
  244. 'yellow' => "\033[33m%s\033[0m",
  245. 'blue' => "\033[34m%s\033[0m",
  246. 'magenta' => "\033[35m%s\033[0m",
  247. 'cyan' => "\033[36m%s\033[0m",
  248. 'white' => "\033[37m%s\033[0m",
  249. // background colors
  250. 'bg_black' => "\033[40m%s\033[0m",
  251. 'bg_red' => "\033[41m%s\033[0m",
  252. 'bg_green' => "\033[42m%s\033[0m",
  253. 'bg_yellow' => "\033[43m%s\033[0m",
  254. 'bg_blue' => "\033[44m%s\033[0m",
  255. 'bg_magenta' => "\033[45m%s\033[0m",
  256. 'bg_cyan' => "\033[46m%s\033[0m",
  257. 'bg_white' => "\033[47m%s\033[0m",
  258. );
  259. ?>