Dac Chartrand 7 years ago
parent
commit
e9d0938a17
  1. 3
      functions.php
  2. 11
      inc/actions/namespace.php
  3. 179
      inc/activation/namespace.php

3
functions.php

@ -37,9 +37,6 @@ foreach ( $includes as $include ) {
} }
require get_template_directory() . '/inc/intervention.php'; require get_template_directory() . '/inc/intervention.php';
add_action( 'after_switch_theme', '\Aldine\Activation\create_default_content', 10 );
add_action( 'after_switch_theme', '\Aldine\Activation\create_menus', 11 );
add_action( 'after_switch_theme', '\Aldine\Activation\assign_menus', 12 );
add_action( 'admin_bar_init', '\Aldine\Actions\remove_admin_bar_callback' ); add_action( 'admin_bar_init', '\Aldine\Actions\remove_admin_bar_callback' );
add_action( 'after_setup_theme', '\Aldine\Actions\setup' ); add_action( 'after_setup_theme', '\Aldine\Actions\setup' );
add_action( 'after_setup_theme', '\Aldine\Actions\content_width', 0 ); add_action( 'after_setup_theme', '\Aldine\Actions\content_width', 0 );

11
inc/actions/namespace.php

@ -81,6 +81,17 @@ function setup() {
'flex-height' => true, 'flex-height' => true,
] ); ] );
// Add starter content
add_theme_support( 'starter-content', [
'options' => [
'show_on_front' => 'page',
'page_on_front' => '{{home}}',
'page_for_posts' => '{{blog}}',
],
'posts' => \Aldine\Activation\get_starter_posts(),
'nav_menus' => \Aldine\Activation\get_starter_nav_menus(),
] );
// Add editor style. // Add editor style.
add_editor_style( $assets->getPath( 'styles/editor.css' ) ); add_editor_style( $assets->getPath( 'styles/editor.css' ) );
} }

179
inc/activation/namespace.php

@ -7,10 +7,11 @@
namespace Aldine\Activation; namespace Aldine\Activation;
/** /**
* Create default page content, importing from Pressbooks Publisher, if possible. * Create starter content, importing from Pressbooks Publisher, if possible.
*
* @return array
*/ */
function create_default_content() { function get_starter_posts() {
if ( ! get_option( 'pb_aldine_activated' ) ) {
$mods = get_option( 'theme_mods_pressbooks-publisher' ); $mods = get_option( 'theme_mods_pressbooks-publisher' );
if ( $mods === false ) { if ( $mods === false ) {
$mods = get_option( 'mods_pressbooks-publisher' ); $mods = get_option( 'mods_pressbooks-publisher' );
@ -31,6 +32,8 @@ function create_default_content() {
$default_pages = [ $default_pages = [
'about' => [ 'about' => [
'post_type' => 'page',
'comment_status' => 'closed',
'post_title' => __( 'About', 'pressbooks-aldine' ), 'post_title' => __( 'About', 'pressbooks-aldine' ),
'post_content' => apply_filters( 'post_content' => apply_filters(
'pb_root_about_page_content', 'pb_root_about_page_content',
@ -47,8 +50,11 @@ function create_default_content() {
) )
) )
), ),
], ],
'help' => [ 'help' => [
'post_type' => 'page',
'comment_status' => 'closed',
'post_title' => __( 'Help', 'pressbooks-aldine' ), 'post_title' => __( 'Help', 'pressbooks-aldine' ),
'post_content' => apply_filters( 'post_content' => apply_filters(
'pb_root_help_page_content', 'pb_root_help_page_content',
@ -64,10 +70,14 @@ function create_default_content() {
), ),
], ],
'catalog' => [ 'catalog' => [
'post_type' => 'page',
'comment_status' => 'closed',
'post_title' => __( 'Catalog', 'pressbooks-aldine' ), 'post_title' => __( 'Catalog', 'pressbooks-aldine' ),
'post_content' => '', 'post_content' => '',
], ],
'home' => [ 'home' => [
'post_type' => 'page',
'comment_status' => 'closed',
'post_title' => __( 'Home', 'pressbooks-aldine' ), 'post_title' => __( 'Home', 'pressbooks-aldine' ),
'post_content' => sprintf( 'post_content' => sprintf(
'<div class="page-section">%s</div>', '<div class="page-section">%s</div>',
@ -76,132 +86,51 @@ function create_default_content() {
], ],
]; ];
// Add our pages return $default_pages;
$pages = [];
foreach ( $default_pages as $slug => $page ) {
$check = get_page_by_path( $slug );
if ( empty( $check ) ) {
$pages[ $slug ] = wp_insert_post( array_merge( $page, [ 'post_type' => 'page', 'post_status' => 'publish' ] ) );
} else {
$pages[ $slug ] = $check->ID;
}
}
// Set front page to Home
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $pages['home'] );
// Remove content generated by wp_install_defaults
wp_delete_post( 1, true );
wp_delete_post( 2, true );
wp_delete_comment( 1, true );
// Add "pb_aldine_activated" option to enable check above
add_option( 'pb_aldine_activated', 1 );
}
} }
/** /**
* Create default primary and footer menus. * Create default primary and footer menus.
*
* @return array
*/ */
function create_menus() { function get_starter_nav_menus() {
$menu_name = __( 'Primary Menu', 'pressbooks-aldine' ); $default_menus = [
'primary-menu' => [
if ( ! wp_get_nav_menu_object( $menu_name ) ) { 'name' => __( 'Primary Menu', 'pressbooks-aldine' ),
$menu_id = wp_create_nav_menu( $menu_name ); 'items' => [
'page_catalog' => [
$catalog = get_page_by_title( __( 'Catalog', 'pressbooks-aldine' ) ); 'title' => __( 'Catalog', 'pressbooks-aldine' ),
if ( $catalog && defined( 'PB_PLUGIN_VERSION' ) ) { 'type' => 'post_type',
wp_update_nav_menu_item( 'object' => 'page',
$menu_id, 'object_id' => '{{catalog}}',
0, ],
[ ],
'menu-item-title' => __( 'Catalog', 'pressbooks-aldine' ), ],
'menu-item-type' => 'post_type', 'network-footer-menu' => [
'menu-item-object' => 'page', 'name' => __( 'Footer Menu', 'pressbooks-aldine' ),
'menu-item-object-id' => $catalog->ID, 'items' => [
'menu-item-status' => 'publish', 'page_about' => [
] 'title' => __( 'About', 'pressbooks-aldine' ),
); 'type' => 'post_type',
} 'object' => 'page',
} 'object_id' => '{{about}}',
],
$menu_name = __( 'Footer Menu', 'pressbooks-aldine' ); 'page_catalog' => [
'title' => __( 'Catalog', 'pressbooks-aldine' ),
if ( ! wp_get_nav_menu_object( $menu_name ) ) { 'type' => 'post_type',
$menu_id = wp_create_nav_menu( $menu_name ); 'object' => 'page',
'object_id' => '{{catalog}}',
$about = get_page_by_title( __( 'About', 'pressbooks-aldine' ) ); ],
if ( $about ) { 'page_help' => [
wp_update_nav_menu_item( 'title' => __( 'Help', 'pressbooks-aldine' ),
$menu_id, 'type' => 'post_type',
0, 'object' => 'page',
[ 'object_id' => '{{help}}',
'menu-item-title' => __( 'About', 'pressbooks-aldine' ), ],
'menu-item-type' => 'post_type', ],
'menu-item-object' => 'page', ],
'menu-item-object-id' => $about->ID, ];
'menu-item-status' => 'publish',
]
);
}
$catalog = get_page_by_title( __( 'Catalog', 'pressbooks-aldine' ) );
if ( $catalog && defined( 'PB_PLUGIN_VERSION' ) ) {
wp_update_nav_menu_item(
$menu_id,
0,
[
'menu-item-title' => __( 'Catalog', 'pressbooks-aldine' ),
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $catalog->ID,
'menu-item-status' => 'publish',
]
);
}
$help = get_page_by_title( __( 'Help', 'pressbooks-aldine' ) );
if ( $help ) {
wp_update_nav_menu_item(
$menu_id,
0,
[
'menu-item-title' => __( 'Help', 'pressbooks-aldine' ),
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $help->ID,
'menu-item-status' => 'publish',
]
);
}
}
}
/**
* Check for presence of menus; if they exist, assign them to their locations.
*/
function assign_menus() {
$locations = get_theme_mod( 'nav_menu_locations' );
if ( ! empty( $locations ) ) {
foreach ( $locations as $id => $value ) {
switch ( $id ) {
case 'primary-menu':
$menu = get_term_by( 'name', 'Primary Menu', 'nav_menu' );
break;
case 'network-footer-menu':
$menu = get_term_by( 'name', 'Footer Menu', 'nav_menu' );
break;
}
if ( $menu ) {
$locations[ $id ] = $menu->term_id;
}
}
set_theme_mod( 'nav_menu_locations', $locations ); return $default_menus;
}
} }

Loading…
Cancel
Save