PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.25.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.25.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.25.1, at classes/helpers/FrmFormTemplatesHelper.php

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