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

207 lines 4.5 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 ( false !== strpos( $category, '+Views' ) || in_array( $category, self::$categories, true ) ) {
84 continue;
85 }
86 self::$categories[] = $category;
87 }
88 }
89
90 /**
91 * @return array<string>
92 */
93 public static function get_categories() {
94 return isset( self::$categories ) ? self::$categories : array();
95 }
96
97 /**
98 * @return array
99 */
100 public function as_js_object() {
101 $application = array();
102 foreach ( self::$keys as $key ) {
103 if ( ! isset( $this->api_data[ $key ] ) ) {
104 continue;
105 }
106
107 $value = $this->api_data[ $key ];
108
109 if ( 'icon' === $key ) {
110 // Icon is an array. The first array item is the image URL.
111 $application[ $key ] = reset( $value );
112 } elseif ( 'categories' === $key ) {
113 $application[ $key ] = array_values(
114 array_filter(
115 $value,
116 function( $category ) {
117 return false === strpos( $category, '+Views' );
118 }
119 )
120 );
121 } else {
122 if ( 'views' === $key ) {
123 $key = 'viewCount';
124 } elseif ( 'forms' === $key ) {
125 $key = 'formCount';
126 }
127
128 if ( 'name' === $key && ' Template' === substr( $value, -9 ) ) {
129 // Strip off the " Template" text at the end of the name as it takes up space.
130 $value = substr( $value, 0, -9 );
131 }
132 $application[ $key ] = $value;
133 }
134 }
135
136 $application['hasLiteThumbnail'] = in_array( $application['key'], self::$keys_with_images, true );
137
138 if ( ! array_key_exists( 'url', $application ) ) {
139 $purchase_url = $this->is_available_for_purchase();
140 if ( false !== $purchase_url ) {
141 $application['forPurchase'] = true;
142 }
143 $application['upgradeUrl'] = $this->get_admin_upgrade_link();
144 $application['link'] = $application['upgradeUrl'];
145 }
146
147 return $application;
148 }
149
150 /**
151 * @return bool
152 */
153 private function is_available_for_purchase() {
154 if ( ! array_key_exists( 'min_plan', $this->api_data ) ) {
155 return false;
156 }
157
158 $license_type = '';
159 $api = new FrmFormApi();
160 $addons = $api->get_api_info();
161
162 if ( ! array_key_exists( 93790, $addons ) ) {
163 return false;
164 }
165
166 $pro = $addons[93790];
167 if ( ! array_key_exists( 'type', $pro ) ) {
168 return false;
169 }
170
171 $license_type = strtolower( $pro['type'] );
172 $args = array(
173 'license_type' => $license_type,
174 'plan_required' => $this->get_required_license(),
175 );
176 if ( ! FrmFormsHelper::plan_is_allowed( $args ) ) {
177 return false;
178 }
179
180 return true;
181 }
182
183 /**
184 * @return string
185 */
186 private function get_required_license() {
187 $required_license = strtolower( $this->api_data['min_plan'] );
188 if ( 'plus' === $required_license ) {
189 $required_license = 'personal';
190 }
191 return $required_license;
192 }
193
194 /**
195 * @return string
196 */
197 private function get_admin_upgrade_link() {
198 return FrmAppHelper::admin_upgrade_link(
199 array(
200 'content' => 'upgrade',
201 'medium' => 'applications',
202 ),
203 '/view-templates/' . $this->api_data['slug']
204 );
205 }
206 }
207