【问题标题】:json_decode as a global variablejson_decode 作为全局变量
【发布时间】:2021-07-16 16:48:27
【问题描述】:

我正在尝试为客户端网站 (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, 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');

我将不胜感激在改进这一点的任何帮助。我只是在学习,我尝试通过这种方式获取知识)))

【问题讨论】:

    标签: php json wordpress widget


    【解决方案1】:

    您可以在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' );
    

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 1970-01-01
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多