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

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