PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.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 All 81 releases
ablocks / includes / blocks / dynamic-text / block.php

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

152 lines 5.4 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 $dynamic_text_css->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_dynamic_text_css( $attributes, $device ); } )
28 );
29 $dynamic_text_css->add_class_styles(
30 '{{WRAPPER}} .ablocks-dynamic-text-output a',
31 $this->get_dynamic_text_css( $attributes ),
32 $this->get_dynamic_text_css( $attributes, 'Tablet' ),
33 $this->get_dynamic_text_css( $attributes, 'Mobile' ),
34 $dynamic_text_css->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_dynamic_text_css( $attributes, $device ); } )
35 );
36
37 return $dynamic_text_css->generate_css();
38 }
39 public function get_dynamic_text_css( $attributes, $device = '' ) {
40
41 $dt_text_css = [];
42 if ( ! empty( $attributes['dynamicTextColor'] ) ) {
43 $dt_text_css['color'] = $attributes['dynamicTextColor'];
44 }
45 $textAlignCss = ! empty( $attributes['alignment'] ) ? Alignment::get_css( $attributes['alignment'], 'text-align', $device ) : [];
46
47 return array_merge(
48 $dt_text_css,
49 [ 'color' => Color::get_css( isset( $attributes['dynamicTextColor'] ) ? $attributes['dynamicTextColor'] : '' ) ],
50 Typography::get_css( $attributes['dynamicTypography'] ?? [], '', $device ),
51 $textAlignCss
52 );
53 }
54
55 private function trim_text( $text, $mode_trim, $words_limit, $symbol ) {
56 $text = wp_strip_all_tags( $text );
57 if ( $words_limit && $mode_trim ) {
58 return esc_html( wp_trim_words( $text, $words_limit, $symbol ) );
59 } else {
60 return esc_html( $text );
61 }
62 }
63 private function render_core( $key, $post_id, $words_limit, $mode_trim, $symbol ) {
64 switch ( $key ) {
65 case 'core:title':
66 $title = get_the_title( $post_id );
67 if (
68 empty( $title )
69 && $this->is_editor_preview()
70 ) {
71 return __( 'No title available — please add title', 'ablocks' );
72 }
73 return $this->trim_text( $title, $mode_trim, $words_limit, $symbol );
74 case 'core:excerpt':
75 return $this->trim_text( get_the_excerpt( $post_id ), $mode_trim, $words_limit, $symbol );
76 case 'core:date':
77 return esc_html( get_the_date( get_option( 'date_format' ), $post_id ) );
78 case 'core:last_modified':
79 return esc_html( get_the_modified_date( get_option( 'date_format' ), $post_id ) );
80 case 'core:author':
81 return esc_html( get_the_author_meta( 'display_name', get_post_field( 'post_author', $post_id ) ) );
82 case 'core:permalink':
83 $url = get_permalink( $post_id );
84 return '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
85 case 'core:content':
86 static $guard = false;
87 static $content_cache = [];
88 if ( $guard ) {
89 return '';
90 }
91 // Render the post's content once per request; multiple dynamic-text
92 // "content" blocks (or a loop/archive) would otherwise re-run
93 // do_blocks() on the full post each time.
94 if ( ! isset( $content_cache[ $post_id ] ) ) {
95 $guard = true;
96 $content_cache[ $post_id ] = do_blocks( get_post_field( 'post_content', $post_id ) );
97 $guard = false;
98 }
99 $content = $content_cache[ $post_id ];
100 if (
101 empty( trim( wp_strip_all_tags( $content ) ) )
102 && $this->is_editor_preview()
103 ) {
104 return __( 'No content available — please add content', 'ablocks' );
105 }
106 return ( $words_limit && $mode_trim )
107 ? $this->trim_text( $content, $mode_trim, $words_limit, $symbol )
108 : $content;
109 }//end switch
110 return '';
111 }
112 private function render_tax( $key, $post_id ) {
113 $taxonomy = strtok( str_replace( '__tax:', '', $key ), ':' );
114 $terms = get_the_terms( $post_id, $taxonomy );
115 if ( empty( $terms ) || is_wp_error( $terms ) ) {
116 return '';
117 }
118 return implode( ', ', wp_list_pluck( $terms, 'name' ) );
119 }
120 private function is_editor_preview() {
121 return Helper::is_gutenberg_editor();
122 }
123 public function render_block_content( $attributes, $content = '', $block = null ) {
124 $post_id = $attributes['postId']
125 ?? ( $block->context['postId'] ?? get_the_ID() );
126
127 if ( ! $post_id ) {
128 return '<em>No preview post found.</em>';
129 }
130 $key = ! empty( $attributes['metaKey'] ) ? $attributes['metaKey'] : 'core:title';
131 $limit = (int) ( $attributes['trimLimit'] ?? 0 );
132 $trim = ! empty( $attributes['trimMode'] );
133 $symbol = $attributes['trimEndSymbol'] ?? '…';
134
135 if ( strpos( $key, 'core:' ) === 0 ) {
136 $output = $this->render_core( $key, $post_id, $limit, $trim ? 'words' : null, $symbol );
137 } elseif ( strpos( $key, '__tax:' ) === 0 ) {
138 $output = $this->render_tax( $key, $post_id );
139 } else {
140 $output = '';
141 }
142
143 if ( ! $output ) {
144 return '';
145 }
146 if ( ! empty( $attributes['linkAdd'] ) && $key !== 'core:permalink' ) {
147 $output = '<a href="' . esc_url( get_permalink( $post_id ) ) . '">' . $output . '</a>';
148 }
149 return '<div class="ablocks-dynamic-text-output">' . $output . '</div>';
150 }
151 }
152