PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.21
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.21
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.21, at classes/models/FrmApplicationTemplate.php

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