Completed
Push — sync-attachment-attached ( 434d2d...937791 )
by
unknown
11:59 queued 05:06
created

Jetpack_Sync_Module_Attachments::process_update()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
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( 'edit_attachment', array( $this, 'send_attachment_info' ) );
10
		// Once we don't have to support 4.3 we can start using add_action( 'attachment_updated', $handler, 10, 3 ); instead
11
		add_action( 'add_attachment', array( $this, 'send_attachment_info' ) );
12
		add_action( 'attachment_updated', array( $this, 'process_update'), 10, 3 );
13
		add_action( 'jetpack_sync_save_update_attachment', $callable, 10, 2 );
14
		add_action( 'jetpack_sync_save_add_attachment', $callable, 10, 2 );
15
		add_action( 'jetpack_sync_save_attach_attachment', $callable, 10, 2 );
16
	}
17
18
	function send_attachment_info( $attachment_id ) {
19
		$attachment = get_post( $attachment_id );
20
		if ( 'add_attachment' === current_filter() ) {
21
			/**
22
			 * Fires when the client needs to sync an new attachment
23
			 *
24
			 * @since 4.2.0
25
			 *
26
			 * @param int The attachment ID
27
			 * @param object The attachment
28
			 */
29
			do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment );
30
		} else {
31
			/**
32
			 * Fires when the client needs to sync an updated attachment
33
			 *
34
			 * @since 4.9.0
35
			 *
36
			 * @param int The attachment ID
37
			 * @param object The attachment
38
			 *
39
			 * Previously this action was synced using jetpack_sync_save_add_attachment action.
40
			 */
41
			do_action( 'jetpack_sync_save_update_attachment', $attachment_id, $attachment );
42
		}
43
44
	}
45
46
	function process_update( $attachment_id, $attachment_after, $attachment_before ) {
47
		// Check whether attachment was added to a post for the first time
48
		if ( 0 === $attachment_before->post_parent && 0 !== $attachment_after->post_parent ) {
49
			do_action( 'jetpack_sync_save_attach_attachment', $attachment_id, $attachment_after );
50
		}
51
	}
52
}
53