Skip to content

Instantly share code, notes, and snippets.

@ihslimn
Last active March 31, 2022 14:18
Show Gist options
  • Save ihslimn/39b975dccbedda81e5075dcab0f9eec3 to your computer and use it in GitHub Desktop.
Save ihslimn/39b975dccbedda81e5075dcab0f9eec3 to your computer and use it in GitHub Desktop.
Get related items by "Sibling" Relation
add_filter( 'jet-engine/listings/macros-list', 'register_get_related_items_by_sibling_relation_macro' );
function register_get_related_items_by_sibling_relation_macro( $macros_list ) {
$macros_list['rel_get_items_by_relation'] = array(
'label' => 'Related Items By Sibling Relation',
'cb' => '__get_related_items_by_sibling_relation',
'args' => array(
'_rel_id' => array(
'label' => __( 'From Relation', 'jet-engine' ),
'type' => 'select',
'options' => function() {
return jet_engine()->relations->get_relations_for_js( true, __( 'Select...', 'jet-engine' ) );
},
'default' => '',
),
'_rel_object' => array(
'label' => __( 'From Object', 'jet-engine' ),
'type' => 'select',
'options' => array(
'parent_object' => __( 'Parent Object', 'jet-engine' ),
'child_object' => __( 'Child Object', 'jet-engine' ),
),
'default' => 'child_object',
),
'_rel_id2' => array(
'label' => __( 'By Sibling Relation', 'jet-engine' ),
'type' => 'select',
'options' => function() {
return jet_engine()->relations->get_relations_for_js( true, __( 'Select...', 'jet-engine' ) );
},
'default' => '',
),
'_rel_object2' => array(
'label' => __( 'From Sibling Object', 'jet-engine' ),
'type' => 'select',
'options' => array(
'parent_object' => __( 'Parent Object', 'jet-engine' ),
'child_object' => __( 'Child Object', 'jet-engine' ),
),
'default' => 'child_object',
),
'_rel_object_from' => array(
'label' => __( 'Initial Object ID From', 'jet-engine' ),
'type' => 'select',
'options' => array( jet_engine()->relations->sources, 'get_sources' ),
'default' => 'current_object',
),
'_rel_object_var' => array(
'label' => __( 'Variable Name', 'jet-engine' ),
'type' => 'text',
'default' => '',
'condition' => array( 'rel_object_from' => array( 'query_var', 'object_var' ) ),
),
),
);
return $macros_list;
}
function __get_related_items_by_sibling_relation( $field_value = 0, $args ) {
$args = explode( '|', $args );
$rel_id = isset( $args[0] ) ? $args[0] : false;
$rel_object = isset( $args[1] ) ? $args[1] : 'child_object';
$rel_id2 = isset( $args[2] ) ? $args[2] : false;
$rel_object2 = isset( $args[3] ) ? $args[3] : 'child_object';
$rel_object_from = isset( $args[4] ) ? $args[4] : 'current_object';
$rel_object_var = isset( $args[5] ) ? $args[5] : '';
if ( ! (int) $rel_id || ! (int) $rel_id2 ) {
return;
}
$relation = jet_engine()->relations->get_active_relations( $rel_id );
if ( ! $relation ) {
return;
}
$object_id = false;
if ( $rel_object_from ) {
$object_id = jet_engine()->relations->sources->get_id_by_source( $rel_object_from, $rel_object_var );
if ( ! $object_id ) {
return false;
}
}
$related_ids = array();
switch ( $rel_object ) {
case 'parent_object':
$related_ids = $relation->get_parents( $object_id, 'ids' );
break;
default:
$related_ids = $relation->get_children( $object_id, 'ids' );
break;
}
$related_ids = ! empty( $related_ids ) ? $related_ids : array( PHP_INT_MAX );
$related = array();
$relation = jet_engine()->relations->get_active_relations( $rel_id2 );
switch ( $rel_object2 ) {
case 'parent_object':
foreach ( $related_ids as $object_id ) {
$related = array_merge( $relation->get_parents( $object_id, 'ids' ), $related );
}
break;
default:
foreach ($related_ids as $object_id) {
$related = array_merge( $relation->get_children( $object_id, 'ids' ), $related );
}
break;
}
$related = array_unique( $related );
return implode( ',', $related );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment