PluginProbe
Embed Privacy / 1.10.2
Embed Privacy v1.10.2
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / inc / embed / class-style.php

class-style.php in Embed Privacy 1.10.2, at inc/embed/class-style.php

143 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace epiphyt\Embed_Privacy\embed;
3
4 use WP_Theme_JSON_Resolver;
5
6 /**
7 * Styles of an embed.
8 *
9 * @author Epiphyt
10 * @license GPL2
11 * @package epiphyt\Embed_Privacy
12 * @since 1.10.0
13 */
14 final class Style {
15 /**
16 * @var \epiphyt\Embed_Privacy\embed\Assets Assets object
17 */
18 private $assets = null;
19
20 /**
21 * @var array List of CSS properties and values divided by elements
22 */
23 private $styling = [];
24
25 /**
26 * Construct the object.
27 *
28 * @param string $provider Provider name
29 * @param \WP_Post|null $embed_post Settings of the embed provider
30 * @param array $attributes Additional embed attributes
31 */
32 public function __construct( $provider, $embed_post = null, $attributes = [] ) {
33 $this->assets = new Assets( $provider, $embed_post, $attributes );
34
35 $this->register_from_assets();
36 $this->register_from_attributes( $attributes );
37 }
38
39 /**
40 * Get the style for an element.
41 *
42 * @param string $element Element to get the style for
43 * @return string Style as CSS
44 */
45 public function get( $element ) {
46 $style = '';
47
48 if ( empty( $this->styling[ $element ] ) ) {
49 return $style;
50 }
51
52 foreach ( $this->styling[ $element ] as $property => $value ) {
53 $style .= \sprintf( '%1$s: %2$s; ', $property, $value );
54 }
55
56 return \trim( $style );
57 }
58
59 /**
60 * Register a style for an element.
61 *
62 * @param string $element Element the style is for
63 * @param string $property CSS property
64 * @param string $value CSS value
65 */
66 public function register( $element, $property, $value ) {
67 $this->styling[ $element ][ $property ] = $value;
68 }
69
70 /**
71 * Register style from embed assets.
72 */
73 private function register_from_assets() {
74 $background = $this->assets->get_thumbnail();
75 $logo = $this->assets->get_logo();
76
77 if ( empty( $background['path'] ) ) {
78 $background = $this->assets->get_background();
79 }
80
81 if ( ! empty( $background['path'] ) ) {
82 $this->styling['container']['background-image'] = \sprintf(
83 'url(%1$s?ver=%2$s)',
84 $background['url'],
85 $background['version']
86 );
87 }
88
89 if ( ! empty( $logo['path'] ) ) {
90 $this->styling['logo']['background-image'] = \sprintf( 'url(%1$s?ver=%2$s)', $logo['url'], $logo['version'] );
91 }
92 }
93
94 /**
95 * Register style from embed attributes.
96 *
97 * @param array $attributes Embed attributes
98 */
99 private function register_from_attributes( $attributes ) {
100 if (
101 ! empty( $attributes['height'] )
102 && ! empty( $attributes['width'] )
103 && empty( $attributes['ignore_aspect_ratio'] )
104 ) {
105 // if height is in percentage, we cannot determine the aspect ratio
106 if ( \str_contains( $attributes['height'], '%' ) ) {
107 $attributes['ignore_aspect_ratio'] = true;
108 }
109
110 // if width is in percentage, we need to use the content width
111 // since we cannot determine the actual width
112 if ( \str_contains( $attributes['width'], '%' ) ) {
113 global $content_width;
114
115 if ( $content_width === null ) {
116 $theme_json_data = WP_Theme_JSON_Resolver::get_theme_data();
117
118 if ( ! empty( $theme_json_data->get_settings()['layout']['contentSize'] ) ) {
119 $content_width = (int) \preg_replace( '/[^\d]*/', '', $theme_json_data->get_settings()['layout']['contentSize'] );
120 }
121 }
122
123 if ( $content_width === null ) {
124 $content_width = 800;
125 }
126
127 /**
128 * Filter the theme content width, which is used to determine the correct aspect ratio.
129 *
130 * @since 1.10.0
131 *
132 * @param int $content_width Current content width
133 */
134 $content_width = (int) \apply_filters( 'embed_privacy_theme_content_width', (int) $content_width );
135
136 $attributes['width'] = $content_width;
137 }
138
139 $this->register( 'container', 'aspect-ratio', $attributes['width'] . '/' . $attributes['height'] );
140 }
141 }
142 }
143