| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Author_Box { |
| 7 |
public function __construct() { |
| 8 |
add_action( 'init', array( $this, 'register' ) ); |
| 9 |
} |
| 10 |
public function register() { |
| 11 |
register_block_type( |
| 12 |
'ultimate-post/author-box', |
| 13 |
array( |
| 14 |
'editor_script' => 'ultp-blocks-editor-script', |
| 15 |
'editor_style' => 'ultp-blocks-editor-css', |
| 16 |
'render_callback' => array( $this, 'content' ), |
| 17 |
) |
| 18 |
); |
| 19 |
} |
| 20 |
public function get_attributes() { |
| 21 |
return array( |
| 22 |
'blockId' => '', |
| 23 |
'layout' => 'layout1', |
| 24 |
|
| 25 |
/* |
| 26 |
============================ |
| 27 |
Author Box Settings |
| 28 |
============================*/ |
| 29 |
'imgShow' => true, |
| 30 |
'writtenByShow' => true, |
| 31 |
'authorBioShow' => true, |
| 32 |
'metaShow' => true, |
| 33 |
'allPostLinkShow' => true, |
| 34 |
'authorBoxAlign' => 'center', |
| 35 |
|
| 36 |
/* |
| 37 |
============================ |
| 38 |
Author Image Settings |
| 39 |
============================*/ |
| 40 |
'imgSize' => (object) array( 'lg' => '100' ), |
| 41 |
'imgRatio' => '100', |
| 42 |
|
| 43 |
/* |
| 44 |
============================ |
| 45 |
Written by Settings |
| 46 |
============================*/ |
| 47 |
'writtenByText' => 'Written by', |
| 48 |
|
| 49 |
/* |
| 50 |
============================ |
| 51 |
Author Name Settings |
| 52 |
============================*/ |
| 53 |
'authorNameTag' => 'h4', |
| 54 |
|
| 55 |
/* |
| 56 |
============================ |
| 57 |
Meta Setting/Style Settings |
| 58 |
============================*/ |
| 59 |
'metaPosition' => 'bottom', |
| 60 |
|
| 61 |
/* |
| 62 |
============================ |
| 63 |
View all Post Button Settings |
| 64 |
============================*/ |
| 65 |
'viewAllPostText' => 'View All Posts', |
| 66 |
|
| 67 |
/* |
| 68 |
============================ |
| 69 |
Advanced Settings |
| 70 |
============================*/ |
| 71 |
'advanceId' => '', |
| 72 |
'advanceZindex' => '', |
| 73 |
'hideExtraLarge' => false, |
| 74 |
'hideDesktop' => false, |
| 75 |
'hideTablet' => false, |
| 76 |
'hideMobile' => false, |
| 77 |
'advanceCss' => '', |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
public function content( $attr, $noAjax ) { |
| 82 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 83 |
|
| 84 |
$block_name = 'author_box'; |
| 85 |
$author_bio = $wrapper_before = $wrapper_after = $content = ''; |
| 86 |
|
| 87 |
$page_post_id = get_the_ID(); |
| 88 |
|
| 89 |
if ( $page_post_id ) { |
| 90 |
|
| 91 |
$attr['className'] = isset( $attr['className'] ) && $attr['className'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['className'] ) : ''; |
| 92 |
$attr['align'] = isset( $attr['align'] ) && $attr['align'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['align'] ) : ''; |
| 93 |
$attr['advanceId'] = isset( $attr['advanceId'] ) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 94 |
$attr['blockId'] = isset( $attr['blockId'] ) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 95 |
$attr['authorNameTag'] = in_array( $attr['authorNameTag'], ultimate_post()->ultp_allowed_block_tags() ) ? $attr['authorNameTag'] : 'h4'; |
| 96 |
$attr['layout'] = sanitize_html_class( $attr['layout'] ); |
| 97 |
$attr['writtenByText'] = wp_kses( $attr['writtenByText'], ultimate_post()->ultp_allowed_html_tags() ); |
| 98 |
|
| 99 |
$_post = get_post( $page_post_id ); |
| 100 |
$post_author = get_userdata( $_post->post_author ); |
| 101 |
|
| 102 |
// Author Image |
| 103 |
$author_image = '<div class="ultp-post-author-image-section">'; |
| 104 |
$author_image .= get_avatar( $post_author->ID, $attr['imgRatio'] ); |
| 105 |
$author_image .= '</div>'; |
| 106 |
|
| 107 |
// Author Meta |
| 108 |
$author_meta = '<div class="ultp-post-author-meta">'; |
| 109 |
$author_meta .= '<span class="ultp-total-post">' . count_user_posts( $_post->post_author, $post_type = 'post' ) . ' Posts</span>'; |
| 110 |
$author_meta .= '<span class="ultp-total-comment">' . get_comments_number( $page_post_id ) . ' Comments</span>'; |
| 111 |
$author_meta .= '</div>'; |
| 112 |
|
| 113 |
// Author Description |
| 114 |
if ( $post_author->description ) { |
| 115 |
$author_bio .= '<div class="ultp-post-author-bio">'; |
| 116 |
$author_bio .= '<span class="ultp-post-author-bio-meta">' . $post_author->description . '</span>'; |
| 117 |
$author_bio .= '</div>'; |
| 118 |
} |
| 119 |
|
| 120 |
// Author Url |
| 121 |
if ( get_author_posts_url( $_post->post_author ) ) { |
| 122 |
$all_post_link = '<div class="ultp-author-post-link">'; |
| 123 |
$all_post_link .= '<a class="ultp-author-post-link-text" href="' . get_author_posts_url( $_post->post_author ) . '">' . $attr['viewAllPostText'] . '</a>'; |
| 124 |
$all_post_link .= '</div>'; |
| 125 |
} |
| 126 |
|
| 127 |
$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'] : '' ) . '">'; |
| 128 |
$wrapper_before .= '<div class="ultp-block-wrapper">'; |
| 129 |
$content .= '<div class="ultp-author-box ultp-author-box-' . $attr['layout'] . '-content">'; |
| 130 |
$content .= ( $attr['imgShow'] && $attr['layout'] !== 'layout4' ? $author_image : '' ); |
| 131 |
$content .= '<div class="ultp-post-author-details">'; |
| 132 |
$content .= '<div class="ultp-post-author-title">'; |
| 133 |
$content .= $attr['writtenByShow'] ? '<span class="ultp-post-author-written-by">' . $attr['writtenByText'] . '</span>' : ''; |
| 134 |
$content .= '<' . $attr['authorNameTag'] . ' class="ultp-post-author-name"><a href="' . get_author_posts_url( $_post->post_author ) . '">' . $post_author->display_name . '</a></' . $attr['authorNameTag'] . '>'; |
| 135 |
$content .= '</div>'; |
| 136 |
|
| 137 |
$content .= ( $attr['metaShow'] && $attr['metaPosition'] == 'top' ? $author_meta : '' ); |
| 138 |
|
| 139 |
$content .= ( $attr['authorBioShow'] && $author_bio ? $author_bio : '' ); |
| 140 |
$content .= ( $attr['metaShow'] && $attr['metaPosition'] == 'bottom' ? $author_meta : '' ); |
| 141 |
$content .= ( $attr['allPostLinkShow'] ? $all_post_link : '' ); |
| 142 |
$content .= '</div>'; |
| 143 |
$content .= ( $attr['imgShow'] && $attr['layout'] === 'layout4' ? $author_image : '' ); |
| 144 |
$content .= '</div>'; |
| 145 |
$wrapper_after .= '</div>'; |
| 146 |
$wrapper_after .= '</div>'; |
| 147 |
} |
| 148 |
|
| 149 |
return $wrapper_before . $content . $wrapper_after; |
| 150 |
} |
| 151 |
} |
| 152 |
|