Form
in package
Basic form functionality.
- Enqueue form assets.
- Provide intercepts before showing form.
- Render form.
- Handle submission.
Tags
Table of Contents
Constants
- EXTRA_VALIDATION_HOOK = 'pnet_validation'
- The extra_validation hook base name.
- FORM_ACTION = ''
- The admin_post and extra_validation hook action name.
- FORM_CONTAINER = ''
- Form container partial.
- FORM_NAME = 'form_name'
- The name attribute of the form.
- NEXT_PAGE = ''
- The page to redirect to when closing form response banner.
- NONCE_ACTION = ''
- The nonce action.
- NONCE_NAME = 'token'
- The nonce name.
- RESPONSE_PAGE = ''
- The page to redirect to for form submission response.
Properties
- $form_container : string
- The fully qualified path to the form container partial.
- $form_header : string
- The text before the form.
- $http_handler : HTTP_Handler
- The instance of the HTTP_Handler class.
- $instances : array<string|int, object>
- The array of instances of this class and sub-classes.
- $messages : Messages
- The instance of the Messages class.
- $next_url : string
- The fully qualified path to the next page after closing the success banner.
- $notifications : array<string|int, string>
- The marked up notifications to display at top of form.
- $progress_container : string
- The progress indicator container markup.
- $recaptcha_site_key : string
- Google reCAPTCHA V3 site key.
- $response_url : string
- The fully qualified path to the submission response target.
- $success_message_code : string
- The success message code.
- $validation_hook : string
- The name of the extra validation hook.
Methods
- get_form_action() : string
- Return the form action.
- get_instance() : object
- Creates and/or returns the instance of this class.
- get_validation_hook() : string
- Return the extra validation hook name.
- handle_submission() : void
- Handle the form submission.
- render() : void
- Render the form.
- __construct() : void
- Initialise the class.
- add_notifications() : void
- Add conditional notifications to form.
- do_intercepts() : void
- Take special actions before initialising the form.
- do_submission() : WP_Error
- Execute the submission request.
- enqueue_assets() : void
- Enqueue the form script and styles.
- render_success_notification() : void
- Render the success notification.
- set_form_header() : void
- Set the form header text.
- set_form_values() : void
- Initialise the values used in the form.
- validate_submission() : WP_Error
- Validate the submitted form contents.
Constants
EXTRA_VALIDATION_HOOK
The extra_validation hook base name.
protected
string
EXTRA_VALIDATION_HOOK
= 'pnet_validation'
Tags
FORM_ACTION
The admin_post and extra_validation hook action name.
protected
string
FORM_ACTION
= ''
- This MUST be defined in the child class.
Tags
FORM_CONTAINER
Form container partial.
protected
string
FORM_CONTAINER
= ''
- This MUST be defined in the child class.
Tags
FORM_NAME
The name attribute of the form.
protected
string
FORM_NAME
= 'form_name'
- This MUST be defined in the child class.
Tags
NEXT_PAGE
The page to redirect to when closing form response banner.
protected
string
NEXT_PAGE
= ''
- Override this constant in the child class.
- Page name must start with a slash to be appended to home_url().
Tags
NONCE_ACTION
The nonce action.
protected
string
NONCE_ACTION
= ''
- This MUST be defined in the child class.
Tags
NONCE_NAME
The nonce name.
protected
string
NONCE_NAME
= 'token'
Tags
RESPONSE_PAGE
The page to redirect to for form submission response.
protected
string
RESPONSE_PAGE
= ''
- Override this constant in the child class.
Tags
Properties
$form_container
The fully qualified path to the form container partial.
protected
string
$form_container
= ''
Tags
$form_header
The text before the form.
protected
string
$form_header
= ''
- This is the text to display between the page content and the form.
- Describes the required field markup.
- Set in set_form_header() to allow translation.
Tags
$http_handler
The instance of the HTTP_Handler class.
protected
HTTP_Handler
$http_handler
Tags
$instances
The array of instances of this class and sub-classes.
protected
static array<string|int, object>
$instances
= array()
Tags
$messages
The instance of the Messages class.
protected
Messages
$messages
Tags
$next_url
The fully qualified path to the next page after closing the success banner.
protected
string
$next_url
= ''
- Blank means no redirection.
Tags
$notifications
The marked up notifications to display at top of form.
protected
array<string|int, string>
$notifications
= array()
Tags
$progress_container
The progress indicator container markup.
protected
string
$progress_container
= ''
Tags
$recaptcha_site_key
Google reCAPTCHA V3 site key.
protected
string
$recaptcha_site_key
= ''
Tags
$response_url
The fully qualified path to the submission response target.
protected
string
$response_url
= ''
Tags
$success_message_code
The success message code.
protected
string
$success_message_code
= ''
- This is the key to the success message from the message handler.
- It is of the form "success_{FORM_ACTION}"
Tags
$validation_hook
The name of the extra validation hook.
protected
string
$validation_hook
= ''
- This adds the FORM_ACTION to the EXTRA_VALIDATION_HOOK constant.
Tags
Methods
get_form_action()
Return the form action.
public
get_form_action() : string
- This is the {action} part of the admin_post hook.
Tags
Return values
string —The form action.
get_instance()
Creates and/or returns the instance of this class.
public
static get_instance() : object
Tags
Return values
object —The instance of this class.
get_validation_hook()
Return the extra validation hook name.
public
get_validation_hook() : string
Tags
Return values
string —The hook name.
handle_submission()
Handle the form submission.
public
handle_submission() : void
- Validate nonce.
- Call validate_submission(). This is unique to each child class.
- Apply filter for extra validation if required (eg reCAPTCHA).
- Call do_submission(). This is unique to each child class.
- Redirect back to form page.
Tags
render()
Render the form.
public
render() : void
- Intercept any required actions before rendering form.
- Add any conditional notifications.
- Display form or notification if form submitted successfully.
- Enqueue form assets.
Tags
__construct()
Initialise the class.
protected
__construct() : void
Tags
add_notifications()
Add conditional notifications to form.
protected
add_notifications([array<string|int, string> $get_vars = array() ]) : void
- (Maybe) override this in child classes to set specific form notifications.
Parameters
- $get_vars : array<string|int, string> = array()
-
The sanitised $_GET arguments.
Tags
do_intercepts()
Take special actions before initialising the form.
protected
do_intercepts([array<string|int, string> $get_vars = array() ]) : void
- (Maybe) override this in child classes to set specific intercept actions.
Parameters
- $get_vars : array<string|int, string> = array()
-
{ The sanitised $_GET arguments, including a nonce. @type string $action The action to take. }
Tags
do_submission()
Execute the submission request.
protected
do_submission(WP_Error $errors, array<string|int, string> $post_vars, array<string|int, string> $server_vars) : WP_Error
- This MUST be overridden in child classes.
Parameters
- $errors : WP_Error
-
The WP_Error object to use if errors.
- $post_vars : array<string|int, string>
-
The sanitised $_POST arguments.
- $server_vars : array<string|int, string>
-
The sanitised $_SERVER arguments.
Tags
Return values
WP_Error —The maybe modified WP_Error object.
enqueue_assets()
Enqueue the form script and styles.
protected
enqueue_assets() : void
- Extend this in child classes using parent::enqueue_assets() then add more assets.
- See class-form-user-account.php for example and setting inline scripts.
Tags
render_success_notification()
Render the success notification.
protected
render_success_notification() : void
Tags
set_form_header()
Set the form header text.
protected
set_form_header() : void
- (Maybe) extend this in child classes to set specific instructions for that form.
Tags
set_form_values()
Initialise the values used in the form.
protected
set_form_values(array<string|int, string> $get_vars, array<string|int, string> $server_vars) : void
- This MUST be overridden in child classes.
Parameters
- $get_vars : array<string|int, string>
-
The sanitised $_GET arguments.
- $server_vars : array<string|int, string>
-
The sanitised $_SERVER arguments.
Tags
validate_submission()
Validate the submitted form contents.
protected
validate_submission(WP_Error $errors, array<string|int, string> $post_vars, array<string|int, string> $server_vars) : WP_Error
- This MUST be overridden in child classes.
Parameters
- $errors : WP_Error
-
The WP_Error object to use if errors.
- $post_vars : array<string|int, string>
-
The sanitised $_POST arguments.
- $server_vars : array<string|int, string>
-
The sanitised $_SERVER arguments.
Tags
Return values
WP_Error —The maybe modified WP_Error object.