C_Photocrati_Resource_Manager

The NextGEN Gallery C Photocrati Resource Manager class.

Defined (1)

The class is defined in the following location(s).

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