init.php
176 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite\Compatibility\Wpml; |
| 3 | |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | /** |
| 7 | * Init |
| 8 | * Initiate all necessary classes, hooks, configs. |
| 9 | * |
| 10 | * @since 1.2.6 |
| 11 | */ |
| 12 | class Init { |
| 13 | |
| 14 | /** |
| 15 | * @var self |
| 16 | */ |
| 17 | private static $instance; |
| 18 | |
| 19 | /** |
| 20 | * Ensures only one instance of the plugin class is loaded or can be loaded. |
| 21 | * |
| 22 | * @since 1.2.6 |
| 23 | * @return self |
| 24 | */ |
| 25 | public static function instance() { |
| 26 | if ( is_null( self::$instance ) ) { |
| 27 | self::$instance = new self(); |
| 28 | } |
| 29 | |
| 30 | return self::$instance; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @since 1.2.6 |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { |
| 38 | add_filter( 'elementor/documents/get/post_id', [ $this, 'wpml_template_translation' ] ); |
| 39 | } |
| 40 | |
| 41 | // Brace-to-span bridge for every ElementsKit field that uses the "{...}" focused-text |
| 42 | // syntax (\ElementsKit_Lite\Utils::kspan): Heading title, Heading shadow text and |
| 43 | // Testimonial designation. WPML registers these under generic page-builder keys |
| 44 | // (e.g. "package-string-3-810"), so the conversion is gated on the brace/span token |
| 45 | // in the value, not on the field key — which makes it cover every kspan field. |
| 46 | add_filter( 'wpml_tm_translation_job_data', [ $this, 'rewrite_job_data' ], 10, 2 ); |
| 47 | add_filter( 'wpml_tm_job_fields', [ $this, 'rewrite_job_fields' ], 10, 2 ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Outbound: rewrite the field value sent to ATE when the job is created so the word |
| 52 | * inside "{...}" is exposed for translation instead of treated as a placeholder token. |
| 53 | */ |
| 54 | public function rewrite_job_data( $package, $post ) { |
| 55 | if ( empty( $package['contents'] ) || ! is_array( $package['contents'] ) ) { |
| 56 | return $package; |
| 57 | } |
| 58 | |
| 59 | foreach ( $package['contents'] as &$field ) { |
| 60 | if ( empty( $field['data'] ) ) { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | $is_base64 = isset( $field['format'] ) && 'base64' === $field['format']; |
| 65 | $value = $is_base64 ? base64_decode( $field['data'] ) : $field['data']; |
| 66 | |
| 67 | if ( false === strpos( $value, '{' ) ) { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | $value = $this->braces_to_span( $value ); |
| 72 | $field['data'] = $is_base64 ? base64_encode( $value ) : $value; |
| 73 | } |
| 74 | unset( $field ); |
| 75 | |
| 76 | return $package; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Inbound: rewrite the translated value back to braces before it is applied. |
| 81 | */ |
| 82 | public function rewrite_job_fields( $fields, $job ) { |
| 83 | if ( empty( $fields ) ) { |
| 84 | return $fields; |
| 85 | } |
| 86 | |
| 87 | foreach ( $fields as &$field ) { |
| 88 | $translated = $this->get_field_prop( $field, 'field_data_translated' ); |
| 89 | |
| 90 | if ( '' === $translated || false === strpos( $translated, '<span>' ) ) { |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | $is_base64 = 'base64' === $this->get_field_prop( $field, 'field_format' ); |
| 95 | $decoded = $is_base64 ? base64_decode( $translated ) : $translated; |
| 96 | $decoded = $this->span_to_braces( $decoded ); |
| 97 | $encoded = $is_base64 ? base64_encode( $decoded ) : $decoded; |
| 98 | |
| 99 | $this->set_field_prop( $field, 'field_data_translated', $encoded ); |
| 100 | } |
| 101 | unset( $field ); |
| 102 | |
| 103 | return $fields; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Read a property from a job field, whether it's an object or an array. |
| 108 | * |
| 109 | * @param object|array $field |
| 110 | * @param string $key |
| 111 | * @return mixed |
| 112 | */ |
| 113 | private function get_field_prop( $field, $key ) { |
| 114 | if ( is_object( $field ) ) { |
| 115 | return $field->$key ?? ''; |
| 116 | } |
| 117 | |
| 118 | return $field[ $key ] ?? ''; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Write a property on a job field, whether it's an object or an array. |
| 123 | * |
| 124 | * @param object|array $field |
| 125 | * @param string $key |
| 126 | * @param mixed $value |
| 127 | */ |
| 128 | private function set_field_prop( &$field, $key, $value ) { |
| 129 | if ( is_object( $field ) ) { |
| 130 | $field->$key = $value; |
| 131 | } else { |
| 132 | $field[ $key ] = $value; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Collapse "{{report}}" or "{report}" into a single <span>report</span> (mirrors |
| 138 | * \ElementsKit_Lite\Utils::kspan) so ATE sees an inline tag wrapping translatable text, |
| 139 | * not a placeholder token. Lone/unbalanced braces are left untouched. |
| 140 | * |
| 141 | * @param string $text |
| 142 | * @return string |
| 143 | */ |
| 144 | public function braces_to_span( $text ) { |
| 145 | return preg_replace( '/\{+([^{}]*)\}+/', '<span>$1</span>', $text ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Restore to single-brace "{report}" form; kspan collapses braces to one span on render, |
| 150 | * so single braces reproduce the original focused-title output identically. |
| 151 | * |
| 152 | * @param string $text |
| 153 | * @return string |
| 154 | */ |
| 155 | public function span_to_braces( $text ) { |
| 156 | return preg_replace( '/<span>(.*?)<\/span>/s', '{$1}', $text ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the ID in the current language or in another language you specify. |
| 161 | * |
| 162 | * @param int $element_id |
| 163 | * @return int|array Object id, or array of object ids. |
| 164 | * @since 2.6.1 |
| 165 | */ |
| 166 | public function wpml_template_translation( $element_id ) { |
| 167 | $element_type = get_post_type( $element_id ); |
| 168 | |
| 169 | if ( in_array( $element_type, [ 'elementskit_template', 'elementskit_content' ], true ) ) { |
| 170 | return apply_filters( 'wpml_object_id', $element_id, $element_type, true ); |
| 171 | } |
| 172 | |
| 173 | return $element_id; |
| 174 | } |
| 175 | } |
| 176 |