Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Get the languages of the website
- * @return array
- */
- function glsr_get_pll_languages () {
- if (!function_exists('pll_languages_list') || !function_exists('pll_default_language')) {
- return [];
- }
- $slugs = pll_languages_list();
- $names = pll_languages_list(['fields' => 'name']);
- $languages = array_combine($slugs, $names);
- unset($languages[pll_default_language()]);
- return $languages;
- }
- /**
- * Add a language field to new categories
- */
- function glsr_add_category_fields () {
- $languages = glsr_get_pll_languages();
- $html = '' .
- '<div class="form-field">' .
- '<label for="%1$s-slug">%2$s</label>' .
- '<input type="text" name="%1$s" id="%1$s-slug" />' .
- '</div>';
- foreach ($languages as $slug => $label) {
- printf($html, $slug, $label);
- }
- }
- /**
- * Add a language field to existing categories
- * @param \WP_Term $term
- */
- function glsr_edit_category_fields ($term) {
- $languages = glsr_get_pll_languages();
- $html = '' .
- '<tr class="form-field">' .
- '<th><label for="%1$s-slug">%2$s</label></th>' .
- '<td><input type="text" name="%1$s" id="%1$s-slug" value="%3$s" /></td>' .
- '</tr>';
- foreach ($languages as $slug => $label) {
- $value = esc_attr(get_term_meta($term->term_id, $slug, true));
- printf($html, $slug, $label, $value);
- }
- }
- /**
- * Save the language field when creating or editing a category
- * @param int $termId
- */
- function glsr_save_category_fields ($termId) {
- $languages = glsr_get_pll_languages();
- foreach ($languages as $slug => $label) {
- $value = sanitize_text_field(filter_input(INPUT_POST, $slug));
- update_term_meta($termId, $slug, $value);
- }
- }
- /**
- * Translate the category on the frontend
- * @param array $terms
- * @param array $taxonomies
- * @param array $args
- * @return array
- */
- function glsr_translate_category ($terms, $taxonomies, $args) {
- if ($taxonomies !== ['site-review-category'] || !function_exists('pll_current_language')) {
- return $terms;
- }
- $lang = pll_current_language();
- if ($args['fields'] === 'id=>name') {
- foreach ($terms as $termId => $label) {
- $label = get_term_meta($termId, $lang, true);
- if (!empty($label)) {
- $terms[$termId] = $label;
- }
- }
- }
- elseif ($args['fields'] === 'all') {
- foreach ($terms as &$term) {
- $label = get_term_meta($term->term_id, $lang, true);
- if (!empty($label)) {
- $term->name = $label;
- }
- }
- }
- return $terms;
- }
- add_action('site-review-category_add_form_fields', 'glsr_add_category_fields');
- add_action('site-review-category_edit_form_fields', 'glsr_edit_category_fields');
- add_action('created_site-review-category', 'glsr_save_category_fields');
- add_action('edited_site-review-category', 'glsr_save_category_fields');
- add_filter('get_terms', 'glsr_translate_category', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement