' . esc_html__( 'Settings > Performance', 'performance-lab' ) . '' ); } $wp_kses_options = array( 'a' => array( 'href' => array(), ), ); ?> %s', esc_url( add_query_arg( 'page', PERFLAB_SCREEN, admin_url( 'options-general.php' ) ) ), esc_html__( 'Settings', 'performance-lab' ) ); return array_merge( array( 'settings' => $settings_link ), $links ); } /** * Dismisses notification pointer after verifying nonce. * * This function adds a nonce check before dismissing perflab-admin-pointer * It runs before the dismiss-wp-pointer AJAX action is performed. * * @since 2.3.0 */ function perflab_dismiss_wp_pointer_wrapper(): void { if ( isset( $_POST['pointer'] ) && 'perflab-admin-pointer' !== $_POST['pointer'] ) { // Another plugin's pointer, do nothing. return; } check_ajax_referer( 'dismiss_pointer' ); } add_action( 'wp_ajax_dismiss-wp-pointer', 'perflab_dismiss_wp_pointer_wrapper', 0 ); /** * Gets the path to a script or stylesheet. * * @since 3.7.0 * * @param string $src_path Source path. * @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path. * @return string URL to script or stylesheet. */ function perflab_get_asset_path( string $src_path, ?string $min_path = null ): string { if ( null === $min_path ) { // Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths. $min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path ); } $force_src = false; if ( WP_DEBUG && ! file_exists( trailingslashit( PERFLAB_PLUGIN_DIR_PATH ) . $min_path ) ) { $force_src = true; wp_trigger_error( __FUNCTION__, sprintf( /* translators: %s is the minified asset path */ __( 'Minified asset has not been built: %s', 'performance-lab' ), $min_path ), E_USER_WARNING ); } if ( SCRIPT_DEBUG || $force_src ) { return $src_path; } return $min_path; } /** * Callback function to handle admin scripts. * * @since 2.8.0 * @since 3.0.0 Renamed to perflab_enqueue_features_page_scripts(). */ function perflab_enqueue_features_page_scripts(): void { // These assets are needed for the "Learn more" popover. wp_enqueue_script( 'thickbox' ); wp_enqueue_style( 'thickbox' ); wp_enqueue_script( 'plugin-install' ); // Enqueue plugin activate AJAX script and localize script data. wp_enqueue_script( 'perflab-plugin-activate-ajax', plugins_url( perflab_get_asset_path( 'includes/admin/plugin-activate-ajax.js' ), PERFLAB_MAIN_FILE ), array( 'wp-i18n', 'wp-a11y', 'wp-api-fetch' ), PERFLAB_VERSION, true ); } /** * Sanitizes a plugin slug. * * @since 3.1.0 * * @param mixed $unsanitized_plugin_slug Unsanitized plugin slug. * @return string|null Validated and sanitized slug or else null. */ function perflab_sanitize_plugin_slug( $unsanitized_plugin_slug ): ?string { if ( in_array( $unsanitized_plugin_slug, perflab_get_standalone_plugins(), true ) ) { return $unsanitized_plugin_slug; } else { return null; } } /** * Callback for handling installation/activation of plugin. * * @since 3.0.0 */ function perflab_install_activate_plugin_callback(): void { check_admin_referer( 'perflab_install_activate_plugin' ); require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; if ( ! isset( $_GET['slug'] ) ) { wp_die( esc_html__( 'Missing required parameter.', 'performance-lab' ) ); } $plugin_slug = perflab_sanitize_plugin_slug( wp_unslash( $_GET['slug'] ) ); if ( null === $plugin_slug ) { wp_die( esc_html__( 'Invalid plugin.', 'performance-lab' ) ); } // Install and activate the plugin and its dependencies. $result = perflab_install_and_activate_plugin( $plugin_slug ); if ( $result instanceof WP_Error ) { wp_die( wp_kses_post( $result->get_error_message() ) ); } $url = add_query_arg( array( 'page' => PERFLAB_SCREEN, 'activate' => $plugin_slug, ), admin_url( 'options-general.php' ) ); if ( wp_safe_redirect( $url ) ) { exit; } } add_action( 'admin_action_perflab_install_activate_plugin', 'perflab_install_activate_plugin_callback' ); /** * Callback function to handle admin inline style. * * @since 3.0.0 */ function perflab_print_features_page_style(): void { ?> $constant_name ) { if ( ! in_array( $plugin_slug, $installed_plugin_slugs, true ) ) { $are_all_plugins_installed = false; break; } } if ( ! $are_all_plugins_installed ) { wp_admin_notice( esc_html__( 'Due to your site\'s configuration, you may not be able to activate the performance features, unless the underlying plugin is already installed. Please install the relevant plugins manually.', 'performance-lab' ), array( 'type' => 'warning', ) ); return; } } $activated_plugin_slug = null; if ( isset( $_GET['activate'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $activated_plugin_slug = perflab_sanitize_plugin_slug( wp_unslash( $_GET['activate'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } if ( null !== $activated_plugin_slug ) { $message = __( 'Feature activated.', 'performance-lab' ); $plugin_settings_url = perflab_get_plugin_settings_url( $activated_plugin_slug ); if ( null !== $plugin_settings_url ) { /* translators: %s is the settings URL */ $message .= ' ' . sprintf( __( 'Review settings.', 'performance-lab' ), esc_url( $plugin_settings_url ) ); } wp_admin_notice( wp_kses( $message, array( 'a' => array( 'href' => array(), ), ) ), array( 'type' => 'success', 'dismissible' => true, ) ); } } /** * Gets the URL to the plugin settings screen if one exists. * * @since 3.1.0 * * @param string $plugin_slug Plugin slug passed to generate the settings link. * @return string|null Either the plugin settings URL or null if not available. */ function perflab_get_plugin_settings_url( string $plugin_slug ): ?string { $plugin_file = null; foreach ( array_keys( get_plugins() ) as $file ) { if ( strtok( $file, '/' ) === $plugin_slug ) { $plugin_file = $file; break; } } if ( null === $plugin_file ) { return null; } /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ $plugin_links = apply_filters( "plugin_action_links_{$plugin_file}", array() ); if ( ! is_array( $plugin_links ) || ! array_key_exists( 'settings', $plugin_links ) ) { return null; } $p = new WP_HTML_Tag_Processor( $plugin_links['settings'] ); if ( ! $p->next_tag( array( 'tag_name' => 'A' ) ) ) { return null; } $href = $p->get_attribute( 'href' ); if ( is_string( $href ) && '' !== $href ) { return $href; } return null; } /** * Prints the Performance Lab install notice after each feature plugin's row meta. * * @since 3.2.0 * * @param string $plugin_file Plugin file. */ function perflab_print_row_meta_install_notice( string $plugin_file ): void { if ( ! in_array( strtok( $plugin_file, '/' ), perflab_get_standalone_plugins(), true ) ) { return; } $message = sprintf( /* translators: %s: link to Performance Lab settings screen */ __( 'This plugin is installed by Performance Lab.', 'performance-lab' ), esc_url( add_query_arg( 'page', PERFLAB_SCREEN, admin_url( 'options-general.php' ) ) ) ); printf( '

%1$s

', wp_kses( $message, array( 'a' => array( 'href' => array() ) ) ) ); } add_action( 'after_plugin_row_meta', 'perflab_print_row_meta_install_notice' );