|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // Populate Anything // WPML Current Language Choices |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-populate-anything/ |
| 5 | + * |
| 6 | + * With WPML you can create multiple versions of posts each with their own language. |
| 7 | + * When populating posts with Populate Anything, there's no way to identify the current |
| 8 | + * language and filter by that language with the existing UI. This snippet automatically |
| 9 | + * filters posts by the current language. |
| 10 | + * |
| 11 | + * Instructions: |
| 12 | + * 1. [Install the snippet.](https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets) |
| 13 | + * 2. Add the CSS class `wpml-limit-to-current-language` to any GPPA-enabled field you want filtered. |
| 14 | + * 3. Populate that field with posts; the snippet filters choices to the current WPML language. |
| 15 | + */ |
| 16 | +add_filter( 'gppa_input_choices', 'hb_wpml_limit_gppa_choices_to_current_language', 10, 4 ); |
| 17 | + |
| 18 | +function hb_wpml_limit_gppa_choices_to_current_language( $choices, $field, $objects, $field_values ) { |
| 19 | + // Ensure WPML is active. |
| 20 | + if ( ! function_exists( 'icl_object_id' ) ) { |
| 21 | + return $choices; |
| 22 | + } |
| 23 | + |
| 24 | + // Only apply filtering if the marker CSS class is present. |
| 25 | + $css_class = isset( $field->cssClass ) ? $field->cssClass : ''; |
| 26 | + if ( strpos( $css_class, 'wpml-limit-to-current-language' ) === false ) { |
| 27 | + return $choices; |
| 28 | + } |
| 29 | + |
| 30 | + // Get current WPML language (e.g. 'nl', 'en'). |
| 31 | + $current_lang = apply_filters( 'wpml_current_language', null ); |
| 32 | + if ( ! $current_lang ) { |
| 33 | + return $choices; |
| 34 | + } |
| 35 | + |
| 36 | + $filtered_choices = []; |
| 37 | + |
| 38 | + foreach ( $choices as $index => $choice ) { |
| 39 | + $post_id = 0; |
| 40 | + $post_type = null; |
| 41 | + |
| 42 | + // Preferred: WP_Post object from $objects array. |
| 43 | + if ( isset( $objects[ $index ] ) && $objects[ $index ] instanceof WP_Post ) { |
| 44 | + $post_id = $objects[ $index ]->ID; |
| 45 | + $post_type = $objects[ $index ]->post_type; |
| 46 | + } |
| 47 | + |
| 48 | + // Fallback: use the choice value as post ID. |
| 49 | + if ( ! $post_id && isset( $choice['value'] ) ) { |
| 50 | + $post_id = absint( $choice['value'] ); |
| 51 | + |
| 52 | + if ( $post_id && ! $post_type ) { |
| 53 | + $post_obj = get_post( $post_id ); |
| 54 | + $post_type = $post_obj ? $post_obj->post_type : null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // If we can resolve a post and post type, apply WPML language check. |
| 59 | + if ( $post_id && $post_type ) { |
| 60 | + // Convert the post type to a WPML element type, e.g. 'post_post'. |
| 61 | + $element_type = apply_filters( 'wpml_element_type', $post_type ); |
| 62 | + |
| 63 | + // Get the language code for this specific element. |
| 64 | + $choice_lang = apply_filters( |
| 65 | + 'wpml_element_language_code', |
| 66 | + null, |
| 67 | + [ |
| 68 | + 'element_id' => $post_id, |
| 69 | + 'element_type' => $element_type, |
| 70 | + ] |
| 71 | + ); |
| 72 | + |
| 73 | + // If WPML knows the language and it does not match the current language, skip this choice. |
| 74 | + if ( $choice_lang && $choice_lang !== $current_lang ) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Keep the choice. |
| 80 | + $filtered_choices[] = $choice; |
| 81 | + } |
| 82 | + |
| 83 | + return $filtered_choices; |
| 84 | +} |
0 commit comments