| 1 |
# WP ORM for WordPress Developers |
| 2 |
|
| 3 |
## Description |
| 4 |
|
| 5 |
WP ORM is a specialized Object Relational Mapper designed for WordPress, simplifying the management of entities using the `wp_options` table. It provides an efficient way to handle single and collection entities, ideal for plugin settings and simple plugin entities, without the need for custom databases. |
| 6 |
|
| 7 |
The primary goal of WP ORM is to obviate the need for creating custom database or multiple wp_options entries reducing the complexity associated with managing such data in WordPress. |
| 8 |
|
| 9 |
This makes it an ideal solution for WordPress developers looking for a simplified and efficient way to handle data within plugins. |
| 10 |
|
| 11 |
WP ORM excels in scenarios that require simple data management. It is particularly adept at managing settings and other basic entities within WordPress plugins. |
| 12 |
|
| 13 |
For applications that require more complex data structures or advanced features, we recommend using Symlink ORM, which is better suited for handling intricate data requirements. |
| 14 |
|
| 15 |
In summary, WP ORM offers a developer-friendly, efficient, and streamlined approach to managing WordPress data, making it an excellent choice for developers looking to enhance their plugin development process without the added complexity of custom database management. |
| 16 |
|
| 17 |
## Key Features |
| 18 |
|
| 19 |
- **Simple Entity Management**: Easily create and manage single entities. |
| 20 |
- **Collection Entity Handling**: Handle collection instances with ease. |
| 21 |
|
| 22 |
## Installation |
| 23 |
|
| 24 |
``` |
| 25 |
composer require franmastromarino/wp-options-orm |
| 26 |
``` |
| 27 |
|
| 28 |
## Usage |
| 29 |
|
| 30 |
WP ORM can be utilized to create and manage entities within WordPress. Below are examples showing its practical implementation: |
| 31 |
|
| 32 |
### Single Entity |
| 33 |
|
| 34 |
In this example we will share a plugin settings implementation. |
| 35 |
|
| 36 |
```php |
| 37 |
// Define the PluginSettings entity |
| 38 |
namespace YourNamespace\Entities; |
| 39 |
|
| 40 |
use QuadLayers\WP_Orm\Entity\SingleEntity; |
| 41 |
|
| 42 |
class PluginSettings extends SingleEntity |
| 43 |
{ |
| 44 |
public $feature_enabled = 1; |
| 45 |
public $layout_type = 'grid'; |
| 46 |
public $max_items = 10; |
| 47 |
public $post_type = array( 'post', 'page' ); |
| 48 |
public $more = array( |
| 49 |
'test1' => true, |
| 50 |
'test2' => true, |
| 51 |
'test3' => false, |
| 52 |
); |
| 53 |
// Additional settings properties... |
| 54 |
} |
| 55 |
``` |
| 56 |
|
| 57 |
```php |
| 58 |
// Model for PluginSettings |
| 59 |
namespace YourNamespace\Models; |
| 60 |
|
| 61 |
use YourNamespace\Entities\PluginSettings; |
| 62 |
use QuadLayers\WP_Orm\Builder\SingleRepositoryBuilder; |
| 63 |
|
| 64 |
class PluginSettingsModel |
| 65 |
{ |
| 66 |
protected $repository; |
| 67 |
|
| 68 |
public function __construct() |
| 69 |
{ |
| 70 |
$builder = new SingleRepositoryBuilder(); |
| 71 |
$builder->setTable('your_plugin_settings') |
| 72 |
->setEntity(PluginSettings::class); |
| 73 |
|
| 74 |
$this->repository = $builder->getRepository(); |
| 75 |
} |
| 76 |
|
| 77 |
public function getSettingsTable() |
| 78 |
{ |
| 79 |
return $this->repository->getTable(); |
| 80 |
} |
| 81 |
|
| 82 |
public function getSettings() |
| 83 |
{ |
| 84 |
$entity = $this->repository->find(); |
| 85 |
|
| 86 |
if ($entity) { |
| 87 |
return $entity->getProperties(); |
| 88 |
} else { |
| 89 |
$admin = new PluginSettings(); |
| 90 |
return $admin->getProperties(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public function deleteSettings() |
| 95 |
{ |
| 96 |
return $this->repository->delete(); |
| 97 |
} |
| 98 |
|
| 99 |
public function saveSettings($data) |
| 100 |
{ |
| 101 |
$entity = $this->repository->create($data); |
| 102 |
|
| 103 |
if ($entity) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
} |
| 107 |
// Additional model methods... |
| 108 |
} |
| 109 |
``` |
| 110 |
|
| 111 |
### Collection Entity |
| 112 |
|
| 113 |
In this example we will share a plugin items collection implementation. |
| 114 |
|
| 115 |
```php |
| 116 |
namespace YourNamespace\Entities; |
| 117 |
|
| 118 |
use QuadLayers\WP_Orm\Entity\CollectionEntity; |
| 119 |
|
| 120 |
class PluginItems extends CollectionEntity |
| 121 |
{ |
| 122 |
public static $primaryKey = 'item_id'; |
| 123 |
public $item_id = 0; |
| 124 |
public $feature_enabled = 1; |
| 125 |
public $layout_type = 'grid'; |
| 126 |
public $max_items = 10; |
| 127 |
public $post_type = array( 'post', 'page' ); |
| 128 |
public $more = array( |
| 129 |
'test1' => true, |
| 130 |
'test2' => true, |
| 131 |
'test3' => false, |
| 132 |
); |
| 133 |
// Additional settings properties... |
| 134 |
} |
| 135 |
``` |
| 136 |
|
| 137 |
```php |
| 138 |
namespace YourNamespace\Models; |
| 139 |
|
| 140 |
use YourNamespace\Entities\PluginItems; |
| 141 |
use QuadLayers\WP_Orm\Builder\CollectionRepositoryBuilder; |
| 142 |
|
| 143 |
class PluginItemsModel |
| 144 |
{ |
| 145 |
protected static $instance; |
| 146 |
protected $repository; |
| 147 |
|
| 148 |
private function __construct() |
| 149 |
{ |
| 150 |
|
| 151 |
$builder = ( new CollectionRepositoryBuilder() ) |
| 152 |
->setTable('your_plugin_items') |
| 153 |
->setEntity(PluginItems::class) |
| 154 |
->setAutoIncrement(true); |
| 155 |
|
| 156 |
$this->repository = $builder->getRepository(); |
| 157 |
} |
| 158 |
|
| 159 |
public function getItemsTable() |
| 160 |
{ |
| 161 |
return $this->repository->getTable(); |
| 162 |
} |
| 163 |
|
| 164 |
public function getItemDefaults() |
| 165 |
{ |
| 166 |
$entity = new PluginItems(); |
| 167 |
$defaults = $entity->getDefaults(); |
| 168 |
return $defaults; |
| 169 |
} |
| 170 |
|
| 171 |
public function getItem(int $item_id) |
| 172 |
{ |
| 173 |
$entity = $this->repository->find($item_id); |
| 174 |
if ($entity) { |
| 175 |
return $entity->getProperties(); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
public function deleteItem(int $item_id) |
| 180 |
{ |
| 181 |
return $this->repository->delete($item_id); |
| 182 |
} |
| 183 |
|
| 184 |
public function updateItem(int $item_id, array $item) |
| 185 |
{ |
| 186 |
$entity = $this->repository->update($item_id, $item); |
| 187 |
if ($entity) { |
| 188 |
return $entity->getProperties(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
public function createItem(array $item) |
| 193 |
{ |
| 194 |
if (isset($item['item_id'])) { |
| 195 |
unset($item['item_id']); |
| 196 |
} |
| 197 |
|
| 198 |
$entity = $this->repository->create($item); |
| 199 |
|
| 200 |
if ($entity) { |
| 201 |
return $entity->getProperties(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
public function getAllItems() |
| 206 |
{ |
| 207 |
$entities = $this->repository->findAll(); |
| 208 |
if (! $entities) { |
| 209 |
return; |
| 210 |
} |
| 211 |
$actions = array(); |
| 212 |
foreach ($entities as $entity) { |
| 213 |
$actions[] = $entity->getProperties(); |
| 214 |
} |
| 215 |
return $actions; |
| 216 |
} |
| 217 |
|
| 218 |
public function deleteAllItems() |
| 219 |
{ |
| 220 |
return $this->repository->deleteAll(); |
| 221 |
} |
| 222 |
|
| 223 |
public static function instance() |
| 224 |
{ |
| 225 |
if (! isset(self::$instance)) { |
| 226 |
self::$instance = new self(); |
| 227 |
} |
| 228 |
return self::$instance; |
| 229 |
} |
| 230 |
} |
| 231 |
``` |
| 232 |
|