Migration320.php
179 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FCF\Free\Settings\Migrations; |
| 4 | |
| 5 | use WPDesk\FCF\Free\Field\Type\FileType; |
| 6 | use WPDesk\FCF\Free\Field\Type\MultiSelectType; |
| 7 | use WPDesk\FCF\Free\Field\Type\RadioType; |
| 8 | use WPDesk\FCF\Free\Field\Type\SelectType; |
| 9 | use WPDesk\FCF\Free\Settings\Form\EditFieldsForm; |
| 10 | |
| 11 | /** |
| 12 | * {@inheritdoc} |
| 13 | */ |
| 14 | class Migration320 implements Migration { |
| 15 | |
| 16 | /** |
| 17 | * {@inheritdoc} |
| 18 | */ |
| 19 | public function get_version(): string { |
| 20 | return '3.2.0'; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * {@inheritdoc} |
| 25 | */ |
| 26 | public function up() { |
| 27 | $plugin_settings = get_option( EditFieldsForm::SETTINGS_OPTION_NAME, [] ); |
| 28 | |
| 29 | foreach ( $plugin_settings as $section_id => $section_fields ) { |
| 30 | foreach ( $section_fields as $field_id => $field_data ) { |
| 31 | switch ( $field_data['type'] ?? '' ) { |
| 32 | case RadioType::FIELD_TYPE: |
| 33 | case SelectType::FIELD_TYPE: |
| 34 | case MultiSelectType::FIELD_TYPE: |
| 35 | $field_data = $this->convert_option_string_to_options_array( $field_data ); |
| 36 | break; |
| 37 | case FileType::FIELD_TYPE: |
| 38 | $field_data = $this->convert_extensions_to_mime_types( $field_data ); |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | $plugin_settings[ $section_id ][ $field_id ] = $field_data; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | update_option( EditFieldsForm::SETTINGS_OPTION_NAME, $plugin_settings ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Rolls back migration operations. |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | public function down() { |
| 55 | $plugin_settings = get_option( EditFieldsForm::SETTINGS_OPTION_NAME, [] ); |
| 56 | |
| 57 | foreach ( $plugin_settings as $section_id => $section_fields ) { |
| 58 | foreach ( $section_fields as $field_id => $field_data ) { |
| 59 | switch ( $field_data['type'] ?? '' ) { |
| 60 | case RadioType::FIELD_TYPE: |
| 61 | case SelectType::FIELD_TYPE: |
| 62 | case MultiSelectType::FIELD_TYPE: |
| 63 | $field_data = $this->convert_options_array_to_option_string( $field_data ); |
| 64 | break; |
| 65 | case FileType::FIELD_TYPE: |
| 66 | $field_data = $this->convert_mime_types_to_extensions( $field_data ); |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | $plugin_settings[ $section_id ][ $field_id ] = $field_data; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | update_option( EditFieldsForm::SETTINGS_OPTION_NAME, $plugin_settings ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Converts "value1 : Value 1\nvalue2 : Value 2" to array structure. |
| 79 | */ |
| 80 | private function convert_option_string_to_options_array( array $field_data ): array { |
| 81 | if ( isset( $field_data['options'] ) ) { |
| 82 | return $field_data; |
| 83 | } |
| 84 | |
| 85 | $options = explode( "\n", $field_data['option'] ?? '' ); |
| 86 | $rows = []; |
| 87 | foreach ( $options as $option ) { |
| 88 | $values = explode( ':', $option ); |
| 89 | if ( ! $values ) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | $rows[] = [ |
| 94 | 'key' => trim( $values[0] ), |
| 95 | 'value' => trim( implode( ':', array_slice( $values, 1 ) ) ), |
| 96 | ]; |
| 97 | } |
| 98 | |
| 99 | $field_data['options'] = $rows; |
| 100 | if ( isset( $field_data['option'] ) ) { |
| 101 | unset( $field_data['option'] ); |
| 102 | } |
| 103 | |
| 104 | return $field_data; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Converts array structure to "value1 : Value 1\nvalue2 : Value 2". |
| 109 | */ |
| 110 | private function convert_options_array_to_option_string( array $field_data ): array { |
| 111 | if ( isset( $field_data['option'] ) ) { |
| 112 | return $field_data; |
| 113 | } |
| 114 | |
| 115 | $options = $field_data['options'] ?? []; |
| 116 | $rows = []; |
| 117 | foreach ( $options as $option ) { |
| 118 | $rows[] = sprintf( '%1$s : %2$s', $option['key'], $option['value'] ); |
| 119 | } |
| 120 | |
| 121 | $field_data['option'] = implode( "\n", $rows ); |
| 122 | if ( isset( $field_data['options'] ) ) { |
| 123 | unset( $field_data['options'] ); |
| 124 | } |
| 125 | |
| 126 | return $field_data; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Converts "jpg,pdf" to "image/jpeg,application/pdf". |
| 131 | */ |
| 132 | private function convert_extensions_to_mime_types( array $field_data ): array { |
| 133 | $values = array_map( 'trim', explode( ',', $field_data['file_types'] ?? '' ) ); |
| 134 | $mime_types = get_allowed_mime_types(); |
| 135 | $mime_values = []; |
| 136 | foreach ( $mime_types as $mime_extensions => $mime_type ) { |
| 137 | foreach ( explode( '|', $mime_extensions ) as $mime_extension ) { |
| 138 | $mime_values[ $mime_extension ] = $mime_type; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | $new_values = []; |
| 143 | foreach ( $values as $value ) { |
| 144 | if ( in_array( $value, $mime_values, true ) ) { |
| 145 | $new_values[] = $value; |
| 146 | } elseif ( isset( $mime_values[ $value ] ) ) { |
| 147 | $new_values[] = $mime_values[ $value ]; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | $field_data['file_types'] = implode( ',', array_unique( $new_values ) ); |
| 152 | return $field_data; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Converts "image/jpeg,application/pdf" to "jpg,jpeg,jpe,pdf". |
| 157 | */ |
| 158 | private function convert_mime_types_to_extensions( array $field_data ): array { |
| 159 | $values = array_map( 'trim', explode( ',', $field_data['file_types'] ?? '' ) ); |
| 160 | $mime_types = get_allowed_mime_types(); |
| 161 | $mime_values = []; |
| 162 | foreach ( $mime_types as $mime_extensions => $mime_type ) { |
| 163 | $mime_values[ $mime_type ] = explode( '|', $mime_extensions ); |
| 164 | } |
| 165 | |
| 166 | $new_values = []; |
| 167 | foreach ( $values as $value ) { |
| 168 | if ( isset( $mime_values[ $value ] ) ) { |
| 169 | $new_values[] = implode( ',', $mime_values[ $value ] ); |
| 170 | } else { |
| 171 | $new_values[] = $value; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | $field_data['file_types'] = implode( ',', array_unique( $new_values ) ); |
| 176 | return $field_data; |
| 177 | } |
| 178 | } |
| 179 |