C_NextGen_Style_Manager

The NextGEN Gallery C NextGen Style Manager class.

Defined (1)

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

/non_pope/class.nextgen_style_manager.php  
  1. class C_NextGen_Style_Manager 
  2. static $_instance = NULL; 
  3. var $directories = array(); 
  4. var $unsafe_directories = array(); 
  5. var $default_dir = ''; 
  6. var $new_dir = ''; 
  7.  
  8. function __construct() 
  9. $this->default_dir = implode(DIRECTORY_SEPARATOR, array( 
  10. NGG_MODULE_DIR,  
  11. 'ngglegacy',  
  12. 'css' 
  13. )); 
  14.  
  15. $this->new_dir = implode(DIRECTORY_SEPARATOR, array( 
  16. rtrim(WP_CONTENT_DIR, "/\\"),  
  17. 'ngg_styles' 
  18. )); 
  19.  
  20. // The last place we look for a stylesheet is in ngglegacy 
  21. $this->add_directory($this->default_dir); 
  22.  
  23. // This is where all stylesheets should be stored 
  24. $this->add_directory($this->new_dir); 
  25.  
  26. // We also check wp-content/ngg/styles 
  27. $this->add_directory(implode(DIRECTORY_SEPARATOR, array( 
  28. WP_CONTENT_DIR, 'ngg', 'styles' 
  29. ))); 
  30.  
  31. // We check the parent theme directory. Needed for child themes 
  32. $this->add_directory(rtrim(get_template_directory(), "/\\"), TRUE); 
  33.  
  34. // We also check parent_theme/nggallery 
  35. $this->add_directory(implode(DIRECTORY_SEPARATOR, array( 
  36. rtrim(get_template_directory(), "/\\"),  
  37. 'nggallery' 
  38. )), TRUE); 
  39.  
  40. // We also check parent_theme/ngg_styles 
  41. $this->add_directory(implode(DIRECTORY_SEPARATOR, array( 
  42. rtrim(get_template_directory(), "/\\"),  
  43. 'ngg_styles' 
  44. )), TRUE); 
  45.  
  46. // We check the root directory of the theme. Users shouldn't store here,  
  47. // but they might 
  48. $this->add_directory(rtrim(get_stylesheet_directory(), "/\\"), TRUE); 
  49.  
  50. // We also check the theme/nggallery directory 
  51. $this->add_directory(implode(DIRECTORY_SEPARATOR, array( 
  52. rtrim(get_stylesheet_directory(), "/\\"),  
  53. 'nggallery' 
  54. )), TRUE); 
  55.  
  56. // We also check the theme/ngg_styles directory 
  57. $this->add_directory(implode(DIRECTORY_SEPARATOR, array( 
  58. rtrim(get_stylesheet_directory(), "/\\"),  
  59. 'ngg_styles' 
  60. )), TRUE); 
  61.  
  62. /** 
  63. * Add a directory to search for stylesheets 
  64. * @param $dir 
  65. * @param bool $unsafe 
  66. */ 
  67. function add_directory($dir, $unsafe=FALSE) 
  68. array_unshift($this->directories, $dir); 
  69. if ($unsafe) { 
  70. $this->unsafe_directories[] = $dir; 
  71.  
  72. /** 
  73. * Determines if a directory is upgrade-safe or not 
  74. * @param $dir 
  75. * @return bool 
  76. */ 
  77. function is_directory_unsafe($dir=FALSE) 
  78. if (!$dir) $dir = dirname($this->find_selected_stylesheet_abspath()); 
  79.  
  80. return in_array($dir, $this->unsafe_directories); 
  81.  
  82. /** 
  83. * Determines if the directory is the default ngglegacy path 
  84. * @param $dir 
  85. * @return bool 
  86. */ 
  87. function is_default_dir($dir) 
  88. return rtrim($dir, "/\\") == $this->default_dir; 
  89.  
  90. function get_new_dir($filename) 
  91. return implode(DIRECTORY_SEPARATOR, array( 
  92. rtrim($this->new_dir, "/\\"),  
  93. $filename 
  94. )); 
  95.  
  96. /** 
  97. * Gets the location where the selected stylesheet will be saved to 
  98. * @param bool|string $selected 
  99. * @return string 
  100. */ 
  101. function get_selected_stylesheet_saved_abspath($selected=FALSE) 
  102. if (!$selected) $selected = $this->get_selected_stylesheet(); 
  103.  
  104. $abspath = $this->find_selected_stylesheet_abspath($selected); 
  105. if ($this->is_default_dir(dirname($abspath))) { 
  106. $abspath = $this->get_new_dir(basename($abspath)); 
  107.  
  108. return $abspath; 
  109.  
  110. function save($contents, $selected=FALSE) 
  111. $retval = FALSE; 
  112.  
  113. if (!$selected) $selected = $this->get_selected_stylesheet(); 
  114. $abspath = $this->get_selected_stylesheet_saved_abspath($selected); 
  115.  
  116. wp_mkdir_p(dirname($abspath)); 
  117. if (is_writable($abspath) OR (!@file_exists($abspath) && is_writable(dirname($abspath)))) { 
  118. $retval = file_put_contents($abspath, $contents); 
  119. return $retval; 
  120.  
  121. /** 
  122. * Gets the selected stylesheet from the user 
  123. * @return mixed 
  124. */ 
  125. function get_selected_stylesheet() 
  126. $settings = C_NextGen_Settings::get_instance(); 
  127.  
  128. // use the same css resource for all subsites when wpmuStyle=true 
  129. if (!is_multisite() || (is_multisite() && $settings->get('wpmuStyle'))) 
  130. return $settings->get('CSSfile', 'nggallery.css'); 
  131. else 
  132. return C_Nextgen_Global_Settings::get_instance()->get('wpmuCSSfile'); 
  133.  
  134.  
  135. /** 
  136. * Finds the location of the selected stylesheet 
  137. */ 
  138. function find_selected_stylesheet_abspath($selected=FALSE) 
  139. if (!$selected) $selected = $this->get_selected_stylesheet(); 
  140.  
  141. $retval = implode(DIRECTORY_SEPARATOR, array( 
  142. rtrim($this->default_dir, "/\\"),  
  143. $selected 
  144. )); 
  145.  
  146. foreach ($this->directories as $dir) { 
  147. $path = implode(DIRECTORY_SEPARATOR, array( 
  148. rtrim($dir, "/\\"),  
  149. $selected 
  150. )); 
  151.  
  152. if (@file_exists($path)) { 
  153. $retval = $path; 
  154. break; 
  155.  
  156. $retval = str_replace('/', DIRECTORY_SEPARATOR, $retval); 
  157.  
  158. return $retval; 
  159.  
  160. /** 
  161. * Returns the url to the selected stylesheet 
  162. * @return mixed 
  163. */ 
  164. function get_selected_stylesheet_url($selected=FALSE) 
  165. if (!$selected) 
  166. $selected = $this->get_selected_stylesheet(); 
  167. $abspath = $this->find_selected_stylesheet_abspath($selected); 
  168.  
  169. // default_dir is the only resource loaded from inside the plugin directory 
  170. $type = 'content'; 
  171. $url = content_url(); 
  172. if (0 === strpos($abspath, $this->default_dir)) 
  173. $type = 'plugins'; 
  174. $url = plugins_url(); 
  175.  
  176. $retval = str_replace( 
  177. C_Fs::get_instance()->get_document_root($type),  
  178. $url,  
  179. $this->find_selected_stylesheet_abspath($selected) 
  180. ); 
  181.  
  182. return rtrim(str_replace('\\', '/', $retval), "/"); 
  183.  
  184.  
  185. function find_all_stylesheets($dir = FALSE) 
  186. $retval = array(); 
  187. if (!$dir) 
  188. $dir = $this->directories; 
  189.  
  190. foreach (array_reverse($dir) as $dir) { 
  191. $path = implode(DIRECTORY_SEPARATOR, array( 
  192. rtrim($dir, "/\\"),  
  193. '*.css' 
  194. )); 
  195. $files = glob($path); 
  196. if (is_array($files)) foreach ($files as $abspath) { 
  197. if (($meta = $this->get_stylesheet_metadata($abspath))) { 
  198. $filename = $meta['filename']; 
  199. $retval[$filename] = $meta; 
  200.  
  201. return $retval; 
  202.  
  203. /** 
  204. * Gets the metadata for a particular stylesheet 
  205. * @param $abspath 
  206. * @return array 
  207. */ 
  208. function get_stylesheet_metadata($abspath) 
  209. $retval = array(); 
  210. $contents = file_get_contents($abspath); 
  211. $name = ''; 
  212. $desc = ''; 
  213. $version = ''; 
  214. $author = ''; 
  215.  
  216. // Find the name of the stylesheet 
  217. if (preg_match("/CSS Name:(.*)/i", $contents, $match)) { 
  218. $name = trim($match[1]); 
  219.  
  220. // Find the description of the stylesheet 
  221. if (preg_match("/Description:(.*)/", $contents, $match)) { 
  222. $desc = trim($match[1]); 
  223.  
  224. // Find the author of the stylesheet 
  225. if (preg_match("/Author:(.*)/", $contents, $match)) { 
  226. $author = trim($match[1]); 
  227.  
  228. // Find the version of the stylesheet 
  229. if (preg_match("/Version:(.*)/", $contents, $match)) { 
  230. $version = trim($match[1]); 
  231.  
  232. if ($name) { 
  233. $retval = array( 
  234. 'filename' => basename($abspath),  
  235. 'abspath' => $abspath,  
  236. 'name' => $name,  
  237. 'description' => $desc,  
  238. 'author' => $author,  
  239. 'version' => $version 
  240. ); 
  241.  
  242. return $retval; 
  243.  
  244. /** 
  245. * Gets an instance of the class 
  246. * @return C_NextGen_Style_Manager 
  247. */ 
  248. static function get_instance() 
  249. if (is_null(self::$_instance)) { 
  250. $klass = get_class(); 
  251. self::$_instance = new $klass(); 
  252. return self::$_instance;