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

266 lines 5.8 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' )
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 $purchase_url = $this->is_available_for_purchase();
185 if ( false !== $purchase_url ) {
186 $application['forPurchase'] = true;
187 }
188 $application['upgradeUrl'] = $this->get_admin_upgrade_link();
189 $application['requires'] = FrmFormsHelper::get_plan_required( $application );
190 $application['link'] = $application['upgradeUrl'];
191 }
192
193 $application['isNew'] = $this->is_new();
194
195 return $application;
196 }
197
198 /**
199 * @return bool
200 */
201 private function is_available_for_purchase() {
202 if ( ! array_key_exists( 'min_plan', $this->api_data ) ) {
203 return false;
204 }
205
206 $license_type = '';
207 $api = new FrmFormApi();
208 $addons = $api->get_api_info();
209
210 if ( ! array_key_exists( 93790, $addons ) ) {
211 return false;
212 }
213
214 $pro = $addons[93790];
215 if ( ! array_key_exists( 'type', $pro ) ) {
216 return false;
217 }
218
219 $license_type = strtolower( $pro['type'] );
220 $args = array(
221 'license_type' => $license_type,
222 'plan_required' => $this->get_required_license(),
223 );
224 if ( ! FrmFormsHelper::plan_is_allowed( $args ) ) {
225 return false;
226 }
227
228 return true;
229 }
230
231 /**
232 * Check if an application template is new. If it is, we include a "NEW" pill beside the title.
233 *
234 * @since 6.0
235 *
236 * @return bool
237 */
238 private function is_new() {
239 return ! empty( $this->api_data['is_new'] );
240 }
241
242 /**
243 * @return string
244 */
245 private function get_required_license() {
246 $required_license = strtolower( $this->api_data['min_plan'] );
247 if ( 'plus' === $required_license ) {
248 $required_license = 'personal';
249 }
250 return $required_license;
251 }
252
253 /**
254 * @return string
255 */
256 private function get_admin_upgrade_link() {
257 return FrmAppHelper::admin_upgrade_link(
258 array(
259 'content' => 'upgrade',
260 'medium' => 'applications',
261 ),
262 'view-templates/' . $this->api_data['slug']
263 );
264 }
265 }
266