| 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 |
* @return void |
| 36 |
*/ |
| 37 |
public static function init() { |
| 38 |
/** |
| 39 |
* @since 5.3 |
| 40 |
* |
| 41 |
* @param array $keys |
| 42 |
*/ |
| 43 |
self::$keys = apply_filters( |
| 44 |
'frm_application_data_keys', |
| 45 |
array( 'key', 'name', 'description', 'link', 'categories', 'views', 'forms' ) |
| 46 |
); |
| 47 |
self::$keys_with_images = self::get_template_keys_with_local_images(); |
| 48 |
self::$categories = array(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Return the template keys that have embedded images. Otherwise, we want to avoid trying to load the URL and use the placeholder instead. |
| 53 |
* |
| 54 |
* @return array<string> |
| 55 |
*/ |
| 56 |
private static function get_template_keys_with_local_images() { |
| 57 |
return array( |
| 58 |
'business-hours', |
| 59 |
'faq-template-wordpress', |
| 60 |
'restaurant-menu', |
| 61 |
'team-directory', |
| 62 |
'product-review', |
| 63 |
'real-estate-listings', |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param array $api_data |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function __construct( $api_data ) { |
| 72 |
$this->api_data = $api_data; |
| 73 |
|
| 74 |
if ( ! empty( $api_data['categories'] ) ) { |
| 75 |
self::populate_category_information( $api_data['categories'] ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param array<string> $categories |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
private static function populate_category_information( $categories ) { |
| 84 |
foreach ( $categories as $category ) { |
| 85 |
if ( self::category_matches_a_license_type( $category ) ) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
if ( in_array( $category, self::$categories, true ) ) { |
| 89 |
continue; |
| 90 |
} |
| 91 |
self::$categories[] = $category; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @since 5.5.2 |
| 97 |
* |
| 98 |
* @param string $category |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
private static function category_matches_a_license_type( $category ) { |
| 102 |
if ( false !== strpos( $category, '+Views' ) ) { |
| 103 |
return true; |
| 104 |
} |
| 105 |
return in_array( $category, FrmFormsHelper::ignore_template_categories(), true ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @return array<string> |
| 110 |
*/ |
| 111 |
public static function get_categories() { |
| 112 |
return isset( self::$categories ) ? self::$categories : array(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function as_js_object() { |
| 119 |
$application = array(); |
| 120 |
foreach ( self::$keys as $key ) { |
| 121 |
if ( ! isset( $this->api_data[ $key ] ) ) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
$value = $this->api_data[ $key ]; |
| 126 |
|
| 127 |
if ( 'icon' === $key ) { |
| 128 |
// Icon is an array. The first array item is the image URL. |
| 129 |
$application[ $key ] = reset( $value ); |
| 130 |
} elseif ( 'categories' === $key ) { |
| 131 |
$application[ $key ] = array_values( |
| 132 |
array_filter( |
| 133 |
$value, |
| 134 |
function( $category ) { |
| 135 |
return false === strpos( $category, '+Views' ); |
| 136 |
} |
| 137 |
) |
| 138 |
); |
| 139 |
} else { |
| 140 |
if ( 'views' === $key ) { |
| 141 |
$key = 'viewCount'; |
| 142 |
} elseif ( 'forms' === $key ) { |
| 143 |
$key = 'formCount'; |
| 144 |
} |
| 145 |
|
| 146 |
if ( 'name' === $key && ' Template' === substr( $value, -9 ) ) { |
| 147 |
// Strip off the " Template" text at the end of the name as it takes up space. |
| 148 |
$value = substr( $value, 0, -9 ); |
| 149 |
} |
| 150 |
$application[ $key ] = $value; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
$application['hasLiteThumbnail'] = in_array( $application['key'], self::$keys_with_images, true ); |
| 155 |
|
| 156 |
if ( ! array_key_exists( 'url', $application ) ) { |
| 157 |
$purchase_url = $this->is_available_for_purchase(); |
| 158 |
if ( false !== $purchase_url ) { |
| 159 |
$application['forPurchase'] = true; |
| 160 |
} |
| 161 |
$application['upgradeUrl'] = $this->get_admin_upgrade_link(); |
| 162 |
$application['requires'] = FrmFormsHelper::get_plan_required( $application ); |
| 163 |
$application['link'] = $application['upgradeUrl']; |
| 164 |
} |
| 165 |
|
| 166 |
$application['isNew'] = $this->is_new(); |
| 167 |
|
| 168 |
return $application; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* @return bool |
| 173 |
*/ |
| 174 |
private function is_available_for_purchase() { |
| 175 |
if ( ! array_key_exists( 'min_plan', $this->api_data ) ) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
$license_type = ''; |
| 180 |
$api = new FrmFormApi(); |
| 181 |
$addons = $api->get_api_info(); |
| 182 |
|
| 183 |
if ( ! array_key_exists( 93790, $addons ) ) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
$pro = $addons[93790]; |
| 188 |
if ( ! array_key_exists( 'type', $pro ) ) { |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
$license_type = strtolower( $pro['type'] ); |
| 193 |
$args = array( |
| 194 |
'license_type' => $license_type, |
| 195 |
'plan_required' => $this->get_required_license(), |
| 196 |
); |
| 197 |
if ( ! FrmFormsHelper::plan_is_allowed( $args ) ) { |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Check if an application template is new. If it is, we include a "NEW" pill beside the title. |
| 206 |
* |
| 207 |
* @since 6.0 |
| 208 |
* |
| 209 |
* @return bool |
| 210 |
*/ |
| 211 |
private function is_new() { |
| 212 |
return ! empty( $this->api_data['is_new'] ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
private function get_required_license() { |
| 219 |
$required_license = strtolower( $this->api_data['min_plan'] ); |
| 220 |
if ( 'plus' === $required_license ) { |
| 221 |
$required_license = 'personal'; |
| 222 |
} |
| 223 |
return $required_license; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* @return string |
| 228 |
*/ |
| 229 |
private function get_admin_upgrade_link() { |
| 230 |
return FrmAppHelper::admin_upgrade_link( |
| 231 |
array( |
| 232 |
'content' => 'upgrade', |
| 233 |
'medium' => 'applications', |
| 234 |
), |
| 235 |
'/view-templates/' . $this->api_data['slug'] |
| 236 |
); |
| 237 |
} |
| 238 |
} |
| 239 |
|