Last active
April 7, 2023 07:19
-
-
Save LaxusCroco/c9e015239ee266c00bce314886fdee75 to your computer and use it in GitHub Desktop.
Items with Relation macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'jet-engine/register-macros', function(){ | |
class Items_With_Relation extends \Jet_Engine_Base_Macros { | |
public function macros_tag() { | |
return 'items_with_relation'; | |
} | |
public function macros_name() { | |
return esc_html__( 'Items with Relation', 'jet-engine' ); | |
} | |
public function macros_args() { | |
return 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 (what to show)', 'jet-engine' ), | |
'type' => 'select', | |
'options' => array( | |
'parent_object_id' => 'Parents with children', | |
'child_object_id' => 'Children with parents', | |
), | |
'default' => 'parent_object_id', | |
), | |
); | |
} | |
public function macros_callback( $args = array() ) { | |
$relation_id = $args['rel_id']; | |
if ( ! $relation_id ) { | |
return ''; | |
} | |
$relation_object = jet_engine()->relations->get_active_relations( $relation_id ); | |
if ( ! $relation_object ) { | |
return ''; | |
} | |
$db_table = $relation_object->get_args( 'db_table' ); | |
$object_type = $args['rel_object']; | |
global $wpdb; | |
$prefix = $wpdb->prefix; | |
if ( ! $db_table ) { | |
$results = $wpdb->get_results( "SELECT * FROM {$prefix}jet_rel_default AS jet_rel_default WHERE rel_id = '$relation_id' GROUP BY $object_type;", ARRAY_A ); | |
} else { | |
$results = $wpdb->get_results( "SELECT * FROM {$prefix}jet_rel_$relation_id WHERE rel_id = '$relation_id' GROUP BY $object_type;", ARRAY_A ); | |
} | |
$ids = array_column( $results, $object_type ); | |
return implode( ',', $ids ); | |
} | |
} | |
new Items_With_Relation(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment