PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / integrations / wp-offload-media / as3cf.php

as3cf.php in Polylang trunk, at src/integrations/wp-offload-media/as3cf.php

73 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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