| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy\handler; |
| 3 |
|
| 4 |
use epiphyt\Embed_Privacy\data\Providers; |
| 5 |
use epiphyt\Embed_Privacy\data\Replacer; |
| 6 |
use epiphyt\Embed_Privacy\Embed_Privacy; |
| 7 |
use WP_Post; |
| 8 |
|
| 9 |
/** |
| 10 |
* Post handler. |
| 11 |
* |
| 12 |
* @author Epiphyt |
| 13 |
* @license GPL2 |
| 14 |
* @package epiphyt\Embed_Privacy |
| 15 |
* @since 1.10.0 |
| 16 |
*/ |
| 17 |
final class Post { |
| 18 |
/** |
| 19 |
* Initialize functionality. |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
\add_filter( 'acf_the_content', [ Replacer::class, 'replace_embeds' ] ); |
| 23 |
\add_filter( 'do_shortcode_tag', [ Replacer::class, 'replace_embeds' ], 10, 2 ); |
| 24 |
\add_filter( 'embed_oembed_html', [ Replacer::class, 'replace_oembed' ], 10, 3 ); |
| 25 |
\add_filter( 'the_content', [ Replacer::class, 'replace_embeds' ] ); |
| 26 |
\add_filter( 'wp_video_shortcode', [ Replacer::class, 'replace_video_shortcode' ], 10, 2 ); |
| 27 |
\register_activation_hook( \EPI_EMBED_PRIVACY_FILE, [ self::class, 'clear_embed_cache' ] ); |
| 28 |
\register_deactivation_hook( \EPI_EMBED_PRIVACY_FILE, [ self::class, 'clear_embed_cache' ] ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Embeds are cached in the postmeta database table and need to be removed |
| 33 |
* whenever the plugin will be enabled or disabled. |
| 34 |
*/ |
| 35 |
public static function clear_embed_cache() { |
| 36 |
global $wpdb; |
| 37 |
|
| 38 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 39 |
if ( \is_plugin_active_for_network( 'embed-privacy/embed-privacy.php' ) ) { |
| 40 |
// on networks we need to iterate through every site |
| 41 |
$sites = \get_sites( [ |
| 42 |
'fields' => 'ids', |
| 43 |
'number' => 99999, |
| 44 |
] ); |
| 45 |
|
| 46 |
foreach ( $sites as $blog_id ) { |
| 47 |
$wpdb->query( |
| 48 |
$wpdb->prepare( |
| 49 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 50 |
"DELETE FROM $wpdb->get_blog_prefix( $blog_id )postmeta |
| 51 |
WHERE meta_key LIKE %s", |
| 52 |
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 53 |
[ '%_oembed_%' ] |
| 54 |
) |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
else { |
| 59 |
$wpdb->query( |
| 60 |
$wpdb->prepare( |
| 61 |
"DELETE FROM $wpdb->postmeta |
| 62 |
WHERE meta_key LIKE %s", |
| 63 |
[ '%_oembed_%' ] |
| 64 |
) |
| 65 |
); |
| 66 |
} |
| 67 |
//phpcs:enable |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Check if a post contains an embed. |
| 72 |
* |
| 73 |
* @param \WP_Post|int|null $post A post object, post ID or null |
| 74 |
* @return bool True if a post contains an embed, false otherwise |
| 75 |
*/ |
| 76 |
public static function has_embed( $post = null ) { |
| 77 |
if ( $post === null ) { |
| 78 |
global $post; |
| 79 |
} |
| 80 |
|
| 81 |
if ( \is_numeric( $post ) ) { |
| 82 |
$post = \get_post( $post ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Allow overwriting the return value of has_embed(). |
| 87 |
* If set to anything other than null, this value will be returned. |
| 88 |
* |
| 89 |
* @since 1.3.0 |
| 90 |
* |
| 91 |
* @param null $has_embed The default value |
| 92 |
*/ |
| 93 |
$has_embed = \apply_filters( 'embed_privacy_has_embed', null ); |
| 94 |
|
| 95 |
if ( $has_embed !== null ) { |
| 96 |
return $has_embed; |
| 97 |
} |
| 98 |
|
| 99 |
if ( ! $post instanceof WP_Post ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
if ( Embed_Privacy::get_instance()->has_embed ) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
$embed_providers = Providers::get_instance()->get_list(); |
| 108 |
|
| 109 |
// check post content |
| 110 |
foreach ( $embed_providers as $provider ) { |
| 111 |
if ( $provider->is_matching( $post->post_content ) ) { |
| 112 |
return true; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|