Completed
Push — fix/subscriptions ( 9496b5...b6d983 )
by
unknown
135:30 queued 128:12
created

Jetpack_Sync_Module_Posts::do_not_send_post()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 2
dl 0
loc 16
rs 9.2
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, $max_items_to_enqueue, $state ) {
41
		global $wpdb;
42
43
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
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 ) ) . ')';
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
		// return non existant post 
112
		$post_type = get_post_type_object( $post->post_type );
113
		if ( empty( $post_type ) || ! is_object( $post_type ) ) {
114
			$non_existant_post                          = new stdClass();
115
			$non_existant_post->ID                      = $post->ID;
116
			$non_existant_post->post_modified           = $post->post_modified;
117
			$non_existant_post->post_modified_gmt       = $post->post_modified_gmt;
118
			$non_existant_post->post_status             = 'jetpack_sync_non_registered_post_type';
119
			$non_existant_post->dont_email_post_to_subs = true;
120
			$non_existant_post->post_type               = $post->post_type;
121
122
			return $non_existant_post;
123
		}
124
		/**
125
		 * Filters whether to prevent sending post data to .com
126
		 *
127
		 * Passing true to the filter will prevent the post data from being sent
128
		 * to the WordPress.com.
129
		 * Instead we pass data that will still enable us to do a checksum against the
130
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
131
		 * other services.
132
		 * @since 4.2.0
133
		 *
134
		 * @param boolean false prevent post data from being synced to WordPress.com
135
		 * @param mixed $post WP_POST object
136
		 */
137
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
138
			// We only send the bare necessary object to be able to create a checksum.
139
			$blocked_post                          = new stdClass();
140
			$blocked_post->ID                      = $post->ID;
141
			$blocked_post->post_modified           = $post->post_modified;
142
			$blocked_post->post_modified_gmt       = $post->post_modified_gmt;
143
			$blocked_post->post_status             = 'jetpack_sync_blocked';
144
			$blocked_post->dont_email_post_to_subs = true;
145
146
			return $blocked_post;
147
		}
148
149
		// lets not do oembed just yet.
150
		$this->remove_embed();
151
152
		if ( 0 < strlen( $post->post_password ) ) {
153
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
154
		}
155
156
		/** This filter is already documented in core. wp-includes/post-template.php */
157
		if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) {
158
159
			$post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
160
			$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt );
161
		}
162
163
		$this->add_embed();
164
165
		if ( has_post_thumbnail( $post->ID ) ) {
166
			$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
167
			if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
168
				$post->featured_image = $image_attributes[0];
169
			}
170
		}
171
172
		$post->permalink               = get_permalink( $post->ID );
173
		$post->shortlink               = wp_get_shortlink( $post->ID );
174
		$post->dont_email_post_to_subs = self::do_not_send_post( $post->ID, $post->post_status );
175
176
		return $post;
177
	}
178
179
	public function do_not_send_post( $post_id, $post_status ) {
180
		if ( 'publish' !== $post_status ) {
181
			return true;
182
		}
183
		$pending = get_post_meta( $post_id, '_jetpack_set_pending_email_post_to_subs', true );
184
185
		if ( $pending ) {
186
			delete_post_meta( $post_id, '_jetpack_set_pending_email_post_to_subs' );
187
188
			return Jetpack::is_module_active( 'subscriptions' ) ?
189
				false :
190
				true; // Don't email subscription if the subscription module is not active.
191
		}
192
193
		return true;
194
	}
195
196
	public function expand_post_ids( $args ) {
197
		$post_ids = $args[0];
198
199
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
200
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
201
		$posts = array_values( $posts ); // reindex in case posts were deleted
202
203
		return array(
204
			$posts,
205
			$this->get_metadata( $post_ids, 'post' ),
206
			$this->get_term_relationships( $post_ids ),
207
		);
208
	}
209
}
210