PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / compatibility / class-wpo-polylang-compatibility.php

class-wpo-polylang-compatibility.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at compatibility/class-wpo-polylang-compatibility.php

93 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) {
3 die('No direct access allowed');
4 }
5
6 /**
7 * Adds compatibility for Polylang plugin.
8 */
9 class WPO_Polylang_Compatibility {
10
11 /**
12 * Instance of this class
13 *
14 * @var WPO_Polylang_Compatibility|null
15 */
16 protected static $instance = null;
17
18 /**
19 * Constructor.
20 */
21 private function __construct() {
22 add_action('wpo_single_post_cache_deleted', array($this, 'delete_post_cache_for_all_languages'));
23 add_action('wpo_single_post_feed_cache_deleted', array($this, 'delete_post_feed_cache_for_all_languages'));
24 }
25
26 /**
27 * Returns singleton instance.
28 *
29 * @return WPO_Polylang_Compatibility
30 */
31 public static function instance() {
32 if (null === self::$instance) {
33 self::$instance = new self();
34 }
35
36 return self::$instance;
37 }
38
39 /**
40 * Gets translated post IDs excluding the original post.
41 *
42 * @param int $post_id The original post ID.
43 * @return array<string, int> List of translated post IDs, excluding the original.
44 */
45 private function get_related_translations($post_id) {
46 if (!function_exists('pll_get_post_translations')) {
47 return array();
48 }
49
50 $translations = pll_get_post_translations($post_id);
51
52 return array_filter($translations, function ($id) use ($post_id) {
53 return $id !== $post_id;
54 });
55 }
56
57 /**
58 * Deletes cache files for all connected languages for a post.
59 *
60 * @param int $deleted_post_id Post id whose cache file is already deleted.
61 * @return void
62 */
63 public function delete_post_cache_for_all_languages($deleted_post_id) {
64 $related_ids = $this->get_related_translations($deleted_post_id);
65
66 foreach ($related_ids as $post_id) {
67 WPO_Page_Cache::really_delete_single_post_cache($post_id);
68 }
69
70 if (!empty($related_ids)) {
71 WPO_Page_Cache::instance()->file_log("Cache for associated Polylang translation posts for Title: {{title}} have been purged", $deleted_post_id);
72 }
73 }
74
75 /**
76 * Deletes cache files for all connected languages for a post feed.
77 *
78 * @param int $deleted_post_id Post id whose cache file for feed is already deleted.
79 * @return void
80 */
81 public function delete_post_feed_cache_for_all_languages($deleted_post_id) {
82 $related_ids = $this->get_related_translations($deleted_post_id);
83
84 foreach ($related_ids as $post_id) {
85 WPO_Page_Cache::really_delete_post_feed_cache($post_id);
86 }
87
88 if (!empty($related_ids)) {
89 WPO_Page_Cache::instance()->file_log("Cache for associated Polylang translation posts' feeds for Title: {{title}} have been purged", $deleted_post_id);
90 }
91 }
92 }
93