/wp-content/plugins/nextgen-gallery/non_pope/class.photocrati_resource_manager.php

https://bitbucket.org/kenaku/karate · PHP · 199 lines · 122 code · 28 blank · 49 comment · 32 complexity · fc74d711758a5e2de2762a3286d1b6f2 MD5 · raw file

  1. <?php
  2. class C_Photocrati_Resource_Manager
  3. {
  4. static $instance = NULL;
  5. var $buffer = '';
  6. var $styles = '';
  7. var $scripts = '';
  8. var $other_output = '';
  9. var $wrote_footer = FALSE;
  10. var $run_shutdown = FALSE;
  11. var $valid_request = TRUE;
  12. /**
  13. * Start buffering all generated output. We'll then do two things with the buffer
  14. * 1) Find stylesheets lately enqueued and move them to the header
  15. * 2) Ensure that wp_print_footer_scripts() is called
  16. */
  17. function __construct()
  18. {
  19. // Validate the request
  20. $this->validate_request();
  21. add_action('init',array(&$this, 'start_buffer'), 1);
  22. }
  23. /**
  24. * Determines if the resource manager should perform it's routines for this request
  25. * @return bool
  26. */
  27. function validate_request()
  28. {
  29. $retval = TRUE;
  30. if (is_admin()) {
  31. if (isset($_REQUEST['page']) && !preg_match("#^(ngg|nextgen)#", $_REQUEST['page'])) $retval = FALSE;
  32. }
  33. if (strpos($_SERVER['REQUEST_URI'], 'wp-admin/update') !== FALSE) $retval = FALSE;
  34. else if (isset($_GET['display_gallery_iframe'])) $retval = FALSE;
  35. else if (defined('WP_ADMIN') && WP_ADMIN && defined('DOING_AJAX') && DOING_AJAX) $retval = FALSE;
  36. else if (preg_match("/(js|css|xsl|xml|kml)$/", $_SERVER['REQUEST_URI'])) $retval = FALSE;
  37. elseif (preg_match("/\\.(\\w{3,4})$/", $_SERVER['REQUEST_URI'], $match)) {
  38. if (!in_array($match[1], array('htm', 'html', 'php'))) {
  39. $retval = FALSE;
  40. }
  41. }
  42. $this->valid_request = $retval;
  43. }
  44. /**
  45. * Start the output buffers
  46. */
  47. function start_buffer()
  48. {
  49. if (apply_filters('run_ngg_resource_manager', $this->valid_request)) {
  50. ob_start(array(&$this, 'output_buffer_handler'));
  51. ob_start(array(&$this, 'get_buffer'));
  52. add_action('wp_print_footer_scripts', array(&$this, 'get_resources'), 1);
  53. add_action('admin_print_footer_scripts', array(&$this, 'get_resources'), 1);
  54. add_action('shutdown', array(&$this, 'shutdown'));
  55. }
  56. }
  57. /**
  58. *
  59. **/
  60. function get_resources()
  61. {
  62. ob_start();
  63. wp_print_styles();
  64. print_admin_styles();
  65. $this->styles = ob_get_clean();
  66. if (!is_admin()) {
  67. ob_start();
  68. wp_print_scripts();
  69. $this->scripts = ob_get_clean();
  70. }
  71. $this->wrote_footer = TRUE;
  72. }
  73. /**
  74. * Output the buffer after PHP execution has ended (but before shutdown)
  75. * @param $content
  76. * @return string
  77. */
  78. function output_buffer_handler($content)
  79. {
  80. return $this->output_buffer();
  81. }
  82. /**
  83. * Removes the closing </html> tag from the output buffer. We'll then write our own closing tag
  84. * in the shutdown function after running wp_print_footer_scripts()
  85. * @param $content
  86. * @return mixed
  87. */
  88. function get_buffer($content)
  89. {
  90. $this->buffer = $content;
  91. return '';
  92. }
  93. /**
  94. * Moves resources to their appropriate place
  95. */
  96. function move_resources()
  97. {
  98. if ($this->valid_request) {
  99. // Move stylesheets to head
  100. if ($this->styles) {
  101. $this->buffer = str_ireplace('</head>', $this->styles.'</head>', $this->buffer);
  102. }
  103. // Move the scripts to the bottom of the page
  104. if ($this->scripts) {
  105. $this->buffer = str_ireplace('</body>', $this->scripts.'</body>', $this->buffer);
  106. }
  107. if ($this->other_output) {
  108. $this->buffer = str_replace('</body>', $this->other_output.'</body>', $this->buffer);
  109. }
  110. }
  111. }
  112. /**
  113. * When PHP has finished, we output the footer scripts and closing tags
  114. */
  115. function output_buffer($in_shutdown=FALSE)
  116. {
  117. // If the footer scripts haven't been outputted, then
  118. // we need to take action - as they're required
  119. if (!$this->wrote_footer) {
  120. // If W3TC is installed and activated, we can't output the
  121. // scripts and manipulate the buffer, so we can only provide a warning
  122. if (defined('W3TC') && defined('WP_DEBUG') && WP_DEBUG) {
  123. if (defined('DONOTCACHEPAGE')) define('DONOTCACHEPAGE', TRUE);
  124. if (!did_action('wp_footer')) {
  125. error_log("We're sorry, but your theme's page template didn't make a call to wp_footer(), which is required by NextGEN Gallery. Please add this call to your page templates.");
  126. }
  127. else {
  128. error_log("We're sorry, but your theme's page template didn't make a call to wp_print_footer_scripts(), which is required by NextGEN Gallery. Please add this call to your page templates.");
  129. }
  130. }
  131. // We don't want to manipulate the buffer if it doesn't contain HTML
  132. elseif (strpos($this->buffer, '</body>') === FALSE) {
  133. $this->valid_request = FALSE;
  134. }
  135. // The output_buffer() function has been called in the PHP shutdown callback
  136. // This will allow us to print the scripts ourselves and manipulate the buffer
  137. if ($in_shutdown === TRUE) {
  138. ob_start();
  139. if (!did_action('wp_footer')) {
  140. wp_footer();
  141. }
  142. else {
  143. wp_print_footer_scripts();
  144. }
  145. $this->other_output = ob_get_clean();
  146. }
  147. // W3TC isn't activated and we're not in the shutdown callback.
  148. // We'll therefore add a shutdown callback to print the scripts
  149. else {
  150. $this->run_shutdown = TRUE;
  151. return '';
  152. }
  153. }
  154. // Once we have the footer scripts, we can modify the buffer and
  155. // move the resources around
  156. if ($this->wrote_footer) $this->move_resources();
  157. return $this->buffer;
  158. }
  159. /**
  160. * PHP shutdown callback. Manipulate and output the buffer
  161. */
  162. function shutdown()
  163. {
  164. if ($this->run_shutdown) echo $this->output_buffer(TRUE);
  165. }
  166. static function init()
  167. {
  168. $klass = get_class();
  169. return self::$instance = new $klass;
  170. }
  171. }