| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* A class to manage the integration with WP Offload Media Lite. |
| 8 |
* Version tested: 2.1.1 |
| 9 |
* |
| 10 |
* @since 2.6 |
| 11 |
*/ |
| 12 |
class PLL_AS3CF { |
| 13 |
/** |
| 14 |
* Stores if a media is translated when it is deleted. |
| 15 |
* |
| 16 |
* @var bool[] |
| 17 |
*/ |
| 18 |
private $is_media_translated = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* Initializes filters and actions. |
| 22 |
* |
| 23 |
* @since 2.6 |
| 24 |
*/ |
| 25 |
public function init() { |
| 26 |
add_filter( 'pll_copy_post_metas', array( $this, 'copy_post_metas' ) ); |
| 27 |
add_action( 'delete_attachment', array( $this, 'check_translated_media' ), 5 ); // Before Polylang deletes the translations information. |
| 28 |
add_action( 'delete_attachment', array( $this, 'prevent_file_deletion' ), 15 ); // Between Polylang and WP Offload Media. |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Synchronizes post metas |
| 33 |
* |
| 34 |
* @since 2.6 |
| 35 |
* |
| 36 |
* @param array $metas List of custom fields names. |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
public function copy_post_metas( $metas ) { |
| 40 |
$metas[] = 'amazonS3_info'; |
| 41 |
$metas[] = 'as3cf_filesize_total'; |
| 42 |
return $metas; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Checks if the deleted attachment was translated and stores the information. |
| 47 |
* |
| 48 |
* @since 2.6 |
| 49 |
* |
| 50 |
* @param int $post_id Id of the attachment being deleted. |
| 51 |
*/ |
| 52 |
public function check_translated_media( $post_id ) { |
| 53 |
$this->is_media_translated[ $post_id ] = ( count( pll_get_post_translations( $post_id ) ) > 1 ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Deletes the WP Offload Media information from the attachment being deleted. |
| 58 |
* That way WP Offload Media won't delete the file stored in the cloud. |
| 59 |
* Done after Polylang has deleted the translations information, to avoid the synchronization of the deletion |
| 60 |
* and of course before WP Offload Media deletes the file, normally at priority 20. |
| 61 |
* |
| 62 |
* @since 2.6 |
| 63 |
* |
| 64 |
* @param int $post_id Id of the attachment being deleted. |
| 65 |
*/ |
| 66 |
public function prevent_file_deletion( $post_id ) { |
| 67 |
if ( ! empty( $this->is_media_translated[ $post_id ] ) ) { |
| 68 |
delete_post_meta( $post_id, 'amazonS3_info' ); |
| 69 |
delete_post_meta( $post_id, 'as3cf_filesize_total' ); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|