Completed
Push — add/google-maps/api-key ( 5599f7...90239a )
by
unknown
26:57 queued 16:47
created

Jetpack_Sync_Module_Posts::set_defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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