PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.8.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.8.0
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / blocks / dynamic-text / block.php

block.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.8.0, at includes/blocks/dynamic-text/block.php

143 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Blocks\DynamicText;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use ABlocks\Classes\BlockBaseAbstract;
9 use ABlocks\Classes\CssGeneratorV2;
10 use ABlocks\Controls\Typography;
11 use ABlocks\Controls\Alignment;
12 use ABlocks\Controls\Color;
13 use ABlocks\Helper;
14
15 class Block extends BlockBaseAbstract {
16
17 protected $block_name = 'dynamic-text';
18
19 public function build_css( $attributes ) {
20 $dynamic_text_css = new CssGeneratorV2( $attributes, $this->block_name );
21
22 $dynamic_text_css->add_class_styles(
23 '{{WRAPPER}} .ablocks-dynamic-text-output',
24 $this->get_dynamic_text_css( $attributes ),
25 $this->get_dynamic_text_css( $attributes, 'Tablet' ),
26 $this->get_dynamic_text_css( $attributes, 'Mobile' )
27 );
28 $dynamic_text_css->add_class_styles(
29 '{{WRAPPER}} .ablocks-dynamic-text-output a',
30 $this->get_dynamic_text_css( $attributes ),
31 $this->get_dynamic_text_css( $attributes, 'Tablet' ),
32 $this->get_dynamic_text_css( $attributes, 'Mobile' )
33 );
34
35 return $dynamic_text_css->generate_css();
36 }
37 public function get_dynamic_text_css( $attributes, $device = '' ) {
38
39 $dt_text_css = [];
40 if ( ! empty( $attributes['dynamicTextColor'] ) ) {
41 $dt_text_css['color'] = $attributes['dynamicTextColor'];
42 }
43 $textAlignCss = ! empty( $attributes['alignment'] ) ? Alignment::get_css( $attributes['alignment'], 'text-align', $device ) : [];
44
45 return array_merge(
46 $dt_text_css,
47 [ 'color' => Color::get_css( isset( $attributes['dynamicTextColor'] ) ? $attributes['dynamicTextColor'] : '' ) ],
48 Typography::get_css( $attributes['dynamicTypography'] ?? [], '', $device ),
49 $textAlignCss
50 );
51 }
52
53 private function trim_text( $text, $mode_trim, $words_limit, $symbol ) {
54 $text = wp_strip_all_tags( $text );
55 if ( $words_limit && $mode_trim ) {
56 return esc_html( wp_trim_words( $text, $words_limit, $symbol ) );
57 } else {
58 return esc_html( $text );
59 }
60 }
61 private function render_core( $key, $post_id, $words_limit, $mode_trim, $symbol ) {
62 switch ( $key ) {
63 case 'core:title':
64 $title = get_the_title( $post_id );
65 if (
66 empty( $title )
67 && $this->is_editor_preview()
68 ) {
69 return __( 'No title available — please add title', 'ablocks' );
70 }
71 return $this->trim_text( $title, $mode_trim, $words_limit, $symbol );
72 case 'core:excerpt':
73 return $this->trim_text( get_the_excerpt( $post_id ), $mode_trim, $words_limit, $symbol );
74 case 'core:date':
75 return esc_html( get_the_date( get_option( 'date_format' ), $post_id ) );
76 case 'core:last_modified':
77 return esc_html( get_the_modified_date( get_option( 'date_format' ), $post_id ) );
78 case 'core:author':
79 return esc_html( get_the_author_meta( 'display_name', get_post_field( 'post_author', $post_id ) ) );
80 case 'core:permalink':
81 $url = get_permalink( $post_id );
82 return '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
83 case 'core:content':
84 static $guard = false;
85 if ( $guard ) {
86 return '';
87 }
88 $guard = true;
89 $content = do_blocks( get_post_field( 'post_content', $post_id ) );
90 $guard = false;
91 if (
92 empty( trim( wp_strip_all_tags( $content ) ) )
93 && $this->is_editor_preview()
94 ) {
95 return __( 'No content available — please add content', 'ablocks' );
96 }
97 return ( $words_limit && $mode_trim )
98 ? $this->trim_text( $content, $mode_trim, $words_limit, $symbol )
99 : $content;
100 }//end switch
101 return '';
102 }
103 private function render_tax( $key, $post_id ) {
104 $taxonomy = strtok( str_replace( '__tax:', '', $key ), ':' );
105 $terms = get_the_terms( $post_id, $taxonomy );
106 if ( empty( $terms ) || is_wp_error( $terms ) ) {
107 return '';
108 }
109 return implode( ', ', wp_list_pluck( $terms, 'name' ) );
110 }
111 private function is_editor_preview() {
112 return Helper::is_gutenberg_editor();
113 }
114 public function render_block_content( $attributes, $content = '', $block = null ) {
115 $post_id = $attributes['postId']
116 ?? ( $block->context['postId'] ?? get_the_ID() );
117
118 if ( ! $post_id ) {
119 return '<em>No preview post found.</em>';
120 }
121 $key = ! empty( $attributes['metaKey'] ) ? $attributes['metaKey'] : 'core:title';
122 $limit = (int) ( $attributes['trimLimit'] ?? 0 );
123 $trim = ! empty( $attributes['trimMode'] );
124 $symbol = $attributes['trimEndSymbol'] ?? '';
125
126 if ( strpos( $key, 'core:' ) === 0 ) {
127 $output = $this->render_core( $key, $post_id, $limit, $trim ? 'words' : null, $symbol );
128 } elseif ( strpos( $key, '__tax:' ) === 0 ) {
129 $output = $this->render_tax( $key, $post_id );
130 } else {
131 $output = '';
132 }
133
134 if ( ! $output ) {
135 return '';
136 }
137 if ( ! empty( $attributes['linkAdd'] ) && $key !== 'core:permalink' ) {
138 $output = '<a href="' . esc_url( get_permalink( $post_id ) ) . '">' . $output . '</a>';
139 }
140 return '<div class="ablocks-dynamic-text-output">' . $output . '</div>';
141 }
142 }
143