Completed
Push — add/changelog-66 ( 3dd88f...f645f6 )
by Jeremy
27:12 queued 17:45
created

Jetpack_Sync_Module_Attachments   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 7 1
A process_add() 0 12 1
A process_update() 0 26 3
1
<?php
2
3
class Jetpack_Sync_Module_Attachments extends Jetpack_Sync_Module {
4
	function name() {
5
		return 'attachments';
6
	}
7
8
	public function init_listeners( $callable ) {
9
		add_action( 'add_attachment', array( $this, 'process_add' ) );
10
		add_action( 'attachment_updated', array( $this, 'process_update'), 10, 3 );
11
		add_action( 'jetpack_sync_save_update_attachment', $callable, 10, 2 );
12
		add_action( 'jetpack_sync_save_add_attachment', $callable, 10, 2 );
13
		add_action( 'jetpack_sync_save_attach_attachment', $callable, 10, 2 );
14
	}
15
16
	function process_add( $attachment_id ) {
17
		$attachment = get_post( $attachment_id );
18
		/**
19
		 * Fires when the client needs to sync an new attachment
20
		 *
21
		 * @since 4.2.0
22
		 *
23
		 * @param int The attachment ID
24
		 * @param object The attachment
25
		 */
26
		do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment );
27
	}
28
29
	function process_update( $attachment_id, $attachment_after, $attachment_before ) {
30
		// Check whether attachment was added to a post for the first time
31
		if ( 0 === $attachment_before->post_parent && 0 !== $attachment_after->post_parent ) {
32
			/**
33
			 * Fires when an existing attachment is added to a post for the first time
34
			 *
35
			 * @since 6.6.0
36
			 *
37
			 * @param int The attachment ID
38
			 * @param object The attachment
39
			 */
40
			do_action( 'jetpack_sync_save_attach_attachment', $attachment_id, $attachment_after );
41
		} else {
42
			/**
43
			 * Fires when the client needs to sync an updated attachment
44
			 *
45
			 * @since 4.9.0
46
			 *
47
			 * @param int The attachment ID
48
			 * @param object The attachment
49
			 *
50
			 * Previously this action was synced using jetpack_sync_save_add_attachment action.
51
			 */
52
			do_action( 'jetpack_sync_save_update_attachment', $attachment_id, $attachment_after );
53
		}
54
	}
55
}
56