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

240 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
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 = 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 if ( ! is_array( self::$keys ) ) {
117 return array();
118 }
119
120 $application = array();
121 foreach ( self::$keys as $key ) {
122 if ( ! isset( $this->api_data[ $key ] ) ) {
123 continue;
124 }
125
126 $value = $this->api_data[ $key ];
127
128 if ( 'icon' === $key ) {
129 // Icon is an array. The first array item is the image URL.
130 $application[ $key ] = reset( $value );
131 } elseif ( 'categories' === $key ) {
132 $application[ $key ] = array_values(
133 array_filter(
134 $value,
135 function ( $category ) {
136 return false === strpos( $category, '+Views' );
137 }
138 )
139 );
140 } else {
141 if ( 'views' === $key ) {
142 $key = 'viewCount';
143 } elseif ( 'forms' === $key ) {
144 $key = 'formCount';
145 }
146
147 if ( 'name' === $key && ' Template' === substr( $value, -9 ) ) {
148 // Strip off the " Template" text at the end of the name as it takes up space.
149 $value = substr( $value, 0, -9 );
150 }
151 $application[ $key ] = $value;
152 }//end if
153 }//end foreach
154
155 $application['hasLiteThumbnail'] = in_array( $application['key'], self::$keys_with_images, true );
156
157 if ( ! array_key_exists( 'url', $application ) ) {
158 $purchase_url = $this->is_available_for_purchase();
159 if ( false !== $purchase_url ) {
160 $application['forPurchase'] = true;
161 }
162 $application['upgradeUrl'] = $this->get_admin_upgrade_link();
163 $application['requires'] = FrmFormsHelper::get_plan_required( $application );
164 $application['link'] = $application['upgradeUrl'];
165 }
166
167 $application['isNew'] = $this->is_new();
168
169 return $application;
170 }
171
172 /**
173 * @return bool
174 */
175 private function is_available_for_purchase() {
176 if ( ! array_key_exists( 'min_plan', $this->api_data ) ) {
177 return false;
178 }
179
180 $license_type = '';
181 $api = new FrmFormApi();
182 $addons = $api->get_api_info();
183
184 if ( ! array_key_exists( 93790, $addons ) ) {
185 return false;
186 }
187
188 $pro = $addons[93790];
189 if ( ! array_key_exists( 'type', $pro ) ) {
190 return false;
191 }
192
193 $license_type = strtolower( $pro['type'] );
194 $args = array(
195 'license_type' => $license_type,
196 'plan_required' => $this->get_required_license(),
197 );
198 if ( ! FrmFormsHelper::plan_is_allowed( $args ) ) {
199 return false;
200 }
201
202 return true;
203 }
204
205 /**
206 * Check if an application template is new. If it is, we include a "NEW" pill beside the title.
207 *
208 * @since 6.0
209 *
210 * @return bool
211 */
212 private function is_new() {
213 return ! empty( $this->api_data['is_new'] );
214 }
215
216 /**
217 * @return string
218 */
219 private function get_required_license() {
220 $required_license = strtolower( $this->api_data['min_plan'] );
221 if ( 'plus' === $required_license ) {
222 $required_license = 'personal';
223 }
224 return $required_license;
225 }
226
227 /**
228 * @return string
229 */
230 private function get_admin_upgrade_link() {
231 return FrmAppHelper::admin_upgrade_link(
232 array(
233 'content' => 'upgrade',
234 'medium' => 'applications',
235 ),
236 '/view-templates/' . $this->api_data['slug']
237 );
238 }
239 }
240