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