| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Post_Excerpt { |
| 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 Excerpt Setting |
| 17 |
============================*/ |
| 18 |
'excerptLimit' => "150", |
| 19 |
|
| 20 |
//-------------------------- |
| 21 |
// Advanced Style |
| 22 |
//-------------------------- |
| 23 |
'advanceId' => '', |
| 24 |
'advanceZindex' => '', |
| 25 |
'hideExtraLarge' => false, |
| 26 |
'hideDesktop' => false, |
| 27 |
'hideTablet' => false, |
| 28 |
'hideMobile' => false, |
| 29 |
'advanceCss' => '', |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
public function register() { |
| 34 |
register_block_type( 'ultimate-post/post-excerpt', |
| 35 |
array( |
| 36 |
'editor_script' => 'ultp-blocks-editor-script', |
| 37 |
'editor_style' => 'ultp-blocks-editor-css', |
| 38 |
'render_callback' => array($this, 'content') |
| 39 |
) |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
public function content($attr, $noAjax) { |
| 44 |
$attr = wp_parse_args($attr, $this->get_attributes()); |
| 45 |
$block_name = 'post-excerpt'; |
| 46 |
$wrapper_before = $wrapper_after = $content = ''; |
| 47 |
|
| 48 |
$attr['className'] = isset($attr['className']) && $attr['className'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['className']) : ''; |
| 49 |
$attr['align'] = isset($attr['align']) && $attr['align'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['align']) : ''; |
| 50 |
$attr['advanceId'] = isset($attr['advanceId']) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 51 |
$attr['blockId'] = isset($attr['blockId']) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 52 |
|
| 53 |
$excerpt = $this->excerpt_word($attr['excerptLimit']); |
| 54 |
if ($excerpt) { |
| 55 |
$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"]:'' ).'">'; |
| 56 |
$wrapper_before .= '<div class="ultp-block-wrapper">'; |
| 57 |
$content .= '<div class="ultp-builder-excerpt">'; |
| 58 |
$content .= $excerpt; |
| 59 |
$content .= '</div>'; |
| 60 |
$wrapper_after .= '</div>'; |
| 61 |
$wrapper_after .= '</div>'; |
| 62 |
} |
| 63 |
|
| 64 |
return $wrapper_before.$content.$wrapper_after; |
| 65 |
} |
| 66 |
|
| 67 |
public function excerpt_word($charlength = 200) { |
| 68 |
$html = ''; |
| 69 |
$charlength++; |
| 70 |
$excerpt = get_the_excerpt(); |
| 71 |
if (mb_strlen( $excerpt ) > $charlength ) { |
| 72 |
$subex = mb_substr( $excerpt, 0, $charlength ); |
| 73 |
$exwords = explode( ' ', $subex ); |
| 74 |
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) ); |
| 75 |
if ($excut < 0 ) { |
| 76 |
$html = mb_substr( $subex, 0, $excut ); |
| 77 |
} else { |
| 78 |
$html = $subex; |
| 79 |
} |
| 80 |
$html .= '...'; |
| 81 |
} else { |
| 82 |
$html = $excerpt; |
| 83 |
} |
| 84 |
return $html; |
| 85 |
} |
| 86 |
|
| 87 |
} |