| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shortcode rendering. |
| 4 |
* |
| 5 |
* @package ERecht24LegalText |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ERecht24LegalText\Frontend; |
| 9 |
|
| 10 |
use ERecht24LegalText\Settings; |
| 11 |
|
| 12 |
defined( 'ABSPATH' ) || exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Registers and renders shortcodes. |
| 16 |
*/ |
| 17 |
final class Shortcodes { |
| 18 |
|
| 19 |
/** |
| 20 |
* Settings service. |
| 21 |
* |
| 22 |
* @var Settings |
| 23 |
*/ |
| 24 |
private $settings; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @param Settings $settings Settings service. |
| 30 |
*/ |
| 31 |
public function __construct( Settings $settings ) { |
| 32 |
$this->settings = $settings; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Register shortcodes. |
| 37 |
*/ |
| 38 |
public function register(): void { |
| 39 |
add_shortcode( 'erecht24', array( $this, 'render' ) ); |
| 40 |
add_shortcode( 'erecht24_widget', array( $this, 'render' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Render legal text shortcode. |
| 45 |
* |
| 46 |
* @param array<string,mixed> $atts Shortcode attributes. |
| 47 |
* @param string|null $content Enclosed content, unused. |
| 48 |
* @param string $shortcode_tag Shortcode tag. |
| 49 |
*/ |
| 50 |
public function render( $atts = array(), ?string $content = null, string $shortcode_tag = 'erecht24' ): string { |
| 51 |
unset( $content, $shortcode_tag ); |
| 52 |
|
| 53 |
$atts = shortcode_atts( |
| 54 |
array( |
| 55 |
'type' => 'imprint', |
| 56 |
'lang' => 'de', |
| 57 |
'strip_title' => false, |
| 58 |
), |
| 59 |
is_array( $atts ) ? $atts : array(), |
| 60 |
'erecht24' |
| 61 |
); |
| 62 |
|
| 63 |
$type = Settings::normalize_document_type( (string) $atts['type'] ); |
| 64 |
$language = 'en' === strtolower( (string) $atts['lang'] ) ? 'en' : 'de'; |
| 65 |
$strip_title = wp_validate_boolean( $atts['strip_title'] ); |
| 66 |
|
| 67 |
if ( '' === $type ) { |
| 68 |
return esc_html__( 'Der angeforderte Rechtstext-Typ ist nicht erlaubt.', 'erecht24' ); |
| 69 |
} |
| 70 |
|
| 71 |
$html = $this->settings->get_document_html( $type, $language ); |
| 72 |
|
| 73 |
if ( '' === trim( wp_strip_all_tags( $html ) ) ) { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
|
| 77 |
if ( $strip_title ) { |
| 78 |
$html = preg_replace( '#<h1\b[^>]*>.*?</h1>#is', '', $html, 1 ); |
| 79 |
} |
| 80 |
|
| 81 |
return sprintf( |
| 82 |
'<div class="erecht24-legal-texts erecht24-legal-texts-%1$s" style="word-wrap: break-word;">%2$s</div>', |
| 83 |
esc_attr( $type ), |
| 84 |
wp_kses_post( $html ) |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
|