PluginProbe
Blocks – Reusable Content, Shortcodes & Site Variables / trunk
Blocks – Reusable Content, Shortcodes & Site Variables vtrunk
26.09.03.15 26.08.31.21 26.08.22.20 26.08.22.22 26.08.22.17 26.08.22.13 26.08.21.19 26.08.07.23 26.07.19.14 26.07.13.21 26.07.13.17 26.07.12.13 026.07.07.21 026.07.05.18 026.06.26.20 026.06.26.21 026.06.08.20 026.05.13.14 026.04.29.10 trunk 026.02.22.22 026.03.16.23 026.04.23.13
blocks / src / Variables / VariableResolver.php

VariableResolver.php in Blocks – Reusable Content, Shortcodes & Site Variables trunk, at src/Variables/VariableResolver.php

93 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace RenzoJohnson\Blocks\Variables;
6
7 \defined( 'ABSPATH' ) || exit;
8
9 final class VariableResolver {
10
11 /** @var array<string, string>|null */
12 private ?array $map = null;
13
14 /** @param array<string, string> $option_tokens */
15 public function __construct(
16 private readonly \Blocks_Settings_Repository $settings,
17 private readonly array $option_tokens,
18 ) {
19 }
20
21 public function replace_html( string $content ): string {
22 if ( false === \stripos( $content, 'blocks:' ) ) {
23 return $content;
24 }
25
26 $replaced = \preg_replace_callback(
27 '/\{\{\s*blocks:([A-Za-z0-9_]{1,40})\s*\}\}/i',
28 fn ( array $matches ): string => \esc_html( $this->resolve( \strtolower( $matches[1] ) ) ),
29 $content,
30 );
31
32 return \is_string( $replaced ) ? $replaced : $content;
33 }
34
35 public function resolve( string $name ): string {
36 return $this->map()[ \strtolower( $name ) ] ?? '';
37 }
38
39 /** @return array<string, string> */
40 public function public_variables(): array {
41 $variables = array();
42
43 foreach ( $this->option_tokens as $option_name => $token_name ) {
44 $variables[ $token_name ] = \str_ends_with( $token_name, '_url' )
45 ? $this->image_url( $option_name )
46 : $this->settings->get_string( $option_name );
47 }
48
49 $site_title = $variables['site_title'];
50 $tagline = $variables['tagline'];
51 $date_format = \get_option( 'date_format' );
52 $date_format = \is_string( $date_format ) && '' !== $date_format ? $date_format : 'F j, Y';
53
54 return \array_merge(
55 $variables,
56 array(
57 'phone_plain' => (string) \preg_replace( '/[^0-9]/', '', $variables['phone'] ),
58 'site_title' => '' !== $site_title ? $site_title : (string) \get_bloginfo( 'name' ),
59 'tagline' => '' !== $tagline ? $tagline : (string) \get_bloginfo( 'description' ),
60 'site_url' => (string) \home_url( '/' ),
61 'year' => (string) \wp_date( 'Y' ),
62 'month' => (string) \wp_date( 'F' ),
63 'today' => (string) \wp_date( $date_format ),
64 ),
65 );
66 }
67
68 public function flush(): void {
69 $this->map = null;
70 }
71
72 /** @return array<string, string> */
73 private function map(): array {
74 if ( null === $this->map ) {
75 $this->map = $this->public_variables();
76 }
77
78 return $this->map;
79 }
80
81 private function image_url( string $option_name ): string {
82 $attachment_id = $this->settings->get_int( $option_name );
83
84 if ( 0 >= $attachment_id ) {
85 return '';
86 }
87
88 $url = \wp_get_attachment_url( $attachment_id );
89
90 return \is_string( $url ) ? $url : '';
91 }
92 }
93