Activation
8 months ago
Admin
8 months ago
Block
8 months ago
Compatibility
8 months ago
Container
8 months ago
Front
8 months ago
Rest
8 months ago
Shortcode
8 months ago
Traits
8 months ago
Widget
8 months ago
Bootstrap.php
8 months ago
Cache.php
8 months ago
Helper.php
8 months ago
Image.php
8 months ago
Output.php
8 months ago
Query.php
8 months ago
Settings.php
8 months ago
Themer.php
8 months ago
Translate.php
8 months ago
Upgrader.php
8 months ago
WordPressPopularPosts.php
8 months ago
deprecated.php
8 months ago
template-tags.php
8 months ago
Translate.php
161 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Obtains translation data from objects. |
| 4 | * |
| 5 | * @link http://cabrerahector.com |
| 6 | * @since 4.0.0 |
| 7 | * |
| 8 | * @package WordPressPopularPosts |
| 9 | */ |
| 10 | |
| 11 | namespace WordPressPopularPosts; |
| 12 | |
| 13 | class Translate { |
| 14 | /** |
| 15 | * Default language code. |
| 16 | * |
| 17 | * @since 4.0.0 |
| 18 | * @access private |
| 19 | * @var string |
| 20 | */ |
| 21 | private $default_language; |
| 22 | |
| 23 | /** |
| 24 | * Current language code. |
| 25 | * |
| 26 | * @since 4.0.0 |
| 27 | * @access private |
| 28 | * @var string |
| 29 | */ |
| 30 | private $current_language; |
| 31 | |
| 32 | /** |
| 33 | * Initialize the collections used to maintain the actions and filters. |
| 34 | * |
| 35 | * @since 4.0.0 |
| 36 | */ |
| 37 | public function __construct() |
| 38 | { |
| 39 | // |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Retrieves the code of the default language. |
| 44 | * |
| 45 | * @since 4.0.0 |
| 46 | * @return string|null |
| 47 | */ |
| 48 | public function get_default_language() |
| 49 | { |
| 50 | if ( ! $this->default_language ) { |
| 51 | $this->default_language = ( function_exists('pll_default_language') ) ? pll_default_language() : apply_filters('wpml_default_language', null); |
| 52 | } |
| 53 | return $this->default_language; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Retrieves the code of the currently active language. |
| 58 | * |
| 59 | * @since 4.0.0 |
| 60 | * @return string|null |
| 61 | */ |
| 62 | public function get_current_language() |
| 63 | { |
| 64 | if ( ! $this->current_language ) { |
| 65 | $this->current_language = ( function_exists('pll_current_language') ) ? pll_current_language() : apply_filters('wpml_current_language', null); |
| 66 | } |
| 67 | return $this->current_language; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Sets the code of the currently active language. |
| 72 | * |
| 73 | * @since 4.0.0 |
| 74 | * @return string |
| 75 | */ |
| 76 | public function set_current_language(string $code) |
| 77 | { |
| 78 | $this->current_language = $code; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets language locale. |
| 83 | * |
| 84 | * @since 5.0.0 |
| 85 | * @param string $lang Language code (eg. 'es') |
| 86 | * @return string|bool |
| 87 | */ |
| 88 | public function get_locale(string $lang) |
| 89 | { |
| 90 | // Polylang support |
| 91 | if ( function_exists('PLL') ) { |
| 92 | $lang_object = PLL()->model->get_language($lang); |
| 93 | if ( $lang_object && isset($lang_object->locale) ) { |
| 94 | return $lang_object->locale; |
| 95 | } |
| 96 | } else { |
| 97 | // WPML support |
| 98 | global $sitepress; |
| 99 | if ( is_object($sitepress) && method_exists($sitepress, 'get_locale_from_language_code') ) { |
| 100 | return $sitepress->get_locale_from_language_code($lang); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Retrieves the ID of an object. |
| 109 | * |
| 110 | * @since 4.0.0 |
| 111 | * @param integer $object_id |
| 112 | * @param string $object_type |
| 113 | * @param boolean $return_original_if_missing |
| 114 | * @param string $lang_code |
| 115 | * @return integer |
| 116 | */ |
| 117 | public function get_object_id(int $object_id, string $object_type = 'post', bool $return_original_if_missing = true, ?string $lang_code = '') |
| 118 | { |
| 119 | return apply_filters( |
| 120 | 'wpml_object_id', |
| 121 | $object_id, |
| 122 | $object_type, |
| 123 | $return_original_if_missing, |
| 124 | null == $lang_code ? $this->get_current_language() : $lang_code |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Translates URL. |
| 130 | * |
| 131 | * @since 5.0.0 |
| 132 | * @param string $original_permalink |
| 133 | * @param string $lang |
| 134 | * @return string |
| 135 | */ |
| 136 | public function url(string $original_permalink, ?string $lang) |
| 137 | { |
| 138 | return apply_filters('wpml_permalink', $original_permalink, $lang); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Retrieves the language code of an object. |
| 143 | * |
| 144 | * @since 4.0.0 |
| 145 | * @param integer $object_id |
| 146 | * @param string $object_type |
| 147 | * @return string|null |
| 148 | */ |
| 149 | public function get_object_lang_code(int $object_id, string $object_type = 'post') |
| 150 | { |
| 151 | return apply_filters( |
| 152 | 'wpml_element_language_code', |
| 153 | null, |
| 154 | [ |
| 155 | 'element_id' => $object_id, |
| 156 | 'element_type' => $object_type |
| 157 | ] |
| 158 | ); |
| 159 | } |
| 160 | } |
| 161 |