Completed
Push — fix/refactor-full-sync-status-... ( d41df7 )
by
unknown
55:42 queued 45:27
created

Jetpack_Sync_Module_Posts::set_defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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