Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active February 20, 2025 08:28
Show Gist options
  • Save Crocoblock/a9be7dbb1cb05aa2741aec97757c7f72 to your computer and use it in GitHub Desktop.
Save Crocoblock/a9be7dbb1cb05aa2741aec97757c7f72 to your computer and use it in GitHub Desktop.
JetEngine CCT CRUD

Get/Create/Update/Delete CCT item

Check if CCT module active

$module_active = jet_engine()->modules->is_module_active( 'custom-content-types' );

Get type object and handler

$type_object is an instance of \jet-engine\includes\modules\custom-content-types\inc\factory.php
get_content_types() should be called on init with priority 12 or higher,
or at any action later than init (there are no registered CCTs before that action)

$handler is an instance of \jet-engine\includes\modules\custom-content-types\inc\item-handler.php

$cct_slug    = 'test_cct';
$type_object = \Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types( $cct_slug );
//or: 
//$type_object = jet_engine()->modules->get_module( 'custom-content-types' )->instance->manager->get_content_types( $cct_slug );
$handler     = $type_object->get_item_handler();

Set DB flag to ensure $item will be an associative array

$flag = \ARRAY_A;
$type_object->db->set_format_flag( $flag );

Get item

$item_id = 4;
$item    = $type_object->db->get_item( $item_id );

$item is an associative array of CCT item fields

Query for items

Get 3 items where f1 is equal to 1 and f2 LIKE 2, order by f2 DESC

$args = $type_object->prepare_query_args( array(
  array(
    'field'    => 'f1',
    'operator' => '=',
    'value'    => 1,
  ),
  array(
    'field'    => 'f2',
    'operator' => 'LIKE',
    'value'    => 2,
    'type'     => 'CHAR',
  ),
) );

$limit = 3;

$offset = 0;

$order = array(
  array(
    'orderby' => 'f2',
    'order'   => 'DESC',
    'type'    => 'CHAR',
  ),
);

$rel = 'AND';

$items = $type_object->db->query( $args, $limit, $offset, $order, $rel );

Returns an array of items in a format (ARRAY_A | ARRAY_N | OBJECT | OBJECT_K) set by $type_object->db->set_format_flag()

Create/update/delete item

Create item

$item_id = $handler->update_item( array( 'f1' => 'test' ) );

Update item with '_ID' of 1

$item_id = $handler->update_item( array( '_ID' => 1, 'f1' => 'test' ) );

update_item() method expects an associative array of CCT fields that need to be updated
If '_ID' in array is a CCT item ID, the item with that ID will be updated, if there is no ID passed - new item gets created

Delete item

$cct_item_id - ID of CCT item, $redirect - whether to redirect to CCT admin page, default true

$cct_item_id = 1;
$redirect    = false;
$item_id = $handler->delete_item( $cct_item_id, $redirect );

Whether the user can delete CCT item is determined by user_has_access() method
So, if you do not want to duplicate delete_item() functionality -
use 'jet-engine/custom-content-types/user-has-access' filter from \jet-engine\includes\modules\custom-content-types\inc\factory.php
to override user permissions (be sure to do this in a secure way) or use $handler->raw_delete_item( $item_id ); to avoid user check

Hooks

See \jet-engine\includes\modules\custom-content-types\inc\item-handler.php

Filter item data before update

add_filter( 
  'jet-engine/custom-content-types/item-to-update', 
  function( $item, $fields, $item_handler ) {
    //do something with $item
    return $item;
  }, 
  0, 
  3 
);

Do something before item update

add_action( 
  'jet-engine/custom-content-types/update-item/' . $cct_slug, 
  function( $item, $prev_item, $item_handler ) {
    //do something
  }, 
  0, 
  3 
);

Do something after item updated

add_action( 
  'jet-engine/custom-content-types/updated-item/' . $cct_slug, 
  function( $item, $prev_item, $item_handler ) {
    //do something
  }, 
  0, 
  3 
);

Do something before item create

add_action( 
  'jet-engine/custom-content-types/create-item/' . $cct_slug, 
  function( $item, $item_handler ) {
    //do something
  }, 
  0, 
  2 
);

Do something after item created

add_action( 
  'jet-engine/custom-content-types/created-item/' . $cct_slug, 
  function( $item, $item_id, $item_handler ) {
    //do something
  }, 
  0, 
  3 
);

Do something after item deleted

add_action( 
  'jet-engine/custom-content-types/delete-item/' . $cct_slug, 
  function( $item_id, $item, $item_handler ) {
    //do something
  }, 
  0, 
  3 
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment