Completed
Push — add/signature-error-reporting ( 95a087...072d13 )
by
unknown
18:31 queued 09:22
created

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
namespace Automattic\Jetpack\Sync\Modules;
4
5
class Attachments extends \Jetpack_Sync_Module {
6
	function name() {
7
		return 'attachments';
8
	}
9
10
	public function init_listeners( $callable ) {
11
		add_action( 'add_attachment', array( $this, 'process_add' ) );
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 process_add( $attachment_id ) {
19
		$attachment = get_post( $attachment_id );
20
		/**
21
		 * Fires when the client needs to sync an new attachment
22
		 *
23
		 * @since 4.2.0
24
		 *
25
		 * @param int The attachment ID
26
		 * @param object The attachment
27
		 */
28
		do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment );
29
	}
30
31
	function process_update( $attachment_id, $attachment_after, $attachment_before ) {
32
		// Check whether attachment was added to a post for the first time
33
		if ( 0 === $attachment_before->post_parent && 0 !== $attachment_after->post_parent ) {
34
			/**
35
			 * Fires when an existing attachment is added to a post for the first time
36
			 *
37
			 * @since 6.6.0
38
			 *
39
			 * @param int The attachment ID
40
			 * @param object The attachment
41
			 */
42
			do_action( 'jetpack_sync_save_attach_attachment', $attachment_id, $attachment_after );
43
		} else {
44
			/**
45
			 * Fires when the client needs to sync an updated attachment
46
			 *
47
			 * @since 4.9.0
48
			 *
49
			 * @param int The attachment ID
50
			 * @param object The attachment
51
			 *
52
			 * Previously this action was synced using jetpack_sync_save_add_attachment action.
53
			 */
54
			do_action( 'jetpack_sync_save_update_attachment', $attachment_id, $attachment_after );
55
		}
56
	}
57
}
58