pressbooks aldine theme the theme used for the front page or default book
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.2 KiB

<?php
/**
* @package Aldine
*/
namespace Aldine\Activation;
use ParsedownExtra;
function create_default_content() {
if ( ! get_option( 'pb_aldine_activated' ) ) {
$extra = new ParsedownExtra();
$about = $extra->text( file_get_contents( get_stylesheet_directory() . '/docs/about.md' ) );
$help = $extra->text( file_get_contents( get_stylesheet_directory() . '/docs/help.md' ) );
$default_pages = [
'about' => [
'post_title' => __( 'About', 'pressbooks-aldine' ),
'post_content' => apply_filters( 'pb_root_about_page_content', $about ),
],
'help' => [
'post_title' => __( 'Help', 'pressbooks-aldine' ),
'post_content' => apply_filters( 'pb_root_help_page_content', $help ),
],
'catalog' => [
'post_title' => __( 'Catalog', 'pressbooks-aldine' ),
'post_content' => '',
],
'home' => [
'post_title' => __( 'Home', 'pressbooks-aldine' ),
'post_content' => '',
],
];
// Add our 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
if ( ! wp_delete_post( 1, true ) ) {
return;
}
if ( ! wp_delete_post( 2, true ) ) {
return;
}
if ( ! wp_delete_comment( 1, true ) ) {
return;
}
// Add "pb_aldine_activated" option to enable check above
add_option( 'pb_aldine_activated', 1 );
}
}
/**
* Create default primary and footer menus.
*/
function create_menus() {
$menu_name = __( 'Primary Menu', 'pressbooks-aldine' );
if ( ! wp_get_nav_menu_object( $menu_name ) ) {
$menu_id = wp_create_nav_menu( $menu_name );
$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',
]
);
}
}
$menu_name = __( 'Footer Menu', 'pressbooks-aldine' );
if ( ! wp_get_nav_menu_object( $menu_name ) ) {
$menu_id = wp_create_nav_menu( $menu_name );
$about = get_page_by_title( __( 'About', 'pressbooks-aldine' ) );
if ( $about ) {
wp_update_nav_menu_item(
$menu_id,
0,
[
'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 );
}
}