|
| 1 | +<?php |
| 2 | + |
| 3 | +class MLTools_Custom_Fields_Translation { |
| 4 | + public function __construct() { |
| 5 | + add_action( 'wp_ajax_wpml_cf_generate_xml', array( $this, 'wpml_cf_generate_xml' ) ); |
| 6 | + } |
| 7 | + |
| 8 | + public function get_custom_fields() { |
| 9 | + global $wpdb; |
| 10 | + |
| 11 | + // We don't need system fields starting with an underscore "_" |
| 12 | + |
| 13 | + $meta_keys = $wpdb->get_results( "SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE meta_key NOT LIKE '\_%' ORDER BY meta_key ASC" ); |
| 14 | + |
| 15 | + $custom_fields = array(); |
| 16 | + |
| 17 | + foreach ( $meta_keys as $meta_key ) { |
| 18 | + $custom_fields[] = $meta_key->meta_key; |
| 19 | + } |
| 20 | + |
| 21 | + // We need to exclude the fields with defined translation preference in WPML |
| 22 | + |
| 23 | + $excluded_custom_fields = array(); |
| 24 | + |
| 25 | + $settings = get_option( 'icl_sitepress_settings' ); |
| 26 | + |
| 27 | + if ( ! empty( $settings['translation-management']['custom_fields_translation'] ) ) { |
| 28 | + foreach ( $settings['translation-management']['custom_fields_translation'] as $custom_field => $value ) { |
| 29 | + $excluded_custom_fields[] = $custom_field; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Providing a filter to add more fields to be excluded |
| 34 | + |
| 35 | + /** |
| 36 | + * Example |
| 37 | + * |
| 38 | + * function my_custom_excluded_fields($excluded_fields) { |
| 39 | + * $excluded_fields[] = 'my_custom_field_1'; |
| 40 | + * $excluded_fields[] = 'my_custom_field_2'; |
| 41 | + * return $excluded_fields; |
| 42 | + * } |
| 43 | + * add_filter('wpml_custom_fields_helper_excluded_custom_fields', 'my_custom_excluded_fields'); |
| 44 | + */ |
| 45 | + |
| 46 | + $excluded_custom_fields = apply_filters( 'wpml_custom_fields_helper_excluded_custom_fields', $excluded_custom_fields ); |
| 47 | + |
| 48 | + $custom_fields = array_diff( $custom_fields, $excluded_custom_fields ); |
| 49 | + |
| 50 | + // We don't need these fields wpml_, attribute_pa-, acfml, etc.. |
| 51 | + |
| 52 | + $excluded_prefixes = [ 'acfml', 'attribute_pa', 'wpml', 'wpform' ]; |
| 53 | + |
| 54 | + $custom_fields = array_filter( $custom_fields, function ( $field ) use ( $excluded_prefixes ) { |
| 55 | + foreach ( $excluded_prefixes as $prefix ) { |
| 56 | + if ( strpos( $field, $prefix ) === 0 ) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | + } ); |
| 63 | + |
| 64 | + |
| 65 | + return $custom_fields; |
| 66 | + } |
| 67 | + |
| 68 | + public function determine_translation_preference() { |
| 69 | + |
| 70 | + global $wpdb; |
| 71 | + |
| 72 | + $custom_fields = $this->get_custom_fields(); |
| 73 | + $translation_preferences = array(); |
| 74 | + |
| 75 | + foreach ( $custom_fields as $custom_field ) { |
| 76 | + |
| 77 | + $value = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT 1", $custom_field ) ); |
| 78 | + |
| 79 | + // Check if value is numeric, a date string, or specific strings |
| 80 | + |
| 81 | + if ( $value ) { |
| 82 | + $date = DateTime::createFromFormat( 'd-m-Y', $value ); |
| 83 | + $date_errors = DateTime::getLastErrors(); |
| 84 | + } |
| 85 | + |
| 86 | + // These values should be copied to translations |
| 87 | + $copy_values = [ 'yes', 'no', 'on', 'off', 'true', 'false', 'default' ]; |
| 88 | + |
| 89 | + // Is it a hash-like string? Something like ffd4rf34d should be set to copy |
| 90 | + $isHashString = $value && strlen( $value ) > 5 && preg_match( '/\d/', $value ) && preg_match( '/[a-zA-Z]/', $value ) && strpos( $value, ' ' ) === false; |
| 91 | + |
| 92 | + |
| 93 | + if ( is_numeric( $value ) || |
| 94 | + ( $date && $date_errors['warning_count'] == 0 && $date_errors['error_count'] == 0 ) || |
| 95 | + in_array( $value, $copy_values ) || |
| 96 | + is_serialized( $value ) || |
| 97 | + null || |
| 98 | + empty( $value ) || |
| 99 | + // Check if the value is an email or a URL. |
| 100 | + filter_var( $value, FILTER_VALIDATE_EMAIL ) || |
| 101 | + strpos( $value, 'http' ) !== false |
| 102 | + || $isHashString |
| 103 | + ) { |
| 104 | + $translation_preferences[ $custom_field ] = 'copy'; |
| 105 | + } else { |
| 106 | + $translation_preferences[ $custom_field ] = 'translate'; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return $translation_preferences; |
| 111 | + } |
| 112 | + |
| 113 | + public function wpml_cf_generate_xml() { |
| 114 | + |
| 115 | + check_ajax_referer( 'wpml_cf_nonce', 'wpml_cf_nonce' ); |
| 116 | + |
| 117 | + // Prepare the base of your XML |
| 118 | + $wpml_config = '<wpml-config><custom-fields>'; |
| 119 | + foreach ( $_POST['cf'] as $custom_field => $preference ) { |
| 120 | + $custom_field = sanitize_text_field( $custom_field ); |
| 121 | + $preference = sanitize_text_field( $preference ); |
| 122 | + |
| 123 | + $wpml_config .= "<custom-field action=\"$preference\">$custom_field</custom-field>"; |
| 124 | + } |
| 125 | + |
| 126 | + $wpml_config .= '</custom-fields></wpml-config>'; |
| 127 | + |
| 128 | + // Create the XML file |
| 129 | + $formatted_xml = $this->format_xml( $wpml_config ); |
| 130 | + |
| 131 | + echo $formatted_xml; |
| 132 | + |
| 133 | + wp_die(); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + public function format_xml( $xml_string ) { |
| 138 | + $dom = new DOMDocument; |
| 139 | + $dom->preserveWhiteSpace = false; |
| 140 | + $dom->loadXML( $xml_string ); |
| 141 | + $dom->formatOutput = true; |
| 142 | + |
| 143 | + return htmlentities( $dom->saveXML( $dom->documentElement ) ); |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments