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

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