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 / models / FrmApplicationTemplate.php

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

310 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 5.3
8 */
9 class FrmApplicationTemplate {
10
11 /**
12 * @var array<string>|null
13 */
14 private static $keys;
15
16 /**
17 * @var array<string>|null
18 */
19 private static $keys_with_images;
20
21 /**
22 * @var array<string>|null
23 */
24 private static $categories;
25
26 /**
27 * @var array
28 */
29 private $api_data;
30
31 /**
32 * @return void
33 */
34 public static function init() {
35 /**
36 * @since 5.3
37 *
38 * @param array $keys
39 */
40 self::$keys = apply_filters(
41 'frm_application_data_keys',
42 array( 'key', 'name', 'description', 'link', 'categories', 'views', 'forms', 'used_addons' )
43 );
44 self::$keys_with_images = array_merge(
45 self::get_template_keys_with_local_png_images(),
46 self::get_template_keys_with_local_webp_images()
47 );
48 self::$categories = array();
49 }
50
51 /**
52 * Newer templates now use .webp files instead of .png.
53 *
54 * @since 6.16
55 *
56 * @return array<string>
57 */
58 private static function get_template_keys_with_local_webp_images() {
59 return array(
60 'member-directory',
61 'link-in-bio-instagram',
62 'letter-of-recommendation',
63 'invoice-pdf',
64 'freelance-invoice-generator',
65 'contract-agreement',
66 'charity-tracker',
67 'certificate',
68 'testimonials',
69 );
70 }
71
72 /**
73 * Return the template keys that have embedded images. Otherwise, we want to avoid trying to load the URL and use the placeholder instead.
74 *
75 * @return array<string>
76 */
77 private static function get_template_keys_with_local_png_images() {
78 return array(
79 'business-hours',
80 'faq-template-wordpress',
81 'restaurant-menu',
82 'team-directory',
83 'product-review',
84 'real-estate-listings',
85 'business-directory',
86 'artist-gallery',
87 'classifieds',
88 'community-event-calendar',
89 'company-timeline',
90 'crowdfunding',
91 'job-board',
92 'logo-cloud',
93 'multi-property-booking-system',
94 'wedding-venue-booking',
95 'permission-slip',
96 'property-directory',
97 'radio-station',
98 'recipe',
99 'rsvp-invite',
100 'searchable-directory',
101 'shopping-cart',
102 'sports-league-calendar',
103 'staff-directory',
104 'tiered-pricing',
105 'trip-itinerary',
106 'twitterx',
107 );
108 }
109
110 /**
111 * @param array $api_data
112 *
113 * @return void
114 */
115 public function __construct( $api_data ) {
116 $this->api_data = $api_data;
117
118 if ( ! empty( $api_data['categories'] ) ) {
119 self::populate_category_information( $api_data['categories'] );
120 }
121 }
122
123 /**
124 * @param array<string> $categories
125 *
126 * @return void
127 */
128 private static function populate_category_information( $categories ) {
129 foreach ( $categories as $category ) {
130 if ( self::category_matches_a_license_type( $category ) ) {
131 continue;
132 }
133
134 if ( in_array( $category, self::$categories, true ) ) {
135 continue;
136 }
137 self::$categories[] = $category;
138 }
139 }
140
141 /**
142 * @since 5.5.2
143 *
144 * @param string $category
145 *
146 * @return bool
147 */
148 private static function category_matches_a_license_type( $category ) {
149 if ( false !== strpos( $category, '+Views' ) ) {
150 return true;
151 }
152 return in_array( $category, FrmFormsHelper::get_license_types(), true );
153 }
154
155 /**
156 * @return array<string>
157 */
158 public static function get_categories() {
159 return self::$categories ?? array();
160 }
161
162 /**
163 * @return array
164 */
165 public function as_js_object() {
166 if ( ! is_array( self::$keys ) ) {
167 return array();
168 }
169
170 $application = array();
171
172 foreach ( self::$keys as $key ) {
173 if ( ! isset( $this->api_data[ $key ] ) ) {
174 continue;
175 }
176
177 $value = $this->api_data[ $key ];
178
179 if ( 'icon' === $key ) {
180 // Icon is an array. The first array item is the image URL.
181 $application[ $key ] = reset( $value );
182 } elseif ( 'categories' === $key ) {
183 $application[ $key ] = array_values(
184 array_filter(
185 $value,
186 function ( $category ) {
187 return false === strpos( $category, '+Views' );
188 }
189 )
190 );
191 } else {
192 if ( 'views' === $key ) {
193 $key = 'viewCount';
194 } elseif ( 'forms' === $key ) {
195 $key = 'formCount';
196 }
197
198 if ( 'name' === $key && ' Template' === substr( $value, -9 ) ) {
199 // Strip off the " Template" text at the end of the name as it takes up space.
200 $value = substr( $value, 0, -9 );
201 }
202 $application[ $key ] = $value;
203 }//end if
204 }//end foreach
205
206 $application['hasLiteThumbnail'] = in_array( $application['key'], self::$keys_with_images, true );
207 $application['isWebp'] = in_array( $application['key'], self::get_template_keys_with_local_webp_images(), true );
208
209 if ( ! array_key_exists( 'url', $application ) ) {
210 $application['requires'] = FrmFormsHelper::get_plan_required( $application );
211
212 if ( false === $application['requires'] ) {
213 // Application is invalid if the URL is unavailable and there is no plan required.
214 return array();
215 }
216
217 $purchase_url = $this->is_available_for_purchase();
218
219 if ( false !== $purchase_url ) {
220 $application['forPurchase'] = true;
221 }
222 $application['upgradeUrl'] = $this->get_admin_upgrade_link();
223 $application['link'] = $application['upgradeUrl'];
224 }
225
226 $application['isNew'] = $this->is_new();
227
228 $application['usedAddons'] = array();
229
230 if ( isset( $application['used_addons'] ) ) {
231 // Change key to camel case.
232 $application['usedAddons'] = $application['used_addons'];
233 unset( $application['used_addons'] );
234 }
235
236 return $application;
237 }
238
239 /**
240 * @return bool
241 */
242 private function is_available_for_purchase() {
243 if ( ! array_key_exists( 'min_plan', $this->api_data ) ) {
244 return false;
245 }
246
247 $license_type = '';
248 $api = new FrmFormApi();
249 $addons = $api->get_api_info();
250
251 if ( ! array_key_exists( 93790, $addons ) ) {
252 return false;
253 }
254
255 $pro = $addons[93790];
256
257 if ( ! array_key_exists( 'type', $pro ) ) {
258 return false;
259 }
260
261 $license_type = strtolower( $pro['type'] );
262 $args = array(
263 'license_type' => $license_type,
264 'plan_required' => $this->get_required_license(),
265 );
266
267 if ( ! FrmFormsHelper::plan_is_allowed( $args ) ) {
268 return false;
269 }
270
271 return true;
272 }
273
274 /**
275 * Check if an application template is new. If it is, we include a "NEW" pill beside the title.
276 *
277 * @since 6.0
278 *
279 * @return bool
280 */
281 private function is_new() {
282 return ! empty( $this->api_data['is_new'] );
283 }
284
285 /**
286 * @return string
287 */
288 private function get_required_license() {
289 $required_license = strtolower( $this->api_data['min_plan'] );
290
291 if ( 'plus' === $required_license ) {
292 $required_license = 'personal';
293 }
294 return $required_license;
295 }
296
297 /**
298 * @return string
299 */
300 private function get_admin_upgrade_link() {
301 return FrmAppHelper::admin_upgrade_link(
302 array(
303 'content' => 'upgrade',
304 'medium' => 'applications',
305 ),
306 'view-templates/' . $this->api_data['slug']
307 );
308 }
309 }
310