| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 3 |
|
| 4 |
if (!class_exists('WPO_TranslatePress_Compatibility')) : |
| 5 |
/** |
| 6 |
* Adds compatibility for TranslatePress plugin. |
| 7 |
*/ |
| 8 |
class WPO_TranslatePress_Compatibility { |
| 9 |
|
| 10 |
private $trp_settings; |
| 11 |
|
| 12 |
/** |
| 13 |
* Constructor. |
| 14 |
*/ |
| 15 |
private function __construct() { |
| 16 |
// Bail out if TranslatePress is not active |
| 17 |
if (!class_exists('TRP_Translate_Press')) return; |
| 18 |
|
| 19 |
// Using the TranslatePress instance retrieve the settings |
| 20 |
$this->trp_settings = TRP_Translate_Press::get_trp_instance()->get_component('settings')->get_settings(); |
| 21 |
// Add action hooks to delete cache for all languages |
| 22 |
add_action('wpo_single_post_cache_deleted', array($this, 'translatepress_delete_post_cache_for_all_languages')); |
| 23 |
add_action('wpo_single_post_feed_cache_deleted', array($this, 'translatepress_delete_post_feed_cache_for_all_languages')); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Returns singleton instance. |
| 28 |
* |
| 29 |
* @return WPO_TranslatePress_Compatibility |
| 30 |
*/ |
| 31 |
public static function instance() { |
| 32 |
static $_instance; |
| 33 |
if (null === $_instance) { |
| 34 |
$_instance = new self(); |
| 35 |
} |
| 36 |
|
| 37 |
return $_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 translatepress_delete_post_cache_for_all_languages($deleted_post_id) { |
| 46 |
|
| 47 |
$deleted_post_url = get_permalink($deleted_post_id); |
| 48 |
$homepage_url = get_home_url(get_current_blog_id()); |
| 49 |
$post_path = str_replace($homepage_url, "", $deleted_post_url); |
| 50 |
|
| 51 |
// Retrieve the url slugs from TranslatePress setting options |
| 52 |
$url_slugs = $this->trp_settings['url-slugs']; |
| 53 |
foreach ($url_slugs as $value) { |
| 54 |
// Build url using the url-slugs and the path of the deleted post |
| 55 |
$possible_lang_url = $homepage_url. '/' . $value . $post_path; |
| 56 |
WPO_Page_Cache::delete_cache_by_url($possible_lang_url); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Deletes cache files for all connected languages for a post feed. |
| 62 |
* |
| 63 |
* @param int $deleted_post_id Post id whose cache file for feed is already deleted. |
| 64 |
*/ |
| 65 |
public function translatepress_delete_post_feed_cache_for_all_languages($deleted_post_id) { |
| 66 |
|
| 67 |
$deleted_post_url = get_permalink($deleted_post_id); |
| 68 |
$homepage_url = get_home_url(get_current_blog_id()); |
| 69 |
$post_path = str_replace($homepage_url, "", $deleted_post_url); |
| 70 |
|
| 71 |
// Retrieve the url slugs from TranslatePress setting options |
| 72 |
$url_slugs = $this->trp_settings['url-slugs']; |
| 73 |
foreach ($url_slugs as $value) { |
| 74 |
// Build url using the url-slugs and the path of the deleted post |
| 75 |
$possible_lang_url = $homepage_url.'/'. $value . untrailingslashit($post_path) . '/feed/'; |
| 76 |
WPO_Page_Cache::delete_cache_by_url($possible_lang_url); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
endif; |
| 81 |
|