-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEmbeds.php
166 lines (147 loc) · 4.11 KB
/
Embeds.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* Embed filters and actions.
*
* @author Justin Tadlock <[email protected]>
* @copyright 2023 Justin Tadlock
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0-or-later
* @link https://github.com/x3p0-dev/x3p0-ideas
*/
declare(strict_types=1);
namespace X3P0\Ideas;
use X3P0\Ideas\Contracts\Bootable;
use X3P0\Ideas\Tools\HookAnnotation;
class Embeds implements Bootable
{
use HookAnnotation;
/**
* Image size to use for featured images.
*
* @since 1.0.0
* @todo Type hint with PHP 8.3+ requirement.
*/
protected const IMAGE_SIZE = 'x3p0-wide';
/**
* Image shape to use for featured images (`rectangular` or `square`).
*
* @since 1.0.0
* @todo Type hint with PHP 8.3+ requirement.
*/
protected const IMAGE_SHAPE = 'rectangular';
/**
* Maximum number of words in the excerpt.
*
* @since 1.0.0
* @todo Type hint with PHP 8.3+ requirement.
*/
protected const EXCERPT_LENGTH = 24;
/**
* Bootstraps the class' actions/filters.
*
* @since 1.0.0
*/
#[\Override]
public function boot(): void
{
$this->hookMethods();
}
/**
* Loads assets needed for the embed.
*
* @hook enqueue_embed_scripts
* @since 1.0.0
* @link https://developer.wordpress.org/reference/hooks/enqueue_embed_scripts/
*/
public function enqueueAssets(): void
{
$embed_styles = file_get_contents(get_parent_theme_file_path('public/css/embed.css'));
$style_asset = include get_parent_theme_file_path('public/css/embed.asset.php');
// Register empty stylesheet so that our inline styles can
// piggyback off of it. Use the core `wp-embed-template` style
// as a dependency so that our styles will load afterward. We
// cannot add our inline styles directly to it due to the way
// that its own inline styles are loaded (no way to add ours
// after it's been enqueued).
wp_register_style(
'x3p0-ideas-embed',
false,
[ 'wp-embed-template' ],
$style_asset['version']
);
// Add inline styles.
wp_add_inline_style('x3p0-ideas-embed', wp_get_global_stylesheet());
wp_add_inline_style('x3p0-ideas-embed', $embed_styles);
// Enqueue embed style.
wp_enqueue_style('x3p0-ideas-embed');
}
/**
* Replaces the default size with our custom image size.
*
* @hook embed_thumbnail_image_size
* @since 1.0.0
* @link https://developer.wordpress.org/reference/hooks/embed_thumbnail_image_size/
*/
public function filterImageSize(): string
{
return self::IMAGE_SIZE;
}
/**
* Ensures that the featured image shape is set to match our size. This
* also affects how the embed is laid out.
*
* @hook embed_thumbnail_image_shape
* @since 1.0.0
* @link https://developer.wordpress.org/reference/hooks/embed_thumbnail_image_shape/
*/
public function filterImageShape(): string
{
return self::IMAGE_SHAPE;
}
/**
* Adds a custom excerpt length.
*
* @hook excerpt_length
* @since 1.0.0
* @link https://developer.wordpress.org/reference/hooks/excerpt_length/
*/
public function filterExcerptLength(int $number): int
{
return is_embed() ? self::EXCERPT_LENGTH : $number;
}
/**
* Adds a custom excerpt more string.
*
* @hook excerpt_more last
* @since 1.0.0
* @link https://developer.wordpress.org/reference/hooks/excerpt_more/
*/
public function filterExcerptMore(string $more_string): string
{
return is_embed() ? ' …' : $more_string;
}
/**
* Overwrites the embed site title HTML if the site doesn't have a custom
* icon. We do this to replace the WordPress logo with an SVG version
* that is more customizable via CSS.
*
* @hook embed_site_title_html
* @since 1.0.0
* @link https://developer.wordpress.org/reference/hooks/embed_site_title_html/
*/
public function filterSiteTitleHtml(string $site_title): string
{
// Bail if the site has an icon.
if (get_site_icon_url()) {
return $site_title;
}
return sprintf(
'<div class="wp-embed-site-title">
<a href="%s" target="_top">%s<span>%s</span></a>
</div>',
esc_url(home_url()),
// phpcs:ignore WordPress.WP.CapitalPDangit
block_core_social_link_get_icon('wordpress'),
esc_html(get_bloginfo('name'))
);
}
}