| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form Templates Helper class. |
| 4 |
* |
| 5 |
* @package Formidable |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
die( 'You are not allowed to call this page directly.' ); |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Provides helper functions for managing form templates in the admin area. |
| 14 |
* |
| 15 |
* @since 6.7 |
| 16 |
*/ |
| 17 |
class FrmFormTemplatesHelper { |
| 18 |
|
| 19 |
/** |
| 20 |
* Updates template array with additional details and URL. |
| 21 |
* |
| 22 |
* @param array $template Template data. |
| 23 |
* @param string $pricing Upgrade link URL. |
| 24 |
* @param string $license_type License type. |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public static function prepare_template_details( &$template, $pricing, $license_type ) { |
| 29 |
$template['is_featured'] = ! empty( $template['is_featured'] ); |
| 30 |
$template['is_favorite'] = ! empty( $template['is_favorite'] ); |
| 31 |
$template['is_custom'] = ! empty( $template['is_custom'] ); |
| 32 |
$template['plan_required'] = FrmFormsHelper::get_plan_required( $template ); |
| 33 |
|
| 34 |
if ( self::needs_free_plan( $template ) ) { |
| 35 |
$template['plan_required'] = 'free'; |
| 36 |
} |
| 37 |
|
| 38 |
if ( ! empty( $template['name'] ) ) { |
| 39 |
$template['name'] = $template['is_custom'] ? $template['name'] : preg_replace( '/(\sForm)?(\sTemplate)?$/', '', $template['name'] ); |
| 40 |
} else { |
| 41 |
$template['name'] = ''; |
| 42 |
} |
| 43 |
|
| 44 |
$template['use_template'] = '#'; |
| 45 |
|
| 46 |
if ( $template['is_custom'] ) { |
| 47 |
$template['use_template'] = $template['url']; |
| 48 |
} elseif ( ! $template['plan_required'] ) { |
| 49 |
$link = FrmFormsHelper::get_template_install_link( $template, compact( 'pricing', 'license_type' ) ); |
| 50 |
$template['use_template'] = esc_url( $link['url'] ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Echo attributes for a given template. |
| 56 |
* |
| 57 |
* @since 6.7 |
| 58 |
* |
| 59 |
* @param array $template The template data. |
| 60 |
* @param bool $expired Whether the API request is expired or not. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public static function add_template_attributes( $template, $expired ) { |
| 65 |
$attributes = array( |
| 66 |
'tabindex' => '0', |
| 67 |
'data-id' => $template['id'], |
| 68 |
'frm-search-text' => strtolower( $template['name'] ), |
| 69 |
); |
| 70 |
|
| 71 |
// Set 'data-slug' attribute. |
| 72 |
if ( ! empty( $template['slug'] ) ) { |
| 73 |
$attributes['data-slug'] = $template['slug']; |
| 74 |
} |
| 75 |
|
| 76 |
// Set 'data-categories' attribute. |
| 77 |
if ( ! empty( $template['category_slugs'] ) ) { |
| 78 |
$attributes['data-categories'] = implode( ',', $template['category_slugs'] ); |
| 79 |
} |
| 80 |
|
| 81 |
$attributes['class'] = self::prepare_single_template_classes( $template ); |
| 82 |
self::prepare_single_template_plan( $template, $expired, $attributes ); |
| 83 |
|
| 84 |
FrmAppHelper::array_to_html_params( $attributes, true ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Add classes for a given template. |
| 89 |
* |
| 90 |
* @since 6.7 |
| 91 |
* |
| 92 |
* @param array $template The template data. |
| 93 |
* |
| 94 |
* @return string |
| 95 |
*/ |
| 96 |
private static function prepare_single_template_classes( $template ) { |
| 97 |
$class_names = array( 'frm-card-item' ); |
| 98 |
|
| 99 |
if ( $template['is_featured'] ) { |
| 100 |
$class_names[] = 'frm-form-templates-featured-item'; |
| 101 |
} |
| 102 |
|
| 103 |
if ( $template['is_favorite'] ) { |
| 104 |
$class_names[] = 'frm-form-templates-favorite-item'; |
| 105 |
} |
| 106 |
|
| 107 |
if ( $template['is_custom'] ) { |
| 108 |
$class_names[] = 'frm-form-templates-custom-item'; |
| 109 |
} |
| 110 |
|
| 111 |
return implode( ' ', $class_names ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Add info about the required plan for this template. |
| 116 |
* |
| 117 |
* @since 6.7 |
| 118 |
* |
| 119 |
* @param array $template The template data. |
| 120 |
* @param bool $expired Whether the license is expired. |
| 121 |
* @param array $attributes The template attributes. |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
private static function prepare_single_template_plan( $template, $expired, &$attributes ) { |
| 126 |
if ( ! $template['plan_required'] ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$required_plan_slug = sanitize_title( $template['plan_required'] ); |
| 131 |
$attributes['data-required-plan'] = $expired && 'free' !== $required_plan_slug ? 'renew' : $required_plan_slug; |
| 132 |
|
| 133 |
if ( 'free' === $required_plan_slug ) { |
| 134 |
$attributes['data-key'] = $template['key']; |
| 135 |
} |
| 136 |
|
| 137 |
$attributes['class'] .= ' frm-form-templates-locked-item frm-' . esc_attr( $required_plan_slug ) . '-template'; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Echo attributes for the link to view a template. |
| 142 |
* |
| 143 |
* @since 6.7 |
| 144 |
* |
| 145 |
* @param array $template The template data. |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public static function add_template_link_attributes( $template ) { |
| 150 |
$attributes = array( |
| 151 |
'class' => 'button button-secondary frm-button-secondary frm-small', |
| 152 |
'role' => 'button', |
| 153 |
'href' => $template['link'], |
| 154 |
); |
| 155 |
|
| 156 |
if ( ! $template['is_custom'] ) { |
| 157 |
$utm = array( |
| 158 |
'medium' => 'form-templates', |
| 159 |
'content' => $template['slug'], |
| 160 |
); |
| 161 |
|
| 162 |
$attributes['href'] = FrmAppHelper::admin_upgrade_link( $utm, $attributes['href'] ); |
| 163 |
$attributes['target'] = '_blank'; |
| 164 |
} |
| 165 |
|
| 166 |
FrmAppHelper::array_to_html_params( $attributes, true ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Show the CTA to upgrade or renew. |
| 171 |
* |
| 172 |
* @since 6.7 |
| 173 |
* |
| 174 |
* @param array $args { |
| 175 |
* Arguments for the CTA. |
| 176 |
* |
| 177 |
* @type string $upgrade_link Upgrade link URL. |
| 178 |
* @type string $renew_link Renew link URL. |
| 179 |
* } |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
public static function show_upgrade_renew_cta( $args ) { |
| 184 |
// Show 'renew' banner for expired users. |
| 185 |
if ( $args['expired'] ) { |
| 186 |
FrmTipsHelper::show_admin_cta( |
| 187 |
array( |
| 188 |
'title' => esc_html__( 'Get Super Powers with Pre-built Forms', 'formidable' ), |
| 189 |
'description' => esc_html__( 'Unleash the potential of hundreds of form templates and save precious time. Renew today for unparalleled form-building speed.', 'formidable' ), // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong |
| 190 |
'link_text' => esc_html__( 'Renew Now', 'formidable' ), |
| 191 |
'link_url' => $args['renew_link'], |
| 192 |
'id' => 'frm-renew-subscription-banner', |
| 193 |
) |
| 194 |
); |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
// Show 'upgrade' banner for non-elite users. |
| 199 |
if ( ! in_array( FrmAddonsController::license_type(), array( 'elite', 'business' ), true ) ) { |
| 200 |
FrmTipsHelper::show_admin_cta( |
| 201 |
array( |
| 202 |
'class' => 'frm-gradient', |
| 203 |
'icon' => 'frmfont frm_speaker_icon', |
| 204 |
'title' => sprintf( |
| 205 |
/* translators: %1$s: Open span tag, %2$s: Close span tag */ |
| 206 |
esc_html__( 'Upgrade to get all %1$s%2$s templates', 'formidable' ), |
| 207 |
'<span class="frm-form-templates-extra-templates-count">', |
| 208 |
'</span>' |
| 209 |
), |
| 210 |
'description' => esc_html__( 'Upgrade to PRO to get access to all of our templates and unlock the full potential of your forms.', 'formidable' ), |
| 211 |
'link_text' => esc_html__( 'Get More Templates', 'formidable' ), |
| 212 |
'link_url' => $args['upgrade_link'], |
| 213 |
'id' => 'frm-upgrade-banner', |
| 214 |
) |
| 215 |
); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Echo the get free templates banner. |
| 221 |
* |
| 222 |
* @since 6.25 |
| 223 |
* |
| 224 |
* @return void |
| 225 |
*/ |
| 226 |
public static function echo_get_free_templates_banner() { |
| 227 |
$args = array( |
| 228 |
'direction' => 'vertical', |
| 229 |
); |
| 230 |
|
| 231 |
// phpcs:disable Generic.WhiteSpace.ScopeIndent |
| 232 |
?> |
| 233 |
<div class="frm-card-item frm-px-sm"> |
| 234 |
<?php require FrmAppHelper::plugin_path() . '/classes/views/shared/get-free-templates-banner.php'; ?> |
| 235 |
</div> |
| 236 |
<?php |
| 237 |
// phpcs:enable Generic.WhiteSpace.ScopeIndent |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Checks if the get free templates banner should be displayed. |
| 242 |
* |
| 243 |
* @since 6.25 |
| 244 |
* |
| 245 |
* @return bool |
| 246 |
*/ |
| 247 |
public static function needs_get_free_templates_banner() { |
| 248 |
return ! FrmAppHelper::pro_is_installed() && ! FrmFormTemplateApi::get_free_license_code(); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Checks if a template needs the free plan override. |
| 253 |
* |
| 254 |
* @since 6.25 |
| 255 |
* |
| 256 |
* @param array $template The template data. |
| 257 |
* |
| 258 |
* @return bool |
| 259 |
*/ |
| 260 |
private static function needs_free_plan( $template ) { |
| 261 |
// phpcs:disable Generic.WhiteSpace.ScopeIndent |
| 262 |
return self::needs_get_free_templates_banner() |
| 263 |
&& ! empty( $template['category_slugs'] ) |
| 264 |
&& in_array( 'free', $template['category_slugs'], true ) |
| 265 |
&& ! in_array( $template['id'], FrmFormTemplatesController::FREE_TEMPLATES_IDS, true ); |
| 266 |
// phpcs:enable Generic.WhiteSpace.ScopeIndent |
| 267 |
} |
| 268 |
} |
| 269 |
|