1. Go to this page and download the library: Download devuri/wp-adapter 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/ */
devuri / wp-adapter example snippets
use AdapterKit\Core\Contracts\OptionStorageInterface;
final class SettingsService
{
private OptionStorageInterface $options;
public function __construct(OptionStorageInterface $options)
{
$this->options = $options;
}
public function enable(): void
{
$this->options->update(
'myplugin_settings',
['enabled' => true]
);
}
}
use AdapterKit\Core\Storage\WordPressOptionStorage;
$settings = new SettingsService(new WordPressOptionStorage());
use AdapterKit\Core\Testing\InMemoryOptionStorage;
use PHPUnit\Framework\TestCase;
final class SettingsServiceTest extends TestCase
{
public function test_enable_stores_the_setting(): void
{
$options = new InMemoryOptionStorage();
$settings = new SettingsService($options);
$settings->enable();
$this->assertSame(
['enabled' => true],
$options->get('myplugin_settings')
);
}
}
// Wrong: the service now ugin_settings', []);
// Right: the service depends on a contract.
$value = $this->options->get('myplugin_settings', []);
use AdapterKit\Core\Http\WordPressHttpClient;
use AdapterKit\Core\Hooks\WordPressHooks;
use AdapterKit\Core\Logging\NullLogger;
use AdapterKit\Core\PluginContext;
use AdapterKit\Core\Storage\WordPressOptionStorage;
use AdapterKit\Core\Storage\WordPressTransientStorage;
$context = PluginContext::fromPluginFile(
__FILE__,
'my-plugin',
'1.0.0',
'my-plugin',
'myplugin_'
);
$plugin = new MyPlugin\Plugin(
$context,
new WordPressHooks(),
new WordPressOptionStorage(),
new WordPressTransientStorage(),
new WordPressHttpClient(),
new NullLogger()
);
$plugin->register();
use AdapterKit\Core\Contracts\HttpClientInterface;
use AdapterKit\Core\Contracts\OptionStorageInterface;
use AdapterKit\Core\Result;
use Psr\Log\LoggerInterface;
final class LicenseService
{
private OptionStorageInterface $options;
private HttpClientInterface $http;
private LoggerInterface $logger;
private string $optionKey;
public function __construct(
OptionStorageInterface $options,
HttpClientInterface $http,
LoggerInterface $logger,
string $optionKey
) {
$this->options = $options;
$this->http = $http;
$this->logger = $logger;
$this->optionKey = $optionKey;
}
public function activate(string $key): Result
{
$response = $this->http->post(
'https://api.example.com/activate',
['body' => ['key' => $key]]
);
if ($response['is_error']) {
$message = $response['error_message'] ?? 'Activation request failed.';
$this->logger->warning('activation_failed', ['reason' => $message]);
return Result::failure('activation_failed', $message);
}
if ($response['code'] < 200 || $response['code'] >= 300) {
return Result::failure(
'activation_rejected',
'The activation server rejected the request.'
);
}
$payload = json_decode($response['body'], true);
if (!is_array($payload) || empty($payload['ok'])) {
return Result::failure(
'invalid_response',
'The activation server returned an invalid response.'
);
}
$this->options->update($this->optionKey, [
'active' => true,
'key' => $key,
]);
return Result::success(['active' => true]);
}
}
use AdapterKit\Core\Testing\InMemoryOptionStorage;
$options = new InMemoryOptionStorage([
'myplugin_settings' => ['enabled' => true],
]);
$options->update('myplugin_settings', ['enabled' => false]);
$options->has('myplugin_settings'); // true
$options->all(); // complete in-memory store
$options->clear(); // removes every stored key
use AdapterKit\Core\Testing\InMemoryTransientStorage;
use AdapterKit\Core\Time\FrozenClock;
$clock = new FrozenClock(1700000000);
$transients = new InMemoryTransientStorage($clock);
$transients->set('token', 'abc123', 60);
$transients->get('token'); // 'abc123'
$clock->advance(60);
$transients->get('token'); // false because expiration is inclusive
use AdapterKit\Core\Contracts\OptionStorageInterface;
final class NetworkOptionStorage implements OptionStorageInterface
{
// Implement the contract with network option functions.
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.