-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace-social-services-icons.php
117 lines (95 loc) · 3.38 KB
/
replace-social-services-icons.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Plugin Name: Replace Social Services Icons
* Version: 0.2.2
*/
class Replace_Social_Services_Icons
{
private static $version = '0.2.2';
private static function replace_icons(string $block_content, array $block, string $dir, $settings)
{
$replacement = null;
if (!array_key_exists('services', $settings)) {
return $block_content;
}
foreach ($settings['services'] as $serviceName => $serviceConfig) {
if ($block['attrs']['service'] === $serviceName) {
$iconPath = $serviceConfig['icon'];
if (!file_exists($iconPath)) {
continue;
}
$replacement = file_get_contents($iconPath);
break;
}
}
if (isset($replacement)) {
return preg_replace('/\<svg width(.*?)\<\/svg\>/', $replacement, $block_content);
}
return $block_content;
}
private static function get_settings()
{
$dir = get_stylesheet_directory();
$settingsFile = $dir . '/social-services.json';
if (!file_exists($settingsFile)) {
return;
}
$settings = json_decode(file_get_contents($settingsFile), true);
foreach ($settings['services'] as $serviceName => $serviceConfig) {
$filePath = null;
$icon = $serviceConfig['icon'];
if (!str_starts_with($icon, 'file:')) {
continue;
}
$filePath = substr($icon, 5);
if (str_starts_with($filePath, './')) {
$settings['services'][$serviceName]['icon'] = $dir . substr($filePath, 1);
}
}
return $settings;
}
private static function get_settings_embed_svg()
{
$settings = self::get_settings();
if (!$settings) {
return;
}
foreach ($settings['services'] as $serviceName => $serviceConfig) {
$iconPath = $serviceConfig['icon'];
$iconSVGString = file_get_contents($iconPath);
if (!$iconSVGString) {
continue;
}
$settings['services'][$serviceName]['icon'] = $iconSVGString;
}
return $settings;
}
public static function render_block_hook(string $block_content, array $block)
{
$dir = get_stylesheet_directory();
$settings = self::get_settings();
if (!$settings) {
return $block_content;
}
if (in_array($block['blockName'], [
'core/social-link',
'outermost/social-sharing-link',
])) {
return self::replace_icons($block_content, $block, $dir, $settings);
}
return $block_content;
}
public static function enqueue_block_editor_assets_hook()
{
$settings = self::get_settings_embed_svg();
if (!$settings) {
return;
}
wp_enqueue_script('replace-social-services-icons-script', plugin_dir_url(__FILE__) . 'build/index.js', [], self::$version, true);
wp_localize_script('replace-social-services-icons-script', 'themeData', [
'settings' => $settings,
]);
}
}
add_action('enqueue_block_editor_assets', ['Replace_Social_Services_Icons', 'enqueue_block_editor_assets_hook'], 100);
add_filter('render_block', ['Replace_Social_Services_Icons', 'render_block_hook'], null, 2);