Completed
Push — master-stable ( 2e27ce...519850 )
by Jeremy
12:36
created

Jetpack_Sync_Module_Posts::get_where_sql()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 9.4285
1
<?php
2
3
class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module {
4
5
	public function name() {
6
		return 'posts';
7
	}
8
9
	public function set_defaults() {
10
	}
11
12
	public function init_listeners( $callable ) {
13
		add_action( 'wp_insert_post', $callable, 10, 3 );
14
		add_action( 'deleted_post', $callable, 10 );
15
		add_action( 'jetpack_publicize_post', $callable );
16
	}
17
18
	public function init_full_sync_listeners( $callable ) {
19
		add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta
20
	}
21
22
	public function init_before_send() {
23
		add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) );
24
25
		// full sync
26
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
27
	}
28
29
	public function enqueue_full_sync_actions( $config ) {
30
		global $wpdb;
31
32
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ) );
33
	}
34
35
	public function estimate_full_sync_actions( $config ) {
36
		global $wpdb;
37
38
		$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
39
		$count = $wpdb->get_var( $query );
40
41
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
42
	}
43
44
	private function get_where_sql( $config ) {
45
		$where_sql = Jetpack_Sync_Defaults::get_blacklisted_post_types_sql();
46
47
		// config is a list of post IDs to sync
48
		if ( is_array( $config ) ) {
49
			$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...
50
		}
51
52
		return $where_sql;
53
	}
54
55
	function get_full_sync_actions() {
56
		return array( 'jetpack_full_sync_posts' );
57
	}
58
59
	/**
60
	 * Process content before send
61
	 */
62
63
	function expand_wp_insert_post( $args ) {
64
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
65
	}
66
67
	// Expands wp_insert_post to include filtered content
68
	function filter_post_content_and_add_links( $post_object ) {
69
		global $post;
70
		$post = $post_object;
71
		/**
72
		 * Filters whether to prevent sending post data to .com
73
		 *
74
		 * Passing true to the filter will prevent the post data from being sent
75
		 * to the WordPress.com.
76
		 * Instead we pass data that will still enable us to do a checksum against the
77
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
78
		 * other services.
79
		 * @since 4.2.0
80
		 *
81
		 * @param boolean false prevent post data from being synced to WordPress.com
82
		 * @param mixed $post WP_POST object
83
		 */
84
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
85
			// We only send the bare necessary object to be able to create a checksum.
86
			$blocked_post                    = new stdClass();
87
			$blocked_post->ID                = $post->ID;
88
			$blocked_post->post_modified     = $post->post_modified;
89
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
90
			$blocked_post->post_status       = 'jetpack_sync_blocked';
91
92
			return $blocked_post;
93
		}
94
95
		if ( 0 < strlen( $post->post_password ) ) {
96
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
97
		}
98
		/** This filter is already documented in core. wp-includes/post-template.php */
99
		$post->post_content_filtered   = apply_filters( 'the_content', $post->post_content );
100
		$post->post_excerpt_filtered   = apply_filters( 'the_content', $post->post_excerpt );
101
		$post->permalink               = get_permalink( $post->ID );
102
		$post->shortlink               = wp_get_shortlink( $post->ID );
103
		$post->dont_email_post_to_subs = Jetpack::is_module_active( 'subscriptions' ) ?
104
				get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) :
105
				true; // Don't email subscription if the subscription module is not active.
106
107
		return $post;
108
	}
109
110
	public function expand_post_ids( $args ) {
111
		$post_ids = $args[0];
112
113
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
114
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
115
116
		return array(
117
			$posts,
118
			$this->get_metadata( $post_ids, 'post' ),
119
			$this->get_term_relationships( $post_ids ),
120
		);
121
	}
122
}
123