| 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 |
static $content_cache = []; |
| 86 |
if ( $guard ) { |
| 87 |
return ''; |
| 88 |
} |
| 89 |
// Render the post's content once per request; multiple dynamic-text |
| 90 |
// "content" blocks (or a loop/archive) would otherwise re-run |
| 91 |
// do_blocks() on the full post each time. |
| 92 |
if ( ! isset( $content_cache[ $post_id ] ) ) { |
| 93 |
$guard = true; |
| 94 |
$content_cache[ $post_id ] = do_blocks( get_post_field( 'post_content', $post_id ) ); |
| 95 |
$guard = false; |
| 96 |
} |
| 97 |
$content = $content_cache[ $post_id ]; |
| 98 |
if ( |
| 99 |
empty( trim( wp_strip_all_tags( $content ) ) ) |
| 100 |
&& $this->is_editor_preview() |
| 101 |
) { |
| 102 |
return __( 'No content available — please add content', 'ablocks' ); |
| 103 |
} |
| 104 |
return ( $words_limit && $mode_trim ) |
| 105 |
? $this->trim_text( $content, $mode_trim, $words_limit, $symbol ) |
| 106 |
: $content; |
| 107 |
}//end switch |
| 108 |
return ''; |
| 109 |
} |
| 110 |
private function render_tax( $key, $post_id ) { |
| 111 |
$taxonomy = strtok( str_replace( '__tax:', '', $key ), ':' ); |
| 112 |
$terms = get_the_terms( $post_id, $taxonomy ); |
| 113 |
if ( empty( $terms ) || is_wp_error( $terms ) ) { |
| 114 |
return ''; |
| 115 |
} |
| 116 |
return implode( ', ', wp_list_pluck( $terms, 'name' ) ); |
| 117 |
} |
| 118 |
private function is_editor_preview() { |
| 119 |
return Helper::is_gutenberg_editor(); |
| 120 |
} |
| 121 |
public function render_block_content( $attributes, $content = '', $block = null ) { |
| 122 |
$post_id = $attributes['postId'] |
| 123 |
?? ( $block->context['postId'] ?? get_the_ID() ); |
| 124 |
|
| 125 |
if ( ! $post_id ) { |
| 126 |
return '<em>No preview post found.</em>'; |
| 127 |
} |
| 128 |
$key = ! empty( $attributes['metaKey'] ) ? $attributes['metaKey'] : 'core:title'; |
| 129 |
$limit = (int) ( $attributes['trimLimit'] ?? 0 ); |
| 130 |
$trim = ! empty( $attributes['trimMode'] ); |
| 131 |
$symbol = $attributes['trimEndSymbol'] ?? '…'; |
| 132 |
|
| 133 |
if ( strpos( $key, 'core:' ) === 0 ) { |
| 134 |
$output = $this->render_core( $key, $post_id, $limit, $trim ? 'words' : null, $symbol ); |
| 135 |
} elseif ( strpos( $key, '__tax:' ) === 0 ) { |
| 136 |
$output = $this->render_tax( $key, $post_id ); |
| 137 |
} else { |
| 138 |
$output = ''; |
| 139 |
} |
| 140 |
|
| 141 |
if ( ! $output ) { |
| 142 |
return ''; |
| 143 |
} |
| 144 |
if ( ! empty( $attributes['linkAdd'] ) && $key !== 'core:permalink' ) { |
| 145 |
$output = '<a href="' . esc_url( get_permalink( $post_id ) ) . '">' . $output . '</a>'; |
| 146 |
} |
| 147 |
return '<div class="ablocks-dynamic-text-output">' . $output . '</div>'; |
| 148 |
} |
| 149 |
} |
| 150 |
|