PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/activation.php

https://github.com/luisnomad/roots
PHP | 324 lines | 266 code | 58 blank | 0 comment | 37 complexity | b912f68ecafd92bbc7c44e7a57b27ce3 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 static front page?', 'roots'); ?></th>
  69. <td>
  70. <fieldset><legend class="screen-reader-text"><span><?php _e('Create static front page?', '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('Yes', 'roots'); ?></option>
  73. <option value="no"><?php echo _e('No', 'roots'); ?></option>
  74. </select>
  75. <br />
  76. <small class="description"><?php printf(__('Create a page called Home and set it to be the static front page', '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']) {
  191. $roots_theme_activation_options['create_front_page'] = false;
  192. $default_pages = array('Home');
  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. foreach ($pages_to_create as $new_page_title) {
  200. $add_default_pages = array(
  201. 'post_title' => $new_page_title,
  202. 'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consequat, orci ac laoreet cursus, dolor sem luctus lorem, eget consequat magna felis a magna. Aliquam scelerisque condimentum ante, eget facilisis tortor lobortis in. In interdum venenatis justo eget consequat. Morbi commodo rhoncus mi nec pharetra. Aliquam erat volutpat. Mauris non lorem eu dolor hendrerit dapibus. Mauris mollis nisl quis sapien posuere consectetur. Nullam in sapien at nisi ornare bibendum at ut lectus. Pellentesque ut magna mauris. Nam viverra suscipit ligula, sed accumsan enim placerat nec. Cras vitae metus vel dolor ultrices sagittis. Duis venenatis augue sed risus laoreet congue ac ac leo. Donec fermentum accumsan libero sit amet iaculis. Duis tristique dictum enim, ac fringilla risus bibendum in. Nunc ornare, quam sit amet ultricies gravida, tortor mi malesuada urna, quis commodo dui nibh in lacus. Nunc vel tortor mi. Pellentesque vel urna a arcu adipiscing imperdiet vitae sit amet neque. Integer eu lectus et nunc dictum sagittis. Curabitur commodo vulputate fringilla. Sed eleifend, arcu convallis adipiscing congue, dui turpis commodo magna, et vehicula sapien turpis sit amet nisi.',
  203. 'post_status' => 'publish',
  204. 'post_type' => 'page'
  205. );
  206. $result = wp_insert_post($add_default_pages);
  207. }
  208. $home = get_page_by_title('Home');
  209. update_option('show_on_front', 'page');
  210. update_option('page_on_front', $home->ID);
  211. $home_menu_order = array(
  212. 'ID' => $home->ID,
  213. 'menu_order' => -1
  214. );
  215. wp_update_post($home_menu_order);
  216. }
  217. if ($roots_theme_activation_options['change_permalink_structure']) {
  218. $roots_theme_activation_options['change_permalink_structure'] = false;
  219. if (get_option('permalink_structure') !== '/%postname%/') {
  220. update_option('permalink_structure', '/%postname%/');
  221. }
  222. global $wp_rewrite;
  223. $wp_rewrite->init();
  224. $wp_rewrite->flush_rules();
  225. }
  226. if ($roots_theme_activation_options['change_uploads_folder']) {
  227. $roots_theme_activation_options['change_uploads_folder'] = false;
  228. update_option('uploads_use_yearmonth_folders', 0);
  229. update_option('upload_path', 'assets');
  230. }
  231. if ($roots_theme_activation_options['create_navigation_menus']) {
  232. $roots_theme_activation_options['create_navigation_menus'] = false;
  233. $roots_nav_theme_mod = false;
  234. if (!has_nav_menu('primary_navigation')) {
  235. $primary_nav_id = wp_create_nav_menu('Primary Navigation', array('slug' => 'primary_navigation'));
  236. $roots_nav_theme_mod['primary_navigation'] = $primary_nav_id;
  237. }
  238. if ($roots_nav_theme_mod) {
  239. set_theme_mod('nav_menu_locations', $roots_nav_theme_mod);
  240. }
  241. }
  242. if ($roots_theme_activation_options['add_pages_to_primary_navigation']) {
  243. $roots_theme_activation_options['add_pages_to_primary_navigation'] = false;
  244. $primary_nav = wp_get_nav_menu_object('Primary Navigation');
  245. $primary_nav_term_id = (int) $primary_nav->term_id;
  246. $menu_items= wp_get_nav_menu_items($primary_nav_term_id);
  247. if (!$menu_items || empty($menu_items)) {
  248. $pages = get_pages();
  249. foreach($pages as $page) {
  250. $item = array(
  251. 'menu-item-object-id' => $page->ID,
  252. 'menu-item-object' => 'page',
  253. 'menu-item-type' => 'post_type',
  254. 'menu-item-status' => 'publish'
  255. );
  256. wp_update_nav_menu_item($primary_nav_term_id, 0, $item);
  257. }
  258. }
  259. }
  260. update_option('roots_theme_activation_options', $roots_theme_activation_options);
  261. }
  262. add_action('admin_init','roots_theme_activation_action');
  263. function roots_deactivation_action() {
  264. update_option('roots_theme_activation_options', roots_get_default_theme_activation_options());
  265. }
  266. add_action('switch_theme', 'roots_deactivation_action');