0

我创建了一个插件来从 API 获取一些数据。我创建了一个自定义帖子类型,并将从 API 检索到的数据包含到帖子中。我的插件代码如下

<?php
/**
 * Plugin Name: Experts
 * Plugin URI: https://****.**
 * Description: Wordpress plugin for experts.
 * Version: 1.0
 * Author: My name
 * Author URI: https://****.**
 */

if (!defined('ABSPATH'))
{
    die;
}

class ExpertsPlugin 
{
    function __construct()
    {
        add_action('init',array($this, 'custom_post_type'));
    }

    function expert_activate()
    {
        $this->custom_post_type();
        flush_rewrite_rules();
        $this-> get_experts_from_api();
    }

    function expert_deactivate()
    {   
        flush_rewrite_rules();   
    }

    function expert_uninstall()
    {

    }

    function custom_post_type()
    {
        register_post_type('expert',['public' => true, 'label' => 'Experts', 'capability_type' => 'post']);
    }

    function get_experts_from_api()
    {
        $experts = [];
        $results = wp_remote_retrieve_body(wp_remote_get('https://api.****.**/v1/users/profiles?get=10'));
        $results = json_decode($results);
        $experts[] = $results;
        foreach($experts[0] as $expert)
        {
            foreach($expert as $profile)
         {
            $expert_slug = sanitize_title($profile->userId);

            $inserted_expert = wp_insert_post([
                'post_name' => $expert_slug,
                'post_title' => $expert_slug,
                'post_type' => 'expert',
                'post_status' => 'publish'
            ]);

            if(is_wp_error($inserted_expert))
            {
                continue;
            }
         }
        }

    }

}

if(class_exists('ExpertsPlugin '))
{
    $expertsPlugin = new ExpertsPlugin ();
}

//activation
register_activation_hook(__FILE__,array($expertsPlugin , 'expert_activate'));

//deactivation
register_deactivation_hook(__FILE__,array($expertsPlugin , 'expert_deactivate'));

//uninstall
register_uninstall_hook(__FILE__,array($expertsPlugin , 'expert_uninstall'));

这个插件可以正常工作。我的问题是因为我只get_experts_from_api()在激活插件时调用,如果更新 API 中的数据,我将无法获取新值。我该如何解决这个问题。

我尝试过的一件事是像这样在构造函数中调用函数

function __construct()
{
    add_action('init',array($this, 'custom_post_type'));
    add_action('init',array($this, 'get_experts_from_api'));
}

但是使用这种方法,每次我刷新管理页面时都会调用该函数,并且还会复制数据。

如何防止数据重复,最好的方法是什么?

4

1 回答 1

0

我认为,理想情况下,数据更新将源自源头并通过 webhook 发送到您的站点。如果您使用的 API 无法提供该容量,那么您将不得不依赖其他方式。您选择后者中的哪一个可能又取决于您的安装和应用程序以及 API 的特定特征。

刷新频率可能与任何人访问该站点一样频繁(不仅仅是管理员,如果您使用的是“init”),将足以满足您的目的,并且不会引起任何重大问题。或者它可能会产生各种类型的问题。如果是这样,您可能需要将请求限制在特定页面或其他上下文中,或者依赖 cron 作业或其他形式的外部激活。

同时,为了避免当前框架中的冗余更新,您可能会在代码中使用类似这样的内容(显然未经测试):

        $expert_slug = sanitize_title($profile->userId);

        //check for post and go to next if found
        if ( get_page_by_path( $expert_slug, OBJECT, 'expert' ) ) {

             // 'expert' - post with same slug already exists, so
             continue;

        }

        $inserted_expert = wp_insert_post([
            'post_name' => $expert_slug,
            'post_title' => $expert_slug,
            'post_type' => 'expert',
            'post_status' => 'publish'
        ]);

        //etc.

https://developer.wordpress.org/reference/functions/get_page_by_path/

于 2020-06-06T18:37:46.723 回答