| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if( ! class_exists( 'MywpShortcodeAbstractModule' ) ) { |
| 8 |
return false; |
| 9 |
} |
| 10 |
|
| 11 |
if ( ! class_exists( 'MywpShortcodeModuleTerm' ) ) : |
| 12 |
|
| 13 |
final class MywpShortcodeModuleTerm extends MywpShortcodeAbstractModule { |
| 14 |
|
| 15 |
protected static $id = 'mywp_term'; |
| 16 |
|
| 17 |
public static function do_shortcode( $atts = false , $content = false , $tag = false ) { |
| 18 |
|
| 19 |
if( empty( $atts['field'] ) ) { |
| 20 |
|
| 21 |
MywpHelper::error_not_found_message( '$atts["field"]' , sprintf( '[%s] shortcode' , self::$id ) ); |
| 22 |
|
| 23 |
return $content; |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
$field = strip_tags( $atts['field'] ); |
| 28 |
|
| 29 |
$term_id = false; |
| 30 |
|
| 31 |
if( ! empty( $atts['term_id'] ) ) { |
| 32 |
|
| 33 |
$term_id = (int) $atts['term_id']; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
if( empty( $term_id ) ) { |
| 38 |
|
| 39 |
return $content; |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
$get_term = get_term( $term_id ); |
| 44 |
|
| 45 |
if( empty( $get_term ) or is_wp_error( $get_term ) ) { |
| 46 |
|
| 47 |
return $content; |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
if( $field === 'id' or $field === 'ID' or $field === 'term_id' ) { |
| 52 |
|
| 53 |
$content = $term_id; |
| 54 |
|
| 55 |
} elseif( $field === 'name' ) { |
| 56 |
|
| 57 |
$content = $get_term->name; |
| 58 |
|
| 59 |
} elseif( $field === 'slug' ) { |
| 60 |
|
| 61 |
$content = $get_term->slug; |
| 62 |
|
| 63 |
} elseif( $field === 'link' ) { |
| 64 |
|
| 65 |
$content = get_term_link( $get_term ); |
| 66 |
|
| 67 |
} else { |
| 68 |
|
| 69 |
MywpHelper::error_not_found_message( '$atts["field"]' , sprintf( '[%s] shortcode' , self::$id ) ); |
| 70 |
|
| 71 |
return $content; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
$content = apply_filters( 'mywp_shortcode_term' , $content , $atts ); |
| 76 |
|
| 77 |
return $content; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
MywpShortcodeModuleTerm::init(); |
| 84 |
|
| 85 |
endif; |
| 86 |
|