Forked from Crocoblock/current-object-property-macro.php
Created
April 10, 2025 04:42
-
-
Save franitpro/ce2c1c6a676b94f40f0238677a375e21 to your computer and use it in GitHub Desktop.
JetEngine Get current object propery 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(){ | |
| /** | |
| * 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