Example #1
0
/**
 * Hook for add_filter('locale ', ...), change the user interface language
 *
 * @param string $lang
 *
 * @return string
 */
function set_locale($lang)
{
    // Cheap cache
    static $loc = '__UNSET__';
    if (is_admin()) {
        // go with the user setting
        // get_current_user_id uses wp_get_current_user which may not be available the first time(s) get_locale is called
        if ('__UNSET__' == $loc && function_exists('wp_get_current_user')) {
            $loc = get_user_option('user_interface_lang');
        }
    } elseif ('wp-signup.php' == @$GLOBALS['pagenow']) {
        // use global setting
        $loc = get_site_option('WPLANG');
    } else {
        // go with the book info setting
        $metadata = \Pressbooks\Book::getBookInformation();
        if ('__UNSET__' == $loc && !empty($metadata['pb_language'])) {
            $locations = \Pressbooks\L10n\wplang_codes();
            $loc = $locations[$metadata['pb_language']];
        }
    }
    // Return
    if ('__UNSET__' == $loc) {
        return $lang;
    } else {
        return $loc ? $loc : $lang;
    }
}
 /**
  * Hook for add_filter('locale ', ...), change the book language
  *
  * @param string $lang
  *
  * @return string
  */
 static function setLocale($lang)
 {
     // Cheap cache
     static $loc = '__UNSET__';
     if ('__UNSET__' == $loc && function_exists('get_available_languages')) {
         $compare_with = get_available_languages(PB_PLUGIN_DIR . '/languages/');
         $codes = \Pressbooks\L10n\wplang_codes();
         $book_lang = Book::getBookInformation();
         $book_lang = isset($book_lang['pb_language']) ? $book_lang['pb_language'] : 'en';
         $book_lang = $codes[$book_lang];
         foreach ($compare_with as $compare) {
             $compare = str_replace('pressbooks-', '', $compare);
             if (strpos($book_lang, $compare) === 0) {
                 $loc = $compare;
                 break;
             }
         }
         if ('__UNSET__' == $loc) {
             $loc = 'en_US';
             // No match found, default to english
         }
     }
     // Return
     if ('__UNSET__' == $loc) {
         return $lang;
     } else {
         return $loc ? $loc : $lang;
     }
 }
 /**
  * Set up default terms for Front Matter and Back Matter
  * Insert default part, chapter, front matter, and back matter
  * Insert default pages (Authors, Cover, TOC, About, Buy, and Access Denied)
  * Remove content generated by wp_install_defaults
  * Anything which needs to run on blog activation must go in this function
  */
 private function wpmuActivate()
 {
     /** @var $wpdb \wpdb */
     global $wpdb;
     \Pressbooks\Taxonomy::insertTerms();
     $posts = array(array('post_title' => __('Main Body', 'pressbooks'), 'post_name' => 'main-body', 'post_type' => 'part', 'menu_order' => 1), array('post_title' => __('Introduction', 'pressbooks'), 'post_name' => 'introduction', 'post_content' => __('This is where you can write your introduction.', 'pressbooks'), 'post_type' => 'front-matter', 'menu_order' => 1), array('post_title' => __('Chapter 1', 'pressbooks'), 'post_name' => 'chapter-1', 'post_content' => __('This is the first chapter in the main body of the text. You can change the text, rename the chapter, add new chapters, and add new parts.', 'pressbooks'), 'post_type' => 'chapter', 'menu_order' => 1), array('post_title' => __('Appendix', 'pressbooks'), 'post_name' => 'appendix', 'post_content' => __('This is where you can add appendices or other back matter.', 'pressbooks'), 'post_type' => 'back-matter', 'menu_order' => 1), array('post_title' => __('Authors', 'pressbooks'), 'post_name' => 'authors', 'post_type' => 'page'), array('post_title' => __('Cover', 'pressbooks'), 'post_name' => 'cover', 'post_type' => 'page'), array('post_title' => __('Table of Contents', 'pressbooks'), 'post_name' => 'table-of-contents', 'post_type' => 'page'), array('post_title' => __('About', 'pressbooks'), 'post_name' => 'about', 'post_type' => 'page'), array('post_title' => __('Buy', 'pressbooks'), 'post_name' => 'buy', 'post_type' => 'page'), array('post_title' => __('Access Denied', 'pressbooks'), 'post_name' => 'access-denied', 'post_content' => __('This book is private, and accessible only to registered users. If you have an account you can login <a href="/wp-login.php">here</a>. You can also set up your own Pressbooks book at: <a href="http://pressbooks.com">Pressbooks.com</a>.', 'pressbooks'), 'post_type' => 'page'), array('post_title' => __('Custom CSS for Ebook', 'pressbooks'), 'post_name' => 'epub', 'post_type' => 'custom-css'), array('post_title' => __('Custom CSS for PDF', 'pressbooks'), 'post_name' => 'prince', 'post_type' => 'custom-css'), array('post_title' => __('Custom CSS for Web', 'pressbooks'), 'post_name' => 'web', 'post_type' => 'custom-css'), array('post_title' => __('Book Information', 'pressbooks'), 'post_name' => 'book-information', 'post_type' => 'metadata'));
     $post = array('post_status' => 'publish', 'comment_status' => 'open', 'post_author' => $this->user_id);
     $page = array('post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_content' => '<!-- Here be dragons.-->', 'post_author' => $this->user_id, 'tags_input' => __('Default Data', 'pressbooks'));
     update_option('blogdescription', __('Simple Book Production', 'pressbooks'));
     $parent_part = 0;
     $intro = 0;
     $appendix = 0;
     $chapter1 = 0;
     foreach ($posts as $item) {
         $exists = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s AND post_name = %s AND post_status = 'publish' ", array($item['post_title'], $item['post_type'], $item['post_name'])));
         if (empty($exists)) {
             if ('page' == $item['post_type']) {
                 $data = array_merge($item, $page);
             } else {
                 $data = array_merge($item, $post);
             }
             $newpost = wp_insert_post($data, true);
             if (!is_wp_error($newpost)) {
                 switch ($item['post_name']) {
                     case 'cover':
                         $this->opts['page_on_front'] = (int) $newpost;
                         break;
                     case 'table-of-contents':
                         $this->opts['page_for_posts'] = (int) $newpost;
                         break;
                 }
                 if ('part' == $item['post_type']) {
                     $parent_part = $newpost;
                 } elseif ('chapter' == $item['post_type']) {
                     $my_post = array();
                     $my_post['ID'] = $newpost;
                     $my_post['post_parent'] = $parent_part;
                     wp_update_post($my_post);
                     $chapter1 = $newpost;
                 } elseif ('front-matter' == $item['post_type']) {
                     $intro = $newpost;
                 } elseif ('back-matter' == $item['post_type']) {
                     $appendix = $newpost;
                 } elseif ('metadata' == $item['post_type']) {
                     $metadata_id = $newpost;
                     if (0 !== get_current_user_id()) {
                         $user_info = get_userdata(get_current_user_id());
                         $name = $user_info->display_name;
                         update_post_meta($metadata_id, 'pb_author', $name);
                     }
                     $locale = get_site_option('WPLANG');
                     if (!$locale) {
                         $locale = 'en';
                     } else {
                         $locale = array_search($locale, \Pressbooks\L10n\wplang_codes());
                     }
                     update_post_meta($metadata_id, 'pb_title', get_option('blogname'));
                     update_post_meta($metadata_id, 'pb_language', $locale);
                     update_post_meta($metadata_id, 'pb_cover_image', \Pressbooks\Image\default_cover_url());
                 }
             } else {
                 trigger_error($newpost->get_error_message(), E_USER_ERROR);
             }
         }
     }
     // Apply 'introduction' front matter type to 'introduction' post
     wp_set_object_terms($intro, 'introduction', 'front-matter-type');
     // Apply 'appendix' front matter type to 'appendix' post
     wp_set_object_terms($appendix, 'appendix', 'back-matter-type');
     // Apply 'standard' chapter type to 'chapter 1' post
     wp_set_object_terms($chapter1, 'standard', 'chapter-type');
     // 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;
     }
     $this->opts['pb_activated'] = time();
     refresh_blog_details($this->blog_id);
 }
Example #4
0
 /**
  * @covers \Pressbooks\L10n\wplang_codes
  */
 public function test_wplang_codes()
 {
     $wplang_codes = \Pressbooks\L10n\wplang_codes();
     $this->assertTrue(is_array($wplang_codes));
 }