namespace WPForms\Admin\Builder;
const TEMPLATES_HASH_OPTION = 'wpforms_templates_hash';
* Favorite templates option.
const FAVORITE_TEMPLATES_OPTION = 'wpforms_favorite_templates';
* All templates data from API.
private $api_templates = [];
* Template categories data.
* Template subcategories data.
* Favorite templates list.
* Determine if the class is allowed to load.
private function allow_load() {
$has_permissions = wpforms_current_user_can( [ 'create_forms', 'edit_forms' ] );
$allowed_requests = wpforms_is_admin_ajax() || wpforms_is_admin_page( 'builder' ) || wpforms_is_admin_page( 'templates' );
$allow = $has_permissions && $allowed_requests;
* Whether to allow the form templates functionality to load.
* @param bool $allow True or false.
return (bool) apply_filters( 'wpforms_admin_builder_templates_allow_load', $allow );
if ( ! $this->allow_load() ) {
$this->init_license_data();
$this->init_templates_data();
protected function hooks() {
add_action( 'admin_init', [ $this, 'create_form_on_request' ], 100 );
add_filter( 'wpforms_form_templates_core', [ $this, 'add_templates_to_setup_panel' ], 20 );
add_filter( 'wpforms_create_form_args', [ $this, 'apply_to_new_form' ], 10, 2 );
add_filter( 'wpforms_save_form_args', [ $this, 'apply_to_existing_form' ], 10, 3 );
add_action( 'admin_print_scripts', [ $this, 'upgrade_banner_template' ] );
add_action( 'admin_print_scripts', [ $this, 'upgrade_lite_banner_template' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueues' ] );
add_action( 'wp_ajax_wpforms_templates_favorite', [ $this, 'ajax_save_favorites' ] );
add_filter( 'wpforms_form_templates', [ $this, 'add_addons_templates' ] );
* Enqueue assets for the Setup panel.
public function enqueues() {
$min = wpforms_get_min_suffix();
WPFORMS_PLUGIN_URL . 'assets/lib/list.min.js',
'wpforms-form-templates',
WPFORMS_PLUGIN_URL . "assets/js/admin/builder/form-templates{$min}.js",
[ 'underscore', 'wp-util', 'listjs' ],
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'admin_nonce' => wp_create_nonce( 'wpforms-admin' ),
'nonce' => wp_create_nonce( 'wpforms-form-templates' ),
'can_install_addons' => wpforms_can_install( 'addon' ),
'activating' => esc_html__( 'Activating', 'wpforms-lite' ),
'cancel' => esc_html__( 'Cancel', 'wpforms-lite' ),
'heads_up' => esc_html__( 'Heads Up!', 'wpforms-lite' ),
'install_confirm' => esc_html__( 'Install and activate', 'wpforms-lite' ),
'activate_confirm' => esc_html__( 'Activate', 'wpforms-lite' ),
'ok' => esc_html__( 'Ok', 'wpforms-lite' ),
'template_addons_error' => esc_html__( 'Could not install OR activate all the required addons. Please download from wpforms.com and install them manually. Would you like to use the template anyway?', 'wpforms-lite' ),
'use_template' => esc_html__( 'Yes, use template', 'wpforms-lite' ),
'delete_template' => esc_html__( 'Yes, Delete', 'wpforms-lite' ),
'delete_template_title' => esc_html__( 'Delete Form Template', 'wpforms-lite' ),
'delete_template_content' => esc_html__( 'Are you sure you want to delete this form template? This cannot be undone.', 'wpforms-lite' ),
if ( $strings['can_install_addons'] ) {
/* translators: %1$s - template name, %2$s - addon name(s). */
$strings['template_addon_prompt'] = esc_html( sprintf( __( 'The %1$s template requires the %2$s. Would you like to install and activate it?', 'wpforms-lite' ), '%template%', '%addons%' ) );
/* translators: %1$s - template name, %2$s - addon name(s). */
$strings['template_addons_prompt'] = esc_html( sprintf( __( 'The %1$s template requires the %2$s. Would you like to install and activate all the required addons?', 'wpforms-lite' ), '%template%', '%addons%' ) );
/* translators: %1$s - template name, %2$s - addon name(s). */
$strings['template_addon_activate'] = esc_html( sprintf( __( 'The %1$s template requires the %2$s addon. Would you like to activate it?', 'wpforms-lite' ), '%template%', '%addons%' ) );
/* translators: %s - addon name(s). */
$strings['template_addon_prompt'] = esc_html( sprintf( __( "To use all of the features in this template, you'll need the %s. Contact your site administrator to install it, then try opening this template again.", 'wpforms-lite' ), '%addons%' ) );
/* translators: %s - addon name(s). */
$strings['template_addons_prompt'] = esc_html( sprintf( __( "To use all of the features in this template, you'll need the %s. Contact your site administrator to install them, then try opening this template again.", 'wpforms-lite' ), '%addons%' ) );
'wpforms-form-templates',
'wpforms_form_templates',
'wpforms-form-templates',
$this->get_localized_addons()
private function get_localized_addons() {
return wpforms_chain( wpforms()->obj( 'addons' )->get_available() )
static function( $addon ) {
'title' => $addon['title'],
'action' => $addon['action'],
private function init_license_data() {
$this->all_licenses = [ 'lite', 'basic', 'plus', 'pro', 'elite', 'agency', 'ultimate' ];
$this->license['key'] = wpforms_get_license_key();
$this->license['type'] = wpforms_get_license_type();
$this->license['type'] = in_array( $this->license['type'], [ 'agency', 'ultimate' ], true ) ? 'elite' : $this->license['type'];
$this->license['type'] = empty( $this->license['type'] ) ? 'lite' : $this->license['type'];
$this->license['index'] = array_search( $this->license['type'], $this->all_licenses, true );
* Init templates and categories data.
private function init_templates_data() {
// Get cached templates data.
$cache_obj = wpforms()->obj( 'builder_templates_cache' );
$cache_data = $cache_obj->get();
$templates_all = ! empty( $cache_data['templates'] ) ? $this->sort_templates_by_created_at( $cache_data['templates'] ) : [];
$this->categories = ! empty( $cache_data['categories'] ) ? $cache_data['categories'] : [];
$this->subcategories = ! empty( $cache_data['subcategories'] ) ? $cache_data['subcategories'] : [];
$this->init_api_templates( $templates_all );
* Sort templates by their created_at value in ascending order.
* @param array $templates Templates to be sorted.
* @return array Sorted templates.
private function sort_templates_by_created_at( array $templates ): array {
static function ( $template_a, $template_b ) {
if ( $template_a['created_at'] === $template_b['created_at'] ) {
return $template_a['created_at'] < $template_b['created_at'] ? -1 : 1;
* Determine if user's license level has access to the template.
* @param array $template Template data.
private function has_access( $template ) {
if ( ! empty( $template['has_access'] ) ) {
$template_licenses = empty( $template['license'] ) ? [] : array_map( 'strtolower', (array) $template['license'] );
foreach ( $template_licenses as $template_license ) {
$has_access = $this->license['index'] >= array_search( $template_license, $this->all_licenses, true );
* Get favorites templates list.
* @param bool $all Optional. True for getting all favorites lists. False by default.
public function get_favorites_list( $all = false ) {
$favorites_list = (array) get_option( self::FAVORITE_TEMPLATES_OPTION, [] );
$user_id = get_current_user_id();
return isset( $favorites_list[ $user_id ] ) ? $favorites_list[ $user_id ] : [];
* Update favorites templates list.
public function update_favorites_list() {
$this->favorites_list = $this->get_favorites_list();
* Determine if template is marked as favorite.
* @param string $template_slug Template slug.
public function is_favorite( $template_slug ) {
if ( $this->favorites_list === null ) {
$this->update_favorites_list();
return isset( $this->favorites_list[ $template_slug ] );
* Save favorites templates.
public function ajax_save_favorites(): void {
if ( ! $this->is_valid_ajax_request() ) {
[ $template_slug, $favorite ] = $this->get_ajax_input();
$favorites = $this->get_favorites_list( true );
$user_id = get_current_user_id();
$is_favorite = $favorite === 'true';
$is_exists = isset( $favorites[ $user_id ][ $template_slug ] );
if ( $is_favorite && $is_exists ) {
$favorites[ $user_id ][ $template_slug ] = true;
} elseif ( $is_exists ) {
unset( $favorites[ $user_id ][ $template_slug ] );
update_option( self::FAVORITE_TEMPLATES_OPTION, $favorites );
// Update and save the template content cache.
$templates_cache_obj = wpforms()->obj( 'builder_templates_cache' );
if ( $templates_cache_obj ) {
$templates_cache_obj->wipe_content_cache();
protected function get_ajax_input(): array {
// Nonce is checked in the is_valid_ajax_request() method.
// phpcs:disable WordPress.Security.NonceVerification.Missing
$template_slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
$favorite = isset( $_POST['favorite'] ) ? sanitize_key( wp_unslash( $_POST['favorite'] ) ) : '';
return [ $template_slug, $favorite ];
// phpcs:enable WordPress.Security.NonceVerification.Missing
* Determine if the AJAX request is valid.
private function is_valid_ajax_request(): bool {
return check_ajax_referer( 'wpforms-form-templates', 'nonce', false ) &&
wpforms_current_user_can( 'create_forms' ) &&
isset( $_POST['slug'], $_POST['favorite'] );
* Determine if the template exists and the customer has access to it.
* @param string $slug Template slug or ID.
public function is_valid_template( $slug ) {
$template = $this->get_template_by_id( $slug );
return ! empty( $this->get_template_by_slug( $slug ) );
$has_cache = wpforms()->obj( 'builder_template_single' )->instance( $template['id'], $this->license )->get();
return $this->has_access( $template ) && $has_cache;
* Determine license level of the template.
* @param array $template Template data.
private function get_license_level( $template ) {
$licenses_pro = [ 'basic', 'plus', 'pro' ];
$licenses_template = (array) $template['license'];
empty( $template['license'] ) ||
in_array( 'lite', $licenses_template, true )