hokoo / wpconnections
A library for many-to-many relationships in WordPress
Requires
- psr/log: >=1.1
- ramsey/collection: ^1.3
Requires (Dev)
- composer/installers: ^2.3
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- johnpbloch/wordpress: ^6.7
- johnpbloch/wordpress-core-installer: ^2.0
- phpcompatibility/phpcompatibility-wp: ^2.1
- phpunit/phpunit: 9.6.x-dev
- squizlabs/php_codesniffer: ^3.7
- symfony/var-dumper: 5.4.x-dev
- wp-coding-standards/wpcs: ^2.3
- yoast/phpunit-polyfills: ^3.0
- dev-master
- dev-issue-35-permisstions-policy
- dev-issue-32-fix-wp-units-github
- dev-issue-37-local-dev
- dev-issue-33-cardinality
- dev-duplicatable-put-to-first
- dev-connection-save
- dev-ga-refactored
- dev-local-wp-tests
- dev-exception-codes
- dev-readme
- dev-wp-unit-tests
- dev-github-autotests
- dev-fix-order
- dev-feature-add-wpcs
- dev-ConnectionTests
- dev-docs
- dev-dev
- dev-restapi
This package is auto-updated.
Last update: 2025-04-21 13:26:12 UTC
README
wpConnections allows to link posts in WordPress by graph-like connections. The library provides such connection properties as:
- direction (from, to)
- from post id
- to post id
- order
- meta data.
Connections belong to a Relation and never exist out. The relation has properties:
- cardinality (1-1, 1-m, m-1, m-m)
- from post type
- to post type
- direction type (from, to, both)
- duplicatable (whether may have same connections)
- closurable (whether may have the same post on from and to).
EXAMPLE. There are four CPT:
magazine
,issue
,article
andauthor
. Magazine posts may have connections with some Issues (one-to-many type) so that the Issues constitute the Magazine.
The Issues in turn have connections with Articles (one-to-many as well). But an Author might have been linked with many Articles, and an Article might have many connections with Authors (many-to-many).
Why wpConnection?
It can be used as multiple installed library being parts of different plugins in a WordPress installation. All you need is creating a client instance for your application. Every client has its own tables and REST API hooks, and does not influence to another clients.
Ok, what should I do to start using?
Full documentation is available on Wiki project pages.
Add the package
composer require hokoo/wpconnections
So, you have to create client instance...
use iTRON\wpConnections\Client; $wpc_client = new Client( 'my-app-wpc-client' );
...and relations for your connections.
use iTRON\wpConnections\Query; $qr = new Query\Relation(); $qr->set( 'name', 'post-to-page' ); $qr->set( 'from', 'post' ); $qr->set( 'to', 'page' ); $qr->set( 'cardinality', 'm-m' ); $wpc_client->registerRelation( $qr );
Ok, now you can create connections inside the relation.
$qc = new Query\Connection(); $qc->set( 'from', $post_id_from ); $qc->set( 'to', $post_id_to ); $wpc_client->getRelation( 'post-to-page' )->createConnection( $qc );
Since you have initialized new client, its REST API endpoints are available.
http://cf7tgdev.loc/wp-json/wp-connections/v1/client/my-app-wpc-client/
Local Development
Prerequisites
- Windows 10 or later (WSL2), or Linux, or MacOS
- Docker Desktop, Docker Compose
- Make
Installation
- Clone this repo to the Ubuntu disk space. Location path should look like
\\wsl$\Ubuntu-20.04\home\username\path\to\the\repo
. - Make sure you have
make
installed in your system. If not, runsudo apt install make
. - Make sure you have installed Docker Desktop with configured WSL2 support if you are using Windows.
- Add
127.0.0.1 wpconnections.local
to the hosts file (on the host machine). - Run folowing command in the root directory to install the project:
bash ./local-dev/init.sh && make tests.init && make docker.up && make dev.install