NGG_Style

The NextCellent Gallery NGG Style class.

Defined (1)

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

/admin/class-ngg-style.php  
  1. class NGG_Style extends NGG_Post_Admin_Page { 
  2.  
  3. /** 
  4. * Find stylesheets. 
  5. * @since 1.9.22 
  6. *  
  7. * @param array $directions Absolute paths to the folders that contain stylesheets. 
  8. *@return array Absolute paths to the stylesheets. 
  9. */ 
  10. static function ngg_get_cssfiles( $directions ) { 
  11.  
  12. $plugin_files = array (); 
  13.  
  14. foreach ($directions as $direction) { 
  15. $plugins_dir = dir($direction); 
  16. if ($plugins_dir) { 
  17. while (($file = $plugins_dir->read()) !== false) { 
  18. if (preg_match('|^\.+$|', $file)) 
  19. {continue;} 
  20. if (is_dir($direction.'/'.$file)) { 
  21. $plugins_subdir = dir($direction.'/'.$file); 
  22. if ($plugins_subdir) { 
  23. while (($subfile = $plugins_subdir->read()) !== false) { 
  24. if (preg_match('|^\.+$|', $subfile)) 
  25. {continue;} 
  26. if (preg_match('|\.css$|', $subfile)) 
  27. {$plugin_files[] = "$direction/$file/$subfile";} 
  28. } else { 
  29. if (preg_match('|\.css$|', $file)) 
  30. {$plugin_files[] = $direction . '/' . $file;} 
  31.  
  32. return $plugin_files; 
  33.  
  34. /** 
  35. * Parse stylesheet information. 
  36. * @since 1.9.22 
  37. *  
  38. * @param string $plugin_file Absolute path to the stylesheet. 
  39. *@return array The information about the stylesheet. 
  40. */ 
  41. static function ngg_get_cssfiles_data($plugin_file) { 
  42.  
  43. $css_data = implode('', file($plugin_file)); 
  44. $folder = basename(dirname($plugin_file)); 
  45.  
  46. preg_match("|CSS Name:(.*)|i", $css_data, $plugin_name); 
  47. preg_match("|Description:(.*)|i", $css_data, $description); 
  48. preg_match("|Author:(.*)|i", $css_data, $author_name); 
  49.  
  50. if (preg_match("|Version:(.*)|i", $css_data, $version)) 
  51. {$version = trim($version[1]);} 
  52. else 
  53. {$version = '';} 
  54.  
  55. $description = wptexturize(trim($description[1])); 
  56.  
  57. $name = trim($plugin_name[1]); 
  58. $author = trim($author_name[1]); 
  59.  
  60. return array ('Name' => $name, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Folder' => $folder ); 
  61.  
  62. /** 
  63. * Output a set of options for a select element. 
  64. * @since 1.9.26 
  65. * @param array $css_list The paths to the stylesheets. 
  66. * @param string $act_css_file The path to the current active stylesheet. 
  67. */ 
  68. static function output_css_files_dropdown( $css_list, $act_css_file ) { 
  69. foreach ( $css_list as $file) { 
  70. $a_cssfile = NGG_Style::ngg_get_cssfiles_data($file); 
  71. $css_name = esc_attr( $a_cssfile['Name'] ); 
  72. $css_folder = esc_attr( $a_cssfile['Folder'] ); 
  73. if ( $css_name != '' ) { 
  74. echo '<option value="' . $file . '" ' . selected( $file, $act_css_file ) . '>' . $css_name . ' (' . $css_folder . ')</option>'; 
  75.  
  76. /** 
  77. * Save, change and move the css files and options. 
  78. * @since 1.9.22 
  79. */ 
  80. protected function processor() { 
  81. global $ngg; 
  82. $i = 0; 
  83.  
  84. if ( isset( $_POST['activate'] ) ) { 
  85. check_admin_referer('ngg_style'); 
  86. $file = $_POST['css']; 
  87. $activate = $_POST['activateCSS'];  
  88.  
  89. // save option now 
  90. $ngg->options['activateCSS'] = $activate; 
  91. $ngg->options['CSSfile'] = $file; 
  92. update_option('ngg_options', $ngg->options); 
  93.  
  94. if ( isset($activate) ) { 
  95. nggGallery::show_message(__('Successfully selected CSS file.', 'nggallery') ); 
  96. } else { 
  97. nggGallery::show_message(__('No CSS file will be used.', 'nggallery') ); 
  98.  
  99. if (isset($_POST['updatecss'])) { 
  100.  
  101. check_admin_referer('ngg_style'); 
  102.  
  103. if ( !current_user_can('edit_themes') ) 
  104. {wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this blog.').'</p>');} 
  105.  
  106. $newcontent = stripslashes($_POST['newcontent']); 
  107. $old_path = $_POST['file']; 
  108. $folder = $_POST['folder']; 
  109.  
  110. //if the file is in the css folder, copy it. 
  111. if ($folder === 'css') { 
  112. $filename = basename ($old_path, '.css'); 
  113. $new_path = NGG_CONTENT_DIR . "/ngg_styles/" . $filename . ".css"; 
  114. //check for duplicate files 
  115. while ( file_exists( $new_path ) ) { 
  116. $i++; 
  117. $new_path = NGG_CONTENT_DIR . "/ngg_styles/" . $filename . "-" . $i . ".css"; 
  118. //check if ngg_styles exist or not 
  119. if ( !file_exists(NGG_CONTENT_DIR . "/ngg_styles") ) { 
  120. wp_mkdir_p( NGG_CONTENT_DIR . "/ngg_styles" ); 
  121. //copy the file 
  122. if ( copy($old_path, $new_path) ) { 
  123. //set option to new file 
  124. $ngg->options['CSSfile'] = $new_path; 
  125. update_option('ngg_options', $ngg->options); 
  126. } else { 
  127. nggGallery::show_error(__('Could not move file.', 'nggallery')); 
  128. return; 
  129.  
  130. if ( file_put_contents($old_path, $newcontent) ) { 
  131. nggGallery::show_message(__('CSS file successfully updated.', 'nggallery')); 
  132. } else { 
  133. nggGallery::show_error(__('Could not save file.', 'nggallery')); 
  134.  
  135. if (isset($_POST['movecss'])) { 
  136.  
  137. if ( !current_user_can('edit_themes') ) 
  138. {wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this blog.').'</p>');} 
  139.  
  140. $old_path = $_POST['oldpath']; 
  141. $new_path = NGG_CONTENT_DIR . "/ngg_styles/nggallery.css"; 
  142.  
  143. //check for duplicate files 
  144. while ( file_exists( $new_path ) ) { 
  145. $i++; 
  146. $new_path = NGG_CONTENT_DIR . "/ngg_styles/nggallery-" . $i . ".css"; 
  147.  
  148. //move file 
  149. if ( rename( $old_path, $new_path) ) { 
  150. nggGallery::show_message(__('CSS file successfully moved.', 'nggallery')); 
  151. //set option to new file 
  152. $ngg->options['CSSfile'] = $new_path; 
  153. update_option('ngg_options', $ngg->options); 
  154. } else { 
  155. nggGallery::show_error(__('Could not move the CSS file.', 'nggallery')); 
  156.  
  157. /** 
  158. * Render the page content. 
  159. * @since 1.9.22 
  160. */ 
  161. public function display() { 
  162.  
  163. parent::display(); 
  164.  
  165. global $ngg; 
  166.  
  167. //the directions containing the css files 
  168. if ( file_exists(NGG_CONTENT_DIR . "/ngg_styles") ) { 
  169. $dir = array(NGGALLERY_ABSPATH . "css", NGG_CONTENT_DIR . "/ngg_styles"); 
  170. } else { 
  171. $dir = array(NGGALLERY_ABSPATH . "css"); 
  172.  
  173. //support for legacy location (in theme folder) 
  174. if ( $theme_css_exists = file_exists (get_stylesheet_directory() . "/nggallery.css") ) { 
  175. $act_cssfile = get_stylesheet_directory() . "/nggallery.css"; 
  176.  
  177. //if someone uses the filter, don't display this page. 
  178. if ( !$theme_css_exists && $set_css_file = nggGallery::get_theme_css_file() ) { 
  179. nggGallery::show_error( __('Your CSS file is set by a theme or another plugin.', 'nggallery') . "<br><br>" . __('This CSS file will be applied:', 'nggallery') . "<br>" . $set_css_file); 
  180. return; 
  181.  
  182. //load all files 
  183. if ( !isset($act_cssfile) ) { 
  184. $csslist = NGG_Style::ngg_get_cssfiles($dir); 
  185. $act_cssfile = $ngg->options['CSSfile']; 
  186.  
  187. //get the data from the file 
  188. $act_css_data = NGG_Style::ngg_get_cssfiles_data($act_cssfile); 
  189. $act_css_name = $act_css_data['Name']; 
  190. $act_css_description = $act_css_data['Description']; 
  191. $act_css_author = $act_css_data['Author']; 
  192. $act_css_version = $act_css_data['Version']; 
  193. $act_css_folder = $act_css_data['Folder']; 
  194.  
  195.  
  196. // get the content of the file 
  197. $error = ( !is_file($act_cssfile) ); 
  198.  
  199. if (!$error && filesize($act_cssfile) > 0) { 
  200. $f = fopen($act_cssfile, 'r'); 
  201. $content = fread($f, filesize($act_cssfile)); 
  202. $content = htmlspecialchars($content);  
  203. }  
  204. ?> 
  205. <div class="wrap"> 
  206. <div class="bordertitle"> 
  207. <h2><?php _e('Style Editor', 'nggallery') ?></h2> 
  208. <div class="fileedit-sub"> 
  209. <?php if(!$theme_css_exists): //no need if there is a theme css?> 
  210. <div class="alignright"> 
  211. <form id="themeselector" name="cssfiles" method="post"> 
  212. <?php wp_nonce_field('ngg_style') ?> 
  213. <strong><?php _e('Activate and use style sheet:', 'nggallery') ?></strong> 
  214. <input type="checkbox" name="activateCSS" value="1" <?php checked('1', $ngg->options['activateCSS']); ?> />  
  215. <select name="css" id="theme" style="margin: 0pt; padding: 0pt;"> 
  216. <?php self::output_css_files_dropdown($csslist, $act_cssfile); ?> 
  217. </select> 
  218. <input class="button" type="submit" name="activate" value="<?php _e('Activate', 'nggallery') ?> »" class="button" /> 
  219. </form> 
  220. </div> 
  221. <?php endif; ?> 
  222. <?php if (!is_multisite() || is_super_admin() ) { ?> 
  223. <div class="alignleft"> 
  224. <?php 
  225. $title = '<h3>'; 
  226. if ( is_writeable($act_cssfile) ) { 
  227. $title .= sprintf(__('Editing %s', 'nggallery'), $act_css_name); 
  228. } else { 
  229. $title .= sprintf(__('Browsing %s', 'nggallery'), $act_css_name); 
  230. if ( $theme_css_exists ) 
  231. {$title .= ' ' . __('(from the theme folder)', 'nggallery');} 
  232. $title .= '</h3>'; 
  233. echo $title 
  234. ?> 
  235. </div> 
  236. <br class="clear" /> 
  237. </div> <!-- fileedit-sub --> 
  238. <div id="templateside"> 
  239. <?php if ( $theme_css_exists ) : ?> 
  240. <form id="filemover" name="filemover" method="post" style="background:white; padding: 1px 10px 10px;"> 
  241. <p><?php _e('To ensure your css file stays safe during upgrades, please move it to the right folder.', 'nggallery') ?></p> 
  242. <input type="hidden" name="movecss" value="movecss" /> 
  243. <input type="hidden" name="oldpath" value="<?php echo $act_cssfile ?>" /> 
  244. <input class="button-primary action" type="submit" name="submit" value="<?php _e('Move file', 'nggallery') ?>" /> 
  245. </form> 
  246. <br class="clear" /> 
  247. <?php endif; ?> 
  248. <ul> 
  249. <li><strong><?php _e('Author', 'nggallery') ?>:</strong> <?php echo $act_css_author ?></li> 
  250. <li><strong><?php _e('Version', 'nggallery') ?>:</strong> <?php echo $act_css_version ?></li> 
  251. </ul> 
  252. <p><strong><?php _e('Description', 'nggallery') ?>:</strong></p> 
  253. <p class="description"><?php echo $act_css_description ?></p> 
  254. <p><strong><?php _e('File location', 'nggallery') ?>:</strong></p> 
  255. <p class="description"><?php echo $act_cssfile; ?></p> 
  256. </div> 
  257. <?php if ( !$error ) { ?> 
  258. <form name="template" id="template" method="post"> 
  259. <?php wp_nonce_field('ngg_style') ?> 
  260. <div> 
  261. <textarea cols="70" rows="25" name="newcontent" id="newcontent" tabindex="1" class="codepress css"><?php echo $content ?></textarea> 
  262. <input type="hidden" name="updatecss" value="updatecss" /> 
  263. <input type="hidden" name="folder" value="<?php echo $act_css_folder ?>" /> 
  264. <input type="hidden" name="file" value="<?php echo $act_cssfile ?>" /> 
  265. </div> 
  266. <?php if ( is_writeable($act_cssfile) ) : ?> 
  267. <p class="submit"><input class="button-primary action" type="submit" name="submit" value="<?php _e('Update File', 'nggallery') ?>" tabindex="2" /></p> 
  268. <?php else : ?> 
  269. <p><em><?php _e('If this file were writable you could edit it.', 'nggallery'); ?></em></p> 
  270. <?php endif; ?> 
  271. </form> 
  272. <?php  
  273. } else { 
  274. echo '<div class="error"><p>' . __('This file does not exist. Double check the name and try again.', 'nggallery') . '</p></div>'; 
  275. }  
  276. ?> 
  277. <div class="clear">   </div> 
  278. </div> <!-- wrap--> 
  279. <?php 
  280. } //end if ( !is_multisite() || is_super_admin() )