| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Post_View_Count { |
| 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 View Settings |
| 18 |
============================*/ |
| 19 |
'viewLabel' => true, |
| 20 |
'viewIconShow' => true, |
| 21 |
/* |
| 22 |
============================ |
| 23 |
Post View Label Style |
| 24 |
============================*/ |
| 25 |
|
| 26 |
'viewLabelText' => 'View', |
| 27 |
'viewLabelAlign' => 'after', |
| 28 |
/* |
| 29 |
============================ |
| 30 |
Post View Icon Style |
| 31 |
============================*/ |
| 32 |
'viewIconStyle' => 'viewCount1', |
| 33 |
|
| 34 |
// -------------------------- |
| 35 |
// Advanced Settings |
| 36 |
// -------------------------- |
| 37 |
'advanceId' => '', |
| 38 |
'advanceZindex' => '', |
| 39 |
'hideExtraLarge' => false, |
| 40 |
'hideDesktop' => false, |
| 41 |
'hideTablet' => false, |
| 42 |
'hideMobile' => false, |
| 43 |
'advanceCss' => '', |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
public function register() { |
| 48 |
register_block_type( |
| 49 |
'ultimate-post/post-view-count', |
| 50 |
array( |
| 51 |
'editor_script' => 'ultp-blocks-editor-script', |
| 52 |
'editor_style' => 'ultp-blocks-editor-css', |
| 53 |
'render_callback' => array( $this, 'content' ), |
| 54 |
) |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
public function content( $attr, $noAjax ) { |
| 59 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 60 |
$block_name = 'post-view-count'; |
| 61 |
$wrapper_before = $wrapper_after = $content = ''; |
| 62 |
|
| 63 |
$count = get_post_meta( get_the_ID(), '__post_views_count', true ); |
| 64 |
|
| 65 |
$attr['className'] = isset( $attr['className'] ) && $attr['className'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['className'] ) : ''; |
| 66 |
$attr['align'] = isset( $attr['align'] ) && $attr['align'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['align'] ) : ''; |
| 67 |
$attr['advanceId'] = isset( $attr['advanceId'] ) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 68 |
$attr['blockId'] = isset( $attr['blockId'] ) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 69 |
$attr['viewLabelText'] = wp_kses( $attr['viewLabelText'], ultimate_post()->ultp_allowed_html_tags() ); |
| 70 |
|
| 71 |
$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'] : '' ) . '">'; |
| 72 |
$wrapper_before .= '<div class="ultp-block-wrapper">'; |
| 73 |
$content .= '<span class="ultp-view-count">'; |
| 74 |
if ( $attr['viewIconShow'] && $attr['viewIconStyle'] ) { |
| 75 |
$content .= ultimate_post()->get_svg_icon( $attr['viewIconStyle'] ); |
| 76 |
} |
| 77 |
$content .= '<span class="ultp-view-count-number">'; |
| 78 |
$content .= $count ? $count : 0; |
| 79 |
$content .= '</span>'; |
| 80 |
if ( $attr['viewLabel'] ) { |
| 81 |
$content .= '<span class="ultp-view-label"> ' . $attr['viewLabelText'] . '</span>'; |
| 82 |
} |
| 83 |
$content .= '</span>'; |
| 84 |
$wrapper_after .= '</div>'; |
| 85 |
$wrapper_after .= '</div>'; |
| 86 |
|
| 87 |
return $wrapper_before . $content . $wrapper_after; |
| 88 |
} |
| 89 |
} |
| 90 |
|