save_configuration( $settings, $network_wide ); } /** * Deactivate the plugin * * Uninstall routines should be in uninstall.php * * @param bool $network_wide Whether network-wide configuration or not * * @return void */ function deactivate( $network_wide ) { Config::factory()->clean_up(); // cancel async jobs $cache_preloader = CachePreloader::factory(); $cache_preloader->cancel_process(); $db_optimizer = DatabaseOptimizer::factory(); $db_optimizer->cancel_process(); // cleanup transients delete_site_transient( DB_CLEANUP_COUNT_CACHE_KEY ); delete_transient( DB_CLEANUP_COUNT_CACHE_KEY ); } /** * The list of knows contexts for enqueuing scripts/styles. * * @return array */ function get_enqueue_contexts() { return [ 'admin', 'frontend', 'shared', 'classic-editor' ]; } /** * Generate an URL to a script, taking into account whether SCRIPT_DEBUG is enabled. * * @param string $script Script file name (no .js extension) * @param string $context Context for the script ('admin', 'frontend', or 'shared') * * @return string|WP_Error URL */ function script_url( $script, $context ) { if ( ! in_array( $context, get_enqueue_contexts(), true ) ) { return new WP_Error( 'invalid_enqueue_context', 'Invalid $context specified in PoweredCache script loader.' ); } return POWERED_CACHE_URL . "dist/js/${script}.js"; } /** * Generate an URL to a stylesheet, taking into account whether SCRIPT_DEBUG is enabled. * * @param string $stylesheet Stylesheet file name (no .css extension) * @param string $context Context for the script ('admin', 'frontend', or 'shared') * * @return string URL */ function style_url( $stylesheet, $context ) { if ( ! in_array( $context, get_enqueue_contexts(), true ) ) { return new WP_Error( 'invalid_enqueue_context', 'Invalid $context specified in PoweredCache stylesheet loader.' ); } return POWERED_CACHE_URL . "dist/css/${stylesheet}.css"; } /** * Enqueue scripts for admin. * * @param string $hook Current hook. * * @return void */ function admin_scripts( $hook ) { $classic_editor_hooks = [ 'post-new.php', 'post.php' ]; if ( in_array( $hook, $classic_editor_hooks, true ) ) { wp_enqueue_script( 'powered-cache-classic-editor', script_url( 'classic-editor', 'classic-editor' ), [ 'jquery', ], POWERED_CACHE_VERSION, true ); } if ( empty( $_GET['page'] ) || MENU_SLUG !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return; } wp_enqueue_script( 'powered-cache-admin', script_url( 'admin', 'admin' ), [ 'jquery', 'lodash', 'wp-i18n', ], POWERED_CACHE_VERSION, true ); wp_set_script_translations( 'powered-cache-admin', 'powered-cache', plugin_dir_path( POWERED_CACHE_PLUGIN_FILE ) . 'languages' ); } /** * Enqueue Block Editor assets * * @since 2.0 */ function block_editor_assets() { if ( ! current_user_can( 'edit_others_posts' ) ) { return; } /** * Min. WP 5.3 required for block editor plugin due to useSelect * Likely the older version of react didn't support hooks. * The post meta-box works with compat mode vice-versa... */ if ( version_compare( get_bloginfo( 'version' ), '5.3', '>=' ) ) { wp_register_script( 'powered-cache-editor', script_url( 'editor', 'admin' ), [ 'jquery', 'lodash', 'wp-i18n', 'wp-edit-post', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-plugins', ], POWERED_CACHE_VERSION, true ); wp_enqueue_script( 'powered-cache-editor' ); wp_set_script_translations( 'powered-cache-editor', 'powered-cache', plugin_dir_path( POWERED_CACHE_PLUGIN_FILE ) . 'languages' ); } } /** * Enqueue styles for admin. * * @return void */ function admin_styles() { // load on the powered cache page only if ( empty( $_GET['page'] ) || MENU_SLUG !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return; } wp_enqueue_style( 'powered-cache-admin', style_url( 'admin-style', 'admin' ), [], POWERED_CACHE_VERSION ); } /** * Add async/defer attributes to enqueued scripts that have the specified script_execution flag. * * @link https://core.trac.wordpress.org/ticket/12009 * * @param string $tag The script tag. * @param string $handle The script handle. * * @return string */ function script_loader_tag( $tag, $handle ) { $script_execution = wp_scripts()->get_data( $handle, 'script_execution' ); if ( ! $script_execution ) { return $tag; } if ( 'async' !== $script_execution && 'defer' !== $script_execution ) { return $tag; } // Abort adding async/defer for scripts that have this script as a dependency. _doing_it_wrong()? foreach ( wp_scripts()->registered as $script ) { if ( in_array( $handle, $script->deps, true ) ) { return $tag; } } // Add the attribute if it hasn't already been added. if ( ! preg_match( ":\s$script_execution(=|>|\s):", $tag ) ) { $tag = preg_replace( ':(?=>):', " $script_execution", $tag, 1 ); } return $tag; } /** * Invoke async classes */ function register_async_process() { DatabaseOptimizer::factory(); }