PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.0.41
Post Grid Gutenberg Blocks – PostX v5.0.41
5.0.41 5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 All 238 releases
ultimate-post / addons / builder / blocks / Post_Excerpt.php

Post_Excerpt.php in Post Grid Gutenberg Blocks – PostX 5.0.41, at addons/builder/blocks/Post_Excerpt.php

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