forked from uoe-dlam/ed-embed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathed-embed.php
More file actions
120 lines (93 loc) · 3.03 KB
/
Copy pathed-embed.php
File metadata and controls
120 lines (93 loc) · 3.03 KB
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
<?php
/*
Plugin Name: UoE Media Hopper Embed
Description: Converts Media Hopper links into video embeds in the WordPress TinyMCE editor.
Author: DLAM Applications Development Team
Version: 1.0
*/
require_once 'OpenGraph.php';
require_once 'classes/class-ed-embed-init-cache.php';
require_once 'classes/class-ed-embed-video-cache.php';
function add_media_hopper_embed_handler() {
$regex = '#https://media.ed.ac.uk/media/*#i';
wp_embed_register_handler( 'mediahopper', $regex, 'media_hopper_embed_handler' );
}
add_action( 'init', 'add_media_hopper_embed_handler' );
function media_hopper_embed_handler( $matches, $attr, $url, $rawattr ) {
// function media_hopper_embed_handler( $url ) {
try {
$ed_embed_video_cache = new Ed_Embed_Video_Cache();
$embed = $ed_embed_video_cache->get( $url );
if ( ! empty( $embed ) ) {
return $embed;
}
// get open graph tags for url
$openGraph = OpenGraph::fetch( $url );
// get open graph video url if one exists on the page
$video_url = $openGraph->getVideoUrl();
if ( '' !== $video_url ) {
$embed = "<iframe src='$video_url' width='525' height='394' frameborder='0' allowfullscreen></iframe>";
}
$ed_embed_video_cache->save( $url, $embed );
return $embed;
} catch ( Exception $e ) {
return '';
}
}
/**
* We pretty much just hijack wp_trim_excerpt below
* We need to do this to strip out media hopper links
* because the excerpt will try and render the embed which is very slow if not
*
* @param string $text
* @return mixed|void
*/
function custom_wp_trim_excerpt( $text = '' ) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content( '' );
$link = '#https://example.com/[^\s]+#i'; //Please provide your data.
$text = preg_replace( $link, '', $text );
$text = strip_shortcodes( $text );
/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters( 'the_content', $text );
$text = str_replace( ']]>', ']]>', $text );
/**
* Filters the number of words in an excerpt.
*
* @since 2.7.0
*
* @param int $number The number of words. Default 55.
*/
$excerpt_length = apply_filters( 'excerpt_length', 55 );
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
*
* @since 2.9.0
*
* @param string $more_string The string shown within the more link.
*/
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
/**
* Filters the trimmed excerpt string.
*
* @since 2.8.0
*
* @param string $text The trimmed text.
* @param string $raw_excerpt The text prior to trimming.
*/
return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
}
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
add_filter( 'get_the_excerpt', 'custom_wp_trim_excerpt' );
register_activation_hook( __FILE__, 'embed_do_setup' );
/**
* Create cache when plugin is activated
*
* @return void
*/
function embed_do_setup() {
(new Ed_Embed_Init_Cache())->initialise_mediahopper_table();
}