BlockAttributes.php
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace WPML\PB\Gutenberg\ConvertIdsInBlock;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\PB\Gutenberg\StringsInBlock\Attributes;
class BlockAttributes extends Base {
/** @var array $attributesToConvert */
private $attributesToConvert;
public function __construct( array $attributesToConvert ) {
$this->attributesToConvert = $attributesToConvert;
}
public function convert( array $block ) {
if ( ! isset( $block['attrs'] ) ) {
return $block;
}
foreach ( $this->attributesToConvert as $attributeConfig ) {
$getConfig = Obj::prop( Fns::__, $attributeConfig );
$name = $getConfig( 'name' );
if ( $name ) {
$block['attrs'] = self::convertByName( $block['attrs'], $name, $getConfig );
} elseif ( $getConfig( 'path' ) ) {
$path = explode( '>', $getConfig( 'path' ) );
$block['attrs'] = self::convertByPath( $block['attrs'], $path, $getConfig );
}
}
return $block;
}
/**
* @param array $attrs
* @param string $name
* @param callable $getConfig
*
* @return array
*/
private function convertByName( $attrs, $name, $getConfig ) {
if ( isset( $attrs[ $name ] ) ) {
$attrs[ $name ] = self::convertIds(
$attrs[ $name ],
$getConfig( 'slug' ),
$getConfig( 'type' )
);
}
return $attrs;
}
/**
* @param array|string|int $attrs
* @param array $path
* @param callable $getConfig
*
* @return mixed
*/
private function convertByPath( $attrs, $path, $getConfig ) {
$currentKey = reset( $path );
$nextPath = Lst::drop( 1, $path );
$hasWildCard = false !== strpos( $currentKey, '*' );
if ( $hasWildCard && is_array( $attrs ) ) {
$regex = Attributes::getWildcardRegex( $currentKey );
foreach ( $attrs as $key => $attr ) {
if ( Str::match( $regex, $key ) ) {
$attrs[ $key ] = self::convertByPath( $attr, $nextPath, $getConfig );
}
}
} elseif ( $currentKey && isset( $attrs[ $currentKey ] ) ) {
$attrs[ $currentKey ] = self::convertByPath( $attrs[ $currentKey ], $nextPath, $getConfig );
} elseif ( ! $nextPath && is_scalar( $attrs ) ) {
$attrs = self::convertIds( $attrs, $getConfig( 'slug' ), $getConfig( 'type' ) );
}
return $attrs;
}
}