| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manage course embed |
| 4 |
* |
| 5 |
* @package Tutor |
| 6 |
* @author Themeum <support@themeum.com> |
| 7 |
* @link https://themeum.com |
| 8 |
* @since 2.1.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace TUTOR; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Course embed class |
| 19 |
* |
| 20 |
* @since 2.1.0 |
| 21 |
*/ |
| 22 |
class Course_Embed { |
| 23 |
|
| 24 |
/** |
| 25 |
* Register hooks |
| 26 |
* |
| 27 |
* @since 2.1.0 |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
add_filter( 'tutor_get_template_path', __CLASS__ . '::filter_template_path', 100, 2 ); |
| 32 |
add_filter( 'embed_oembed_html', __CLASS__ . '::oembed_iframe_overrides', 10, 3 ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Filter oembed data |
| 37 |
* |
| 38 |
* @since 2.1.0 |
| 39 |
* |
| 40 |
* @param string $html html content to filter. |
| 41 |
* @param string $url post embed url. |
| 42 |
* @param array $attr attrs. |
| 43 |
* |
| 44 |
* @return string customized html content |
| 45 |
*/ |
| 46 |
public static function oembed_iframe_overrides( $html, $url, $attr ) { |
| 47 |
|
| 48 |
$post_id = url_to_postid( $url ); |
| 49 |
if ( ! $post_id || tutor()->course_post_type !== get_post_type( $post_id ) ) { |
| 50 |
return $html; |
| 51 |
} |
| 52 |
|
| 53 |
if ( strpos( $html, '<iframe' ) !== false ) { |
| 54 |
$html = str_replace( |
| 55 |
'<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);"', |
| 56 |
'<iframe class="wp-embedded-content" sandbox="allow-forms allow-popups allow-scripts allow-same-origin allow-top-navigation" security="restricted" ', |
| 57 |
$html |
| 58 |
); |
| 59 |
|
| 60 |
$html = preg_replace( '/( height=".*")/i', ' height="620" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" ', $html ); |
| 61 |
|
| 62 |
$html = str_replace( |
| 63 |
'<blockquote class="wp-embedded-content"', |
| 64 |
'<blockquote class="wp-embedded-content" style="display:none" ', |
| 65 |
$html |
| 66 |
); |
| 67 |
return $html; |
| 68 |
} else { |
| 69 |
return $html; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Filter template |
| 75 |
* |
| 76 |
* @since 2.1.0 |
| 77 |
* |
| 78 |
* @param string $template_location default template location. |
| 79 |
* @param string $template template name. |
| 80 |
* |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public static function filter_template_path( $template_location, $template ) { |
| 84 |
$post_id = get_the_ID(); |
| 85 |
if ( get_post_type( $post_id ) === tutor()->course_post_type && function_exists( 'is_embed' ) && is_embed() ) { |
| 86 |
if ( 'single-course' === $template ) { |
| 87 |
$template_location = tutor()->path . 'templates/course-embed.php'; |
| 88 |
} |
| 89 |
} |
| 90 |
return $template_location; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Check if current course is embedded |
| 95 |
* |
| 96 |
* @since 2.1.0 |
| 97 |
* @return boolean |
| 98 |
*/ |
| 99 |
public static function is_embed_course() { |
| 100 |
$post_id = get_the_ID(); |
| 101 |
if ( get_post_type( $post_id ) === tutor()->course_post_type && function_exists( 'is_embed' ) && is_embed() ) { |
| 102 |
return true; |
| 103 |
} |
| 104 |
return false; |
| 105 |
} |
| 106 |
} |
| 107 |
|