/** * If the user leaves certain meta info blank, forcefully fill it with our own * * @param int $pid * @param \WP_Post $post */ function add_required_data($pid, $post) { $pb_author = get_post_meta($pid, 'pb_author', true); if (!$pb_author) { // if the pb_author metadata value is not set, set it to the primary book user's name if (0 !== get_current_user_id()) { /** @var $user_info \WP_User */ $user_info = get_userdata(get_current_user_id()); $name = $user_info->display_name; update_post_meta($pid, 'pb_author', $name); } } $pb_language = get_post_meta($pid, 'pb_language', true); if (!$pb_language) { // if the pb_language metadata value is not set, set it to the network default $locale = get_site_option('WPLANG'); $locale = array_search($locale, \PressBooks\L10n\wplang_codes()); if (!$locale) { $locale = 'en'; } update_post_meta($pid, 'pb_language', $locale); } $pb_cover_image = get_post_meta($pid, 'pb_cover_image', true); if (!$pb_cover_image) { // if the pb_cover_image metadata value is not set, set it to the default image update_post_meta($pid, 'pb_cover_image', \PressBooks\Image\default_cover_url()); } }
/** * 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 ($GLOBALS['pagenow'] == 'wp-signup.php') { // 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; } }
/** * @covers \PressBooks\L10n\wplang_codes */ public function test_wplang_codes() { $wplang_codes = \PressBooks\L10n\wplang_codes(); $this->assertTrue(is_array($wplang_codes)); }