llms-functions-forms.php 4.63 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7
<?php
/**
 * Functions for LifterLMS Forms
 *
 * @package LifterLMS/Functions/Forms
 *
 * @since 5.0.0
cyrille's avatar
cyrille committed
8
 * @version 5.10.0
cyrille's avatar
cyrille committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 */

defined( 'ABSPATH' ) || exit;

/**
 * Generate the HTML for a form field
 *
 * This function is used during AJAX calls so needs to be in a core file
 * loaded during AJAX calls!
 *
 * @since 3.0.0
 * @since 3.19.4 Unknown.
 * @since 5.0.0 Move from file: llms.functions.core.php.
 *              Utilize `LLMS_Form_Field` class for field generation and output.
 *
 * @param array      $field       Field settings.
 * @param boolean    $echo        Optional. Whether or not to output (echo) the field HTML. Default is `true`.
 * @param int|object $data_source Optional. Data source where to get field value from. Default is `null`.
 * @return string
 */
function llms_form_field( $field = array(), $echo = true, $data_source = null ) {

	$args = array( $field );
	if ( ! is_null( $data_source ) ) {
		$args[] = $data_source;
	}

	$field = new LLMS_Form_Field( ...$args );

	if ( $echo ) {
		$field->render();
	}

	return $field->get_html();

}

/**
 * Retrieve the form post for a form at a given location.
 *
 * @since 5.0.0
 *
 * @param string $location Form location, one of: "checkout", "registration", or "account".
 * @param array  $args Additional arguments passed to the short-circuit filter in `LLMS_Forms->get_form_post()`.
 * @return WP_Post|false
 */
function llms_get_form( $location, $args = array() ) {
	return LLMS_Forms::instance()->get_form_post( $location, $args );
}

/**
 * Retrieve the HTML for a form at the given location.
 *
 * @since 5.0.0
 *
 * @param string $location Form location, one of: "checkout", "registration", or "account".
 * @param array  $args Additional arguments passed to the short-circuit filter in `LLMS_Forms->get_form_post()`.
 * @return string
 */
function llms_get_form_html( $location, $args = array() ) {
	return LLMS_Forms::instance()->get_form_html( $location, $args );
}

/**
 * Retrieve the title of a form at a given location.
 *
 * Returns an empty string if the form is disabled via form settings.
 *
 * @since 5.0.0
cyrille's avatar
cyrille committed
78
 * @since 5.10.0 Return specific form title for checkout forms and free access plans.
cyrille's avatar
cyrille committed
79 80 81 82 83 84 85 86 87 88 89 90
 *
 * @param string $location Form location, one of: "checkout", "registration", or "account".
 * @param array  $args Additional arguments passed to the short-circuit filter in `LLMS_Forms->get_form_post()`.
 * @return string
 */
function llms_get_form_title( $location, $args = array() ) {

	$post = llms_get_form( $location, $args );
	if ( ! $post || ! llms_parse_bool( get_post_meta( $post->ID, '_llms_form_show_title', true ) ) ) {
		return '';
	}

cyrille's avatar
cyrille committed
91 92 93 94 95
	return 'checkout' === $location && isset( $args['plan'] ) && $args['plan']->is_free()
		?
		apply_filters( 'the_title', get_post_meta( $post->ID, '_llms_form_title_free_access_plans', true ) )
		:
		get_the_title( $post->ID );
cyrille's avatar
cyrille committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

}

/**
 * Displays a login form.
 *
 * Only displays the form for logged out users (because logged in users cannot login).
 *
 * @since 1.0.0
 * @since 3.19.4 Unknown
 * @since 5.0.0 Moved logic and filters for the $message, $redirect, and $layout parameters from the template into the function.
 *
 * @param string $message  Optional. Messages to display before login form via llms_add_notice().
 * @param string $redirect Optional. URL to redirect to after login. Defaults to current page url.
 * @param string $layout   Optional. Form layout. Accepts either 'columns' (default) or 'stacked'. Default is 'columns'.
 * @return void
 */
if ( ! function_exists( 'llms_get_login_form' ) ) {
	function llms_get_login_form( $message = null, $redirect = null, $layout = 'columns' ) {

		/**
		 * Filters whether or not the login form should be displayed
		 *
		 * By default, the registration form is hidden from logged-in users and
		 * displayed to logged out users.
		 *
		 * @since 4.16.0
		 * @since 5.0.0 Moved from template `global/form-login.php`/.
		 *
		 * @param boolean $hide_form Whether or not to hide the form. If `true`, the form is hidden, otherwise it is displayed.
		 */
		if ( apply_filters( 'llms_hide_login_form', is_user_logged_in() ) ) {
			return;
		}

		/**
		 * Customize the layout of the login form.
		 *
		 * @since Unknown
		 *
		 * @param string $layout Form layout. Accepts "columns" (default) for a side-by-side layout
		 *                       for form fields or "stacked" so fields sit on top of each other. Default is 'columns'.
		 */
		$layout = apply_filters( 'llms_login_form_layout', $layout );

		if ( ! empty( $message ) ) {
			llms_add_notice( $message, 'notice' );
		}

		$redirect = empty( $redirect ) ? get_permalink() : $redirect;

		llms_get_template( 'global/form-login.php', compact( 'message', 'redirect', 'layout' ) );
	}
}