cherrypulp/laravel-wordpress 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

cherrypulp/laravel-wordpress

Composer 安装命令:

composer require cherrypulp/laravel-wordpress

包简介

Package helper for Wordpress integration into your Laravel project using direct DB, GraphQL or Rest integration

README 文档

README

Build Status Scrutinizer Code Quality

Packagist Packagist Packagist

Laravel helper for Wordpress integration into your project. It's a wrapper to implement 3 solutions : using direct database connection (using Corcel), using the RestAPI (using WP-Json) and using GraphQL Wordpress Plugin.

Why having 3 solutions and not using only one ?

Corcel access directly to the WP Database to make queries that grant definitely a better db access for quering post but it doesn't render any native wordpress scripts/hooks like Gutenberg rendering or plugins filters see issue.

The strategy for that is to use the WpGraphQL Api (strongly typed) or Wp Rest Api (weakly typed) to allow special rendering through the Wordpress so you can have benefits from these 3 solutions in your Laravel Project.

Installation

Install via composer

composer require cherrypulp/laravel-wordpress

Register Service Provider

Note! This and next step are optional if you use laravel>=5.5 with package auto discovery feature.

Add service provider to config/app.php in providers section

Cherrypulp\LaravelWordpress\ServiceProvider::class,

Register Facade

Register package facade in config/app.php in aliases section

Cherrypulp\LaravelWordpress\Facades\LaravelWordpress::class,

Publish Configuration File

php artisan vendor:publish --provider="Cherrypulp\LaravelWordpress\ServiceProvider" --tag="config"

Setup the database

To make it work, you must set the database connection to your Wordpress and add https://github.com/wp-graphql/wp-graphql.

Then in your .env file you must define :

WP_GRAPHQL_URL="http://xxxx/wp/graphql"
WP_API_URL="http://xxxx/wp-json/"

# Important if you want to have access to protected endpoint
WP_API_TOKEN="http://xxxx/wp-json/" #Api Token @see https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#authentication-plugins on how to generate that
WP_API_TYPE="BEARER" #The type of Authentication used @see : https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#authentication-plugins for more information (default is Bearer Oauth type)
WP_DB_HOST="xxxx"
WP_DB_NAME="xxxx"
WP_DB_USER="xxxx"
WP_DB_PASSWORD="xxxx"

Usage

This package is a Wrapper for Laravel Corcel and Wordpress GraphQL allowing direct connection to Wordpress.

Please check the documentation for more information https://github.com/corcel/corcel, https://gitlab.com/cherrypulp/libraries/blok-graphql and https://github.com/wp-graphql/wp-graphql for more infos.

GraphQL Based query

GraphQL client is based on our package : https://gitlab.com/cherrypulp/libraries/blok-graphql.

You can basically make just a query like that :

$posts = lwp()->graphql()->query($graphqlQuery, $params);

getPostBySlug

$post = lwp()->getPostBySlug('hello-world');

Return the post by is slug

Available methods

$home = lwp()->getHome();

# Return the homepage information

$post = lwp()->getPostBySlug('hello-world');

$posts = lwp()->getHighlightedPosts();

# Get the posts marked as featured posts

$category = lwp()->getCategoryBySlug('essential');

lwp()->flush()->getHome();

# Flush all cached queries

Using REST Api

You can access through :

$posts = lwp()->http()->get('wp/v2/posts');

Under the hood it's just a wrapper on top of HTTP client of Laravel : https://laravel.com/docs/8.x/http-client#introduction

Using Corcel models

Corcel models are accessible through lwp()->{$modelName} variable.

Exemple : the model Post is accessible through lwp()->post->slug('hello-world');

For more infos, see : https://github.com/corcel/corcel

Getting deeper

How to override GraphQL query ?

You can override default GraphQL query by simply copy-paste the graphql/query.ql in the folder of this plugin into your graphql folder. You can also override the config inside config/laravel-wordpress.php

How to extends the repository ?

You can replace the default WordpressRepository by changing the config/laravel-wordpress :

``php

"repository" => MyOwnWordpressRepository::class,

``

Then you are free to implement your own class but be sure to implements the WordpressRepositoryContract interface.


use Cherrypulp\LaravelWordpress\Repositories;
use Cherrypulp\LaravelWordpress\Repositories\WordpressRepositoryContract;

class MyOwnWordpressRepository extends WordpressRepository implements WordpressRepositoryContract {

}

Security

If you discover any security related issues, please email instead of using the issue tracker.

Credits

This package is bootstrapped with the help of cherrypulp/laravel-package-generator.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固