| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register the plugin oEmbed provider. |
| 4 |
* |
| 5 |
* @package civist |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Register the plugin oEmbed provider. |
| 10 |
*/ |
| 11 |
class Civist_OEmbed { |
| 12 |
/** |
| 13 |
* The plugin file. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private $plugin_file; |
| 18 |
/** |
| 19 |
* The plugin slug. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $plugin_slug; |
| 24 |
/** |
| 25 |
* The plugin settings manager. |
| 26 |
* |
| 27 |
* @var Civist_Settings_Manager |
| 28 |
*/ |
| 29 |
private $settings_manager; |
| 30 |
|
| 31 |
/** |
| 32 |
* The Civist_OEmbed class constructor. |
| 33 |
* |
| 34 |
* @param string $plugin_file The file of the plugin. |
| 35 |
* @param string $plugin_slug The slug of the plugin. |
| 36 |
* @param Civist_Settings_Manager $settings_manager The plugin settings manager. |
| 37 |
*/ |
| 38 |
public function __construct( $plugin_file, $plugin_slug, Civist_Settings_Manager $settings_manager ) { |
| 39 |
$this->plugin_slug = $plugin_slug; |
| 40 |
$this->settings_manager = $settings_manager; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Handle oEmbed provider registration. |
| 45 |
*/ |
| 46 |
public function oembed_provider() { |
| 47 |
$widget_url = $this->settings_manager->get_option_by_name( 'widget_url' ); |
| 48 |
$oembed_url = $this->settings_manager->get_option_by_name( 'oembed_url' ); |
| 49 |
$pattern = $widget_url . '*'; |
| 50 |
wp_oembed_add_provider( $pattern, $oembed_url, false ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Allow localhost as origin for the oEmbed provider. |
| 55 |
* |
| 56 |
* @param bool $is_external Is request host external. |
| 57 |
* @param string $host The host. |
| 58 |
* @param string $url The request url. |
| 59 |
*/ |
| 60 |
public function allow_localhost( $is_external, $host, $url ) { |
| 61 |
if ( ! $is_external ) { |
| 62 |
$oembed_url = $this->settings_manager->get_option_by_name( 'oembed_url' ); |
| 63 |
$is_external = ( substr( $url, 0, strlen( $oembed_url ) ) === $oembed_url ); |
| 64 |
} |
| 65 |
return $is_external; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Wrap the widget with a div containing a custom class so that it can be used in the script |
| 70 |
* that communicates with the embedded resource to select the iframe element inside a post. |
| 71 |
* |
| 72 |
* @param string $data The oembed response. |
| 73 |
* @param string $url The oembed request url. |
| 74 |
* @param string $args The oembed request args. |
| 75 |
* @param string $post_id The post id. |
| 76 |
*/ |
| 77 |
public function wrap_widget( $data, $url, $args, $post_id ) { |
| 78 |
$widget_url = $this->settings_manager->get_option_by_name( 'widget_url' ); |
| 79 |
if ( strpos( $url, $widget_url ) !== false ) { |
| 80 |
$permalink = get_permalink( $post_id ); |
| 81 |
$html_safe_permalink = esc_html( $permalink ); |
| 82 |
$wrapper_class = $this->wrap_widget_html_class( $post_id ); |
| 83 |
$mode = esc_js( wp_parse_args( $args, array( 'mode' => 'default' ) )['mode'] ); |
| 84 |
|
| 85 |
$content = "<div class=\"{$wrapper_class}\">$data</div>"; |
| 86 |
|
| 87 |
$content .= "<script type='text/javascript'>"; |
| 88 |
$content .= " var origin = '" . esc_js( $widget_url ) . "';"; |
| 89 |
$content .= ' origin = origin.slice(0, -1);'; |
| 90 |
$content .= ' var handleEmbedPostMessage = function(embedContainer) {'; |
| 91 |
$content .= " window.addEventListener('message', function(msg) {"; |
| 92 |
$content .= " if(msg.origin !== origin || msg.data !== 'ready') {"; |
| 93 |
$content .= ' return;'; |
| 94 |
$content .= ' }'; |
| 95 |
$content .= " embedContainer.querySelector('iframe').contentWindow.postMessage({"; |
| 96 |
$content .= " permalink:'" . esc_js( $permalink ) . "',"; |
| 97 |
$content .= ' referrer: window.location.href'; |
| 98 |
$content .= " },'" . esc_js( $widget_url ) . "')"; |
| 99 |
$content .= ' });'; |
| 100 |
$content .= ' };'; |
| 101 |
|
| 102 |
$content .= ' var handleEmbed = function(embedContainer) {'; |
| 103 |
$content .= " embedContainer.setAttribute('data-permalink', '{$html_safe_permalink}');"; |
| 104 |
$content .= " embedContainer.setAttribute('data-mode', '{$mode}');"; |
| 105 |
$content .= ' };'; |
| 106 |
|
| 107 |
$content .= " var embedsThemeId = document.querySelectorAll('#post-" . esc_js( $post_id ) . " [id^=civist]');"; |
| 108 |
$content .= " var embedsInternalClass = document.querySelectorAll('." . esc_js( $wrapper_class ) . " [id^=civist]');"; |
| 109 |
|
| 110 |
$content .= ' [].forEach.call(embedsThemeId, handleEmbedPostMessage);'; |
| 111 |
$content .= ' [].forEach.call(embedsInternalClass, handleEmbedPostMessage);'; |
| 112 |
$content .= ' [].forEach.call(embedsThemeId, handleEmbed);'; |
| 113 |
$content .= ' [].forEach.call(embedsInternalClass, handleEmbed);'; |
| 114 |
$content .= '</script>'; |
| 115 |
return $content; |
| 116 |
} |
| 117 |
return $data; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Clears the plugin's oembed cache. |
| 122 |
*/ |
| 123 |
public function clear_cache() { |
| 124 |
global $wpdb; |
| 125 |
// @codingStandardsIgnoreLine |
| 126 |
$wpdb->query( "DELETE $wpdb->postmeta |
| 127 |
FROM |
| 128 |
$wpdb->postmeta, |
| 129 |
( |
| 130 |
SELECT `meta_key` FROM $wpdb->postmeta |
| 131 |
WHERE |
| 132 |
`meta_key` LIKE '\_oembed\_%' |
| 133 |
AND `meta_key` NOT LIKE '\_oembed\_time\_%' |
| 134 |
AND `meta_value` LIKE '%id=\"civist-%' |
| 135 |
) pm |
| 136 |
WHERE $wpdb->postmeta.`meta_key` IN (`pm`.`meta_key`, CONCAT('_oembed_time_', SUBSTRING(`pm`.`meta_key` FROM 9))) |
| 137 |
" |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Build the class used in the widget wrapper. |
| 143 |
* |
| 144 |
* @param int $id The post id. |
| 145 |
*/ |
| 146 |
private function wrap_widget_html_class( $id ) { |
| 147 |
return "{$this->plugin_slug}-oembed-wrapper-{$id}"; |
| 148 |
} |
| 149 |
} |
| 150 |
|