Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active December 14, 2024 14:09
Show Gist options
  • Save Crocoblock/4170d042e451cb5f7550462bffa96261 to your computer and use it in GitHub Desktop.
Save Crocoblock/4170d042e451cb5f7550462bffa96261 to your computer and use it in GitHub Desktop.
JetEngine Data Store API

Get store items, add items, remove items

Check if data store module active

returns true/false; call not earlier than 'init' action with the priority of 1

$module_active = jet_engine()->modules->is_module_active( 'data-stores' );
Get store factory

returns instance of \Jet_Engine\Modules\Data_Stores\Stores\Factory
To be found here \jet-engine\includes\modules\data-stores\inc\stores\factory.php

$store_factory = \Jet_Engine\Modules\Data_Stores\Module::instance()->stores->get_store( $store_slug );
Get store type

returns instance of a class, which is an extension of \Jet_Engine\Modules\Data_Stores\Stores\Base_Store
Store types to be found here \jet-engine\includes\modules\data-stores\inc\stores

$store_type = $store_factory->get_type();
Check if item in store
$in_store = $store_factory->in_store( $item_id );
Get item count

for countable stores

$in_store = $store_factory->get_post_count( $item_id );
Get store items
$items = $store_type->get( $store_slug );
Add item to the store
$store_type->add_to_store( $store_slug, $item_id );
Remove item from the store
$store_type->remove( $store_slug, $item_id );

Hooks

On add / remove

Run on AJAX add to store / remove from store (when the user clicks Dynamic Link / Data Store Button)

Before add to store
add_action( 'jet-engine/data-stores/before-add-to-store', function( $item_id, $store_slug, $store_factory ) {
  //do something
}, 0, 3 );
After add to store
add_action( 'jet-engine/data-stores/after-add-to-store', function( $item_id, $store_slug, $store_factory ) {
  //do something
}, 0, 3 );
Before remove from store
add_action( 'jet-engine/data-stores/before-remove-from-store', function( $item_id, $store_slug, $store_factory ) {
  //do something
}, 0, 3 );
After remove from store
add_action( 'jet-engine/data-stores/after-remove-from-store', function( $item_id, $store_slug, $store_factory ) {
  //do something
}, 0, 3 );

On item count get / increase / decrease

Meant to be used for a custom countable store

Action on item count increase
add_action( 'jet-engine/data-stores/post-count-increased', function( $item_id, $count, $store_factory ) {
  //store new count increased by 1
} );
Action on item count decrease
add_action( 'jet-engine/data-stores/post-count-decreased', function( $item_id, $count, $store_factory ) {
  //store new count decreased by 1
} );
Filter item count
add_filter( 'jet-engine/data-stores/pre-get-post-count', function( $count, $item_id, $store_factory ) {
  //$count = false
  //get item count in a custom way and store it to $count
  return $count;
} );
@naimbhuiya
Copy link

Help full Docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment