summaryrefslogtreecommitdiff
blob: bf716be3dd4952dd4df4e2589d77f8bd6711d221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
 * Attachments sync module.
 *
 * @package automattic/jetpack-sync
 */

namespace Automattic\Jetpack\Sync\Modules;

/**
 * Class to handle sync for attachments.
 */
class Attachments extends Module {
	/**
	 * Sync module name.
	 *
	 * @access public
	 *
	 * @return string
	 */
	public function name() {
		return 'attachments';
	}

	/**
	 * Initialize attachment action listeners.
	 *
	 * @access public
	 *
	 * @param callable $callable Action handler callable.
	 */
	public function init_listeners( $callable ) {
		add_action( 'add_attachment', array( $this, 'process_add' ) );
		add_action( 'attachment_updated', array( $this, 'process_update' ), 10, 3 );
		add_action( 'jetpack_sync_save_update_attachment', $callable, 10, 2 );
		add_action( 'jetpack_sync_save_add_attachment', $callable, 10, 2 );
		add_action( 'jetpack_sync_save_attach_attachment', $callable, 10, 2 );
	}

	/**
	 * Handle the creation of a new attachment.
	 *
	 * @access public
	 *
	 * @param int $attachment_id ID of the attachment.
	 */
	public function process_add( $attachment_id ) {
		$attachment = get_post( $attachment_id );
		/**
		 * Fires when the client needs to sync an new attachment
		 *
		 * @since 4.2.0
		 *
		 * @param int      Attachment ID.
		 * @param \WP_Post Attachment post object.
		 */
		do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment );
	}

	/**
	 * Handle updating an existing attachment.
	 *
	 * @access public
	 *
	 * @param int      $attachment_id     Attachment ID.
	 * @param \WP_Post $attachment_after  Attachment post object before the update.
	 * @param \WP_Post $attachment_before Attachment post object after the update.
	 */
	public function process_update( $attachment_id, $attachment_after, $attachment_before ) {
		// Check whether attachment was added to a post for the first time.
		if ( 0 === $attachment_before->post_parent && 0 !== $attachment_after->post_parent ) {
			/**
			 * Fires when an existing attachment is added to a post for the first time
			 *
			 * @since 6.6.0
			 *
			 * @param int      $attachment_id     Attachment ID.
			 * @param \WP_Post $attachment_after  Attachment post object after the update.
			 */
			do_action( 'jetpack_sync_save_attach_attachment', $attachment_id, $attachment_after );
		} else {
			/**
			 * Fires when the client needs to sync an updated attachment
			 *
			 * @since 4.9.0
			 *
			 * @param int      $attachment_id     Attachment ID.
			 * @param \WP_Post $attachment_after  Attachment post object after the update.
			 *
			 * Previously this action was synced using jetpack_sync_save_add_attachment action.
			 */
			do_action( 'jetpack_sync_save_update_attachment', $attachment_id, $attachment_after );
		}
	}
}