承接 dhii/wp-containers 相关项目开发

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

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

dhii/wp-containers

最新稳定版本:v0.1.0-alpha1

Composer 安装命令:

composer require dhii/wp-containers

包简介

PSR-11 container implementations that wrap some WP features, for convenience and interoperability.

README 文档

README

Please check Packagist for alternatives

Build Status Code Climate Test Coverage Join the chat at https://gitter.im/Dhii/wp-containers This package complies with Dhii standards

Details

PSR-11 container implementations that wrap some WP features, for convenience and interoperability.

Features

Retrieve sites by key

use Dhii\Wp\Containers\Sites;
use WP_Site;

$sites = new Sites();
$site2 = $sites->get(2);
assert($site2 instanceof WP_Site);

Retrieve site options by key

use Dhii\Wp\Containers\Options\BlogOptions;
use Dhii\Wp\Containers\Options\BlogOptionsContainer;
use Dhii\Wp\Containers\Sites;

// Set up sites container (see other example)
// ...
assert($sites instanceof WP_Site);

// Definition
$optionsContainer = new BlogOptionsContainer(
    function ($id) {
        return new BlogOptions($id, uniqid('default-option-value'));
    },
    $sites
);

// Usage
$blog3Options = $optionsContainer->get(3);
$myOption = $blog3Options->get('my_option');

Retrieve site meta by key

use Dhii\Wp\Containers\Options\SiteMeta;
use Dhii\Wp\Containers\Options\SiteMetaContainer;
use Dhii\Wp\Containers\Sites;

// Set up sites container (see other example)
// ...
assert($sites instanceof WP_Site);

// Definition
$metaContainer = new SiteMetaContainer(
    function ($id) {
        return new SiteMeta($id, uniqid('default-meta-value'));
    },
    $sites
);

// Usage
$blog4Meta = $metaContainer->get(4);
$myMeta = $blog4Meta->get('my_meta');

Structured error handling

use Dhii\Wp\Containers\Options\BlogOptions;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Container\ContainerExceptionInterface;
use Dhii\Data\Container\Exception\NotFoundExceptionInterface as ExtendedNotFoundException;

// Set up options (see previous examples)
// ...
assert($options instanceof BlogOptions);

try {
    $options->set('other_option', 'My Value');
    $value = $options->get('my_option');
} catch (NotFoundExceptionInterface $e) {
    assert($e instanceof ExtendedNotFoundException);
    echo sprintf('Option "%1$s" does not exist', $e->getDataKey());
    assert($e->getContainer() === $options);
}

This solves the problem of inconsistent behaviour of native WordPress option-related funtions:

  • retrieved options returned false for both a false value and when not found, making them hard to distinguish;
  • setting an option returned false for both failure, an when the value is the same as the curent value, often resulting in a false error.

This is no longer the case with the above containers: option operations succeed or correctly fail by throwing PSR-11 exceptions. Furthermore, the original behaviour of these exceptions has been extended to allow retrieval of the key that was not found (when applicable) and the container that failed the operation. This is optional, however, and depending simply on the PSR-11 exceptions will work as expected.

The set(), has(), and delete() also throw ContainerExceptionInterface on failure.

Wraps WP

The containers do not re-create the functionality to go around WordPress. Instead, they wrap native WordPress functionality, so you can be sure that everything is done in the same way, all the hooks, such as option_* or pre_update_option_*, still work.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固