Skip to content

Commit 3031f38

Browse files
author
Yoast
committed
Updates to 14.8
1 parent 42e2684 commit 3031f38

File tree

288 files changed

+22537
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

288 files changed

+22537
-0
lines changed

assets/play-button.png

2.12 KB
Loading

changelog.md

Lines changed: 697 additions & 0 deletions
Large diffs are not rendered by default.

classes/class-wpseo-meta-video.php

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
<?php
2+
/**
3+
* Yoast SEO Video plugin file.
4+
*
5+
* @package Internals
6+
* @since 1.6.0
7+
* @version 1.6.0
8+
*/
9+
10+
// Avoid direct calls to this file.
11+
if ( ! class_exists( 'WPSEO_Video_Sitemap' ) ) {
12+
header( 'Status: 403 Forbidden' );
13+
header( 'HTTP/1.1 403 Forbidden' );
14+
exit();
15+
}
16+
17+
if ( ! class_exists( 'WPSEO_Meta_Video' ) ) {
18+
19+
/**
20+
* Add the VideoSEO metabox fields and associated validation to the WPSEO-meta management
21+
*
22+
* {@internal WPSEO_Meta is a class with only static methods, so we will not extend it with
23+
* a child class, we will only add to it by hooking in.}
24+
*
25+
* @phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- This actually extends / builds upon WPSEO_Meta, so it sorta makes sense.
26+
*/
27+
class WPSEO_Meta_Video {
28+
29+
/**
30+
* Meta box field definitions for the meta box form.
31+
*
32+
* @var array {
33+
* @type string $type Required. Field type. i.e. text / textarea / checkbox /
34+
* radio / select / multiselect / upload / snippetpreview etc.
35+
* @type string $title Required. Table row title.
36+
* @type string $default_value Recommended. Default value for the field.
37+
* IMPORTANT:
38+
* - if the field has options, the default has to be the
39+
* key of one of the options
40+
* - if the field is a text field, the default **has** to be
41+
* an empty string as otherwise the user can't save
42+
* an empty value/delete the meta value
43+
* - if the field is a checkbox, the only valid values
44+
* are 'on' or 'off'
45+
* @type array $options Semi-required. Options for use with (multi-)select and radio
46+
* fields, required if that's the field type.
47+
* Key = (string) value which will be saved to db.
48+
* Value = (string) text label for the option.
49+
* @type bool $autocomplete Optional. Whether auto complete is on for text fields.
50+
* Defaults to true.
51+
* @type string $class Optional. Classname(s) to add to the actual <input> tag.
52+
* @type string $description Optional. Description to show underneath the field.
53+
* @type string $expl Optional. Label for a checkbox.
54+
* @type string $help Optional. Help text to show on mouse over ? image.
55+
* @type int $rows Optional. Number of rows for a textarea, defaults to 3.
56+
* @type string $placeholder Optional. Currently not used in this class.
57+
* }
58+
*
59+
* {@internal
60+
* - Titles, help texts, description text and option labels are added via a translate_meta_boxes() method
61+
* in the relevant child classes (WPSEO_Metabox and WPSEO_Social_admin) as they are only needed there.
62+
* - Beware: even though the meta keys are divided into subsets, they still have to be uniquely named!}
63+
*/
64+
public static $meta_fields = [
65+
'video' => [
66+
'videositemap-disable' => [
67+
'type' => 'hidden',
68+
'title' => '', // Translation added later.
69+
'default_value' => 'off',
70+
'expl' => '', // Translation added later.
71+
],
72+
'videositemap-thumbnail' => [
73+
'type' => 'hidden',
74+
'title' => '', // Translation added later.
75+
'default_value' => '',
76+
'description' => '',
77+
'placeholder' => '', // Translation added later.
78+
],
79+
'videositemap-duration' => [
80+
'type' => 'hidden',
81+
'title' => '', // Translation added later.
82+
'default_value' => '0',
83+
'description' => '', // Translation added later.
84+
'options' => [
85+
'min_value' => '0',
86+
'max_value' => null,
87+
'step' => '1',
88+
],
89+
],
90+
91+
/*
92+
* {@internal Not used directly anywhere except for storage and retrieval of the meta box
93+
* form values. The real use is in the video_meta key which retrieves the value
94+
* of this from $_POST.
95+
* However removing this would cause the meta box field to go blank as the info
96+
* can no longer be retrieved, so leaving it as is for now.}
97+
*/
98+
'videositemap-tags' => [
99+
'type' => 'hidden',
100+
'title' => '', // Translation added later.
101+
'default_value' => '',
102+
'description' => '', // Translation added later.
103+
],
104+
105+
/*
106+
* {@internal Not used directly anywhere except for storage and retrieval of the meta box
107+
* form values. The real use is in the video_meta key which retrieves the value
108+
* of this from $_POST.
109+
* However removing this would cause the meta box field to go blank as the info
110+
* can no longer be retrieved, so leaving it as is for now.}
111+
*/
112+
'videositemap-rating' => [
113+
'type' => 'hidden',
114+
'title' => '', // Translation added later.
115+
'default_value' => 0,
116+
'description' => '', // Translation added later.
117+
'options' => [
118+
'min_value' => '0',
119+
'max_value' => '5',
120+
'step' => '0.1',
121+
],
122+
],
123+
'videositemap-not-family-friendly' => [
124+
'type' => 'hidden',
125+
'title' => '', // Translation added later.
126+
'default_value' => 'off',
127+
'expl' => '', // Translation added later.
128+
'description' => '', // Translation added later.
129+
],
130+
],
131+
132+
133+
/* Fields we should validate & save, but not show on any form */
134+
'non_form' => [
135+
'video_meta' => [
136+
'type' => null,
137+
'default_value' => 'none',
138+
'serialized' => true,
139+
],
140+
],
141+
];
142+
143+
/**
144+
* Hook into WPSEO_Meta
145+
*
146+
* @return void
147+
*/
148+
public static function init() {
149+
add_filter( 'add_extra_wpseo_meta_fields', [ __CLASS__, 'register_video_meta_fields' ] );
150+
add_filter( 'wpseo_metabox_entries_video', [ __CLASS__, 'adjust_video_meta_field_defs' ], 10, 2 );
151+
152+
add_filter( 'wpseo_sanitize_post_meta_' . WPSEO_Meta::$meta_prefix . 'videositemap-duration', [ __CLASS__, 'sanitize_duration' ], 10, 3 );
153+
add_filter( 'wpseo_sanitize_post_meta_' . WPSEO_Meta::$meta_prefix . 'videositemap-rating', [ __CLASS__, 'sanitize_rating' ], 10, 3 );
154+
add_filter( 'wpseo_sanitize_post_meta_' . WPSEO_Meta::$meta_prefix . 'videositemap-thumbnail', [ __CLASS__, 'sanitize_thumbnail_upload' ], 10, 3 );
155+
add_filter( 'wpseo_sanitize_post_meta_' . WPSEO_Meta::$meta_prefix . 'video_meta', [ __CLASS__, 'sanitize_video_meta' ], 10, 2 );
156+
}
157+
158+
/**
159+
* Add the video meta fields to the WPSEO_Meta::$meta_fields definitions
160+
*
161+
* @param array $fields Fields already in place (possibly from other add-on plugins).
162+
*
163+
* @return array
164+
*/
165+
public static function register_video_meta_fields( $fields ) {
166+
return WPSEO_Meta::array_merge_recursive_distinct( $fields, self::$meta_fields );
167+
}
168+
169+
/**
170+
* Prepare the video meta field definitions for display in the metabox
171+
*
172+
* @param string $field_defs Field definitions for the requested tab.
173+
* @param string $post_type Post type of the current post.
174+
*
175+
* @return array Array containing the meta box field definitions
176+
*/
177+
public static function adjust_video_meta_field_defs( $field_defs, $post_type ) {
178+
$post = ( ! empty( $GLOBALS['post'] ) ) ? $GLOBALS['post'] : null;
179+
180+
$field_defs['videositemap-disable']['expl'] = sprintf( $field_defs['videositemap-disable']['expl'], $post_type );
181+
182+
$video = [];
183+
if ( isset( $post->ID ) ) {
184+
$video = WPSEO_Meta::get_value( 'video_meta', $post->ID );
185+
}
186+
187+
if ( ( ! isset( $post->ID ) || WPSEO_Meta::get_value( 'videositemap-thumbnail', $post->ID ) === '' )
188+
&& ( isset( $video['thumbnail_loc'] ) && $video['thumbnail_loc'] !== '' )
189+
) {
190+
$field_defs['videositemap-thumbnail']['description'] = sprintf( $field_defs['videositemap-thumbnail']['description'], '<a target="_blank" href="' . esc_url( $video['thumbnail_loc'] ) . '">', '</a>' );
191+
}
192+
else {
193+
$field_defs['videositemap-thumbnail']['description'] = '';
194+
}
195+
196+
if ( isset( $video['duration'] ) ) {
197+
$field_defs['videositemap-duration']['default_value'] = $video['duration'];
198+
}
199+
200+
return $field_defs;
201+
}
202+
203+
/**
204+
* Sanitize the video thumbnail upload post meta
205+
*
206+
* @param mixed $clean Potentially pre-cleaned version of the new meta value.
207+
* @param mixed $meta_value The new value.
208+
* @param string $field_def The field definition for the current meta field.
209+
*
210+
* @return string Cleaned value
211+
*/
212+
public static function sanitize_thumbnail_upload( $clean, $meta_value, $field_def ) {
213+
// Validate as url.
214+
$clean = $field_def['default_value'];
215+
216+
$url = WPSEO_Video_Wrappers::yoast_wpseo_video_sanitize_url( $meta_value );
217+
218+
if ( $url !== '' ) {
219+
$clean = $url;
220+
}
221+
222+
return $clean;
223+
}
224+
225+
/**
226+
* Sanitize the video duration post meta
227+
*
228+
* @param mixed $clean Potentially pre-cleaned version of the new meta value.
229+
* @param mixed $meta_value The new value.
230+
* @param string $field_def The field definition for the current meta field.
231+
*
232+
* @return string Cleaned value
233+
*/
234+
public static function sanitize_duration( $clean, $meta_value, $field_def ) {
235+
$field_def = WPSEO_Meta::get_meta_field_defs( 'video' );
236+
$field_def = $field_def['videositemap-duration'];
237+
$clean = $field_def['default_value'];
238+
239+
$int = WPSEO_Video_Wrappers::yoast_wpseo_video_validate_int( $meta_value );
240+
241+
if ( $int !== false && $int > 0 ) {
242+
$clean = strval( $int );
243+
}
244+
245+
return $clean;
246+
}
247+
248+
/**
249+
* Sanitize the video rating post meta
250+
*
251+
* @param mixed $clean Potentially pre-cleaned version of the new meta value.
252+
* @param mixed $meta_value The new value.
253+
* @param string $field_def The field definition for the current meta field.
254+
*
255+
* @return string Cleaned value
256+
*/
257+
public static function sanitize_rating( $clean, $meta_value, $field_def ) {
258+
$clean = $field_def['default_value'];
259+
if ( is_numeric( $meta_value ) && ( $meta_value >= 0 && $meta_value <= 5 ) ) {
260+
$clean = $meta_value;
261+
}
262+
263+
return $clean;
264+
}
265+
266+
/**
267+
* Sanitize the video meta post meta - set in function, not from user input so no extra validation done
268+
*
269+
* @param mixed $clean Potentially pre-cleaned version of the new meta value.
270+
* @param mixed $meta_value The new value.
271+
*
272+
* @return string Cleaned value
273+
*/
274+
public static function sanitize_video_meta( $clean, $meta_value ) {
275+
if ( is_array( $meta_value ) && $meta_value !== [] ) {
276+
$clean = $meta_value;
277+
}
278+
279+
return $clean;
280+
}
281+
282+
/**
283+
* Upgrade routine to deal with the fall-out of issue #102
284+
*/
285+
public static function re_add_durations() {
286+
global $wpdb;
287+
288+
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Only runs on upgrade, only gets data.
289+
$query = $wpdb->prepare(
290+
"SELECT post_id, meta_value
291+
FROM {$wpdb->postmeta}
292+
WHERE meta_key = %s",
293+
WPSEO_Meta::$meta_prefix . 'video_meta'
294+
);
295+
$video_metas = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL -- Correctly prepared above.
296+
297+
$query = $wpdb->prepare(
298+
"SELECT post_id
299+
FROM {$wpdb->postmeta}
300+
WHERE meta_key = %s",
301+
WPSEO_Meta::$meta_prefix . 'videositemap-duration'
302+
);
303+
$known_durations = $wpdb->get_col( $query ); // phpcs:ignore WordPress.DB.PreparedSQL -- Correctly prepared above.
304+
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
305+
306+
if ( is_array( $video_metas ) && $video_metas !== [] ) {
307+
foreach ( $video_metas as $video ) {
308+
if ( $known_durations === [] || ! in_array( $video->post_id, $known_durations, true ) ) {
309+
$meta = maybe_unserialize( $video->meta_value );
310+
if ( isset( $meta['duration'] ) ) {
311+
WPSEO_Meta::set_value( 'videositemap-duration', $meta['duration'], $video->post_id );
312+
}
313+
unset( $meta );
314+
}
315+
}
316+
}
317+
unset( $query, $video_metas, $known_durations, $video );
318+
}
319+
}
320+
}

0 commit comments

Comments
 (0)