| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form Templates Controller class. |
| 4 |
* |
| 5 |
* @package Formidable |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
die( 'You are not allowed to call this page directly.' ); |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class FrmFormTemplatesController. |
| 14 |
* Handles the Form Templates page in the admin area. |
| 15 |
* |
| 16 |
* @since 6.7 |
| 17 |
*/ |
| 18 |
class FrmFormTemplatesController { |
| 19 |
|
| 20 |
/** |
| 21 |
* The slug of the Form Templates page. |
| 22 |
* |
| 23 |
* @var string Unique identifier for the "Form Templates" page. |
| 24 |
*/ |
| 25 |
const PAGE_SLUG = 'formidable-form-templates'; |
| 26 |
|
| 27 |
/** |
| 28 |
* The script handle. |
| 29 |
* |
| 30 |
* @var string Unique handle for the admin script. |
| 31 |
*/ |
| 32 |
const SCRIPT_HANDLE = 'frm-form-templates'; |
| 33 |
|
| 34 |
/** |
| 35 |
* The required user capability to view form templates. |
| 36 |
* |
| 37 |
* @var string Required capability to access the view form templates. |
| 38 |
*/ |
| 39 |
const REQUIRED_CAPABILITY = 'frm_view_forms'; |
| 40 |
|
| 41 |
/** |
| 42 |
* The IDs of the featured templates. |
| 43 |
* |
| 44 |
* Contains the unique IDs for the templates that are considered "featured": |
| 45 |
* "Contact Us", "Stripe Payment", "User Registration", "Create WordPress Post", "Survey", and "Quiz". |
| 46 |
* |
| 47 |
* @var array Unique IDs for the featured templates. |
| 48 |
*/ |
| 49 |
const FEATURED_TEMPLATES_IDS = array( 20872734, 28223640, 20874748, 20882522, 20908981, 28109851 ); |
| 50 |
|
| 51 |
/** |
| 52 |
* The IDs of the free templates. |
| 53 |
* |
| 54 |
* @var array Unique IDs for the free templates. |
| 55 |
*/ |
| 56 |
const FREE_TEMPLATES_IDS = array( 20872734, 28223640 ); |
| 57 |
|
| 58 |
/** |
| 59 |
* Option name to store favorite templates. |
| 60 |
* |
| 61 |
* @var string Unique identifier for storing favorite templates. |
| 62 |
*/ |
| 63 |
const FAVORITE_TEMPLATES_OPTION = 'frm_favorite_templates'; |
| 64 |
|
| 65 |
/** |
| 66 |
* Instance of the Form Template API handler. |
| 67 |
* |
| 68 |
* @var FrmFormTemplateApi Form Template API handler. |
| 69 |
*/ |
| 70 |
private static $form_template_api; |
| 71 |
|
| 72 |
/** |
| 73 |
* Templates fetched from the API. |
| 74 |
* |
| 75 |
* @var array Templates information from API. |
| 76 |
*/ |
| 77 |
private static $templates = array(); |
| 78 |
|
| 79 |
/** |
| 80 |
* Featured templates. |
| 81 |
* |
| 82 |
* @var array Associative array with the featured templates' information. |
| 83 |
*/ |
| 84 |
private static $featured_templates = array(); |
| 85 |
|
| 86 |
/** |
| 87 |
* List of user favorite templates. |
| 88 |
* |
| 89 |
* @var array List of templates that the user has marked as favorites. |
| 90 |
*/ |
| 91 |
private static $favorite_templates = array(); |
| 92 |
|
| 93 |
/** |
| 94 |
* Templates fetched from the published form by user. |
| 95 |
* |
| 96 |
* @var array Templates information from published form. |
| 97 |
*/ |
| 98 |
private static $custom_templates = array(); |
| 99 |
|
| 100 |
/** |
| 101 |
* Categories for organizing templates. |
| 102 |
* |
| 103 |
* @var array Categories for organizing templates. |
| 104 |
*/ |
| 105 |
private static $categories = array(); |
| 106 |
|
| 107 |
/** |
| 108 |
* Status of API request, true if expired. |
| 109 |
* |
| 110 |
* @var bool Whether the API request is expired or not. |
| 111 |
*/ |
| 112 |
private static $is_expired = false; |
| 113 |
|
| 114 |
/** |
| 115 |
* The type of license received from the API. |
| 116 |
* |
| 117 |
* @var string License type received from the API. |
| 118 |
*/ |
| 119 |
private static $license_type = ''; |
| 120 |
|
| 121 |
/** |
| 122 |
* Path to views. |
| 123 |
* |
| 124 |
* @var string Path to form templates views. |
| 125 |
*/ |
| 126 |
private static $view_path = ''; |
| 127 |
|
| 128 |
/** |
| 129 |
* Upgrade URL. |
| 130 |
* |
| 131 |
* @var string URL for upgrading accounts. |
| 132 |
*/ |
| 133 |
private static $upgrade_link = ''; |
| 134 |
|
| 135 |
/** |
| 136 |
* Renew URL. |
| 137 |
* |
| 138 |
* @var string URL for renewing accounts. |
| 139 |
*/ |
| 140 |
private static $renew_link = ''; |
| 141 |
|
| 142 |
/** |
| 143 |
* Initialize hooks for template page only. |
| 144 |
* |
| 145 |
* @since 6.7 |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public static function load_admin_hooks() { |
| 150 |
self::init_template_resources(); |
| 151 |
|
| 152 |
// Use the same priority as Applications so Form Templates appear directly under Applications. |
| 153 |
add_action( 'admin_menu', self::class . '::menu', 14 ); |
| 154 |
add_action( 'admin_footer', self::class . '::render_modal' ); |
| 155 |
add_filter( 'frm_form_nav_list', self::class . '::append_new_template_to_nav', 10, 2 ); |
| 156 |
|
| 157 |
if ( self::is_templates_page() ) { |
| 158 |
add_action( 'admin_init', self::class . '::set_form_templates_data' ); |
| 159 |
add_action( 'admin_enqueue_scripts', self::class . '::enqueue_assets', 15 ); |
| 160 |
add_filter( 'frm_show_footer_links', '__return_false' ); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Add Form Templates menu item to sidebar and define index page. |
| 166 |
* |
| 167 |
* @since 6.7 |
| 168 |
* |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public static function menu() { |
| 172 |
$label = __( 'Form Templates', 'formidable' ); |
| 173 |
|
| 174 |
add_submenu_page( |
| 175 |
'formidable', |
| 176 |
'Formidable | ' . $label, |
| 177 |
$label, |
| 178 |
self::REQUIRED_CAPABILITY, |
| 179 |
self::PAGE_SLUG, |
| 180 |
array( self::class, 'render' ) |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Renders the Form Templates page in the WordPress admin area. |
| 186 |
* |
| 187 |
* Sets up template data, fetches relevant information, determines which blocks to render, |
| 188 |
* and includes the view file for displaying the Form Templates page. |
| 189 |
* |
| 190 |
* @since 6.7 |
| 191 |
* |
| 192 |
* @return void |
| 193 |
*/ |
| 194 |
public static function render() { |
| 195 |
// Include SVG images for icons. |
| 196 |
FrmAppHelper::include_svg(); |
| 197 |
|
| 198 |
$view_path = self::get_view_path(); |
| 199 |
$upgrade_link = self::get_upgrade_link(); |
| 200 |
$renew_link = self::get_renew_link(); |
| 201 |
$license_type = self::get_license_type(); |
| 202 |
$pricing = FrmAppHelper::admin_upgrade_link( 'form-templates' ); |
| 203 |
$expired = self::is_expired(); |
| 204 |
|
| 205 |
// Get various template types and categories. |
| 206 |
$templates = self::get_templates(); |
| 207 |
$favorite_templates = self::get_favorite_templates(); |
| 208 |
$featured_templates = self::get_featured_templates(); |
| 209 |
$custom_templates = self::get_custom_templates(); |
| 210 |
$categories = self::get_categories(); |
| 211 |
|
| 212 |
include $view_path . 'index.php'; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Renders a modal component in the WordPress admin area. |
| 217 |
* |
| 218 |
* @since 6.7 |
| 219 |
* |
| 220 |
* @return void |
| 221 |
*/ |
| 222 |
public static function render_modal() { |
| 223 |
$view_path = self::$view_path; |
| 224 |
$view_parts = array(); |
| 225 |
|
| 226 |
// Check if the current page is the form templates page. |
| 227 |
if ( self::is_templates_page() ) { |
| 228 |
// User and license-related variables. |
| 229 |
$user = wp_get_current_user(); |
| 230 |
$expired = self::is_expired(); |
| 231 |
$upgrade_link = self::get_upgrade_link(); |
| 232 |
$renew_link = self::get_renew_link(); |
| 233 |
$published_forms = self::get_published_forms(); |
| 234 |
|
| 235 |
// Add `create-template` modal view. |
| 236 |
$view_parts[] = 'modals/create-template-modal.php'; |
| 237 |
|
| 238 |
if ( FrmFormTemplatesHelper::needs_get_free_templates_banner() ) { |
| 239 |
$leave_email_args = array( |
| 240 |
'title' => esc_html__( 'Get 30+ Free Form Templates', 'formidable' ), |
| 241 |
'description' => esc_html__( 'Just add your email address and you\'ll get 30+ free form templates to your account.', 'formidable' ), |
| 242 |
'submit_button_text' => esc_html_x( 'Get Templates', 'get free templates modal submit button text', 'formidable' ), |
| 243 |
); |
| 244 |
$view_parts[] = 'modals/leave-email-modal.php'; |
| 245 |
} |
| 246 |
|
| 247 |
// Add 'upgrade' modal view for non-elite users. |
| 248 |
if ( 'elite' !== FrmAddonsController::license_type() ) { |
| 249 |
$view_parts[] = 'modals/upgrade-modal.php'; |
| 250 |
} |
| 251 |
|
| 252 |
// Add 'renew-account' modal view for expired users. |
| 253 |
if ( $expired ) { |
| 254 |
$view_parts[] = 'modals/renew-account-modal.php'; |
| 255 |
} |
| 256 |
}//end if |
| 257 |
|
| 258 |
// Check if the current page is the form builder page. |
| 259 |
if ( FrmAppHelper::is_admin_page( 'formidable' ) ) { |
| 260 |
$action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' ); |
| 261 |
|
| 262 |
if ( 'edit' === $action || 'settings' === $action ) { |
| 263 |
$view_parts[] = 'modals/name-your-form-modal.php'; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
include $view_path . 'modal.php'; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Initializes and organizes form template data by performing the following actions: |
| 272 |
* - Instantiates the Form Template API class |
| 273 |
* - Retrieves and sets templates, including featured ones |
| 274 |
* - Organizes and categorizes templates |
| 275 |
* - Formats custom templates |
| 276 |
* - Updates global variables to reflect the current state |
| 277 |
* |
| 278 |
* @since 6.7 |
| 279 |
* |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
public static function set_form_templates_data() { |
| 283 |
self::$form_template_api = new FrmFormTemplateApi(); |
| 284 |
|
| 285 |
self::init_favorite_templates(); |
| 286 |
self::fetch_and_format_custom_templates(); |
| 287 |
self::retrieve_and_set_templates(); |
| 288 |
self::organize_and_set_categories(); |
| 289 |
self::assign_featured_templates(); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Initialize favorite templates from WordPress options. |
| 294 |
* |
| 295 |
* @since 6.7 |
| 296 |
* |
| 297 |
* @return void |
| 298 |
*/ |
| 299 |
private static function init_favorite_templates() { |
| 300 |
$default_option_structure = array( |
| 301 |
'default' => array(), |
| 302 |
'custom' => array(), |
| 303 |
); |
| 304 |
|
| 305 |
$favorites = get_option( self::FAVORITE_TEMPLATES_OPTION, $default_option_structure ); |
| 306 |
$favorites = wp_parse_args( $favorites, $default_option_structure ); |
| 307 |
|
| 308 |
// Set the favorite templates property and remove empty values. |
| 309 |
self::$favorite_templates = array( |
| 310 |
'default' => array_filter( (array) $favorites['default'] ), |
| 311 |
'custom' => array_filter( (array) $favorites['custom'] ), |
| 312 |
); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Handle AJAX request to add or remove favorite templates. |
| 317 |
* |
| 318 |
* Manages the $favorite_templates by using WordPress options to |
| 319 |
* add or remove templates from the favorites list. |
| 320 |
* |
| 321 |
* @since 6.7 |
| 322 |
* |
| 323 |
* @return void |
| 324 |
*/ |
| 325 |
public static function ajax_add_or_remove_favorite() { |
| 326 |
// Check permission and nonce. |
| 327 |
FrmAppHelper::permission_check( self::REQUIRED_CAPABILITY ); |
| 328 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 329 |
|
| 330 |
// Set up form templates environment, ensuring data is ready for processing. |
| 331 |
self::set_form_templates_data(); |
| 332 |
|
| 333 |
// Get posted data. |
| 334 |
$template_id = FrmAppHelper::get_post_param( 'template_id', '', 'absint' ); |
| 335 |
$operation = FrmAppHelper::get_post_param( 'operation', '', 'sanitize_text_field' ); |
| 336 |
$is_custom_template = FrmAppHelper::get_post_param( 'is_custom_template', '', 'rest_sanitize_boolean' ); |
| 337 |
|
| 338 |
// Determine the key based on whether it's a custom template or not. |
| 339 |
$key = $is_custom_template ? 'custom' : 'default'; |
| 340 |
|
| 341 |
// Perform add or remove operation. |
| 342 |
if ( 'add' === $operation ) { |
| 343 |
self::$favorite_templates[ $key ][] = $template_id; |
| 344 |
} elseif ( 'remove' === $operation ) { |
| 345 |
$position = array_search( $template_id, self::$favorite_templates[ $key ], true ); |
| 346 |
|
| 347 |
if ( $position !== false ) { |
| 348 |
unset( self::$favorite_templates[ $key ][ $position ] ); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
// Update the favorite templates option. |
| 353 |
update_option( self::FAVORITE_TEMPLATES_OPTION, self::$favorite_templates ); |
| 354 |
|
| 355 |
// Return the updated list of favorite templates. |
| 356 |
wp_send_json_success( self::$favorite_templates ); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Create a custom template from a form. |
| 361 |
* |
| 362 |
* @since 6.7 |
| 363 |
* |
| 364 |
* @return void |
| 365 |
*/ |
| 366 |
public static function ajax_create_template() { |
| 367 |
// Check permission and nonce. |
| 368 |
FrmAppHelper::permission_check( self::REQUIRED_CAPABILITY ); |
| 369 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 370 |
|
| 371 |
// Set up form templates environment, ensuring data is ready for processing. |
| 372 |
self::set_form_templates_data(); |
| 373 |
|
| 374 |
// Get posted data. |
| 375 |
$form_id = FrmAppHelper::get_param( 'xml', '', 'post', 'absint' ); |
| 376 |
$new_form_id = FrmForm::duplicate( $form_id, 1, true ); |
| 377 |
|
| 378 |
if ( ! $new_form_id ) { |
| 379 |
// Send an error response if form duplication fails. |
| 380 |
$response = array( |
| 381 |
'message' => __( 'There was an error creating a template.', 'formidable' ), |
| 382 |
); |
| 383 |
} else { |
| 384 |
FrmForm::update( $new_form_id, FrmFormsController::get_modal_values() ); |
| 385 |
|
| 386 |
// Send a success response with redirect URL. |
| 387 |
$response = array( |
| 388 |
'redirect' => admin_url( 'admin.php?page=formidable&frm_action=duplicate&id=' . $new_form_id ) . '&_wpnonce=' . wp_create_nonce(), |
| 389 |
); |
| 390 |
} |
| 391 |
|
| 392 |
// Send response. |
| 393 |
echo wp_json_encode( $response ); |
| 394 |
wp_die(); |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Handle AJAX request to subscribe the user to ActiveCampaign. |
| 399 |
* |
| 400 |
* @since 6.25 |
| 401 |
* |
| 402 |
* @return void |
| 403 |
*/ |
| 404 |
public static function ajax_get_free_templates() { |
| 405 |
FrmAppHelper::permission_check( self::REQUIRED_CAPABILITY ); |
| 406 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 407 |
|
| 408 |
$email = FrmAppHelper::get_post_param( 'email', '', 'sanitize_email' ); |
| 409 |
|
| 410 |
if ( empty( $email ) || ! is_email( $email ) ) { |
| 411 |
wp_send_json_error( |
| 412 |
array( 'message' => __( 'Please enter a valid email address.', 'formidable' ) ), |
| 413 |
WP_Http::BAD_REQUEST |
| 414 |
); |
| 415 |
} |
| 416 |
|
| 417 |
self::$form_template_api = new FrmFormTemplateApi(); |
| 418 |
self::$form_template_api->reset_cached(); |
| 419 |
|
| 420 |
FrmEmailCollectionHelper::subscribe_to_active_campaign( $email ); |
| 421 |
self::$form_template_api::set_free_license_code( '1' ); |
| 422 |
|
| 423 |
wp_send_json_success(); |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Fetch and format custom templates. |
| 428 |
* |
| 429 |
* Retrieves the custom templates, formats them, and assigns them to the class property. |
| 430 |
* |
| 431 |
* @since 6.7 |
| 432 |
* |
| 433 |
* @return void |
| 434 |
*/ |
| 435 |
private static function fetch_and_format_custom_templates() { |
| 436 |
// Get all custom templates that are not default templates. |
| 437 |
$custom_templates = FrmForm::getAll( |
| 438 |
array( |
| 439 |
'is_template' => 1, |
| 440 |
'default_template' => 0, |
| 441 |
'status' => array( null, '', 'published' ), |
| 442 |
), |
| 443 |
'name' |
| 444 |
); |
| 445 |
|
| 446 |
// Extract IDs from the custom templates for matching with favorite templates. |
| 447 |
$custom_templates_ids = wp_list_pluck( $custom_templates, 'id' ); |
| 448 |
|
| 449 |
// Refine the list of favorite templates to include only those present in custom templates. |
| 450 |
self::$favorite_templates['custom'] = array_intersect( self::$favorite_templates['custom'], $custom_templates_ids ); |
| 451 |
|
| 452 |
foreach ( $custom_templates as $template ) { |
| 453 |
$template = array( |
| 454 |
'id' => absint( $template->id ), |
| 455 |
'name' => $template->name, |
| 456 |
'key' => $template->form_key, |
| 457 |
'description' => $template->description, |
| 458 |
'link' => FrmForm::get_edit_link( absint( $template->id ) ), |
| 459 |
'url' => wp_nonce_url( admin_url( 'admin.php?page=formidable&frm_action=duplicate&new_template=true&id=' . absint( $template->id ) ) ), |
| 460 |
'released' => $template->created_at, |
| 461 |
'installed' => 1, |
| 462 |
'is_custom' => true, |
| 463 |
'created_at' => $template->created_at, |
| 464 |
); |
| 465 |
|
| 466 |
// Mark the template as favorite if it's in the favorite templates list. |
| 467 |
$template['is_favorite'] = in_array( $template['id'], self::$favorite_templates['custom'], true ); |
| 468 |
|
| 469 |
// Add the formatted template to the custom templates list. |
| 470 |
array_unshift( self::$custom_templates, $template ); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Retrieve and set templates. |
| 476 |
* |
| 477 |
* Gets the templates from the API and assigns them to the class property. |
| 478 |
* Also handles any errors returned from the API. |
| 479 |
* |
| 480 |
* @since 6.7 |
| 481 |
* |
| 482 |
* @return void |
| 483 |
*/ |
| 484 |
private static function retrieve_and_set_templates() { |
| 485 |
self::$templates = self::$form_template_api->get_api_info(); |
| 486 |
|
| 487 |
self::$is_expired = FrmAddonsController::is_license_expired(); |
| 488 |
self::$license_type = FrmAddonsController::license_type(); |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* Organize and set categories. |
| 493 |
* |
| 494 |
* Iterates through templates to organize categories, performs filtering, sorting, |
| 495 |
* and adds special categories. |
| 496 |
* |
| 497 |
* @since 6.7 |
| 498 |
* |
| 499 |
* @return void |
| 500 |
*/ |
| 501 |
private static function organize_and_set_categories() { |
| 502 |
// Iterate through templates to assign categories. |
| 503 |
foreach ( self::$templates as $key => &$template ) { |
| 504 |
// Skip the template if the categories are not set. |
| 505 |
if ( ! isset( $template['categories'] ) || ! isset( $template['id'] ) ) { |
| 506 |
unset( self::$templates[ $key ] ); |
| 507 |
continue; |
| 508 |
} |
| 509 |
|
| 510 |
// Add a new key for category slugs. |
| 511 |
$template['category_slugs'] = array(); |
| 512 |
|
| 513 |
// Increment the count for each category. |
| 514 |
foreach ( $template['categories'] as $category ) { |
| 515 |
$category_slug = sanitize_title( $category ); |
| 516 |
|
| 517 |
// Add the slug to the new array. |
| 518 |
$template['category_slugs'][] = $category_slug; |
| 519 |
|
| 520 |
if ( ! isset( self::$categories[ $category_slug ] ) ) { |
| 521 |
self::$categories[ $category_slug ] = array( |
| 522 |
'name' => $category, |
| 523 |
'count' => 0, |
| 524 |
); |
| 525 |
} |
| 526 |
|
| 527 |
++self::$categories[ $category_slug ]['count']; |
| 528 |
} |
| 529 |
|
| 530 |
// Mark the template as favorite if it's in the favorite templates list. |
| 531 |
$template['is_favorite'] = in_array( $template['id'], self::$favorite_templates['default'], true ); |
| 532 |
}//end foreach |
| 533 |
|
| 534 |
// Unset the reference `$template` variable. |
| 535 |
unset( $template ); |
| 536 |
|
| 537 |
// Filter out certain and redundant categories. |
| 538 |
// 'PayPal', 'Stripe', and 'Twilio' are included elsewhere and should be ignored in this context. |
| 539 |
$redundant_cats = array_merge( array( 'PayPal', 'Stripe', 'Twilio' ), FrmFormsHelper::get_license_types() ); |
| 540 |
|
| 541 |
foreach ( $redundant_cats as $redundant_cat ) { |
| 542 |
$category_slug = sanitize_title( $redundant_cat ); |
| 543 |
unset( self::$categories[ $category_slug ] ); |
| 544 |
} |
| 545 |
|
| 546 |
// Sort the categories by keys alphabetically. |
| 547 |
ksort( self::$categories ); |
| 548 |
|
| 549 |
// Add special categories. |
| 550 |
$special_categories = array( |
| 551 |
'favorites' => array( |
| 552 |
'name' => __( 'Favorites', 'formidable' ), |
| 553 |
'count' => self::get_favorite_templates_count(), |
| 554 |
), |
| 555 |
'custom' => array( |
| 556 |
'name' => __( 'Custom', 'formidable' ), |
| 557 |
'count' => count( self::$custom_templates ), |
| 558 |
), |
| 559 |
); |
| 560 |
|
| 561 |
// Add the 'Available Templates' category for non-elite users. |
| 562 |
if ( 'elite' !== FrmAddonsController::license_type() ) { |
| 563 |
$special_categories['available-templates'] = array( |
| 564 |
'name' => __( 'Available Templates', 'formidable' ), |
| 565 |
// Assigned via JavaScript. |
| 566 |
'count' => 0, |
| 567 |
); |
| 568 |
} |
| 569 |
$special_categories['all-items'] = array( |
| 570 |
'name' => __( 'All Templates', 'formidable' ), |
| 571 |
'count' => self::get_template_count(), |
| 572 |
); |
| 573 |
|
| 574 |
self::$categories = array_merge( |
| 575 |
$special_categories, |
| 576 |
self::$categories |
| 577 |
); |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Assign featured templates. |
| 582 |
* |
| 583 |
* Iterates through FEATURED_TEMPLATES_IDS and adds matching templates to |
| 584 |
* the `featured_templates` class property. |
| 585 |
* |
| 586 |
* @since 6.7 |
| 587 |
* |
| 588 |
* @return void |
| 589 |
*/ |
| 590 |
private static function assign_featured_templates() { |
| 591 |
foreach ( self::FEATURED_TEMPLATES_IDS as $key ) { |
| 592 |
if ( isset( self::$templates[ $key ] ) ) { |
| 593 |
self::$templates[ $key ]['is_featured'] = true; |
| 594 |
self::$featured_templates[] = self::$templates[ $key ]; |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* Get the total count of favorite templates. |
| 601 |
* |
| 602 |
* @since 6.7 |
| 603 |
* |
| 604 |
* @return int |
| 605 |
*/ |
| 606 |
public static function get_favorite_templates_count() { |
| 607 |
$custom_count = count( self::$favorite_templates['custom'] ); |
| 608 |
$default_count = count( self::$favorite_templates['default'] ); |
| 609 |
|
| 610 |
return $custom_count + $default_count; |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Initializes essential resources. |
| 615 |
* |
| 616 |
* @since 6.7 |
| 617 |
* |
| 618 |
* @return void |
| 619 |
*/ |
| 620 |
private static function init_template_resources() { |
| 621 |
self::$view_path = FrmAppHelper::plugin_path() . '/classes/views/form-templates/'; |
| 622 |
|
| 623 |
self::$upgrade_link = FrmAppHelper::admin_upgrade_link( |
| 624 |
array( |
| 625 |
'medium' => 'form-templates', |
| 626 |
'content' => 'upgrade', |
| 627 |
) |
| 628 |
); |
| 629 |
|
| 630 |
self::$renew_link = FrmAppHelper::admin_upgrade_link( |
| 631 |
array( |
| 632 |
'medium' => 'form-templates', |
| 633 |
'content' => 'renew', |
| 634 |
) |
| 635 |
); |
| 636 |
} |
| 637 |
|
| 638 |
/** |
| 639 |
* Adds a Cancel button to the header of the Form Templates page. |
| 640 |
* |
| 641 |
* It's hidden by default and will show when the user clicks on 'Create Form' from |
| 642 |
* another place in Formidable Forms. |
| 643 |
* |
| 644 |
* @since 6.7 |
| 645 |
* |
| 646 |
* @return void |
| 647 |
*/ |
| 648 |
public static function get_header_cancel_button() { |
| 649 |
echo ' |
| 650 |
<a class="button frm-button-secondary frm_hidden" href="' . esc_url( admin_url( 'admin.php?page=formidable' ) ) . '" role="button"> |
| 651 |
' . esc_html__( 'Cancel', 'formidable' ) . ' |
| 652 |
</a>'; |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* Append 'new_template' query parameter to navigation links if it exists in the URL. |
| 657 |
* |
| 658 |
* @since 6.7 |
| 659 |
* |
| 660 |
* @param array $nav_items Navigation items. |
| 661 |
* @param array $nav_args Additional navigation arguments. |
| 662 |
* |
| 663 |
* @return array Modified navigation items with 'new_template' query parameter. |
| 664 |
*/ |
| 665 |
public static function append_new_template_to_nav( $nav_items, $nav_args ) { |
| 666 |
$is_new_template = FrmAppHelper::simple_get( 'new_template' ); |
| 667 |
|
| 668 |
// Append 'new_template=true' to each nav item's link if 'new_template' exists in the URL. |
| 669 |
if ( $is_new_template ) { |
| 670 |
foreach ( $nav_items as &$item ) { |
| 671 |
$item['link'] .= '&new_template=true'; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
return $nav_items; |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Enqueues "Form Templates" scripts and styles. |
| 680 |
* |
| 681 |
* @since 6.7 |
| 682 |
* |
| 683 |
* @return void |
| 684 |
*/ |
| 685 |
public static function enqueue_assets() { |
| 686 |
$plugin_url = FrmAppHelper::plugin_url(); |
| 687 |
$version = FrmAppHelper::plugin_version(); |
| 688 |
$js_dependencies = array( |
| 689 |
'wp-i18n', |
| 690 |
// This prevents a console error "wp.hooks is undefined" in WP versions older than 5.7. |
| 691 |
'wp-hooks', |
| 692 |
'formidable_dom', |
| 693 |
); |
| 694 |
|
| 695 |
// Enqueue styles that needed. |
| 696 |
wp_enqueue_style( 'formidable-admin' ); |
| 697 |
wp_enqueue_style( 'formidable-animations' ); |
| 698 |
wp_enqueue_style( 'formidable-grids' ); |
| 699 |
|
| 700 |
// Register and enqueue "Form Templates" style. |
| 701 |
wp_register_style( self::SCRIPT_HANDLE, $plugin_url . '/css/admin/form-templates.css', array(), $version ); |
| 702 |
wp_enqueue_style( self::SCRIPT_HANDLE ); |
| 703 |
|
| 704 |
// Register and enqueue "Form Templates" script. |
| 705 |
wp_register_script( self::SCRIPT_HANDLE, $plugin_url . '/js/form-templates.js', $js_dependencies, $version, true ); |
| 706 |
wp_localize_script( self::SCRIPT_HANDLE, 'frmFormTemplatesVars', self::get_js_variables() ); |
| 707 |
wp_enqueue_script( self::SCRIPT_HANDLE ); |
| 708 |
|
| 709 |
/** |
| 710 |
* Fires after "Form Templates" enqueue assets. |
| 711 |
* |
| 712 |
* @since 6.7 |
| 713 |
*/ |
| 714 |
do_action( 'frm_form_templates_enqueue_assets' ); |
| 715 |
|
| 716 |
FrmAppHelper::dequeue_extra_global_scripts(); |
| 717 |
} |
| 718 |
|
| 719 |
/** |
| 720 |
* Get "Form Templates" JS variables as an array. |
| 721 |
* |
| 722 |
* @since 6.7 |
| 723 |
* |
| 724 |
* @return array |
| 725 |
*/ |
| 726 |
private static function get_js_variables() { |
| 727 |
$js_variables = array( |
| 728 |
'FEATURED_TEMPLATES_IDS' => self::FEATURED_TEMPLATES_IDS, |
| 729 |
'FREE_TEMPLATES_IDS' => self::FREE_TEMPLATES_IDS, |
| 730 |
'templatesCount' => self::get_template_count(), |
| 731 |
'favoritesCount' => array( |
| 732 |
'total' => self::get_favorite_templates_count(), |
| 733 |
'default' => count( self::$favorite_templates['default'] ), |
| 734 |
'custom' => count( self::$favorite_templates['custom'] ), |
| 735 |
), |
| 736 |
'customCount' => count( self::$custom_templates ), |
| 737 |
'upgradeLink' => self::$upgrade_link, |
| 738 |
); |
| 739 |
|
| 740 |
/** |
| 741 |
* Filters `js_variables` passed to the "Form Templates". |
| 742 |
* |
| 743 |
* @since 6.7 |
| 744 |
* |
| 745 |
* @param array $js_variables Array of js_variables passed to "Form Templates". |
| 746 |
*/ |
| 747 |
return apply_filters( 'frm_form_templates_js_variables', $js_variables ); |
| 748 |
} |
| 749 |
|
| 750 |
/** |
| 751 |
* Check if the current page is the form templates page. |
| 752 |
* |
| 753 |
* @since 6.7 |
| 754 |
* |
| 755 |
* @return bool True if the current page is the form templates page, false otherwise. |
| 756 |
*/ |
| 757 |
public static function is_templates_page() { |
| 758 |
return FrmAppHelper::is_admin_page( self::PAGE_SLUG ); |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
* Get the list of templates. |
| 763 |
* |
| 764 |
* @since 6.7 |
| 765 |
* |
| 766 |
* @return array A list of templates. |
| 767 |
*/ |
| 768 |
public static function get_templates() { |
| 769 |
return self::$templates; |
| 770 |
} |
| 771 |
|
| 772 |
/** |
| 773 |
* Get the total number of templates. |
| 774 |
* |
| 775 |
* @since 6.8 |
| 776 |
* |
| 777 |
* @return int |
| 778 |
*/ |
| 779 |
public static function get_template_count() { |
| 780 |
if ( empty( self::$templates ) ) { |
| 781 |
self::$form_template_api = new FrmFormTemplateApi(); |
| 782 |
self::retrieve_and_set_templates(); |
| 783 |
} |
| 784 |
return count( self::$templates ); |
| 785 |
} |
| 786 |
|
| 787 |
/** |
| 788 |
* Get the published forms based on applied filters. |
| 789 |
* |
| 790 |
* @since 6.7 |
| 791 |
* |
| 792 |
* @return array An array of published forms. |
| 793 |
*/ |
| 794 |
public static function get_published_forms() { |
| 795 |
$where = apply_filters( 'frm_forms_dropdown', array(), '' ); |
| 796 |
return FrmForm::get_published_forms( $where ); |
| 797 |
} |
| 798 |
|
| 799 |
/** |
| 800 |
* Get the list of featured templates. |
| 801 |
* |
| 802 |
* @since 6.7 |
| 803 |
* |
| 804 |
* @return array A list of featured templates. |
| 805 |
*/ |
| 806 |
public static function get_featured_templates() { |
| 807 |
return self::$featured_templates; |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* Get the list of categories. |
| 812 |
* |
| 813 |
* @since 6.7 |
| 814 |
* |
| 815 |
* @return array A list of categories. |
| 816 |
*/ |
| 817 |
public static function get_categories() { |
| 818 |
return self::$categories; |
| 819 |
} |
| 820 |
|
| 821 |
/** |
| 822 |
* Get the user's favorite form templates. |
| 823 |
* |
| 824 |
* @since 6.7 |
| 825 |
* |
| 826 |
* @return array The IDs of the user's favorite form templates. |
| 827 |
*/ |
| 828 |
public static function get_favorite_templates() { |
| 829 |
return self::$favorite_templates; |
| 830 |
} |
| 831 |
|
| 832 |
/** |
| 833 |
* Get the list of custom templates. |
| 834 |
* |
| 835 |
* @since 6.7 |
| 836 |
* |
| 837 |
* @return array A list of custom templates. |
| 838 |
*/ |
| 839 |
public static function get_custom_templates() { |
| 840 |
return self::$custom_templates; |
| 841 |
} |
| 842 |
|
| 843 |
/** |
| 844 |
* Get the license type. |
| 845 |
* |
| 846 |
* @since 6.7 |
| 847 |
* |
| 848 |
* @return string The license type. |
| 849 |
*/ |
| 850 |
public static function get_license_type() { |
| 851 |
return self::$license_type; |
| 852 |
} |
| 853 |
|
| 854 |
/** |
| 855 |
* Checks if the API request was expired. |
| 856 |
* |
| 857 |
* @since 6.7 |
| 858 |
* |
| 859 |
* @return bool True if the API request was expired, false otherwise. |
| 860 |
*/ |
| 861 |
public static function is_expired() { |
| 862 |
return self::$is_expired; |
| 863 |
} |
| 864 |
|
| 865 |
/** |
| 866 |
* Get the path to form templates views. |
| 867 |
* |
| 868 |
* @since 6.7 |
| 869 |
* |
| 870 |
* @return string Path to views. |
| 871 |
*/ |
| 872 |
public static function get_view_path() { |
| 873 |
return self::$view_path; |
| 874 |
} |
| 875 |
|
| 876 |
/** |
| 877 |
* Get the upgrade link. |
| 878 |
* |
| 879 |
* @since 6.7 |
| 880 |
* |
| 881 |
* @return string URL for upgrading accounts. |
| 882 |
*/ |
| 883 |
public static function get_upgrade_link() { |
| 884 |
return self::$upgrade_link; |
| 885 |
} |
| 886 |
|
| 887 |
/** |
| 888 |
* Get the renewal link. |
| 889 |
* |
| 890 |
* @since 6.7 |
| 891 |
* |
| 892 |
* @return string URL for renewing accounts. |
| 893 |
*/ |
| 894 |
public static function get_renew_link() { |
| 895 |
return self::$renew_link; |
| 896 |
} |
| 897 |
} |
| 898 |
|