PluginProbe
eRecht24 Legal Texts / trunk
eRecht24 Legal Texts vtrunk
4.1.0 4.0.5 4.0.4 4.0.3 trunk 4.0.0 4.0.1 4.0.2
erecht24 / src / Frontend / Shortcodes.php

Shortcodes.php in eRecht24 Legal Texts trunk, at src/Frontend/Shortcodes.php

88 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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