PluginProbe
Embed Privacy / trunk
Embed Privacy vtrunk
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 trunk, at inc/embed/class-style.php

174 lines 4.6 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 epiphyt\Embed_Privacy\data\Providers;
5 use WP_Theme_JSON_Resolver;
6
7 /**
8 * Styles of an embed.
9 *
10 * @author Epiphyt
11 * @license GPL2
12 * @package epiphyt\Embed_Privacy
13 * @since 1.10.0
14 */
15 final class Style {
16 /**
17 * @var \epiphyt\Embed_Privacy\embed\Assets Assets object
18 */
19 private $assets = null;
20
21 /**
22 * @var array List of CSS properties and values divided by elements
23 */
24 private $styling = [];
25
26 /**
27 * Construct the object.
28 *
29 * @since 1.11.0 Deprecated second parameter
30 * @since 1.11.0 First parameter must be a provider object
31 *
32 * @param string|\epiphyt\Embed_Privacy\embed\Provider $provider Provider object
33 * @param null $deprecated Deprecated parameter
34 * @param array $attributes Additional embed attributes
35 */
36 public function __construct( $provider, $deprecated = null, $attributes = [] ) {
37 if ( \is_string( $provider ) ) {
38 \_doing_it_wrong(
39 __METHOD__,
40 \sprintf(
41 /* translators: parameter name */
42 \esc_html__( 'Passing a string as parameter %s is deprecated.', 'embed-privacy' ),
43 '$provider'
44 ),
45 '1.11.0'
46 );
47
48 $provider = Providers::get_instance()->get_by_name( $provider );
49 }
50
51 if ( $deprecated !== null ) {
52 \_deprecated_argument( __METHOD__, '1.11.0' );
53 }
54
55 $this->assets = new Assets( $provider, $deprecated, $attributes );
56
57 $this->register_from_assets();
58 $this->register_from_attributes( $attributes );
59 }
60
61 /**
62 * Get the style for an element.
63 *
64 * @param string $element Element to get the style for
65 * @return string Style as CSS
66 */
67 public function get( $element ) {
68 $style = '';
69
70 if ( empty( $this->styling[ $element ] ) ) {
71 return $style;
72 }
73
74 foreach ( $this->styling[ $element ] as $property => $value ) {
75 $style .= \sprintf( '%1$s: %2$s; ', $property, $value );
76 }
77
78 return \trim( $style );
79 }
80
81 /**
82 * Register a style for an element.
83 *
84 * @param string $element Element the style is for
85 * @param string $property CSS property
86 * @param string $value CSS value
87 */
88 public function register( $element, $property, $value ) {
89 $this->styling[ $element ][ $property ] = $value;
90 }
91
92 /**
93 * Register style from embed assets.
94 */
95 private function register_from_assets() {
96 $background = $this->assets->get_thumbnail();
97 $logo = $this->assets->get_logo();
98
99 if ( empty( $background['path'] ) ) {
100 $background = $this->assets->get_background();
101 }
102
103 if ( ! empty( $background['path'] ) ) {
104 $this->styling['container']['background-image'] = \sprintf(
105 'url(%1$s?ver=%2$s)',
106 \esc_url( $background['url'] ),
107 $background['version']
108 );
109 }
110
111 if ( ! empty( $logo['path'] ) ) {
112 $this->styling['logo']['background-image'] = \sprintf( 'url(%1$s?ver=%2$s)', \esc_url( $logo['url'] ), $logo['version'] );
113 }
114 }
115
116 /**
117 * Register style from embed attributes.
118 *
119 * @param array $attributes Embed attributes
120 */
121 private function register_from_attributes( $attributes ) {
122 if (
123 ! empty( $attributes['height'] )
124 && ! empty( $attributes['width'] )
125 && empty( $attributes['ignore_aspect_ratio'] )
126 ) {
127 // if height is in percentage, we cannot determine the aspect ratio
128 if ( \str_contains( $attributes['height'], '%' ) ) {
129 $attributes['ignore_aspect_ratio'] = true;
130
131 return;
132 }
133
134 // if width is in percentage, we need to use the content width
135 // since we cannot determine the actual width
136 if (
137 \str_contains( $attributes['width'], '%' )
138 || \str_contains( $attributes['width'], '@@epi_percentage' )
139 ) {
140 global $content_width;
141
142 if ( $content_width === null ) {
143 $theme_json_data = WP_Theme_JSON_Resolver::get_theme_data();
144
145 if ( ! empty( $theme_json_data->get_settings()['layout']['contentSize'] ) ) {
146 $content_width = (int) \preg_replace( '/[^\d]*/', '', $theme_json_data->get_settings()['layout']['contentSize'] );
147 }
148 }
149
150 if ( $content_width === null ) {
151 $content_width = 800;
152 }
153
154 /**
155 * Filter the theme content width, which is used to determine the correct aspect ratio.
156 *
157 * @since 1.10.0
158 *
159 * @param int $content_width Current content width
160 */
161 $content_width = (int) \apply_filters( 'embed_privacy_theme_content_width', (int) $content_width );
162
163 $attributes['width'] = $content_width;
164 }
165
166 // explicit units are not welcome here
167 $attributes['height'] = \preg_replace( '/[^0-9]/', '', $attributes['height'] );
168 $attributes['width'] = \preg_replace( '/[^0-9]/', '', $attributes['width'] );
169
170 $this->register( 'container', 'aspect-ratio', $attributes['width'] . '/' . $attributes['height'] );
171 }
172 }
173 }
174