PageRenderTime 27ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/admin.php

https://github.com/tylerreed/mobilepress
PHP | 325 lines | 238 code | 42 blank | 45 comment | 75 complexity | 0861715ac976d08f4664ade4d6ab9b48 MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'Mobilepress_admin' ) ) {
  3. /**
  4. * MobilePress class for creating the admin area
  5. *
  6. * @package MobilePress
  7. * @since 1.0
  8. */
  9. class Mobilepress_admin {
  10. /**
  11. * Constructor which runs a few checks
  12. *
  13. * @package MobilePress
  14. * @since 1.2
  15. */
  16. public function __construct() {
  17. if ( MOPR_DBVERSION < MOPR_VERSION ) {
  18. require_once( MOPR_PATH . 'classes/setup.php' );
  19. $mobilepress_setup = new Mobilepress_setup;
  20. $mobilepress_setup->mopr_install();
  21. }
  22. }
  23. /**
  24. * Renders the themes page
  25. *
  26. * @package MobilePress
  27. * @since 1.0
  28. */
  29. public function mopr_admin_themes() {
  30. global $wpdb;
  31. if ( isset( $_GET['action'] ) == 'activate' && $_GET['theme_type'] == 'mobile' ) {
  32. $theme_type = $_GET['theme_type'];
  33. $wpdb->update( MOPR_TABLE,
  34. array(
  35. 'option_value' => $_GET['template'],
  36. 'option_value_2' => $_GET['theme']
  37. ),
  38. array( 'option_name' => $theme_type .'_theme' )
  39. );
  40. $theme_root = explode( 'wp-content', $_GET['theme_root'] );
  41. $theme_root = $theme_root[1];
  42. $wpdb->update( MOPR_TABLE,
  43. array(
  44. 'option_value' => $theme_root
  45. ),
  46. array( 'option_name' => $theme_type .'_theme_root' )
  47. );
  48. }
  49. $mobile_theme = mopr_get_option( 'mobile_theme', 2 );
  50. $themes = $this->mopr_select_themes();
  51. $theme_names = array_keys( $themes );
  52. $data['mobile_theme'] = $mobile_theme;
  53. $data['themes'] = $themes;
  54. $data['theme_names'] = $theme_names;
  55. mopr_load_view( 'admin_themes', $data );
  56. }
  57. /**
  58. * Renders the MobilePress settings page
  59. *
  60. * @package MobilePress
  61. * @since 1.2
  62. */
  63. public function mopr_admin_settings() {
  64. global $wpdb;
  65. if ( isset( $_POST['save'] ) ) {
  66. $mopr_settings = array(
  67. 'front_page' => $_POST['mopr_front_page'],
  68. 'page_posts' => $_POST['mopr_page_posts'],
  69. 'show_categories' => $_POST['mopr_show_categories'],
  70. 'show_pages' => $_POST['mopr_show_pages'],
  71. 'show_tags' => $_POST['mopr_show_tags'],
  72. 'show_thumbnails' => $_POST['mopr_show_thumbnails'],
  73. 'comments' => $_POST['mopr_comments'],
  74. 'force_mobile' => $_POST['mopr_force_mobile'],
  75. 'custom_themes' => $_POST['mopr_custom_themes']
  76. );
  77. foreach( $mopr_settings as $mopr_setting => $mopr_value ) {
  78. $wpdb->update( MOPR_TABLE,
  79. array( 'option_value' => $mopr_value ),
  80. array( 'option_name' => $mopr_setting )
  81. );
  82. }
  83. }
  84. $data['mopr_front_page'] = mopr_get_option( 'front_page', 1 );
  85. $data['mopr_page_posts'] = mopr_get_option( 'page_posts', 1 );
  86. $data['mopr_show_categories'] = mopr_get_option( 'show_categories', 1 );
  87. $data['mopr_show_pages'] = mopr_get_option( 'show_pages', 1 );
  88. $data['mopr_show_tags'] = mopr_get_option( 'show_tags', 1 );
  89. $data['mopr_show_thumbnails'] = mopr_get_option( 'show_thumbnails', 1 );
  90. $data['mopr_comments'] = mopr_get_option( 'comments', 1 );
  91. $data['mopr_force_mobile'] = mopr_get_option( 'force_mobile', 1 );
  92. $data['mopr_custom_themes'] = mopr_get_option( 'custom_themes', 1 );
  93. mopr_load_view( 'admin_settings', $data );
  94. }
  95. /**
  96. * Function to check if themes exist and if default theme exists, if so, returns the themes
  97. *
  98. * @package MobilePress
  99. * @since 1.1.1
  100. */
  101. private function mopr_select_themes() {
  102. $default_themes = $this->mopr_get_themes( MOPR_ROOT_PATH . 'themes' );
  103. $custom_themes = $this->mopr_get_themes( WP_CONTENT_DIR . mopr_get_option( 'custom_themes', 1 ) );
  104. if ( is_array( $default_themes ) && is_array( $custom_themes ) ) {
  105. // Merge themes
  106. $themes = array_merge( $default_themes, $custom_themes );
  107. ksort( $themes );
  108. } else if ( is_array( $default_themes ) ) {
  109. $themes = $default_themes;
  110. ksort( $themes );
  111. } else if ( is_array( $custom_themes ) ) {
  112. $themes = $custom_themes;
  113. ksort( $themes );
  114. } else {
  115. $themes = array();
  116. }
  117. if ( empty( $themes ) ) {
  118. mopr_display_notice( '<p>Please upload a theme to your MobilePress themes directory!</p>' );
  119. return false;
  120. } else {
  121. if ( empty( $themes['Default']['Title'] ) ) {
  122. mopr_display_notice( '<p>You need to upload the default theme!</p>' );
  123. return false;
  124. } else {
  125. return $themes;
  126. }
  127. }
  128. }
  129. /**
  130. * Core WP function for getting themes (with a few modifications) - located at: wp-includes/theme.php
  131. *
  132. * @package MobilePress
  133. * @since 1.0
  134. */
  135. private function mopr_get_themes( $directory ) {
  136. $themes = array();
  137. $theme_loc = $theme_root = $directory;
  138. // Files in wp-content/themes directory and one subdir down
  139. $themes_dir = @ opendir($theme_root);
  140. if ( !$themes_dir )
  141. return false;
  142. while ( ($theme_dir = readdir($themes_dir)) !== false ) {
  143. if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
  144. if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
  145. continue;
  146. $stylish_dir = @ opendir($theme_root . '/' . $theme_dir);
  147. $found_stylesheet = false;
  148. while ( ($theme_file = readdir($stylish_dir)) !== false ) {
  149. if ( $theme_file == 'style.css' ) {
  150. $theme_files[] = $theme_dir . '/' . $theme_file;
  151. $found_stylesheet = true;
  152. break;
  153. }
  154. }
  155. @closedir($stylish_dir);
  156. if ( !$found_stylesheet ) { // look for themes in that dir
  157. $subdir = "$theme_root/$theme_dir";
  158. $subdir_name = $theme_dir;
  159. $theme_subdir = @ opendir( $subdir );
  160. while ( ($theme_dir = readdir($theme_subdir)) !== false ) {
  161. if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) {
  162. if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
  163. continue;
  164. $stylish_dir = @ opendir($subdir . '/' . $theme_dir);
  165. $found_stylesheet = false;
  166. while ( ($theme_file = readdir($stylish_dir)) !== false ) {
  167. if ( $theme_file == 'style.css' ) {
  168. $theme_files[] = $subdir_name . '/' . $theme_dir . '/' . $theme_file;
  169. $found_stylesheet = true;
  170. break;
  171. }
  172. }
  173. @closedir($stylish_dir);
  174. }
  175. }
  176. @closedir($theme_subdir);
  177. $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
  178. }
  179. }
  180. }
  181. if ( is_dir( $theme_dir ) )
  182. @closedir( $theme_dir );
  183. if ( !$themes_dir || !$theme_files )
  184. return $themes;
  185. sort($theme_files);
  186. foreach ( (array) $theme_files as $theme_file ) {
  187. if ( !is_readable("$theme_root/$theme_file") ) {
  188. $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
  189. continue;
  190. }
  191. $theme_data = get_theme_data("$theme_root/$theme_file");
  192. $name = $theme_data['Name'];
  193. $title = $theme_data['Title'];
  194. $description = wptexturize($theme_data['Description']);
  195. $version = $theme_data['Version'];
  196. $author = $theme_data['Author'];
  197. $template = $theme_data['Template'];
  198. $stylesheet = dirname($theme_file);
  199. $screenshot = false;
  200. foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
  201. if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
  202. $screenshot = "screenshot.$ext";
  203. break;
  204. }
  205. }
  206. if ( empty($name) ) {
  207. $name = dirname($theme_file);
  208. $title = $name;
  209. }
  210. if ( empty($template) ) {
  211. if ( file_exists(dirname("$theme_root/$theme_file/index.php")) )
  212. $template = dirname($theme_file);
  213. else
  214. continue;
  215. }
  216. $template = trim($template);
  217. if ( !file_exists("$theme_root/$template/index.php") ) {
  218. $parent_dir = dirname(dirname($theme_file));
  219. if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
  220. $template = "$parent_dir/$template";
  221. } else {
  222. $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
  223. continue;
  224. }
  225. }
  226. $stylesheet_files = array();
  227. $stylesheet_dir = @ dir("$theme_root/$stylesheet");
  228. if ( $stylesheet_dir ) {
  229. while ( ($file = $stylesheet_dir->read()) !== false ) {
  230. if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) )
  231. $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
  232. }
  233. }
  234. $template_files = array();
  235. $template_dir = @ dir("$theme_root/$template");
  236. if ( $template_dir ) {
  237. while(($file = $template_dir->read()) !== false) {
  238. if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
  239. $template_files[] = "$theme_loc/$template/$file";
  240. }
  241. }
  242. $template_dir = dirname($template_files[0]);
  243. $stylesheet_dir = dirname($stylesheet_files[0]);
  244. if ( empty($template_dir) )
  245. $template_dir = '/';
  246. if ( empty($stylesheet_dir) )
  247. $stylesheet_dir = '/';
  248. // Check for theme name collision. This occurs if a theme is copied to
  249. // a new theme directory and the theme header is not updated. Whichever
  250. // theme is first keeps the name. Subsequent themes get a suffix applied.
  251. // The Default always trump their pretenders.
  252. if ( isset($themes[$name]) ) {
  253. if ( ('Default' == $name) &&
  254. ('default' == $stylesheet) ) {
  255. // If another theme has claimed to be one of our default themes, move
  256. // them aside.
  257. $suffix = $themes[$name]['Stylesheet'];
  258. $new_name = "$name/$suffix";
  259. $themes[$new_name] = $themes[$name];
  260. $themes[$new_name]['Name'] = $new_name;
  261. } else {
  262. $name = "$name/$stylesheet";
  263. }
  264. }
  265. $themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Stylesheet' => $stylesheet, 'Template Files' => $template_files, 'Stylesheet Files' => $stylesheet_files, 'Template Dir' => $template_dir, 'Stylesheet Dir' => $stylesheet_dir, 'Status' => $theme_data['Status'], 'Screenshot' => $screenshot, 'Tags' => $theme_data['Tags'], 'Theme Root' => $theme_root, 'Theme Root URI' => str_replace( WP_CONTENT_DIR, content_url(), $theme_root ) );
  266. }
  267. // Resolve theme dependencies.
  268. $theme_names = array_keys($themes);
  269. foreach ( (array) $theme_names as $theme_name ) {
  270. $themes[$theme_name]['Parent Theme'] = '';
  271. if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) {
  272. foreach ( (array) $theme_names as $parent_theme_name ) {
  273. if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) {
  274. $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
  275. break;
  276. }
  277. }
  278. }
  279. }
  280. return $themes;
  281. }
  282. }
  283. }