| 1 |
<?php |
| 2 |
namespace Depicter\Document\Migrations\DataMigration; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\JSON; |
| 5 |
|
| 6 |
class BackgroundImage implements MigrationInterface { |
| 7 |
|
| 8 |
public function migrate( $slider_content ) { |
| 9 |
|
| 10 |
$slider_data = JSON::isJson( $slider_content ) ? JSON::decode( $slider_content, true ) : JSON::decode( JSON::encode( $slider_content ), true ); |
| 11 |
$backgrounds = []; |
| 12 |
if ( ! empty( $slider_data['sections'] ) ) { |
| 13 |
foreach ( $slider_data['sections'] as $section_key => $section ) { |
| 14 |
if ( ! empty( $section['background']['image']['src'] ) ) { |
| 15 |
if ( is_string( $section['background']['image']['src'] ) ) { |
| 16 |
$backgrounds[ $section['background']['image']['src'] ] = JSON::encode( $section['background'], JSON_FORCE_OBJECT ); |
| 17 |
} |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
if ( ! empty( $backgrounds ) ) { |
| 23 |
|
| 24 |
$modified_data = JSON::isJson( $slider_content ) ? $slider_content : JSON::encode( $slider_content ); |
| 25 |
foreach( $backgrounds as $backgroundID => $background ) { |
| 26 |
$new_background = str_replace( '"src":"' . $backgroundID .'"', '"src":{"default":"' . $backgroundID .'"}', $background ); |
| 27 |
$modified_data = str_replace( $background, $new_background, $modified_data ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( JSON::isJson( $slider_content ) ) { |
| 31 |
return $modified_data; |
| 32 |
} else { |
| 33 |
return JSON::decode( $modified_data, false ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return $slider_content; |
| 38 |
} |
| 39 |
} |
| 40 |
|