| 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 hooks to delete cache for all languages |
| 23 |
add_action('wpo_single_post_cache_deleted', array($this, 'polylang_delete_post_cache_for_all_languages')); |
| 24 |
add_action('wpo_single_post_feed_cache_deleted', array($this, 'polylang_delete_post_feed_cache_for_all_languages')); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns singleton instance. |
| 29 |
* |
| 30 |
* @return WPO_Polylang_Compatibility |
| 31 |
*/ |
| 32 |
public static function instance() { |
| 33 |
if (null === self::$instance) { |
| 34 |
self::$instance = new self(); |
| 35 |
} |
| 36 |
|
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Deletes cache files for all connected languages for a post. |
| 42 |
* |
| 43 |
* @param int $deleted_post_id Post id whose cache file is already deleted. |
| 44 |
*/ |
| 45 |
public function polylang_delete_post_cache_for_all_languages($deleted_post_id) { |
| 46 |
|
| 47 |
// Check if polylang translation function is available |
| 48 |
if (!function_exists('pll_get_post_translations')) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$translated_post_ids = pll_get_post_translations($deleted_post_id); |
| 53 |
|
| 54 |
// Delete cache for each translated post |
| 55 |
foreach ($translated_post_ids as $post_id) { |
| 56 |
if ($deleted_post_id !== $post_id) { |
| 57 |
WPO_Page_Cache::really_delete_single_post_cache($post_id); |
| 58 |
} |
| 59 |
} |
| 60 |
if (!empty($translated_post_ids)) WPO_Page_Cache::instance()->file_log("Cache for associated Polylang translation posts for Title: {{title}} have been purged", $deleted_post_id); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Deletes cache files for all connected languages for a post feed. |
| 65 |
* |
| 66 |
* @param int $deleted_post_id Post id whose cache file for feed is already deleted. |
| 67 |
*/ |
| 68 |
public function polylang_delete_post_feed_cache_for_all_languages($deleted_post_id) { |
| 69 |
|
| 70 |
// Check if polylang translation function is available |
| 71 |
if (!function_exists('pll_get_post_translations')) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$translated_post_ids = pll_get_post_translations($deleted_post_id); |
| 76 |
$is_cache_purged = false; |
| 77 |
|
| 78 |
// Delete cache for each translated post |
| 79 |
foreach ($translated_post_ids as $post_id) { |
| 80 |
if ($deleted_post_id !== $post_id) { |
| 81 |
WPO_Page_Cache::really_delete_post_feed_cache($post_id); |
| 82 |
$is_cache_purged = true; |
| 83 |
} |
| 84 |
} |
| 85 |
if ($is_cache_purged) WPO_Page_Cache::instance()->file_log("Cache for associated Polylang translation posts' feeds for Title: {{title}} have been purged", $deleted_post_id); |
| 86 |
} |
| 87 |
} |
| 88 |
|