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

224 lines 5.0 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 return $application;
165 }
166
167 /**
168 * @return bool
169 */
170 private function is_available_for_purchase() {
171 if ( ! array_key_exists( 'min_plan', $this->api_data ) ) {
172 return false;
173 }
174
175 $license_type = '';
176 $api = new FrmFormApi();
177 $addons = $api->get_api_info();
178
179 if ( ! array_key_exists( 93790, $addons ) ) {
180 return false;
181 }
182
183 $pro = $addons[93790];
184 if ( ! array_key_exists( 'type', $pro ) ) {
185 return false;
186 }
187
188 $license_type = strtolower( $pro['type'] );
189 $args = array(
190 'license_type' => $license_type,
191 'plan_required' => $this->get_required_license(),
192 );
193 if ( ! FrmFormsHelper::plan_is_allowed( $args ) ) {
194 return false;
195 }
196
197 return true;
198 }
199
200 /**
201 * @return string
202 */
203 private function get_required_license() {
204 $required_license = strtolower( $this->api_data['min_plan'] );
205 if ( 'plus' === $required_license ) {
206 $required_license = 'personal';
207 }
208 return $required_license;
209 }
210
211 /**
212 * @return string
213 */
214 private function get_admin_upgrade_link() {
215 return FrmAppHelper::admin_upgrade_link(
216 array(
217 'content' => 'upgrade',
218 'medium' => 'applications',
219 ),
220 '/view-templates/' . $this->api_data['slug']
221 );
222 }
223 }
224