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

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