Completed
Push — add/get-object-api-for-sync ( 3aa2c5 )
by
unknown
123:54 queued 113:39
created

Jetpack_Sync_Module_Posts::get_object_by_id()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 7
rs 9.4285
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
	// Expands wp_insert_post to include filtered content
89
	function filter_post_content_and_add_links( $post_object ) {
90
		global $post;
91
		$post = $post_object;
92
		/**
93
		 * Filters whether to prevent sending post data to .com
94
		 *
95
		 * Passing true to the filter will prevent the post data from being sent
96
		 * to the WordPress.com.
97
		 * Instead we pass data that will still enable us to do a checksum against the
98
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
99
		 * other services.
100
		 * @since 4.2.0
101
		 *
102
		 * @param boolean false prevent post data from being synced to WordPress.com
103
		 * @param mixed $post WP_POST object
104
		 */
105
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
106
			// We only send the bare necessary object to be able to create a checksum.
107
			$blocked_post                    = new stdClass();
108
			$blocked_post->ID                = $post->ID;
109
			$blocked_post->post_modified     = $post->post_modified;
110
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
111
			$blocked_post->post_status       = 'jetpack_sync_blocked';
112
113
			return $blocked_post;
114
		}
115
116
		if ( 0 < strlen( $post->post_password ) ) {
117
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
118
		}
119
		/** This filter is already documented in core. wp-includes/post-template.php */
120
		$post->post_content_filtered   = apply_filters( 'the_content', $post->post_content );
121
		$post->post_excerpt_filtered   = apply_filters( 'the_content', $post->post_excerpt );
122
		$post->permalink               = get_permalink( $post->ID );
123
		$post->shortlink               = wp_get_shortlink( $post->ID );
124
		$post->dont_email_post_to_subs = Jetpack::is_module_active( 'subscriptions' ) ?
125
				get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) :
126
				true; // Don't email subscription if the subscription module is not active.
127
128
		return $post;
129
	}
130
131
	public function expand_post_ids( $args ) {
132
		$post_ids = $args[0];
133
134
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
135
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
136
137
		return array(
138
			$posts,
139
			$this->get_metadata( $post_ids, 'post' ),
140
			$this->get_term_relationships( $post_ids ),
141
		);
142
	}
143
}
144