/** * I18N: WP_Translation_Controller class. * * @package WordPress * @subpackage I18N * @since 6.5.0 */ /** * Class WP_Translation_Controller. * * @since 6.5.0 */ final class WP_Translation_Controller { /** * Current locale. * * @since 6.5.0 * @var string */ protected $current_locale = 'en_US'; /** * Map of loaded translations per locale and text domain. * * [ Locale => [ Textdomain => [ ..., ... ] ] ] * * @since 6.5.0 * @var array> */ protected $loaded_translations = array(); /** * List of loaded translation files. * * [ Filename => [ Locale => [ Textdomain => WP_Translation_File ] ] ] * * @since 6.5.0 * @var array>> */ protected $loaded_files = array(); /** * Container for the main instance of the class. * * @since 6.5.0 * @var WP_Translation_Controller|null */ private static $instance = null; /** * Utility method to retrieve the main instance of the class. * * The instance will be created if it does not exist yet. * * @since 6.5.0 * * @return WP_Translation_Controller */ public static function get_instance(): WP_Translation_Controller { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Returns the current locale. * * @since 6.5.0 * * @return string Locale. */ public function get_locale(): string { return $this->current_locale; } /** * Sets the current locale. * * @since 6.5.0 * * @param string $locale Locale. */ public function set_locale( string $locale ) { $this->current_locale = $locale; } /** * Loads a translation file for a given text domain. * * @since 6.5.0 * * @param string $translation_file Translation file. * @param string $textdomain Optional. Text domain. Default 'default'. * @param string $locale Optional. Locale. Default current locale. * @return bool True on success, false otherwise. */ public function load_file( string $translation_file, string $textdomain = 'default', string $locale = null ): bool { if ( null === $locale ) { $locale = $this->current_locale; } $translation_file = realpath( $translation_file ); if ( false === $translation_file ) { return false; } if ( isset( $this->loaded_files[ $translation_file ][ $locale ][ $textdomain ] ) && false !== $this->loaded_files[ $translation_file ][ $locale ][ $textdomain ] ) { return null === $this->loaded_files[ $translation_file ][ $locale ][ $textdomain ]->error(); } if ( isset( $this->loaded_files[ $translation_file ][ $locale ] ) && array() !== $this->loaded_files[ $translation_file ][ $locale ] ) { $moe = reset( $this->loaded_files[ $translation_file ][ $locale ] ); } else { $moe = WP_Translation_File::create( $translation_file ); if ( false === $moe || null !== $moe->error() ) { $moe = false; } } $this->loaded_files[ $translation_file ][ $locale ][ $textdomain ] = $moe; if ( ! $moe instanceof WP_Translation_File ) { return false; } if ( ! isset( $this->loaded_translations[ $locale ][ $textdomain ] ) ) { $this->loaded_translations[ $locale ][ $textdomain ] = array(); } $this->loaded_translations[ $locale ][ $textdomain ][] = $moe; return true; } /** * Unloads a translation file for a given text domain. * * @since 6.5.0 * * @param WP_Translation_File|string $file Translation file instance or file name. * @param string $textdomain Optional. Text domain. Default 'default'. * @param string $locale Optional. Locale. Defaults to all locales. * @return bool True on success, false otherwise. */ public function unload_file( $file, string $textdomain = 'default', string $locale = null ): bool { if ( is_string( $file ) ) { $file = realpath( $file ); } if ( null !== $locale ) { if ( isset( $this->loaded_translations[ $locale ][ $textdomain ] ) ) { foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $i => $moe ) { if ( $file === $moe || $file === $moe->get_file() ) { unset( $this->loaded_translations[ $locale ][ $textdomain ][ $i ] ); unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] ); return true; } } } return true; } foreach ( $this->loaded_translations as $l => $domains ) { if ( ! isset( $domains[ $textdomain ] ) ) { continue; } foreach ( $domains[ $textdomain ] as $i => $moe ) { if ( $file === $moe || $file === $moe->get_file() ) { unset( $this->loaded_translations[ $l ][ $textdomain ][ $i ] ); unset( $this->loaded_files[ $moe->get_file() ][ $l ][ $textdomain ] ); return true; } } } return false; } /** * Unloads all translation files for a given text domain. * * @since 6.5.0 * * @param string $textdomain Optional. Text domain. Default 'default'. * @param string $locale Optional. Locale. Defaults to all locales. * @return bool True on success, false otherwise. */ public function unload_textdomain( string $textdomain = 'default', string $locale = null ): bool { $unloaded = false; if ( null !== $locale ) { if ( isset( $this->loaded_translations[ $locale ][ $textdomain ] ) ) { $unloaded = true; foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $moe ) { unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] ); } } unset( $this->loaded_translations[ $locale ][ $textdomain ] ); return $unloaded; } foreach ( $this->loaded_translations as $l => $domains ) { if ( ! isset( $domains[ $textdomain ] ) ) { continue; } $unloaded = true; foreach ( $domains[ $textdomain ] as $moe ) { unset( $this->loaded_files[ $moe->get_file() ][ $l ][ $textdomain ] ); } unset( $this->loaded_translations[ $l ][ $textdomain ] ); } return $unloaded; } /** * Determines whether translations are loaded for a given text domain. * * @since 6.5.0 * * @param string $textdomain Optional. Text domain. Default 'default'. * @param string $locale Optional. Locale. Default current locale. * @return bool True if there are any loaded translations, false otherwise. */ public function is_textdomain_loaded( string $textdomain = 'default', string $locale = null ): bool { if ( null === $locale ) { $locale = $this->current_locale; } return isset( $this->loaded_translations[ $locale ][ $textdomain ] ) && array() !== $this->loaded_translations[ $locale ][ $textdomain ]; } /** * Translates a singular string. * * @since 6.5.0 * * @param string $text Text to translate. * @param string $context Optional. Context for the string. Default empty string. * @param string $textdomain Optional. Text domain. Default 'default'. * @param string $locale Optional. Locale. Default current locale. * @return string|false Translation on success, false otherwise. */ public function translate( string $text, string $context = '', string $textdomain = 'default', string $locale = null ) { if ( '' !== $context ) { $context .= "\4"; } $translation = $this->locate_translation( "{$context}{$text}", $textdomain, $locale ); if ( false === $translation ) { return false; } return $translation['entries'][0]; } /** * Translates plurals. * * Checks both singular+plural combinations as well as just singulars, * in case the translation file does not store the plural. * * @since 6.5.0 * * @param array{0: string, 1: string} $plurals { * Pair of singular and plural translations. * * @type string $0 Singular translation. * @type string $1 Plural translation. * } * @param int $number Number of items. * @param string $context Optional. Context for the string. Default empty string. * @param string $textdomain Optional. Text domain. Default 'default'. * @param string $locale Optional. Locale. Default current locale. * @return string|false Translation on success, false otherwise. */ public function translate_plural( array $plurals, int $number, string $context = '', string $textdomain = 'default', string $locale = null ) { if ( '' !== $context ) { $context .= "\4"; } $text = implode( "\0", $plurals ); $translation = $this->locate_translation( "{$context}{$text}", $textdomain, $locale ); if ( false === $translation ) { $text = $plurals[0]; $translation = $this->locate_translation( "{$context}{$text}", $textdomain, $locale ); if ( false === $translation ) { return false; } } /** @var WP_Translation_File $source */ $source = $translation['source']; $num = $source->get_plural_form( $number ); // See \Translations::translate_plural(). return $translation['entries'][ $num ] ?? $translation['entries'][0]; } /** * Returns all existing headers for a given text domain. * * @since 6.5.0 * * @param string $textdomain Optional. Text domain. Default 'default'. * @return array Headers. */ public function get_headers( string $textdomain = 'default' ): array { if ( array() === $this->loaded_translations ) { return array(); } $headers = array(); foreach ( $this->get_files( $textdomain ) as $moe ) { foreach ( $moe->headers() as $header => $value ) { $headers[ $this->normalize_header( $header ) ] = $value; } } return $headers; } /** * Normalizes header names to be capitalized. * * @since 6.5.0 * * @param string $header Header name. * @return string Normalized header name. */ protected function normalize_header( string $header ): string { $parts = explode( '-', $header ); $parts = array_map( 'ucfirst', $parts ); return implode( '-', $parts ); } /** * Returns all entries for a given text domain. * * @since 6.5.0 * * @param string $textdomain Optional. Text domain. Default 'default'. * @return array Entries. */ public function get_entries( string $textdomain = 'default' ): array { if ( array() === $this->loaded_translations ) { return array(); } $entries = array(); foreach ( $this->get_files( $textdomain ) as $moe ) { $entries = array_merge( $entries, $moe->entries() ); } return $entries; } /** * Locates translation for a given string and text domain. * * @since 6.5.0 * * @param string $singular Singular translation. * @param string $textdomain Optional. Text domain. Default 'default'. * @param string $locale Optional. Locale. Default current locale. * @return array{source: WP_Translation_File, entries: string[]}|false { * Translations on success, false otherwise. * * @type WP_Translation_File $source Translation file instance. * @type string[] $entries Array of translation entries. * } */ protected function locate_translation( string $singular, string $textdomain = 'default', string $locale = null ) { if ( array() === $this->loaded_translations ) { return false; } // Find the translation in all loaded files for this text domain. foreach ( $this->get_files( $textdomain, $locale ) as $moe ) { $translation = $moe->translate( $singular ); if ( false !== $translation ) { return array( 'entries' => explode( "\0", $translation ), 'source' => $moe, ); } if ( null !== $moe->error() ) { // Unload this file, something is wrong. $this->unload_file( $moe, $textdomain, $locale ); } } // Nothing could be found. return false; } /** * Returns all translation files for a given text domain. * * @since 6.5.0 * * @param string $textdomain Optional. Text domain. Default 'default'. * @param string $locale Optional. Locale. Default current locale. * @return WP_Translation_File[] List of translation files. */ protected function get_files( string $textdomain = 'default', string $locale = null ): array { if ( null === $locale ) { $locale = $this->current_locale; } return $this->loaded_translations[ $locale ][ $textdomain ] ?? array(); } } /** * Locale API: WP_Locale class * * @package WordPress * @subpackage i18n * @since 4.6.0 */ /** * Core class used to store translated data for a locale. * * @since 2.1.0 * @since 4.6.0 Moved to its own file from wp-includes/locale.php. */ #[AllowDynamicProperties] class WP_Locale { /** * Stores the translated strings for the full weekday names. * * @since 2.1.0 * @since 6.2.0 Initialized to an empty array. * @var string[] */ public $weekday = array(); /** * Stores the translated strings for the one character weekday names. * * There is a hack to make sure that Tuesday and Thursday, as well * as Sunday and Saturday, don't conflict. See init() method for more. * * @see WP_Locale::init() for how to handle the hack. * * @since 2.1.0 * @since 6.2.0 Initialized to an empty array. * @var string[] */ public $weekday_initial = array(); /** * Stores the translated strings for the abbreviated weekday names. * * @since 2.1.0 * @since 6.2.0 Initialized to an empty array. * @var string[] */ public $weekday_abbrev = array(); /** * Stores the translated strings for the full month names. * * @since 2.1.0 * @since 6.2.0 Initialized to an empty array. * @var string[] */ public $month = array(); /** * Stores the translated strings for the month names in genitive case, if the locale specifies. * * @since 4.4.0 * @since 6.2.0 Initialized to an empty array. * @var string[] */ public $month_genitive = array(); /** * Stores the translated strings for the abbreviated month names. * * @since 2.1.0 * @since 6.2.0 Initialized to an empty array. * @var string[] */ public $month_abbrev = array(); /** * Stores the translated strings for 'am' and 'pm'. * * Also the capitalized versions. * * @since 2.1.0 * @since 6.2.0 Initialized to an empty array. * @var string[] */ public $meridiem = array(); /** * The text direction of the locale language. * * Default is left to right 'ltr'. * * @since 2.1.0 * @var string */ public $text_direction = 'ltr'; /** * The thousands separator and decimal point values used for localizing numbers. * * @since 2.3.0 * @since 6.2.0 Initialized to an empty array. * @var array */ public $number_format = array(); /** * The separator string used for localizing list item separator. * * @since 6.0.0 * @var string */ public $list_item_separator; /** * The word count type of the locale language. * * Default is 'words'. * * @since 6.2.0 * @var string */ public $word_count_type; /** * Constructor which calls helper methods to set up object variables. * * @since 2.1.0 */ public function __construct() { $this->init(); $this->register_globals(); } /** * Sets up the translated strings and object properties. * * The method creates the translatable strings for various * calendar elements. Which allows for specifying locale * specific calendar names and text direction. * * @since 2.1.0 * * @global string $text_direction */ public function init() { // The weekdays. $this->weekday[0] = /* translators: Weekday. */ __( 'Sunday' ); $this->weekday[1] = /* translators: Weekday. */ __( 'Monday' ); $this->weekday[2] = /* translators: Weekday. */ __( 'Tuesday' ); $this->weekday[3] = /* translators: Weekday. */ __( 'Wednesday' ); $this->weekday[4] = /* translators: Weekday. */ __( 'Thursday' ); $this->weekday[5] = /* translators: Weekday. */ __( 'Friday' ); $this->weekday[6] = /* translators: Weekday. */ __( 'Saturday' ); // The first letter of each day. $this->weekday_initial[ $this->weekday[0] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Sunday initial' ); $this->weekday_initial[ $this->weekday[1] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'M', 'Monday initial' ); $this->weekday_initial[ $this->weekday[2] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Tuesday initial' ); $this->weekday_initial[ $this->weekday[3] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'W', 'Wednesday initial' ); $this->weekday_initial[ $this->weekday[4] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'T', 'Thursday initial' ); $this->weekday_initial[ $this->weekday[5] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'F', 'Friday initial' ); $this->weekday_initial[ $this->weekday[6] ] = /* translators: One-letter abbreviation of the weekday. */ _x( 'S', 'Saturday initial' ); // Abbreviations for each day. $this->weekday_abbrev[ $this->weekday[0] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Sun' ); $this->weekday_abbrev[ $this->weekday[1] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Mon' ); $this->weekday_abbrev[ $this->weekday[2] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Tue' ); $this->weekday_abbrev[ $this->weekday[3] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Wed' ); $this->weekday_abbrev[ $this->weekday[4] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Thu' ); $this->weekday_abbrev[ $this->weekday[5] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Fri' ); $this->weekday_abbrev[ $this->weekday[6] ] = /* translators: Three-letter abbreviation of the weekday. */ __( 'Sat' ); // The months. $this->month['01'] = /* translators: Month name. */ __( 'January' ); $this->month['02'] = /* translators: Month name. */ __( 'February' ); $this->month['03'] = /* translators: Month name. */ __( 'March' ); $this->month['04'] = /* translators: Month name. */ __( 'April' ); $this->month['05'] = /* translators: Month name. */ __( 'May' ); $this->month['06'] = /* translators: Month name. */ __( 'June' ); $this->month['07'] = /* translators: Month name. */ __( 'July' ); $this->month['08'] = /* translators: Month name. */ __( 'August' ); $this->month['09'] = /* translators: Month name. */ __( 'September' ); $this->month['10'] = /* translators: Month name. */ __( 'October' ); $this->month['11'] = /* translators: Month name. */ __( 'November' ); $this->month['12'] = /* translators: Month name. */ __( 'December' ); // The months, genitive. $this->month_genitive['01'] = /* translators: Month name, genitive. */ _x( 'January', 'genitive' ); $this->month_genitive['02'] = /* translators: Month name, genitive. */ _x( 'February', 'genitive' ); $this->month_genitive['03'] = /* translators: Month name, genitive. */ _x( 'March', 'genitive' ); $this->month_genitive['04'] = /* translators: Month name, genitive. */ _x( 'April', 'genitive' ); $this->month_genitive['05'] = /* translators: Month name, genitive. */ _x( 'May', 'genitive' ); $this->month_genitive['06'] = /* translators: Month name, genitive. */ _x( 'June', 'genitive' ); $this->month_genitive['07'] = /* translators: Month name, genitive. */ _x( 'July', 'genitive' ); $this->month_genitive['08'] = /* translators: Month name, genitive. */ _x( 'August', 'genitive' ); $this->month_genitive['09'] = /* translators: Month name, genitive. */ _x( 'September', 'genitive' ); $this->month_genitive['10'] = /* translators: Month name, genitive. */ _x( 'October', 'genitive' ); $this->month_genitive['11'] = /* translators: Month name, genitive. */ _x( 'November', 'genitive' ); $this->month_genitive['12'] = /* translators: Month name, genitive. */ _x( 'December', 'genitive' ); // Abbreviations for each month. $this->month_abbrev[ $this->month['01'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jan', 'January abbreviation' ); $this->month_abbrev[ $this->month['02'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Feb', 'February abbreviation' ); $this->month_abbrev[ $this->month['03'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Mar', 'March abbreviation' ); $this->month_abbrev[ $this->month['04'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Apr', 'April abbreviation' ); $this->month_abbrev[ $this->month['05'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'May', 'May abbreviation' ); $this->month_abbrev[ $this->month['06'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jun', 'June abbreviation' ); $this->month_abbrev[ $this->month['07'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Jul', 'July abbreviation' ); $this->month_abbrev[ $this->month['08'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Aug', 'August abbreviation' ); $this->month_abbrev[ $this->month['09'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Sep', 'September abbreviation' ); $this->month_abbrev[ $this->month['10'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Oct', 'October abbreviation' ); $this->month_abbrev[ $this->month['11'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Nov', 'November abbreviation' ); $this->month_abbrev[ $this->month['12'] ] = /* translators: Three-letter abbreviation of the month. */ _x( 'Dec', 'December abbreviation' ); // The meridiems. $this->meridiem['am'] = __( 'am' ); $this->meridiem['pm'] = __( 'pm' ); $this->meridiem['AM'] = __( 'AM' ); $this->meridiem['PM'] = __( 'PM' ); /* * Numbers formatting. * See https://www.php.net/number_format */ /* translators: $thousands_sep argument for https://www.php.net/number_format, default is ',' */ $thousands_sep = __( 'number_format_thousands_sep' ); // Replace space with a non-breaking space to avoid wrapping. $thousands_sep = str_replace( ' ', ' ', $thousands_sep ); $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep; /* translators: $dec_point argument for https://www.php.net/number_format, default is '.' */ $decimal_point = __( 'number_format_decimal_point' ); $this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point; /* translators: Used between list items, there is a space after the comma. */ $this->list_item_separator = __( ', ' ); // Set text direction. if ( isset( $GLOBALS['text_direction'] ) ) { $this->text_direction = $GLOBALS['text_direction']; /* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */ } elseif ( 'rtl' === _x( 'ltr', 'text direction' ) ) { $this->text_direction = 'rtl'; } // Set the word count type. $this->word_count_type = $this->get_word_count_type(); } /** * Retrieves the full translated weekday word. * * Week starts on translated Sunday and can be fetched * by using 0 (zero). So the week starts with 0 (zero) * and ends on Saturday with is fetched by using 6 (six). * * @since 2.1.0 * * @param int $weekday_number 0 for Sunday through 6 Saturday. * @return string Full translated weekday. */ public function get_weekday( $weekday_number ) { return $this->weekday[ $weekday_number ]; } /** * Retrieves the translated weekday initial. * * The weekday initial is retrieved by the translated * full weekday word. When translating the weekday initial * pay attention to make sure that the starting letter does * not conflict. * * @since 2.1.0 * * @param string $weekday_name Full translated weekday word. * @return string Translated weekday initial. */ public function get_weekday_initial( $weekday_name ) { return $this->weekday_initial[ $weekday_name ]; } /** * Retrieves the translated weekday abbreviation. * * The weekday abbreviation is retrieved by the translated * full weekday word. * * @since 2.1.0 * * @param string $weekday_name Full translated weekday word. * @return string Translated weekday abbreviation. */ public function get_weekday_abbrev( $weekday_name ) { return $this->weekday_abbrev[ $weekday_name ]; } /** * Retrieves the full translated month by month number. * * The $month_number parameter has to be a string * because it must have the '0' in front of any number * that is less than 10. Starts from '01' and ends at * '12'. * * You can use an integer instead and it will add the * '0' before the numbers less than 10 for you. * * @since 2.1.0 * * @param string|int $month_number '01' through '12'. * @return string Translated full month name. */ public function get_month( $month_number ) { return $this->month[ zeroise( $month_number, 2 ) ]; } /** * Retrieves translated version of month abbreviation string. * * The $month_name parameter is expected to be the translated or * translatable version of the month. * * @since 2.1.0 * * @param string $month_name Translated month to get abbreviated version. * @return string Translated abbreviated month. */ public function get_month_abbrev( $month_name ) { return $this->month_abbrev[ $month_name ]; } /** * Retrieves translated version of meridiem string. * * The $meridiem parameter is expected to not be translated. * * @since 2.1.0 * * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version. * @return string Translated version */ public function get_meridiem( $meridiem ) { return $this->meridiem[ $meridiem ]; } /** * Global variables are deprecated. * * For backward compatibility only. * * @deprecated For backward compatibility only. * * @global array $weekday * @global array $weekday_initial * @global array $weekday_abbrev * @global array $month * @global array $month_abbrev * * @since 2.1.0 */ public function register_globals() { $GLOBALS['weekday'] = $this->weekday; $GLOBALS['weekday_initial'] = $this->weekday_initial; $GLOBALS['weekday_abbrev'] = $this->weekday_abbrev; $GLOBALS['month'] = $this->month; $GLOBALS['month_abbrev'] = $this->month_abbrev; } /** * Checks if current locale is RTL. * * @since 3.0.0 * @return bool Whether locale is RTL. */ public function is_rtl() { return 'rtl' === $this->text_direction; } /** * Registers date/time format strings for general POT. * * Private, unused method to add some date/time formats translated * on wp-admin/options-general.php to the general POT that would * otherwise be added to the admin POT. * * @since 3.6.0 */ public function _strings_for_pot() { /* translators: Localized date format, see https://www.php.net/manual/datetime.format.php */ __( 'F j, Y' ); /* translators: Localized time format, see https://www.php.net/manual/datetime.format.php */ __( 'g:i a' ); /* translators: Localized date and time format, see https://www.php.net/manual/datetime.format.php */ __( 'F j, Y g:i a' ); } /** * Retrieves the localized list item separator. * * @since 6.0.0 * * @return string Localized list item separator. */ public function get_list_item_separator() { return $this->list_item_separator; } /** * Retrieves the localized word count type. * * @since 6.2.0 * * @return string Localized word count type. Possible values are `characters_excluding_spaces`, * `characters_including_spaces`, or `words`. Defaults to `words`. */ public function get_word_count_type() { /* * translators: If your word count is based on single characters (e.g. East Asian characters), * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. * Do not translate into your own language. */ $word_count_type = is_null( $this->word_count_type ) ? _x( 'words', 'Word count type. Do not translate!' ) : $this->word_count_type; // Check for valid types. if ( 'characters_excluding_spaces' !== $word_count_type && 'characters_including_spaces' !== $word_count_type ) { // Defaults to 'words'. $word_count_type = 'words'; } return $word_count_type; } } /** * Toolbar API: Top-level Toolbar functionality * * @package WordPress * @subpackage Toolbar * @since 3.1.0 */ /** * Instantiates the admin bar object and set it up as a global for access elsewhere. * * UNHOOKING THIS FUNCTION WILL NOT PROPERLY REMOVE THE ADMIN BAR. * For that, use show_admin_bar(false) or the {@see 'show_admin_bar'} filter. * * @since 3.1.0 * @access private * * @global WP_Admin_Bar $wp_admin_bar * * @return bool Whether the admin bar was successfully initialized. */ function _wp_admin_bar_init() { global $wp_admin_bar; if ( ! is_admin_bar_showing() ) { return false; } /* Load the admin bar class code ready for instantiation */ require_once ABSPATH . WPINC . '/class-wp-admin-bar.php'; /* Instantiate the admin bar */ /** * Filters the admin bar class to instantiate. * * @since 3.1.0 * * @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'. */ $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' ); if ( class_exists( $admin_bar_class ) ) { $wp_admin_bar = new $admin_bar_class(); } else { return false; } $wp_admin_bar->initialize(); $wp_admin_bar->add_menus(); return true; } /** * Renders the admin bar to the page based on the $wp_admin_bar->menu member var. * * This is called very early on the {@see 'wp_body_open'} action so that it will render * before anything else being added to the page body. * * For backward compatibility with themes not using the 'wp_body_open' action, * the function is also called late on {@see 'wp_footer'}. * * It includes the {@see 'admin_bar_menu'} action which should be used to hook in and * add new menus to the admin bar. That way you can be sure that you are adding at most * optimal point, right before the admin bar is rendered. This also gives you access to * the `$post` global, among others. * * @since 3.1.0 * @since 5.4.0 Called on 'wp_body_open' action first, with 'wp_footer' as a fallback. * * @global WP_Admin_Bar $wp_admin_bar */ function wp_admin_bar_render() { global $wp_admin_bar; static $rendered = false; if ( $rendered ) { return; } if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) ) { return; } /** * Loads all necessary admin bar items. * * This is the hook used to add, remove, or manipulate admin bar items. * * @since 3.1.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance, passed by reference. */ do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) ); /** * Fires before the admin bar is rendered. * * @since 3.1.0 */ do_action( 'wp_before_admin_bar_render' ); $wp_admin_bar->render(); /** * Fires after the admin bar is rendered. * * @since 3.1.0 */ do_action( 'wp_after_admin_bar_render' ); $rendered = true; } /** * Adds the WordPress logo menu. * * @since 3.3.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_wp_menu( $wp_admin_bar ) { if ( current_user_can( 'read' ) ) { $about_url = self_admin_url( 'about.php' ); $contribute_url = self_admin_url( 'contribute.php' ); } elseif ( is_multisite() ) { $about_url = get_dashboard_url( get_current_user_id(), 'about.php' ); $contribute_url = get_dashboard_url( get_current_user_id(), 'contribute.php' ); } else { $about_url = false; $contribute_url = false; } $wp_logo_menu_args = array( 'id' => 'wp-logo', 'title' => '' . /* translators: Hidden accessibility text. */ __( 'About WordPress' ) . '', 'href' => $about_url, 'meta' => array( 'menu_title' => __( 'About WordPress' ), ), ); // Set tabindex="0" to make sub menus accessible when no URL is available. if ( ! $about_url ) { $wp_logo_menu_args['meta'] = array( 'tabindex' => 0, ); } $wp_admin_bar->add_node( $wp_logo_menu_args ); if ( $about_url ) { // Add "About WordPress" link. $wp_admin_bar->add_node( array( 'parent' => 'wp-logo', 'id' => 'about', 'title' => __( 'About WordPress' ), 'href' => $about_url, ) ); } if ( $contribute_url ) { // Add contribute link. $wp_admin_bar->add_node( array( 'parent' => 'wp-logo', 'id' => 'contribute', 'title' => __( 'Get Involved' ), 'href' => $contribute_url, ) ); } // Add WordPress.org link. $wp_admin_bar->add_node( array( 'parent' => 'wp-logo-external', 'id' => 'wporg', 'title' => __( 'WordPress.org' ), 'href' => __( 'https://wordpress.org/' ), ) ); // Add documentation link. $wp_admin_bar->add_node( array( 'parent' => 'wp-logo-external', 'id' => 'documentation', 'title' => __( 'Documentation' ), 'href' => __( 'https://wordpress.org/documentation/' ), ) ); // Add learn link. $wp_admin_bar->add_node( array( 'parent' => 'wp-logo-external', 'id' => 'learn', 'title' => __( 'Learn WordPress' ), 'href' => 'https://learn.wordpress.org/', ) ); // Add forums link. $wp_admin_bar->add_node( array( 'parent' => 'wp-logo-external', 'id' => 'support-forums', 'title' => __( 'Support' ), 'href' => __( 'https://wordpress.org/support/forums/' ), ) ); // Add feedback link. $wp_admin_bar->add_node( array( 'parent' => 'wp-logo-external', 'id' => 'feedback', 'title' => __( 'Feedback' ), 'href' => __( 'https://wordpress.org/support/forum/requests-and-feedback' ), ) ); } /** * Adds the sidebar toggle button. * * @since 3.8.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_sidebar_toggle( $wp_admin_bar ) { if ( is_admin() ) { $wp_admin_bar->add_node( array( 'id' => 'menu-toggle', 'title' => '' . /* translators: Hidden accessibility text. */ __( 'Menu' ) . '', 'href' => '#', ) ); } } /** * Adds the "My Account" item. * * @since 3.3.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_my_account_item( $wp_admin_bar ) { $user_id = get_current_user_id(); $current_user = wp_get_current_user(); if ( ! $user_id ) { return; } if ( current_user_can( 'read' ) ) { $profile_url = get_edit_profile_url( $user_id ); } elseif ( is_multisite() ) { $profile_url = get_dashboard_url( $user_id, 'profile.php' ); } else { $profile_url = false; } $avatar = get_avatar( $user_id, 26 ); /* translators: %s: Current user's display name. */ $howdy = sprintf( __( 'Howdy, %s' ), '' . $current_user->display_name . '' ); $class = empty( $avatar ) ? '' : 'with-avatar'; $wp_admin_bar->add_node( array( 'id' => 'my-account', 'parent' => 'top-secondary', 'title' => $howdy . $avatar, 'href' => $profile_url, 'meta' => array( 'class' => $class, /* translators: %s: Current user's display name. */ 'menu_title' => sprintf( __( 'Howdy, %s' ), $current_user->display_name ), 'tabindex' => ( false !== $profile_url ) ? '' : 0, ), ) ); } /** * Adds the "My Account" submenu items. * * @since 3.1.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_my_account_menu( $wp_admin_bar ) { $user_id = get_current_user_id(); $current_user = wp_get_current_user(); if ( ! $user_id ) { return; } if ( current_user_can( 'read' ) ) { $profile_url = get_edit_profile_url( $user_id ); } elseif ( is_multisite() ) { $profile_url = get_dashboard_url( $user_id, 'profile.php' ); } else { $profile_url = false; } $wp_admin_bar->add_group( array( 'parent' => 'my-account', 'id' => 'user-actions', ) ); $user_info = get_avatar( $user_id, 64 ); $user_info .= "{$current_user->display_name}"; if ( $current_user->display_name !== $current_user->user_login ) { $user_info .= "{$current_user->user_login}"; } if ( false !== $profile_url ) { $user_info .= "" . __( 'Edit Profile' ) . ''; } $wp_admin_bar->add_node( array( 'parent' => 'user-actions', 'id' => 'user-info', 'title' => $user_info, 'href' => $profile_url, ) ); $wp_admin_bar->add_node( array( 'parent' => 'user-actions', 'id' => 'logout', 'title' => __( 'Log Out' ), 'href' => wp_logout_url(), ) ); } /** * Adds the "Site Name" menu. * * @since 3.3.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_site_menu( $wp_admin_bar ) { // Don't show for logged out users. if ( ! is_user_logged_in() ) { return; } // Show only when the user is a member of this site, or they're a super admin. if ( ! is_user_member_of_blog() && ! current_user_can( 'manage_network' ) ) { return; } $blogname = get_bloginfo( 'name' ); if ( ! $blogname ) { $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() ); } if ( is_network_admin() ) { /* translators: %s: Site title. */ $blogname = sprintf( __( 'Network Admin: %s' ), esc_html( get_network()->site_name ) ); } elseif ( is_user_admin() ) { /* translators: %s: Site title. */ $blogname = sprintf( __( 'User Dashboard: %s' ), esc_html( get_network()->site_name ) ); } $title = wp_html_excerpt( $blogname, 40, '…' ); $wp_admin_bar->add_node( array( 'id' => 'site-name', 'title' => $title, 'href' => ( is_admin() || ! current_user_can( 'read' ) ) ? home_url( '/' ) : admin_url(), 'meta' => array( 'menu_title' => $title, ), ) ); // Create submenu items. if ( is_admin() ) { // Add an option to visit the site. $wp_admin_bar->add_node( array( 'parent' => 'site-name', 'id' => 'view-site', 'title' => __( 'Visit Site' ), 'href' => home_url( '/' ), ) ); if ( is_blog_admin() && is_multisite() && current_user_can( 'manage_sites' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'site-name', 'id' => 'edit-site', 'title' => __( 'Edit Site' ), 'href' => network_admin_url( 'site-info.php?id=' . get_current_blog_id() ), ) ); } } elseif ( current_user_can( 'read' ) ) { // We're on the front end, link to the Dashboard. $wp_admin_bar->add_node( array( 'parent' => 'site-name', 'id' => 'dashboard', 'title' => __( 'Dashboard' ), 'href' => admin_url(), ) ); // Add the appearance submenu items. wp_admin_bar_appearance_menu( $wp_admin_bar ); // Add a Plugins link. if ( current_user_can( 'activate_plugins' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'site-name', 'id' => 'plugins', 'title' => __( 'Plugins' ), 'href' => admin_url( 'plugins.php' ), ) ); } } } /** * Adds the "Edit site" link to the Toolbar. * * @since 5.9.0 * @since 6.3.0 Added `$_wp_current_template_id` global for editing of current template directly from the admin bar. * * @global string $_wp_current_template_id * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_edit_site_menu( $wp_admin_bar ) { global $_wp_current_template_id; // Don't show if a block theme is not activated. if ( ! wp_is_block_theme() ) { return; } // Don't show for users who can't edit theme options or when in the admin. if ( ! current_user_can( 'edit_theme_options' ) || is_admin() ) { return; } $wp_admin_bar->add_node( array( 'id' => 'site-editor', 'title' => __( 'Edit site' ), 'href' => add_query_arg( array( 'postType' => 'wp_template', 'postId' => $_wp_current_template_id, ), admin_url( 'site-editor.php' ) ), ) ); } /** * Adds the "Customize" link to the Toolbar. * * @since 4.3.0 * * @global WP_Customize_Manager $wp_customize * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_customize_menu( $wp_admin_bar ) { global $wp_customize; // Don't show if a block theme is activated and no plugins use the customizer. if ( wp_is_block_theme() && ! has_action( 'customize_register' ) ) { return; } // Don't show for users who can't access the customizer or when in the admin. if ( ! current_user_can( 'customize' ) || is_admin() ) { return; } // Don't show if the user cannot edit a given customize_changeset post currently being previewed. if ( is_customize_preview() && $wp_customize->changeset_post_id() && ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->edit_post, $wp_customize->changeset_post_id() ) ) { return; } $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; if ( is_customize_preview() && $wp_customize->changeset_uuid() ) { $current_url = remove_query_arg( 'customize_changeset_uuid', $current_url ); } $customize_url = add_query_arg( 'url', urlencode( $current_url ), wp_customize_url() ); if ( is_customize_preview() ) { $customize_url = add_query_arg( array( 'changeset_uuid' => $wp_customize->changeset_uuid() ), $customize_url ); } $wp_admin_bar->add_node( array( 'id' => 'customize', 'title' => __( 'Customize' ), 'href' => $customize_url, 'meta' => array( 'class' => 'hide-if-no-customize', ), ) ); add_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' ); } /** * Adds the "My Sites/[Site Name]" menu and all submenus. * * @since 3.1.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_my_sites_menu( $wp_admin_bar ) { // Don't show for logged out users or single site mode. if ( ! is_user_logged_in() || ! is_multisite() ) { return; } // Show only when the user has at least one site, or they're a super admin. if ( count( $wp_admin_bar->user->blogs ) < 1 && ! current_user_can( 'manage_network' ) ) { return; } if ( $wp_admin_bar->user->active_blog ) { $my_sites_url = get_admin_url( $wp_admin_bar->user->active_blog->blog_id, 'my-sites.php' ); } else { $my_sites_url = admin_url( 'my-sites.php' ); } $wp_admin_bar->add_node( array( 'id' => 'my-sites', 'title' => __( 'My Sites' ), 'href' => $my_sites_url, ) ); if ( current_user_can( 'manage_network' ) ) { $wp_admin_bar->add_group( array( 'parent' => 'my-sites', 'id' => 'my-sites-super-admin', ) ); $wp_admin_bar->add_node( array( 'parent' => 'my-sites-super-admin', 'id' => 'network-admin', 'title' => __( 'Network Admin' ), 'href' => network_admin_url(), ) ); $wp_admin_bar->add_node( array( 'parent' => 'network-admin', 'id' => 'network-admin-d', 'title' => __( 'Dashboard' ), 'href' => network_admin_url(), ) ); if ( current_user_can( 'manage_sites' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'network-admin', 'id' => 'network-admin-s', 'title' => __( 'Sites' ), 'href' => network_admin_url( 'sites.php' ), ) ); } if ( current_user_can( 'manage_network_users' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'network-admin', 'id' => 'network-admin-u', 'title' => __( 'Users' ), 'href' => network_admin_url( 'users.php' ), ) ); } if ( current_user_can( 'manage_network_themes' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'network-admin', 'id' => 'network-admin-t', 'title' => __( 'Themes' ), 'href' => network_admin_url( 'themes.php' ), ) ); } if ( current_user_can( 'manage_network_plugins' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'network-admin', 'id' => 'network-admin-p', 'title' => __( 'Plugins' ), 'href' => network_admin_url( 'plugins.php' ), ) ); } if ( current_user_can( 'manage_network_options' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'network-admin', 'id' => 'network-admin-o', 'title' => __( 'Settings' ), 'href' => network_admin_url( 'settings.php' ), ) ); } } // Add site links. $wp_admin_bar->add_group( array( 'parent' => 'my-sites', 'id' => 'my-sites-list', 'meta' => array( 'class' => current_user_can( 'manage_network' ) ? 'ab-sub-secondary' : '', ), ) ); /** * Filters whether to show the site icons in toolbar. * * Returning false to this hook is the recommended way to hide site icons in the toolbar. * A truthy return may have negative performance impact on large multisites. * * @since 6.0.0 * * @param bool $show_site_icons Whether site icons should be shown in the toolbar. Default true. */ $show_site_icons = apply_filters( 'wp_admin_bar_show_site_icons', true ); foreach ( (array) $wp_admin_bar->user->blogs as $blog ) { switch_to_blog( $blog->userblog_id ); if ( true === $show_site_icons && has_site_icon() ) { $blavatar = sprintf( '', esc_url( get_site_icon_url( 16 ) ), esc_url( get_site_icon_url( 32 ) ), ( wp_lazy_loading_enabled( 'img', 'site_icon_in_toolbar' ) ? ' loading="lazy"' : '' ) ); } else { $blavatar = '
'; } $blogname = $blog->blogname; if ( ! $blogname ) { $blogname = preg_replace( '#^(https?://)?(www.)?#', '', get_home_url() ); } $menu_id = 'blog-' . $blog->userblog_id; if ( current_user_can( 'read' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'my-sites-list', 'id' => $menu_id, 'title' => $blavatar . $blogname, 'href' => admin_url(), ) ); $wp_admin_bar->add_node( array( 'parent' => $menu_id, 'id' => $menu_id . '-d', 'title' => __( 'Dashboard' ), 'href' => admin_url(), ) ); } else { $wp_admin_bar->add_node( array( 'parent' => 'my-sites-list', 'id' => $menu_id, 'title' => $blavatar . $blogname, 'href' => home_url(), ) ); } if ( current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) { $wp_admin_bar->add_node( array( 'parent' => $menu_id, 'id' => $menu_id . '-n', 'title' => get_post_type_object( 'post' )->labels->new_item, 'href' => admin_url( 'post-new.php' ), ) ); } if ( current_user_can( 'edit_posts' ) ) { $wp_admin_bar->add_node( array( 'parent' => $menu_id, 'id' => $menu_id . '-c', 'title' => __( 'Manage Comments' ), 'href' => admin_url( 'edit-comments.php' ), ) ); } $wp_admin_bar->add_node( array( 'parent' => $menu_id, 'id' => $menu_id . '-v', 'title' => __( 'Visit Site' ), 'href' => home_url( '/' ), ) ); restore_current_blog(); } } /** * Provides a shortlink. * * @since 3.1.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_shortlink_menu( $wp_admin_bar ) { $short = wp_get_shortlink( 0, 'query' ); $id = 'get-shortlink'; if ( empty( $short ) ) { return; } $html = ''; $wp_admin_bar->add_node( array( 'id' => $id, 'title' => __( 'Shortlink' ), 'href' => $short, 'meta' => array( 'html' => $html ), ) ); } /** * Provides an edit link for posts and terms. * * @since 3.1.0 * @since 5.5.0 Added a "View Post" link on Comments screen for a single post. * * @global WP_Term $tag * @global WP_Query $wp_the_query WordPress Query object. * @global int $user_id The ID of the user being edited. Not to be confused with the * global $user_ID, which contains the ID of the current user. * @global int $post_id The ID of the post when editing comments for a single post. * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_edit_menu( $wp_admin_bar ) { global $tag, $wp_the_query, $user_id, $post_id; if ( is_admin() ) { $current_screen = get_current_screen(); $post = get_post(); $post_type_object = null; if ( 'post' === $current_screen->base ) { $post_type_object = get_post_type_object( $post->post_type ); } elseif ( 'edit' === $current_screen->base ) { $post_type_object = get_post_type_object( $current_screen->post_type ); } elseif ( 'edit-comments' === $current_screen->base && $post_id ) { $post = get_post( $post_id ); if ( $post ) { $post_type_object = get_post_type_object( $post->post_type ); } } if ( ( 'post' === $current_screen->base || 'edit-comments' === $current_screen->base ) && 'add' !== $current_screen->action && ( $post_type_object ) && current_user_can( 'read_post', $post->ID ) && ( $post_type_object->public ) && ( $post_type_object->show_in_admin_bar ) ) { if ( 'draft' === $post->post_status ) { $preview_link = get_preview_post_link( $post ); $wp_admin_bar->add_node( array( 'id' => 'preview', 'title' => $post_type_object->labels->view_item, 'href' => esc_url( $preview_link ), 'meta' => array( 'target' => 'wp-preview-' . $post->ID ), ) ); } else { $wp_admin_bar->add_node( array( 'id' => 'view', 'title' => $post_type_object->labels->view_item, 'href' => get_permalink( $post->ID ), ) ); } } elseif ( 'edit' === $current_screen->base && ( $post_type_object ) && ( $post_type_object->public ) && ( $post_type_object->show_in_admin_bar ) && ( get_post_type_archive_link( $post_type_object->name ) ) && ! ( 'post' === $post_type_object->name && 'posts' === get_option( 'show_on_front' ) ) ) { $wp_admin_bar->add_node( array( 'id' => 'archive', 'title' => $post_type_object->labels->view_items, 'href' => get_post_type_archive_link( $current_screen->post_type ), ) ); } elseif ( 'term' === $current_screen->base && isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag ) ) { $tax = get_taxonomy( $tag->taxonomy ); if ( is_term_publicly_viewable( $tag ) ) { $wp_admin_bar->add_node( array( 'id' => 'view', 'title' => $tax->labels->view_item, 'href' => get_term_link( $tag ), ) ); } } elseif ( 'user-edit' === $current_screen->base && isset( $user_id ) ) { $user_object = get_userdata( $user_id ); $view_link = get_author_posts_url( $user_object->ID ); if ( $user_object->exists() && $view_link ) { $wp_admin_bar->add_node( array( 'id' => 'view', 'title' => __( 'View User' ), 'href' => $view_link, ) ); } } } else { $current_object = $wp_the_query->get_queried_object(); if ( empty( $current_object ) ) { return; } if ( ! empty( $current_object->post_type ) ) { $post_type_object = get_post_type_object( $current_object->post_type ); $edit_post_link = get_edit_post_link( $current_object->ID ); if ( $post_type_object && $edit_post_link && current_user_can( 'edit_post', $current_object->ID ) && $post_type_object->show_in_admin_bar ) { $wp_admin_bar->add_node( array( 'id' => 'edit', 'title' => $post_type_object->labels->edit_item, 'href' => $edit_post_link, ) ); } } elseif ( ! empty( $current_object->taxonomy ) ) { $tax = get_taxonomy( $current_object->taxonomy ); $edit_term_link = get_edit_term_link( $current_object->term_id, $current_object->taxonomy ); if ( $tax && $edit_term_link && current_user_can( 'edit_term', $current_object->term_id ) ) { $wp_admin_bar->add_node( array( 'id' => 'edit', 'title' => $tax->labels->edit_item, 'href' => $edit_term_link, ) ); } } elseif ( $current_object instanceof WP_User && current_user_can( 'edit_user', $current_object->ID ) ) { $edit_user_link = get_edit_user_link( $current_object->ID ); if ( $edit_user_link ) { $wp_admin_bar->add_node( array( 'id' => 'edit', 'title' => __( 'Edit User' ), 'href' => $edit_user_link, ) ); } } } } /** * Adds "Add New" menu. * * @since 3.1.0 * @since 6.5.0 Added a New Site link for network installations. * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_new_content_menu( $wp_admin_bar ) { $actions = array(); $cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' ); if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->create_posts ) ) { $actions['post-new.php'] = array( $cpts['post']->labels->name_admin_bar, 'new-post' ); } if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) ) { $actions['media-new.php'] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' ); } if ( current_user_can( 'manage_links' ) ) { $actions['link-add.php'] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' ); } if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->create_posts ) ) { $actions['post-new.php?post_type=page'] = array( $cpts['page']->labels->name_admin_bar, 'new-page' ); } unset( $cpts['post'], $cpts['page'], $cpts['attachment'] ); // Add any additional custom post types. foreach ( $cpts as $cpt ) { if ( ! current_user_can( $cpt->cap->create_posts ) ) { continue; } $key = 'post-new.php?post_type=' . $cpt->name; $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name ); } // Avoid clash with parent node and a 'content' post type. if ( isset( $actions['post-new.php?post_type=content'] ) ) { $actions['post-new.php?post_type=content'][1] = 'add-new-content'; } if ( current_user_can( 'create_users' ) || ( is_multisite() && current_user_can( 'promote_users' ) ) ) { $actions['user-new.php'] = array( _x( 'User', 'add new from admin bar' ), 'new-user' ); } if ( ! $actions ) { return; } $title = '' . _x( 'New', 'admin bar menu group label' ) . ''; $wp_admin_bar->add_node( array( 'id' => 'new-content', 'title' => $title, 'href' => admin_url( current( array_keys( $actions ) ) ), 'meta' => array( 'menu_title' => _x( 'New', 'admin bar menu group label' ), ), ) ); foreach ( $actions as $link => $action ) { list( $title, $id ) = $action; $wp_admin_bar->add_node( array( 'parent' => 'new-content', 'id' => $id, 'title' => $title, 'href' => admin_url( $link ), ) ); } if ( is_multisite() && current_user_can( 'create_sites' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'new-content', 'id' => 'add-new-site', 'title' => _x( 'Site', 'add new from admin bar' ), 'href' => network_admin_url( 'site-new.php' ), ) ); } } /** * Adds edit comments link with awaiting moderation count bubble. * * @since 3.1.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_comments_menu( $wp_admin_bar ) { if ( ! current_user_can( 'edit_posts' ) ) { return; } $awaiting_mod = wp_count_comments(); $awaiting_mod = $awaiting_mod->moderated; $awaiting_text = sprintf( /* translators: Hidden accessibility text. %s: Number of comments. */ _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod ), number_format_i18n( $awaiting_mod ) ); $icon = ''; $title = ''; $title .= '' . $awaiting_text . ''; $wp_admin_bar->add_node( array( 'id' => 'comments', 'title' => $icon . $title, 'href' => admin_url( 'edit-comments.php' ), ) ); } /** * Adds appearance submenu items to the "Site Name" menu. * * @since 3.1.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_appearance_menu( $wp_admin_bar ) { $wp_admin_bar->add_group( array( 'parent' => 'site-name', 'id' => 'appearance', ) ); if ( current_user_can( 'switch_themes' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'appearance', 'id' => 'themes', 'title' => __( 'Themes' ), 'href' => admin_url( 'themes.php' ), ) ); } if ( ! current_user_can( 'edit_theme_options' ) ) { return; } if ( current_theme_supports( 'widgets' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'appearance', 'id' => 'widgets', 'title' => __( 'Widgets' ), 'href' => admin_url( 'widgets.php' ), ) ); } if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'appearance', 'id' => 'menus', 'title' => __( 'Menus' ), 'href' => admin_url( 'nav-menus.php' ), ) ); } if ( current_theme_supports( 'custom-background' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'appearance', 'id' => 'background', 'title' => _x( 'Background', 'custom background' ), 'href' => admin_url( 'themes.php?page=custom-background' ), 'meta' => array( 'class' => 'hide-if-customize', ), ) ); } if ( current_theme_supports( 'custom-header' ) ) { $wp_admin_bar->add_node( array( 'parent' => 'appearance', 'id' => 'header', 'title' => _x( 'Header', 'custom image header' ), 'href' => admin_url( 'themes.php?page=custom-header' ), 'meta' => array( 'class' => 'hide-if-customize', ), ) ); } } /** * Provides an update link if theme/plugin/core updates are available. * * @since 3.1.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_updates_menu( $wp_admin_bar ) { $update_data = wp_get_update_data(); if ( ! $update_data['counts']['total'] ) { return; } $updates_text = sprintf( /* translators: Hidden accessibility text. %s: Total number of updates available. */ _n( '%s update available', '%s updates available', $update_data['counts']['total'] ), number_format_i18n( $update_data['counts']['total'] ) ); $icon = ''; $title = ''; $title .= '' . $updates_text . ''; $wp_admin_bar->add_node( array( 'id' => 'updates', 'title' => $icon . $title, 'href' => network_admin_url( 'update-core.php' ), ) ); } /** * Adds search form. * * @since 3.3.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_search_menu( $wp_admin_bar ) { if ( is_admin() ) { return; } $form = '
'; $form .= ''; $form .= ''; $form .= ''; $form .= '
'; $wp_admin_bar->add_node( array( 'parent' => 'top-secondary', 'id' => 'search', 'title' => $form, 'meta' => array( 'class' => 'admin-bar-search', 'tabindex' => -1, ), ) ); } /** * Adds a link to exit recovery mode when Recovery Mode is active. * * @since 5.2.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_recovery_mode_menu( $wp_admin_bar ) { if ( ! wp_is_recovery_mode() ) { return; } $url = wp_login_url(); $url = add_query_arg( 'action', WP_Recovery_Mode::EXIT_ACTION, $url ); $url = wp_nonce_url( $url, WP_Recovery_Mode::EXIT_ACTION ); $wp_admin_bar->add_node( array( 'parent' => 'top-secondary', 'id' => 'recovery-mode', 'title' => __( 'Exit Recovery Mode' ), 'href' => $url, ) ); } /** * Adds secondary menus. * * @since 3.3.0 * * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. */ function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) { $wp_admin_bar->add_group( array( 'id' => 'top-secondary', 'meta' => array( 'class' => 'ab-top-secondary', ), ) ); $wp_admin_bar->add_group( array( 'parent' => 'wp-logo', 'id' => 'wp-logo-external', 'meta' => array( 'class' => 'ab-sub-secondary', ), ) ); } /** * Enqueues inline style to hide the admin bar when printing. * * @since 6.4.0 */ function wp_enqueue_admin_bar_header_styles() { // Back-compat for plugins that disable functionality by unhooking this action. $action = is_admin() ? 'admin_head' : 'wp_head'; if ( ! has_action( $action, 'wp_admin_bar_header' ) ) { return; } remove_action( $action, 'wp_admin_bar_header' ); wp_add_inline_style( 'admin-bar', '@media print { #wpadminbar { display:none; } }' ); } /** * Enqueues inline bump styles to make room for the admin bar. * * @since 6.4.0 */ function wp_enqueue_admin_bar_bump_styles() { if ( current_theme_supports( 'admin-bar' ) ) { $admin_bar_args = get_theme_support( 'admin-bar' ); $header_callback = $admin_bar_args[0]['callback']; } if ( empty( $header_callback ) ) { $header_callback = '_admin_bar_bump_cb'; } if ( '_admin_bar_bump_cb' !== $header_callback ) { return; } // Back-compat for plugins that disable functionality by unhooking this action. if ( ! has_action( 'wp_head', $header_callback ) ) { return; } remove_action( 'wp_head', $header_callback ); $css = ' @media screen { html { margin-top: 32px !important; } } @media screen and ( max-width: 782px ) { html { margin-top: 46px !important; } } '; wp_add_inline_style( 'admin-bar', $css ); } /** * Sets the display status of the admin bar. * * This can be called immediately upon plugin load. It does not need to be called * from a function hooked to the {@see 'init'} action. * * @since 3.1.0 * * @global bool $show_admin_bar * * @param bool $show Whether to allow the admin bar to show. */ function show_admin_bar( $show ) { global $show_admin_bar; $show_admin_bar = (bool) $show; } /** * Determines whether the admin bar should be showing. * * For more information on this and similar theme functions, check out * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ * Conditional Tags} article in the Theme Developer Handbook. * * @since 3.1.0 * * @global bool $show_admin_bar * @global string $pagenow The filename of the current screen. * * @return bool Whether the admin bar should be showing. */ function is_admin_bar_showing() { global $show_admin_bar, $pagenow; // For all these types of requests, we never want an admin bar. if ( defined( 'XMLRPC_REQUEST' ) || defined( 'DOING_AJAX' ) || defined( 'IFRAME_REQUEST' ) || wp_is_json_request() ) { return false; } if ( is_embed() ) { return false; } // Integrated into the admin. if ( is_admin() ) { return true; } if ( ! isset( $show_admin_bar ) ) { if ( ! is_user_logged_in() || 'wp-login.php' === $pagenow ) { $show_admin_bar = false; } else { $show_admin_bar = _get_admin_bar_pref(); } } /** * Filters whether to show the admin bar. * * Returning false to this hook is the recommended way to hide the admin bar. * The user's display preference is used for logged in users. * * @since 3.1.0 * * @param bool $show_admin_bar Whether the admin bar should be shown. Default false. */ $show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar ); return $show_admin_bar; } /** * Retrieves the admin bar display preference of a user. * * @since 3.1.0 * @access private * * @param string $context Context of this preference check. Defaults to 'front'. The 'admin' * preference is no longer used. * @param int $user Optional. ID of the user to check, defaults to 0 for current user. * @return bool Whether the admin bar should be showing for this user. */ function _get_admin_bar_pref( $context = 'front', $user = 0 ) { $pref = get_user_option( "show_admin_bar_{$context}", $user ); if ( false === $pref ) { return true; } return 'true' === $pref; }