GroupsAndLabels.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Compatibility\WcTabManager\TranslationEditor; |
| 4 | |
| 5 | use WCML_Tab_Manager; |
| 6 | use WCML\TranslationJob\Hooks as TranslationJobHooks; |
| 7 | use WPML\FP\Obj; |
| 8 | use WPML\FP\Str; |
| 9 | |
| 10 | class GroupsAndLabels implements \IWPML_Action { |
| 11 | |
| 12 | const TAB_GROUP_SLUG = 'product-tab'; |
| 13 | const TAB_GROUP_LABEL = 'Product Tab'; |
| 14 | |
| 15 | const DEFAULT_FIELD_LABEL = 'Field'; |
| 16 | const TAB_CONTENT_LABEL = 'Content'; |
| 17 | |
| 18 | public function add_hooks() { |
| 19 | add_filter( 'wpml_tm_adjust_translation_fields', [ $this, 'adjustFields' ] ); |
| 20 | } |
| 21 | |
| 22 | public function adjustFields( $fields ) { |
| 23 | foreach ( $fields as &$field ) { |
| 24 | if ( Str::startsWith( WCML_Tab_Manager::TAB_FIELD_PREFIX, Obj::prop( 'field_type', $field ) ) ) { |
| 25 | $field = $this->adjustField( $field ); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | return $fields; |
| 30 | } |
| 31 | |
| 32 | private function adjustField( $field ) { |
| 33 | $fieldData = Str::replace( WCML_Tab_Manager::TAB_FIELD_PREFIX, '', $field['field_type'] ); |
| 34 | |
| 35 | if ( Str::startsWith( WCML_Tab_Manager::TAB_FIELD_CORE_INTERFIX, $fieldData ) ) { |
| 36 | $field = $this->adjustCoreTabField( $field ); |
| 37 | } elseif ( Str::startsWith( WCML_Tab_Manager::TAB_FIELD_PRODUCT_INTERFIX, $fieldData ) ) { |
| 38 | $field = $this->adjustProductTabField( $field ); |
| 39 | } |
| 40 | |
| 41 | return $field; |
| 42 | } |
| 43 | |
| 44 | private function adjustCoreTabField( $field ) { |
| 45 | $fieldData = Str::replace( WCML_Tab_Manager::TAB_FIELD_PREFIX . WCML_Tab_Manager::TAB_FIELD_CORE_INTERFIX, '', $field['field_type'] ); |
| 46 | $fieldParts = explode( ':', $fieldData ); |
| 47 | $name = reset( $fieldParts ); |
| 48 | $tabId = count( $fieldParts ) > 1 ? end( $fieldParts ) : false; |
| 49 | |
| 50 | return $this->adjustTabField( $field, $name, $tabId ); |
| 51 | } |
| 52 | |
| 53 | private function adjustProductTabField( $field ) { |
| 54 | $fieldData = Str::replace( WCML_Tab_Manager::TAB_FIELD_PREFIX . WCML_Tab_Manager::TAB_FIELD_PRODUCT_INTERFIX, '', $field['field_type'] ); |
| 55 | $fieldParts = explode( ':', $fieldData ); |
| 56 | $name = count( $fieldParts ) > 1 ? end( $fieldParts ) : false; |
| 57 | $tabId = reset( $fieldParts ); |
| 58 | |
| 59 | return $this->adjustTabField( $field, $name, $tabId ); |
| 60 | } |
| 61 | |
| 62 | private function adjustTabField( $field, $name, $tabId ) { |
| 63 | $getTitle = function( $slug ) { |
| 64 | if ( empty( $slug ) ) { |
| 65 | return self::DEFAULT_FIELD_LABEL; |
| 66 | } |
| 67 | if ( 'description' === $slug ) { |
| 68 | return self::TAB_CONTENT_LABEL; |
| 69 | } |
| 70 | return apply_filters( 'wpml_labelize_string', $slug, 'TranslationJob' ); |
| 71 | }; |
| 72 | |
| 73 | $getGroup = function( $id ) { |
| 74 | if ( empty( $id ) ) { |
| 75 | return self::TAB_GROUP_SLUG . '-0'; |
| 76 | } |
| 77 | return self::TAB_GROUP_SLUG . '-' . $id; |
| 78 | }; |
| 79 | |
| 80 | $group = $getGroup( $tabId ); |
| 81 | |
| 82 | $field['title'] = $getTitle( $name ); |
| 83 | $field['group'] = TranslationJobHooks::getTopLevelGroup(); |
| 84 | $field['group'][ $group ] = self::TAB_GROUP_LABEL; |
| 85 | |
| 86 | return $field; |
| 87 | } |
| 88 | |
| 89 | } |
| 90 |