namespace WPForms\Forms\Fields\Traits;
use WPForms\Admin\Education\Helpers;
* Mostly educational things for the Pro field in the Lite plugin.
protected $is_pro = false;
* Whether the field is a Pro field.
protected $is_pro_field = true;
* Whether the Addon is initialized.
protected $is_addon_initialized = false;
* Whether the field is disabled.
protected $is_disabled_field = true;
* Addon educational data.
protected $addon_edu_data = [];
private function init_pro_field(): void {
$this->is_pro = wpforms()->is_pro();
$this->is_addon_initialized = ! empty( $this->addon_slug ) && wpforms_is_addon_initialized( $this->addon_slug );
$this->is_disabled_field = $this->is_disabled_field();
add_filter( 'admin_init', [ $this, 'admin_init_pro_field' ] );
add_filter( 'wpforms_builder_field_option_class', [ $this, 'filter_field_option_class' ], 10, 2 );
add_filter( "wpforms_admin_builder_ajax_save_form_field_$this->type", [ $this, 'filter_save_form_field_data' ], 10, 3 );
add_filter( 'wpforms_field_data', [ $this, 'filter_frontend_field_data' ], PHP_INT_MAX, 2 );
add_filter( 'wpforms_helpers_form_pro_fields', [ $this, 'filter_form_pro_fields' ], PHP_INT_MAX, 2 );
add_filter( 'wpforms_helpers_form_addons_edu_data', [ $this, 'filter_form_addons_edu_data' ], PHP_INT_MAX, 2 );
add_filter( 'wpforms_field_preview_display_duplicate_button', [ $this, 'filter_field_preview_display_duplicate_button' ], 10, 2 );
add_filter( 'wpforms_field_preview_class', [ $this, 'filter_field_preview_class' ], 10, 2 );
add_filter( 'wpforms_entry_save_data', [ $this, 'filter_entry_save_data' ], 10, 3 );
add_filter( 'wpforms_pro_admin_entries_table_facades_columns_get_field_columns_forbidden_fields', [ $this, 'filter_field_columns_forbidden_fields' ], 10, 2 );
add_filter( 'wpforms_pro_admin_entries_export_configuration', [ $this, 'filter_entries_export_configuration' ] );
add_filter( "wpforms_pro_admin_entries_edit_is_field_displayable_$this->type", [ $this, 'filter_is_field_displayable' ], 10, 3 );
* Init Pro field on the ` admin_init ` hook.
public function admin_init_pro_field(): void {
$this->addon_edu_data = $this->get_field_addon_edu_data();
* Get the Pro field options tab CSS class.
* @param string|mixed $css_class CSS class.
* @param array $field Field data.
* @noinspection PhpMissingParamTypeInspection
public function filter_field_option_class( $css_class, $field ): string {
$css_class = (string) $css_class;
if ( $field['type'] !== $this->type ) {
$css_class .= empty( $this->is_disabled_field ) ? '' : ' wpforms-field-is-pro';
return trim( $css_class );
* Filter field data before saving the form.
* @param array $field_data Field data.
* @param array $form_data Forms data.
* @param array $saved_form_data Saved form data.
* @noinspection PhpMissingParamTypeInspection
* @noinspection PhpUnusedParameterInspection
public function filter_save_form_field_data( $field_data, $form_data, $saved_form_data ) {
if ( empty( $this->is_disabled_field ) ) {
$field_id = $field_data['id'] ?? '';
// Prevent changes in the field data if it's a Pro field in Lite.
// The settings are disabled in the Form Builder, but users can still hijack the data.
// Therefore, return the saved field data if it exists.
return $saved_form_data['fields'][ $field_id ] ?? $field_data;
* Filter form pro fields array.
* @param array|mixed $pro_fields Pro fields array.
* @param array $field Field data.
public function filter_form_pro_fields( $pro_fields, array $field ): array {
$pro_fields = is_array( $pro_fields ) ? $pro_fields : [];
if ( isset( $field['type'] ) && $field['type'] === $this->type ) {
* Filter the form addons educational data array.
* @param array|mixed $addons_edu_data Addons educational data.
* @param array $field Field data.
public function filter_form_addons_edu_data( $addons_edu_data, array $field ): array {
$addons_edu_data = is_array( $addons_edu_data ) ? $addons_edu_data : [];
if ( ! isset( $field['type'] ) || $field['type'] !== $this->type || empty( $this->addon_edu_data ) ) {
$addon = $this->addon_edu_data['slug'] ?? '';
$addons_edu_data[ $addon ] = $this->addon_edu_data;
* Get the Pro field options notice.
* @noinspection HtmlUnknownAttribute
private function get_field_options_notice(): string {
if ( empty( $this->is_disabled_field ) ) {
[ $name, $title, $content, $button_label, $button_utm ] = $this->get_field_options_notice_texts();
$action = $this->addon_edu_data['action'] ?? 'upgrade';
$button_class = 'education-action-button';
if ( $action !== 'upgrade' ) {
$button_class = 'education-modal';
'data-nonce="%1$s" data-path="%2$s" data-url="%3$s" data-message="%4$s" data-field-type="%5$s" data-name="%6$s"',
esc_attr( wp_create_nonce( 'wpforms-admin' ) ),
$this->addon_edu_data['path'] ?? '',
$this->addon_edu_data['url'] ?? '',
$action === 'incompatible' ? $this->addon_edu_data['message'] : '',
'<div class="wpforms-field-option-field-title-notice">
<div class="wpforms-alert-info wpforms-alert wpforms-educational-alert">
<button class="wpforms-btn wpforms-btn-sm wpforms-btn-blue %3$s" data-action="%4$s" %6$s data-license="%7$s" data-utm-content="%8$s">%5$s</button>
esc_attr( $button_class ),
esc_html( $button_label ),
esc_attr( $this->addon_edu_data['license_level'] ?? 'pro' ),
* Get the Pro field options notice texts.
private function get_field_options_notice_texts(): array {
$action = $this->addon_edu_data['action'] ?? 'upgrade';
$addon_name = $this->addon_edu_data['title'] ?? '';
'upgrade' => sprintf( /* translators: %1$s - Field name. */
esc_html__( '%1$s is a Pro Feature', 'wpforms-lite' ),
'incompatible' => esc_html__( 'Incompatible Addon', 'wpforms-lite' ),
'upgrade' => sprintf( /* translators: %1$s - Field name. */
esc_html__( 'Upgrade to gain access to the %1$s field and dozens of other powerful features to help you build smarter forms and grow your business.', 'wpforms-lite' ),
'install' => sprintf( /* translators: %1$s - Addon name. */
esc_html__( 'You have access to the %1$s, but it\'s not currently installed.', 'wpforms-lite' ),
'activate' => sprintf( /* translators: %1$s - Addon name. */
esc_html__( 'You have access to the %1$s, but it\'s not currently activated.', 'wpforms-lite' ),
'incompatible' => sprintf( /* translators: %1$s - Addon name. */
esc_html__( 'The %1$s is not compatible with this version of WPForms and requires an update.', 'wpforms-lite' ),
'upgrade' => esc_html__( 'Upgrade to Pro', 'wpforms-lite' ),
'install' => esc_html__( 'Install Addon', 'wpforms-lite' ),
'activate' => esc_html__( 'Activate Addon', 'wpforms-lite' ),
'incompatible' => esc_html__( 'Update Addon', 'wpforms-lite' ),
// If it's not an upgrade action, use the addon data.
if ( $action !== 'upgrade' ) {
$utm_name = $this->addon_edu_data['utm_content'];
$edu_fields = wpforms()->obj( 'education_fields' );
$edu_field = $edu_fields ? $edu_fields->get_field( $this->type ) : null;
$utm_name = $edu_field['name_en'] ?? $this->type; // Fallback to the field type.
$titles[ $action ] ?? $titles['upgrade'],
$contents[ $action ] ?? $contents['upgrade'],
$button_labels[ $action ] ?? $button_labels['upgrade'],
* Determine if the field is disabled.
* @since 1.9.9.3 The method access modifier is changed from private to protected.
protected function is_disabled_field(): bool {
// It is a Pro field in Lite OR the addon is not initialized.
return ! ( $this->is_pro && ( empty( $this->addon_slug ) || $this->is_addon_initialized ) );
* @param string $option Option name.
* @param array $field Field data.
* @param array $args Additional arguments.
* @param bool $do_echo Echo or return.
* @noinspection ReturnTypeCanBeDeclaredInspection
* @noinspection PhpMultipleClassDeclarationsInspection
public function field_preview_option( $option, $field, $args = [], $do_echo = true ) {
// Hide remaining elements, prevent incompatible addon field elements from being displayed.
if ( $option === 'hide-remaining' && ! empty( $this->is_disabled_field ) ) {
echo '<div class="wpforms-field-hide-remaining"></div>';
parent::field_preview_option( $option, $field, $args, $do_echo );
* Get the Pro field preview badge.
private function get_field_preview_badge(): string {
if ( empty( $this->is_disabled_field ) ) {
$action = $this->addon_edu_data['action'] ?? '';
if ( $action === 'incompatible' ) {
return Helpers::get_badge( esc_html__( 'Update required', 'wpforms-lite' ) , 'lg', 'inline', 'red' );
// If it's an addon field in Pro AND the addon is not initialized, show the ADDON badge.
if ( in_array( $action, [ 'install' ,'activate' ], true ) ) {
return Helpers::get_badge( 'Addon', 'lg', 'inline', 'orange' );
return Helpers::get_badge( 'Pro', 'lg', 'inline', 'green' );
* Get the addon educational data of the field.
private function get_field_addon_edu_data(): array {
if ( empty( $this->addon_slug ) || ! empty( $this->is_addon_initialized ) || ! is_admin() ) {
$addons = Helpers::get_edu_addons();
return $addons[ 'wpforms-' . $this->addon_slug ] ?? [];
* Filter frontend field data to prevent rendering Pro fields in Lite.
* @param array|mixed $field Field data.
* @param array $form_data Form data.
* @noinspection PhpMissingParamTypeInspection
* @noinspection PhpUnusedParameterInspection
public function filter_frontend_field_data( $field, $form_data ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$type = $field['type'] ?? '';
// If it's not a Pro field or the field type doesn't match, return the field data as is.
if ( empty( $this->is_pro_field ) || $type !== $this->type ) {
// If it's a Pro field in Lite OR,
// the addon is not initialized,
// return an empty array to prevent rendering.
if ( ! empty( $this->is_disabled_field ) ) {
* Disallow the field preview "Duplicate" button.
* @param bool|mixed $display Display switch.
* @param array $field Field settings.
* @noinspection PhpMissingParamTypeInspection
public function filter_field_preview_display_duplicate_button( $display, $field ): bool {
if ( $field['type'] !== $this->type || empty( $this->is_disabled_field ) ) {
* Add a class to the field preview container.
* @param string|mixed $css_class CSS class.
* @param array $field Field settings.
* @noinspection PhpMissingParamTypeInspection
public function filter_field_preview_class( $css_class, $field ): string {
$css_class = (string) $css_class;
if ( $field['type'] !== $this->type || empty( $this->is_disabled_field ) ) {
return trim( $css_class . ' wpforms-field-is-pro' );
* Filter entry save data.
* @param array|mixed $fields Entry fields data.
* @param array $entry Entry data.
* @param array $form_data Form data.
* @noinspection PhpUnusedParameterInspection
public function filter_entry_save_data( $fields, array $entry, array $form_data ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$fields = (array) $fields;
// If it's not a disabled Pro field, return the fields as is.
if ( empty( $this->is_disabled_field ) ) {
// Remove disabled Pro fields from the entry fields.
foreach ( $fields as $field_id => $field ) {
if ( isset( $field['type'] ) && $field['type'] === $this->type ) {
unset( $fields[ $field_id ] );