Coder Social home page Coder Social logo

stellarwp / schema Goto Github PK

View Code? Open in Web Editor NEW
25.0 20.0 3.0 369 KB

A library for simplifying the creation, update, and field modification of custom tables within WordPress.

License: GNU General Public License v2.0

PHP 100.00%
database schema wordpress wordpress-library wordpress-php-library

schema's Introduction

Schema Builder

CI Static Analysis

A library for simplifying the creation, update, and field modification of custom tables within WordPress.

Installation

It's recommended that you install Schema as a project dependency via Composer:

composer require stellarwp/schema

We actually recommend that this library gets included in your project using Strauss.

Luckily, adding Strauss to your composer.json is only slightly more complicated than adding a typical dependency, so checkout our strauss docs.

Usage prerequisites

To actually use the schema library, you must have two additional libraries in your project:

  1. A Dependency Injection Container (DI Container) that is compatible with di52 (We recommend using di52.).
  2. The stellarwp/db library.

In order to keep this library as light as possible, those dependencies are not included in the library itself. To avoid version compatibility issues, those libraries are also not included as Composer dependencies. Instead, you must include them in your project. We recommend including them via composer using Strauss, just like you have done with this library.

Important note on ALL examples

All examples within the documentation for this project will be assuming that you are using Strauss to prefix the namespaces provided by this library.

The examples will be using:

  • Boom\Shakalaka\ as the namespace prefix, though will sometimes be referenced as PREFIX\ for the purpose of brevity in the docs.
  • BOOM_SHAKALAKA_ as the constant prefix.

Getting started

For a full understanding of what is available in this library and how to use it, definitely read through the full documentation. But for folks that want to get rolling with the basics quickly, try out the following.

Initializing the library

use Boom\Shakalaka\StellarWP\Schema\Config;

// You'll need a Dependency Injection Container that is compatible with https://github.com/lucatume/di52.
use Boom\Shakalaka\lucatume\DI52\Container;

// You'll need to use the StellarWP\DB library for database operations.
use Boom\Shakalaka\StellarWP\DB\DB;

$container = new Boom\Shakalaka\lucatume\DI52\Container();

Config::set_container( $container );
Config::set_db( DB::class );

Creating a table

Let's say you want a new custom table called sandwiches (with the default WP prefix, it'd be wp_sandwiches). You'll need a class file for the table. For the sake of this example, we'll be assuming this class is going into a Tables/ directory and is reachable via the Boom\Shakalaka\Tables namespace.

<?php
namespace Boom\Shakalaka\Tables;

use Boom\Shakalaka\StellarWP\Schema\Tables\Contracts\Table;

class Sandwiches extends Table {
	/**
	 * {@inheritdoc}
	 */
	const SCHEMA_VERSION = '1.0.0';

	/**
	 * {@inheritdoc}
	 */
	protected static $base_table_name = 'sandwiches';

	/**
	 * {@inheritdoc}
	 */
	protected static $group = 'boom';

	/**
	 * {@inheritdoc}
	 */
	protected static $schema_slug = 'boom-sandwiches';

	/**
	 * {@inheritdoc}
	 */
	protected static $uid_column = 'id';

	/**
	 * {@inheritdoc}
	 */
	protected function get_definition() {
		global $wpdb;
		$table_name = self::table_name( true );
		$charset_collate = $wpdb->get_charset_collate();

		return "
			CREATE TABLE `{$table_name}` (
				`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
				`name` varchar(50) NOT NULL,
				PRIMARY KEY (`id`)
			) {$charset_collate};
		";
	}
}

Here's what the properties and method mean:

  • $base_table_name: The name of the table without the prefix.
  • $group: The group that the table belongs to - this is for organizational purposes.
  • $schema_slug: An identifier for the table. This is used in storing your table's schema version in wp_options.
  • $uid_column: The name of the column that is used to uniquely identify each row.
  • get_definition(): This should return the base SQL definition used to create your sandwiches table. To get the full SQL (with any field schemas included), you can call get_sql()!

Registering the table

The Schema library gets initialized automatically when you register at table or a field schema. To register your table, simply use the handy Register::table() method within the plugins_loaded:

namespace Boom\Shakalaka;

use Boom\Shakalaka\StellarWP\Schema\Register;
use Boom\Shakalaka\Tables\Sandwiches;

add_action( 'plugins_loaded', static function() {
	Register::table( Sandwiches::class );
} );

That's it!

The table will be automatically registered, created, and updated during the plugins_loaded action at priority 1000! (that priority number is filterable via the stellarwp_schema_up_plugins_loaded_priority filter)

Documentation

Here's some more advanced documentation to get you rolling on using this library at a deeper level:

  1. Setting up Strauss
  2. Schema management
  3. Table schemas
    1. Versioning
    2. Registering tables
    3. Deregistering tables
    4. Table collection
    5. Publicly accessible methods
  4. Field schemas
    1. Versioning
    2. Registering fields
    3. Deregistering fields
    4. Field collection
    5. Publicly accessible methods
  5. Automated testing

Acknowledgements

Special props go to @lucatume and @stratease for their initial work on this structure before it was extracted into a standalone library.

schema's People

Contributors

borkweb avatar defunctl avatar lucatume avatar oakesjosh avatar szepeviktor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

schema's Issues

Add a migrations UI

GiveWP has a UI for migrations. Bringing some semblance of that code over such that it can be used and extended by projects.

Add Batch migration controller

Once 1.2.0 is out, we can start looking in to an approach to supporting batch-processing of migration data.

It'd be rad to use Action Scheduler under the hood.

Add namespaced WPCLI command for managing schemas

It'd be rad to have some sort of auto-namespaced WPCLI commands for schema management

Here are some braindump notes:

wp {namespace}:schema help (list the tables)
wp {namespace}:schema {table} {up|down|drop|version}

wp tec:schema tec_events up

// Maybe build the namespace from the base namespace of the plugin that is consuming it.
$prefix = str_replace( '\\', '-', __NAMESPACE__ );

apply_filters( 'stellarwp_schema_wpcli_namespace', $prefix, __NAMESPACE__ );

wp tribe-pue:schema

Reduce number of queries on every run to zero

stellarwp/schema executes few queries on ever run.

Would it be possible to run stellarwp/schema only on plugin activation/update?

(a production WordPress installation works from persistent object cache, with almost no SQL queries)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.