An updated theme (based on Atticus Finch) for ClassicPress
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.2 KiB

3 years ago
  1. <?php
  2. if ( !class_exists('Puc_v4p1_DebugBar_Extension', false) ):
  3. class Puc_v4p1_DebugBar_Extension {
  4. /** @var Puc_v4p1_UpdateChecker */
  5. protected $updateChecker;
  6. protected $panelClass = 'Puc_v4p1_DebugBar_Panel';
  7. public function __construct($updateChecker, $panelClass = null) {
  8. $this->updateChecker = $updateChecker;
  9. if ( isset($panelClass) ) {
  10. $this->panelClass = $panelClass;
  11. }
  12. add_filter('debug_bar_panels', array($this, 'addDebugBarPanel'));
  13. add_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies'));
  14. add_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow'));
  15. }
  16. /**
  17. * Register the PUC Debug Bar panel.
  18. *
  19. * @param array $panels
  20. * @return array
  21. */
  22. public function addDebugBarPanel($panels) {
  23. if ( $this->updateChecker->userCanInstallUpdates() ) {
  24. $panels[] = new $this->panelClass($this->updateChecker);
  25. }
  26. return $panels;
  27. }
  28. /**
  29. * Enqueue our Debug Bar scripts and styles.
  30. */
  31. public function enqueuePanelDependencies() {
  32. wp_enqueue_style(
  33. 'puc-debug-bar-style-v4',
  34. $this->getLibraryUrl("/css/puc-debug-bar.css"),
  35. array('debug-bar'),
  36. '20161217'
  37. );
  38. wp_enqueue_script(
  39. 'puc-debug-bar-js-v4',
  40. $this->getLibraryUrl("/js/debug-bar.js"),
  41. array('jquery'),
  42. '20170516'
  43. );
  44. }
  45. /**
  46. * Run an update check and output the result. Useful for making sure that
  47. * the update checking process works as expected.
  48. */
  49. public function ajaxCheckNow() {
  50. if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) {
  51. return;
  52. }
  53. $this->preAjaxReqest();
  54. $update = $this->updateChecker->checkForUpdates();
  55. if ( $update !== null ) {
  56. echo "An update is available:";
  57. echo '<pre>', htmlentities(print_r($update, true)), '</pre>';
  58. } else {
  59. echo 'No updates found.';
  60. }
  61. exit;
  62. }
  63. /**
  64. * Check access permissions and enable error display (for debugging).
  65. */
  66. protected function preAjaxReqest() {
  67. if ( !$this->updateChecker->userCanInstallUpdates() ) {
  68. die('Access denied');
  69. }
  70. check_ajax_referer('puc-ajax');
  71. error_reporting(E_ALL);
  72. @ini_set('display_errors','On');
  73. }
  74. /**
  75. * @param string $filePath
  76. * @return string
  77. */
  78. private function getLibraryUrl($filePath) {
  79. $absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/'));
  80. //Where is the library located inside the WordPress directory structure?
  81. $absolutePath = wp_normalize_path($absolutePath);
  82. $pluginDir = wp_normalize_path(WP_PLUGIN_DIR);
  83. $muPluginDir = wp_normalize_path(WPMU_PLUGIN_DIR);
  84. $themeDir = wp_normalize_path(get_theme_root());
  85. if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) {
  86. //It's part of a plugin.
  87. return plugins_url(basename($absolutePath), $absolutePath);
  88. } else if ( strpos($absolutePath, $themeDir) === 0 ) {
  89. //It's part of a theme.
  90. $relativePath = substr($absolutePath, strlen($themeDir) + 1);
  91. $template = substr($relativePath, 0, strpos($relativePath, '/'));
  92. $baseUrl = get_theme_root_uri($template);
  93. if ( !empty($baseUrl) && $relativePath ) {
  94. return $baseUrl . '/' . $relativePath;
  95. }
  96. }
  97. return '';
  98. }
  99. }
  100. endif;