Completed
Push — core/custom-css-4.7 ( 7f4836...adaf63 )
by
unknown
09:28
created

Jetpack_Sync_Module_Posts::add_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
	private $just_published;
8
9
	public function name() {
10
		return 'posts';
11
	}
12
13
	public function get_object_by_id( $object_type, $id ) {
14
		if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) {
15
			return $this->filter_post_content_and_add_links( $post );
16
		}
17
18
		return false;
19
	}
20
21
	public function set_defaults() {
22
	}
23
24
	public function init_listeners( $callable ) {
25
		add_action( 'wp_insert_post', $callable, 10, 3 );
26
		add_action( 'wp_insert_post', array( $this, 'send_published'), 11, 3 );
27
		add_action( 'deleted_post', $callable, 10 );
28
		add_action( 'jetpack_publicize_post', $callable );
29
		add_action( 'jetpack_published_post', $callable, 10, 2 );
30
		add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
31
		add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) );
32
	}
33
34
	public function init_full_sync_listeners( $callable ) {
35
		add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta
36
	}
37
38
	public function init_before_send() {
39
		add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) );
40
41
		// full sync
42
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
43
	}
44
45
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
46
		global $wpdb;
47
48
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
49
	}
50
51
	public function estimate_full_sync_actions( $config ) {
52
		global $wpdb;
53
54
		$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
55
		$count = $wpdb->get_var( $query );
56
57
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
58
	}
59
60 View Code Duplication
	private function get_where_sql( $config ) {
61
		$where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql();
62
63
		// config is a list of post IDs to sync
64
		if ( is_array( $config ) ) {
65
			$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...
66
		}
67
68
		return $where_sql;
69
	}
70
71
	function get_full_sync_actions() {
72
		return array( 'jetpack_full_sync_posts' );
73
	}
74
75
	/**
76
	 * Process content before send
77
	 */
78
79
	function expand_wp_insert_post( $args ) {
80
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
81
	}
82
83
	function filter_blacklisted_post_types( $args ) {
84
		$post = $args[1];
85
86
		if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
87
			return false;
88
		}
89
90
		return $args;
91
	}
92
93 View Code Duplication
	function remove_embed() {
94
		global $wp_embed;
95
		remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
96
		// remove the embed shortcode since we would do the part later.
97
		remove_shortcode( 'embed' );
98
		// Attempts to embed all URLs in a post
99
		remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
100
	}
101
102 View Code Duplication
	function add_embed() {
103
		global $wp_embed;
104
		add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
105
		// Shortcode placeholder for strip_shortcodes()
106
		add_shortcode( 'embed', '__return_false' );
107
		// Attempts to embed all URLs in a post
108
		add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
109
	}
110
111
	// Expands wp_insert_post to include filtered content
112
	function filter_post_content_and_add_links( $post_object ) {
113
		global $post;
114
		$post = $post_object;
115
116
		// return non existant post 
117
		$post_type = get_post_type_object( $post->post_type );
118 View Code Duplication
		if ( empty( $post_type) || ! is_object( $post_type ) ) {
119
			$non_existant_post                    = new stdClass();
120
			$non_existant_post->ID                = $post->ID;
121
			$non_existant_post->post_modified     = $post->post_modified;
122
			$non_existant_post->post_modified_gmt = $post->post_modified_gmt;
123
			$non_existant_post->post_status       = 'jetpack_sync_non_registered_post_type';
124
			
125
			return $non_existant_post;
126
		}
127
		/**
128
		 * Filters whether to prevent sending post data to .com
129
		 *
130
		 * Passing true to the filter will prevent the post data from being sent
131
		 * to the WordPress.com.
132
		 * Instead we pass data that will still enable us to do a checksum against the
133
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
134
		 * other services.
135
		 * @since 4.2.0
136
		 *
137
		 * @param boolean false prevent post data from being synced to WordPress.com
138
		 * @param mixed $post WP_POST object
139
		 */
140 View Code Duplication
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
141
			// We only send the bare necessary object to be able to create a checksum.
142
			$blocked_post                    = new stdClass();
143
			$blocked_post->ID                = $post->ID;
144
			$blocked_post->post_modified     = $post->post_modified;
145
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
146
			$blocked_post->post_status       = 'jetpack_sync_blocked';
147
148
			return $blocked_post;
149
		}
150
151
		// lets not do oembed just yet.
152
		$this->remove_embed();
153
154
		if ( 0 < strlen( $post->post_password ) ) {
155
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
156
		}
157
		
158
		/** This filter is already documented in core. wp-includes/post-template.php */
159
		if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public  ) {
160
161
			$post->post_content_filtered   = apply_filters( 'the_content', $post->post_content );
162
			$post->post_excerpt_filtered   = apply_filters( 'the_excerpt', $post->post_excerpt );
163
		}
164
165
		$this->add_embed();
166
167
		if ( has_post_thumbnail( $post->ID ) ) {
168
			$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
169
			if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
170
				$post->featured_image = $image_attributes[0];
171
			}
172
		}
173
174
		$post->permalink               = get_permalink( $post->ID );
175
		$post->shortlink               = wp_get_shortlink( $post->ID );
176
		$post->dont_email_post_to_subs = Jetpack::is_module_active( 'subscriptions' ) ?
177
				get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) :
178
				true; // Don't email subscription if the subscription module is not active.
179
180
		return $post;
181
	}
182
183
	public function save_published( $new_status, $old_status, $post ) {
184
		if ( 'publish' === $new_status && 'publish' !== $old_status ) {
185
			$this->just_published = $post->ID;
186
		}
187
	}
188
189
	public function send_published( $post_ID, $post, $update ) {
0 ignored issues
show
Unused Code introduced by
The parameter $update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
		if ( $this->just_published === $post->ID ) {
191
			$this->just_published = null;
192
			$flags = apply_filters( 'jetpack_published_post_flags', array(), $post );
193
			do_action( 'jetpack_published_post', $post_ID, $flags );
194
		}
195
	}
196
197
	public function expand_post_ids( $args ) {
198
		$post_ids = $args[0];
199
200
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
201
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
202
		$posts = array_values( $posts ); // reindex in case posts were deleted
203
204
		return array(
205
			$posts,
206
			$this->get_metadata( $post_ids, 'post' ),
207
			$this->get_term_relationships( $post_ids ),
208
		);
209
	}
210
}
211