php - json_decode as a global variable

I am trying to make some information widget for the console on client sites (wordpress). The code below works as I need it, perfectly.
$url = 'https://example.site/wp-json/';
function _dashboard_clients_info()
{
global $url;
$request = wp_remote_get(esc_url_raw($url));
if (is_wp_error($request)) {
return false;
}
$html = wp_remote_retrieve_body($request);
$data = json_decode($html);
$head = current(array_filter($data, function ($current) {
return $current->_ID == 2;
}));
$posts = $data;
$foot = current(array_filter($data, function ($current) {
return $current->_ID == 3;
}));
$exclude = array(1, 2, 3, 4);
if (!empty($head->dash_description)) {
echo wpautop('<div class="dash_head">' . $head->dash_description . '</div>');
echo $head->html_css_js;
} else {
};
foreach ($posts as $post) {
if (!in_array($post->_ID, $exclude)) {
if (!empty($posts)) {
echo '<div class="dash_post">';
echo '<h3 class="dash_title">' . $post->dash_title . '</h3>';
echo wpautop('<div class="dash_description">' . $post->dash_description . '</div>');
echo $post->html_css_js;
echo '</div>';
}
} else {
};
}
if (!empty($foot->dash_description)) {
echo wpautop('<div class="dash_foot">' . $foot->dash_description . '</div>');
echo $foot->html_css_js;
} else {
};
}
function _add_dashboard_clients_widget()
{
global $url;
$request = wp_remote_get(esc_url_raw($url));
if (is_wp_error($request)) {
return false;
}
$html = wp_remote_retrieve_body($request);
$data = json_decode($html);
$title = current(array_filter($data, function ($current) {
return $current->_ID == 1;
}));
if (!empty($title->dash_description)) {
$title = '<div class="dash_title">' . $title->dash_description . '</div>';
} else {
};
add_meta_box('dashboard-clients-info', '' . $title . '', '_dashboard_clients_info', $screen, 'normal', 'high');
}
add_action('wp_dashboard_setup', '_add_dashboard_clients_widget');
But I understand that it is not perfect. In particular, I have to include$url
twice to get the widget title and body.
I would like to make the$data
variable global in order to get the$url
once, and then take what me need. I tried it like this, but for some reason it doesn't work, it doesn't return anything.
$url = 'https://example.site/wp-json/';
$request = wp_remote_get(esc_url_raw($url));
if (is_wp_error($request)) {
return false;
}
$html = wp_remote_retrieve_body($request);
$data = json_decode($html);
function _dashboard_clients_info()
{
global $data;
$head = current(array_filter($data, function ($current) {
return $current->_ID == 2;
}));
$posts = $data;
$foot = current(array_filter($data, function ($current) {
return $current->_ID == 3;
}));
$exclude = array(1, 2, 3, 4);
if (!empty($head->dash_description)) {
echo wpautop('<div class="dash_head">' . $head->dash_description . '</div>');
echo $head->html_css_js;
} else {
};
foreach ($posts as $post) {
if (!in_array($post->_ID, $exclude)) {
if (!empty($posts)) {
echo '<div class="dash_post">';
echo '<h3 class="dash_title">' . $post->dash_title . '</h3>';
echo wpautop('<div class="dash_description">' . $post->dash_description . '</div>');
echo $post->html_css_js;
echo '</div>';
}
} else {
};
}
if (!empty($foot->dash_description)) {
echo wpautop('<div class="dash_foot">' . $foot->dash_description . '</div>');
echo $foot->html_css_js;
} else {
};
}
function _add_dashboard_clients_widget()
{
global $data;
$title = current(array_filter($data, function ($current) {
return $current->_ID == 1;
}));
if (!empty($title->dash_description)) {
$title = '<div class="dash_title">' . $title->dash_description . '</div>';
} else {
};
add_meta_box('dashboard-clients-info', 'test', '_dashboard_clients_info', $screen, 'normal', 'high');
}
add_action('wp_dashboard_setup', '_add_dashboard_clients_widget');
I will be grateful for any help in improving this. I'm just learning, I try to get knowledge in this way)))
Answer
Solution:
You can easily define your API URL inwp-config.php
, in the theme'sfunctions.php
or in a plugin's 'root' file (my-plugin.php
for ex.):
define( 'DASHBOARD_API_URL', 'https://example.site/wp-json/' );
Then, create a method to receive the dashboard data making use of the definedDASHBOARD_API_URL
:
function wp68412621_get_dashboard_data() {
$response = wp_remote_get( DASHBOARD_API_URL );
if ( is_wp_error( $response ) ) {
return false;
}
return wp_remote_retrieve_body( $response );
}
You should make use of transients to cache the API response in order to avoid excessive API calls. Let's adjust the previous method to:
function wp68412621_get_dashboard_data() {
$transient_key = 'dashboard_data';
$dashboard_data = get_transient( $transient_key );
if ( false === $dashboard_data ) {
$response = wp_remote_get( DASHBOARD_API_URL );
if ( is_wp_error( $response ) ) {
return false;
}
$dashboard_data = wp_remote_retrieve_body( $response );
set_transient( $transientKey, $dashboard_data, 900 );
}
return $dashboard_data;
}
Then, you can call the data method from any other method:
function wp68412621_dashboard_clients_info()
{
$data = wp68412621_get_dashboard_data();
if ( empty( $data ) ) {
return false;
}
// ...
}
function wp68412621_add_dashboard_clients_widget()
{
$data = wp68412621_get_dashboard_data();
if ( empty( $data ) ) {
return false;
}
// ...
}
add_action( 'wp_dashboard_setup', 'wp68412621_add_dashboard_clients_widget' );
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: unable to determine current zabbix database version: the table "dbversion" was not found.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.