PHP code example of dhii / wp-containers

1. Go to this page and download the library: Download dhii/wp-containers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dhii / wp-containers example snippets


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

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

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');

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');

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);
}