PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmFormTemplatesHelper.php

FrmFormTemplatesHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.26.1, at classes/helpers/FrmFormTemplatesHelper.php

266 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
51 $template['use_template'] = esc_url( $link['url'] );
52 }
53 }
54
55 /**
56 * Echo attributes for a given template.
57 *
58 * @since 6.7
59 *
60 * @param array $template The template data.
61 * @param bool $expired Whether the API request is expired or not.
62 *
63 * @return void
64 */
65 public static function add_template_attributes( $template, $expired ) {
66 $attributes = array(
67 'tabindex' => '0',
68 'data-id' => $template['id'],
69 'frm-search-text' => strtolower( $template['name'] ),
70 );
71
72 // Set 'data-slug' attribute.
73 if ( ! empty( $template['slug'] ) ) {
74 $attributes['data-slug'] = $template['slug'];
75 }
76
77 // Set 'data-categories' attribute.
78 if ( ! empty( $template['category_slugs'] ) ) {
79 $attributes['data-categories'] = implode( ',', $template['category_slugs'] );
80 }
81
82 $attributes['class'] = self::prepare_single_template_classes( $template );
83 self::prepare_single_template_plan( $template, $expired, $attributes );
84
85 FrmAppHelper::array_to_html_params( $attributes, true );
86 }
87
88 /**
89 * Add classes for a given template.
90 *
91 * @since 6.7
92 *
93 * @param array $template The template data.
94 *
95 * @return string
96 */
97 private static function prepare_single_template_classes( $template ) {
98 $class_names = array( 'frm-card-item' );
99
100 if ( $template['is_featured'] ) {
101 $class_names[] = 'frm-form-templates-featured-item';
102 }
103
104 if ( $template['is_favorite'] ) {
105 $class_names[] = 'frm-form-templates-favorite-item';
106 }
107
108 if ( $template['is_custom'] ) {
109 $class_names[] = 'frm-form-templates-custom-item';
110 }
111
112 return implode( ' ', $class_names );
113 }
114
115 /**
116 * Add info about the required plan for this template.
117 *
118 * @since 6.7
119 *
120 * @param array $template The template data.
121 * @param bool $expired Whether the license is expired.
122 * @param array $attributes The template attributes.
123 *
124 * @return void
125 */
126 private static function prepare_single_template_plan( $template, $expired, &$attributes ) {
127 if ( ! $template['plan_required'] ) {
128 return;
129 }
130
131 $required_plan_slug = sanitize_title( $template['plan_required'] );
132 $attributes['data-required-plan'] = $expired && 'free' !== $required_plan_slug ? 'renew' : $required_plan_slug;
133
134 if ( 'free' === $required_plan_slug ) {
135 $attributes['data-key'] = $template['key'];
136 }
137
138 $attributes['class'] .= ' frm-form-templates-locked-item frm-' . esc_attr( $required_plan_slug ) . '-template';
139 }
140
141 /**
142 * Echo attributes for the link to view a template.
143 *
144 * @since 6.7
145 *
146 * @param array $template The template data.
147 *
148 * @return void
149 */
150 public static function add_template_link_attributes( $template ) {
151 $attributes = array(
152 'class' => 'button button-secondary frm-button-secondary frm-small',
153 'role' => 'button',
154 'href' => $template['link'],
155 );
156
157 if ( ! $template['is_custom'] ) {
158 $utm = array(
159 'medium' => 'form-templates',
160 'content' => $template['slug'],
161 );
162
163 $attributes['href'] = FrmAppHelper::admin_upgrade_link( $utm, $attributes['href'] );
164 $attributes['target'] = '_blank';
165 }
166
167 FrmAppHelper::array_to_html_params( $attributes, true );
168 }
169
170 /**
171 * Show the CTA to upgrade or renew.
172 *
173 * @since 6.7
174 *
175 * @param array $args {
176 * Arguments for the CTA.
177 *
178 * @type string $upgrade_link Upgrade link URL.
179 * @type string $renew_link Renew link URL.
180 * }
181 *
182 * @return void
183 */
184 public static function show_upgrade_renew_cta( $args ) {
185 // Show 'renew' banner for expired users.
186 if ( $args['expired'] ) {
187 FrmTipsHelper::show_admin_cta(
188 array(
189 'title' => esc_html__( 'Get Super Powers with Pre-built Forms', 'formidable' ),
190 'description' => esc_html__( 'Unleash the potential of hundreds of form templates and save precious time. Renew today for unparalleled form-building speed.', 'formidable' ),
191 'link_text' => esc_html__( 'Renew Now', 'formidable' ),
192 'link_url' => $args['renew_link'],
193 'id' => 'frm-renew-subscription-banner',
194 )
195 );
196 return;
197 }
198
199 // Show 'upgrade' banner for non-elite users.
200 if ( ! in_array( FrmAddonsController::license_type(), array( 'elite', 'business' ), true ) ) {
201 FrmTipsHelper::show_admin_cta(
202 array(
203 'class' => 'frm-gradient',
204 'icon' => 'frmfont frm_speaker_icon',
205 'title' => sprintf(
206 /* translators: %1$s: Open span tag, %2$s: Close span tag */
207 esc_html__( 'Upgrade to get all %1$s%2$s templates', 'formidable' ),
208 '<span class="frm-form-templates-extra-templates-count">',
209 '</span>'
210 ),
211 'description' => esc_html__( 'Upgrade to PRO to get access to all of our templates and unlock the full potential of your forms.', 'formidable' ),
212 'link_text' => esc_html__( 'Get More Templates', 'formidable' ),
213 'link_url' => $args['upgrade_link'],
214 'id' => 'frm-upgrade-banner',
215 )
216 );
217 }
218 }
219
220 /**
221 * Echo the get free templates banner.
222 *
223 * @since 6.25
224 *
225 * @return void
226 */
227 public static function echo_get_free_templates_banner() {
228 $args = array(
229 'direction' => 'vertical',
230 );
231
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 }
238
239 /**
240 * Checks if the get free templates banner should be displayed.
241 *
242 * @since 6.25
243 *
244 * @return bool
245 */
246 public static function needs_get_free_templates_banner() {
247 return ! FrmAppHelper::pro_is_installed() && ! FrmFormTemplateApi::get_free_license_code();
248 }
249
250 /**
251 * Checks if a template needs the free plan override.
252 *
253 * @since 6.25
254 *
255 * @param array $template The template data.
256 *
257 * @return bool
258 */
259 private static function needs_free_plan( $template ) {
260 return self::needs_get_free_templates_banner()
261 && ! empty( $template['category_slugs'] )
262 && in_array( 'free', $template['category_slugs'], true )
263 && ! in_array( $template['id'], FrmFormTemplatesController::FREE_TEMPLATES_IDS, true );
264 }
265 }
266