Forked from Crocoblock/jet-engine-array-keys-macro.php
Created
June 29, 2023 15:34
-
-
Save jechazelle/c100e60f15c2ac46bf22742e6a78c950 to your computer and use it in GitHub Desktop.
JetEngine Get array keys 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 Current_Meta_Array_Keys extends \Jet_Engine_Base_Macros { | |
| public function macros_tag() { | |
| return 'get_array_keys'; | |
| } | |
| public function macros_name() { | |
| return 'Array keys'; | |
| } | |
| public function macros_args() { | |
| return array( | |
| 'meta_key' => array( | |
| 'label' => 'Meta key', | |
| 'type' => 'text', | |
| 'default' => '', | |
| ), | |
| 'to_regexp' => array( | |
| 'label' => 'To REGEXP', | |
| 'type' => 'select', | |
| 'options' => array( | |
| 'no' => 'No', | |
| 'yes' => 'Yes', | |
| ), | |
| 'default' => '', | |
| ), | |
| ); | |
| } | |
| public function macros_callback( $args = array() ) { | |
| $object = $this->get_macros_object(); | |
| $meta_key = ! empty( $args['meta_key'] ) ? $args['meta_key'] : null; | |
| $to_regexp = ! empty( $args['to_regexp'] ) ? $args['to_regexp'] : 'no'; | |
| if ( ! $meta_key ) { | |
| return; | |
| } | |
| $meta = jet_engine()->listings->macros->get_current_meta( $field_value, $meta_key ); | |
| if ( ! is_array( $meta ) ) { | |
| return; | |
| } | |
| $result = array_keys( $meta ); | |
| $delimiter = ','; | |
| if ( $to_regexp === 'yes' ) { | |
| $result = array_map( function( $item ) { | |
| return sprintf( '"%s"', $item ); | |
| }, $result ); | |
| $delimiter = '|'; | |
| } | |
| return implode( $delimiter, $result ); | |
| } | |
| } | |
| new Current_Meta_Array_Keys(); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment