AdjustIdsHooks.php
1.47 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
<?php
namespace WPML\PB\Shortcode;
use WPML\Convert\Ids;
use WPML\FP\Fns;
use WPML\FP\Obj;
use WPML\LIB\WP\Hooks;
use WPML_PB_Config_Import_Shortcode;
use function WPML\FP\curryN;
use function WPML\FP\spreadArgs;
class AdjustIdsHooks implements \IWPML_Frontend_Action, \IWPML_DIC_Action {
/**
* @var WPML_PB_Config_Import_Shortcode $config
*/
private $config;
public function __construct( WPML_PB_Config_Import_Shortcode $config ) {
$this->config = $config;
}
public function add_hooks() {
Hooks::onFilter( 'pre_do_shortcode_tag', - PHP_INT_MAX, 4 )
->then( spreadArgs( Fns::withoutRecursion( Fns::identity(), [ $this, 'convertAttributeIds' ] ) ) );
}
/**
* @param false|string $bool
* @param string $tag
* @param array $attr
* @param array $m
*
* @return false|string
*/
public function convertAttributeIds( $bool, $tag, $attr, $m ) {
$tagConfig = $this->getConfig( $tag );
if ( $tagConfig ) {
$convert = curryN( 2, function( $type, $arr ) {
return $arr[1] . Ids::convert( $arr[2], $type, true );
} );
foreach ( $tagConfig as $attribute => $type ) {
$convertTypeIds = $convert( $type );
$m = preg_replace_callback( '/(' . $attribute . '=[\"\']{1})([^\"\']+)/', $convertTypeIds, $m );
}
return do_shortcode_tag( $m );
}
return $bool;
}
/**
* @param string $tag
*
* @return array|null
*/
private function getConfig( $tag ) {
return Obj::prop( $tag, $this->config->get_id_settings() );
}
}