PluginProbe
Embed Privacy / 1.11.2
Embed Privacy v1.11.2
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / inc / handler / class-post.php

class-post.php in Embed Privacy 1.11.2, at inc/handler/class-post.php

120 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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( 'render_block', [ Replacer::class, 'replace_embeds' ] );
26 \add_filter( 'the_content', [ Replacer::class, 'replace_embeds' ] );
27 \add_filter( 'wp_video_shortcode', [ Replacer::class, 'replace_video_shortcode' ], 10, 2 );
28 \register_activation_hook( \EPI_EMBED_PRIVACY_FILE, [ self::class, 'clear_embed_cache' ] );
29 \register_deactivation_hook( \EPI_EMBED_PRIVACY_FILE, [ self::class, 'clear_embed_cache' ] );
30 }
31
32 /**
33 * Embeds are cached in the postmeta database table and need to be removed
34 * whenever the plugin will be enabled or disabled.
35 */
36 public static function clear_embed_cache() {
37 global $wpdb;
38
39 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
40 if ( \is_plugin_active_for_network( 'embed-privacy/embed-privacy.php' ) ) {
41 // on networks we need to iterate through every site
42 $sites = \get_sites( [
43 'fields' => 'ids',
44 'number' => 99999,
45 ] );
46
47 foreach ( $sites as $blog_id ) {
48 $wpdb->query(
49 $wpdb->prepare(
50 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
51 "DELETE FROM $wpdb->get_blog_prefix( $blog_id )postmeta
52 WHERE meta_key LIKE %s",
53 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
54 [ '%_oembed_%' ]
55 )
56 );
57 }
58 }
59 else {
60 $wpdb->query(
61 $wpdb->prepare(
62 "DELETE FROM $wpdb->postmeta
63 WHERE meta_key LIKE %s",
64 [ '%_oembed_%' ]
65 )
66 );
67 }
68 //phpcs:enable
69 }
70
71 /**
72 * Check if a post contains an embed.
73 *
74 * @param \WP_Post|int|null $post A post object, post ID or null
75 * @return bool True if a post contains an embed, false otherwise
76 */
77 public static function has_embed( $post = null ) {
78 if ( $post === null ) {
79 global $post;
80 }
81
82 if ( \is_numeric( $post ) ) {
83 $post = \get_post( $post ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
84 }
85
86 /**
87 * Allow overwriting the return value of has_embed().
88 * If set to anything other than null, this value will be returned.
89 *
90 * @since 1.3.0
91 *
92 * @param null $has_embed The default value
93 */
94 $has_embed = \apply_filters( 'embed_privacy_has_embed', null );
95
96 if ( $has_embed !== null ) {
97 return $has_embed;
98 }
99
100 if ( ! $post instanceof WP_Post ) {
101 return false;
102 }
103
104 if ( Embed_Privacy::get_instance()->has_embed ) {
105 return true;
106 }
107
108 $embed_providers = Providers::get_instance()->get_list();
109
110 // check post content
111 foreach ( $embed_providers as $provider ) {
112 if ( $provider->is_matching( $post->post_content ) ) {
113 return true;
114 }
115 }
116
117 return false;
118 }
119 }
120