ACF.php
4 months ago
AdvancedMathCaptcha.php
1 year ago
AeliaCurrencySwitcher.php
11 months ago
BeaverBuilder.php
1 year ago
CF_Helper.php
5 months ago
CURCY_MultiCurrency.php
1 year ago
Cloudflare.php
5 months ago
CommonHelpers.php
1 year ago
CookieNotice.php
1 year ago
DownloadManager.php
1 year ago
Elementor.php
5 months ago
Ezoic.php
1 year ago
FusionBuilder.php
1 year ago
GeoTargetingWP.php
1 year ago
GravityForms.php
2 months ago
JetPackNP.php
1 year ago
MPG.php
11 months ago
NginxHelper.php
1 year ago
RC.php
11 months ago
RankMathNP.php
1 year ago
ShortPixel.php
1 year ago
SquirrlySEO.php
1 year ago
TheEventsCalendar.php
2 months ago
ThriveTheme.php
1 year ago
WCML.php
1 year ago
WPBakeryNP.php
1 year ago
WPCacheHelper.php
1 year ago
WPForms.php
1 year ago
WPML.php
1 year ago
WPRocket.php
1 year ago
WooCommerce.php
11 months ago
WoocommerceCacheHandler.php
1 year ago
YoastSEO.php
1 year ago
Elementor.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\Integration\Plugin; |
| 4 | |
| 5 | class Elementor { |
| 6 | |
| 7 | const STAGE = "late"; |
| 8 | |
| 9 | public static function isActive() { |
| 10 | $activePlugins = apply_filters('active_plugins', get_option('active_plugins')); |
| 11 | if (defined('ELEMENTOR_PRO_VERSION') || in_array( 'elementor-pro/elementor-pro.php', $activePlugins )) { |
| 12 | return true; |
| 13 | } |
| 14 | if (defined('ELEMENTOR_VERSION') || in_array( 'elementor/elementor.php', $activePlugins )) { |
| 15 | return true; |
| 16 | } |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | public function init($stage) { |
| 21 | if ( ! self::isActive() ) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | add_action( 'save_post', array($this, 'purge_cache_on_custom_code_snippet_update'), 10, 3 ); |
| 26 | add_action( 'elementor/document/after_save', array($this, 'purge_on_kit_update'), 10, 2 ); |
| 27 | |
| 28 | // Add AJAX handler for Elementor Tools page cache clearing |
| 29 | add_action( 'wp_ajax_nitropack_elementor_clear_cache', array($this, 'ajax_clear_cache') ); |
| 30 | } |
| 31 | |
| 32 | public function purge_cache_on_custom_code_snippet_update( $post_id, $post, $update ) { |
| 33 | |
| 34 | if ( 'elementor_snippet' !== $post->post_type || defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || 'auto-draft' === $post->post_status ) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if( strpos( wp_get_raw_referer(), 'post-new' ) > 0 ) { |
| 39 | |
| 40 | if ( empty( $_POST['code'] ) ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | /* If new snippet is added */ |
| 45 | nitropack_sdk_invalidate(NULL, NULL, 'Elementor Custom Code Snippet Added'); |
| 46 | } else { |
| 47 | |
| 48 | /* If old snippet is Updated */ |
| 49 | nitropack_sdk_invalidate(NULL, NULL, 'Elementor Custom Code Snippet Updated'); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Handle Global Settings Updates Only (Document of type Kit) |
| 55 | * Hook: 'elementor/document/after_save' |
| 56 | */ |
| 57 | public function purge_on_kit_update( $document, $data ) { |
| 58 | // Check if the document being saved is a "Kit" (Global Settings) |
| 59 | // We strictly check the class name or type to ensure it's not a normal page. |
| 60 | $is_kit = ( |
| 61 | $document instanceof \Elementor\Core\Kits\Documents\Kit || |
| 62 | $document->get_type() === 'kit' |
| 63 | ); |
| 64 | |
| 65 | if (! $is_kit) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $active_kit_id = \Elementor\Plugin::$instance->kits_manager->get_active_id(); |
| 70 | |
| 71 | if ( $document->get_id() != $active_kit_id ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if ( function_exists('nitropack_sdk_purge') && nitropack_sdk_purge( NULL, NULL, 'Light purge, because of Elementor Settings/CSS Update', \NitroPack\SDK\PurgeType::LIGHT_PURGE ) ) { |
| 76 | \NitroPack\WordPress\NitroPack::getInstance()->getLogger()->notice('Light purge, because of Elementor Settings/CSS Update'); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * AJAX handler for clearing NitroPack cache from Elementor Tools page |
| 82 | * Triggered when user clicks Clear Cache button on Elementor Tools admin page |
| 83 | * Hook: 'wp_ajax_nitropack_elementor_clear_cache' |
| 84 | */ |
| 85 | public function ajax_clear_cache() { |
| 86 | // Verify nonce for security |
| 87 | check_ajax_referer( 'nitropack_elementor_clear_cache', 'nonce' ); |
| 88 | |
| 89 | // Check user permissions (same as Elementor's manage_options requirement) |
| 90 | if ( ! current_user_can( 'manage_options' ) ) { |
| 91 | wp_send_json_error( array( 'message' => 'Permission denied' ) ); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | try { |
| 96 | // Execute NitroPack light purge |
| 97 | if ( function_exists('nitropack_sdk_purge') ) { |
| 98 | $result = nitropack_sdk_purge( |
| 99 | NULL, |
| 100 | NULL, |
| 101 | 'Light purge triggered from Elementor Tools Clear Cache', |
| 102 | \NitroPack\SDK\PurgeType::LIGHT_PURGE |
| 103 | ); |
| 104 | |
| 105 | if ( $result ) { |
| 106 | // Log success |
| 107 | \NitroPack\WordPress\NitroPack::getInstance() |
| 108 | ->getLogger() |
| 109 | ->notice('Light purge triggered from Elementor Tools Clear Cache'); |
| 110 | |
| 111 | wp_send_json_success( array( 'message' => 'NitroPack cache cleared successfully' ) ); |
| 112 | } else { |
| 113 | wp_send_json_error( array( 'message' => 'NitroPack cache clearing failed' ) ); |
| 114 | } |
| 115 | } else { |
| 116 | wp_send_json_error( array( 'message' => 'NitroPack SDK not available' ) ); |
| 117 | } |
| 118 | } catch ( \Exception $e ) { |
| 119 | // Log error |
| 120 | \NitroPack\WordPress\NitroPack::getInstance() |
| 121 | ->getLogger() |
| 122 | ->error('Error clearing NitroPack cache from Elementor Tools: ' . $e->getMessage()); |
| 123 | |
| 124 | wp_send_json_error( array( 'message' => 'Error clearing cache: ' . $e->getMessage() ) ); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 |