Last active
January 22, 2025 22:16
-
-
Save VictorPietro/37c3dfe472d23f5c4f5e13643c84b61a to your computer and use it in GitHub Desktop.
JetEngine Get current object property macro. All credits to Crocoblock. Just saving this for me
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 this to functions.php or using a code snippet plugin */ | |
add_action( 'jet-engine/register-macros', function(){ | |
/** | |
* Return current object property. | |
*/ | |
class Current_Object_Prop_Macro extends \Jet_Engine_Base_Macros { | |
/** | |
* @inheritDoc | |
*/ | |
public function macros_tag() { | |
return 'current_object_prop'; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function macros_name() { | |
return esc_html__( 'Current object property', 'jet-engine' ); | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function macros_args() { | |
return array( | |
'prop_key' => array( | |
'label' => __( 'Property', 'jet-engine' ), | |
'type' => 'text', | |
'default' => '', | |
), | |
'is_user_prop' => array( | |
'label' => 'Is user property', | |
'type' => 'select', | |
'options' => array( | |
'no' => 'No', | |
'yes' => 'Yes', | |
), | |
'default' => 'no', | |
), | |
); | |
} | |
public function prevent_user_prop( $props ) { | |
return array(); | |
} | |
public function macros_callback( $args = array() ) { | |
$prop_key = ! empty( $args['prop_key'] ) ? $args['prop_key'] : null; | |
$is_user_prop = ! empty( $args['is_user_prop'] ) ? $args['is_user_prop'] : 'no'; | |
$object = $this->get_macros_object(); | |
if ( ! $object || ! $prop_key ) { | |
return; | |
} | |
if ( 'no' === $is_user_prop ) { | |
add_filter( 'jet-engine/listing/data/user-fields', array( $this, 'prevent_user_prop' ), 999 ); | |
} | |
$result = jet_engine()->listings->data->get_prop( $prop_key, $object ); | |
remove_filter( 'jet-engine/listing/data/user-fields', array( $this, 'prevent_user_prop' ), 999 ); | |
$result = jet_engine_render_checkbox_values( $result ); | |
return $result; | |
} | |
} | |
new Current_Object_Prop_Macro(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment