PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 4.2.6
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v4.2.6
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 3.12.1 All 134 releases
optimole-wp / inc / compatibilities / wpml.php

wpml.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 4.2.6, at inc/compatibilities/wpml.php

97 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class Optml_wpml
5 *
6 * @reason Wpml duplicates everything so we need to offload/update every image/page attachment.
7 */
8 class Optml_wpml extends Optml_compatibility {
9 /**
10 * Should we load the integration logic.
11 *
12 * @return bool Should we load.
13 */
14 public function should_load() {
15 include_once ABSPATH . 'wp-admin/includes/plugin.php';
16 return is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && Optml_Main::instance()->admin->settings->get( 'offload_media' ) === 'enabled';
17 }
18
19 /**
20 * Register integration details.
21 */
22 public function register() {
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 );
25 }
26
27 /**
28 * Ads the duplicated pages/images when offloading.
29 *
30 * @param array $duplicated_attachments The duplicated attachments array.
31 * @param string | int $attachment_id The attachment id that is first offloaded.
32 *
33 * @return array The images array with the specific bakery images.
34 */
35 public function wpml_get_duplicates( $duplicated_attachments, $attachment_id ) {
36
37 // Get the TRID (Translation ID) from element. REF: https://wpml.org/wpml-hook/wpml_element_trid/
38 $trid = apply_filters( 'wpml_element_trid', null, $attachment_id, 'post_attachment' );
39
40 // Get all translations (elements with the same TRID). REF: https://wpml.org/wpml-hook/wpml_get_element_translations/
41 $translations = apply_filters( 'wpml_get_element_translations', null, $trid, 'post_attachment' );
42
43 foreach ( $translations as $translation ) {
44 if ( isset( $translation->element_id ) ) {
45 $duplicated_attachments[] = $translation->element_id;
46 }
47 }
48 return $duplicated_attachments;
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
88 /**
89 * Should we early load the compatibility?
90 *
91 * @return bool Whether to load the compatibility or not.
92 */
93 public function should_load_early() {
94 return true;
95 }
96 }
97