| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Post_Author_Meta { |
| 7 |
public function __construct() { |
| 8 |
add_action( 'init', array( $this, 'register' ) ); |
| 9 |
} |
| 10 |
public function get_attributes() { |
| 11 |
|
| 12 |
return array( |
| 13 |
'blockId' => '', |
| 14 |
|
| 15 |
/* |
| 16 |
============================ |
| 17 |
Post Author Meta Settings |
| 18 |
============================*/ |
| 19 |
'authMetAvatar' => true, |
| 20 |
'authMetaIconShow' => false, |
| 21 |
|
| 22 |
/* |
| 23 |
============================ |
| 24 |
Post Author Avatar Style |
| 25 |
============================*/ |
| 26 |
'authMetaLabel' => true, |
| 27 |
|
| 28 |
/* |
| 29 |
============================ |
| 30 |
Post Author Icon Style |
| 31 |
============================*/ |
| 32 |
'authMetaIconStyle' => 'author1', |
| 33 |
|
| 34 |
/* |
| 35 |
============================ |
| 36 |
Post Author Label Style |
| 37 |
============================*/ |
| 38 |
'authMetaLabelText' => 'By', |
| 39 |
|
| 40 |
/* |
| 41 |
============================ |
| 42 |
Advance Settings |
| 43 |
============================*/ |
| 44 |
'advanceId' => '', |
| 45 |
'advanceZindex' => '', |
| 46 |
'hideExtraLarge' => false, |
| 47 |
'hideDesktop' => false, |
| 48 |
'hideTablet' => false, |
| 49 |
'hideMobile' => false, |
| 50 |
'advanceCss' => '', |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
public function register() { |
| 55 |
register_block_type( |
| 56 |
'ultimate-post/post-author-meta', |
| 57 |
array( |
| 58 |
'editor_script' => 'ultp-blocks-editor-script', |
| 59 |
'editor_style' => 'ultp-blocks-editor-css', |
| 60 |
'render_callback' => array( $this, 'content' ), |
| 61 |
) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
public function content( $attr, $noAjax ) { |
| 66 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 67 |
$block_name = 'post-author-meta'; |
| 68 |
$wrapper_before = $wrapper_after = $content = ''; |
| 69 |
$author_id = get_post_field( 'post_author', get_the_ID() ); |
| 70 |
|
| 71 |
$attr['className'] = isset( $attr['className'] ) && $attr['className'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['className'] ) : ''; |
| 72 |
$attr['align'] = isset( $attr['align'] ) && $attr['align'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['align'] ) : ''; |
| 73 |
$attr['advanceId'] = isset( $attr['advanceId'] ) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 74 |
$attr['blockId'] = isset( $attr['blockId'] ) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 75 |
$attr['authMetaLabelText'] = wp_kses( $attr['authMetaLabelText'], ultimate_post()->ultp_allowed_html_tags() ); |
| 76 |
|
| 77 |
if ( $author_id ) { |
| 78 |
$wrapper_before .= '<div ' . ( $attr['advanceId'] ? 'id="' . $attr['advanceId'] . '" ' : '' ) . ' class="wp-block-ultimate-post-' . $block_name . ' ultp-block-' . $attr['blockId'] . ( $attr['className'] ? ' ' . $attr['className'] : '' ) . '' . ( $attr['align'] ? ' align' . $attr['align'] : '' ) . '">'; |
| 79 |
$wrapper_before .= '<div class="ultp-block-wrapper">'; |
| 80 |
$content .= '<span class="ultp-authMeta-count">'; |
| 81 |
if ( $attr['authMetaIconShow'] && ( $attr['authMetaIconStyle'] != '' ) ) { |
| 82 |
$content .= ultimate_post()->get_svg_icon( $attr['authMetaIconStyle'] ); |
| 83 |
} |
| 84 |
if ( $attr['authMetAvatar'] ) { |
| 85 |
$content .= '<div class="ultp-authMeta-avatar">'; |
| 86 |
$content .= get_avatar( $author_id, 32 ); |
| 87 |
$content .= '</div>'; |
| 88 |
} |
| 89 |
if ( $attr['authMetaLabel'] ) { |
| 90 |
$content .= '<span class="ultp-authMeta-label">' . $attr['authMetaLabelText'] . '</span>'; |
| 91 |
} |
| 92 |
$content .= '<a class="ultp-authMeta-name" href="' . get_author_posts_url( $author_id ) . '">'; |
| 93 |
$content .= get_the_author_meta( 'display_name', $author_id ); |
| 94 |
$content .= '</a>'; |
| 95 |
$content .= '</span>'; |
| 96 |
$wrapper_after .= '</div>'; |
| 97 |
$wrapper_after .= '</div>'; |
| 98 |
} |
| 99 |
|
| 100 |
return $wrapper_before . $content . $wrapper_after; |
| 101 |
} |
| 102 |
} |
| 103 |
|