<?php
namespace NeoRename\NeoGlobal; if (!defined("ABSPATH")) { die("neoWP direct access not allowed"); }

function flush_all_third_party_caches() {
    // Manually tested on 2025-12-17 that all plugin cache flushing mechanisms work as intended.
    \NeoRename\NeoGlobal\global_log_with_module_name("_global-cache-flush", "Clear third party caches " . \NeoRename\NeoGlobal\plugin_slug() . "-" . \NeoRename\NeoGlobal\plugin_edition());

    try { if (function_exists('wp_cache_flush'))                                                                       { wp_cache_flush();                                } } catch (\Throwable $e) {}
    try { if (function_exists('w3tc_pgcache_flush'))                                                                   { w3tc_pgcache_flush();                            } } catch (\Throwable $e) {}
    try { if (function_exists('w3tc_flush_all'))                                                                       { w3tc_flush_all();                                } } catch (\Throwable $e) {}
    try { if (function_exists('wp_cache_clear_cache'))                                                                 { wp_cache_clear_cache();                          } } catch (\Throwable $e) {}
    try { if (function_exists('rocket_clean_domain'))                                                                  { rocket_clean_domain();                           } } catch (\Throwable $e) {}
    try { if (function_exists('wpfc_clear_all_cache'))                                                                 { wpfc_clear_all_cache(true);                      } } catch (\Throwable $e) {}
    try { if (class_exists('WpeCommon') && method_exists('WpeCommon', 'purge_memcached'))                              { \WpeCommon::purge_memcached();                   } } catch (\Throwable $e) {}
    try { if (class_exists('WpeCommon') && method_exists('WpeCommon', 'purge_varnish_cache'))                          { \WpeCommon::purge_varnish_cache();               } } catch (\Throwable $e) {}
    try { if (class_exists('WpeCommon') && method_exists('WpeCommon', 'clear_maxcdn_cache'))                           { \WpeCommon::clear_maxcdn_cache();                } } catch (\Throwable $e) {}
    try { if (true)                                                                                                    { do_action( 'litespeed_purge_all' );              } } catch (\Throwable $e) {} /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound */// According to the plugin author, this is the only supported way to clear the LiteSpeed cache.
    try { if (function_exists('sg_cachepress_purge_cache'))                                                            { sg_cachepress_purge_cache();                     } } catch (\Throwable $e) {}
    try { if (class_exists('comet_cache') && method_exists('comet_cache', 'clear'))                                    { \comet_cache::clear();                           } } catch (\Throwable $e) {}
    try { if (class_exists('comet_cache') && method_exists('comet_cache', 'clearUrl'))                                 { \comet_cache::clearUrl('**');                    } } catch (\Throwable $e) {}
    try { if (class_exists('WP_Optimize') && method_exists(\WP_Optimize(), 'get_page_cache') && method_exists(\WP_Optimize()->get_page_cache(), 'purge')) { \WP_Optimize()->get_page_cache()->purge(); }} catch (\Throwable $e) {}
    try { if (class_exists('Swift_Performance_Cache') && method_exists('Swift_Performance_Cache', 'clear_all_cache'))  { \Swift_Performance_Cache::clear_all_cache();     } } catch (\Throwable $e) {}
    try { if (class_exists('Breeze_Admin') && method_exists('Breeze_Admin', 'breeze_clear_all_cache'))                 { (new \Breeze_Admin())->breeze_clear_all_cache(); } } catch (\Throwable $e) {}
    try { if (class_exists('autoptimizeCache') && method_exists('autoptimizeCache', 'clearall'))                       { \autoptimizeCache::clearall();                   } } catch (\Throwable $e) {}
    try { if (class_exists('Hummingbird\\WP_Hummingbird') && method_exists('Hummingbird\\WP_Hummingbird', 'flush_cache')) { \Hummingbird\WP_Hummingbird::flush_cache();   } } catch (\Throwable $e) {}
    try { if (function_exists('sc_cache_flush'))                                                                       { sc_cache_flush();                                } } catch (\Throwable $e) {}
    try { if (class_exists('Cache_Enabler') && method_exists('Cache_Enabler', 'clear_complete_cache'))                 { \Cache_Enabler::clear_complete_cache();          } } catch (\Throwable $e) {}
    try { if (class_exists('Cache_Enabler') && method_exists('Cache_Enabler', 'clear_total_cache'))                    { \Cache_Enabler::clear_total_cache();             } } catch (\Throwable $e) {}
    try { if (class_exists('PagelyCachePurge') && method_exists('PagelyCachePurge', 'purgeAll'))                       { (new \PagelyCachePurge())->purgeAll();           } } catch (\Throwable $e) {}

    try { if (true) { foreach (\NeoRename\NeoGlobal\fs_glob(\NeoRename\NeoGlobal\uploads_dir() . "/bricks/css/*") ?: [] as $bricks_css_file) { if (\NeoRename\NeoGlobal\fs_is_file($bricks_css_file)) { \NeoRename\NeoGlobal\fs_unlink($bricks_css_file); } } do_action("bricks_regenerate_css_files"); } } catch (\Throwable $e) {} /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound */ // According to the plugin author, this is the only supported way to clear the Bricks cache.
    try { if (true) { global $wpdb; $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_bricks_query_api_%' OR option_name LIKE '_transient_timeout_bricks_query_api_%'"); } } catch (\Throwable $e) {} /* phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching */ // There is no other possibility of flushing the cache than directly querying the database.
    try { $elementor_clear_cache = function () { try { if (class_exists('\Elementor\Plugin') && method_exists('\Elementor\Plugin','instance')) { $e = \Elementor\Plugin::instance(); if (isset($e->files_manager) && method_exists($e->files_manager,'clear_cache')) { $e->files_manager->clear_cache(); } } } catch (\Throwable $e) {} }; if (did_action("shutdown")) { $elementor_clear_cache(); } else { \NeoRename\NeoGlobal\add_action_hook("shutdown", $elementor_clear_cache); } } catch (\Throwable $e) {}
    try { if (function_exists('et_core_page_resource_auto_clear')) { et_core_page_resource_auto_clear(); } else { do_action('et_core_page_resource_auto_clear'); } } catch (\Throwable $e) {} /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound */ // According to the plugin author, this is the only supported way to clear the Bricks cache.
    try { if (class_exists('FLBuilderModel') && method_exists('FLBuilderModel','delete_asset_cache_for_all_posts')) { \FLBuilderModel::delete_asset_cache_for_all_posts(); } } catch (\Throwable $e) {}
    try { if (class_exists('FLBuilderModel') && method_exists('FLBuilderModel', 'delete_asset_cache')) { \FLBuilderModel::delete_asset_cache(); } } catch (\Throwable $e) {}
    try { if (class_exists('Brizy_Editor_Post') && method_exists('Brizy_Editor_Post','markAllForCompilation')) { \Brizy_Editor_Post::markAllForCompilation(); } } catch (\Throwable $e) {}
    try { if (function_exists('\Breakdance\Render\clearAllCssCachesAndDeleteCachedFiles')) { \Breakdance\Render\clearAllCssCachesAndDeleteCachedFiles(); } } catch (\Throwable $e) {}
    try { if (function_exists('oxygen_vsb_cache_universal_css')) { oxygen_vsb_cache_universal_css(); } } catch (\Throwable $e) {}
    try { if (true) { global $wpdb; $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_site_transient_origin_wcss:%' OR option_name LIKE '_site_transient_timeout_origin_wcss:%' OR option_name LIKE '_site_transient_origin_widgets_css_cache:%' OR option_name LIKE '_site_transient_timeout_origin_widgets_css_cache:%'"); } } catch (\Throwable $e) {} /* phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching */ // There is no other possibility of flushing the cache than directly querying the database.
    try { if (true) { global $wpdb; $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_siteorigin_panels_layouts_directory_%' OR option_name LIKE '_transient_timeout_siteorigin_panels_layouts_directory_%'"); } } catch (\Throwable $e) {} /* phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching */ // There is no other possibility of flushing the cache than directly querying the database.
    try { if (true) { delete_option("wpb_js_compiled_js_composer_less"); } } catch (\Throwable $e) {}
    try { if (class_exists('Kadence_Blocks_Prebuilt_Library') && method_exists('Kadence_Blocks_Prebuilt_Library', 'get_instance') && method_exists(\Kadence_Blocks_Prebuilt_Library::get_instance(), 'delete_block_library_folder')) { \Kadence_Blocks_Prebuilt_Library::get_instance()->delete_block_library_folder(); } } catch (\Throwable $e) {}
}
