PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/activation.php

https://github.com/ndimatteo/hpv-theme
PHP | 367 lines | 298 code | 69 blank | 0 comment | 39 complexity | 632abcb479688da409db6c0dfcdd3671 MD5 | raw file
  1. <?php
  2. if (is_admin() && isset($_GET['activated']) && 'themes.php' == $GLOBALS['pagenow']) {
  3. wp_redirect(admin_url('themes.php?page=theme_activation_options'));
  4. exit;
  5. }
  6. function roots_theme_activation_options_init() {
  7. if (roots_get_theme_activation_options() === false) {
  8. add_option('roots_theme_activation_options', roots_get_default_theme_activation_options());
  9. }
  10. register_setting(
  11. 'roots_activation_options',
  12. 'roots_theme_activation_options',
  13. 'roots_theme_activation_options_validate'
  14. );
  15. }
  16. add_action('admin_init', 'roots_theme_activation_options_init');
  17. function roots_activation_options_page_capability($capability) {
  18. return 'edit_theme_options';
  19. }
  20. add_filter('option_page_capability_roots_activation_options', 'roots_activation_options_page_capability');
  21. function roots_theme_activation_options_add_page() {
  22. $roots_activation_options = roots_get_theme_activation_options();
  23. if (!$roots_activation_options['first_run']) {
  24. $theme_page = add_theme_page(
  25. __('Theme Activation', 'roots'),
  26. __('Theme Activation', 'roots'),
  27. 'edit_theme_options',
  28. 'theme_activation_options',
  29. 'roots_theme_activation_options_render_page'
  30. );
  31. } else {
  32. if (is_admin() && isset($_GET['page']) && $_GET['page'] === 'theme_activation_options') {
  33. global $wp_rewrite;
  34. $wp_rewrite->flush_rules();
  35. wp_redirect(admin_url('themes.php'));
  36. exit;
  37. }
  38. }
  39. }
  40. add_action('admin_menu', 'roots_theme_activation_options_add_page', 50);
  41. function roots_get_default_theme_activation_options() {
  42. $default_theme_activation_options = array(
  43. 'first_run' => false,
  44. 'create_front_page' => false,
  45. 'change_permalink_structure' => false,
  46. 'change_uploads_folder' => false,
  47. 'create_navigation_menus' => false,
  48. 'add_pages_to_primary_navigation' => false,
  49. );
  50. return apply_filters('roots_default_theme_activation_options', $default_theme_activation_options);
  51. }
  52. function roots_get_theme_activation_options() {
  53. return get_option('roots_theme_activation_options', roots_get_default_theme_activation_options());
  54. }
  55. function roots_theme_activation_options_render_page() { ?>
  56. <div class="wrap">
  57. <?php screen_icon(); ?>
  58. <h2><?php printf(__('%s Theme Activation', 'roots'), wp_get_theme() ); ?></h2>
  59. <?php settings_errors(); ?>
  60. <form method="post" action="options.php">
  61. <?php
  62. settings_fields('roots_activation_options');
  63. $roots_activation_options = roots_get_theme_activation_options();
  64. $roots_default_activation_options = roots_get_default_theme_activation_options();
  65. ?>
  66. <input type="hidden" value="1" name="roots_theme_activation_options[first_run]" />
  67. <table class="form-table">
  68. <tr valign="top"><th scope="row"><?php _e('Create multiple page or single page site?', 'roots'); ?></th>
  69. <td>
  70. <fieldset><legend class="screen-reader-text"><span><?php _e('Create multiple page or single page site?', 'roots'); ?></span></legend>
  71. <select name="roots_theme_activation_options[create_front_page]" id="create_front_page">
  72. <option selected="selected" value="yes"><?php echo _e('Multiple Pages', 'roots'); ?></option>
  73. <option value="no"><?php echo _e('Single Page', 'roots'); ?></option>
  74. </select>
  75. <br />
  76. <small class="description"><?php printf(__('Creates the necessary pages for selected type', 'roots')); ?></small>
  77. </fieldset>
  78. </td>
  79. </tr>
  80. <tr valign="top"><th scope="row"><?php _e('Change permalink structure?', 'roots'); ?></th>
  81. <td>
  82. <fieldset><legend class="screen-reader-text"><span><?php _e('Update permalink structure?', 'roots'); ?></span></legend>
  83. <select name="roots_theme_activation_options[change_permalink_structure]" id="change_permalink_structure">
  84. <option selected="selected" value="yes"><?php echo _e('Yes', 'roots'); ?></option>
  85. <option value="no"><?php echo _e('No', 'roots'); ?></option>
  86. </select>
  87. <br />
  88. <small class="description"><?php printf(__('Change permalink structure to /&#37;postname&#37;/', 'roots')); ?></small>
  89. </fieldset>
  90. </td>
  91. </tr>
  92. <tr valign="top"><th scope="row"><?php _e('Change uploads folder?', 'roots'); ?></th>
  93. <td>
  94. <fieldset><legend class="screen-reader-text"><span><?php _e('Update uploads folder?', 'roots'); ?></span></legend>
  95. <select name="roots_theme_activation_options[change_uploads_folder]" id="change_uploads_folder">
  96. <option selected="selected" value="yes"><?php echo _e('Yes', 'roots'); ?></option>
  97. <option value="no"><?php echo _e('No', 'roots'); ?></option>
  98. </select>
  99. <br />
  100. <small class="description"><?php printf(__('Change uploads folder to /assets/ instead of /wp-content/uploads/', 'roots')); ?></small>
  101. </fieldset>
  102. </td>
  103. </tr>
  104. <tr valign="top"><th scope="row"><?php _e('Create navigation menu?', 'roots'); ?></th>
  105. <td>
  106. <fieldset><legend class="screen-reader-text"><span><?php _e('Create navigation menu?', 'roots'); ?></span></legend>
  107. <select name="roots_theme_activation_options[create_navigation_menus]" id="create_navigation_menus">
  108. <option selected="selected" value="yes"><?php echo _e('Yes', 'roots'); ?></option>
  109. <option value="no"><?php echo _e('No', 'roots'); ?></option>
  110. </select>
  111. <br />
  112. <small class="description"><?php printf(__('Create the Primary Navigation menu and set the location', 'roots')); ?></small>
  113. </fieldset>
  114. </td>
  115. </tr>
  116. <tr valign="top"><th scope="row"><?php _e('Add pages to menu?', 'roots'); ?></th>
  117. <td>
  118. <fieldset><legend class="screen-reader-text"><span><?php _e('Add pages to menu?', 'roots'); ?></span></legend>
  119. <select name="roots_theme_activation_options[add_pages_to_primary_navigation]" id="add_pages_to_primary_navigation">
  120. <option selected="selected" value="yes"><?php echo _e('Yes', 'roots'); ?></option>
  121. <option value="no"><?php echo _e('No', 'roots'); ?></option>
  122. </select>
  123. <br />
  124. <small class="description"><?php printf(__('Add all current published pages to the Primary Navigation', 'roots')); ?></small>
  125. </fieldset>
  126. </td>
  127. </tr>
  128. </table>
  129. <?php submit_button(); ?>
  130. </form>
  131. </div>
  132. <?php }
  133. function roots_theme_activation_options_validate($input) {
  134. $output = $defaults = roots_get_default_theme_activation_options();
  135. if (isset($input['first_run'])) {
  136. if ($input['first_run'] === '1') {
  137. $input['first_run'] = true;
  138. }
  139. $output['first_run'] = $input['first_run'];
  140. }
  141. if (isset($input['create_front_page'])) {
  142. if ($input['create_front_page'] === 'yes') {
  143. $input['create_front_page'] = true;
  144. }
  145. if ($input['create_front_page'] === 'no') {
  146. $input['create_front_page'] = false;
  147. }
  148. $output['create_front_page'] = $input['create_front_page'];
  149. }
  150. if (isset($input['change_permalink_structure'])) {
  151. if ($input['change_permalink_structure'] === 'yes') {
  152. $input['change_permalink_structure'] = true;
  153. }
  154. if ($input['change_permalink_structure'] === 'no') {
  155. $input['change_permalink_structure'] = false;
  156. }
  157. $output['change_permalink_structure'] = $input['change_permalink_structure'];
  158. }
  159. if (isset($input['change_uploads_folder'])) {
  160. if ($input['change_uploads_folder'] === 'yes') {
  161. $input['change_uploads_folder'] = true;
  162. }
  163. if ($input['change_uploads_folder'] === 'no') {
  164. $input['change_uploads_folder'] = false;
  165. }
  166. $output['change_uploads_folder'] = $input['change_uploads_folder'];
  167. }
  168. if (isset($input['create_navigation_menus'])) {
  169. if ($input['create_navigation_menus'] === 'yes') {
  170. $input['create_navigation_menus'] = true;
  171. }
  172. if ($input['create_navigation_menus'] === 'no') {
  173. $input['create_navigation_menus'] = false;
  174. }
  175. $output['create_navigation_menus'] = $input['create_navigation_menus'];
  176. }
  177. if (isset($input['add_pages_to_primary_navigation'])) {
  178. if ($input['add_pages_to_primary_navigation'] === 'yes') {
  179. $input['add_pages_to_primary_navigation'] = true;
  180. }
  181. if ($input['add_pages_to_primary_navigation'] === 'no') {
  182. $input['add_pages_to_primary_navigation'] = false;
  183. }
  184. $output['add_pages_to_primary_navigation'] = $input['add_pages_to_primary_navigation'];
  185. }
  186. return apply_filters('roots_theme_activation_options_validate', $output, $input, $defaults);
  187. }
  188. function roots_theme_activation_action() {
  189. $roots_theme_activation_options = roots_get_theme_activation_options();
  190. if ($roots_theme_activation_options['create_front_page'] == true) {
  191. $roots_theme_activation_options['create_front_page'] = false;
  192. $default_pages = array('Home', 'About', 'Menu', 'Gallery', 'News', 'Contact');
  193. $existing_pages = get_pages();
  194. $temp = array();
  195. foreach ($existing_pages as $page) {
  196. $temp[] = $page->post_title;
  197. }
  198. $pages_to_create = array_diff($default_pages, $temp);
  199. $i=0;
  200. foreach ($pages_to_create as $new_page_title) {
  201. $i++;
  202. $add_default_pages = array(
  203. 'post_title' => $new_page_title,
  204. 'post_content' => 'Enter page content here...',
  205. 'post_status' => 'publish',
  206. 'post_type' => 'page',
  207. 'menu_order' => $i
  208. );
  209. $result = wp_insert_post($add_default_pages);
  210. }
  211. $sample = get_page_by_title('Sample Page');
  212. wp_delete_post( $sample->ID, true );
  213. $home = get_page_by_title('Home');
  214. update_option('show_on_front', 'page');
  215. update_option('page_on_front', $home->ID);
  216. $home_menu_order = array(
  217. 'ID' => $home->ID,
  218. 'menu_order' => -1
  219. );
  220. wp_update_post($home_menu_order);
  221. } elseif ($roots_theme_activation_options['create_front_page'] == false) {
  222. $default_pages = array('Home');
  223. $existing_pages = get_pages();
  224. $temp = array();
  225. foreach ($existing_pages as $page) {
  226. $temp[] = $page->post_title;
  227. }
  228. $pages_to_create = array_diff($default_pages, $temp);
  229. foreach ($pages_to_create as $new_page_title) {
  230. $add_default_pages = array(
  231. 'post_title' => $new_page_title,
  232. 'post_content' => 'Enter page content here...',
  233. 'post_status' => 'publish',
  234. 'post_type' => 'page'
  235. );
  236. $result = wp_insert_post($add_default_pages);
  237. }
  238. $sample = get_page_by_title('Sample Page');
  239. wp_delete_post( $sample->ID, true );
  240. $home = get_page_by_title('Home');
  241. update_option('show_on_front', 'page');
  242. update_option('page_on_front', $home->ID);
  243. $home_menu_order = array(
  244. 'ID' => $home->ID,
  245. 'menu_order' => -1
  246. );
  247. wp_update_post($home_menu_order);
  248. }
  249. if ($roots_theme_activation_options['change_permalink_structure']) {
  250. $roots_theme_activation_options['change_permalink_structure'] = false;
  251. if (get_option('permalink_structure') !== '/%postname%/') {
  252. update_option('permalink_structure', '/%postname%/');
  253. }
  254. global $wp_rewrite;
  255. $wp_rewrite->init();
  256. $wp_rewrite->flush_rules();
  257. }
  258. if ($roots_theme_activation_options['change_uploads_folder']) {
  259. $roots_theme_activation_options['change_uploads_folder'] = false;
  260. update_option('uploads_use_yearmonth_folders', 0);
  261. update_option('upload_path', 'assets');
  262. }
  263. if ($roots_theme_activation_options['create_navigation_menus']) {
  264. $roots_theme_activation_options['create_navigation_menus'] = false;
  265. $roots_nav_theme_mod = false;
  266. if (!has_nav_menu('primary_navigation')) {
  267. $primary_nav_id = wp_create_nav_menu('Primary Navigation', array('slug' => 'primary_navigation'));
  268. $roots_nav_theme_mod['primary_navigation'] = $primary_nav_id;
  269. }
  270. if ($roots_nav_theme_mod) {
  271. set_theme_mod('nav_menu_locations', $roots_nav_theme_mod);
  272. }
  273. }
  274. if ($roots_theme_activation_options['add_pages_to_primary_navigation']) {
  275. $roots_theme_activation_options['add_pages_to_primary_navigation'] = false;
  276. $primary_nav = wp_get_nav_menu_object('Primary Navigation');
  277. $primary_nav_term_id = (int) $primary_nav->term_id;
  278. $menu_items= wp_get_nav_menu_items($primary_nav_term_id);
  279. if (!$menu_items || empty($menu_items)) {
  280. $pages = get_pages();
  281. foreach($pages as $page) {
  282. $item = array(
  283. 'menu-item-object-id' => $page->ID,
  284. 'menu-item-object' => 'page',
  285. 'menu-item-type' => 'post_type',
  286. 'menu-item-status' => 'publish'
  287. );
  288. wp_update_nav_menu_item($primary_nav_term_id, 0, $item);
  289. }
  290. }
  291. }
  292. update_option('roots_theme_activation_options', $roots_theme_activation_options);
  293. }
  294. add_action('admin_init','roots_theme_activation_action');
  295. function roots_deactivation_action() {
  296. update_option('roots_theme_activation_options', roots_get_default_theme_activation_options());
  297. }
  298. add_action('switch_theme', 'roots_deactivation_action');