From 6d6e2a13fe5b531c675e0f238e96edba06d28af8 Mon Sep 17 00:00:00 2001 From: HerrVigg Date: Sat, 8 Apr 2023 10:47:46 +0200 Subject: [PATCH 1/3] Add PHP type declarations in core --- src/class_translator.php | 10 +++++----- src/compatibility.php | 12 ++++++------ src/date_time.php | 28 +++++++++++++-------------- src/default_language_config.php | 16 ++++++++-------- src/frontend.php | 30 +++++++++++++++-------------- src/hooks.php | 8 ++++---- src/i18n-interface.php | 10 +++++----- src/init.php | 2 +- src/language_blocks.php | 31 +++++++++++++++--------------- src/language_config.php | 12 ++++++------ src/language_detect.php | 18 ++++++++--------- src/options.php | 34 ++++++++++++++++----------------- src/rest_api.php | 2 +- src/taxonomy.php | 6 +++--- src/url.php | 34 ++++++++++++++++----------------- src/utils.php | 4 ++-- src/widget.php | 8 ++++---- 17 files changed, 134 insertions(+), 131 deletions(-) diff --git a/src/class_translator.php b/src/class_translator.php index aa8b7fc6..817dbce5 100644 --- a/src/class_translator.php +++ b/src/class_translator.php @@ -27,13 +27,13 @@ public static function get_translator() { return $q_config['translator']; } - public function get_language() { + public function get_language(): string { global $q_config; return $q_config['language']; } - public function set_language( $lang ) { + public function set_language( string $lang ): string { global $q_config; $lang_curr = $q_config['language']; if ( qtranxf_isEnabled( $lang ) ) { @@ -43,7 +43,7 @@ public function set_language( $lang ) { return $lang_curr; } - public function translate_text( $text, $lang = null, $flags = 0 ) { + public function translate_text( $text, ?string $lang = null, int $flags = 0 ): string { global $q_config; if ( ! $lang ) { $lang = $q_config['language']; @@ -54,7 +54,7 @@ public function translate_text( $text, $lang = null, $flags = 0 ) { return qtranxf_use( $lang, $text, $show_available, $show_empty ); } - public function translate_term( $term, $lang = null, $taxonomy = null ) { + public function translate_term( $term, ?string $lang = null, ?string $taxonomy = null ): string { global $q_config; if ( ! $lang ) { $lang = $q_config['language']; @@ -63,7 +63,7 @@ public function translate_term( $term, $lang = null, $taxonomy = null ) { return qtranxf_term_use( $lang, $term, $taxonomy ); } - public function translate_url( $url, $lang = null ) { + public function translate_url( $url, ?string $lang = null ): string { global $q_config; if ( $lang ) { $showLanguage = true; diff --git a/src/compatibility.php b/src/compatibility.php index b6477292..78e6ced7 100644 --- a/src/compatibility.php +++ b/src/compatibility.php @@ -36,22 +36,22 @@ function qtrans_getAvailableLanguages( $text ) { } if ( ! function_exists( 'qtrans_getLanguage' ) ) { - function qtrans_getLanguage() { + function qtrans_getLanguage(): string { return qtranxf_getLanguage(); } } if ( ! function_exists( 'qtrans_getLanguageName' ) ) { - function qtrans_getLanguageName( $lang = '' ) { + function qtrans_getLanguageName( string $lang = '' ): string { return qtranxf_getLanguageNameNative( $lang ); } } if ( ! function_exists( 'qtrans_getSortedLanguages' ) ) { - function qtrans_getSortedLanguages( $reverse = false ) { + function qtrans_getSortedLanguages( bool $reverse = false ): array { return qtranxf_getSortedLanguages( $reverse ); } } if ( ! function_exists( 'qtrans_join' ) ) { - function qtrans_join( $texts ) { + function qtrans_join( $texts ): string { if ( ! is_array( $texts ) ) { $texts = qtranxf_split( $texts ); } @@ -60,12 +60,12 @@ function qtrans_join( $texts ) { } } if ( ! function_exists( 'qtrans_split' ) ) { - function qtrans_split( $text, $quicktags = true ) { + function qtrans_split( $text, bool $quicktags = true ): array { return qtranxf_split( $text ); } } if ( ! function_exists( 'qtrans_use' ) ) { - function qtrans_use( $lang, $text, $show_available = false ) { + function qtrans_use( string $lang, $text, bool $show_available = false ) { return qtranxf_use( $lang, $text, $show_available ); } } diff --git a/src/date_time.php b/src/date_time.php index 6208c1db..b2b9c978 100644 --- a/src/date_time.php +++ b/src/date_time.php @@ -20,7 +20,7 @@ * @todo Maybe deprecate. Avoid using this function, meant to transition from legacy strftime formats. * @see https://gist.github.com/bohwaz/42fc223031e2b2dd2585aab159a20f30 (for the original code). */ -function qxtranxf_intl_strftime( $format, $timestamp = null, $locale = null ) { +function qxtranxf_intl_strftime( string $format, $timestamp = null, ?string $locale = null ): string { if ( null === $timestamp ) { $timestamp = new \DateTime; } elseif ( is_numeric( $timestamp ) ) { @@ -48,7 +48,7 @@ function qxtranxf_intl_strftime( $format, $timestamp = null, $locale = null ) { ]; // \DateTimeInterface, string - $intl_formatter = function ( $timestamp, $format ) use ( $intl_formats, $locale ) { + $intl_formatter = function ( DateTimeInterface $timestamp, string $format ) use ( $intl_formats, $locale ) { $tz = $timestamp->getTimezone(); $date_type = \IntlDateFormatter::FULL; $time_type = \IntlDateFormatter::FULL; @@ -208,7 +208,7 @@ function qxtranxf_intl_strftime( $format, $timestamp = null, $locale = null ) { * @see https://www.php.net/manual/en/function.strftime.php * @see https://www.php.net/manual/en/datetime.format.php */ -function qtranxf_convert_date_format_to_strftime_format( $format ) { +function qtranxf_convert_date_format_to_strftime_format( string $format ): string { $mappings = array( // day 'd' => '%d', @@ -280,7 +280,7 @@ function qtranxf_convert_date_format_to_strftime_format( $format ) { * @return string format in strftime * @todo Maybe deprecate. Don't use strftime formats anymore, since strftime is deprecated from PHP8.1. */ -function qtranxf_convert_to_strftime_format_using_config( $format, $default_format ) { +function qtranxf_convert_to_strftime_format_using_config( string $format, string $default_format ): string { global $q_config; // If one of special language-neutral formats is requested, don't override it. switch ( $format ) { @@ -316,7 +316,7 @@ function qtranxf_convert_to_strftime_format_using_config( $format, $default_form * * @return string */ -function qtranxf_get_language_date_or_time_format( $config_key ) { +function qtranxf_get_language_date_or_time_format( string $config_key ): string { assert( $config_key == 'date_format' || $config_key == 'time_format' ); global $q_config; if ( isset( $q_config[ $config_key ][ $q_config['language'] ] ) ) { @@ -339,7 +339,7 @@ function qtranxf_get_language_date_or_time_format( $config_key ) { * * @return string date/time if the format is valid, default value otherwise. */ -function qtranxf_format_date( $format, $mysql_time, $default_value, $before = '', $after = '' ) { +function qtranxf_format_date( string $format, string $mysql_time, string $default_value, string $before = '', string $after = '' ): string { if ( ! empty( $before ) || ! empty( $after ) ) { _deprecated_argument( __FUNCTION__, '3.13.0' ); } @@ -364,7 +364,7 @@ function qtranxf_format_date( $format, $mysql_time, $default_value, $before = '' * * @return string date/time if the format is valid, default value otherwise. */ -function qtranxf_format_time( $format, $mysql_time, $default_value, $before = '', $after = '' ) { +function qtranxf_format_time( string $format, string $mysql_time, string $default_value, string $before = '', string $after = '' ): string { if ( ! empty( $before ) || ! empty( $after ) ) { _deprecated_argument( __FUNCTION__, '3.13.0' ); } @@ -379,7 +379,7 @@ function qtranxf_format_time( $format, $mysql_time, $default_value, $before = '' } // @see get_the_date -function qtranxf_dateFromPostForCurrentLanguage( $old_date, $format = '', $post = null ) { +function qtranxf_dateFromPostForCurrentLanguage( string $old_date, string $format = '', $post = null ): string { $post = get_post( $post ); if ( ! $post ) { return $old_date; @@ -389,7 +389,7 @@ function qtranxf_dateFromPostForCurrentLanguage( $old_date, $format = '', $post } // @see get_the_modified_date -function qtranxf_dateModifiedFromPostForCurrentLanguage( $old_date, $format = '' ) { +function qtranxf_dateModifiedFromPostForCurrentLanguage( string $old_date, string $format = '' ): string { global $post; if ( ! $post ) { return $old_date; @@ -399,7 +399,7 @@ function qtranxf_dateModifiedFromPostForCurrentLanguage( $old_date, $format = '' } // @see get_the_time -function qtranxf_timeFromPostForCurrentLanguage( $old_date, $format = '', $post = null, $gmt = false ) { +function qtranxf_timeFromPostForCurrentLanguage( string $old_date, string $format = '', $post = null, bool $gmt = false ): string { $post = get_post( $post ); if ( ! $post ) { return $old_date; @@ -410,7 +410,7 @@ function qtranxf_timeFromPostForCurrentLanguage( $old_date, $format = '', $post } // @see get_post_modified_time -function qtranxf_timeModifiedFromPostForCurrentLanguage( $old_date, $format = '', $gmt = false ) { +function qtranxf_timeModifiedFromPostForCurrentLanguage( string $old_date, string $format = '', bool $gmt = false ): string { global $post; if ( ! $post ) { return $old_date; @@ -421,7 +421,7 @@ function qtranxf_timeModifiedFromPostForCurrentLanguage( $old_date, $format = '' } // @see get_comment_date -function qtranxf_dateFromCommentForCurrentLanguage( $old_date, $format, $comment = null ) { +function qtranxf_dateFromCommentForCurrentLanguage( string $old_date, string $format, ?string $comment = null ): string { if ( ! $comment ) { global $comment; // TODO drop obsolete compatibility with older WP } @@ -433,7 +433,7 @@ function qtranxf_dateFromCommentForCurrentLanguage( $old_date, $format, $comment } // @see get_comment_time -function qtranxf_timeFromCommentForCurrentLanguage( $old_date, $format = '', $gmt = false, $translate = true, $comment = null ) { +function qtranxf_timeFromCommentForCurrentLanguage( string $old_date, string $format = '', bool $gmt = false, bool $translate = true, ?string $comment = null ): string { if ( ! $translate ) { return $old_date; } @@ -453,7 +453,7 @@ function qtranxf_timeFromCommentForCurrentLanguage( $old_date, $format = '', $gm * * @return void */ -function qtranxf_add_date_time_filters() { +function qtranxf_add_date_time_filters(): void { global $q_config; if ( $q_config['use_strftime'] != QTX_DATE_WP && class_exists( 'IntlDateFormatter' ) ) { diff --git a/src/default_language_config.php b/src/default_language_config.php index 41248767..fb2a134c 100644 --- a/src/default_language_config.php +++ b/src/default_language_config.php @@ -4,7 +4,7 @@ * Names for languages in the corresponding native language. * @since 3.3 */ -function qtranxf_default_language_name() { +function qtranxf_default_language_name(): array { return array( 'en' => 'English', 'zh' => '中文', // 简体中 @@ -47,7 +47,7 @@ function qtranxf_default_language_name() { * Locales for languages, matching WordPress locales when possible. * @since 3.3 */ -function qtranxf_default_locale() { +function qtranxf_default_locale(): array { // see locale -a for available locales return array( 'de' => 'de_DE', @@ -91,7 +91,7 @@ function qtranxf_default_locale() { * HTML locales for languages * @since 3.4 */ -function qtranxf_default_locale_html() { +function qtranxf_default_locale_html(): array { //HTML locales for languages are not provided by default return array(); } @@ -100,7 +100,7 @@ function qtranxf_default_locale_html() { * Language not available messages * @since 3.3 */ -function qtranxf_default_not_available() { +function qtranxf_default_not_available(): array { // %LANG::% generates a list of languages separated by // except for the last one, where will be used instead. // Not Available Message @@ -149,7 +149,7 @@ function qtranxf_default_not_available() { * @todo Deprecate strftime format * @since 3.3 */ -function qtranxf_default_date_format() { +function qtranxf_default_date_format(): array { return array( 'en' => '%A %B %e%q, %Y', 'de' => '%A, \d\e\r %e. %B %Y', @@ -193,7 +193,7 @@ function qtranxf_default_date_format() { * @todo Deprecate strftime format * @since 3.3 */ -function qtranxf_default_time_format() { +function qtranxf_default_time_format(): array { return array( 'en' => '%I:%M %p', 'de' => '%H:%M', @@ -237,7 +237,7 @@ function qtranxf_default_time_format() { * Look in /flags/ directory for a huge list of flags for usage. * @since 3.3 */ -function qtranxf_default_flag() { +function qtranxf_default_flag(): array { return array( 'en' => 'gb.png', 'de' => 'de.png', @@ -280,7 +280,7 @@ function qtranxf_default_flag() { * Full country names as locales for Windows systems, in English. * @since 3.3 */ -function qtranxf_default_windows_locale() { +function qtranxf_default_windows_locale(): array { return array( 'aa' => "Afar", 'ab' => "Abkhazian", diff --git a/src/frontend.php b/src/frontend.php index 996b43a6..85afa3df 100644 --- a/src/frontend.php +++ b/src/frontend.php @@ -27,7 +27,7 @@ function qtranxf_get_front_page_config() { return $page_configs; } -function qtranxf_wp_head() { +function qtranxf_wp_head(): void { global $q_config; if ( $q_config['header_css_on'] ) { @@ -64,7 +64,7 @@ function qtranxf_wp_head() { * to remove this line from the header. * @since 3.4.5.4 */ -function qtranxf_wp_head_meta_generator() { +function qtranxf_wp_head_meta_generator(): void { echo '' . PHP_EOL; } @@ -655,7 +655,7 @@ function qtranxf_esc_html( $text ) { if ( ! function_exists( 'qtranxf_trim_words' ) ) { // filter added in qtranslate_hooks.php - function qtranxf_trim_words( $text, $num_words, $more, $original_text ) { + function qtranxf_trim_words( string $text, int $num_words, string $more, string $original_text ): string { global $q_config; $blocks = qtranxf_get_language_blocks( $original_text ); if ( count( $blocks ) <= 1 ) { @@ -673,7 +673,7 @@ function qtranxf_trim_words( $text, $num_words, $more, $original_text ) { * Delete translated post_meta cache for all languages. * Cache may have a few languages, if it is persistent. */ -function qtranxf_cache_delete_metadata( $meta_type, $object_id ) {//, $meta_key) { +function qtranxf_cache_delete_metadata( string $meta_type, int $object_id ): void { global $q_config; // maybe optimized to only replace the meta_key needed ? foreach ( $q_config['enabled_languages'] as $lang ) { @@ -686,7 +686,7 @@ function qtranxf_cache_delete_metadata( $meta_type, $object_id ) {//, $meta_key) * @since 3.2.3 translation of meta data * @since 3.4.6.4 improved caching algorithm */ -function qtranxf_translate_metadata( $meta_type, $original_value, $object_id, $meta_key = '', $single = false ) { +function qtranxf_translate_metadata( string $meta_type, $original_value, int $object_id, string $meta_key = '', bool $single = false ) { global $q_config; static $meta_cache_unserialized = array(); if ( ! isset( $q_config['url_info'] ) ) { @@ -801,7 +801,7 @@ function qtranxf_translate_metadata( $meta_type, $original_value, $object_id, $m /** * @since 3.2.3 translation of postmeta */ -function qtranxf_filter_postmeta( $original_value, $object_id, $meta_key = '', $single = false ) { +function qtranxf_filter_postmeta( $original_value, int $object_id, string $meta_key = '', bool $single = false ) { return qtranxf_translate_metadata( 'post', $original_value, $object_id, $meta_key, $single ); } @@ -810,7 +810,7 @@ function qtranxf_filter_postmeta( $original_value, $object_id, $meta_key = '', $ * Delete translated post_meta cache for all languages on cache update. * Cache may have a few languages, if it is persistent. */ -function qtranxf_updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) { +function qtranxf_updated_postmeta( int $meta_id, int $object_id, string $meta_key, $meta_value ) { qtranxf_cache_delete_metadata( 'post', $object_id ); } @@ -818,7 +818,7 @@ function qtranxf_updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value /** * @since 3.4 translation of usermeta */ -function qtranxf_filter_usermeta( $original_value, $object_id, $meta_key = '', $single = false ) { +function qtranxf_filter_usermeta( $original_value, int $object_id, string $meta_key = '', bool $single = false ) { return qtranxf_translate_metadata( 'user', $original_value, $object_id, $meta_key, $single ); } @@ -827,11 +827,12 @@ function qtranxf_filter_usermeta( $original_value, $object_id, $meta_key = '', $ * Delete translated user_meta cache for all languages on cache update. * Cache may have a few languages, if it is persistent. */ -function qtranxf_updated_usermeta( $meta_id, $object_id, $meta_key, $meta_value ) { +function qtranxf_updated_usermeta( int $meta_id, int $object_id, string $meta_key, $meta_value ) { qtranxf_cache_delete_metadata( 'user', $object_id ); } -function qtranxf_checkCanonical( $redirect_url, $requested_url ) { +// TODO check API with Yoast, seems it's only 1 parameter? +function qtranxf_checkCanonical( string $redirect_url, string $requested_url ): string { global $q_config; $lang = $q_config['language']; // fix canonical conflicts with language urls @@ -843,7 +844,7 @@ function qtranxf_checkCanonical( $redirect_url, $requested_url ) { /** * @since 3.2.8 moved here from _hooks.php */ -function qtranxf_convertBlogInfoURL( $url, $what ) { +function qtranxf_convertBlogInfoURL( string $url, string $what ): string { switch ( $what ) { case 'stylesheet_url': case 'template_url': @@ -859,9 +860,10 @@ function qtranxf_convertBlogInfoURL( $url, $what ) { * @since 3.3.1 * Moved here from qtranslate_hooks.php and modified. */ -function qtranxf_pagenum_link( $url ) { +function qtranxf_pagenum_link( string $url ): string { $lang_code = QTX_LANG_CODE_FORMAT; - $url_fixed = preg_replace( "#\?lang=$lang_code/#i", '/', $url ); //kind of ugly fix for function get_pagenum_link in /wp-includes/link-template.php. Maybe we should cancel filter 'bloginfo_url' instead? + // TODO kind of ugly fix for function get_pagenum_link in /wp-includes/link-template.php. Maybe we should cancel filter 'bloginfo_url' instead? + $url_fixed = preg_replace( "#\?lang=$lang_code/#i", '/', $url ); return qtranxf_convertURL( $url_fixed ); } @@ -869,7 +871,7 @@ function qtranxf_pagenum_link( $url ) { /** * @since 3.3.7 */ -function qtranxf_add_front_filters() { +function qtranxf_add_front_filters(): void { global $q_config; add_action( 'wp_head', 'qtranxf_wp_head' ); diff --git a/src/hooks.php b/src/hooks.php index c4aeb904..794f75ee 100644 --- a/src/hooks.php +++ b/src/hooks.php @@ -6,7 +6,7 @@ /** * locale for current language and set it on PHP. */ -function qtranxf_localeForCurrentLanguage( $locale ) { +function qtranxf_localeForCurrentLanguage( string $locale ): string { static $locale_lang; if ( ! empty( $locale_lang ) ) { return $locale_lang; @@ -72,11 +72,11 @@ function qtranxf_useDefaultLanguage( $content ) { return qtranxf_use( $q_config['default_language'], $content, false, false ); } -function qtranxf_versionLocale() { +function qtranxf_versionLocale(): string { return 'en_US'; } -function qtranxf_useRawTitle( $title, $raw_title = '', $context = 'save' ) { +function qtranxf_useRawTitle( string $title, string $raw_title = '', string $context = 'save' ): string { switch ( $context ) { case 'save': { @@ -120,7 +120,7 @@ function qtranxf_ngettext( $translated_text ) { * * @return void */ -function qtranxf_add_main_filters() { +function qtranxf_add_main_filters(): void { add_filter( 'wp_trim_words', 'qtranxf_trim_words', 0, 4 ); add_filter( 'sanitize_title', 'qtranxf_useRawTitle', 0, 3 ); add_filter( 'comment_moderation_subject', 'qtranxf_useDefaultLanguage', 0 ); diff --git a/src/i18n-interface.php b/src/i18n-interface.php index 06c401c0..4211368d 100644 --- a/src/i18n-interface.php +++ b/src/i18n-interface.php @@ -55,7 +55,7 @@ public static function get_translator(); * @return string two-letter code of active language. * @since 3.4.6.9 */ - public function get_language(); + public function get_language(): string; /** * @param string $lang two-letter code of language to be set as active. Further translations will be to this language unless desired languge is specified. @@ -64,7 +64,7 @@ public function get_language(); * @since 3.4.6.9 * */ - public function set_language( $lang ); + public function set_language( string $lang ): string; /** * Get translated value from a multilingual string. @@ -76,7 +76,7 @@ public function set_language( $lang ); * TRANSLATE_SHOW_AVALABLE - return a list of available languages with language-encoded links to the current page. * TRANSLATE_SHOW_EMPTY - return empty string. */ - public function translate_text( $text, $lang = null, $flags = 0 ); + public function translate_text( $text, ?string $lang = null, int $flags = 0 ): string; /** * Get translated value for a term name. @@ -85,7 +85,7 @@ public function translate_text( $text, $lang = null, $flags = 0 ); * @param (string)(optional) $lang - A two-letter language code of the language to translate $term to. If omitted or null, then the currently active language is assumed. * @param (string)(optional) $taxonomy - Taxonomy name that $term is part of. Currently unused, since all term names assumed to be unique across all taxonomies. */ - public function translate_term( $term, $lang = null, $taxonomy = null ); + public function translate_term( $term, ?string $lang = null, ?string $taxonomy = null ): string; /** * Get language-encoded value for a URL. @@ -93,6 +93,6 @@ public function translate_term( $term, $lang = null, $taxonomy = null ); * @param (mixed) $url - The URL to be encoded. It may be an array of URLs. * @param (string)(optional) $lang - A two-letter language code of the language to encode $url with. If omitted or null, then the currently active language is assumed. */ - public function translate_url( $url, $lang = null ); + public function translate_url( $url, ?string $lang = null ): string; } diff --git a/src/init.php b/src/init.php index b1aa7a79..22400dfc 100644 --- a/src/init.php +++ b/src/init.php @@ -23,7 +23,7 @@ * * @return void */ -function qtranxf_init_language() { +function qtranxf_init_language(): void { global $q_config, $pagenow; qtranxf_load_config(); diff --git a/src/language_blocks.php b/src/language_blocks.php index 727752a4..70215e7b 100644 --- a/src/language_blocks.php +++ b/src/language_blocks.php @@ -35,7 +35,7 @@ function qtranxf_get_language_blocks( $text ) { * * @return string[] array of string items indexed by language. */ -function qtranxf_split( $text ) { +function qtranxf_split( $text ): array { $blocks = qtranxf_get_language_blocks( $text ); return qtranxf_split_blocks( $blocks ); @@ -50,7 +50,7 @@ function qtranxf_split( $text ) { * @return string[] array of string items indexed by language. * @since 3.4.5.2 $found added */ -function qtranxf_split_blocks( $blocks, &$found = array() ) { +function qtranxf_split_blocks( array $blocks, array &$found = array() ): array { global $q_config; $result = array(); @@ -109,7 +109,7 @@ function qtranxf_split_blocks( $blocks, &$found = array() ) { /** * gets only part with encoded languages */ -function qtranxf_split_languages( $blocks ) { +function qtranxf_split_languages( array $blocks ): array { $result = array(); $current_language = false; $lang_code = QTX_LANG_CODE_FORMAT; @@ -177,7 +177,7 @@ function qtranxf_getAvailableLanguages( $text ) { return $result; } -function qtranxf_allthesame( $texts ) { +function qtranxf_allthesame( array $texts ): ?string { $text = null; // take first not empty foreach ( $texts as $lang => $lang_text ) { @@ -199,7 +199,7 @@ function qtranxf_allthesame( $texts ) { return $text; } -function qtranxf_join_c( $texts ) { +function qtranxf_join_c( array $texts ): string { $text = qtranxf_allthesame( $texts ); if ( ! is_null( $text ) ) { return $text; @@ -215,7 +215,7 @@ function qtranxf_join_c( $texts ) { return $text; } -function qtranxf_join_b_no_closing( $texts ) { +function qtranxf_join_b_no_closing( array $texts ): string { $text = qtranxf_allthesame( $texts ); if ( ! is_null( $text ) ) { return $text; @@ -231,7 +231,7 @@ function qtranxf_join_b_no_closing( $texts ) { return $text; } -function qtranxf_join_b( $texts ) { +function qtranxf_join_b( array $texts ): string { $text = qtranxf_allthesame( $texts ); if ( ! is_null( $text ) ) { return $text; @@ -253,7 +253,7 @@ function qtranxf_join_b( $texts ) { /** * @since 3.3.6 swirly bracket encoding */ -function qtranxf_join_s( $texts ) { +function qtranxf_join_s( array $texts ): string { $text = qtranxf_allthesame( $texts ); if ( ! is_null( $text ) ) { return $text; @@ -276,7 +276,7 @@ function qtranxf_join_s( $texts ) { * Prepares multilingual text leaving text that matches $regex_sep outside of language tags. * @since 3.4.6.2 */ -function qtranxf_join_byseparator( $texts, $regex_sep ) { +function qtranxf_join_byseparator( array $texts, string $regex_sep ): string { $text = qtranxf_allthesame( $texts ); if ( ! is_null( $text ) ) { return $text; @@ -316,7 +316,7 @@ function qtranxf_join_byseparator( $texts, $regex_sep ) { /** * Prepare multilingal text leaving new line outside of language tags '[:]'. */ -function qtranxf_join_byline( $texts ) { +function qtranxf_join_byline( array $texts ): string { $text = qtranxf_allthesame( $texts ); if ( ! is_null( $text ) ) { return $text; @@ -351,7 +351,8 @@ function qtranxf_join_byline( $texts ) { return $text; } -function qtranxf_use( $lang, $text, $show_available = false, $show_empty = false ) { +// TODO: this function signature is way too generic and weakly typed, break it by input type. +function qtranxf_use( string $lang, $text, bool $show_available = false, bool $show_empty = false ) { // return full string if language is not enabled if ( is_array( $text ) ) { // handle arrays recursively @@ -382,7 +383,7 @@ function qtranxf_use( $lang, $text, $show_available = false, $show_empty = false } /** when $text is already known to be string */ -function qtranxf_use_language( $lang, $text, $show_available = false, $show_empty = false ) { +function qtranxf_use_language( string $lang, string $text, bool $show_available = false, bool $show_empty = false ) { $blocks = qtranxf_get_language_blocks( $text ); if ( count( $blocks ) <= 1 )//no language is encoded in the $text, the most frequent case { @@ -392,14 +393,14 @@ function qtranxf_use_language( $lang, $text, $show_available = false, $show_empt return qtranxf_use_block( $lang, $blocks, $show_available, $show_empty ); } -function qtranxf_use_block( $lang, $blocks, $show_available = false, $show_empty = false ) { +function qtranxf_use_block( string $lang, array $blocks, bool $show_available = false, bool $show_empty = false ): string { $available_langs = array(); $content = qtranxf_split_blocks( $blocks, $available_langs ); return qtranxf_use_content( $lang, $content, $available_langs, $show_available, $show_empty ); } -function qtranxf_use_content( $lang, $content, $available_langs, $show_available = false, $show_empty = false ) { +function qtranxf_use_content( string $lang, $content, array $available_langs, bool $show_available = false, bool $show_empty = false ): string { global $q_config; // show the content in the requested language, if available if ( ! empty( $available_langs[ $lang ] ) ) { @@ -461,7 +462,7 @@ function qtranxf_use_content( $lang, $content, $available_langs, $show_available return apply_filters( 'i18n_content_translation_not_available', $output, $lang, $language_list, $alt_lang, $alt_content, $msg, $q_config ); } -function qtranxf_showAllSeparated( $text ) { +function qtranxf_showAllSeparated( $text ): string { if ( empty( $text ) ) { return $text; } diff --git a/src/language_config.php b/src/language_config.php index 657715fa..38751d57 100644 --- a/src/language_config.php +++ b/src/language_config.php @@ -1,12 +1,12 @@ $option ) { $cfg[ $name ] = qtranxf_language_configured( $name, $option ); @@ -216,7 +216,7 @@ function qtranxf_languages_configured( &$cfg ) { * Load enabled languages properties from database * @since 3.3 */ -function qtranxf_load_languages_enabled() { +function qtranxf_load_languages_enabled(): void { global $q_config, $qtranslate_options; foreach ( $qtranslate_options['languages'] as $name => $option ) { $func = 'qtranxf_default_' . $name; @@ -237,7 +237,7 @@ function qtranxf_load_languages_enabled() { } } -function qtranxf_load_option( $name, $default_value = null ) { +function qtranxf_load_option( string $name, $default_value = null ): void { global $q_config, $qtranslate_options; $val = get_option( 'qtranslate_' . $name ); if ( $val === false ) { @@ -256,7 +256,7 @@ function qtranxf_load_option( $name, $default_value = null ) { $q_config[ $name ] = $val; } -function qtranxf_load_option_array( $name, $default_value = null ) { +function qtranxf_load_option_array( string $name, $default_value = null ): void { global $q_config; $vals = get_option( 'qtranslate_' . $name ); if ( $vals === false ) { @@ -292,7 +292,7 @@ function qtranxf_load_option_array( $name, $default_value = null ) { $q_config[ $name ] = $vals; } -function qtranxf_load_option_bool( $name, $default_value = null ) { +function qtranxf_load_option_bool( string $name, ?bool $default_value = null ): void { global $q_config; $val = get_option( 'qtranslate_' . $name ); if ( $val === false ) { @@ -327,7 +327,7 @@ function qtranxf_load_option_bool( $name, $default_value = null ) { } } -function qtranxf_load_option_func( $name, $opn = null, $func = null ) { +function qtranxf_load_option_func( string $name, string $opn = null, $func = null ): void { global $q_config; if ( ! $opn ) { $opn = 'qtranslate_' . $name; @@ -342,7 +342,7 @@ function qtranxf_load_option_func( $name, $opn = null, $func = null ) { $q_config[ $name ] = $val; } -function qtranxf_load_option_qtrans_compatibility() { +function qtranxf_load_option_qtrans_compatibility(): void { global $q_config; qtranxf_load_option_bool( 'qtrans_compatibility', false ); $q_config['qtrans_compatibility'] = apply_filters( 'qtranslate_compatibility', $q_config['qtrans_compatibility'] ); @@ -352,7 +352,7 @@ function qtranxf_load_option_qtrans_compatibility() { require_once QTRANSLATE_DIR . '/src/compatibility.php'; } -function qtranxf_load_plugin_textdomain() { +function qtranxf_load_plugin_textdomain(): bool { if ( load_plugin_textdomain( 'qtranslate', false, basename( QTRANSLATE_DIR ) . '/lang' ) ) { return true; } @@ -360,13 +360,13 @@ function qtranxf_load_plugin_textdomain() { return false; } -function qtranxf_is_permalink_structure_query() { +function qtranxf_is_permalink_structure_query(): bool { $permalink_structure = get_option( 'permalink_structure' ); return empty( $permalink_structure ) || strpos( $permalink_structure, '?' ) !== false || strpos( $permalink_structure, 'index.php' ) !== false; } -function qtranxf_front_header_css_default() { +function qtranxf_front_header_css_default(): string { global $q_config; $flag_location = qtranxf_flag_location(); $css = ''; @@ -377,17 +377,17 @@ function qtranxf_front_header_css_default() { return $css; } -function qtranxf_flag_location() { +function qtranxf_flag_location(): string { global $q_config; return trailingslashit( content_url() ) . $q_config['flag_location']; } -function qtranxf_flag_location_default() { +function qtranxf_flag_location_default(): string { return qtranxf_plugin_dirname_from_wp_content() . '/flags/'; } -function qtranxf_load_option_flag_location( $nm ) { +function qtranxf_load_option_flag_location( string $nm ): void { global $q_config; $default_value = qtranxf_flag_location_default(); $option_value = get_option( 'qtranslate_' . $nm ); @@ -405,7 +405,7 @@ function qtranxf_load_option_flag_location( $nm ) { } } -function qtranxf_load_config() { +function qtranxf_load_config(): void { global $qtranslate_options, $q_config; qtranxf_set_default_options( $qtranslate_options ); diff --git a/src/rest_api.php b/src/rest_api.php index 2e1cc4ed..0446d060 100644 --- a/src/rest_api.php +++ b/src/rest_api.php @@ -13,7 +13,7 @@ * * @see rest_api_register_rewrites in wp_includes/rest-api.php */ -function qtranxf_rest_api_register_rewrites() { +function qtranxf_rest_api_register_rewrites(): void { global $q_config; if ( ! $q_config['hide_default_language'] || $q_config['url_mode'] !== QTX_URL_PATH ) { return; diff --git a/src/taxonomy.php b/src/taxonomy.php index 209b5d81..d7bb9ef3 100644 --- a/src/taxonomy.php +++ b/src/taxonomy.php @@ -9,7 +9,7 @@ * @since 3.4.6.9 * */ -function qtranxf_term_set_i18n_config( $term ) { +function qtranxf_term_set_i18n_config( ?WP_Term $term ) { $term->i18n_config = array(); if ( isset( $term->name ) ) { global $q_config; @@ -31,7 +31,7 @@ function qtranxf_term_set_i18n_config( $term ) { /** * @since 3.4 */ -function qtranxf_term_use( $lang, $obj, $taxonomy ) { +function qtranxf_term_use( string $lang, $obj, $taxonomy ) { global $q_config; if ( is_array( $obj ) ) { // handle arrays recursively @@ -72,7 +72,7 @@ function qtranxf_useTermLib( $obj ) { * @since 3.4.6.9 * */ -function qtranxf_term_name_in( $lang, $term ) { +function qtranxf_term_name_in( string $lang, WP_Term $term ): string { if ( isset( $term->i18n_config['name']['ts'][ $lang ] ) ) { return $term->i18n_config['name']['ts'][ $lang ]; } diff --git a/src/url.php b/src/url.php index 865d56be..2e51b4bc 100644 --- a/src/url.php +++ b/src/url.php @@ -12,7 +12,7 @@ * * @return string */ -function qtranxf_convertURL( $url = '', $lang = '', $forceadmin = false, $showDefaultLanguage = false ) { +function qtranxf_convertURL( string $url = '', string $lang = '', bool $forceadmin = false, bool $showDefaultLanguage = false ): string { global $q_config; if ( empty( $lang ) ) { @@ -37,7 +37,7 @@ function qtranxf_convertURL( $url = '', $lang = '', $forceadmin = false, $showDe return $complete; } -function qtranxf_convertURLs( $url, $lang = '', $forceadmin = false, $showDefaultLanguage = false ) { +function qtranxf_convertURLs( $url, string $lang = '', bool $forceadmin = false, bool $showDefaultLanguage = false ) { global $q_config; if ( empty( $lang ) ) { $lang = $q_config['language']; @@ -58,7 +58,7 @@ function qtranxf_convertURLs( $url, $lang = '', $forceadmin = false, $showDefaul /** * @since 3.0 */ -function qtranxf_url_del_language( &$urlinfo ) { +function qtranxf_url_del_language( array &$urlinfo ): void { global $q_config; if ( ! empty( $urlinfo['query'] ) ) { @@ -107,7 +107,7 @@ function qtranxf_url_del_language( &$urlinfo ) { } } -function qtranxf_url_set_language( $urlinfo, $lang, $showLanguage ) { +function qtranxf_url_set_language( array $urlinfo, $lang, bool $showLanguage ): array { global $q_config; $urlinfo = qtranxf_copy_url_info( $urlinfo ); if ( $showLanguage ) { @@ -152,7 +152,7 @@ function qtranxf_url_set_language( $urlinfo, $lang, $showLanguage ) { return $urlinfo; } -function qtranxf_get_url_for_language( $url, $lang, $showLanguage = true ) { +function qtranxf_get_url_for_language( string $url, string $lang, bool $showLanguage = true ): string { global $q_config; static $url_cache = array(); if ( ! isset( $url_cache[ $url ] ) ) { @@ -254,7 +254,7 @@ function qtranxf_get_url_for_language( $url, $lang, $showLanguage = true ) { } // check if it is a link to an ignored file type -function qtranxf_ignored_file_type( $path ) { +function qtranxf_ignored_file_type( string $path ): bool { global $q_config; $i = strpos( $path, '?' ); if ( $i !== false ) { @@ -273,7 +273,7 @@ function qtranxf_ignored_file_type( $path ) { return in_array( $ext, $q_config['ignore_file_types'] ); } -function qtranxf_language_neutral_path( $path ) { +function qtranxf_language_neutral_path( string $path ): bool { if ( empty( $path ) ) { return false; } @@ -299,7 +299,7 @@ function qtranxf_language_neutral_path( $path ) { /* * @since 2.3.8 simplified version of esc_url */ -function qtranxf_sanitize_url( $url ) { +function qtranxf_sanitize_url( string $url ): string { $url = preg_replace( '|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\[\]\\x80-\\xff]|i', '', $url ); $strip = array( '%0d', '%0a', '%0D', '%0A' ); do { @@ -310,7 +310,7 @@ function qtranxf_sanitize_url( $url ) { } -function qtranxf_get_url_info( $url ) { +function qtranxf_get_url_info( string $url ): array { $urlinfo = qtranxf_parseURL( $url ); qtranxf_complete_url_info( $urlinfo ); qtranxf_complete_url_info_path( $urlinfo ); @@ -324,7 +324,7 @@ function qtranxf_get_url_info( $url ) { * * @param array $urlinfo */ -function qtranxf_complete_url_info( &$urlinfo ) { +function qtranxf_complete_url_info( array &$urlinfo ): void { if ( ! isset( $urlinfo['path'] ) ) { $urlinfo['path'] = ''; } @@ -367,7 +367,7 @@ function qtranxf_complete_url_info( &$urlinfo ) { * * @since 3.2.8 */ -function qtranxf_complete_url_info_path( &$urlinfo ) { +function qtranxf_complete_url_info_path( array &$urlinfo ): void { if ( isset( $urlinfo['path-base'] ) ) { if ( empty( $urlinfo['path-base'] ) ) { $urlinfo['wp-path'] = $urlinfo['path']; @@ -384,7 +384,7 @@ function qtranxf_complete_url_info_path( &$urlinfo ) { } } -function qtranxf_parseURL( $url ) { +function qtranxf_parseURL( string $url ): array { // this is not the same as native parse_url and so it is in use // it should also work quicker than native parse_url, so we should keep it? preg_match( '!(?:(\w+)://)?(?:(\w+):(\w+)@)?([^/:?#]+)?(?::(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!', $url, $out ); @@ -420,7 +420,7 @@ function qtranxf_parseURL( $url ) { /** * @since 3.2.8 */ -function qtranxf_buildURL( $urlinfo, $homeinfo ) { +function qtranxf_buildURL( array $urlinfo, array $homeinfo ): string { if ( empty( $urlinfo['host'] ) ) { $url = ''; // relative path stays relative } else { @@ -459,7 +459,7 @@ function qtranxf_buildURL( $urlinfo, $homeinfo ) { /** * @since 3.2.8 Copies the data needed for qtranxf_buildURL and qtranxf_url_set_language */ -function qtranxf_copy_url_info( $urlinfo ) { +function qtranxf_copy_url_info( array $urlinfo ): array { $copy = array(); if ( isset( $urlinfo['scheme'] ) ) { $copy['scheme'] = $urlinfo['scheme']; @@ -492,7 +492,7 @@ function qtranxf_copy_url_info( $urlinfo ) { return $copy; } -function qtranxf_external_host_ex( $host, $homeinfo ) { +function qtranxf_external_host_ex( string $host, array $homeinfo ): bool { global $q_config; switch ( $q_config['url_mode'] ) { @@ -517,13 +517,13 @@ function qtranxf_external_host_ex( $host, $homeinfo ) { } } -function qtranxf_external_host( $host ) { +function qtranxf_external_host( string $host ): bool { $homeinfo = qtranxf_get_home_info(); return qtranxf_external_host_ex( $host, $homeinfo ); } -function qtranxf_get_page_referer() { +function qtranxf_get_page_referer(): string { if ( wp_doing_ajax() ) { global $q_config; if ( isset( $q_config['url_info']['page_referer'] ) ) { diff --git a/src/utils.php b/src/utils.php index e50adce0..72247331 100644 --- a/src/utils.php +++ b/src/utils.php @@ -590,11 +590,11 @@ function qtranxf_remove_filters( $filters ) { /** * @since 3.4 */ -function qtranxf_html_locale( $locale ) { +function qtranxf_html_locale( string $locale ): string { return str_replace( '_', '-', $locale ); } -function qtranxf_match_language_locale( $locale ) { +function qtranxf_match_language_locale( string $locale ): ?string { global $q_config; foreach ( $q_config['enabled_languages'] as $lang ) { if ( qtranxf_html_locale( $q_config['locale'][ $lang ] ) == $locale ) { diff --git a/src/widget.php b/src/widget.php index 9314b59c..e4f5c3c0 100644 --- a/src/widget.php +++ b/src/widget.php @@ -39,7 +39,7 @@ function __construct() { parent::__construct( 'qtranslate', __( 'qTranslate Language Chooser', 'qtranslate' ), $widget_ops ); } - function widget( $args, $instance ) { + function widget( $args, $instance ): void { if ( ! isset( $instance['widget-css-off'] ) ) { echo '" . PHP_EOL; } -function qtranxf_slugs_taxonomy_columns( $columns ) { +function qtranxf_slugs_taxonomy_columns( array $columns ): array { unset( $columns['slug'] ); $columns['qts-slug'] = __( 'Slug' ); return $columns; } -function qtranxf_slugs_taxonomy_custom_column( $str, $column_name, $term_id ) { +function qtranxf_slugs_taxonomy_custom_column( $str, string $column_name, int $term_id ): bool { global $q_config; if ( $column_name === 'qts-slug' ) { @@ -557,9 +558,9 @@ function qtranxf_slugs_taxonomy_custom_column( $str, $column_name, $term_id ) { * @param (array) $terms * @param (int|array) $obj_id * @param (string|array) $taxonomy - * @param (array) $taxonomy + * @param (array) $args */ -function qtranxf_slugs_get_object_terms( $terms, $obj_id, $taxonomy, $args ) { +function qtranxf_slugs_get_object_terms( array $terms, $obj_id, $taxonomy, array $args ): array { global $pagenow; global $q_config; @@ -596,7 +597,7 @@ function qtranxf_slugs_get_object_terms( $terms, $obj_id, $taxonomy, $args ) { * @param (array) $terms * @param (string|array) $taxonomy */ -function qtranxf_slugs_get_terms( $terms, $taxonomy ) { +function qtranxf_slugs_get_terms( array $terms, $taxonomy ): array { global $pagenow; global $q_config; @@ -622,7 +623,7 @@ function qtranxf_slugs_get_terms( $terms, $taxonomy ) { return $terms; } -function qtranxf_slugs_ma_module_updated() { +function qtranxf_slugs_ma_module_updated(): void { if ( QTX_Module_Loader::is_module_active( 'slugs' ) ) { qtranxf_slugs_multi_activate(); } else { diff --git a/src/modules/slugs/admin_migrate_qts.php b/src/modules/slugs/admin_migrate_qts.php index 934dd510..8051bfbc 100644 --- a/src/modules/slugs/admin_migrate_qts.php +++ b/src/modules/slugs/admin_migrate_qts.php @@ -11,7 +11,7 @@ * * @return string messages giving details, empty if new meta found or no legacy meta found. */ -function qtranxf_slugs_check_migrate_qts() { +function qtranxf_slugs_check_migrate_qts(): string { global $wpdb; /** @@ -46,7 +46,7 @@ function qtranxf_slugs_check_migrate_qts() { * * @return string messages giving details. */ -function qtranxf_slugs_migrate_qts_meta( $db_commit ) { +function qtranxf_slugs_migrate_qts_meta( bool $db_commit ): string { global $wpdb; $new_prefix = QTX_SLUGS_META_PREFIX; @@ -57,11 +57,12 @@ function qtranxf_slugs_migrate_qts_meta( $db_commit ) { * * @param string $table name of the meta table (postmeta, termmeta) * @param string $colid column name of the parent id (post_id, term_id) + * @param bool $db_commit true to commit changes, false for dry-run mode. * @param string[] $msg array of messages, updated * * @return void */ - $migrate_meta = function ( $table, $colid, $db_commit, &$msg ) use ( $wpdb, $old_prefix, $new_prefix ) { + $migrate_meta = function ( string $table, string $colid, bool $db_commit, array &$msg ) use ( $wpdb, $old_prefix, $new_prefix ): void { // Escape '_' against LIKE wildcards. $old_esc = str_replace( '_', '\_', $old_prefix ); $new_esc = str_replace( '_', '\_', $new_prefix ); @@ -103,7 +104,7 @@ function qtranxf_slugs_migrate_qts_meta( $db_commit ) { * * @return string messages giving details. */ -function qtranxf_slugs_migrate_qts_options( $db_commit ) { +function qtranxf_slugs_migrate_qts_options( bool $db_commit ): string { $msg = []; $qts_options = get_option( QTX_SLUGS_LEGACY_QTS_OPTIONS_NAME ); @@ -148,7 +149,7 @@ function qtranxf_slugs_migrate_qts_options( $db_commit ) { * * @return string messages giving details. */ -function qtranxf_slugs_migrate_qts_data( $db_commit ) { +function qtranxf_slugs_migrate_qts_data( bool $db_commit ): string { $msg = []; $msg[] = $db_commit ? __( 'Migrate slugs:', 'qtranslate' ) : __( "Dry-run mode:", 'qtranslate' ); $msg[] = qtranxf_slugs_migrate_qts_meta( $db_commit ); diff --git a/src/modules/slugs/admin_settings.php b/src/modules/slugs/admin_settings.php index 0a901391..e1048299 100644 --- a/src/modules/slugs/admin_settings.php +++ b/src/modules/slugs/admin_settings.php @@ -8,7 +8,7 @@ * * @return array */ -function qtranxf_slugs_get_settings() { +function qtranxf_slugs_get_settings(): array { $output = array(); $output['qts_page_sections'] = qtranxf_slugs_options_page_sections(); $output['qts_page_fields'] = qtranxf_slugs_options_page_fields(); @@ -21,7 +21,7 @@ function qtranxf_slugs_get_settings() { * * @return void echoes output */ -function qtranxf_slugs_section_fn( $section_id = '' ) { +function qtranxf_slugs_section_fn( string $section_id = '' ): void { switch ( $section_id ) { case 'post_types': echo "

" . __( 'For example, the post_type books, in Spanish would be displayed as https://example.org/es/libros/post-type-name/. If you leave this blank will use the default option when you registered the post_type.', 'qtranslate' ) . "

"; @@ -38,7 +38,7 @@ function qtranxf_slugs_section_fn( $section_id = '' ) { * * @return void echoes output */ -function qtranxf_slugs_show_form_field( $args = array() ) { +function qtranxf_slugs_show_form_field( array $args = array() ): void { global $qtranslate_slugs; global $q_config; @@ -189,7 +189,7 @@ function qtranxf_slugs_show_form_field( $args = array() ) { * * @return void echoes output */ -function qtranxf_slugs_show_settings_page() { +function qtranxf_slugs_show_settings_page(): void { $settings_output = qtranxf_slugs_get_settings(); if ( empty( $settings_output['qts_page_sections'] ) ) { @@ -217,7 +217,7 @@ function qtranxf_slugs_show_settings_page() { * * @return array */ -function qtranxf_slugs_validate_options( $input ) { +function qtranxf_slugs_validate_options( array $input ): array { global $q_config; // Initialize lookup array to be used to make sure slug for a specific language is unique $slugs_lookup_array = array(); @@ -352,7 +352,7 @@ function qtranxf_slugs_validate_options( $input ) { return $valid_input; } -function qtranxf_slugs_update_settings() { +function qtranxf_slugs_update_settings(): void { global $qtranslate_slugs; $qts_settings = isset( $_POST[ QTX_OPTIONS_MODULE_SLUGS ] ) ? qtranxf_slugs_validate_options( $_POST[ QTX_OPTIONS_MODULE_SLUGS ] ) : array(); @@ -372,7 +372,7 @@ function qtranxf_slugs_update_settings() { * * @return array key=$id, array value=$title in: add_settings_section( $id, $title, $callback, $page ); */ -function qtranxf_slugs_options_page_sections() { +function qtranxf_slugs_options_page_sections(): array { $sections = array(); $sections['post_types'] = __( 'Post types', 'qtranslate' ); $sections['taxonomies'] = __( 'Taxonomies', 'qtranslate' ); @@ -385,7 +385,7 @@ function qtranxf_slugs_options_page_sections() { * * @return array */ -function qtranxf_slugs_get_multi_txt_choices() { +function qtranxf_slugs_get_multi_txt_choices(): array { global $q_config; $choices = array(); @@ -402,7 +402,7 @@ function qtranxf_slugs_get_multi_txt_choices() { * * @return array */ -function qtranxf_slugs_options_page_fields() { +function qtranxf_slugs_options_page_fields(): array { global $qtranslate_slugs; $options = array(); @@ -419,7 +419,7 @@ function qtranxf_slugs_options_page_fields() { return array_filter( $options ); } -function qtranxf_slugs_options_page_build_slug_fields( $object, $target_section, $id_prefix ) { +function qtranxf_slugs_options_page_build_slug_fields( $object, string $target_section, string $id_prefix ): array { if ( is_array( $object->rewrite ) && array_key_exists( 'slug', $object->rewrite ) ) { $slug = ltrim( $object->rewrite['slug'], "/" ); } else { @@ -436,4 +436,4 @@ function qtranxf_slugs_options_page_build_slug_fields( $object, $target_section, "choices" => qtranxf_slugs_get_multi_txt_choices(), "std" => "" ); -} \ No newline at end of file +} diff --git a/src/modules/slugs/slugs.php b/src/modules/slugs/slugs.php index 1f9ce373..84123078 100644 --- a/src/modules/slugs/slugs.php +++ b/src/modules/slugs/slugs.php @@ -498,13 +498,13 @@ function filter_request( $query ) { /** * Parse a hierarquical name and extract the last one * - * @param string $lang Page path + * @param string|bool $lang Page path * * @return string * * @since 1.0 */ - public function get_current_url( $lang = false ) { + public function get_current_url( $lang = false ): string { if ( ! $lang ) { $lang = $this->get_temp_lang(); @@ -520,13 +520,14 @@ public function get_current_url( $lang = false ) { /** * Retrieve the home url for a given site. * - * @param int $blog_id (optional) Blog ID. Defaults to current blog. - * @param string $path (optional) Path relative to the home url. - * @param string $scheme (optional) Scheme to give the home url context. Currently 'http', 'https'. + * @param string $url The complete home URL including scheme and path. + * @param string $path Path relative to the home url. + * @param string|null $scheme (optional) Scheme to give the home url context. Currently 'http', 'https'. + * @param int|null $blog_id (optional) Site ID, or null for the current site. * * @return string Home url link with optional path appended. */ - public function home_url( $url, $path, $scheme, $blog_id ) { + public function home_url( string $url, string $path, ?string $scheme, ?int $blog_id ): string { if ( ! in_array( $scheme, array( 'http', 'https' ) ) ) { $scheme = is_ssl() && ! is_admin() ? 'https' : 'http'; } @@ -562,7 +563,7 @@ public function home_url( $url, $path, $scheme, $blog_id ) { * * @return string processed permastruct */ - public function get_extra_permastruct( $permastruct = false, $name = false ) { + public function get_extra_permastruct( $permastruct = false, $name = false ): string { if ( ! $name || ! $permastruct ) { return ''; @@ -641,9 +642,9 @@ public function post_type_link( $link, $post, $leavename, $sample ) { * @param WP_Post $post the post data * @param bool $leavename parameter used by get_permalink. Whether to keep post name or page name. * - * @return string the link translated + * @return string|false the link translated */ - public function post_link( $link, $post, $leavename ) { + public function post_link( string $link, WP_Post $post, bool $leavename ) { global $q_config; //TODO: q_config : url_mode $rewritecode = array( @@ -744,7 +745,7 @@ public function post_link( $link, $post, $leavename ) { * * @return string the link translated */ - public function _get_page_link( $link, $id ) { + public function _get_page_link( string $link, int $id ): string { global $post, $wp_rewrite, $q_config; //TODO: q_config : url_mode $current_post = $post; @@ -785,10 +786,10 @@ public function _get_page_link( $link, $id ) { * @param WP_Term $term * @param object $taxonomy * - * @return string the link translated + * @return string|WP_Error the link translated */ //TODO: review this function vs get_term_link(), e.g. checks and error handling may be unneeded here - public function term_link( $link, $term, $taxonomy ) { + public function term_link( string $link, WP_Term $term, $taxonomy ) { global $wp_rewrite; // parse normal term names for ?tag=tagname @@ -875,7 +876,7 @@ public function blog_names( $blogs ) { * * @return array of public taxonomies objects */ - public function get_public_taxonomies() { + public function get_public_taxonomies(): array { $all_taxonomies = get_taxonomies( array( 'public' => true, 'show_ui' => true ), 'objects' ); $taxonomies = array(); @@ -893,7 +894,7 @@ public function get_public_taxonomies() { * * @return array of public post_types objects */ - public function get_public_post_types() { + public function get_public_post_types(): array { $all_post_types = get_post_types( array( 'public' => true ), 'objects' ); $post_types = array(); @@ -918,7 +919,7 @@ private function get_temp_lang() { /** * Helper: news rules to translate the URL bases. * - * @param string $name name of extra permastruct + * @param string|false $name name of extra permastruct */ private function generate_extra_rules( $name = false ) { global $q_config; @@ -948,7 +949,7 @@ private function generate_extra_rules( $name = false ) { * * @return string */ - private function get_last_slash( $slug ) { + private function get_last_slash( string $slug ): string { $slug = rawurlencode( urldecode( $slug ) ); $slug = str_replace( '%2F', '/', $slug ); $slug = str_replace( '%20', ' ', $slug ); @@ -965,7 +966,7 @@ private function get_last_slash( $slug ) { * * @return mixed Null when complete. */ - private function get_page_id_by_path( $page_path, $post_type = 'page' ) { + private function get_page_id_by_path( string $page_path, string $post_type = 'page' ) { global $wpdb; // Handle cases where custom query vars with the same names of specific internal query vars (e.g. 'name') are structured as arrays for any reason. @@ -1034,7 +1035,7 @@ private function get_page_id_by_path( $page_path, $post_type = 'page' ) { * * @return array|WP_Post|null Null when complete. */ - private function get_page_by_path( $page_path, $post_type = 'page' ) { + private function get_page_by_path( string $page_path, string $post_type = 'page' ) { $foundid = $this->get_page_id_by_path( $page_path, $post_type ); if ( $foundid ) { return get_post( $foundid ); @@ -1048,7 +1049,7 @@ private function get_page_by_path( $page_path, $post_type = 'page' ) { * * @return boolean */ - private function ignore_rewrite_caller() { + private function ignore_rewrite_caller(): bool { $backtrace = debug_backtrace(); $ignore_functions = array( @@ -1087,7 +1088,7 @@ private function ignore_rewrite_caller() { * @return string */ //TODO: $link seems to be unused (always false), to be removed and function cleaned up - private function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) { + private function get_category_parents( int $id, bool $link = false, string $separator = '/', bool $nicename = false, array $visited = array() ): string { $chain = ''; $parent = get_category( $id ); @@ -1127,7 +1128,7 @@ private function get_category_parents( $id, $link = false, $separator = '/', $ni * * @return string Page URI. */ - private function get_page_uri( $page ) { + private function get_page_uri( $page ): string { if ( ! is_object( $page ) ) { $page = get_post( $page ); } @@ -1166,7 +1167,7 @@ private function get_page_uri( $page ) { * @return array|false|object|WP_Error|WP_Term|null Term Row from database. Will return false if $taxonomy does not exist or $term was not found. * TODO: simplify return type and error handling, unexpected results may cause bugs! */ - private function get_term_by( $field, $value, $taxonomy ) { + private function get_term_by( string $field, $value, string $taxonomy ) { global $wpdb; if ( ! taxonomy_exists( $taxonomy ) ) { diff --git a/src/modules/slugs/utils.php b/src/modules/slugs/utils.php index 3fd9fb90..f032ef56 100644 --- a/src/modules/slugs/utils.php +++ b/src/modules/slugs/utils.php @@ -3,17 +3,18 @@ exit; } -//TOTO: check if this function needs to be retained as documented or not +// TODO: check if this function needs to be retained as documented or not +// TODO: clarify lang type bool|string function qtranxf_slugs_get_url( $lang = false ) { global $qtranslate_slugs; return $qtranslate_slugs->get_current_url( $lang ); } -function qtranxf_slugs_convert_url( $url, $lang ) { +function qtranxf_slugs_convert_url( string $url, $lang ): string { if ( empty( $url ) ) { return qtranxf_slugs_get_url( $lang ); } return $url; -} \ No newline at end of file +} diff --git a/src/modules/woo-commerce/admin.php b/src/modules/woo-commerce/admin.php index ba5393f8..f4b0875c 100644 --- a/src/modules/woo-commerce/admin.php +++ b/src/modules/woo-commerce/admin.php @@ -3,7 +3,7 @@ exit; } -function qtranxf_wc_add_filters_admin() { +function qtranxf_wc_add_filters_admin(): void { // priority 20 is used because in case other plugins add some untranslated content on normal priority // it will still hopefully then get translated. $admin_hooks = array( @@ -46,12 +46,12 @@ function qtranxf_wc_add_filters_admin() { qtranxf_wc_add_filters_admin(); add_action( 'admin_enqueue_scripts', 'qtranxf_wc_add_admin_styles' ); -function qtranxf_wc_add_admin_styles() { +function qtranxf_wc_add_admin_styles(): void { wp_enqueue_style( 'qtranxf_wc_qtranslate_admin', plugins_url( '/css/modules/woo-commerce.css', QTRANSLATE_FILE ), array(), QTX_VERSION ); } add_filter( 'qtranslate_admin_config', 'qtranxf_wc_add_admin_page_config' ); -function qtranxf_wc_add_admin_page_config( $page_configs ) { +function qtranxf_wc_add_admin_page_config( array $page_configs ): array { // post.php // TODO refactor append config if ( ! isset( $page_configs['post'] ) ) { @@ -241,7 +241,7 @@ function qtranxf_wc_add_admin_page_config( $page_configs ) { return $page_configs; } -function qtranxf_wc_email_get_option( $value_translated, $wce /* WC_Email object*/, $value = null, $key = null, $empty_value = null ) { +function qtranxf_wc_email_get_option( $value_translated, WC_Email $wce, $value = null, $key = null, $empty_value = null ) { if ( ! $value ) { return $value_translated; // so that older WC versions do not get nasty output } @@ -257,11 +257,11 @@ function qtranxf_wc_email_get_option( $value_translated, $wce /* WC_Email object * Append the language to the link for changing the order status, so that mails are sent in the language the customer * used during the order process * - * @param $url + * @param string $url * * @return string */ -function qtranxf_wc_admin_url_append_language( $url ) { +function qtranxf_wc_admin_url_append_language( string $url ): string { if ( strpos( $url, 'action=woocommerce_mark_order_status' ) ) { $components = parse_url( $url ); $params = array(); @@ -285,11 +285,11 @@ function qtranxf_wc_admin_url_append_language( $url ) { * Append the language to ajax links on the order edit page, so that mails are sent in the language the customer used * during the order process * - * @param $url + * @param string $url * * @return string */ -function qtranxf_wc_admin_url_append_language_edit_page( $url ) { +function qtranxf_wc_admin_url_append_language_edit_page( string $url ): string { if ( strpos( $url, 'admin-ajax.php' ) === false || ! isset( $_GET['action'] ) || ! isset( $_GET['post'] ) || $_GET['action'] != 'edit' ) { return $url; } @@ -342,7 +342,7 @@ function qtranxf_wc_admin_email_option( $val ) { * This helps to use order's language on re-sent emails from post.php order edit page. * * @param $content - * @param WC_Order $order + * @param WC_Order|null $order * * @return array|mixed|string|void */ @@ -368,7 +368,7 @@ function qtranxf_wc_admin_email_translate( $content, $order = null ) { * * @param WC_Order $order */ -function qtranxf_wc_admin_before_resend_order_emails( $order ) { +function qtranxf_wc_admin_before_resend_order_emails( $order ): void { if ( ! ( $order && $order->get_id() ) ) { return; } @@ -387,14 +387,14 @@ function qtranxf_wc_admin_before_resend_order_emails( $order ) { /** * Undo the effect of qtranxf_wc_admin_before_resend_order_emails */ -function qtranxf_wc_admin_after_resend_order_emails( $order ) { +function qtranxf_wc_admin_after_resend_order_emails( $order ): void { global $q_config; $q_config['language'] = $q_config['url_info']['language']; } add_action( 'woocommerce_after_resend_order_email', 'qtranxf_wc_admin_after_resend_order_emails' ); -function qtranxf_wc_admin_filters() { +function qtranxf_wc_admin_filters(): void { global $pagenow; switch ( $pagenow ) { case 'admin.php': diff --git a/src/modules/woo-commerce/front.php b/src/modules/woo-commerce/front.php index 14624642..23b3c6bf 100644 --- a/src/modules/woo-commerce/front.php +++ b/src/modules/woo-commerce/front.php @@ -3,7 +3,7 @@ exit; } -function qtranxf_wc_add_filters_front() { +function qtranxf_wc_add_filters_front(): void { remove_filter( 'get_post_metadata', 'qtranxf_filter_postmeta', 5 ); add_filter( 'get_post_metadata', 'qtranxf_wc_filter_postmeta', 5, 4 ); @@ -41,7 +41,7 @@ function qtranxf_wc_add_filters_front() { add_filter( 'woocommerce_paypal_args', 'qtranxf_wc_paypal_args' ); } -function qtranxf_wc_filter_postmeta( $original_value, $object_id, $meta_key = '', $single = false ) { +function qtranxf_wc_filter_postmeta( $original_value, int $object_id, string $meta_key = '', bool $single = false ) { switch ( $meta_key ) { case '_product_attributes': return $original_value; @@ -66,7 +66,7 @@ function qtranxf_wc_filter_postmeta( $original_value, $object_id, $meta_key = '' * @see wc_dropdown_variation_attribute_options (single-product/add-to-cart/variable.php) * */ -function qtranxf_wc_dropdown_variation_attribute_options_args( $args ) { +function qtranxf_wc_dropdown_variation_attribute_options_args( array $args ): array { if ( isset( $args['options'] ) ) { $args['options'] = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage( $args['options'] ); } @@ -86,7 +86,7 @@ function qtranxf_wc_save_post_meta( $order_id ) { add_post_meta( $order_id, '_user_language', $q_config['language'], true ); } -function qtranxf_wc_paypal_args( $args ) { +function qtranxf_wc_paypal_args( array $args ): array { $args['lc'] = get_locale(); return $args; @@ -104,7 +104,7 @@ function qtranxf_wc_paypal_args( $args ) { * * @return string cart hash with language information */ -function qtranxf_wc_get_cart_hash( $cart ) { +function qtranxf_wc_get_cart_hash( array $cart ): string { $lang = qtranxf_getLanguage(); return md5( json_encode( $cart ) . $lang ); @@ -116,7 +116,7 @@ function qtranxf_wc_get_cart_hash( $cart ) { * * @param array $cart wc variable holding contents of the cart without language information. */ -function qtranxf_wc_set_cookies_cart_hash( $cart ) { +function qtranxf_wc_set_cookies_cart_hash( array $cart ): void { $hash = qtranxf_wc_get_cart_hash( $cart ); wc_setcookie( 'woocommerce_cart_hash', $hash ); } @@ -127,7 +127,7 @@ function qtranxf_wc_set_cookies_cart_hash( $cart ) { * * @param WC_Cart $wc_cart wc object without language information. */ -function qtranxf_wc_cart_loaded_from_session( $wc_cart ) { +function qtranxf_wc_cart_loaded_from_session( WC_Cart $wc_cart ): void { if ( headers_sent() ) { return; } @@ -143,7 +143,7 @@ function qtranxf_wc_cart_loaded_from_session( $wc_cart ) { * * @param bool $set is true if cookies need to be set, otherwse they are unset in calling function. */ -function qtranxf_wc_set_cart_cookies( $set ) { +function qtranxf_wc_set_cart_cookies( bool $set ): void { if ( $set ) { $wc = WC(); $wc_cart = $wc->cart; @@ -163,7 +163,7 @@ function qtranxf_wc_set_cart_cookies( $set ) { * * @return string cart hash with language information */ -function qtranxf_wc_cart_hash( $hash, $cart ) { +function qtranxf_wc_cart_hash( string $hash, array $cart ): string { $new_hash = qtranxf_wc_get_cart_hash( $cart ); if ( ! headers_sent() ) { wc_setcookie( 'woocommerce_cart_hash', $new_hash ); diff --git a/src/modules/woo-commerce/loader.php b/src/modules/woo-commerce/loader.php index 1111b260..8425c475 100644 --- a/src/modules/woo-commerce/loader.php +++ b/src/modules/woo-commerce/loader.php @@ -6,7 +6,7 @@ exit; } -function qtranxf_wc_init_language( $url_info ) { +function qtranxf_wc_init_language( array $url_info ): void { if ( $url_info['doing_front_end'] ) { require_once __DIR__ . '/front.php'; } else { @@ -27,7 +27,7 @@ function qtranxf_wc_init_language( $url_info ) { * * @return array possibly modified $url_info. */ -function qtranxf_wc_detect_language( $url_info ) { +function qtranxf_wc_detect_language( array $url_info ): array { if ( isset( $url_info['cookie_lang_front'] ) && $url_info['cookie_lang_front'] != $url_info['language'] ) { // language is about to switch if ( ! empty( $_GET['wc-ajax'] ) && ! empty( $url_info['doing_front_end'] ) ) { @@ -49,7 +49,7 @@ function qtranxf_wc_detect_language( $url_info ) { * For some cases (e.g. variations updates) the webhook is generated through AJAX instead of cron. * In that context, qwc-admin.php is loaded instead of qwc-front.php */ -function qtranxf_wc_deliver_webhook_async( $webhook_id, $arg ) { +function qtranxf_wc_deliver_webhook_async( $webhook_id, $arg ): void { if ( function_exists( 'qtranxf_get_front_page_config' ) ) { $page_configs = qtranxf_get_front_page_config(); if ( ! empty( $page_configs['']['filters'] ) ) { diff --git a/src/modules/wp-seo/loader.php b/src/modules/wp-seo/loader.php index c7508998..c896ce48 100644 --- a/src/modules/wp-seo/loader.php +++ b/src/modules/wp-seo/loader.php @@ -5,7 +5,7 @@ exit; } -function qtranxf_wpseo_init_language( $url_info ) { +function qtranxf_wpseo_init_language( array $url_info ): void { if ( $url_info['doing_front_end'] ) { require_once __DIR__ . '/wp-seo-front.php'; } else { diff --git a/src/modules/wp-seo/wp-seo-admin.php b/src/modules/wp-seo/wp-seo-admin.php index e4f0aa1f..3d411d82 100644 --- a/src/modules/wp-seo/wp-seo-admin.php +++ b/src/modules/wp-seo/wp-seo-admin.php @@ -4,7 +4,7 @@ } add_filter( 'qtranslate_admin_config', 'qtranxf_wpseo_load_admin_page_config' ); -function qtranxf_wpseo_load_admin_page_config( $page_configs ) { +function qtranxf_wpseo_load_admin_page_config( array $page_configs ): array { assert( ! isset( $page_configs['yoast_wpseo'] ) ); $page_configs['yoast_wpseo'] = array( diff --git a/src/modules/wp-seo/wp-seo-front.php b/src/modules/wp-seo/wp-seo-front.php index fe89ad05..4fc992bd 100644 --- a/src/modules/wp-seo/wp-seo-front.php +++ b/src/modules/wp-seo/wp-seo-front.php @@ -3,7 +3,7 @@ exit; } -function qtranxf_wpseo_add_filters_front() { +function qtranxf_wpseo_add_filters_front(): void { // Use indexation on "publish/update" events to save all languages data in indexable tables. // If indexation is allowed on the frontend then indexable table saves data of the first visited page // and other languages are missed. @@ -31,7 +31,7 @@ function qtranxf_wpseo_add_filters_front() { add_filter( $name, 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', $priority ); } - function qtranxf_wpseo_webpage_schema( $piece, $context ) { + function qtranxf_wpseo_webpage_schema( array $piece, string $context ): array { if ( isset( $piece['description'] ) ) { $piece['description'] = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage( $piece['description'] ); } @@ -44,7 +44,7 @@ function qtranxf_wpseo_webpage_schema( $piece, $context ) { add_filter( 'wpseo_schema_webpage', 'qtranxf_wpseo_webpage_schema', 10, 2 ); - function qtranxf_wpseo_breadcrumbs_link( $link_info, $index, $crumbs ) { + function qtranxf_wpseo_breadcrumbs_link( array $link_info, int $index, array $crumbs ): array { global $q_config; if ( isset( $link_info['text'] ) ) { @@ -59,7 +59,7 @@ function qtranxf_wpseo_breadcrumbs_link( $link_info, $index, $crumbs ) { add_filter( 'wpseo_breadcrumb_single_link_info', 'qtranxf_wpseo_breadcrumbs_link', 10, 3 ); - function qtranxf_wpseo_schema_organization( $data ) { + function qtranxf_wpseo_schema_organization( array $data ): array { global $q_config; if ( isset( $data['@id'] ) ) { @@ -86,7 +86,7 @@ function qtranxf_wpseo_schema_organization( $data ) { add_filter( 'wpseo_schema_organization', 'qtranxf_wpseo_schema_organization' ); - function qtranxf_wpseo_schema_website( $data ) { + function qtranxf_wpseo_schema_website( array $data ): array { global $q_config; if ( isset( $data['@id'] ) ) { @@ -104,7 +104,7 @@ function qtranxf_wpseo_schema_website( $data ) { add_filter( 'wpseo_schema_website', 'qtranxf_wpseo_schema_website' ); - function qtranxf_wpseo_json_ld_search_url( $search_url ) { + function qtranxf_wpseo_json_ld_search_url( string $search_url ): string { global $q_config; return qtranxf_convertURL( $search_url, $q_config['language'] ); @@ -112,7 +112,7 @@ function qtranxf_wpseo_json_ld_search_url( $search_url ) { add_filter( 'wpseo_json_ld_search_url', 'qtranxf_wpseo_json_ld_search_url', 10, 3 ); - function qtranxf_wpseo_schema_imageobject( $data ) { + function qtranxf_wpseo_schema_imageobject( array $data ): array { global $q_config; if ( isset( $data['@id'] ) ) { @@ -124,7 +124,7 @@ function qtranxf_wpseo_schema_imageobject( $data ) { add_filter( 'wpseo_schema_imageobject', 'qtranxf_wpseo_schema_imageobject' ); - function qtranxf_wpseo_schema_webpage( $data ) { + function qtranxf_wpseo_schema_webpage( array $data ): array { global $q_config; $lang = $q_config['language']; @@ -154,7 +154,7 @@ function qtranxf_wpseo_schema_webpage( $data ) { add_filter( 'wpseo_schema_webpage', 'qtranxf_wpseo_schema_webpage' ); - function qtranxf_wpseo_schema_breadcrumb( $data ) { + function qtranxf_wpseo_schema_breadcrumb( array $data ): array { global $q_config; if ( isset( $data['@id'] ) ) { @@ -166,7 +166,7 @@ function qtranxf_wpseo_schema_breadcrumb( $data ) { add_filter( 'wpseo_schema_breadcrumb', 'qtranxf_wpseo_schema_breadcrumb' ); - function qtranxf_wpseo_schema_person( $data ) { + function qtranxf_wpseo_schema_person( array $data ): array { global $q_config; //$data['@id'] = qtranxf_convertURL($data['@id'], $lang); //Not sure is it required to filter or not??? @@ -179,7 +179,7 @@ function qtranxf_wpseo_schema_person( $data ) { add_filter( 'wpseo_schema_person', 'qtranxf_wpseo_schema_person' ); - function qtranxf_wpseo_next_prev_filter( $link ) { + function qtranxf_wpseo_next_prev_filter( string $link ): string { global $q_config; if ( preg_match_all( '/]+href=([\'"])(?.+?)\1[^>]*>/i', $link, $link_extract_href ) &&