上周我重新发布了 Laravel 新闻,新网站运行在 Laravel 上,后端使用 WordPress。过去两年我一直使用 WordPress,并且越来越喜欢它提供的功能。发布体验、媒体管理器、移动应用程序和 Jetpack 用于跟踪统计信息。
我还没有准备好放弃这些功能,而且我没有时间构建自己的系统,所以我决定保留 WordPress 并使用 API 插件来提取我需要的所有内容,然后将它们存储在我的 Laravel 应用程序中。在本教程中,我想概述我是如何设置这一切的。
请记住,有几种不同的方法可以做到这一点,您可以查看 WordPress 和 Laravel 文章,以获取其他人创建的用于解决同一任务的第三方软件包和资源列表。主要选项是连接 WordPress 数据库,从 API 拉取或将数据从一个系统同步到另一个系统。
我选择同步选项,因为我希望一切都分开。在研究时,我偶然发现了一个名为 WordPressToLaravel 的软件包,它可以做到这一点,但是,它只适用于 wordpress.com 托管网站。
我决定为使用自托管 WordPress 安装创建自己的系统。这种方法需要在前期做更多工作,但它允许我的网站成为一个典型的 Laravel 应用程序,我可以轻松地根据需要进行改进和扩展。
在我的内容同步完成后,我通过 Laravel Scheduler 将其设置为定期任务,因此它是完全自动化的。
我的设置也是完全根据我的需求定制的。我只想拉取帖子、类别和标签。页面和其他部分都由静态 Blade 文件或其他 API 驱动。
让我们来看看它是如何工作的。
WordPress API
WordPress 并没有自带 API;但是,社区构建了一个名为 WP Rest API 的插件,允许任何博客拥有一个 jSON API。
在我的情况下,我正在进行不需要任何身份验证的只读请求。这使得读取和获取数据变得容易,并且简化了许多代码。
这是我用来获取帖子列表的基本类
class WpApi{ protected $url = 'http://site.com/wp-json/wp/v2/'; public function importPosts($page = 1) { $posts = collect($this->getJson($this->url . 'posts/?_embed&filter[orderby]=modified&page=' . $page)); foreach ($posts as $post) { $this->syncPost($post); } } protected function getJson($url) { $response = file_get_contents($url, false); return json_decode( $response ); }}
现在,当您调用 WpAPI->importPosts()
时,它将获取第一页结果。查询字符串值得一提,因为它包含一些特殊的子句。第一个是 _embed
,它将嵌入所有额外的元数据,例如图像嵌入、类别和标签。下一个是按最后修改时间排序的过滤器,这样,当您在 WordPress 上编辑帖子时,它将出现在结果的第一页,这意味着它将被重新同步。
接下来,我们需要能够将帖子与我们的数据库同步。这是我的设置方式
protected function syncPost($data){ $found = Post::where('wp_id', $data->id)->first(); if (! $found) { return $this->createPost($data); } if ($found and $found->updated_at->format("Y-m-d H:i:s") < $this->carbonDate($data->modified)->format("Y-m-d H:i:s")) { return $this->updatePost($found, $data); }} protected function carbonDate($date){ return Carbon::parse($date);}
在此步骤中,我在自己的 Posts 表中添加了一个 wp_id
字段,这样我的本地数据库和 WordPress 之间就有一对一的关系。
接下来,我只需检查它是否存在,如果不存在,就创建它。否则,如果它自上次同步后被修改,则更新它。
createPost
和 updatePost
是典型的 Laravel Eloquent 插入或更新。这是创建的代码
protected function createPost($data){ $post = new Post(); $post->id = $data->id; $post->wp_id = $data->id; $post->user_id = $this->getAuthor($data->_embedded->author); $post->title = $data->title->rendered; $post->slug = $data->slug; $post->featured_image = $this->featuredImage($data->_embedded); $post->featured = ($data->sticky) ? 1 : null; $post->excerpt = $data->excerpt->rendered; $post->content = $data->content->rendered; $post->format = $data->format; $post->status = 'publish'; $post->publishes_at = $this->carbonDate($data->date); $post->created_at = $this->carbonDate($data->date); $post->updated_at = $this->carbonDate($data->modified); $post->category_id = $this->getCategory($data->_embedded->{"wp:term"}); $post->save(); $this->syncTags($post, $data->_embedded->{"wp:term"}); return $post;}
如果您仔细观察,就会发现一些特殊情况,例如作者、特色图片、类别和标签。这些来自原始查询字符串中的 _embed
,同步这些数据只是做与之前相同的事情。
public function featuredImage($data){ if (property_exists($data, "wp:featuredmedia")) { $data = head($data->{"wp:featuredmedia"}); if (isset($data->source_url)) { return $data->source_url; } } return null;} public function getCategory($data){ $category = collect($data)->collapse()->where('taxonomy', 'category')->first(); $found = Category::where('wp_id', $category->id)->first(); if ($found) { return $found->id; } $cat = new Category(); $cat->id = $category->id; $cat->wp_id = $category->id; $cat->name = $category->name; $cat->slug = $category->slug; $cat->description = ''; $cat->save(); return $cat->id;} private function syncTags(Post $post, $tags){ $tags = collect($tags)->collapse()->where('taxonomy', 'post_tag')->pluck('name')->toArray(); if (count($tags) > 0) { $post->setTags($tags); }}
对于类别,我提取了帖子分配的第一个类别,因为我只希望每个类别有一个帖子,然后在同步标签中,我使用了 Cartalyst 的标签包。
创建计划任务
完成导入的最后一步是构建一个计划任务来自动拉取帖子。我通过 Artisan 创建了一个名为 Importer 的命令
php artisan make:console Importer
然后在 handle 方法中
$page = ($this->argument('page')) ? $this->argument('page') : 1;$this->wpApi->importPosts($page);
最后,在 Console/Kernel 中将其设置为每分钟运行一次
$schedule->command('import:wordpress') ->everyMinute();
现在,每天的每分钟,它都会尝试同步数据,并创建、更新或忽略帖子。
更进一步
这是我设置此功能的基本概述,它只触及了可以做到的所有事情的表面。例如,我正在对网站内的所有 DB 查询进行大量缓存,并且在此同步过程中,如果更新了某些内容,则会清除相关的缓存。
我希望这能帮助您了解,使用 WordPress 作为后端并不难管理,同时还能为您提供许多好处,例如从移动设备创建帖子、使用媒体管理器,甚至使用 Jetpack 以 Markdown 形式编写。
