| 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', 'used_addons' ) |
| 43 |
); |
| 44 |
self::$keys_with_images = array_merge( |
| 45 |
self::get_template_keys_with_local_png_images(), |
| 46 |
self::get_template_keys_with_local_webp_images() |
| 47 |
); |
| 48 |
self::$categories = array(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Newer templates now use .webp files instead of .png. |
| 53 |
* |
| 54 |
* @since 6.16 |
| 55 |
* |
| 56 |
* @return array<string> |
| 57 |
*/ |
| 58 |
private static function get_template_keys_with_local_webp_images() { |
| 59 |
return array( |
| 60 |
'member-directory', |
| 61 |
'link-in-bio-instagram', |
| 62 |
'letter-of-recommendation', |
| 63 |
'invoice-pdf', |
| 64 |
'freelance-invoice-generator', |
| 65 |
'contract-agreement', |
| 66 |
'charity-tracker', |
| 67 |
'certificate', |
| 68 |
'testimonials', |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Return the template keys that have embedded images. Otherwise, we want to avoid trying to load the URL and use the placeholder instead. |
| 74 |
* |
| 75 |
* @return array<string> |
| 76 |
*/ |
| 77 |
private static function get_template_keys_with_local_png_images() { |
| 78 |
return array( |
| 79 |
'business-hours', |
| 80 |
'faq-template-wordpress', |
| 81 |
'restaurant-menu', |
| 82 |
'team-directory', |
| 83 |
'product-review', |
| 84 |
'real-estate-listings', |
| 85 |
'business-directory', |
| 86 |
'artist-gallery', |
| 87 |
'classifieds', |
| 88 |
'community-event-calendar', |
| 89 |
'company-timeline', |
| 90 |
'crowdfunding', |
| 91 |
'job-board', |
| 92 |
'logo-cloud', |
| 93 |
'multi-property-booking-system', |
| 94 |
'wedding-venue-booking', |
| 95 |
'permission-slip', |
| 96 |
'property-directory', |
| 97 |
'radio-station', |
| 98 |
'recipe', |
| 99 |
'rsvp-invite', |
| 100 |
'searchable-directory', |
| 101 |
'shopping-cart', |
| 102 |
'sports-league-calendar', |
| 103 |
'staff-directory', |
| 104 |
'tiered-pricing', |
| 105 |
'trip-itinerary', |
| 106 |
'twitterx', |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @param array $api_data |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function __construct( $api_data ) { |
| 116 |
$this->api_data = $api_data; |
| 117 |
|
| 118 |
if ( ! empty( $api_data['categories'] ) ) { |
| 119 |
self::populate_category_information( $api_data['categories'] ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @param array<string> $categories |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
private static function populate_category_information( $categories ) { |
| 129 |
foreach ( $categories as $category ) { |
| 130 |
if ( self::category_matches_a_license_type( $category ) ) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
if ( in_array( $category, self::$categories, true ) ) { |
| 135 |
continue; |
| 136 |
} |
| 137 |
self::$categories[] = $category; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @since 5.5.2 |
| 143 |
* |
| 144 |
* @param string $category |
| 145 |
* |
| 146 |
* @return bool |
| 147 |
*/ |
| 148 |
private static function category_matches_a_license_type( $category ) { |
| 149 |
if ( str_contains( $category, '+Views' ) ) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
return in_array( $category, FrmFormsHelper::get_license_types(), true ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @return array<string> |
| 157 |
*/ |
| 158 |
public static function get_categories() { |
| 159 |
return self::$categories ?? array(); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* @return array |
| 164 |
*/ |
| 165 |
public function as_js_object() { |
| 166 |
if ( ! is_array( self::$keys ) ) { |
| 167 |
return array(); |
| 168 |
} |
| 169 |
|
| 170 |
$application = array(); |
| 171 |
|
| 172 |
foreach ( self::$keys as $key ) { |
| 173 |
if ( ! isset( $this->api_data[ $key ] ) ) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
|
| 177 |
$value = $this->api_data[ $key ]; |
| 178 |
|
| 179 |
if ( 'icon' === $key ) { |
| 180 |
// Icon is an array. The first array item is the image URL. |
| 181 |
$application[ $key ] = reset( $value ); |
| 182 |
} elseif ( 'categories' === $key ) { |
| 183 |
$application[ $key ] = array_values( |
| 184 |
array_filter( |
| 185 |
$value, |
| 186 |
function ( $category ) { |
| 187 |
return ! str_contains( $category, '+Views' ); |
| 188 |
} |
| 189 |
) |
| 190 |
); |
| 191 |
} else { |
| 192 |
if ( 'views' === $key ) { |
| 193 |
$key = 'viewCount'; |
| 194 |
} elseif ( 'forms' === $key ) { |
| 195 |
$key = 'formCount'; |
| 196 |
} |
| 197 |
|
| 198 |
if ( 'name' === $key && str_ends_with( $value, ' Template' ) ) { |
| 199 |
// Strip off the " Template" text at the end of the name as it takes up space. |
| 200 |
$value = substr( $value, 0, -9 ); |
| 201 |
} |
| 202 |
|
| 203 |
$application[ $key ] = $value; |
| 204 |
}//end if |
| 205 |
}//end foreach |
| 206 |
|
| 207 |
$application['hasLiteThumbnail'] = in_array( $application['key'], self::$keys_with_images, true ); |
| 208 |
$application['isWebp'] = in_array( $application['key'], self::get_template_keys_with_local_webp_images(), true ); |
| 209 |
|
| 210 |
if ( ! array_key_exists( 'url', $application ) ) { |
| 211 |
$application['requires'] = FrmFormsHelper::get_plan_required( $application ); |
| 212 |
|
| 213 |
if ( false === $application['requires'] ) { |
| 214 |
// Application is invalid if the URL is unavailable and there is no plan required. |
| 215 |
return array(); |
| 216 |
} |
| 217 |
|
| 218 |
$purchase_url = $this->is_available_for_purchase(); |
| 219 |
|
| 220 |
if ( false !== $purchase_url ) { |
| 221 |
$application['forPurchase'] = true; |
| 222 |
} |
| 223 |
|
| 224 |
$application['upgradeUrl'] = $this->get_admin_upgrade_link(); |
| 225 |
$application['link'] = $application['upgradeUrl']; |
| 226 |
} |
| 227 |
|
| 228 |
$application['isNew'] = $this->is_new(); |
| 229 |
|
| 230 |
$application['usedAddons'] = array(); |
| 231 |
|
| 232 |
if ( isset( $application['used_addons'] ) ) { |
| 233 |
// Change key to camel case. |
| 234 |
$application['usedAddons'] = $application['used_addons']; |
| 235 |
unset( $application['used_addons'] ); |
| 236 |
} |
| 237 |
|
| 238 |
return $application; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* @return bool |
| 243 |
*/ |
| 244 |
private function is_available_for_purchase() { |
| 245 |
if ( ! array_key_exists( 'min_plan', $this->api_data ) ) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
$license_type = ''; |
| 250 |
$api = new FrmFormApi(); |
| 251 |
$addons = $api->get_api_info(); |
| 252 |
|
| 253 |
if ( ! array_key_exists( 93790, $addons ) ) { |
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
$pro = $addons[93790]; |
| 258 |
|
| 259 |
if ( ! array_key_exists( 'type', $pro ) ) { |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
$license_type = strtolower( $pro['type'] ); |
| 264 |
$args = array( |
| 265 |
'license_type' => $license_type, |
| 266 |
'plan_required' => $this->get_required_license(), |
| 267 |
); |
| 268 |
return FrmFormsHelper::plan_is_allowed( $args ); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Check if an application template is new. If it is, we include a "NEW" pill beside the title. |
| 273 |
* |
| 274 |
* @since 6.0 |
| 275 |
* |
| 276 |
* @return bool |
| 277 |
*/ |
| 278 |
private function is_new() { |
| 279 |
return ! empty( $this->api_data['is_new'] ); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* @return string |
| 284 |
*/ |
| 285 |
private function get_required_license() { |
| 286 |
$required_license = strtolower( $this->api_data['min_plan'] ); |
| 287 |
return 'plus' === $required_license ? 'personal' : $required_license; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* @return string |
| 292 |
*/ |
| 293 |
private function get_admin_upgrade_link() { |
| 294 |
return FrmAppHelper::admin_upgrade_link( |
| 295 |
array( |
| 296 |
'content' => 'upgrade', |
| 297 |
'medium' => 'applications', |
| 298 |
), |
| 299 |
'view-templates/' . $this->api_data['slug'] |
| 300 |
); |
| 301 |
} |
| 302 |
} |
| 303 |
|