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 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / TranslationJob / Hooks.php
woocommerce-multilingual / classes / TranslationJob Last commit date
Hooks.php 1 week ago
Hooks.php
159 lines
1 <?php
2
3 namespace WCML\TranslationJob;
4
5 use WPML\FP\Obj;
6 use WPML\FP\Fns;
7 use WPML\FP\Str;
8 use WPML\LIB\WP\Hooks as WPHooks;
9 use WPML\TM\Jobs\FieldId;
10 use WCML\Utilities\WCTaxonomies;
11
12 use function WPML\FP\spreadArgs;
13
14 class Hooks implements \IWPML_Backend_Action, \IWPML_REST_Action {
15
16 const TOP_LEVEL_GROUP = 'wp_product';
17 const TOP_LEVEL_GROUP_LABEL = 'WooCommerce Product';
18
19 const LINKED_PRODUCTS_GROUP = 'wp_product_linked_products';
20 const LINKED_PRODUCTS_GROUP_LABEL = 'Linked Products';
21
22 public function add_hooks() {
23 WPHooks::onFilter( 'wpml_tm_adjust_translation_fields', 10, 2 )
24 ->then( spreadArgs( [ $this, 'adjustFields' ] ) );
25 WPHooks::onFilter( 'wpml_tm_adjust_translation_job', 10, 2 )
26 ->then( spreadArgs( [ $this, 'adjustGlobalAttributes' ] ) );
27 }
28
29 public function adjustFields( $fields, $job ) {
30 if ( ! self::isProduct( $job ) ) {
31 return $fields;
32 }
33
34 foreach ( $fields as &$field ) {
35 $field = $this->adjustField( $field );
36 }
37
38 return $fields;
39 }
40
41 private function adjustField( $field ) {
42 $typeStartsWith = Str::startsWith( Fns::__, Obj::prop( 'field_type', $field ) );
43
44 if ( $typeStartsWith( 'wc_attribute_name:' ) ) {
45 $field = $this->handleAttribute( 'Name', $field );
46 } elseif ( $typeStartsWith( 'wc_attribute_value:' ) ) {
47 $field = $this->handleAttribute( 'Value', $field );
48 } elseif ( $typeStartsWith( 'wc_variation_field:' ) ) {
49 $field = $this->handleVariationField( $field );
50 } elseif ( $typeStartsWith( \WCML_TP_Support::PACKAGE_IMAGE_KEY_PREFIX ) ) {
51 $field = $this->handleImage( $field );
52 } elseif ( $typeStartsWith( 'field-_downloadable_files-' ) ) {
53 $field = $this->handleDownloadableFile( $field );
54 }
55
56 return $field;
57 }
58
59 private function handleAttribute( $title, $field ) {
60 $parts = explode( ':', $field['field_type'] );
61 $group = end( $parts ) . '-attribute';
62
63 $field['title'] = $title;
64
65 $field['group'] = self::getTopLevelGroup();
66 $field['group'][ $group ] = apply_filters( 'wpml_labelize_string', $group, 'TranslationJob' );
67
68 return $field;
69 }
70
71 private function handleVariationField( $field ) {
72 $parts = explode( ':', $field['field_type'] );
73 array_shift( $parts );
74
75 $field['title'] = implode( ' #', $parts );
76
77 $field['group'] = self::getTopLevelGroup();
78 $field['group']['wc_variations'] = 'Variations Data';
79
80 return $field;
81 }
82
83 private function handleImage( $field ) {
84 list( , $imageId, $title ) = Str::match( '/^' . preg_quote( \WCML_TP_Support::PACKAGE_IMAGE_KEY_PREFIX, '/' ) . '(\d+)-(.*)$/', $field['field_type'] );
85
86 $field['title'] = $title;
87 $field['image'] = wp_get_attachment_url( $imageId );
88
89 $field['group'] = self::getTopLevelGroup();
90 $field['group'][ 'wc_image_' . $imageId ] = 'Image';
91
92 return $field;
93 }
94
95 private function handleDownloadableFile( $field ) {
96 list( , $fileNo, $fileId, $title ) = \WCML_Downloadable_Products::parseDownloadableFileField( $field['field_type'] );
97
98 $labelNo = self::convertDownloadableFileIdToNumber( $fileId );
99
100 $ex = explode( ':', $title );
101 if ( isset( $ex[1] ) ) {
102 $title = $ex[0];
103 $labelNo .= ' [' . $ex[1] . ']';
104 }
105
106 $field['title'] = $title;
107 $field['group'] = self::getTopLevelGroup();
108 $field['group'][ sprintf( 'wc_downloadable_file_%s_%s', $fileNo, $fileId ) ] = sprintf( '#%s Downloadable File', $labelNo );
109
110 return $field;
111 }
112
113 public function adjustGlobalAttributes( $fields, $job ) {
114 if ( ! self::isProduct( $job ) ) {
115 return $fields;
116 }
117
118 $isGlobalAttribute = Str::startsWith( WCTaxonomies::TAXONOMY_PREFIX_ATTRIBUTE );
119
120 foreach ( $fields as &$field ) {
121 $title = Obj::path( [ 'attributes', 'resname' ], $field );
122 $key = Obj::path( [ 'attributes', 'id' ], $field );
123 if ( $isGlobalAttribute( $title ) && FieldId::is_any_term_field( $key ) ) {
124 $title = apply_filters( 'wpml_labelize_string', substr( $title, 3 ), 'TranslationJob' );
125 $group = self::getTopLevelGroup();
126 $group['pa'] = 'Attributes';
127
128 $extradata = $field['extradata'] ?? [];
129
130 $extradata['unit'] = $title;
131 $extradata['group'] = implode( '/', array_values( $group ) );
132 $extradata['group_id'] = implode( '/', array_keys( $group ) );
133
134 $field['extradata'] = $extradata;
135 }
136 }
137
138 return $fields;
139 }
140
141 public static function isProduct( $job ) {
142 return 'post_product' === $job->original_post_type;
143 }
144
145 public static function getTopLevelGroup() {
146 return [ self::TOP_LEVEL_GROUP => self::TOP_LEVEL_GROUP_LABEL ];
147 }
148
149 private function convertDownloadableFileIdToNumber( $fileId ): int {
150 static $downloadableFileNo = [];
151
152 if ( ! isset( $downloadableFileNo[ $fileId ] ) ) {
153 $downloadableFileNo[ $fileId ] = count( $downloadableFileNo ) + 1;
154 }
155
156 return $downloadableFileNo[ $fileId ];
157 }
158 }
159