FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ui/examples/wordpress/roxy-ui-shortcode.php at main · RoxyAPI/ui · GitHub
RoxyAPI
/
ui
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
2
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ui
/
examples
/
wordpress
/
roxy-ui-shortcode.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
130 lines (118 loc) · 4.56 KB
Breadcrumbs
ui
/
examples
/
wordpress
/
roxy-ui-shortcode.php
Copy path
File metadata and controls
130 lines (118 loc) · 4.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
/**
* Plugin Name: Roxy UI shortcode (example)
* Description: WordPress shortcode that renders a Roxy UI component. PHP fetches the response server side with your secret key and inlines it, so the key never reaches the browser.
* Version: 0.2.0
* License: MIT
*
* Usage in a post or page:
* [roxy element="horoscope-card" sign="aries" period="daily"]
* [roxy element="vedic-kundli" date="1990-01-15" time="14:30:00" latitude="28.6139" longitude="77.209" timezone="5.5"]
*
* Set your secret API key once in the roxy_api_key option. Get a key at
* https://roxyapi.com/account. The key stays on the server; only the rendered
* response reaches the page.
*/
if
(!
defined
(
'
ABSPATH
'
)) {
exit
;
}
// Load the component bundle once. It defines every roxy-* element.
add_action
(
'
wp_enqueue_scripts
'
,
function
() {
wp_enqueue_script
(
'
roxy-ui
'
,
'
https://cdn.jsdelivr.net/npm/@roxyapi/ui@latest/dist/cdn/roxy-ui.js
'
,
[],
null
,
[
'
strategy
'
=>
'
defer
'
]
);
});
/**
* Map a shortcode element to the RoxyAPI request that feeds it. Returns
* [method, path, body] or null for an unknown element.
*/
function
roxy_request_for
(
$
element
,
$
atts
) {
switch
(
$
element
) {
case
'
horoscope-card
'
:
return
[
'
GET
'
,
sprintf
(
'
/astrology/horoscope/%s/%s
'
,
rawurlencode
(
$
atts
[
'
sign
'
]),
rawurlencode
(
$
atts
[
'
period
'
])),
null
];
case
'
tarot-card
'
:
return
[
'
POST
'
,
'
/tarot/daily
'
,
new
stdClass
()];
case
'
numerology-card
'
:
$
parts
=
explode
(
'
-
'
,
$
atts
[
'
date
'
]);
return
[
'
POST
'
,
'
/numerology/life-path
'
, [
'
year
'
=> (
int
)
$
parts
[
0
],
'
month
'
=> (
int
)
$
parts
[
1
],
'
day
'
=> (
int
)
$
parts
[
2
]]];
case
'
vedic-kundli
'
:
// Production tip: geocode the birth city with GET /location/search here
// instead of passing fixed coordinates in the shortcode.
return
[
'
POST
'
,
'
/vedic-astrology/birth-chart
'
, [
'
date
'
=>
$
atts
[
'
date
'
],
'
time
'
=>
$
atts
[
'
time
'
],
'
latitude
'
=> (
float
)
$
atts
[
'
latitude
'
],
'
longitude
'
=> (
float
)
$
atts
[
'
longitude
'
],
'
timezone
'
=> (
float
)
$
atts
[
'
timezone
'
],
]];
default
:
return
null
;
}
}
add_shortcode
(
'
roxy
'
,
function
(
$
atts
) {
$
atts
=
shortcode_atts
([
'
element
'
=>
'
horoscope-card
'
,
'
sign
'
=>
'
aries
'
,
'
period
'
=>
'
daily
'
,
'
date
'
=>
'
1990-01-15
'
,
'
time
'
=>
'
14:30:00
'
,
'
latitude
'
=>
''
,
'
longitude
'
=>
''
,
'
timezone
'
=>
'
5.5
'
,
],
$
atts
);
$
element
=
sanitize_key
(
$
atts
[
'
element
'
]);
$
request
=
roxy_request_for
(
$
element
,
$
atts
);
if
(
$
request
===
null
) {
return
'
<!-- roxy: unknown element
'
.
esc_html
(
$
element
) .
'
-->
'
;
}
list
(
$
method
,
$
path
,
$
body
) =
$
request
;
$
key
=
get_option
(
'
roxy_api_key
'
,
''
);
if
(
$
key
===
''
) {
return
'
<!-- roxy: set the roxy_api_key option to your secret key -->
'
;
}
// Cache the response so repeat page views do not re-bill a call.
$
cache_key
=
'
roxy_
'
.
md5
(
$
element
.
wp_json_encode
(
$
atts
));
$
data
=
get_transient
(
$
cache_key
);
if
(
$
data
===
false
) {
$
url
=
'
https://roxyapi.com/api/v2
'
.
$
path
;
$
args
= [
'
headers
'
=> [
'
X-API-Key
'
=>
$
key
],
'
timeout
'
=>
10
];
if
(
$
method
===
'
POST
'
) {
$
args
[
'
headers
'
][
'
Content-Type
'
] =
'
application/json
'
;
$
args
[
'
body
'
] =
wp_json_encode
(
$
body
);
$
response
=
wp_remote_post
(
$
url
,
$
args
);
}
else
{
$
response
=
wp_remote_get
(
$
url
,
$
args
);
}
if
(
is_wp_error
(
$
response
) ||
wp_remote_retrieve_response_code
(
$
response
) !==
200
) {
return
'
<!-- roxy: request failed -->
'
;
}
$
data
=
wp_remote_retrieve_body
(
$
response
);
set_transient
(
$
cache_key
,
$
data
,
HOUR_IN_SECONDS
);
}
// Escape the script-unsafe characters so no string field can break out of the
// inline <script> block. Same rule as serializeRoxyData() in @roxyapi/ui: map
// <, >, and & to their \uXXXX JSON escapes, which parse back to the identical
// characters. The API body is already JSON, so this only touches string
// values, never JSON structure.
$
escaped
=
str_replace
([
'
<
'
,
'
>
'
,
'
&
'
], [
'
\u003c
'
,
'
\u003e
'
,
'
\u0026
'
],
$
data
);
$
json
=
'
<script type="application/json" class="roxy-data">
'
.
$
escaped
.
'
</script>
'
;
// Display attributes the component reads alongside data.
$
display
= [];
if
(
$
element
===
'
horoscope-card
'
) {
$
display
[
'
period
'
] =
$
atts
[
'
period
'
];
}
elseif
(
$
element
===
'
numerology-card
'
) {
$
display
[
'
type
'
] =
'
life-path
'
;
}
elseif
(
$
element
===
'
vedic-kundli
'
) {
$
display
[
'
chart-style
'
] =
'
south
'
;
}
$
attr
=
''
;
foreach
(
$
display
as
$
name
=>
$
value
) {
$
attr
.=
sprintf
(
'
%s="%s"
'
,
esc_attr
(
$
name
),
esc_attr
(
$
value
));
}
$
fallback
=
'
<noscript><a href="https://roxyapi.com">Enable JavaScript or read on RoxyAPI.</a></noscript>
'
;
return
sprintf
(
'
<roxy-%s%s>%s</roxy-%s>%s
'
,
esc_attr
(
$
element
),
$
attr
,
$
json
,
esc_attr
(
$
element
),
$
fallback
);
});
Back
|
FazBrowse Home
|
New Git URL