This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwp_download_links.php
137 lines (116 loc) · 3.97 KB
/
wp_download_links.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Output the navigation footer and primary menu too a json file
*/
require_once('../../../wp/wp-load.php');
header('content-type application/json charset=utf-8');
$menus = get_registered_nav_menus();
$json = ' jsonCallback({';
foreach ($menus as $location => $description) {
if (($locations = get_nav_menu_locations()) && isset($locations[$location])) {
$menu = wp_get_nav_menu_object($locations[$location]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$json .= '"' . $location . '":[';
$count = 0;
$count_menu_items = count($menu_items);
foreach ((array)$menu_items as $key => $menu_item) {
$check_id = $menu_item->db_id;
$parent = "No";
foreach ((array)$menu_items as $value => $menu_sub) {
if ($check_id == $menu_sub->menu_item_parent){
$parent = "Yes";
break;
}
}
if ($menu_item->menu_item_parent > 0) {
$json .= '{' . '"' . 'name' . '":' . '"' . esc_attr(
$menu_item->title
) . '",' . '"' . 'link' . '":' . '"' . esc_attr(
fixUrl($menu_item->url)
) . '",' . '"' . 'id' . '":' . '"' . $menu_item->db_id . '",' . '"' . 'parent_id' . '":' . '"' . $menu_item->menu_item_parent . '",' . '"Parent":' . '"No"' . '}';
}else {
$json .= '{' . '"' . 'name' . '":' . '"' . esc_attr(
$menu_item->title
) . '",' . '"' . 'link' . '":' . '"' . esc_attr(
fixUrl($menu_item->url)
) . '",' . '"' . 'id' . '":' . '"' . $menu_item->db_id .'",'. '"Parent":' . '"' . $parent.'"'. '}';
}
if ($count != $count_menu_items - 1) {
$json .= ",";
}
$count++;
}
$json .= '],';
}
}
$json = rtrim($json, ',');
$json .= '});';
print(prettyPrint($json));
function fixUrl($url)
{
if (false === strpos($url, '//')) {
$url = home_url($url);
}
$url = str_replace(array('http://','https://'), '//', $url);
return $url;
}
/**
* @param $json
*
* @return string
*/
function prettyPrint($json)
{
$result = '';
$level = 0;
$prev_char = '';
$in_quotes = false;
$ends_line_level = null;
$json_length = strlen($json);
for ($i = 0; $i < $json_length; $i++) {
$char = $json[$i];
$new_line_level = null;
$post = "";
if ($ends_line_level !== null) {
$new_line_level = $ends_line_level;
$ends_line_level = null;
}
if ($char === '"' && $prev_char != '\\') {
$in_quotes = !$in_quotes;
} else {
if (!$in_quotes) {
switch ($char) {
case '}':
case ']':
$level--;
$ends_line_level = null;
$new_line_level = $level;
break;
case '{':
case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':':
$post = " ";
break;
case " ":
case "\t":
case "\n":
case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = null;
break;
}
}
}
if ($new_line_level !== null) {
$result .= "\n" . str_repeat("\t", $new_line_level);
}
$result .= $char . $post;
$prev_char = $char;
}
return $result;
}