PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / trunk
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization vtrunk
4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 All 135 releases
← All changes | inc/compatibilities/wpml.php +44 -7 3.11.2 → trunk View file →
@@ -5,17 +5,15 @@
5 5 *
6 6 * @reason Wpml duplicates everything so we need to offload/update every image/page attachment.
7 7 */
8 8 class Optml_wpml extends Optml_compatibility {
9 -
10 -
11 9 /**
12 10 * Should we load the integration logic.
13 11 *
14 12 * @return bool Should we load.
15 13 */
16 - function should_load() {
17 - include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
14 + public function should_load() {
15 + include_once ABSPATH . 'wp-admin/includes/plugin.php';
18 16 return is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && Optml_Main::instance()->admin->settings->get( 'offload_media' ) === 'enabled';
19 17 }
20 18
21 19 /**
@@ -21,16 +19,17 @@
21 19 /**
22 20 * Register integration details.
23 21 */
24 22 public function register() {
25 - add_filter( 'optml_offload_duplicated_images', [ $this, 'wpml_get_duplicates' ], PHP_INT_MAX, 2 );
23 + add_filter( 'optml_offload_duplicated_images', [ $this, 'wpml_get_duplicates' ], PHP_INT_MAX, 2 );
24 + add_filter( 'optml_ensure_source_attachment_id', [ $this, 'get_source_attachment' ], PHP_INT_MAX );
26 25 }
27 26
28 27 /**
29 28 * Ads the duplicated pages/images when offloading.
30 29 *
31 - * @param array $duplicated_attachments The duplicated attachments array.
32 - * @param string $attachment_id The attachment id that is first offloaded.
30 + * @param array $duplicated_attachments The duplicated attachments array.
31 + * @param string | int $attachment_id The attachment id that is first offloaded.
33 32 *
34 33 * @return array The images array with the specific bakery images.
35 34 */
36 35 public function wpml_get_duplicates( $duplicated_attachments, $attachment_id ) {
@@ -47,8 +46,46 @@
47 46 }
48 47 }
49 48 return $duplicated_attachments;
50 49 }
50 +
51 + /**
52 + * Get the source attachment ID for the given attachment ID that might be a duplicate.
53 + *
54 + * @param int $attachment_id the attachment ID.
55 + *
56 + * @return int
57 + */
58 + public function get_source_attachment( $attachment_id ) {
59 + if ( $this->is_offload_source( $attachment_id ) ) {
60 + return $attachment_id;
61 + }
62 +
63 + $duplicates = $this->wpml_get_duplicates( [], $attachment_id );
64 +
65 + foreach ( $duplicates as $duplicate_id ) {
66 + if ( $this->is_offload_source( $duplicate_id ) ) {
67 +
68 + return (int) $duplicate_id;
69 + }
70 + }
71 +
72 + return $attachment_id;
73 + }
74 +
75 + /**
76 + * Checks if the given attachment is the source of the initial offload.
77 + * On some instances, it seems that WPML meta is faulty.
78 + * We are sure that the main attachment that has been offloaded has the proper attachment_meta.
79 + *
80 + * @param int $id The attachment ID.
81 + *
82 + * @return bool
83 + */
84 + private function is_offload_source( $id ) {
85 + return ! empty( get_post_meta( (int) $id, Optml_Media_Offload::META_KEYS['offloaded'], true ) );
86 + }
87 +
51 88 /**
52 89 * Should we early load the compatibility?
53 90 *
54 91 * @return bool Whether to load the compatibility or not.