承接 eslamfaroug/laravel-wordpress-sync 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eslamfaroug/laravel-wordpress-sync

Composer 安装命令:

composer require eslamfaroug/laravel-wordpress-sync

包简介

A Laravel package to sync articles with WordPress via API based on language.

README 文档

README

License: MIT

Laravel WordPress Sync is a Laravel package designed to sync Laravel posts with WordPress using the REST API based on specific conditions and publication status. You can easily customize the fields that are sent to WordPress and define the sync conditions.

Requirements

  • Laravel 8.x or higher
  • PHP 7.4 or higher
  • GuzzleHTTP 7.0 or higher
  • WordPress 4.7 or higher

Installation

You can install the package via Composer:

composer require eslamfaroug/laravel-wordpress-sync

Usage Instructions

1. Setting Up the Model

After installing the package, the first step is to set up your Laravel model. Let's assume you have a model named Post.

2. Implement WordpressSyncInterface and Use the SyncsWithWordpress Trait

Modify your Post model to implement WordpressSyncInterface and use the SyncsWithWordpress trait.

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use EslamFaroug\LaravelWordpressSync\SyncsWithWordpress;
use EslamFaroug\LaravelWordpressSync\WordpressSyncInterface;

class Post extends Model implements WordpressSyncInterface
{
    use HasFactory, SyncsWithWordpress;

    protected $fillable = [
        'id', 'type', 'lang', 'title', 'content', 'excerpt', 'publish', 'user_id', 'views',
        'created_at', 'updated_at'
    ];
    
    /**
     * Define the fields to be sent to WordPress
     */
    public function getWordpressFieldsMapping()
    {
        return [
            'title'   => 'title',       // The 'title' field in WordPress maps to the 'title' field in the model
            'content' => 'content',     // The 'content' field in WordPress maps to the 'content' field in the model
            'excerpt' => 'excerpt',     // The 'excerpt' field in WordPress maps to the 'excerpt' field in the model
            'author'  => 'user_id',     // The 'author' field in WordPress maps to the 'user_id' field in the model
        ];
    }

    /**
     * Define the condition for syncing with WordPress
     */
    public function shouldSyncWithWordpress()
    {
        // Sync only if 'publish' is 'true' and the view count is greater than 100
        return $this->publish === 'true' && $this->views > 100;
    }

    /**
     * Define the name of the status field in the model
     */
    public function getStatusField()
    {
        return 'publish'; // The status field in the model is named 'publish'
    }
}

3. Defining the Fields to Send to WordPress

In getWordpressFieldsMapping(), you define the fields in the Post model that you want to send to WordPress and the corresponding fields in WordPress.

4. Defining the Sync Condition

In shouldSyncWithWordpress(), you define a specific condition that must be met for the post to be sent to WordPress. For example, syncing may occur only if the publish status is true and the views count is greater than 100.

5. Defining the Status Field

In getStatusField(), you define the name of the status field in the Post model. This field will be used to determine whether the post will be published (publish) or saved as a draft (draft) in WordPress.

6. Configuring the WordPress API Settings

To make the library more flexible, you can configure the WordPress domain and API path in the .env file of your Laravel project. Add the following entries to your .env file:

WORDPRESS_URL=https://yourwordpresssite.com
WORDPRESS_PATH=/wp-json/wp/v2/
WORDPRESS_USERNAME=your-username
WORDPRESS_PASSWORD=your-application-password
  • WORDPRESS_URL: The domain of your WordPress site.
  • WORDPRESS_PATH: The path to the WordPress REST API.
  • WORDPRESS_USERNAME: Your WordPress username for API authentication.
  • WORDPRESS_PASSWORD: The application password or API key.

These environment variables will be used by the package to connect to your WordPress site.

7. Setting Up Migrations and Service Provider

The package includes migrations to create necessary database tables for managing WordPress posts. To ensure these migrations are run in your Laravel application, make sure the LaravelWordpressSyncServiceProvider is registered.

You can publish the migrations using the following command:

php artisan vendor:publish --tag=migrations

This will copy the migration files to your application's database/migrations directory. Once published, you can run the migrations with:

php artisan migrate

Alternatively, the migrations will be automatically loaded from the package when you run the migrate command if you prefer not to publish them.

8. Testing the Sync

After completing these steps, posts will automatically sync with WordPress when they are created, updated, or deleted, based on the conditions defined in the Post model.

License

This package is distributed under the MIT License. See the LICENSE file for more information.

eslamfaroug/laravel-wordpress-sync 适用场景与选型建议

eslamfaroug/laravel-wordpress-sync 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 eslamfaroug/laravel-wordpress-sync 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 eslamfaroug/laravel-wordpress-sync 我们能提供哪些服务?
定制开发 / 二次开发

基于 eslamfaroug/laravel-wordpress-sync 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-20