NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

php-console-log.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /*
  3. Plugin Name: PHP Console Log
  4. Plugin URI: https://marcusviar.com/php-console-log
  5. Description: Log PHP variables and arrays to the web console in your browser via JavaScript's console.log(). No browser extensions required.
  6. Version: 1.0.1
  7. Contributors: marcusviar
  8. Author: Marcus Viar
  9. Author URI: https://profiles.wordpress.org/marcusviar/
  10. License: GPLv2 or later
  11. License URI: https://www.gnu.org/licenses/gpl-2.0.html
  12. Text Domain: php-console-log
  13. Domain Path: /languages/
  14. */
  15. // If this file is called directly, abort.
  16. if ( !defined( 'WPINC' ) ) {
  17. die;
  18. }
  19. /**
  20. * @info Define constants for use throughout the plugin.
  21. *
  22. * @since 1.0.0
  23. *
  24. * @var string PHP_CONSOLE_LOG_PLUGIN_BASENAME Plugin folder and file name.
  25. * @var string PHP_CONSOLE_LOG_PLUGIN_DIR_PATH Absolute path to plugin directory.
  26. * @var string PHP_CONSOLE_LOG_PLUGIN_DIR_NAME This plugins directory name.
  27. * @var string PHP_CONSOLE_LOG_WP_PLUGIN_DIR_PATH Absolute path to WP plugin directory.
  28. * @var string PHP_CONSOLE_LOG_PLUGINS_URL URL to WordPress plugins directory.
  29. * @var string PHP_CONSOLE_LOG_PLUGIN_DIR_URL Url to PHP_CONSOLE_LOG plugin directory.
  30. * @var string PHP_CONSOLE_LOG_PLUGIN_PAGE_LINK_FILTER Builds filter name for links on plugin page.
  31. * @var string PHP_CONSOLE_LOG_VERSION The version number for this plugin.
  32. */
  33. define( 'PHP_CONSOLE_LOG_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
  34. define( 'PHP_CONSOLE_LOG_PLUGIN_DIR_PATH', plugin_dir_path( __FILE__ ) );
  35. define( 'PHP_CONSOLE_LOG_PLUGIN_DIR_NAME', dirname( PHP_CONSOLE_LOG_PLUGIN_BASENAME ) );
  36. define( 'PHP_CONSOLE_LOG_WP_PLUGIN_DIR_PATH', plugin_dir_path( __DIR__ ) );
  37. define( 'PHP_CONSOLE_LOG_PLUGINS_URL', plugins_url( __FILE__ ) );
  38. define( 'PHP_CONSOLE_LOG_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) );
  39. define( 'PHP_CONSOLE_LOG_PLUGIN_PAGE_LINK_FILTER', "plugin_action_links_" . PHP_CONSOLE_LOG_PLUGIN_BASENAME );
  40. define( 'PHP_CONSOLE_LOG_VERSION', '1.0.1' );
  41. /**
  42. * @info Build menu.
  43. * @since 1.0.0
  44. */
  45. include 'includes/php-console-log-menu.php';
  46. /**
  47. * @info Load styles.
  48. * @since 1.0.0
  49. */
  50. include 'includes/php-console-log-styles.php';
  51. /**
  52. * @info Load scripts.
  53. * @since 1.0.0
  54. */
  55. include 'includes/php-console-log-scripts.php';
  56. /**
  57. * @info Build pages.
  58. * @since 1.0.0
  59. */
  60. include 'pages/php-console-log-page-help.php';
  61. /**
  62. * @info Global array that will be logged to the web Console.
  63. * @since 1.0.0
  64. */
  65. $php_console_log_array = array();
  66. /**
  67. * @func php_console_log_load_plugin_textdomain()
  68. *
  69. * Load textdomain to allow translations for this plugin.
  70. *
  71. * @since 1.0.0
  72. *
  73. */
  74. function php_console_log_load_plugin_textdomain() {
  75. load_plugin_textdomain( 'php-console-log', false, PHP_CONSOLE_LOG_PLUGIN_DIR_NAME . '/languages/' );
  76. }
  77. add_action( 'plugins_loaded', 'php_console_log_load_plugin_textdomain' );
  78. /**
  79. * @func php_console_log()
  80. *
  81. * Function the end user places in any plugin PHP file. Accepts any
  82. * number of arguments that will be logged to the web Console (your browser's
  83. * console) via JavaScript's console.log().
  84. *
  85. * @since 1.0.0
  86. *
  87. * @param string/array PHP strings or arrays to be logged to web console via
  88. * php-console-log.js.
  89. */
  90. function php_console_log() {
  91. // uses global so php_console_log() can be used multiple times and not
  92. // overwrite itself
  93. global $php_console_log_array;
  94. // number of arguments passed to this function
  95. $num_args = func_num_args();
  96. if ( $num_args > 0 ) {
  97. // array of arguments passed to this function
  98. $arg_list = func_get_args();
  99. // set backtrace key based on WordPress version number
  100. global $wp_version;
  101. $backtrack_key = 3;
  102. if ( $wp_version < 4.7 ) {
  103. $backtrack_key = 1;
  104. }
  105. // backtrace args key is always 0
  106. $backtrack_args_key = 0;
  107. foreach ( $arg_list as $arg_value ) {
  108. // build backtrace info
  109. $backtrace = debug_backtrace();
  110. // path to file do_action( 'php_console_log' ) was used in
  111. $backtrace_array['file'] = $backtrace[$backtrack_key]['file'];
  112. // line number do_action( 'php_console_log' ) was used on
  113. $backtrace_array['line'] = $backtrace[$backtrack_key]['line'];
  114. // args passed in with do_action( 'php_console_log' )
  115. $backtrace_array['args'] = $backtrace[$backtrack_args_key]['args'];
  116. // add each argument to the global $php_console_log_array
  117. $php_console_log_array[] = $backtrace_array;
  118. }
  119. }
  120. }
  121. /**
  122. * @info Custom action that is called by end user to log info to web console.
  123. * @since 1.0.0
  124. */
  125. add_action( 'php_console_log', 'php_console_log' );
  126. /**
  127. * @func php_console_log_load_first()
  128. *
  129. * Make sure PHP Console Log plugin is loaded first so
  130. * do_action('php_console_log' ) can be used in other plugins. Runs when a
  131. * plugin is activated and deactivated. Option "active_plugins" order in the
  132. * options table of the database can be changed by other plugins after this
  133. * runs. Any plugin that loads before PHP Console Log will not be able to use
  134. * this plugin's features.
  135. *
  136. * @since 1.0.0
  137. *
  138. */
  139. function php_console_log_load_first() {
  140. // get active_plugins row from options table
  141. if ( $plugins = get_option( 'active_plugins' ) ) {
  142. // if PHP Console Log plugin is not the first element in active_plugins array
  143. if ( $key = array_search( PHP_CONSOLE_LOG_PLUGIN_BASENAME, $plugins ) ) {
  144. // make PHP Console Log plugin first in active_plugins array
  145. array_splice( $plugins, $key, 1 );
  146. array_unshift( $plugins, PHP_CONSOLE_LOG_PLUGIN_BASENAME );
  147. // update active_plugins row in options table
  148. update_option( 'active_plugins', $plugins );
  149. }
  150. }
  151. }
  152. /**
  153. * @info Set PHP Console Log as first plugin to load when any plugin is
  154. * activated or deactivated.
  155. * @since 1.0.0
  156. */
  157. add_action( 'activated_plugin', 'php_console_log_load_first' );
  158. add_action( 'deactivated_plugin', 'php_console_log_load_first' );