微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

json_decode 作为全局变量

如何解决json_decode 作为全局变量

我正在尝试为客户端站点 (wordpress) 上的控制台制作一些信息小部件。下面的代码可以按照我的需要运行,非常完美。

$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');

但我知道它并不完美。特别是,我必须包含 $url 两次才能获取小部件标题和正文。

我想将 $data 变量设为全局变量,以便获得 $url 一次,然后获取我需要的内容。我像这样尝试过,但由于某种原因它不起作用,它没有返回任何东西。

$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,$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,'test','_add_dashboard_clients_widget');

如果您能帮助我改善这一点,我将不胜感激。我只是在学习,我尝试通过这种方式获取知识)))

解决方法

您可以在 wp-config.php、主题的 functions.php 或插件的“根”文件(例如 my-plugin.php)中轻松定义 API URL:

define( 'DASHBOARD_API_URL','https://example.site/wp-json/' );

然后,使用定义的 DASHBOARD_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 );
}

您应该使用 transients 来缓存 API 响应,以避免过多的 API 调用。我们将之前的方法调整为:

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;
}

然后,您可以从任何其他方法调用数据方法:

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' );

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。