← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/modules/class-attachments.php
+102
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,102 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Attachments sync module. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-sync | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\Sync\Modules; | |
| 9 | + | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit( 0 ); | |
| 12 | +} | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Class to handle sync for attachments. | |
| 16 | + */ | |
| 17 | +class Attachments extends Module { | |
| 18 | + /** | |
| 19 | + * Sync module name. | |
| 20 | + * | |
| 21 | + * @access public | |
| 22 | + * | |
| 23 | + * @return string | |
| 24 | + */ | |
| 25 | + public function name() { | |
| 26 | + return 'attachments'; | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Initialize attachment action listeners. | |
| 31 | + * | |
| 32 | + * @access public | |
| 33 | + * | |
| 34 | + * @param callable $callable Action handler callable. | |
| 35 | + */ | |
| 36 | + public function init_listeners( $callable ) { | |
| 37 | + add_action( 'add_attachment', array( $this, 'process_add' ) ); | |
| 38 | + add_action( 'attachment_updated', array( $this, 'process_update' ), 10, 3 ); | |
| 39 | + add_action( 'jetpack_sync_save_update_attachment', $callable, 10, 2 ); | |
| 40 | + add_action( 'jetpack_sync_save_add_attachment', $callable, 10, 2 ); | |
| 41 | + add_action( 'jetpack_sync_save_attach_attachment', $callable, 10, 2 ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Handle the creation of a new attachment. | |
| 46 | + * | |
| 47 | + * @access public | |
| 48 | + * | |
| 49 | + * @param int $attachment_id ID of the attachment. | |
| 50 | + */ | |
| 51 | + public function process_add( $attachment_id ) { | |
| 52 | + $attachment = get_post( $attachment_id ); | |
| 53 | + /** | |
| 54 | + * Fires when the client needs to sync an new attachment | |
| 55 | + * | |
| 56 | + * @since 1.6.3 | |
| 57 | + * @since-jetpack 4.2.0 | |
| 58 | + * | |
| 59 | + * @param int Attachment ID. | |
| 60 | + * @param \WP_Post Attachment post object. | |
| 61 | + */ | |
| 62 | + do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment ); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Handle updating an existing attachment. | |
| 67 | + * | |
| 68 | + * @access public | |
| 69 | + * | |
| 70 | + * @param int $attachment_id Attachment ID. | |
| 71 | + * @param \WP_Post $attachment_after Attachment post object before the update. | |
| 72 | + * @param \WP_Post $attachment_before Attachment post object after the update. | |
| 73 | + */ | |
| 74 | + public function process_update( $attachment_id, $attachment_after, $attachment_before ) { | |
| 75 | + // Check whether attachment was added to a post for the first time. | |
| 76 | + if ( 0 === $attachment_before->post_parent && 0 !== $attachment_after->post_parent ) { | |
| 77 | + /** | |
| 78 | + * Fires when an existing attachment is added to a post for the first time | |
| 79 | + * | |
| 80 | + * @since 1.6.3 | |
| 81 | + * @since-jetpack 6.6.0 | |
| 82 | + * | |
| 83 | + * @param int $attachment_id Attachment ID. | |
| 84 | + * @param \WP_Post $attachment_after Attachment post object after the update. | |
| 85 | + */ | |
| 86 | + do_action( 'jetpack_sync_save_attach_attachment', $attachment_id, $attachment_after ); | |
| 87 | + } else { | |
| 88 | + /** | |
| 89 | + * Fires when the client needs to sync an updated attachment | |
| 90 | + * | |
| 91 | + * @since 1.6.3 | |
| 92 | + * @since-jetpack 4.9.0 | |
| 93 | + * | |
| 94 | + * @param int $attachment_id Attachment ID. | |
| 95 | + * @param \WP_Post $attachment_after Attachment post object after the update. | |
| 96 | + * | |
| 97 | + * Previously this action was synced using jetpack_sync_save_add_attachment action. | |
| 98 | + */ | |
| 99 | + do_action( 'jetpack_sync_save_update_attachment', $attachment_id, $attachment_after ); | |
| 100 | + } | |
| 101 | + } | |
| 102 | +} | |