Advertisement
geminilabs

[site-reviews] Translate categories

Jul 13th, 2022
873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.76 KB | None | 0 0
  1. /**
  2.  * Get the languages of the website
  3.  * @return array
  4.  */
  5. function glsr_get_pll_languages () {
  6.     if (!function_exists('pll_languages_list') || !function_exists('pll_default_language')) {
  7.         return [];
  8.     }
  9.     $slugs = pll_languages_list();
  10.     $names = pll_languages_list(['fields' => 'name']);
  11.     $languages = array_combine($slugs, $names);
  12.     unset($languages[pll_default_language()]);
  13.     return $languages;
  14. }
  15.  
  16. /**
  17.  * Add a language field to new categories
  18.  */
  19. function glsr_add_category_fields () {
  20.     $languages = glsr_get_pll_languages();
  21.     $html = '' .
  22.         '<div class="form-field">' .
  23.             '<label for="%1$s-slug">%2$s</label>' .
  24.             '<input type="text" name="%1$s" id="%1$s-slug" />' .
  25.         '</div>';
  26.     foreach ($languages as $slug => $label) {
  27.         printf($html, $slug, $label);
  28.     }
  29. }
  30.  
  31. /**
  32.  * Add a language field to existing categories
  33.  * @param \WP_Term $term
  34.  */
  35. function glsr_edit_category_fields ($term) {
  36.     $languages = glsr_get_pll_languages();
  37.     $html = '' .
  38.         '<tr class="form-field">' .
  39.             '<th><label for="%1$s-slug">%2$s</label></th>' .
  40.             '<td><input type="text" name="%1$s" id="%1$s-slug" value="%3$s" /></td>' .
  41.         '</tr>';
  42.     foreach ($languages as $slug => $label) {
  43.         $value = esc_attr(get_term_meta($term->term_id, $slug, true));
  44.         printf($html, $slug, $label, $value);
  45.     }
  46. }
  47.  
  48. /**
  49.  * Save the language field when creating or editing a category
  50.  * @param int $termId
  51.  */
  52. function glsr_save_category_fields ($termId) {
  53.     $languages = glsr_get_pll_languages();
  54.     foreach ($languages as $slug => $label) {
  55.         $value = sanitize_text_field(filter_input(INPUT_POST, $slug));
  56.         update_term_meta($termId, $slug, $value);
  57.     }
  58. }
  59.  
  60. /**
  61.  * Translate the category on the frontend
  62.  * @param array $terms
  63.  * @param array $taxonomies
  64.  * @param array $args
  65.  * @return array
  66.  */
  67. function glsr_translate_category ($terms, $taxonomies, $args) {
  68.     if ($taxonomies !== ['site-review-category'] || !function_exists('pll_current_language')) {
  69.         return $terms;
  70.     }
  71.     $lang = pll_current_language();
  72.     if ($args['fields'] === 'id=>name') {
  73.         foreach ($terms as $termId => $label) {
  74.             $label = get_term_meta($termId, $lang, true);
  75.             if (!empty($label)) {
  76.                 $terms[$termId] = $label;
  77.             }
  78.         }
  79.     }
  80.     elseif ($args['fields'] === 'all') {
  81.         foreach ($terms as &$term) {
  82.             $label = get_term_meta($term->term_id, $lang, true);
  83.             if (!empty($label)) {
  84.                 $term->name = $label;
  85.             }
  86.         }
  87.     }
  88.     return $terms;
  89. }
  90.  
  91. add_action('site-review-category_add_form_fields', 'glsr_add_category_fields');
  92. add_action('site-review-category_edit_form_fields', 'glsr_edit_category_fields');
  93. add_action('created_site-review-category', 'glsr_save_category_fields');
  94. add_action('edited_site-review-category', 'glsr_save_category_fields');
  95. add_filter('get_terms', 'glsr_translate_category', 10, 3);
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement