kmp = $kmp; $this->kinsta_cache = $kmp->kinsta_cache; $this->view_role_or_capability = set_view_role_or_capability(); // Whitelabeling slugs, menus, etc. $this->whitelabel = whitelabel(); $this->admin_page = new AdminPage( $this->whitelabel ); $this->clear_values = array( 'all' => $this->whitelabel->getMenuKey( 'clear-all-cache' ), 'site' => $this->whitelabel->getMenuKey( 'clear-site-cache' ), 'cdn' => $this->whitelabel->getMenuKey( 'clear-cdn-cache' ), 'object' => $this->whitelabel->getMenuKey( 'clear-object-cache' ), ); // Only add the footer text and menu icon if whitelabel is not enabled. if ( ! is_whitelabel_enabled() ) { add_filter( 'admin_footer_text', array( $this, 'modify_admin_footer_text' ), 99 ); } // Enqueue assets. add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); // Admin Menu and Admin Toolbar. add_action( 'admin_menu', array( $this, 'admin_menu_item' ) ); add_action( 'admin_bar_menu', array( $this, 'admin_bar_item' ), 100 ); // Notice for successful cache clear. add_action( 'admin_notices', array( $this, 'cleared_cache_notice' ) ); // Ajax actions for cache exclusion path management. add_action( 'wp_ajax_kinsta_save_custom_path', array( $this, 'action_kinsta_save_custom_path' ) ); add_action( 'wp_ajax_kinsta_remove_custom_path', array( $this, 'action_kinsta_remove_custom_path' ) ); add_action( 'wp_ajax_kinsta_cache_save_settings', array( $this, 'action_kinsta_cache_save_settings' ) ); /** * Filter the page cache supported cache headers * $cache_headers contains List of client caching headers and their (optional) verification callbacks. */ add_filter( 'site_status_page_cache_supported_cache_headers', function ( $cache_headers ) { // Add new header to the existing list. $cache_headers['x-kinsta-cache'] = static function ( $header_value ) { return false !== strpos( strtolower( $header_value ), 'hit' ); }; return $cache_headers; } ); } /** * Load assets in the Kinsta plugin setting page. * * @return void */ public function assets() { if ( ! $this->admin_page->isPluginPage() ) { return; } wp_enqueue_style( $this->whitelabel->getMenuKey( 'shared' ), $this->shared_resource_url( 'admin/assets/css/common.css' ), array(), \Kinsta\KMP\VERSION ); } /** * Fix missing resources issues with mu plugin's static files * * It was handled by " plugin_dir_url( __FILE__ )" before we switched to this * * @author Laci * @version 1.2 * @since 2.0.1 resource issue fixing * * @param string $path optional param which is added to the end of the returned string. * @return string URL path of the kinsta-mu-plugins. */ public static function shared_resource_url( $path = '' ) { $base_url = defined( 'WPMU_PLUGIN_URL' ) ? (string) constant( 'WPMU_PLUGIN_URL' ) : ''; $mu_url = ( is_ssl() ) ? str_replace( 'http://', 'https://', $base_url ) : $base_url; $full_path = $mu_url . '/kinsta-mu-plugins/' . $path; if ( defined( 'KINSTAMU_CUSTOM_MUPLUGIN_URL' ) && KINSTAMU_CUSTOM_MUPLUGIN_URL !== '' ) { $full_path = KINSTAMU_CUSTOM_MUPLUGIN_URL . '/kinsta-mu-plugins/' . $path; } return $full_path; } /** * Modify Footer Text * Modifies the thank you text in the bottom of the admin * * @since 1.0.0 * @author Daniel Pataki */ public function modify_admin_footer_text() { // Translators: %1$s WordPress, %2$s Kinsta URL. return '' . sprintf( __( 'Thanks for creating with %1$s and hosting with %2$s', 'kinsta-mu-plugins' ), 'WordPress', 'Kinsta' ) . ''; } /** * Add main Kinsta Tools menu item. */ public function admin_menu_item() { /** * Filters whether or not Admin Menu item/page is visible. * * @param bool $hide True to hide the Admin Menu item/page, false to show. Default is false. */ if ( apply_filters( 'kinsta_admin_disabled', false ) ) { return; } $icon = $this->admin_page->getMenuIcon(); $title = $this->admin_page->getMenuTitle(); add_menu_page( $title, $title, $this->view_role_or_capability, $this->admin_page->getMenuKey( 'tools' ), array( $this, 'admin_cache_page' ), $icon, 3.19992919 ); if ( $this->kmp->is_cdn_enabled() ) { add_submenu_page( $this->admin_page->getMenuKey( 'tools' ), 'CDN', 'CDN', $this->view_role_or_capability, $this->admin_page->getMenuKey( 'cdn' ), array( $this, 'admin_cdn_page' ), 3.19992919 ); add_submenu_page( $this->admin_page->getMenuKey( 'tools' ), 'Settings', 'Settings', $this->view_role_or_capability, $this->admin_page->getMenuKey( 'settings' ), array( $this, 'admin_settings_page' ), 3.19992919 ); } } /** * Add Admin Toolbar menu. * * @param object $wp_admin_bar WP_Admin_Bar object. * * @return void */ public function admin_bar_item( $wp_admin_bar ) { /** * Filters whether or not Admin Menu item/page is visible. * * @param bool $hide True to hide the Admin Menu item/page, false to show. Default is false. */ if ( apply_filters( 'kinsta_admin_disabled', false ) || ! current_user_can( $this->view_role_or_capability ) ) { return; } load_muplugin_textdomain( 'kinsta-mu-plugins', dirname( plugin_basename( __FILE__ ) ) . '/assets/translations' ); $wp_admin_bar->add_node( array( 'id' => 'kinsta-cache', 'title' => __( 'Clear Caches', 'kinsta-mu-plugins' ), 'meta' => array( 'title' => __( 'Clear Caches', 'kinsta-mu-plugins' ), 'tabindex' => 0, ), 'parent' => 'top-secondary', ) ); $wp_admin_bar->add_node( array( 'id' => 'kinsta-cache-all', 'title' => __( 'Clear All Caches', 'kinsta-mu-plugins' ), 'href' => wp_nonce_url( add_query_arg( array( 'page' => $this->admin_page->getMenuKey( 'tools' ), 'clear-cache' => $this->clear_values['all'], ), admin_url( 'admin.php' ) ), 'kinsta-clear-cache-admin-bar' ), 'parent' => 'kinsta-cache', ) ); $wp_admin_bar->add_node( array( 'id' => 'kinsta-cache-full-page', 'title' => __( 'Clear Site Cache', 'kinsta-mu-plugins' ), 'href' => wp_nonce_url( add_query_arg( array( 'page' => $this->admin_page->getMenuKey( 'tools' ), 'clear-cache' => $this->clear_values['site'], ), admin_url( 'admin.php' ) ), 'kinsta-clear-cache-admin-bar' ), 'parent' => 'kinsta-cache', ) ); $wp_admin_bar->add_node( array( 'id' => 'kinsta-cache-cdn', 'title' => __( 'Clear CDN Cache', 'kinsta-mu-plugins' ), 'href' => wp_nonce_url( add_query_arg( array( 'page' => $this->admin_page->getMenuKey( 'tools' ), 'clear-cache' => $this->clear_values['cdn'], ), admin_url( 'admin.php' ) ), 'kinsta-clear-cache-admin-bar' ), 'parent' => 'kinsta-cache', ) ); $wp_admin_bar->add_node( array( 'id' => 'kinsta-cache-object', 'title' => __( 'Clear Object Cache', 'kinsta-mu-plugins' ), 'href' => wp_nonce_url( add_query_arg( array( 'page' => $this->admin_page->getMenuKey( 'tools' ), 'clear-cache' => $this->clear_values['object'], ), admin_url( 'admin.php' ) ), 'kinsta-clear-cache-admin-bar' ), 'parent' => 'kinsta-cache', ) ); } /** * Load the menu page. * * @return void */ public function admin_settings_page() { include plugin_dir_path( __FILE__ ) . 'pages/settings.php'; } /** * Load the menu page. * * @return void */ public function admin_cache_page() { include plugin_dir_path( __FILE__ ) . 'pages/cache.php'; } /** * Load the menu page. * * @return void */ public function admin_cdn_page() { include plugin_dir_path( __FILE__ ) . 'pages/cdn.php'; } /** * Notice that shows for successful cache clear. * Query var: kinsta-cache-cleared. * * @since 1.0.0 * * @return void */ public function cleared_cache_notice() { $cache_cleared_key = $this->whitelabel->getMenuKey( 'cache-cleared' ); $cache_cleared_values = array_values( $this->clear_values ); $key = sanitize_key( $_GET[ $cache_cleared_key ] ?? ( $_GET['kinsta-cache-cleared'] ?? '' ) ); if ( ! in_array( $key, $cache_cleared_values, true ) ) { return; } ?>

get_done_messages( $key ) ); ?>

update( array( 'path' => isset( $_POST['path'] ) ? sanitize_text_field( wp_unslash( $_POST['path'] ) ) : '', 'type' => isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '', ) ); die(); } /** * AJAX action to remove custom path. * * @return void */ public function action_kinsta_remove_custom_path() { check_ajax_referer( 'save_plugin_options', 'kinsta_nonce' ); if ( ! isset( $_POST['index'] ) || ( isset( $_POST['index'] ) && is_int( $_POST['index'] ) ) ) { return; } ( new CustomPaths() )->remove( absint( wp_unslash( $_POST['index'] ) ), ); die(); } /** * Action to save settings from the Kinsta Cache settings page. */ public function action_kinsta_cache_save_settings() { check_ajax_referer( 'kinsta_nonce', 'kinsta_nonce' ); $values = isset( $_POST['values'] ) && is_array( $_POST['values'] ) ? map_deep( wp_unslash( $_POST['values'] ), 'sanitize_text_field' ) : array(); /** * Handle the autopurge status. * * The value of `kinsta-autopurge-status` is derived from the checkbox * in the settings page. If the checkbox is checked, the value will * be 'on', otherwise it will be excluded in the submitted data, * so we can assume that the checkbox is unchecked. */ update_option( 'kinsta-autopurge-status', ! isset( $values['kinsta-autopurge-status'] ) ? 'disabled' : 'enabled' ); die(); } /** * Retrieve the message after the cache clearing has been initiated, * or when the settings have been saved. * * @param string $key The key to retrieve the message. * * @return string The role or capability. */ public static function get_done_messages( string $key ): string { $messages = array( 'all-cache' => __( 'All caches were cleared. Changes usually appear globally within a few minutes.', 'kinsta-mu-plugins' ), 'site-cache' => __( 'Site cache was cleared. Changes usually appear globally within a few minutes.', 'kinsta-mu-plugins' ), 'object-cache' => __( 'Object cache was cleared. Changes usually appear globally within a few minutes.', 'kinsta-mu-plugins' ), 'cdn-cache' => __( 'CDN cache was cleared. Changes usually appear globally within a few minutes.', 'kinsta-mu-plugins' ), 'kinsta-cache-save-settings' => __( 'Settings saved.', 'kinsta-mu-plugins' ), ); return $messages[ $key ] ?? __( 'Cache clearing has been initiated.', 'kinsta-mu-plugins' ); } }