-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThe Code.php
More file actions
75 lines (66 loc) · 2.16 KB
/
The Code.php
File metadata and controls
75 lines (66 loc) · 2.16 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
<?php
/**
* Parse wordpress visual composer / wordpress bakery shortcode markup into workable array
*
* @author Lawrence Cherone
* @link https://github.com/lcherone/vc-parse
*
* @usage <?php print_r(vc_parse('[vc_row vc_row_background=""]...'));
* @param string $str
* @returns array (or breaks)
*/
function vc_parse($str) {
// fix conflicts
$str = str_replace(['vc_row_inner', 'vc_column_inner'], ['vc_r_inner', 'vc_c_inner'], $str);
//
preg_match_all("/(?<=\[).+?(?=\])/", $str, $matches, PREG_PATTERN_ORDER);
//
$structure = array_reverse($matches[0]);
//
$result = [];
//
$vc_elements = [
'vc_row', 'vc_r_inner', 'vc_column', 'vc_c_inner'
];
if (!function_exists('vc_parse_shortcode')) {
function vc_parse_shortcode($str) {
//
$parts = explode(' ', $str);
$key = $parts[0];
unset($parts[0]);
//
$str = implode(' ', $parts);
preg_match_all('/(.*?)="(.*?)"/', $str, $matches);
return [
'shortcode' => $key,
'properties' => array_combine(array_map('trim', $matches[1]), $matches[2])
];
}
}
if (!function_exists('vc_parse_recursion')) {
function vc_parse_recursion($set, $vc_elements) {
$result = [];
$total = count($set);
for ($i = 0; $i < $total; $i++) {
$last_type = explode(' ', substr($set[$i], 1))[0];
if (substr($set[$i], 0, 1) === '/' && in_array($last_type, $vc_elements)) {
$last = explode(' ', substr($set[$i], 1))[0];
$subset = [];
for ($ii = $i+1; $ii < $total; $ii++) {
if (substr($set[$ii], 0, strlen($last)) === $last) {
break;
} else {
$subset[] = $set[$ii];
}
}
$i = $i + (count($subset) + 1);
$result[] = array_reverse(vc_parse_recursion($subset, $vc_elements));
} elseif(substr($set[$i], 0, 1) !== '/') {
$result[] = vc_parse_shortcode($set[$i]);
}
}
return $result;
}
}
return array_reverse(vc_parse_recursion($structure, $vc_elements));
}