class-cookie-law-info-activator.php
1 month ago
class-cookie-law-info-cookieyes.php
1 month ago
class-cookie-law-info-deactivator.php
1 month ago
class-cookie-law-info-i18n.php
1 month ago
class-cookie-law-info-languages.php
1 month ago
class-cookie-law-info-loader.php
1 month ago
class-cookie-law-info-review-request.php
1 month ago
class-cookie-law-info.php
1 month ago
index.php
1 month ago
class-cookie-law-info-languages.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Cookies module to handle all the cookies related operations |
| 5 | * |
| 6 | * @version 1.9.6 |
| 7 | * @package CookieLawInfo |
| 8 | */ |
| 9 | |
| 10 | // https://wpml.org/wpml-hook/wpml_set_element_language_details/ |
| 11 | // https://wpml.org/forums/topic/programmatically-create-post-in-different-languages/ |
| 12 | // https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/ |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | class Cookie_Law_Info_Languages { |
| 19 | |
| 20 | |
| 21 | private static $instance; |
| 22 | |
| 23 | public function __construct() { |
| 24 | |
| 25 | } |
| 26 | |
| 27 | public static function get_instance() { |
| 28 | if ( null === self::$instance ) { |
| 29 | self::$instance = new self(); |
| 30 | } |
| 31 | |
| 32 | return self::$instance; |
| 33 | } |
| 34 | /** |
| 35 | * Check whether a multi language plugin is active or not |
| 36 | * |
| 37 | * @access public |
| 38 | * @return boolean |
| 39 | */ |
| 40 | public function is_multilanguage_plugin_active() { |
| 41 | $status = false; |
| 42 | |
| 43 | if ( defined( 'ICL_LANGUAGE_CODE' ) || defined( 'POLYLANG_FILE' ) ) { |
| 44 | $status = true; |
| 45 | } |
| 46 | |
| 47 | return $status; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get default language of the site |
| 52 | * |
| 53 | * @access public |
| 54 | * @return string |
| 55 | */ |
| 56 | public function get_default_language_code() { |
| 57 | $default = null; |
| 58 | |
| 59 | if ( $this->is_multilanguage_plugin_active() ) { |
| 60 | // Polylang |
| 61 | if ( function_exists( 'pll_default_language' ) ) { |
| 62 | $default = pll_default_language(); |
| 63 | } else { |
| 64 | // WPML |
| 65 | $null = null; |
| 66 | $default = apply_filters( 'wpml_default_language', $null ); |
| 67 | } |
| 68 | } else { |
| 69 | $default = CLI_DEFAULT_LANGUAGE; |
| 70 | } |
| 71 | |
| 72 | return $default; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the current language of the site |
| 77 | * |
| 78 | * @access public |
| 79 | * @return string |
| 80 | */ |
| 81 | public function get_current_language_code() { |
| 82 | $current_language = null; |
| 83 | |
| 84 | if ( $this->is_multilanguage_plugin_active() ) { |
| 85 | // Polylang |
| 86 | if ( function_exists( 'pll_current_language' ) ) { |
| 87 | |
| 88 | $current_language = pll_current_language(); |
| 89 | |
| 90 | // If current_language is still empty, we have to get the default language |
| 91 | if ( empty( $current_language ) ) { |
| 92 | $current_language = pll_default_language(); |
| 93 | } |
| 94 | } else { |
| 95 | // WPML |
| 96 | $null = null; |
| 97 | $current_language = apply_filters( 'wpml_current_language', $null ); |
| 98 | } |
| 99 | |
| 100 | // Fallback |
| 101 | if ( $current_language === 'all' ) { |
| 102 | $current_language = $this->get_default_language_code(); |
| 103 | } |
| 104 | } else { |
| 105 | $current_language = CLI_DEFAULT_LANGUAGE; |
| 106 | } |
| 107 | |
| 108 | return $current_language; |
| 109 | } |
| 110 | public function get_term_by_language( $term_id, $language ) { |
| 111 | $term = false; |
| 112 | if ( $this->is_multilanguage_plugin_active() ) { |
| 113 | // Polylang |
| 114 | if ( function_exists( 'pll_get_term_translations' ) ) { |
| 115 | $terms = pll_get_term_translations( $term_id ); |
| 116 | if ( isset( $terms[ $language ] ) ) { |
| 117 | $original_term_id = $terms[ $language ]; |
| 118 | $term = get_term_by( 'id', $original_term_id, 'cookielawinfo-category' ); |
| 119 | } |
| 120 | } else { |
| 121 | // WPML |
| 122 | if ( function_exists( 'icl_object_id' ) ) { |
| 123 | global $sitepress; |
| 124 | if ( $sitepress ) { |
| 125 | if ( version_compare( ICL_SITEPRESS_VERSION, '3.2.0' ) >= 0 ) { |
| 126 | $original_term_id = apply_filters( 'wpml_object_id', $term_id, 'category', true, $language ); |
| 127 | } else { |
| 128 | $original_term_id = icl_object_id( $term_id, 'category', true, $language ); |
| 129 | } |
| 130 | remove_filter( 'get_term', array( $sitepress, 'get_term_adjust_id' ), 1 ); |
| 131 | $term = get_term_by( 'id', $original_term_id, 'cookielawinfo-category' ); |
| 132 | add_filter( 'get_term', array( $sitepress, 'get_term_adjust_id' ), 1, 1 ); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | return $term; |
| 138 | } |
| 139 | |
| 140 | public function maybe_set_term_language( $term_id ) { |
| 141 | if ( $this->is_multilanguage_plugin_active() ) { |
| 142 | // Polylang |
| 143 | if ( function_exists( 'pll_set_term_language' ) ) { |
| 144 | $language = $this->get_default_language_code(); |
| 145 | pll_set_term_language( $term_id, $language ); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 |