| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Tag Groups |
| 5 |
* |
| 6 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 7 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 8 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 9 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 10 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 11 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 12 |
* THE SOFTWARE. |
| 13 |
* @package Tag Groups Pro |
| 14 |
* |
| 15 |
* @author Christoph Amthor |
| 16 |
* @copyright 2017 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 17 |
* @license see official vendor website |
| 18 |
*/ |
| 19 |
|
| 20 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, Squiz.Scope.MethodScope.Missing, PSR1.Methods.CamelCapsMethodName.NotCamelCaps, Squiz.PHP.CommentedOutCode.Found, WordPress.Security.NonceVerification.Recommended |
| 21 |
if (! class_exists('TagGroups_WPML')) { |
| 22 |
class TagGroups_WPML |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Whether this is a multilingual site |
| 26 |
* |
| 27 |
* checking for: |
| 28 |
* - WPML |
| 29 |
* - Polylang |
| 30 |
* |
| 31 |
* @return boolean |
| 32 |
*/ |
| 33 |
static function is_multilingual() |
| 34 |
{ |
| 35 |
|
| 36 |
if (defined('ICL_LANGUAGE_CODE')) { |
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
if (function_exists('pll_current_language')) { |
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
// if ( ! empty( $_GET[ 'lang' ] ) ) { |
| 45 |
|
| 46 |
// return true; |
| 47 |
|
| 48 |
// } |
| 49 |
|
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the language code (by default ICL_LANGUAGE_CODE) |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
static function get_current_language() |
| 59 |
{ |
| 60 |
|
| 61 |
if (defined('ICL_LANGUAGE_CODE')) { |
| 62 |
return (string) ICL_LANGUAGE_CODE; |
| 63 |
} |
| 64 |
|
| 65 |
if (function_exists('pll_current_language')) { |
| 66 |
$current_language = pll_current_language(); |
| 67 |
if ($current_language) { |
| 68 |
return (string) $current_language; |
| 69 |
} |
| 70 |
|
| 71 |
if (isset($_GET['lang'])) { |
| 72 |
return sanitize_key($_GET['lang']); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return ''; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Get the transient name for tag_groups_group_terms |
| 82 |
* |
| 83 |
* In case we use the WPML plugin: consider the language |
| 84 |
* |
| 85 |
* @param void |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
public static function get_tag_groups_group_terms_transient_name() |
| 89 |
{ |
| 90 |
|
| 91 |
$current_language = self::get_current_language(); |
| 92 |
if ($current_language) { |
| 93 |
return 'tag_groups_group_terms-' . $current_language; |
| 94 |
} else { |
| 95 |
return 'tag_groups_group_terms'; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Get the transient name for tag_groups_post_counts |
| 102 |
* |
| 103 |
* In case we use the WPML plugin: consider the language |
| 104 |
* Use $language if provided, else use current language |
| 105 |
* |
| 106 |
* @param string $language |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public static function get_tag_groups_post_counts_transient_name($language_code = null) |
| 110 |
{ |
| 111 |
|
| 112 |
if (! empty($language_code)) { |
| 113 |
return 'tag_groups_post_counts-' . (string) $language_code; |
| 114 |
} |
| 115 |
|
| 116 |
$current_language = self::get_current_language(); |
| 117 |
if ($current_language) { |
| 118 |
return 'tag_groups_post_counts-' . $current_language; |
| 119 |
} else { |
| 120 |
return 'tag_groups_post_counts'; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Sync the group(s) of a tag to all translations of this tag |
| 127 |
* |
| 128 |
* @param int $term_id |
| 129 |
* @param int $tt_id |
| 130 |
* @param string $taxonomy |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
static function sync_groups($term_id, $tt_id, $taxonomy) |
| 134 |
{ |
| 135 |
|
| 136 |
/** |
| 137 |
* Test for is_multilingual only here because we don't yet know when adding hooks |
| 138 |
*/ |
| 139 |
if (! TagGroups_WPML::is_multilingual()) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
if (! in_array($taxonomy, TagGroups_Taxonomy::get_enabled_taxonomies())) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
TagGroups_Error::verbose_log('[Tag Groups] Syncing groups of term ID %d', $term_id); |
| 148 |
/** |
| 149 |
* get translations |
| 150 |
*/ |
| 151 |
$trid = apply_filters('wpml_element_trid', null, $tt_id, "tax_{$taxonomy}"); |
| 152 |
$translations = apply_filters('wpml_get_element_translations', null, $trid, "tax_{$taxonomy}"); |
| 153 |
if (empty($translations) || ! is_array($translations)) { |
| 154 |
TagGroups_Error::verbose_log('[Tag Groups] Cannot get translations for term ID %d', $term_id); |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* get groups of saved term |
| 160 |
*/ |
| 161 |
$tg_term = new TagGroups_Term($term_id); |
| 162 |
$groups = $tg_term->get_groups(); |
| 163 |
foreach ($translations as $language => $translation) { |
| 164 |
if ($translation->element_id == $term_id) { |
| 165 |
continue; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* do the sync |
| 170 |
*/ |
| 171 |
$tg_translated_term = new TagGroups_Term($translation->element_id); |
| 172 |
$tg_translated_term->set_group($groups)->save(); |
| 173 |
TagGroups_Error::verbose_log('[Tag Groups] Groups synced of term ID %d for language %s', $translation->element_id, $language); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
} |
| 180 |
|