| 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( 'MywpShortcodeModulePost' ) ) : |
| 12 |
|
| 13 |
final class MywpShortcodeModulePost extends MywpShortcodeAbstractModule { |
| 14 |
|
| 15 |
protected static $id = 'mywp_post'; |
| 16 |
|
| 17 |
public static function do_shortcode( $atts , $content = false , $tag ) { |
| 18 |
|
| 19 |
global $post; |
| 20 |
|
| 21 |
if( empty( $atts['field'] ) ) { |
| 22 |
|
| 23 |
MywpHelper::error_not_found_message( '$atts["field"]' , sprintf( '[%s] shortcode' , self::$id ) ); |
| 24 |
|
| 25 |
return $content; |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
$field = strip_tags( $atts['field'] ); |
| 30 |
|
| 31 |
$post_id = false; |
| 32 |
|
| 33 |
if( ! empty( $atts['current'] ) ) { |
| 34 |
|
| 35 |
if( ! empty( $post->ID ) ) { |
| 36 |
|
| 37 |
$post_id = intval( $post->ID ); |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
} else { |
| 42 |
|
| 43 |
if( ! empty( $atts['post_id'] ) ) { |
| 44 |
|
| 45 |
$post_id = intval( $atts['post_id'] ); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
if( empty( $post_id ) ) { |
| 52 |
|
| 53 |
return $content; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
$get_post = get_post( $post_id ); |
| 58 |
|
| 59 |
if( empty( $get_post ) ) { |
| 60 |
|
| 61 |
return $content; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
if( $field === 'id' or $field === 'ID' ) { |
| 66 |
|
| 67 |
$content = $post_id; |
| 68 |
|
| 69 |
} elseif( $field === 'title' ) { |
| 70 |
|
| 71 |
$content = get_the_title( $post_id ); |
| 72 |
|
| 73 |
} elseif( $field === 'post_type' ) { |
| 74 |
|
| 75 |
$content = $get_post->post_type; |
| 76 |
|
| 77 |
} elseif( $field === 'post_status' ) { |
| 78 |
|
| 79 |
$content = $get_post->post_status; |
| 80 |
|
| 81 |
} elseif( $field === 'edit_link' ) { |
| 82 |
|
| 83 |
$content = get_edit_post_link( $post_id ); |
| 84 |
|
| 85 |
} else { |
| 86 |
|
| 87 |
MywpHelper::error_not_found_message( '$atts["field"]' , sprintf( '[%s] shortcode' , self::$id ) ); |
| 88 |
|
| 89 |
return $content; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
$content = apply_filters( 'mywp_shortcode_post' , $content , $atts ); |
| 94 |
|
| 95 |
return $content; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
MywpShortcodeModulePost::init(); |
| 102 |
|
| 103 |
endif; |
| 104 |
|