Completed
Push — update/whitelist-post-meta ( 5531f0...5cbb36 )
by
unknown
18:38 queued 09:09
created

Jetpack_Sync_Module_Posts::remove_embed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
4
5
class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module {
6
7
	public function name() {
8
		return 'posts';
9
	}
10
11
	public function get_object_by_id( $object_type, $id ) {
12
		if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) {
13
			return $this->filter_post_content_and_add_links( $post );
14
		}
15
16
		return false;
17
	}
18
19
	public function set_defaults() {
20
	}
21
22
	public function init_listeners( $callable ) {
23
		add_action( 'wp_insert_post', $callable, 10, 3 );
24
		add_action( 'deleted_post', $callable, 10 );
25
		add_action( 'jetpack_publicize_post', $callable );
26
		add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) );
27
	}
28
29
	public function init_full_sync_listeners( $callable ) {
30
		add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta
31
	}
32
33
	public function init_before_send() {
34
		add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) );
35
36
		// full sync
37
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
38
	}
39
40
	public function enqueue_full_sync_actions( $config ) {
41
		global $wpdb;
42
43
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ) );
44
	}
45
46
	public function estimate_full_sync_actions( $config ) {
47
		global $wpdb;
48
49
		$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
50
		$count = $wpdb->get_var( $query );
51
52
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
53
	}
54
55 View Code Duplication
	private function get_where_sql( $config ) {
56
		$where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql();
57
58
		// config is a list of post IDs to sync
59
		if ( is_array( $config ) ) {
60
			$where_sql   .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
61
		}
62
63
		return $where_sql;
64
	}
65
66
	function get_full_sync_actions() {
67
		return array( 'jetpack_full_sync_posts' );
68
	}
69
70
	/**
71
	 * Process content before send
72
	 */
73
74
	function expand_wp_insert_post( $args ) {
75
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
76
	}
77
78
	function filter_blacklisted_post_types( $args ) {
79
		$post = $args[1];
80
81
		if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
82
			return false;
83
		}
84
85
		return $args;
86
	}
87
88 View Code Duplication
	function remove_embed() {
89
		global $wp_embed;
90
		remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
91
		// remove the embed shortcode since we would do the part later.
92
		remove_shortcode( 'embed' );
93
		// Attempts to embed all URLs in a post
94
		remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
95
	}
96
97 View Code Duplication
	function add_embed() {
98
		global $wp_embed;
99
		add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
100
		// Shortcode placeholder for strip_shortcodes()
101
		add_shortcode( 'embed', '__return_false' );
102
		// Attempts to embed all URLs in a post
103
		add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
104
	}
105
106
	// Expands wp_insert_post to include filtered content
107
	function filter_post_content_and_add_links( $post_object ) {
108
		global $post;
109
		$post = $post_object;
110
		/**
111
		 * Filters whether to prevent sending post data to .com
112
		 *
113
		 * Passing true to the filter will prevent the post data from being sent
114
		 * to the WordPress.com.
115
		 * Instead we pass data that will still enable us to do a checksum against the
116
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
117
		 * other services.
118
		 * @since 4.2.0
119
		 *
120
		 * @param boolean false prevent post data from being synced to WordPress.com
121
		 * @param mixed $post WP_POST object
122
		 */
123
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
124
			// We only send the bare necessary object to be able to create a checksum.
125
			$blocked_post                    = new stdClass();
126
			$blocked_post->ID                = $post->ID;
127
			$blocked_post->post_modified     = $post->post_modified;
128
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
129
			$blocked_post->post_status       = 'jetpack_sync_blocked';
130
131
			return $blocked_post;
132
		}
133
134
		// lets not do oembed just yet.
135
		$this->remove_embed();
136
137
		if ( 0 < strlen( $post->post_password ) ) {
138
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
139
		}
140
		/** This filter is already documented in core. wp-includes/post-template.php */
141
		if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) ) {
142
			$post->post_content_filtered   = apply_filters( 'the_content', $post->post_content );
143
			$post->post_excerpt_filtered   = apply_filters( 'the_content', $post->post_excerpt );
144
		}
145
146
		$this->add_embed();
147
148
		if ( has_post_thumbnail( $post->ID ) ) {
149
			$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
150
			if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
151
				$post->featured_image = $image_attributes[0];
152
			}
153
		}
154
155
		$post->permalink               = get_permalink( $post->ID );
156
		$post->shortlink               = wp_get_shortlink( $post->ID );
157
		$post->dont_email_post_to_subs = Jetpack::is_module_active( 'subscriptions' ) ?
158
				get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) :
159
				true; // Don't email subscription if the subscription module is not active.
160
161
		return $post;
162
	}
163
164
	public function expand_post_ids( $args ) {
165
		$post_ids = $args[0];
166
167
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
168
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
169
170
		return array(
171
			$posts,
172
			$this->get_metadata( $post_ids, 'post' ),
173
			$this->get_term_relationships( $post_ids ),
174
		);
175
	}
176
}
177