DWS WP Framework
  • Welcome
  • Primary goals
    • Modular design
    • No 3rd-party dependencies
  • Key concepts and dev tools
    • PHP and WP requirements
    • Object-Oriented Programming
    • Semantic Versioning
    • Version Control (git / GitHub)
    • Dependency Management (Composer)
    • Automated Testing (Codeception + Github Actions)
    • Dependency Injection (PHP-DI)
    • Coding Standards (PHPCS and PHPMD)
    • Dependencies Scoping (PHP-Scoper)
    • TypeScript and Sass
    • Task Runners (Grunt)
  • Setting up your dev environment
    • Windows
  • Your first plugin
    • Multiple plugins using the framework on the same site
  • Frequently Asked Questions
  • Bootstrapper Module
    • Motivation
    • How it works
    • How to use
    • White Labeling
  • Helpers Module
    • Motivation
    • How to use
  • Foundations Module
    • Motivation and How to use
    • Actions
      • Local action traits
      • Extension action traits
      • Integration action traits
    • States
    • Utilities
      • Stores
      • Handlers and Services
        • Logging Service
  • Plugin
    • Main Plugin Instance
    • Plugin Components
  • Hierarchies
  • Helpers
  • Utilities Module
    • Motivation and How to use
    • Hooks Service
      • Scoped Handler
    • Shortcodes Service
    • Templating Service
    • Assets Service
      • Scripts Handler
      • Styles Handler
    • CRON Events Service
      • Action Scheduler Handler
    • Admin Notices Service
    • Dependencies Service
    • Validation Service
  • Core Module
    • Motivation and How to use
    • Plugin Tree
      • Plugin Root
      • Plugin Functionality
    • Plugin Components
      • Internationalization
      • Installation / Upgrade / Uninstallation
  • Settings Module
    • Motivation and How to use
    • Settings Service
      • WordPress Handler
      • MetaBox Handler
      • ACF Handler
    • Validated Settings
  • WooCommerce Module
    • Motivation and How to use
    • Extended WC Logger
    • WC Settings Handler
Powered by GitBook
On this page

Was this helpful?

  1. Foundations Module
  2. Utilities

Stores

PreviousUtilitiesNextHandlers and Services

Last updated 4 years ago

Was this helpful?

Stores are objects that implement READ/UPDATE/DELETE operations against a storage medium for objects that implement the interface. In a nutshell, an object is storable if it has an ID (see the and for a simple implementation). All classes belonging to the Storage namespace can be found .

There are 3 stores that the Foundations Module comes with:

  • for storing objects in-memory during the current request.

  • for persistent database storage across requests in the WP options table.

  • for persistent database storage across request in the WP user meta table.

Stores are storable as well. It is possible to create a store that stores other stores! Check the and for examples.

Here is a simple example of a dummy storable object stored in a memory store:

<?php

namespace DeepWebSolutions\Plugins\MyTestPlugin\Storage;

use DeepWebSolutions\Framework\Foundations\Utilities\Storage\AbstractStorable;
use DeepWebSolutions\Framework\Foundations\Utilities\Storage\Stores\MemoryStore;

class MyStorable extends AbstractStorable {
    public string $storable_property;
}

$my_storable1 = new MyStorable( 'unique-id-1' );
$my_storable1->storable_property = 'test value 1';

$my_storable2 = new MyStorable( 'unique-id-2' );
$my_storable1->storable_property = 'test value 2';

$memory_store = new MemoryStore( 'my-memory-store' );
$memory_store->add( $my_storable1 );
$memory_store->add( $my_storable2 );

echo $memory_store->count(); // echoes 2
echo $memory_store->get( 'unique-id-1' )->storable_property; // echoes 'test value 1'

$my_storable3 = new MyStorable( 'unique-id-1' ); // same as $my_storable1
$my_storable3->storable_property = 'test value 3';
$memory_store->update( $my_storable3 );

echo $memory_store->get( 'unique-id-1' )->storable_property; // echoes 'test value 3'

There is no guarantee that your storable object will be stored properly when using the options or user meta stores. The two stores rely on WordPress' own and functions respectively and those both rely on PHP's function. If you have trouble storing your objects, implement the magic methods __serialize and __unserialize for better handling.

StorableInterface
StorableTrait
AbstractStorable
here
A memory store
An options table store
A user meta table store
MultiStoreAwareInterface
MultiStoreAwareTrait
update_option
update_user_meta
serialize