if ( ! defined( 'ABSPATH' ) ) {
// phpcs:ignore Generic.Commenting.DocComment.MissingShort
/** @noinspection AutoloadingIssuesInspection */
* Register menu elements and do other global tasks.
class WPForms_Admin_Menu {
* Primary class constructor.
public function __construct() {
private function hooks(): void {
// Let's make some menus.
add_action( 'admin_menu', [ $this, 'register_menus' ], 9 );
add_action( 'admin_head', [ $this, 'hide_wpforms_submenu_items' ] );
add_action( 'admin_head', [ $this, 'adjust_pro_menu_item' ] );
add_action( 'admin_head', [ $this, 'admin_menu_styles' ], 11 );
// Plugins page settings link.
add_filter( 'plugin_action_links_' . plugin_basename( WPFORMS_PLUGIN_DIR . 'wpforms.php' ), [ $this, 'settings_link' ], 10, 4 );
add_action( 'activated_plugin', [ $this, 'activated_rotation_plugin' ], 10, 2 );
public function register_menus(): void {
$manage_cap = wpforms_get_capability_manage_options();
$access = wpforms()->obj( 'access' );
if ( ! $access || ! method_exists( $access, 'get_menu_cap' ) ) {
// Default Forms top level menu item.
esc_html__( 'WPForms', 'wpforms-lite' ),
esc_html__( 'WPForms', 'wpforms-lite' ),
$access->get_menu_cap( 'view_forms' ),
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
'data:image/svg+xml;base64,' . base64_encode( '<svg width="1792" height="1792" viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path fill="#9ea3a8" d="M643 911v128h-252v-128h252zm0-255v127h-252v-127h252zm758 511v128h-341v-128h341zm0-256v128h-672v-128h672zm0-255v127h-672v-127h672zm135 860v-1240q0-8-6-14t-14-6h-32l-378 256-210-171-210 171-378-256h-32q-8 0-14 6t-6 14v1240q0 8 6 14t14 6h1240q8 0 14-6t6-14zm-855-1110l185-150h-406zm430 0l221-150h-406zm553-130v1240q0 62-43 105t-105 43h-1240q-62 0-105-43t-43-105v-1240q0-62 43-105t105-43h1240q62 0 105 43t43 105z"/></svg>' ),
* Filters WPForms menu position.
* @param string|int|float $position Menu position.
apply_filters( 'wpforms_menu_position', '58.9' ) // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
// All Forms sub menu item.
esc_html__( 'WPForms', 'wpforms-lite' ),
esc_html__( 'All Forms', 'wpforms-lite' ),
$access->get_menu_cap( 'view_forms' ),
esc_html__( 'WPForms Builder', 'wpforms-lite' ),
esc_html__( 'Add New Form', 'wpforms-lite' ),
$access->get_menu_cap( [ 'create_forms', 'edit_forms' ] ),
// Entries sub menu item.
esc_html__( 'Form Entries', 'wpforms-lite' ),
esc_html__( 'Entries', 'wpforms-lite' ),
$access->get_menu_cap( 'view_entries' ),
// Payments sub menu item.
esc_html__( 'Payments', 'wpforms-lite' ),
esc_html__( 'Payments', 'wpforms-lite' ) . $this->get_new_badge_html(),
WPForms\Admin\Payments\Payments::SLUG,
do_action_deprecated( // phpcs:ignore WPForms.Comments.PHPDocHooks.RequiredHookDocumentation
'1.5.5 of the WPForms plugin',
* Fires after constructing the WPForms admin menu.
* @param WPForms_Admin_Menu $instance WPForms Admin Menu instance.
do_action( 'wpforms_admin_menu', $this );
// Templates sub menu item.
esc_html__( 'WPForms Templates', 'wpforms-lite' ),
esc_html__( 'Form Templates', 'wpforms-lite' ),
$access->get_menu_cap( 'edit_forms' ),
// Settings submenu item.
esc_html__( 'WPForms Settings', 'wpforms-lite' ),
esc_html__( 'Settings', 'wpforms-lite' ),
esc_html__( 'WPForms Tools', 'wpforms-lite' ),
esc_html__( 'Tools', 'wpforms-lite' ),
$access->get_menu_cap( [ 'create_forms', 'view_forms', 'view_entries' ] ),
// Hidden placeholder paged used for misc content.
esc_html__( 'WPForms', 'wpforms-lite' ),
esc_html__( 'Info', 'wpforms-lite' ),
$access->get_menu_cap( 'any' ),
esc_html__( 'WPForms Addons', 'wpforms-lite' ),
'<span style="color:#f18500">' . esc_html__( 'Addons', 'wpforms-lite' ) . '</span>',
$access->get_menu_cap( 'edit_forms' ),
$rotation = $this->get_rotating_submenu();
esc_html__( 'SMTP', 'wpforms-lite' ),
esc_html__( 'SMTP', 'wpforms-lite' ),
WPForms\Admin\Pages\SMTP::SLUG,
esc_html__( 'About WPForms', 'wpforms-lite' ),
esc_html__( 'About Us', 'wpforms-lite' ),
$access->get_menu_cap( 'any' ),
// Community submenu page.
esc_html__( 'Community', 'wpforms-lite' ),
esc_html__( 'Community', 'wpforms-lite' ),
WPForms\Admin\Pages\Community::SLUG,
if ( ! wpforms()->is_pro() ) {
esc_html__( 'Upgrade to Pro', 'wpforms-lite' ),
esc_html__( 'Upgrade to Pro', 'wpforms-lite' ),
wpforms_admin_upgrade_link( 'admin-menu' )
* Hide the "Add New" admin menu item if a user can't create forms.
public function hide_wpforms_submenu_items(): void {
if ( wpforms_current_user_can( 'create_forms' ) ) {
if ( ! isset( $submenu['wpforms-overview'] ) ) {
foreach ( $submenu['wpforms-overview'] as $key => $item ) {
if ( isset( $item[2] ) && $item[2] === 'wpforms-builder' ) {
unset( $submenu['wpforms-overview'][ $key ] );
$this->hide_wpforms_menu_item();
* Hide the "WPForms" admin menu if it has no submenu items.
public function hide_wpforms_menu_item(): void {
if ( ! empty( $submenu['wpforms-overview'] ) ) {
unset( $submenu['wpforms-overview'] );
foreach ( $menu as $key => $item ) {
if ( isset( $item[2] ) && $item[2] === 'wpforms-overview' ) {
* Make changes to the PRO menu item.
public function adjust_pro_menu_item(): void {
// Bail if a plugin menu is not registered.
if ( ! isset( $submenu['wpforms-overview'] ) ) {
$upgrade_link_position = key(
$submenu['wpforms-overview'],
static function ( $item ) {
return strpos( urldecode( $item[2] ), 'wpforms.com/lite-upgrade' ) !== false;
// Bail if "Upgrade to Pro" menu item is not registered.
if ( $upgrade_link_position === null ) {
// Add the PRO badge to the menu item.
// phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
if ( isset( $submenu['wpforms-overview'][ $upgrade_link_position ][4] ) ) {
$submenu['wpforms-overview'][ $upgrade_link_position ][4] .= ' wpforms-sidebar-upgrade-pro';
$submenu['wpforms-overview'][ $upgrade_link_position ][] = 'wpforms-sidebar-upgrade-pro';
$current_screen = get_current_screen();
$upgrade_utm_content = $current_screen === null ? 'Upgrade to Pro' : 'Upgrade to Pro - ' . $current_screen->base;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$upgrade_utm_content = empty( $_GET['view'] ) ? $upgrade_utm_content : $upgrade_utm_content . ': ' . sanitize_key( $_GET['view'] );
// Add utm_content to the menu item.
$submenu['wpforms-overview'][ $upgrade_link_position ][2] = esc_url(
$submenu['wpforms-overview'][ $upgrade_link_position ][2]
// phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
* Wrapper for the hook to render our custom settings pages.
public function admin_page(): void {
* Fires to show the WPForms admin page.
do_action( 'wpforms_admin_page' ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
* Add a settings link to the Plugins page.
* @param array|mixed $links Plugin row links.
* @param string $plugin_file Path to the plugin file relative to the plugins' directory.
* @param array $plugin_data An array of plugin data. See `get_plugin_data()`.
* @param string $context The plugin context.
* @noinspection PhpUnusedParameterInspection
* @noinspection HtmlUnknownTarget
* @noinspection PhpMissingParamTypeInspection
public function settings_link( $links, $plugin_file, $plugin_data, $context ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$custom['wpforms-pro'] = sprintf(
'<a href="%1$s" aria-label="%2$s" target="_blank" rel="noopener noreferrer"
style="color: #00a32a; font-weight: 700;"
onmouseover="this.style.color=\'#008a20\';"
onmouseout="this.style.color=\'#00a32a\';"
wpforms_admin_upgrade_link(
esc_attr__( 'Upgrade to WPForms Pro', 'wpforms-lite' ),
esc_html__( 'Get WPForms Pro', 'wpforms-lite' )
$custom['wpforms-settings'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
[ 'page' => 'wpforms-settings' ],
esc_attr__( 'Go to WPForms Settings page', 'wpforms-lite' ),
esc_html__( 'Settings', 'wpforms-lite' )
$custom['wpforms-docs'] = sprintf(
'<a href="%1$s" aria-label="%2$s" target="_blank" rel="noopener noreferrer">%3$s</a>',
'utm_content' => 'Documentation',
'utm_campaign' => 'liteplugin',
'utm_medium' => 'all-plugins',
'utm_source' => 'WordPress',
'https://wpforms.com/docs/'
esc_attr__( 'Read the documentation', 'wpforms-lite' ),
esc_html__( 'Docs', 'wpforms-lite' )
return array_merge( $custom, (array) $links );
* Determine which submenu item to show (rotation).
* - Show item until the plugin has been activated for 7 or more days.
* - Once 7+ days have passed since activation - show next item.
* - Once the last item has been active for more than 7 days, always display the first item (WP Consent page).
* @return array|null { menu_title, page_title, menu_slug } or null to show none.
private function get_rotating_submenu(): ?array {
$items = $this->get_rotation_items();
// Find the first item that should be displayed.
foreach ( $items as $item ) {
$item = wp_parse_args( $item, $defaults );
$label = (string) $item['label'];
$menu_slug = (string) $item['menu_slug'];
$plugin_slug = (string) $item['slug'];
if ( empty( $label ) || empty( $menu_slug ) ) {
continue; // Skip misconfigured items.
$timestamp = $this->get_promo_plugin_activation_timestamp( $plugin_slug );
// Show if a plugin has never activated or within 7 days of activation.
$within = $timestamp === 0 || ( $now - $timestamp ) < 7 * DAY_IN_SECONDS;
'menu_slug' => $menu_slug,
// If all items are considered "complete", return the first one (cycle back).
$label = $first['label'];
$menu_slug = $first['menu_slug'];
if ( ! empty( $label ) && ! empty( $menu_slug ) ) {
'menu_slug' => $menu_slug,
* List of rotating plugins files.
private function get_rotation_plugins(): array {
'wpconsent-cookies-banner-privacy-suite/wpconsent.php' => 'wpconsent',