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

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

300 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 * @return void
113 */
114 public function __construct( $api_data ) {
115 $this->api_data = $api_data;
116
117 if ( ! empty( $api_data['categories'] ) ) {
118 self::populate_category_information( $api_data['categories'] );
119 }
120 }
121
122 /**
123 * @param array<string> $categories
124 * @return void
125 */
126 private static function populate_category_information( $categories ) {
127 foreach ( $categories as $category ) {
128 if ( self::category_matches_a_license_type( $category ) ) {
129 continue;
130 }
131 if ( in_array( $category, self::$categories, true ) ) {
132 continue;
133 }
134 self::$categories[] = $category;
135 }
136 }
137
138 /**
139 * @since 5.5.2
140 *
141 * @param string $category
142 * @return bool
143 */
144 private static function category_matches_a_license_type( $category ) {
145 if ( false !== strpos( $category, '+Views' ) ) {
146 return true;
147 }
148 return in_array( $category, FrmFormsHelper::get_license_types(), true );
149 }
150
151 /**
152 * @return array<string>
153 */
154 public static function get_categories() {
155 return self::$categories ?? array();
156 }
157
158 /**
159 * @return array
160 */
161 public function as_js_object() {
162 if ( ! is_array( self::$keys ) ) {
163 return array();
164 }
165
166 $application = array();
167 foreach ( self::$keys as $key ) {
168 if ( ! isset( $this->api_data[ $key ] ) ) {
169 continue;
170 }
171
172 $value = $this->api_data[ $key ];
173
174 if ( 'icon' === $key ) {
175 // Icon is an array. The first array item is the image URL.
176 $application[ $key ] = reset( $value );
177 } elseif ( 'categories' === $key ) {
178 $application[ $key ] = array_values(
179 array_filter(
180 $value,
181 function ( $category ) {
182 return false === strpos( $category, '+Views' );
183 }
184 )
185 );
186 } else {
187 if ( 'views' === $key ) {
188 $key = 'viewCount';
189 } elseif ( 'forms' === $key ) {
190 $key = 'formCount';
191 }
192
193 if ( 'name' === $key && ' Template' === substr( $value, -9 ) ) {
194 // Strip off the " Template" text at the end of the name as it takes up space.
195 $value = substr( $value, 0, -9 );
196 }
197 $application[ $key ] = $value;
198 }//end if
199 }//end foreach
200
201 $application['hasLiteThumbnail'] = in_array( $application['key'], self::$keys_with_images, true );
202 $application['isWebp'] = in_array( $application['key'], self::get_template_keys_with_local_webp_images(), true );
203
204 if ( ! array_key_exists( 'url', $application ) ) {
205 $application['requires'] = FrmFormsHelper::get_plan_required( $application );
206
207 if ( false === $application['requires'] ) {
208 // Application is invalid if the URL is unavailable and there is no plan required.
209 return array();
210 }
211
212 $purchase_url = $this->is_available_for_purchase();
213 if ( false !== $purchase_url ) {
214 $application['forPurchase'] = true;
215 }
216 $application['upgradeUrl'] = $this->get_admin_upgrade_link();
217 $application['link'] = $application['upgradeUrl'];
218 }
219
220 $application['isNew'] = $this->is_new();
221
222 $application['usedAddons'] = array();
223 if ( isset( $application['used_addons'] ) ) {
224 // Change key to camel case.
225 $application['usedAddons'] = $application['used_addons'];
226 unset( $application['used_addons'] );
227 }
228
229 return $application;
230 }
231
232 /**
233 * @return bool
234 */
235 private function is_available_for_purchase() {
236 if ( ! array_key_exists( 'min_plan', $this->api_data ) ) {
237 return false;
238 }
239
240 $license_type = '';
241 $api = new FrmFormApi();
242 $addons = $api->get_api_info();
243
244 if ( ! array_key_exists( 93790, $addons ) ) {
245 return false;
246 }
247
248 $pro = $addons[93790];
249 if ( ! array_key_exists( 'type', $pro ) ) {
250 return false;
251 }
252
253 $license_type = strtolower( $pro['type'] );
254 $args = array(
255 'license_type' => $license_type,
256 'plan_required' => $this->get_required_license(),
257 );
258 if ( ! FrmFormsHelper::plan_is_allowed( $args ) ) {
259 return false;
260 }
261
262 return true;
263 }
264
265 /**
266 * Check if an application template is new. If it is, we include a "NEW" pill beside the title.
267 *
268 * @since 6.0
269 *
270 * @return bool
271 */
272 private function is_new() {
273 return ! empty( $this->api_data['is_new'] );
274 }
275
276 /**
277 * @return string
278 */
279 private function get_required_license() {
280 $required_license = strtolower( $this->api_data['min_plan'] );
281 if ( 'plus' === $required_license ) {
282 $required_license = 'personal';
283 }
284 return $required_license;
285 }
286
287 /**
288 * @return string
289 */
290 private function get_admin_upgrade_link() {
291 return FrmAppHelper::admin_upgrade_link(
292 array(
293 'content' => 'upgrade',
294 'medium' => 'applications',
295 ),
296 'view-templates/' . $this->api_data['slug']
297 );
298 }
299 }
300