Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jechazelle/c100e60f15c2ac46bf22742e6a78c950 to your computer and use it in GitHub Desktop.
Save jechazelle/c100e60f15c2ac46bf22742e6a78c950 to your computer and use it in GitHub Desktop.
JetEngine Get array keys macro
<?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