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

237 lines 5.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 $keys
13 */
14 private static $keys;
15
16 /**
17 * @var array<string>|null $keys_with_images
18 */
19 private static $keys_with_images;
20
21 /**
22 * @var array<string>|null $categories
23 */
24 private static $categories;
25
26 /**
27 * @var array $api_data
28 */
29 private $api_data;
30
31 /**
32 * @param array<string> $keys
33 * @param array<string> $keys_with_images
34 */
35 public static function init() {
36 /**
37 * @since 5.3
38 *
39 * @param array $keys
40 */
41 self::$keys = apply_filters(
42 'frm_application_data_keys',
43 array( 'key', 'name', 'description', 'link', 'categories', 'views', 'forms' )
44 );
45 self::$keys_with_images = self::get_template_keys_with_local_images();
46 self::$categories = array();
47 }
48
49 /**
50 * Return the template keys that have embedded images. Otherwise, we want to avoid trying to load the URL and use the placeholder instead.
51 *
52 * @return array<string>
53 */
54 private static function get_template_keys_with_local_images() {
55 return array(
56 'business-hours',
57 'faq-template-wordpress',
58 'restaurant-menu',
59 'team-directory',
60 'product-review',
61 'real-estate-listings',
62 );
63 }
64
65 /**
66 * @param array $api_data
67 * @return void
68 */
69 public function __construct( $api_data ) {
70 $this->api_data = $api_data;
71
72 if ( ! empty( $api_data['categories'] ) ) {
73 self::populate_category_information( $api_data['categories'] );
74 }
75 }
76
77 /**
78 * @param array<string> $categories
79 * @return void
80 */
81 private static function populate_category_information( $categories ) {
82 foreach ( $categories as $category ) {
83 if ( self::category_matches_a_license_type( $category ) ) {
84 continue;
85 }
86 if ( in_array( $category, self::$categories, true ) ) {
87 continue;
88 }
89 self::$categories[] = $category;
90 }
91 }
92
93 /**
94 * @since 5.5.2
95 *
96 * @param string $category
97 * @return bool
98 */
99 private static function category_matches_a_license_type( $category ) {
100 if ( false !== strpos( $category, '+Views' ) ) {
101 return true;
102 }
103 return in_array( $category, FrmFormsHelper::ignore_template_categories(), true );
104 }
105
106 /**
107 * @return array<string>
108 */
109 public static function get_categories() {
110 return isset( self::$categories ) ? self::$categories : array();
111 }
112
113 /**
114 * @return array
115 */
116 public function as_js_object() {
117 $application = array();
118 foreach ( self::$keys as $key ) {
119 if ( ! isset( $this->api_data[ $key ] ) ) {
120 continue;
121 }
122
123 $value = $this->api_data[ $key ];
124
125 if ( 'icon' === $key ) {
126 // Icon is an array. The first array item is the image URL.
127 $application[ $key ] = reset( $value );
128 } elseif ( 'categories' === $key ) {
129 $application[ $key ] = array_values(
130 array_filter(
131 $value,
132 function( $category ) {
133 return false === strpos( $category, '+Views' );
134 }
135 )
136 );
137 } else {
138 if ( 'views' === $key ) {
139 $key = 'viewCount';
140 } elseif ( 'forms' === $key ) {
141 $key = 'formCount';
142 }
143
144 if ( 'name' === $key && ' Template' === substr( $value, -9 ) ) {
145 // Strip off the " Template" text at the end of the name as it takes up space.
146 $value = substr( $value, 0, -9 );
147 }
148 $application[ $key ] = $value;
149 }
150 }
151
152 $application['hasLiteThumbnail'] = in_array( $application['key'], self::$keys_with_images, true );
153
154 if ( ! array_key_exists( 'url', $application ) ) {
155 $purchase_url = $this->is_available_for_purchase();
156 if ( false !== $purchase_url ) {
157 $application['forPurchase'] = true;
158 }
159 $application['upgradeUrl'] = $this->get_admin_upgrade_link();
160 $application['requires'] = FrmFormsHelper::get_plan_required( $application );
161 $application['link'] = $application['upgradeUrl'];
162 }
163
164 $application['isNew'] = $this->is_new();
165
166 return $application;
167 }
168
169 /**
170 * @return bool
171 */
172 private function is_available_for_purchase() {
173 if ( ! array_key_exists( 'min_plan', $this->api_data ) ) {
174 return false;
175 }
176
177 $license_type = '';
178 $api = new FrmFormApi();
179 $addons = $api->get_api_info();
180
181 if ( ! array_key_exists( 93790, $addons ) ) {
182 return false;
183 }
184
185 $pro = $addons[93790];
186 if ( ! array_key_exists( 'type', $pro ) ) {
187 return false;
188 }
189
190 $license_type = strtolower( $pro['type'] );
191 $args = array(
192 'license_type' => $license_type,
193 'plan_required' => $this->get_required_license(),
194 );
195 if ( ! FrmFormsHelper::plan_is_allowed( $args ) ) {
196 return false;
197 }
198
199 return true;
200 }
201
202 /**
203 * Check if an application template is new. If it is, we include a "NEW" pill beside the title.
204 *
205 * @since 6.0
206 *
207 * @return bool
208 */
209 private function is_new() {
210 return ! empty( $this->api_data['is_new'] );
211 }
212
213 /**
214 * @return string
215 */
216 private function get_required_license() {
217 $required_license = strtolower( $this->api_data['min_plan'] );
218 if ( 'plus' === $required_license ) {
219 $required_license = 'personal';
220 }
221 return $required_license;
222 }
223
224 /**
225 * @return string
226 */
227 private function get_admin_upgrade_link() {
228 return FrmAppHelper::admin_upgrade_link(
229 array(
230 'content' => 'upgrade',
231 'medium' => 'applications',
232 ),
233 '/view-templates/' . $this->api_data['slug']
234 );
235 }
236 }
237