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