adiungo / integrations-wordpress
掌握您的在线身份,并自动将其所有在线内容存档到他们的网站上
dev-main
2023-01-02 16:20 UTC
Requires
- adiungo/core: ^0.1.3
Requires (Dev)
- adiungo/tests: ^0.1.0
This package is auto-updated.
Last update: 2024-08-30 01:38:24 UTC
README
WordPress集成使得将WordPress网站上的内容索引到您自己的网站上成为可能。
安装
composer require adiungo/integrations/wordpress
使用
此集成提供了一个工厂类,您可以在自己的平台上使用它来索引网站上的内容。假设您正在创建一个WordPress插件,可以从其他WordPress网站上获取帖子。
首先,我们必须为REST集成提供一种实际获取数据的方法。Adiungo没有提供实际进行REST请求的方法。这是平台的责任,因此您需要在自己的插件中有一个类似于Http_Strategy
的类。我们稍后会使用这个类。
use Adiungo\Core\Abstracts\Http_Strategy; // This is needed for REST, so it knows how to make requests. class WordPress_Http_Strategy extends Http_Strategy { /** * Uses the provided request to make a wp_remote_* request. * Returns the response body, as a string. * * @return string */ public function to_string(): string { // This would probably use the WordPress Requests class to get the body. https://developer.wordpress.org/reference/classes/requests/ } public function __toString() { return $this->to_string(); } }
好的,现在我们已经创建了一个描述如何通过REST实际获取数据的类,让我们使用WordPress集成。在这种情况下,我们可以使用WordPress集成中的WordPress_Rest_Strategy_Factory
类来构建大部分REST策略。我们只需要提供我们的HTTP策略,它就可以使用了。
$factory = (new WordPress_Rest_Strategy_Factory())->set_http_strategy(new WordPress_Http_Strategy());
完成之后,您可以使用工厂来构建您需要的任意数量的索引策略。每个策略都与用于进行REST请求的不同URL相关联,
use Underpin\Factories\Url; // First, Create our actual Index Strategy. $factory = (new WordPress_Rest_Strategy_Factory())->set_http_strategy(new WordPress_Http_Strategy()); // Now, specify the REST URL. This should include any filters that provides the necessary specificity to ensure you don't get content that isn't yours. $url = Url::from('https://blog.example.org/wp-json/wp/v2/posts?author=1'); // Use that URL in your strategy. $strategy = $factory->build($url, new DateTime());
现在您已经拥有了一个完整的索引策略,只要您已经注册了模型保存事件,就可以进行如下操作
// Index all the things. This would continually fetch the data from the strategy, and index it until there's nothing left to fetch. while ($strategy->get_data_source()->has_more()) { $strategy->index_data(); } // Index a specific record. $strategy->index_item(123);