@@ -16,112 +16,112 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class StatusChangeNotice extends WordPressOption |
| 18 | 18 | { |
| 19 | - public function __construct() |
|
| 20 | - { |
|
| 21 | - parent::__construct('ee_hide_status_change_notices_for_users', [], false); |
|
| 22 | - } |
|
| 19 | + public function __construct() |
|
| 20 | + { |
|
| 21 | + parent::__construct('ee_hide_status_change_notices_for_users', [], false); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | 24 | |
| 25 | - public static function loadAssets() |
|
| 26 | - { |
|
| 27 | - wp_enqueue_style( |
|
| 28 | - 'status_change_notice', |
|
| 29 | - EE_PLUGIN_DIR_URL . 'core/domain/services/admin/notices/status_change/status_change_notice.css', |
|
| 30 | - ['espresso_menu'], |
|
| 31 | - EVENT_ESPRESSO_VERSION |
|
| 32 | - ); |
|
| 33 | - wp_enqueue_script( |
|
| 34 | - 'status_change_notice', |
|
| 35 | - EE_PLUGIN_DIR_URL . 'core/domain/services/admin/notices/status_change/status_change_notice.js', |
|
| 36 | - ['jquery'], |
|
| 37 | - EVENT_ESPRESSO_VERSION, |
|
| 38 | - true |
|
| 39 | - ); |
|
| 40 | - wp_localize_script( |
|
| 41 | - 'status_change_notice', |
|
| 42 | - 'eeStatusChangeNotice', |
|
| 43 | - [ |
|
| 44 | - 'failed_request_msg' => wp_strip_all_tags( |
|
| 45 | - __( |
|
| 46 | - 'Request failed. The server returned status code: ', |
|
| 47 | - 'event_espresso' |
|
| 48 | - ) |
|
| 49 | - ), |
|
| 50 | - 'unknown_error_msg' => wp_strip_all_tags( |
|
| 51 | - __( |
|
| 52 | - 'Oops... an unknown error has occurred on the server and this notice could not be dismissed.', |
|
| 53 | - 'event_espresso' |
|
| 54 | - ) |
|
| 55 | - ), |
|
| 56 | - ] |
|
| 57 | - ); |
|
| 58 | - } |
|
| 25 | + public static function loadAssets() |
|
| 26 | + { |
|
| 27 | + wp_enqueue_style( |
|
| 28 | + 'status_change_notice', |
|
| 29 | + EE_PLUGIN_DIR_URL . 'core/domain/services/admin/notices/status_change/status_change_notice.css', |
|
| 30 | + ['espresso_menu'], |
|
| 31 | + EVENT_ESPRESSO_VERSION |
|
| 32 | + ); |
|
| 33 | + wp_enqueue_script( |
|
| 34 | + 'status_change_notice', |
|
| 35 | + EE_PLUGIN_DIR_URL . 'core/domain/services/admin/notices/status_change/status_change_notice.js', |
|
| 36 | + ['jquery'], |
|
| 37 | + EVENT_ESPRESSO_VERSION, |
|
| 38 | + true |
|
| 39 | + ); |
|
| 40 | + wp_localize_script( |
|
| 41 | + 'status_change_notice', |
|
| 42 | + 'eeStatusChangeNotice', |
|
| 43 | + [ |
|
| 44 | + 'failed_request_msg' => wp_strip_all_tags( |
|
| 45 | + __( |
|
| 46 | + 'Request failed. The server returned status code: ', |
|
| 47 | + 'event_espresso' |
|
| 48 | + ) |
|
| 49 | + ), |
|
| 50 | + 'unknown_error_msg' => wp_strip_all_tags( |
|
| 51 | + __( |
|
| 52 | + 'Oops... an unknown error has occurred on the server and this notice could not be dismissed.', |
|
| 53 | + 'event_espresso' |
|
| 54 | + ) |
|
| 55 | + ), |
|
| 56 | + ] |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | - public function display(string $context, string $page_slug): string |
|
| 62 | - { |
|
| 63 | - return $this->isNotDismissed() |
|
| 64 | - ? EEH_Template::display_template( |
|
| 65 | - __DIR__ . '/status_change_notice.template.php', |
|
| 66 | - [ |
|
| 61 | + public function display(string $context, string $page_slug): string |
|
| 62 | + { |
|
| 63 | + return $this->isNotDismissed() |
|
| 64 | + ? EEH_Template::display_template( |
|
| 65 | + __DIR__ . '/status_change_notice.template.php', |
|
| 66 | + [ |
|
| 67 | 67 | 'context' => $context, |
| 68 | 68 | 'page_slug' => ! empty($page_slug) ? "$page_slug-page" : '', |
| 69 | - ], |
|
| 70 | - true |
|
| 71 | - ) |
|
| 72 | - : ''; |
|
| 73 | - } |
|
| 69 | + ], |
|
| 70 | + true |
|
| 71 | + ) |
|
| 72 | + : ''; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @return int |
|
| 78 | - * @throws RuntimeException |
|
| 79 | - */ |
|
| 80 | - public function dismiss(): int |
|
| 81 | - { |
|
| 82 | - $user = $this->getCurrentUser(); |
|
| 83 | - $dismissed = (array) $this->loadOption(); |
|
| 84 | - if (! in_array($user, $dismissed)) { |
|
| 85 | - $dismissed[] = $user; |
|
| 86 | - } |
|
| 87 | - return $this->updateOption($dismissed); |
|
| 88 | - } |
|
| 76 | + /** |
|
| 77 | + * @return int |
|
| 78 | + * @throws RuntimeException |
|
| 79 | + */ |
|
| 80 | + public function dismiss(): int |
|
| 81 | + { |
|
| 82 | + $user = $this->getCurrentUser(); |
|
| 83 | + $dismissed = (array) $this->loadOption(); |
|
| 84 | + if (! in_array($user, $dismissed)) { |
|
| 85 | + $dismissed[] = $user; |
|
| 86 | + } |
|
| 87 | + return $this->updateOption($dismissed); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * @return bool |
|
| 93 | - * @throws RuntimeException |
|
| 94 | - */ |
|
| 95 | - public function isDismissed(): bool |
|
| 96 | - { |
|
| 97 | - $user = $this->getCurrentUser(); |
|
| 98 | - $dismissed = (array) $this->loadOption(); |
|
| 99 | - return in_array($user, $dismissed); |
|
| 100 | - } |
|
| 91 | + /** |
|
| 92 | + * @return bool |
|
| 93 | + * @throws RuntimeException |
|
| 94 | + */ |
|
| 95 | + public function isDismissed(): bool |
|
| 96 | + { |
|
| 97 | + $user = $this->getCurrentUser(); |
|
| 98 | + $dismissed = (array) $this->loadOption(); |
|
| 99 | + return in_array($user, $dismissed); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @return bool |
|
| 105 | - * @throws RuntimeException |
|
| 106 | - */ |
|
| 107 | - public function isNotDismissed(): bool |
|
| 108 | - { |
|
| 109 | - return ! $this->isDismissed(); |
|
| 110 | - } |
|
| 103 | + /** |
|
| 104 | + * @return bool |
|
| 105 | + * @throws RuntimeException |
|
| 106 | + */ |
|
| 107 | + public function isNotDismissed(): bool |
|
| 108 | + { |
|
| 109 | + return ! $this->isDismissed(); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * @return string |
|
| 115 | - * @throws RuntimeException |
|
| 116 | - */ |
|
| 117 | - private function getCurrentUser(): string |
|
| 118 | - { |
|
| 119 | - $user = wp_get_current_user(); |
|
| 120 | - if (! $user instanceof WP_User || ! $user->exists()) { |
|
| 121 | - throw new RuntimeException( |
|
| 122 | - esc_html__('A valid WP User could not be retrieved.', 'event_espresso') |
|
| 123 | - ); |
|
| 124 | - } |
|
| 125 | - return $user->user_login; |
|
| 126 | - } |
|
| 113 | + /** |
|
| 114 | + * @return string |
|
| 115 | + * @throws RuntimeException |
|
| 116 | + */ |
|
| 117 | + private function getCurrentUser(): string |
|
| 118 | + { |
|
| 119 | + $user = wp_get_current_user(); |
|
| 120 | + if (! $user instanceof WP_User || ! $user->exists()) { |
|
| 121 | + throw new RuntimeException( |
|
| 122 | + esc_html__('A valid WP User could not be retrieved.', 'event_espresso') |
|
| 123 | + ); |
|
| 124 | + } |
|
| 125 | + return $user->user_login; |
|
| 126 | + } |
|
| 127 | 127 | } |
@@ -26,13 +26,13 @@ discard block |
||
| 26 | 26 | { |
| 27 | 27 | wp_enqueue_style( |
| 28 | 28 | 'status_change_notice', |
| 29 | - EE_PLUGIN_DIR_URL . 'core/domain/services/admin/notices/status_change/status_change_notice.css', |
|
| 29 | + EE_PLUGIN_DIR_URL.'core/domain/services/admin/notices/status_change/status_change_notice.css', |
|
| 30 | 30 | ['espresso_menu'], |
| 31 | 31 | EVENT_ESPRESSO_VERSION |
| 32 | 32 | ); |
| 33 | 33 | wp_enqueue_script( |
| 34 | 34 | 'status_change_notice', |
| 35 | - EE_PLUGIN_DIR_URL . 'core/domain/services/admin/notices/status_change/status_change_notice.js', |
|
| 35 | + EE_PLUGIN_DIR_URL.'core/domain/services/admin/notices/status_change/status_change_notice.js', |
|
| 36 | 36 | ['jquery'], |
| 37 | 37 | EVENT_ESPRESSO_VERSION, |
| 38 | 38 | true |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | { |
| 63 | 63 | return $this->isNotDismissed() |
| 64 | 64 | ? EEH_Template::display_template( |
| 65 | - __DIR__ . '/status_change_notice.template.php', |
|
| 65 | + __DIR__.'/status_change_notice.template.php', |
|
| 66 | 66 | [ |
| 67 | 67 | 'context' => $context, |
| 68 | 68 | 'page_slug' => ! empty($page_slug) ? "$page_slug-page" : '', |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | { |
| 82 | 82 | $user = $this->getCurrentUser(); |
| 83 | 83 | $dismissed = (array) $this->loadOption(); |
| 84 | - if (! in_array($user, $dismissed)) { |
|
| 84 | + if ( ! in_array($user, $dismissed)) { |
|
| 85 | 85 | $dismissed[] = $user; |
| 86 | 86 | } |
| 87 | 87 | return $this->updateOption($dismissed); |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | private function getCurrentUser(): string |
| 118 | 118 | { |
| 119 | 119 | $user = wp_get_current_user(); |
| 120 | - if (! $user instanceof WP_User || ! $user->exists()) { |
|
| 120 | + if ( ! $user instanceof WP_User || ! $user->exists()) { |
|
| 121 | 121 | throw new RuntimeException( |
| 122 | 122 | esc_html__('A valid WP User could not be retrieved.', 'event_espresso') |
| 123 | 123 | ); |
@@ -30,86 +30,86 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class ConfirmDeletion |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * @var NodeGroupDao |
|
| 35 | - */ |
|
| 36 | - private $dao; |
|
| 33 | + /** |
|
| 34 | + * @var NodeGroupDao |
|
| 35 | + */ |
|
| 36 | + private $dao; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * ConfirmDeletion constructor. |
|
| 40 | - * @param NodeGroupDao $dao |
|
| 41 | - */ |
|
| 42 | - public function __construct( |
|
| 43 | - NodeGroupDao $dao |
|
| 44 | - ) { |
|
| 38 | + /** |
|
| 39 | + * ConfirmDeletion constructor. |
|
| 40 | + * @param NodeGroupDao $dao |
|
| 41 | + */ |
|
| 42 | + public function __construct( |
|
| 43 | + NodeGroupDao $dao |
|
| 44 | + ) { |
|
| 45 | 45 | |
| 46 | - $this->dao = $dao; |
|
| 47 | - } |
|
| 46 | + $this->dao = $dao; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Redirects to the batch job for deleting events if the form submission is valid, otherwise back to the deletion |
|
| 51 | - * preview page. |
|
| 52 | - * @since 4.10.12.p |
|
| 53 | - * @param $request_data |
|
| 54 | - * @param $admin_base_url |
|
| 55 | - * @throws EE_Error |
|
| 56 | - * @throws InvalidArgumentException |
|
| 57 | - * @throws InvalidDataTypeException |
|
| 58 | - * @throws InvalidInterfaceException |
|
| 59 | - * @throws ReflectionException |
|
| 60 | - * @throws UnexpectedEntityException |
|
| 61 | - */ |
|
| 62 | - public function handle($request_data, $admin_base_url) |
|
| 63 | - { |
|
| 64 | - $deletion_job_code = isset($request_data['deletion_job_code']) ? sanitize_key($request_data['deletion_job_code']) : ''; |
|
| 65 | - $models_and_ids_to_delete = $this->dao->getModelsAndIdsFromGroup($deletion_job_code); |
|
| 66 | - $form = new ConfirmEventDeletionForm($models_and_ids_to_delete['Event']); |
|
| 67 | - // Initialize the form from the request, and check if its valid. |
|
| 68 | - $form->receive_form_submission($request_data); |
|
| 69 | - if ($form->is_valid()) { |
|
| 70 | - // Redirect the user to the deletion batch job. |
|
| 71 | - EEH_URL::safeRedirectAndExit( |
|
| 72 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 73 | - array( |
|
| 74 | - 'page' => EED_Batch::PAGE_SLUG, |
|
| 75 | - 'batch' => EED_Batch::batch_job, |
|
| 76 | - 'deletion_job_code' => $deletion_job_code, |
|
| 77 | - 'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\ExecuteBatchDeletion'), |
|
| 78 | - 'return_url' => urlencode( |
|
| 79 | - add_query_arg( |
|
| 80 | - [ |
|
| 81 | - 'status' => 'trash' |
|
| 82 | - ], |
|
| 83 | - EVENTS_ADMIN_URL |
|
| 84 | - ) |
|
| 85 | - ) |
|
| 86 | - ), |
|
| 87 | - admin_url() |
|
| 88 | - ) |
|
| 89 | - ); |
|
| 90 | - } |
|
| 91 | - // Dont' use $form->submission_error_message() because it adds the form input's label in front |
|
| 92 | - // of each validation error which ends up looking quite confusing. |
|
| 93 | - $validation_errors = $form->get_validation_errors_accumulated(); |
|
| 94 | - foreach ($validation_errors as $validation_error) { |
|
| 95 | - EE_Error::add_error( |
|
| 96 | - $validation_error->getMessage(), |
|
| 97 | - __FILE__, |
|
| 98 | - __FUNCTION__, |
|
| 99 | - __LINE__ |
|
| 100 | - ); |
|
| 101 | - } |
|
| 49 | + /** |
|
| 50 | + * Redirects to the batch job for deleting events if the form submission is valid, otherwise back to the deletion |
|
| 51 | + * preview page. |
|
| 52 | + * @since 4.10.12.p |
|
| 53 | + * @param $request_data |
|
| 54 | + * @param $admin_base_url |
|
| 55 | + * @throws EE_Error |
|
| 56 | + * @throws InvalidArgumentException |
|
| 57 | + * @throws InvalidDataTypeException |
|
| 58 | + * @throws InvalidInterfaceException |
|
| 59 | + * @throws ReflectionException |
|
| 60 | + * @throws UnexpectedEntityException |
|
| 61 | + */ |
|
| 62 | + public function handle($request_data, $admin_base_url) |
|
| 63 | + { |
|
| 64 | + $deletion_job_code = isset($request_data['deletion_job_code']) ? sanitize_key($request_data['deletion_job_code']) : ''; |
|
| 65 | + $models_and_ids_to_delete = $this->dao->getModelsAndIdsFromGroup($deletion_job_code); |
|
| 66 | + $form = new ConfirmEventDeletionForm($models_and_ids_to_delete['Event']); |
|
| 67 | + // Initialize the form from the request, and check if its valid. |
|
| 68 | + $form->receive_form_submission($request_data); |
|
| 69 | + if ($form->is_valid()) { |
|
| 70 | + // Redirect the user to the deletion batch job. |
|
| 71 | + EEH_URL::safeRedirectAndExit( |
|
| 72 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 73 | + array( |
|
| 74 | + 'page' => EED_Batch::PAGE_SLUG, |
|
| 75 | + 'batch' => EED_Batch::batch_job, |
|
| 76 | + 'deletion_job_code' => $deletion_job_code, |
|
| 77 | + 'job_handler' => urlencode('EventEspressoBatchRequest\JobHandlers\ExecuteBatchDeletion'), |
|
| 78 | + 'return_url' => urlencode( |
|
| 79 | + add_query_arg( |
|
| 80 | + [ |
|
| 81 | + 'status' => 'trash' |
|
| 82 | + ], |
|
| 83 | + EVENTS_ADMIN_URL |
|
| 84 | + ) |
|
| 85 | + ) |
|
| 86 | + ), |
|
| 87 | + admin_url() |
|
| 88 | + ) |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | + // Dont' use $form->submission_error_message() because it adds the form input's label in front |
|
| 92 | + // of each validation error which ends up looking quite confusing. |
|
| 93 | + $validation_errors = $form->get_validation_errors_accumulated(); |
|
| 94 | + foreach ($validation_errors as $validation_error) { |
|
| 95 | + EE_Error::add_error( |
|
| 96 | + $validation_error->getMessage(), |
|
| 97 | + __FILE__, |
|
| 98 | + __FUNCTION__, |
|
| 99 | + __LINE__ |
|
| 100 | + ); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - EEH_URL::safeRedirectAndExit( |
|
| 104 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 105 | - [ |
|
| 106 | - 'action' => 'preview_deletion', |
|
| 107 | - 'deletion_job_code' => $deletion_job_code |
|
| 108 | - ], |
|
| 109 | - $admin_base_url |
|
| 110 | - ) |
|
| 111 | - ); |
|
| 112 | - } |
|
| 103 | + EEH_URL::safeRedirectAndExit( |
|
| 104 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 105 | + [ |
|
| 106 | + 'action' => 'preview_deletion', |
|
| 107 | + 'deletion_job_code' => $deletion_job_code |
|
| 108 | + ], |
|
| 109 | + $admin_base_url |
|
| 110 | + ) |
|
| 111 | + ); |
|
| 112 | + } |
|
| 113 | 113 | } |
| 114 | 114 | // End of file ConfirmDeletion.php |
| 115 | 115 | // Location: EventEspresso\core\domain\services\admin\events\data/ConfirmDeletion.php |
@@ -6,80 +6,80 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class EE_Float_Field extends EE_Model_Field_Base |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var EE_Currency_Config |
|
| 11 | - */ |
|
| 12 | - protected $currency; |
|
| 9 | + /** |
|
| 10 | + * @var EE_Currency_Config |
|
| 11 | + */ |
|
| 12 | + protected $currency; |
|
| 13 | 13 | |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @param string $table_column |
|
| 17 | - * @param string $nicename |
|
| 18 | - * @param bool $nullable |
|
| 19 | - * @param null $default_value |
|
| 20 | - */ |
|
| 21 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
| 22 | - { |
|
| 23 | - $this->currency = EE_Config::instance()->currency instanceof EE_Currency_Config |
|
| 15 | + /** |
|
| 16 | + * @param string $table_column |
|
| 17 | + * @param string $nicename |
|
| 18 | + * @param bool $nullable |
|
| 19 | + * @param null $default_value |
|
| 20 | + */ |
|
| 21 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
| 22 | + { |
|
| 23 | + $this->currency = EE_Config::instance()->currency instanceof EE_Currency_Config |
|
| 24 | 24 | ? EE_Config::instance()->currency |
| 25 | 25 | : new EE_Currency_Config(); |
| 26 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
| 27 | - $this->setSchemaType('number'); |
|
| 28 | - } |
|
| 26 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
| 27 | + $this->setSchemaType('number'); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * If provided a string, strips out number-related formatting, like commas, periods, spaces, other junk, etc. |
|
| 33 | - * However, treats commas and periods as thousand-separators ro decimal marks, as indicate by the config's currency. |
|
| 34 | - * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
| 35 | - * Returns a float |
|
| 36 | - * |
|
| 37 | - * @param float|string $value_inputted_for_field_on_model_object |
|
| 38 | - * @return float |
|
| 39 | - */ |
|
| 40 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 41 | - { |
|
| 42 | - // remove whitespaces and thousands separators |
|
| 43 | - if (is_string($value_inputted_for_field_on_model_object)) { |
|
| 44 | - $value_inputted_for_field_on_model_object = str_replace( |
|
| 45 | - array(" ", $this->currency->thsnds), |
|
| 46 | - "", |
|
| 47 | - $value_inputted_for_field_on_model_object |
|
| 48 | - ); |
|
| 49 | - // normalize it so periods are decimal marks (we don't care where you're from: we're talking PHP now) |
|
| 50 | - $value_inputted_for_field_on_model_object = str_replace( |
|
| 51 | - $this->currency->dec_mrk, |
|
| 52 | - ".", |
|
| 53 | - $value_inputted_for_field_on_model_object |
|
| 54 | - ); |
|
| 55 | - // double-check there's absolutely nothing left on this string besides numbers |
|
| 56 | - $value_inputted_for_field_on_model_object = preg_replace( |
|
| 57 | - "/[^0-9,.]/", |
|
| 58 | - "", |
|
| 59 | - $value_inputted_for_field_on_model_object |
|
| 60 | - ); |
|
| 61 | - } |
|
| 62 | - return floatval($value_inputted_for_field_on_model_object); |
|
| 63 | - } |
|
| 31 | + /** |
|
| 32 | + * If provided a string, strips out number-related formatting, like commas, periods, spaces, other junk, etc. |
|
| 33 | + * However, treats commas and periods as thousand-separators ro decimal marks, as indicate by the config's currency. |
|
| 34 | + * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
| 35 | + * Returns a float |
|
| 36 | + * |
|
| 37 | + * @param float|string $value_inputted_for_field_on_model_object |
|
| 38 | + * @return float |
|
| 39 | + */ |
|
| 40 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
| 41 | + { |
|
| 42 | + // remove whitespaces and thousands separators |
|
| 43 | + if (is_string($value_inputted_for_field_on_model_object)) { |
|
| 44 | + $value_inputted_for_field_on_model_object = str_replace( |
|
| 45 | + array(" ", $this->currency->thsnds), |
|
| 46 | + "", |
|
| 47 | + $value_inputted_for_field_on_model_object |
|
| 48 | + ); |
|
| 49 | + // normalize it so periods are decimal marks (we don't care where you're from: we're talking PHP now) |
|
| 50 | + $value_inputted_for_field_on_model_object = str_replace( |
|
| 51 | + $this->currency->dec_mrk, |
|
| 52 | + ".", |
|
| 53 | + $value_inputted_for_field_on_model_object |
|
| 54 | + ); |
|
| 55 | + // double-check there's absolutely nothing left on this string besides numbers |
|
| 56 | + $value_inputted_for_field_on_model_object = preg_replace( |
|
| 57 | + "/[^0-9,.]/", |
|
| 58 | + "", |
|
| 59 | + $value_inputted_for_field_on_model_object |
|
| 60 | + ); |
|
| 61 | + } |
|
| 62 | + return floatval($value_inputted_for_field_on_model_object); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Returns the number formatted according to local custom (set by the country of the blog). |
|
| 67 | - * |
|
| 68 | - * @param float $value_on_field_to_be_outputted |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
| 72 | - { |
|
| 73 | - return number_format( |
|
| 74 | - $value_on_field_to_be_outputted, |
|
| 75 | - $this->currency->dec_plc, |
|
| 76 | - $this->currency->dec_mrk, |
|
| 77 | - $this->currency->thsnds |
|
| 78 | - ); |
|
| 79 | - } |
|
| 65 | + /** |
|
| 66 | + * Returns the number formatted according to local custom (set by the country of the blog). |
|
| 67 | + * |
|
| 68 | + * @param float $value_on_field_to_be_outputted |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
| 72 | + { |
|
| 73 | + return number_format( |
|
| 74 | + $value_on_field_to_be_outputted, |
|
| 75 | + $this->currency->dec_plc, |
|
| 76 | + $this->currency->dec_mrk, |
|
| 77 | + $this->currency->thsnds |
|
| 78 | + ); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
| 82 | - { |
|
| 83 | - return floatval($value_found_in_db_for_model_object); |
|
| 84 | - } |
|
| 81 | + public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
| 82 | + { |
|
| 83 | + return floatval($value_found_in_db_for_model_object); |
|
| 84 | + } |
|
| 85 | 85 | } |
@@ -29,643 +29,643 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | abstract class FormHandler implements FormHandlerInterface |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * will add opening and closing HTML form tags as well as a submit button |
|
| 34 | - */ |
|
| 35 | - const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * will add opening and closing HTML form tags but NOT a submit button |
|
| 39 | - */ |
|
| 40 | - const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * will NOT add opening and closing HTML form tags but will add a submit button |
|
| 44 | - */ |
|
| 45 | - const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * will NOT add opening and closing HTML form tags NOR a submit button |
|
| 49 | - */ |
|
| 50 | - const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * if set to false, then this form has no displayable content, |
|
| 54 | - * and will only be used for processing data sent passed via GET or POST |
|
| 55 | - * defaults to true ( ie: form has displayable content ) |
|
| 56 | - * |
|
| 57 | - * @var boolean $displayable |
|
| 58 | - */ |
|
| 59 | - private $displayable = true; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var string $form_name |
|
| 63 | - */ |
|
| 64 | - private $form_name; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var string $admin_name |
|
| 68 | - */ |
|
| 69 | - private $admin_name; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string $slug |
|
| 73 | - */ |
|
| 74 | - private $slug; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var string $submit_btn_text |
|
| 78 | - */ |
|
| 79 | - private $submit_btn_text; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @var string $form_action |
|
| 83 | - */ |
|
| 84 | - private $form_action; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * form params in key value pairs |
|
| 88 | - * can be added to form action URL or as hidden inputs |
|
| 89 | - * |
|
| 90 | - * @var array $form_args |
|
| 91 | - */ |
|
| 92 | - private $form_args = array(); |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * value of one of the string constant above |
|
| 96 | - * |
|
| 97 | - * @var string $form_config |
|
| 98 | - */ |
|
| 99 | - private $form_config; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * whether or not the form was determined to be invalid |
|
| 103 | - * |
|
| 104 | - * @var boolean $form_has_errors |
|
| 105 | - */ |
|
| 106 | - private $form_has_errors; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * the absolute top level form section being used on the page |
|
| 110 | - * |
|
| 111 | - * @var EE_Form_Section_Proper $form |
|
| 112 | - */ |
|
| 113 | - private $form; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @var EE_Registry $registry |
|
| 117 | - */ |
|
| 118 | - protected $registry; |
|
| 119 | - |
|
| 120 | - // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
| 121 | - /** |
|
| 122 | - * Form constructor. |
|
| 123 | - * |
|
| 124 | - * @param string $form_name |
|
| 125 | - * @param string $admin_name |
|
| 126 | - * @param string $slug |
|
| 127 | - * @param string $form_action |
|
| 128 | - * @param string $form_config |
|
| 129 | - * @param EE_Registry $registry |
|
| 130 | - * @throws InvalidDataTypeException |
|
| 131 | - * @throws DomainException |
|
| 132 | - * @throws InvalidArgumentException |
|
| 133 | - */ |
|
| 134 | - public function __construct( |
|
| 135 | - $form_name, |
|
| 136 | - $admin_name, |
|
| 137 | - $slug, |
|
| 138 | - $form_action = '', |
|
| 139 | - $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 140 | - EE_Registry $registry |
|
| 141 | - ) { |
|
| 142 | - $this->setFormName($form_name); |
|
| 143 | - $this->setAdminName($admin_name); |
|
| 144 | - $this->setSlug($slug); |
|
| 145 | - $this->setFormAction($form_action); |
|
| 146 | - $this->setFormConfig($form_config); |
|
| 147 | - $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
| 148 | - $this->registry = $registry; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @return array |
|
| 154 | - */ |
|
| 155 | - public static function getFormConfigConstants() |
|
| 156 | - { |
|
| 157 | - return array( |
|
| 158 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 159 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 160 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 161 | - FormHandler::DO_NOT_SETUP_FORM, |
|
| 162 | - ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param bool $for_display |
|
| 168 | - * @return EE_Form_Section_Proper |
|
| 169 | - * @throws EE_Error |
|
| 170 | - * @throws LogicException |
|
| 171 | - */ |
|
| 172 | - public function form($for_display = false) |
|
| 173 | - { |
|
| 174 | - if (! $this->formIsValid()) { |
|
| 175 | - return null; |
|
| 176 | - } |
|
| 177 | - if ($for_display) { |
|
| 178 | - $form_config = $this->formConfig(); |
|
| 179 | - if ( |
|
| 180 | - $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 181 | - || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
| 182 | - ) { |
|
| 183 | - $this->appendSubmitButton(); |
|
| 184 | - $this->clearFormButtonFloats(); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - return $this->form; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @return boolean |
|
| 193 | - * @throws LogicException |
|
| 194 | - */ |
|
| 195 | - public function formIsValid() |
|
| 196 | - { |
|
| 197 | - if ($this->form instanceof EE_Form_Section_Proper) { |
|
| 198 | - return true; |
|
| 199 | - } |
|
| 200 | - $form = apply_filters( |
|
| 201 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
| 202 | - $this->generate(), |
|
| 203 | - $this |
|
| 204 | - ); |
|
| 205 | - if ($this->verifyForm($form)) { |
|
| 206 | - $this->setForm($form); |
|
| 207 | - } |
|
| 208 | - return true; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @param EE_Form_Section_Proper|null $form |
|
| 214 | - * @return bool |
|
| 215 | - * @throws LogicException |
|
| 216 | - */ |
|
| 217 | - public function verifyForm(EE_Form_Section_Proper $form = null) |
|
| 218 | - { |
|
| 219 | - $form = $form !== null ? $form : $this->form; |
|
| 220 | - if ($form instanceof EE_Form_Section_Proper) { |
|
| 221 | - return true; |
|
| 222 | - } |
|
| 223 | - throw new LogicException( |
|
| 224 | - sprintf( |
|
| 225 | - esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
| 226 | - $this->form_name, |
|
| 227 | - var_export($form, true) |
|
| 228 | - ) |
|
| 229 | - ); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @param EE_Form_Section_Proper $form |
|
| 235 | - */ |
|
| 236 | - public function setForm(EE_Form_Section_Proper $form) |
|
| 237 | - { |
|
| 238 | - $this->form = $form; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * @return boolean |
|
| 244 | - */ |
|
| 245 | - public function displayable() |
|
| 246 | - { |
|
| 247 | - return $this->displayable; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * @param boolean $displayable |
|
| 253 | - */ |
|
| 254 | - public function setDisplayable($displayable = false) |
|
| 255 | - { |
|
| 256 | - $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * a public name for the form that can be displayed on the frontend of a site |
|
| 262 | - * |
|
| 263 | - * @return string |
|
| 264 | - */ |
|
| 265 | - public function formName() |
|
| 266 | - { |
|
| 267 | - return $this->form_name; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * @param string $form_name |
|
| 273 | - * @throws InvalidDataTypeException |
|
| 274 | - */ |
|
| 275 | - public function setFormName($form_name) |
|
| 276 | - { |
|
| 277 | - if (! is_string($form_name)) { |
|
| 278 | - throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
| 279 | - } |
|
| 280 | - $this->form_name = $form_name; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * a public name for the form that can be displayed, but only in the admin |
|
| 286 | - * |
|
| 287 | - * @return string |
|
| 288 | - */ |
|
| 289 | - public function adminName() |
|
| 290 | - { |
|
| 291 | - return $this->admin_name; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * @param string $admin_name |
|
| 297 | - * @throws InvalidDataTypeException |
|
| 298 | - */ |
|
| 299 | - public function setAdminName($admin_name) |
|
| 300 | - { |
|
| 301 | - if (! is_string($admin_name)) { |
|
| 302 | - throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
| 303 | - } |
|
| 304 | - $this->admin_name = $admin_name; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * a URL friendly string that can be used for identifying the form |
|
| 310 | - * |
|
| 311 | - * @return string |
|
| 312 | - */ |
|
| 313 | - public function slug() |
|
| 314 | - { |
|
| 315 | - return $this->slug; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * @param string $slug |
|
| 321 | - * @throws InvalidDataTypeException |
|
| 322 | - */ |
|
| 323 | - public function setSlug($slug) |
|
| 324 | - { |
|
| 325 | - if (! is_string($slug)) { |
|
| 326 | - throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
| 327 | - } |
|
| 328 | - $this->slug = $slug; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * @return string |
|
| 334 | - */ |
|
| 335 | - public function submitBtnText() |
|
| 336 | - { |
|
| 337 | - return $this->submit_btn_text; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * @param string $submit_btn_text |
|
| 343 | - * @throws InvalidDataTypeException |
|
| 344 | - * @throws InvalidArgumentException |
|
| 345 | - */ |
|
| 346 | - public function setSubmitBtnText($submit_btn_text) |
|
| 347 | - { |
|
| 348 | - if (! is_string($submit_btn_text)) { |
|
| 349 | - throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
| 350 | - } |
|
| 351 | - if (empty($submit_btn_text)) { |
|
| 352 | - throw new InvalidArgumentException( |
|
| 353 | - esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
| 354 | - ); |
|
| 355 | - } |
|
| 356 | - $this->submit_btn_text = $submit_btn_text; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * @return string |
|
| 362 | - */ |
|
| 363 | - public function formAction() |
|
| 364 | - { |
|
| 365 | - return ! empty($this->form_args) |
|
| 366 | - ? add_query_arg($this->form_args, $this->form_action) |
|
| 367 | - : $this->form_action; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * @param string $form_action |
|
| 373 | - * @throws InvalidDataTypeException |
|
| 374 | - */ |
|
| 375 | - public function setFormAction($form_action) |
|
| 376 | - { |
|
| 377 | - if (! is_string($form_action)) { |
|
| 378 | - throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
| 379 | - } |
|
| 380 | - $this->form_action = $form_action; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * @param array $form_args |
|
| 386 | - * @throws InvalidDataTypeException |
|
| 387 | - * @throws InvalidArgumentException |
|
| 388 | - */ |
|
| 389 | - public function addFormActionArgs($form_args = array()) |
|
| 390 | - { |
|
| 391 | - if (is_object($form_args)) { |
|
| 392 | - throw new InvalidDataTypeException( |
|
| 393 | - '$form_args', |
|
| 394 | - $form_args, |
|
| 395 | - 'anything other than an object was expected.' |
|
| 396 | - ); |
|
| 397 | - } |
|
| 398 | - if (empty($form_args)) { |
|
| 399 | - throw new InvalidArgumentException( |
|
| 400 | - esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
| 401 | - ); |
|
| 402 | - } |
|
| 403 | - $this->form_args = array_merge($this->form_args, $form_args); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * @return string |
|
| 409 | - */ |
|
| 410 | - public function formConfig() |
|
| 411 | - { |
|
| 412 | - return $this->form_config; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * @param string $form_config |
|
| 418 | - * @throws DomainException |
|
| 419 | - */ |
|
| 420 | - public function setFormConfig($form_config) |
|
| 421 | - { |
|
| 422 | - if ( |
|
| 423 | - ! in_array( |
|
| 424 | - $form_config, |
|
| 425 | - array( |
|
| 426 | - FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 427 | - FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 428 | - FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 429 | - FormHandler::DO_NOT_SETUP_FORM, |
|
| 430 | - ), |
|
| 431 | - true |
|
| 432 | - ) |
|
| 433 | - ) { |
|
| 434 | - throw new DomainException( |
|
| 435 | - sprintf( |
|
| 436 | - esc_html__( |
|
| 437 | - '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
| 438 | - 'event_espresso' |
|
| 439 | - ), |
|
| 440 | - $form_config |
|
| 441 | - ) |
|
| 442 | - ); |
|
| 443 | - } |
|
| 444 | - $this->form_config = $form_config; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * called after the form is instantiated |
|
| 450 | - * and used for performing any logic that needs to occur early |
|
| 451 | - * before any of the other methods are called. |
|
| 452 | - * returns true if everything is ok to proceed, |
|
| 453 | - * and false if no further form logic should be implemented |
|
| 454 | - * |
|
| 455 | - * @return boolean |
|
| 456 | - */ |
|
| 457 | - public function initialize() |
|
| 458 | - { |
|
| 459 | - $this->form_has_errors = EE_Error::has_error(true); |
|
| 460 | - return true; |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * used for setting up css and js |
|
| 466 | - * |
|
| 467 | - * @return void |
|
| 468 | - * @throws LogicException |
|
| 469 | - * @throws EE_Error |
|
| 470 | - */ |
|
| 471 | - public function enqueueStylesAndScripts() |
|
| 472 | - { |
|
| 473 | - $this->form()->enqueue_js(); |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * creates and returns the actual form |
|
| 479 | - * |
|
| 480 | - * @return EE_Form_Section_Proper |
|
| 481 | - */ |
|
| 482 | - abstract public function generate(); |
|
| 483 | - |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * creates and returns an EE_Submit_Input labeled "Submit" |
|
| 487 | - * |
|
| 488 | - * @param string $text |
|
| 489 | - * @return EE_Submit_Input |
|
| 490 | - */ |
|
| 491 | - public function generateSubmitButton($text = '') |
|
| 492 | - { |
|
| 493 | - $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
| 494 | - return new EE_Submit_Input( |
|
| 495 | - array( |
|
| 496 | - 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
| 497 | - 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
| 498 | - 'html_class' => 'ee-form-submit', |
|
| 499 | - 'html_label' => ' ', |
|
| 500 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 501 | - 'default' => $text, |
|
| 502 | - ) |
|
| 503 | - ); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
| 509 | - * |
|
| 510 | - * @param string $text |
|
| 511 | - * @return void |
|
| 512 | - * @throws EE_Error |
|
| 513 | - */ |
|
| 514 | - public function appendSubmitButton($text = '') |
|
| 515 | - { |
|
| 516 | - if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
| 517 | - return; |
|
| 518 | - } |
|
| 519 | - $this->form->add_subsections( |
|
| 520 | - array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
| 521 | - null, |
|
| 522 | - false |
|
| 523 | - ); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * creates and returns an EE_Submit_Input labeled "Cancel" |
|
| 529 | - * |
|
| 530 | - * @param string $text |
|
| 531 | - * @return EE_Submit_Input |
|
| 532 | - */ |
|
| 533 | - public function generateCancelButton($text = '') |
|
| 534 | - { |
|
| 535 | - $cancel_button = new EE_Submit_Input( |
|
| 536 | - array( |
|
| 537 | - 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
| 538 | - 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
| 539 | - 'html_class' => 'ee-cancel-form', |
|
| 540 | - 'html_label' => ' ', |
|
| 541 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 542 | - 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
| 543 | - ) |
|
| 544 | - ); |
|
| 545 | - $cancel_button->set_button_css_attributes(false); |
|
| 546 | - return $cancel_button; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * appends a float clearing div onto end of form |
|
| 552 | - * |
|
| 553 | - * @return void |
|
| 554 | - * @throws EE_Error |
|
| 555 | - */ |
|
| 556 | - public function clearFormButtonFloats() |
|
| 557 | - { |
|
| 558 | - $this->form->add_subsections( |
|
| 559 | - array( |
|
| 560 | - 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
| 561 | - EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
| 562 | - ), |
|
| 563 | - ), |
|
| 564 | - null, |
|
| 565 | - false |
|
| 566 | - ); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
| 572 | - * returns a string of HTML that can be directly echoed in a template |
|
| 573 | - * |
|
| 574 | - * @return string |
|
| 575 | - * @throws InvalidArgumentException |
|
| 576 | - * @throws InvalidInterfaceException |
|
| 577 | - * @throws InvalidDataTypeException |
|
| 578 | - * @throws LogicException |
|
| 579 | - * @throws EE_Error |
|
| 580 | - */ |
|
| 581 | - public function display() |
|
| 582 | - { |
|
| 583 | - $form_html = apply_filters( |
|
| 584 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
| 585 | - '' |
|
| 586 | - ); |
|
| 587 | - $form_config = $this->formConfig(); |
|
| 588 | - if ( |
|
| 589 | - $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 590 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 591 | - ) { |
|
| 592 | - $additional_props = $this->requiresMultipartEnctype() |
|
| 593 | - ? 'enctype="multipart/form-data"' |
|
| 594 | - : ''; |
|
| 595 | - $form_html .= $this->form()->form_open( |
|
| 596 | - $this->formAction(), |
|
| 597 | - 'POST', |
|
| 598 | - $additional_props |
|
| 599 | - ); |
|
| 600 | - } |
|
| 601 | - $form_html .= $this->form(true)->get_html(); |
|
| 602 | - if ( |
|
| 603 | - $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 604 | - || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 605 | - ) { |
|
| 606 | - $form_html .= $this->form()->form_close(); |
|
| 607 | - } |
|
| 608 | - $form_html .= apply_filters( |
|
| 609 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
| 610 | - '' |
|
| 611 | - ); |
|
| 612 | - return $form_html; |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Determines if this form needs "enctype='multipart/form-data'" or not. |
|
| 617 | - * @since 4.9.80.p |
|
| 618 | - * @return bool |
|
| 619 | - * @throws EE_Error |
|
| 620 | - */ |
|
| 621 | - public function requiresMultipartEnctype() |
|
| 622 | - { |
|
| 623 | - foreach ($this->form()->inputs_in_subsections() as $input) { |
|
| 624 | - if ($input instanceof EE_File_Input) { |
|
| 625 | - return true; |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - return false; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * handles processing the form submission |
|
| 634 | - * returns true or false depending on whether the form was processed successfully or not |
|
| 635 | - * |
|
| 636 | - * @param array $submitted_form_data |
|
| 637 | - * @return array |
|
| 638 | - * @throws InvalidArgumentException |
|
| 639 | - * @throws InvalidInterfaceException |
|
| 640 | - * @throws InvalidDataTypeException |
|
| 641 | - * @throws EE_Error |
|
| 642 | - * @throws LogicException |
|
| 643 | - * @throws InvalidFormSubmissionException |
|
| 644 | - */ |
|
| 645 | - public function process($submitted_form_data = array()) |
|
| 646 | - { |
|
| 647 | - if (! $this->form()->was_submitted($submitted_form_data)) { |
|
| 648 | - throw new InvalidFormSubmissionException($this->form_name); |
|
| 649 | - } |
|
| 650 | - $this->form(true)->receive_form_submission($submitted_form_data); |
|
| 32 | + /** |
|
| 33 | + * will add opening and closing HTML form tags as well as a submit button |
|
| 34 | + */ |
|
| 35 | + const ADD_FORM_TAGS_AND_SUBMIT = 'add_form_tags_and_submit'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * will add opening and closing HTML form tags but NOT a submit button |
|
| 39 | + */ |
|
| 40 | + const ADD_FORM_TAGS_ONLY = 'add_form_tags_only'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * will NOT add opening and closing HTML form tags but will add a submit button |
|
| 44 | + */ |
|
| 45 | + const ADD_FORM_SUBMIT_ONLY = 'add_form_submit_only'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * will NOT add opening and closing HTML form tags NOR a submit button |
|
| 49 | + */ |
|
| 50 | + const DO_NOT_SETUP_FORM = 'do_not_setup_form'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * if set to false, then this form has no displayable content, |
|
| 54 | + * and will only be used for processing data sent passed via GET or POST |
|
| 55 | + * defaults to true ( ie: form has displayable content ) |
|
| 56 | + * |
|
| 57 | + * @var boolean $displayable |
|
| 58 | + */ |
|
| 59 | + private $displayable = true; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var string $form_name |
|
| 63 | + */ |
|
| 64 | + private $form_name; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var string $admin_name |
|
| 68 | + */ |
|
| 69 | + private $admin_name; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string $slug |
|
| 73 | + */ |
|
| 74 | + private $slug; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var string $submit_btn_text |
|
| 78 | + */ |
|
| 79 | + private $submit_btn_text; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @var string $form_action |
|
| 83 | + */ |
|
| 84 | + private $form_action; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * form params in key value pairs |
|
| 88 | + * can be added to form action URL or as hidden inputs |
|
| 89 | + * |
|
| 90 | + * @var array $form_args |
|
| 91 | + */ |
|
| 92 | + private $form_args = array(); |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * value of one of the string constant above |
|
| 96 | + * |
|
| 97 | + * @var string $form_config |
|
| 98 | + */ |
|
| 99 | + private $form_config; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * whether or not the form was determined to be invalid |
|
| 103 | + * |
|
| 104 | + * @var boolean $form_has_errors |
|
| 105 | + */ |
|
| 106 | + private $form_has_errors; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * the absolute top level form section being used on the page |
|
| 110 | + * |
|
| 111 | + * @var EE_Form_Section_Proper $form |
|
| 112 | + */ |
|
| 113 | + private $form; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @var EE_Registry $registry |
|
| 117 | + */ |
|
| 118 | + protected $registry; |
|
| 119 | + |
|
| 120 | + // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd |
|
| 121 | + /** |
|
| 122 | + * Form constructor. |
|
| 123 | + * |
|
| 124 | + * @param string $form_name |
|
| 125 | + * @param string $admin_name |
|
| 126 | + * @param string $slug |
|
| 127 | + * @param string $form_action |
|
| 128 | + * @param string $form_config |
|
| 129 | + * @param EE_Registry $registry |
|
| 130 | + * @throws InvalidDataTypeException |
|
| 131 | + * @throws DomainException |
|
| 132 | + * @throws InvalidArgumentException |
|
| 133 | + */ |
|
| 134 | + public function __construct( |
|
| 135 | + $form_name, |
|
| 136 | + $admin_name, |
|
| 137 | + $slug, |
|
| 138 | + $form_action = '', |
|
| 139 | + $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 140 | + EE_Registry $registry |
|
| 141 | + ) { |
|
| 142 | + $this->setFormName($form_name); |
|
| 143 | + $this->setAdminName($admin_name); |
|
| 144 | + $this->setSlug($slug); |
|
| 145 | + $this->setFormAction($form_action); |
|
| 146 | + $this->setFormConfig($form_config); |
|
| 147 | + $this->setSubmitBtnText(esc_html__('Submit', 'event_espresso')); |
|
| 148 | + $this->registry = $registry; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @return array |
|
| 154 | + */ |
|
| 155 | + public static function getFormConfigConstants() |
|
| 156 | + { |
|
| 157 | + return array( |
|
| 158 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 159 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 160 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 161 | + FormHandler::DO_NOT_SETUP_FORM, |
|
| 162 | + ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param bool $for_display |
|
| 168 | + * @return EE_Form_Section_Proper |
|
| 169 | + * @throws EE_Error |
|
| 170 | + * @throws LogicException |
|
| 171 | + */ |
|
| 172 | + public function form($for_display = false) |
|
| 173 | + { |
|
| 174 | + if (! $this->formIsValid()) { |
|
| 175 | + return null; |
|
| 176 | + } |
|
| 177 | + if ($for_display) { |
|
| 178 | + $form_config = $this->formConfig(); |
|
| 179 | + if ( |
|
| 180 | + $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 181 | + || $form_config === FormHandler::ADD_FORM_SUBMIT_ONLY |
|
| 182 | + ) { |
|
| 183 | + $this->appendSubmitButton(); |
|
| 184 | + $this->clearFormButtonFloats(); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + return $this->form; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @return boolean |
|
| 193 | + * @throws LogicException |
|
| 194 | + */ |
|
| 195 | + public function formIsValid() |
|
| 196 | + { |
|
| 197 | + if ($this->form instanceof EE_Form_Section_Proper) { |
|
| 198 | + return true; |
|
| 199 | + } |
|
| 200 | + $form = apply_filters( |
|
| 201 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__formIsValid__generated_form_object', |
|
| 202 | + $this->generate(), |
|
| 203 | + $this |
|
| 204 | + ); |
|
| 205 | + if ($this->verifyForm($form)) { |
|
| 206 | + $this->setForm($form); |
|
| 207 | + } |
|
| 208 | + return true; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @param EE_Form_Section_Proper|null $form |
|
| 214 | + * @return bool |
|
| 215 | + * @throws LogicException |
|
| 216 | + */ |
|
| 217 | + public function verifyForm(EE_Form_Section_Proper $form = null) |
|
| 218 | + { |
|
| 219 | + $form = $form !== null ? $form : $this->form; |
|
| 220 | + if ($form instanceof EE_Form_Section_Proper) { |
|
| 221 | + return true; |
|
| 222 | + } |
|
| 223 | + throw new LogicException( |
|
| 224 | + sprintf( |
|
| 225 | + esc_html__('The "%1$s" form is invalid or missing. %2$s', 'event_espresso'), |
|
| 226 | + $this->form_name, |
|
| 227 | + var_export($form, true) |
|
| 228 | + ) |
|
| 229 | + ); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @param EE_Form_Section_Proper $form |
|
| 235 | + */ |
|
| 236 | + public function setForm(EE_Form_Section_Proper $form) |
|
| 237 | + { |
|
| 238 | + $this->form = $form; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * @return boolean |
|
| 244 | + */ |
|
| 245 | + public function displayable() |
|
| 246 | + { |
|
| 247 | + return $this->displayable; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * @param boolean $displayable |
|
| 253 | + */ |
|
| 254 | + public function setDisplayable($displayable = false) |
|
| 255 | + { |
|
| 256 | + $this->displayable = filter_var($displayable, FILTER_VALIDATE_BOOLEAN); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * a public name for the form that can be displayed on the frontend of a site |
|
| 262 | + * |
|
| 263 | + * @return string |
|
| 264 | + */ |
|
| 265 | + public function formName() |
|
| 266 | + { |
|
| 267 | + return $this->form_name; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * @param string $form_name |
|
| 273 | + * @throws InvalidDataTypeException |
|
| 274 | + */ |
|
| 275 | + public function setFormName($form_name) |
|
| 276 | + { |
|
| 277 | + if (! is_string($form_name)) { |
|
| 278 | + throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
|
| 279 | + } |
|
| 280 | + $this->form_name = $form_name; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * a public name for the form that can be displayed, but only in the admin |
|
| 286 | + * |
|
| 287 | + * @return string |
|
| 288 | + */ |
|
| 289 | + public function adminName() |
|
| 290 | + { |
|
| 291 | + return $this->admin_name; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * @param string $admin_name |
|
| 297 | + * @throws InvalidDataTypeException |
|
| 298 | + */ |
|
| 299 | + public function setAdminName($admin_name) |
|
| 300 | + { |
|
| 301 | + if (! is_string($admin_name)) { |
|
| 302 | + throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
|
| 303 | + } |
|
| 304 | + $this->admin_name = $admin_name; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * a URL friendly string that can be used for identifying the form |
|
| 310 | + * |
|
| 311 | + * @return string |
|
| 312 | + */ |
|
| 313 | + public function slug() |
|
| 314 | + { |
|
| 315 | + return $this->slug; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * @param string $slug |
|
| 321 | + * @throws InvalidDataTypeException |
|
| 322 | + */ |
|
| 323 | + public function setSlug($slug) |
|
| 324 | + { |
|
| 325 | + if (! is_string($slug)) { |
|
| 326 | + throw new InvalidDataTypeException('$slug', $slug, 'string'); |
|
| 327 | + } |
|
| 328 | + $this->slug = $slug; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * @return string |
|
| 334 | + */ |
|
| 335 | + public function submitBtnText() |
|
| 336 | + { |
|
| 337 | + return $this->submit_btn_text; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * @param string $submit_btn_text |
|
| 343 | + * @throws InvalidDataTypeException |
|
| 344 | + * @throws InvalidArgumentException |
|
| 345 | + */ |
|
| 346 | + public function setSubmitBtnText($submit_btn_text) |
|
| 347 | + { |
|
| 348 | + if (! is_string($submit_btn_text)) { |
|
| 349 | + throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
|
| 350 | + } |
|
| 351 | + if (empty($submit_btn_text)) { |
|
| 352 | + throw new InvalidArgumentException( |
|
| 353 | + esc_html__('Can not set Submit button text because an empty string was provided.', 'event_espresso') |
|
| 354 | + ); |
|
| 355 | + } |
|
| 356 | + $this->submit_btn_text = $submit_btn_text; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * @return string |
|
| 362 | + */ |
|
| 363 | + public function formAction() |
|
| 364 | + { |
|
| 365 | + return ! empty($this->form_args) |
|
| 366 | + ? add_query_arg($this->form_args, $this->form_action) |
|
| 367 | + : $this->form_action; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * @param string $form_action |
|
| 373 | + * @throws InvalidDataTypeException |
|
| 374 | + */ |
|
| 375 | + public function setFormAction($form_action) |
|
| 376 | + { |
|
| 377 | + if (! is_string($form_action)) { |
|
| 378 | + throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
|
| 379 | + } |
|
| 380 | + $this->form_action = $form_action; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * @param array $form_args |
|
| 386 | + * @throws InvalidDataTypeException |
|
| 387 | + * @throws InvalidArgumentException |
|
| 388 | + */ |
|
| 389 | + public function addFormActionArgs($form_args = array()) |
|
| 390 | + { |
|
| 391 | + if (is_object($form_args)) { |
|
| 392 | + throw new InvalidDataTypeException( |
|
| 393 | + '$form_args', |
|
| 394 | + $form_args, |
|
| 395 | + 'anything other than an object was expected.' |
|
| 396 | + ); |
|
| 397 | + } |
|
| 398 | + if (empty($form_args)) { |
|
| 399 | + throw new InvalidArgumentException( |
|
| 400 | + esc_html__('The redirect arguments can not be an empty array.', 'event_espresso') |
|
| 401 | + ); |
|
| 402 | + } |
|
| 403 | + $this->form_args = array_merge($this->form_args, $form_args); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * @return string |
|
| 409 | + */ |
|
| 410 | + public function formConfig() |
|
| 411 | + { |
|
| 412 | + return $this->form_config; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * @param string $form_config |
|
| 418 | + * @throws DomainException |
|
| 419 | + */ |
|
| 420 | + public function setFormConfig($form_config) |
|
| 421 | + { |
|
| 422 | + if ( |
|
| 423 | + ! in_array( |
|
| 424 | + $form_config, |
|
| 425 | + array( |
|
| 426 | + FormHandler::ADD_FORM_TAGS_AND_SUBMIT, |
|
| 427 | + FormHandler::ADD_FORM_TAGS_ONLY, |
|
| 428 | + FormHandler::ADD_FORM_SUBMIT_ONLY, |
|
| 429 | + FormHandler::DO_NOT_SETUP_FORM, |
|
| 430 | + ), |
|
| 431 | + true |
|
| 432 | + ) |
|
| 433 | + ) { |
|
| 434 | + throw new DomainException( |
|
| 435 | + sprintf( |
|
| 436 | + esc_html__( |
|
| 437 | + '"%1$s" is not a valid value for the form config. Please use one of the class constants on \EventEspresso\core\libraries\form_sections\form_handlers\Form', |
|
| 438 | + 'event_espresso' |
|
| 439 | + ), |
|
| 440 | + $form_config |
|
| 441 | + ) |
|
| 442 | + ); |
|
| 443 | + } |
|
| 444 | + $this->form_config = $form_config; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * called after the form is instantiated |
|
| 450 | + * and used for performing any logic that needs to occur early |
|
| 451 | + * before any of the other methods are called. |
|
| 452 | + * returns true if everything is ok to proceed, |
|
| 453 | + * and false if no further form logic should be implemented |
|
| 454 | + * |
|
| 455 | + * @return boolean |
|
| 456 | + */ |
|
| 457 | + public function initialize() |
|
| 458 | + { |
|
| 459 | + $this->form_has_errors = EE_Error::has_error(true); |
|
| 460 | + return true; |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * used for setting up css and js |
|
| 466 | + * |
|
| 467 | + * @return void |
|
| 468 | + * @throws LogicException |
|
| 469 | + * @throws EE_Error |
|
| 470 | + */ |
|
| 471 | + public function enqueueStylesAndScripts() |
|
| 472 | + { |
|
| 473 | + $this->form()->enqueue_js(); |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * creates and returns the actual form |
|
| 479 | + * |
|
| 480 | + * @return EE_Form_Section_Proper |
|
| 481 | + */ |
|
| 482 | + abstract public function generate(); |
|
| 483 | + |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * creates and returns an EE_Submit_Input labeled "Submit" |
|
| 487 | + * |
|
| 488 | + * @param string $text |
|
| 489 | + * @return EE_Submit_Input |
|
| 490 | + */ |
|
| 491 | + public function generateSubmitButton($text = '') |
|
| 492 | + { |
|
| 493 | + $text = ! empty($text) ? $text : $this->submitBtnText(); |
|
| 494 | + return new EE_Submit_Input( |
|
| 495 | + array( |
|
| 496 | + 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
| 497 | + 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
| 498 | + 'html_class' => 'ee-form-submit', |
|
| 499 | + 'html_label' => ' ', |
|
| 500 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 501 | + 'default' => $text, |
|
| 502 | + ) |
|
| 503 | + ); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * calls generateSubmitButton() and appends it onto the form along with a float clearing div |
|
| 509 | + * |
|
| 510 | + * @param string $text |
|
| 511 | + * @return void |
|
| 512 | + * @throws EE_Error |
|
| 513 | + */ |
|
| 514 | + public function appendSubmitButton($text = '') |
|
| 515 | + { |
|
| 516 | + if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
| 517 | + return; |
|
| 518 | + } |
|
| 519 | + $this->form->add_subsections( |
|
| 520 | + array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
| 521 | + null, |
|
| 522 | + false |
|
| 523 | + ); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * creates and returns an EE_Submit_Input labeled "Cancel" |
|
| 529 | + * |
|
| 530 | + * @param string $text |
|
| 531 | + * @return EE_Submit_Input |
|
| 532 | + */ |
|
| 533 | + public function generateCancelButton($text = '') |
|
| 534 | + { |
|
| 535 | + $cancel_button = new EE_Submit_Input( |
|
| 536 | + array( |
|
| 537 | + 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
| 538 | + 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
| 539 | + 'html_class' => 'ee-cancel-form', |
|
| 540 | + 'html_label' => ' ', |
|
| 541 | + 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 542 | + 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
|
| 543 | + ) |
|
| 544 | + ); |
|
| 545 | + $cancel_button->set_button_css_attributes(false); |
|
| 546 | + return $cancel_button; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * appends a float clearing div onto end of form |
|
| 552 | + * |
|
| 553 | + * @return void |
|
| 554 | + * @throws EE_Error |
|
| 555 | + */ |
|
| 556 | + public function clearFormButtonFloats() |
|
| 557 | + { |
|
| 558 | + $this->form->add_subsections( |
|
| 559 | + array( |
|
| 560 | + 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
|
| 561 | + EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
| 562 | + ), |
|
| 563 | + ), |
|
| 564 | + null, |
|
| 565 | + false |
|
| 566 | + ); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * takes the generated form and displays it along with ony other non-form HTML that may be required |
|
| 572 | + * returns a string of HTML that can be directly echoed in a template |
|
| 573 | + * |
|
| 574 | + * @return string |
|
| 575 | + * @throws InvalidArgumentException |
|
| 576 | + * @throws InvalidInterfaceException |
|
| 577 | + * @throws InvalidDataTypeException |
|
| 578 | + * @throws LogicException |
|
| 579 | + * @throws EE_Error |
|
| 580 | + */ |
|
| 581 | + public function display() |
|
| 582 | + { |
|
| 583 | + $form_html = apply_filters( |
|
| 584 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__before_form', |
|
| 585 | + '' |
|
| 586 | + ); |
|
| 587 | + $form_config = $this->formConfig(); |
|
| 588 | + if ( |
|
| 589 | + $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 590 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 591 | + ) { |
|
| 592 | + $additional_props = $this->requiresMultipartEnctype() |
|
| 593 | + ? 'enctype="multipart/form-data"' |
|
| 594 | + : ''; |
|
| 595 | + $form_html .= $this->form()->form_open( |
|
| 596 | + $this->formAction(), |
|
| 597 | + 'POST', |
|
| 598 | + $additional_props |
|
| 599 | + ); |
|
| 600 | + } |
|
| 601 | + $form_html .= $this->form(true)->get_html(); |
|
| 602 | + if ( |
|
| 603 | + $form_config === FormHandler::ADD_FORM_TAGS_AND_SUBMIT |
|
| 604 | + || $form_config === FormHandler::ADD_FORM_TAGS_ONLY |
|
| 605 | + ) { |
|
| 606 | + $form_html .= $this->form()->form_close(); |
|
| 607 | + } |
|
| 608 | + $form_html .= apply_filters( |
|
| 609 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__display__after_form', |
|
| 610 | + '' |
|
| 611 | + ); |
|
| 612 | + return $form_html; |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Determines if this form needs "enctype='multipart/form-data'" or not. |
|
| 617 | + * @since 4.9.80.p |
|
| 618 | + * @return bool |
|
| 619 | + * @throws EE_Error |
|
| 620 | + */ |
|
| 621 | + public function requiresMultipartEnctype() |
|
| 622 | + { |
|
| 623 | + foreach ($this->form()->inputs_in_subsections() as $input) { |
|
| 624 | + if ($input instanceof EE_File_Input) { |
|
| 625 | + return true; |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + return false; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * handles processing the form submission |
|
| 634 | + * returns true or false depending on whether the form was processed successfully or not |
|
| 635 | + * |
|
| 636 | + * @param array $submitted_form_data |
|
| 637 | + * @return array |
|
| 638 | + * @throws InvalidArgumentException |
|
| 639 | + * @throws InvalidInterfaceException |
|
| 640 | + * @throws InvalidDataTypeException |
|
| 641 | + * @throws EE_Error |
|
| 642 | + * @throws LogicException |
|
| 643 | + * @throws InvalidFormSubmissionException |
|
| 644 | + */ |
|
| 645 | + public function process($submitted_form_data = array()) |
|
| 646 | + { |
|
| 647 | + if (! $this->form()->was_submitted($submitted_form_data)) { |
|
| 648 | + throw new InvalidFormSubmissionException($this->form_name); |
|
| 649 | + } |
|
| 650 | + $this->form(true)->receive_form_submission($submitted_form_data); |
|
| 651 | 651 | if (! $this->form()->is_valid()) { |
| 652 | 652 | throw new InvalidFormSubmissionException( |
| 653 | - $this->form_name, |
|
| 654 | - sprintf( |
|
| 655 | - esc_html__( |
|
| 656 | - 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
| 657 | - 'event_espresso' |
|
| 658 | - ), |
|
| 659 | - $this->form_name, |
|
| 660 | - '<br />', |
|
| 661 | - implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
| 662 | - ) |
|
| 663 | - ); |
|
| 664 | - } |
|
| 665 | - return apply_filters( |
|
| 666 | - 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
| 667 | - $this->form()->valid_data(), |
|
| 668 | - $this |
|
| 669 | - ); |
|
| 670 | - } |
|
| 653 | + $this->form_name, |
|
| 654 | + sprintf( |
|
| 655 | + esc_html__( |
|
| 656 | + 'The "%1$s" form is invalid. Please correct the following errors and resubmit: %2$s %3$s', |
|
| 657 | + 'event_espresso' |
|
| 658 | + ), |
|
| 659 | + $this->form_name, |
|
| 660 | + '<br />', |
|
| 661 | + implode('<br />', $this->form()->get_validation_errors_accumulated()) |
|
| 662 | + ) |
|
| 663 | + ); |
|
| 664 | + } |
|
| 665 | + return apply_filters( |
|
| 666 | + 'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_FormHandler__process__valid_data', |
|
| 667 | + $this->form()->valid_data(), |
|
| 668 | + $this |
|
| 669 | + ); |
|
| 670 | + } |
|
| 671 | 671 | } |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | */ |
| 172 | 172 | public function form($for_display = false) |
| 173 | 173 | { |
| 174 | - if (! $this->formIsValid()) { |
|
| 174 | + if ( ! $this->formIsValid()) { |
|
| 175 | 175 | return null; |
| 176 | 176 | } |
| 177 | 177 | if ($for_display) { |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | */ |
| 275 | 275 | public function setFormName($form_name) |
| 276 | 276 | { |
| 277 | - if (! is_string($form_name)) { |
|
| 277 | + if ( ! is_string($form_name)) { |
|
| 278 | 278 | throw new InvalidDataTypeException('$form_name', $form_name, 'string'); |
| 279 | 279 | } |
| 280 | 280 | $this->form_name = $form_name; |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | */ |
| 299 | 299 | public function setAdminName($admin_name) |
| 300 | 300 | { |
| 301 | - if (! is_string($admin_name)) { |
|
| 301 | + if ( ! is_string($admin_name)) { |
|
| 302 | 302 | throw new InvalidDataTypeException('$admin_name', $admin_name, 'string'); |
| 303 | 303 | } |
| 304 | 304 | $this->admin_name = $admin_name; |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | */ |
| 323 | 323 | public function setSlug($slug) |
| 324 | 324 | { |
| 325 | - if (! is_string($slug)) { |
|
| 325 | + if ( ! is_string($slug)) { |
|
| 326 | 326 | throw new InvalidDataTypeException('$slug', $slug, 'string'); |
| 327 | 327 | } |
| 328 | 328 | $this->slug = $slug; |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | */ |
| 346 | 346 | public function setSubmitBtnText($submit_btn_text) |
| 347 | 347 | { |
| 348 | - if (! is_string($submit_btn_text)) { |
|
| 348 | + if ( ! is_string($submit_btn_text)) { |
|
| 349 | 349 | throw new InvalidDataTypeException('$submit_btn_text', $submit_btn_text, 'string'); |
| 350 | 350 | } |
| 351 | 351 | if (empty($submit_btn_text)) { |
@@ -374,7 +374,7 @@ discard block |
||
| 374 | 374 | */ |
| 375 | 375 | public function setFormAction($form_action) |
| 376 | 376 | { |
| 377 | - if (! is_string($form_action)) { |
|
| 377 | + if ( ! is_string($form_action)) { |
|
| 378 | 378 | throw new InvalidDataTypeException('$form_action', $form_action, 'string'); |
| 379 | 379 | } |
| 380 | 380 | $this->form_action = $form_action; |
@@ -493,11 +493,11 @@ discard block |
||
| 493 | 493 | $text = ! empty($text) ? $text : $this->submitBtnText(); |
| 494 | 494 | return new EE_Submit_Input( |
| 495 | 495 | array( |
| 496 | - 'html_name' => 'ee-form-submit-' . $this->slug(), |
|
| 497 | - 'html_id' => 'ee-form-submit-' . $this->slug(), |
|
| 496 | + 'html_name' => 'ee-form-submit-'.$this->slug(), |
|
| 497 | + 'html_id' => 'ee-form-submit-'.$this->slug(), |
|
| 498 | 498 | 'html_class' => 'ee-form-submit', |
| 499 | 499 | 'html_label' => ' ', |
| 500 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 500 | + 'other_html_attributes' => ' rel="'.$this->slug().'"', |
|
| 501 | 501 | 'default' => $text, |
| 502 | 502 | ) |
| 503 | 503 | ); |
@@ -513,11 +513,11 @@ discard block |
||
| 513 | 513 | */ |
| 514 | 514 | public function appendSubmitButton($text = '') |
| 515 | 515 | { |
| 516 | - if ($this->form->subsection_exists($this->slug() . '-submit-btn')) { |
|
| 516 | + if ($this->form->subsection_exists($this->slug().'-submit-btn')) { |
|
| 517 | 517 | return; |
| 518 | 518 | } |
| 519 | 519 | $this->form->add_subsections( |
| 520 | - array($this->slug() . '-submit-btn' => $this->generateSubmitButton($text)), |
|
| 520 | + array($this->slug().'-submit-btn' => $this->generateSubmitButton($text)), |
|
| 521 | 521 | null, |
| 522 | 522 | false |
| 523 | 523 | ); |
@@ -534,11 +534,11 @@ discard block |
||
| 534 | 534 | { |
| 535 | 535 | $cancel_button = new EE_Submit_Input( |
| 536 | 536 | array( |
| 537 | - 'html_name' => 'ee-form-submit-' . $this->slug(), // YES! Same name as submit !!! |
|
| 538 | - 'html_id' => 'ee-cancel-form-' . $this->slug(), |
|
| 537 | + 'html_name' => 'ee-form-submit-'.$this->slug(), // YES! Same name as submit !!! |
|
| 538 | + 'html_id' => 'ee-cancel-form-'.$this->slug(), |
|
| 539 | 539 | 'html_class' => 'ee-cancel-form', |
| 540 | 540 | 'html_label' => ' ', |
| 541 | - 'other_html_attributes' => ' rel="' . $this->slug() . '"', |
|
| 541 | + 'other_html_attributes' => ' rel="'.$this->slug().'"', |
|
| 542 | 542 | 'default' => ! empty($text) ? $text : esc_html__('Cancel', 'event_espresso'), |
| 543 | 543 | ) |
| 544 | 544 | ); |
@@ -558,7 +558,7 @@ discard block |
||
| 558 | 558 | $this->form->add_subsections( |
| 559 | 559 | array( |
| 560 | 560 | 'clear-submit-btn-float' => new EE_Form_Section_HTML( |
| 561 | - EEH_HTML::div('', '', 'clear-float') . EEH_HTML::divx() |
|
| 561 | + EEH_HTML::div('', '', 'clear-float').EEH_HTML::divx() |
|
| 562 | 562 | ), |
| 563 | 563 | ), |
| 564 | 564 | null, |
@@ -644,11 +644,11 @@ discard block |
||
| 644 | 644 | */ |
| 645 | 645 | public function process($submitted_form_data = array()) |
| 646 | 646 | { |
| 647 | - if (! $this->form()->was_submitted($submitted_form_data)) { |
|
| 647 | + if ( ! $this->form()->was_submitted($submitted_form_data)) { |
|
| 648 | 648 | throw new InvalidFormSubmissionException($this->form_name); |
| 649 | 649 | } |
| 650 | 650 | $this->form(true)->receive_form_submission($submitted_form_data); |
| 651 | - if (! $this->form()->is_valid()) { |
|
| 651 | + if ( ! $this->form()->is_valid()) { |
|
| 652 | 652 | throw new InvalidFormSubmissionException( |
| 653 | 653 | $this->form_name, |
| 654 | 654 | sprintf( |
@@ -24,9 +24,9 @@ |
||
| 24 | 24 | ] |
| 25 | 25 | ) |
| 26 | 26 | ); |
| 27 | - if (! is_array($answer_options) || empty($answer_options)) { |
|
| 27 | + if ( ! is_array($answer_options) || empty($answer_options)) { |
|
| 28 | 28 | $answer_options = [ |
| 29 | - EE_Switch_Input::OPTION_ON => esc_html__( 'enabled', 'event_espresso'), |
|
| 29 | + EE_Switch_Input::OPTION_ON => esc_html__('enabled', 'event_espresso'), |
|
| 30 | 30 | EE_Switch_Input::OPTION_OFF => esc_html__('disabled', 'event_espresso'), |
| 31 | 31 | ]; |
| 32 | 32 | } |
@@ -13,16 +13,16 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class EE_Select_Input extends EE_Form_Input_With_Options_Base |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * @param array $answer_options |
|
| 18 | - * @param array $input_settings |
|
| 19 | - */ |
|
| 20 | - public function __construct($answer_options, $input_settings = array()) |
|
| 21 | - { |
|
| 22 | - $this->_set_display_strategy(new EE_Select_Display_Strategy($answer_options)); |
|
| 23 | - $this->_add_validation_strategy( |
|
| 24 | - new EE_Enum_Validation_Strategy($input_settings['validation_error_message'] ?? null) |
|
| 25 | - ); |
|
| 26 | - parent::__construct($answer_options, $input_settings); |
|
| 27 | - } |
|
| 16 | + /** |
|
| 17 | + * @param array $answer_options |
|
| 18 | + * @param array $input_settings |
|
| 19 | + */ |
|
| 20 | + public function __construct($answer_options, $input_settings = array()) |
|
| 21 | + { |
|
| 22 | + $this->_set_display_strategy(new EE_Select_Display_Strategy($answer_options)); |
|
| 23 | + $this->_add_validation_strategy( |
|
| 24 | + new EE_Enum_Validation_Strategy($input_settings['validation_error_message'] ?? null) |
|
| 25 | + ); |
|
| 26 | + parent::__construct($answer_options, $input_settings); |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -11,17 +11,17 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class EE_Radio_Button_Input extends EE_Form_Input_With_Options_Base |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @param array $answer_options |
|
| 16 | - * @param array $input_settings |
|
| 17 | - */ |
|
| 18 | - public function __construct($answer_options, $input_settings = array()) |
|
| 19 | - { |
|
| 20 | - $this->_set_display_strategy(new EE_Radio_Button_Display_Strategy()); |
|
| 21 | - $this->_add_validation_strategy( |
|
| 22 | - new EE_Enum_Validation_Strategy($input_settings['validation_error_message'] ?? null) |
|
| 23 | - ); |
|
| 24 | - $this->_multiple_selections = false; |
|
| 25 | - parent::__construct($answer_options, $input_settings); |
|
| 26 | - } |
|
| 14 | + /** |
|
| 15 | + * @param array $answer_options |
|
| 16 | + * @param array $input_settings |
|
| 17 | + */ |
|
| 18 | + public function __construct($answer_options, $input_settings = array()) |
|
| 19 | + { |
|
| 20 | + $this->_set_display_strategy(new EE_Radio_Button_Display_Strategy()); |
|
| 21 | + $this->_add_validation_strategy( |
|
| 22 | + new EE_Enum_Validation_Strategy($input_settings['validation_error_message'] ?? null) |
|
| 23 | + ); |
|
| 24 | + $this->_multiple_selections = false; |
|
| 25 | + parent::__construct($answer_options, $input_settings); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -15,336 +15,336 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class EE_Form_Section_Proper extends EE_Form_Section_Validatable |
| 17 | 17 | { |
| 18 | - const SUBMITTED_FORM_DATA_SSN_KEY = 'submitted_form_data'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Subsections |
|
| 22 | - * |
|
| 23 | - * @var EE_Form_Section_Validatable[] |
|
| 24 | - */ |
|
| 25 | - protected $_subsections = array(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Strategy for laying out the form |
|
| 29 | - * |
|
| 30 | - * @var EE_Form_Section_Layout_Base |
|
| 31 | - */ |
|
| 32 | - protected $_layout_strategy; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Whether or not this form has received and validated a form submission yet |
|
| 36 | - * |
|
| 37 | - * @var boolean |
|
| 38 | - */ |
|
| 39 | - protected $_received_submission = false; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * message displayed to users upon successful form submission |
|
| 43 | - * |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - protected $_form_submission_success_message = ''; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * message displayed to users upon unsuccessful form submission |
|
| 50 | - * |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - protected $_form_submission_error_message = ''; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var array like post / request |
|
| 57 | - */ |
|
| 58 | - protected $cached_request_data; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Stores whether this form (and its sub-sections) were found to be valid or not. |
|
| 62 | - * Starts off as null, but once the form is validated, it set to either true or false |
|
| 63 | - * @var boolean|null |
|
| 64 | - */ |
|
| 65 | - protected $is_valid; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Stores all the data that will localized for form validation |
|
| 69 | - * |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected static $_js_localization = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * whether or not the form's localized validation JS vars have been set |
|
| 76 | - * |
|
| 77 | - * @type boolean |
|
| 78 | - */ |
|
| 79 | - protected static $_scripts_localized = false; |
|
| 80 | - |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * when constructing a proper form section, calls _construct_finalize on children |
|
| 84 | - * so that they know who their parent is, and what name they've been given. |
|
| 85 | - * |
|
| 86 | - * @param array[] $options_array { |
|
| 87 | - * @type $subsections EE_Form_Section_Validatable[] where keys are the section's name |
|
| 88 | - * @type $include string[] numerically-indexed where values are section names to be included, |
|
| 89 | - * and in that order. This is handy if you want |
|
| 90 | - * the subsections to be ordered differently than the default, and if you override |
|
| 91 | - * which fields are shown |
|
| 92 | - * @type $exclude string[] values are subsections to be excluded. This is handy if you want |
|
| 93 | - * to remove certain default subsections (note: if you specify BOTH 'include' AND |
|
| 94 | - * 'exclude', the inclusions will be applied first, and the exclusions will exclude |
|
| 95 | - * items from that list of inclusions) |
|
| 96 | - * @type $layout_strategy EE_Form_Section_Layout_Base strategy for laying out the form |
|
| 97 | - * } @see EE_Form_Section_Validatable::__construct() |
|
| 98 | - * @throws EE_Error |
|
| 99 | - */ |
|
| 100 | - public function __construct($options_array = array()) |
|
| 101 | - { |
|
| 102 | - $options_array = (array) apply_filters( |
|
| 103 | - 'FHEE__EE_Form_Section_Proper___construct__options_array', |
|
| 104 | - $options_array, |
|
| 105 | - $this |
|
| 106 | - ); |
|
| 107 | - // call parent first, as it may be setting the name |
|
| 108 | - parent::__construct($options_array); |
|
| 109 | - // if they've included subsections in the constructor, add them now |
|
| 110 | - if (isset($options_array['include'])) { |
|
| 111 | - // we are going to make sure we ONLY have those subsections to include |
|
| 112 | - // AND we are going to make sure they're in that specified order |
|
| 113 | - $reordered_subsections = array(); |
|
| 114 | - foreach ($options_array['include'] as $input_name) { |
|
| 115 | - if (isset($this->_subsections[ $input_name ])) { |
|
| 116 | - $reordered_subsections[ $input_name ] = $this->_subsections[ $input_name ]; |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - $this->_subsections = $reordered_subsections; |
|
| 120 | - } |
|
| 121 | - if (isset($options_array['exclude'])) { |
|
| 122 | - $exclude = $options_array['exclude']; |
|
| 123 | - $this->_subsections = array_diff_key($this->_subsections, array_flip($exclude)); |
|
| 124 | - } |
|
| 125 | - if (isset($options_array['layout_strategy'])) { |
|
| 126 | - $this->_layout_strategy = $options_array['layout_strategy']; |
|
| 127 | - } |
|
| 128 | - if (! $this->_layout_strategy) { |
|
| 129 | - $this->_layout_strategy = is_admin() ? new EE_Admin_Two_Column_Layout() : new EE_Two_Column_Layout(); |
|
| 130 | - } |
|
| 131 | - $this->_layout_strategy->_construct_finalize($this); |
|
| 132 | - // ok so we are definitely going to want the forms JS, |
|
| 133 | - // so enqueue it or remember to enqueue it during wp_enqueue_scripts |
|
| 134 | - if (did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts')) { |
|
| 135 | - // ok so they've constructed this object after when they should have. |
|
| 136 | - // just enqueue the generic form scripts and initialize the form immediately in the JS |
|
| 137 | - EE_Form_Section_Proper::wp_enqueue_scripts(true); |
|
| 138 | - } else { |
|
| 139 | - add_action('wp_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
| 140 | - add_action('admin_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
| 141 | - } |
|
| 142 | - add_action('wp_footer', array($this, 'ensure_scripts_localized'), 1); |
|
| 143 | - /** |
|
| 144 | - * Gives other plugins a chance to hook in before construct finalize is called. |
|
| 145 | - * The form probably doesn't yet have a parent form section. |
|
| 146 | - * Since 4.9.32, when this action was introduced, this is the best place to add a subsection onto a form, |
|
| 147 | - * assuming you don't care what the form section's name, HTML ID, or HTML name etc are. |
|
| 148 | - * Also see AHEE__EE_Form_Section_Proper___construct_finalize__end |
|
| 149 | - * |
|
| 150 | - * @since 4.9.32 |
|
| 151 | - * @param EE_Form_Section_Proper $this before __construct is done, but all of its logic, |
|
| 152 | - * except maybe calling _construct_finalize has been done |
|
| 153 | - * @param array $options_array options passed into the constructor |
|
| 154 | - */ |
|
| 155 | - do_action( |
|
| 156 | - 'AHEE__EE_Form_Input_Base___construct__before_construct_finalize_called', |
|
| 157 | - $this, |
|
| 158 | - $options_array |
|
| 159 | - ); |
|
| 160 | - if (isset($options_array['name'])) { |
|
| 161 | - $this->_construct_finalize(null, $options_array['name']); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Finishes construction given the parent form section and this form section's name |
|
| 168 | - * |
|
| 169 | - * @param EE_Form_Section_Proper $parent_form_section |
|
| 170 | - * @param string $name |
|
| 171 | - * @throws EE_Error |
|
| 172 | - */ |
|
| 173 | - public function _construct_finalize($parent_form_section, $name) |
|
| 174 | - { |
|
| 175 | - parent::_construct_finalize($parent_form_section, $name); |
|
| 176 | - $this->_set_default_name_if_empty(); |
|
| 177 | - $this->_set_default_html_id_if_empty(); |
|
| 178 | - foreach ($this->_subsections as $subsection_name => $subsection) { |
|
| 179 | - if ($subsection instanceof EE_Form_Section_Base) { |
|
| 180 | - $subsection->_construct_finalize($this, $subsection_name); |
|
| 181 | - } else { |
|
| 182 | - throw new EE_Error( |
|
| 183 | - sprintf( |
|
| 184 | - esc_html__( |
|
| 185 | - 'Subsection "%s" is not an instanceof EE_Form_Section_Base on form "%s". It is a "%s"', |
|
| 186 | - 'event_espresso' |
|
| 187 | - ), |
|
| 188 | - $subsection_name, |
|
| 189 | - get_class($this), |
|
| 190 | - $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso') |
|
| 191 | - ) |
|
| 192 | - ); |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - /** |
|
| 196 | - * Action performed just after form has been given a name (and HTML ID etc) and is fully constructed. |
|
| 197 | - * If you have code that should modify the form and needs it and its subsections to have a name, HTML ID |
|
| 198 | - * (or other attributes derived from the name like the HTML label id, etc), this is where it should be done. |
|
| 199 | - * This might only happen just before displaying the form, or just before it receives form submission data. |
|
| 200 | - * If you need to modify the form or its subsections before _construct_finalize is called on it (and we've |
|
| 201 | - * ensured it has a name, HTML IDs, etc |
|
| 202 | - * |
|
| 203 | - * @param EE_Form_Section_Proper $this |
|
| 204 | - * @param EE_Form_Section_Proper|null $parent_form_section |
|
| 205 | - * @param string $name |
|
| 206 | - */ |
|
| 207 | - do_action( |
|
| 208 | - 'AHEE__EE_Form_Section_Proper___construct_finalize__end', |
|
| 209 | - $this, |
|
| 210 | - $parent_form_section, |
|
| 211 | - $name |
|
| 212 | - ); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Gets the layout strategy for this form section |
|
| 218 | - * |
|
| 219 | - * @return EE_Form_Section_Layout_Base |
|
| 220 | - */ |
|
| 221 | - public function get_layout_strategy() |
|
| 222 | - { |
|
| 223 | - return $this->_layout_strategy; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Gets the HTML for a single input for this form section according |
|
| 229 | - * to the layout strategy |
|
| 230 | - * |
|
| 231 | - * @param EE_Form_Input_Base $input |
|
| 232 | - * @return string |
|
| 233 | - */ |
|
| 234 | - public function get_html_for_input($input) |
|
| 235 | - { |
|
| 236 | - return $this->_layout_strategy->layout_input($input); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * was_submitted - checks if form inputs are present in request data |
|
| 242 | - * Basically an alias for form_data_present_in() (which is used by both |
|
| 243 | - * proper form sections and form inputs) |
|
| 244 | - * |
|
| 245 | - * @param null $form_data |
|
| 246 | - * @return boolean |
|
| 247 | - * @throws EE_Error |
|
| 248 | - */ |
|
| 249 | - public function was_submitted($form_data = null) |
|
| 250 | - { |
|
| 251 | - return $this->form_data_present_in($form_data); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Gets the cached request data; but if there is none, or $req_data was set with |
|
| 256 | - * something different, refresh the cache, and then return it |
|
| 257 | - * @param null $req_data |
|
| 258 | - * @return array |
|
| 259 | - */ |
|
| 260 | - protected function getCachedRequest($req_data = null) |
|
| 261 | - { |
|
| 262 | - if ( |
|
| 263 | - $this->cached_request_data === null |
|
| 264 | - || ( |
|
| 265 | - $req_data !== null |
|
| 266 | - && $req_data !== $this->cached_request_data |
|
| 267 | - ) |
|
| 268 | - ) { |
|
| 269 | - $req_data = apply_filters( |
|
| 270 | - 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
| 271 | - $req_data, |
|
| 272 | - $this |
|
| 273 | - ); |
|
| 274 | - if ($req_data === null) { |
|
| 275 | - /** @var RequestInterface $request */ |
|
| 276 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
| 277 | - $req_data = $request->requestParams(); |
|
| 278 | - } |
|
| 279 | - $req_data = apply_filters( |
|
| 280 | - 'FHEE__EE_Form_Section_Proper__receive_form_submission__request_data', |
|
| 281 | - $req_data, |
|
| 282 | - $this |
|
| 283 | - ); |
|
| 284 | - $this->cached_request_data = (array) $req_data; |
|
| 285 | - } |
|
| 286 | - return $this->cached_request_data; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * After the form section is initially created, call this to sanitize the data in the submission |
|
| 292 | - * which relates to this form section, validate it, and set it as properties on the form. |
|
| 293 | - * |
|
| 294 | - * @param array|null $req_data should usually be post data (the default). |
|
| 295 | - * However, you CAN supply a different array. |
|
| 296 | - * Consider using set_defaults() instead however. |
|
| 297 | - * (If you rendered the form in the page using echo $form_x->get_html() |
|
| 298 | - * the inputs will have the correct name in the request data for this function |
|
| 299 | - * to find them and populate the form with them. |
|
| 300 | - * If you have a flat form (with only input subsections), |
|
| 301 | - * you can supply a flat array where keys |
|
| 302 | - * are the form input names and values are their values) |
|
| 303 | - * @param boolean $validate whether or not to perform validation on this data. Default is, |
|
| 304 | - * of course, to validate that data, and set errors on the invalid values. |
|
| 305 | - * But if the data has already been validated |
|
| 306 | - * (eg you validated the data then stored it in the DB) |
|
| 307 | - * you may want to skip this step. |
|
| 308 | - * @throws InvalidArgumentException |
|
| 309 | - * @throws InvalidInterfaceException |
|
| 310 | - * @throws InvalidDataTypeException |
|
| 311 | - * @throws EE_Error |
|
| 312 | - */ |
|
| 313 | - public function receive_form_submission($req_data = null, $validate = true) |
|
| 314 | - { |
|
| 315 | - $req_data = $this->getCachedRequest($req_data); |
|
| 316 | - $this->_normalize($req_data); |
|
| 317 | - if ($validate) { |
|
| 318 | - $this->_validate(); |
|
| 319 | - // if it's invalid, we're going to want to re-display so remember what they submitted |
|
| 320 | - if (! $this->is_valid()) { |
|
| 321 | - $this->store_submitted_form_data_in_session(); |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - if ($this->submission_error_message() === '' && ! $this->is_valid()) { |
|
| 325 | - $this->set_submission_error_message(); |
|
| 326 | - } |
|
| 327 | - do_action( |
|
| 328 | - 'AHEE__EE_Form_Section_Proper__receive_form_submission__end', |
|
| 329 | - $req_data, |
|
| 330 | - $this, |
|
| 331 | - $validate |
|
| 332 | - ); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * caches the originally submitted input values in the session |
|
| 338 | - * so that they can be used to repopulate the form if it failed validation |
|
| 339 | - * |
|
| 340 | - * @return boolean whether or not the data was successfully stored in the session |
|
| 341 | - * @throws InvalidArgumentException |
|
| 342 | - * @throws InvalidInterfaceException |
|
| 343 | - * @throws InvalidDataTypeException |
|
| 344 | - * @throws EE_Error |
|
| 345 | - */ |
|
| 346 | - protected function store_submitted_form_data_in_session() |
|
| 347 | - { |
|
| 18 | + const SUBMITTED_FORM_DATA_SSN_KEY = 'submitted_form_data'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Subsections |
|
| 22 | + * |
|
| 23 | + * @var EE_Form_Section_Validatable[] |
|
| 24 | + */ |
|
| 25 | + protected $_subsections = array(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Strategy for laying out the form |
|
| 29 | + * |
|
| 30 | + * @var EE_Form_Section_Layout_Base |
|
| 31 | + */ |
|
| 32 | + protected $_layout_strategy; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Whether or not this form has received and validated a form submission yet |
|
| 36 | + * |
|
| 37 | + * @var boolean |
|
| 38 | + */ |
|
| 39 | + protected $_received_submission = false; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * message displayed to users upon successful form submission |
|
| 43 | + * |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + protected $_form_submission_success_message = ''; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * message displayed to users upon unsuccessful form submission |
|
| 50 | + * |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + protected $_form_submission_error_message = ''; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var array like post / request |
|
| 57 | + */ |
|
| 58 | + protected $cached_request_data; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Stores whether this form (and its sub-sections) were found to be valid or not. |
|
| 62 | + * Starts off as null, but once the form is validated, it set to either true or false |
|
| 63 | + * @var boolean|null |
|
| 64 | + */ |
|
| 65 | + protected $is_valid; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Stores all the data that will localized for form validation |
|
| 69 | + * |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected static $_js_localization = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * whether or not the form's localized validation JS vars have been set |
|
| 76 | + * |
|
| 77 | + * @type boolean |
|
| 78 | + */ |
|
| 79 | + protected static $_scripts_localized = false; |
|
| 80 | + |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * when constructing a proper form section, calls _construct_finalize on children |
|
| 84 | + * so that they know who their parent is, and what name they've been given. |
|
| 85 | + * |
|
| 86 | + * @param array[] $options_array { |
|
| 87 | + * @type $subsections EE_Form_Section_Validatable[] where keys are the section's name |
|
| 88 | + * @type $include string[] numerically-indexed where values are section names to be included, |
|
| 89 | + * and in that order. This is handy if you want |
|
| 90 | + * the subsections to be ordered differently than the default, and if you override |
|
| 91 | + * which fields are shown |
|
| 92 | + * @type $exclude string[] values are subsections to be excluded. This is handy if you want |
|
| 93 | + * to remove certain default subsections (note: if you specify BOTH 'include' AND |
|
| 94 | + * 'exclude', the inclusions will be applied first, and the exclusions will exclude |
|
| 95 | + * items from that list of inclusions) |
|
| 96 | + * @type $layout_strategy EE_Form_Section_Layout_Base strategy for laying out the form |
|
| 97 | + * } @see EE_Form_Section_Validatable::__construct() |
|
| 98 | + * @throws EE_Error |
|
| 99 | + */ |
|
| 100 | + public function __construct($options_array = array()) |
|
| 101 | + { |
|
| 102 | + $options_array = (array) apply_filters( |
|
| 103 | + 'FHEE__EE_Form_Section_Proper___construct__options_array', |
|
| 104 | + $options_array, |
|
| 105 | + $this |
|
| 106 | + ); |
|
| 107 | + // call parent first, as it may be setting the name |
|
| 108 | + parent::__construct($options_array); |
|
| 109 | + // if they've included subsections in the constructor, add them now |
|
| 110 | + if (isset($options_array['include'])) { |
|
| 111 | + // we are going to make sure we ONLY have those subsections to include |
|
| 112 | + // AND we are going to make sure they're in that specified order |
|
| 113 | + $reordered_subsections = array(); |
|
| 114 | + foreach ($options_array['include'] as $input_name) { |
|
| 115 | + if (isset($this->_subsections[ $input_name ])) { |
|
| 116 | + $reordered_subsections[ $input_name ] = $this->_subsections[ $input_name ]; |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + $this->_subsections = $reordered_subsections; |
|
| 120 | + } |
|
| 121 | + if (isset($options_array['exclude'])) { |
|
| 122 | + $exclude = $options_array['exclude']; |
|
| 123 | + $this->_subsections = array_diff_key($this->_subsections, array_flip($exclude)); |
|
| 124 | + } |
|
| 125 | + if (isset($options_array['layout_strategy'])) { |
|
| 126 | + $this->_layout_strategy = $options_array['layout_strategy']; |
|
| 127 | + } |
|
| 128 | + if (! $this->_layout_strategy) { |
|
| 129 | + $this->_layout_strategy = is_admin() ? new EE_Admin_Two_Column_Layout() : new EE_Two_Column_Layout(); |
|
| 130 | + } |
|
| 131 | + $this->_layout_strategy->_construct_finalize($this); |
|
| 132 | + // ok so we are definitely going to want the forms JS, |
|
| 133 | + // so enqueue it or remember to enqueue it during wp_enqueue_scripts |
|
| 134 | + if (did_action('wp_enqueue_scripts') || did_action('admin_enqueue_scripts')) { |
|
| 135 | + // ok so they've constructed this object after when they should have. |
|
| 136 | + // just enqueue the generic form scripts and initialize the form immediately in the JS |
|
| 137 | + EE_Form_Section_Proper::wp_enqueue_scripts(true); |
|
| 138 | + } else { |
|
| 139 | + add_action('wp_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
| 140 | + add_action('admin_enqueue_scripts', array('EE_Form_Section_Proper', 'wp_enqueue_scripts')); |
|
| 141 | + } |
|
| 142 | + add_action('wp_footer', array($this, 'ensure_scripts_localized'), 1); |
|
| 143 | + /** |
|
| 144 | + * Gives other plugins a chance to hook in before construct finalize is called. |
|
| 145 | + * The form probably doesn't yet have a parent form section. |
|
| 146 | + * Since 4.9.32, when this action was introduced, this is the best place to add a subsection onto a form, |
|
| 147 | + * assuming you don't care what the form section's name, HTML ID, or HTML name etc are. |
|
| 148 | + * Also see AHEE__EE_Form_Section_Proper___construct_finalize__end |
|
| 149 | + * |
|
| 150 | + * @since 4.9.32 |
|
| 151 | + * @param EE_Form_Section_Proper $this before __construct is done, but all of its logic, |
|
| 152 | + * except maybe calling _construct_finalize has been done |
|
| 153 | + * @param array $options_array options passed into the constructor |
|
| 154 | + */ |
|
| 155 | + do_action( |
|
| 156 | + 'AHEE__EE_Form_Input_Base___construct__before_construct_finalize_called', |
|
| 157 | + $this, |
|
| 158 | + $options_array |
|
| 159 | + ); |
|
| 160 | + if (isset($options_array['name'])) { |
|
| 161 | + $this->_construct_finalize(null, $options_array['name']); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Finishes construction given the parent form section and this form section's name |
|
| 168 | + * |
|
| 169 | + * @param EE_Form_Section_Proper $parent_form_section |
|
| 170 | + * @param string $name |
|
| 171 | + * @throws EE_Error |
|
| 172 | + */ |
|
| 173 | + public function _construct_finalize($parent_form_section, $name) |
|
| 174 | + { |
|
| 175 | + parent::_construct_finalize($parent_form_section, $name); |
|
| 176 | + $this->_set_default_name_if_empty(); |
|
| 177 | + $this->_set_default_html_id_if_empty(); |
|
| 178 | + foreach ($this->_subsections as $subsection_name => $subsection) { |
|
| 179 | + if ($subsection instanceof EE_Form_Section_Base) { |
|
| 180 | + $subsection->_construct_finalize($this, $subsection_name); |
|
| 181 | + } else { |
|
| 182 | + throw new EE_Error( |
|
| 183 | + sprintf( |
|
| 184 | + esc_html__( |
|
| 185 | + 'Subsection "%s" is not an instanceof EE_Form_Section_Base on form "%s". It is a "%s"', |
|
| 186 | + 'event_espresso' |
|
| 187 | + ), |
|
| 188 | + $subsection_name, |
|
| 189 | + get_class($this), |
|
| 190 | + $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso') |
|
| 191 | + ) |
|
| 192 | + ); |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + /** |
|
| 196 | + * Action performed just after form has been given a name (and HTML ID etc) and is fully constructed. |
|
| 197 | + * If you have code that should modify the form and needs it and its subsections to have a name, HTML ID |
|
| 198 | + * (or other attributes derived from the name like the HTML label id, etc), this is where it should be done. |
|
| 199 | + * This might only happen just before displaying the form, or just before it receives form submission data. |
|
| 200 | + * If you need to modify the form or its subsections before _construct_finalize is called on it (and we've |
|
| 201 | + * ensured it has a name, HTML IDs, etc |
|
| 202 | + * |
|
| 203 | + * @param EE_Form_Section_Proper $this |
|
| 204 | + * @param EE_Form_Section_Proper|null $parent_form_section |
|
| 205 | + * @param string $name |
|
| 206 | + */ |
|
| 207 | + do_action( |
|
| 208 | + 'AHEE__EE_Form_Section_Proper___construct_finalize__end', |
|
| 209 | + $this, |
|
| 210 | + $parent_form_section, |
|
| 211 | + $name |
|
| 212 | + ); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Gets the layout strategy for this form section |
|
| 218 | + * |
|
| 219 | + * @return EE_Form_Section_Layout_Base |
|
| 220 | + */ |
|
| 221 | + public function get_layout_strategy() |
|
| 222 | + { |
|
| 223 | + return $this->_layout_strategy; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Gets the HTML for a single input for this form section according |
|
| 229 | + * to the layout strategy |
|
| 230 | + * |
|
| 231 | + * @param EE_Form_Input_Base $input |
|
| 232 | + * @return string |
|
| 233 | + */ |
|
| 234 | + public function get_html_for_input($input) |
|
| 235 | + { |
|
| 236 | + return $this->_layout_strategy->layout_input($input); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * was_submitted - checks if form inputs are present in request data |
|
| 242 | + * Basically an alias for form_data_present_in() (which is used by both |
|
| 243 | + * proper form sections and form inputs) |
|
| 244 | + * |
|
| 245 | + * @param null $form_data |
|
| 246 | + * @return boolean |
|
| 247 | + * @throws EE_Error |
|
| 248 | + */ |
|
| 249 | + public function was_submitted($form_data = null) |
|
| 250 | + { |
|
| 251 | + return $this->form_data_present_in($form_data); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Gets the cached request data; but if there is none, or $req_data was set with |
|
| 256 | + * something different, refresh the cache, and then return it |
|
| 257 | + * @param null $req_data |
|
| 258 | + * @return array |
|
| 259 | + */ |
|
| 260 | + protected function getCachedRequest($req_data = null) |
|
| 261 | + { |
|
| 262 | + if ( |
|
| 263 | + $this->cached_request_data === null |
|
| 264 | + || ( |
|
| 265 | + $req_data !== null |
|
| 266 | + && $req_data !== $this->cached_request_data |
|
| 267 | + ) |
|
| 268 | + ) { |
|
| 269 | + $req_data = apply_filters( |
|
| 270 | + 'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data', |
|
| 271 | + $req_data, |
|
| 272 | + $this |
|
| 273 | + ); |
|
| 274 | + if ($req_data === null) { |
|
| 275 | + /** @var RequestInterface $request */ |
|
| 276 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
| 277 | + $req_data = $request->requestParams(); |
|
| 278 | + } |
|
| 279 | + $req_data = apply_filters( |
|
| 280 | + 'FHEE__EE_Form_Section_Proper__receive_form_submission__request_data', |
|
| 281 | + $req_data, |
|
| 282 | + $this |
|
| 283 | + ); |
|
| 284 | + $this->cached_request_data = (array) $req_data; |
|
| 285 | + } |
|
| 286 | + return $this->cached_request_data; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * After the form section is initially created, call this to sanitize the data in the submission |
|
| 292 | + * which relates to this form section, validate it, and set it as properties on the form. |
|
| 293 | + * |
|
| 294 | + * @param array|null $req_data should usually be post data (the default). |
|
| 295 | + * However, you CAN supply a different array. |
|
| 296 | + * Consider using set_defaults() instead however. |
|
| 297 | + * (If you rendered the form in the page using echo $form_x->get_html() |
|
| 298 | + * the inputs will have the correct name in the request data for this function |
|
| 299 | + * to find them and populate the form with them. |
|
| 300 | + * If you have a flat form (with only input subsections), |
|
| 301 | + * you can supply a flat array where keys |
|
| 302 | + * are the form input names and values are their values) |
|
| 303 | + * @param boolean $validate whether or not to perform validation on this data. Default is, |
|
| 304 | + * of course, to validate that data, and set errors on the invalid values. |
|
| 305 | + * But if the data has already been validated |
|
| 306 | + * (eg you validated the data then stored it in the DB) |
|
| 307 | + * you may want to skip this step. |
|
| 308 | + * @throws InvalidArgumentException |
|
| 309 | + * @throws InvalidInterfaceException |
|
| 310 | + * @throws InvalidDataTypeException |
|
| 311 | + * @throws EE_Error |
|
| 312 | + */ |
|
| 313 | + public function receive_form_submission($req_data = null, $validate = true) |
|
| 314 | + { |
|
| 315 | + $req_data = $this->getCachedRequest($req_data); |
|
| 316 | + $this->_normalize($req_data); |
|
| 317 | + if ($validate) { |
|
| 318 | + $this->_validate(); |
|
| 319 | + // if it's invalid, we're going to want to re-display so remember what they submitted |
|
| 320 | + if (! $this->is_valid()) { |
|
| 321 | + $this->store_submitted_form_data_in_session(); |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + if ($this->submission_error_message() === '' && ! $this->is_valid()) { |
|
| 325 | + $this->set_submission_error_message(); |
|
| 326 | + } |
|
| 327 | + do_action( |
|
| 328 | + 'AHEE__EE_Form_Section_Proper__receive_form_submission__end', |
|
| 329 | + $req_data, |
|
| 330 | + $this, |
|
| 331 | + $validate |
|
| 332 | + ); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * caches the originally submitted input values in the session |
|
| 338 | + * so that they can be used to repopulate the form if it failed validation |
|
| 339 | + * |
|
| 340 | + * @return boolean whether or not the data was successfully stored in the session |
|
| 341 | + * @throws InvalidArgumentException |
|
| 342 | + * @throws InvalidInterfaceException |
|
| 343 | + * @throws InvalidDataTypeException |
|
| 344 | + * @throws EE_Error |
|
| 345 | + */ |
|
| 346 | + protected function store_submitted_form_data_in_session() |
|
| 347 | + { |
|
| 348 | 348 | $session = EE_Registry::instance()->SSN; |
| 349 | 349 | if ($session instanceof EE_Session) { |
| 350 | 350 | return EE_Registry::instance()->SSN->set_session_data( |
@@ -357,1194 +357,1194 @@ discard block |
||
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | |
| 360 | - /** |
|
| 361 | - * retrieves the originally submitted input values in the session |
|
| 362 | - * so that they can be used to repopulate the form if it failed validation |
|
| 363 | - * |
|
| 364 | - * @return array |
|
| 365 | - * @throws InvalidArgumentException |
|
| 366 | - * @throws InvalidInterfaceException |
|
| 367 | - * @throws InvalidDataTypeException |
|
| 368 | - */ |
|
| 369 | - protected function get_submitted_form_data_from_session() |
|
| 370 | - { |
|
| 371 | - $session = EE_Registry::instance()->SSN; |
|
| 372 | - if ($session instanceof EE_Session) { |
|
| 373 | - return $session->get_session_data( |
|
| 374 | - EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY |
|
| 375 | - ); |
|
| 376 | - } |
|
| 377 | - return array(); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * flushed the originally submitted input values from the session |
|
| 383 | - * |
|
| 384 | - * @return boolean whether or not the data was successfully removed from the session |
|
| 385 | - * @throws InvalidArgumentException |
|
| 386 | - * @throws InvalidInterfaceException |
|
| 387 | - * @throws InvalidDataTypeException |
|
| 388 | - */ |
|
| 389 | - public static function flush_submitted_form_data_from_session() |
|
| 390 | - { |
|
| 391 | - return EE_Registry::instance()->SSN->reset_data( |
|
| 392 | - array(EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY) |
|
| 393 | - ); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Populates this form and its subsections with data from the session. |
|
| 399 | - * (Wrapper for EE_Form_Section_Proper::receive_form_submission, so it shows |
|
| 400 | - * validation errors when displaying too) |
|
| 401 | - * Returns true if the form was populated from the session, false otherwise |
|
| 402 | - * |
|
| 403 | - * @return boolean |
|
| 404 | - * @throws InvalidArgumentException |
|
| 405 | - * @throws InvalidInterfaceException |
|
| 406 | - * @throws InvalidDataTypeException |
|
| 407 | - * @throws EE_Error |
|
| 408 | - */ |
|
| 409 | - public function populate_from_session() |
|
| 410 | - { |
|
| 411 | - $form_data_in_session = $this->get_submitted_form_data_from_session(); |
|
| 412 | - if (empty($form_data_in_session)) { |
|
| 413 | - return false; |
|
| 414 | - } |
|
| 415 | - $this->receive_form_submission($form_data_in_session); |
|
| 416 | - add_action('shutdown', array('EE_Form_Section_Proper', 'flush_submitted_form_data_from_session')); |
|
| 417 | - if ($this->form_data_present_in($form_data_in_session)) { |
|
| 418 | - return true; |
|
| 419 | - } |
|
| 420 | - return false; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Populates the default data for the form, given an array where keys are |
|
| 426 | - * the input names, and values are their values (preferably normalized to be their |
|
| 427 | - * proper PHP types, not all strings... although that should be ok too). |
|
| 428 | - * Proper subsections are sub-arrays, the key being the subsection's name, and |
|
| 429 | - * the value being an array formatted in teh same way |
|
| 430 | - * |
|
| 431 | - * @param array $default_data |
|
| 432 | - * @throws EE_Error |
|
| 433 | - */ |
|
| 434 | - public function populate_defaults($default_data) |
|
| 435 | - { |
|
| 436 | - foreach ($this->subsections(false) as $subsection_name => $subsection) { |
|
| 437 | - if (isset($default_data[ $subsection_name ])) { |
|
| 438 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
| 439 | - $subsection->set_default($default_data[ $subsection_name ]); |
|
| 440 | - } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
| 441 | - $subsection->populate_defaults($default_data[ $subsection_name ]); |
|
| 442 | - } |
|
| 443 | - } |
|
| 444 | - } |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * returns true if subsection exists |
|
| 450 | - * |
|
| 451 | - * @param string $name |
|
| 452 | - * @return boolean |
|
| 453 | - */ |
|
| 454 | - public function subsection_exists($name) |
|
| 455 | - { |
|
| 456 | - return isset($this->_subsections[ $name ]) ? true : false; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * Gets the subsection specified by its name |
|
| 462 | - * |
|
| 463 | - * @param string $name |
|
| 464 | - * @param boolean $require_construction_to_be_finalized most client code should leave this as TRUE |
|
| 465 | - * so that the inputs will be properly configured. |
|
| 466 | - * However, some client code may be ok |
|
| 467 | - * with construction finalize being called later |
|
| 468 | - * (realizing that the subsections' html names |
|
| 469 | - * might not be set yet, etc.) |
|
| 470 | - * @return EE_Form_Section_Base |
|
| 471 | - * @throws EE_Error |
|
| 472 | - */ |
|
| 473 | - public function get_subsection($name, $require_construction_to_be_finalized = true) |
|
| 474 | - { |
|
| 475 | - if ($require_construction_to_be_finalized) { |
|
| 476 | - $this->ensure_construct_finalized_called(); |
|
| 477 | - } |
|
| 478 | - return $this->subsection_exists($name) ? $this->_subsections[ $name ] : null; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Gets all the validatable subsections of this form section |
|
| 484 | - * |
|
| 485 | - * @return EE_Form_Section_Validatable[] |
|
| 486 | - * @throws EE_Error |
|
| 487 | - */ |
|
| 488 | - public function get_validatable_subsections() |
|
| 489 | - { |
|
| 490 | - $validatable_subsections = array(); |
|
| 491 | - foreach ($this->subsections() as $name => $obj) { |
|
| 492 | - if ($obj instanceof EE_Form_Section_Validatable) { |
|
| 493 | - $validatable_subsections[ $name ] = $obj; |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - return $validatable_subsections; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * Gets an input by the given name. If not found, or if its not an EE_FOrm_Input_Base child, |
|
| 502 | - * throw an EE_Error. |
|
| 503 | - * |
|
| 504 | - * @param string $name |
|
| 505 | - * @param boolean $require_construction_to_be_finalized most client code should |
|
| 506 | - * leave this as TRUE so that the inputs will be properly |
|
| 507 | - * configured. However, some client code may be ok with |
|
| 508 | - * construction finalize being called later |
|
| 509 | - * (realizing that the subsections' html names might not be |
|
| 510 | - * set yet, etc.) |
|
| 511 | - * @return EE_Form_Input_Base |
|
| 512 | - * @throws EE_Error |
|
| 513 | - */ |
|
| 514 | - public function get_input($name, $require_construction_to_be_finalized = true) |
|
| 515 | - { |
|
| 516 | - $subsection = $this->get_subsection( |
|
| 517 | - $name, |
|
| 518 | - $require_construction_to_be_finalized |
|
| 519 | - ); |
|
| 520 | - if (! $subsection instanceof EE_Form_Input_Base) { |
|
| 521 | - throw new EE_Error( |
|
| 522 | - sprintf( |
|
| 523 | - esc_html__( |
|
| 524 | - "Subsection '%s' is not an instanceof EE_Form_Input_Base on form '%s'. It is a '%s'", |
|
| 525 | - 'event_espresso' |
|
| 526 | - ), |
|
| 527 | - $name, |
|
| 528 | - get_class($this), |
|
| 529 | - $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso') |
|
| 530 | - ) |
|
| 531 | - ); |
|
| 532 | - } |
|
| 533 | - return $subsection; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * Like get_input(), gets the proper subsection of the form given the name, |
|
| 539 | - * otherwise throws an EE_Error |
|
| 540 | - * |
|
| 541 | - * @param string $name |
|
| 542 | - * @param boolean $require_construction_to_be_finalized most client code should |
|
| 543 | - * leave this as TRUE so that the inputs will be properly |
|
| 544 | - * configured. However, some client code may be ok with |
|
| 545 | - * construction finalize being called later |
|
| 546 | - * (realizing that the subsections' html names might not be |
|
| 547 | - * set yet, etc.) |
|
| 548 | - * @return EE_Form_Section_Proper |
|
| 549 | - * @throws EE_Error |
|
| 550 | - */ |
|
| 551 | - public function get_proper_subsection($name, $require_construction_to_be_finalized = true) |
|
| 552 | - { |
|
| 553 | - $subsection = $this->get_subsection( |
|
| 554 | - $name, |
|
| 555 | - $require_construction_to_be_finalized |
|
| 556 | - ); |
|
| 557 | - if (! $subsection instanceof EE_Form_Section_Proper) { |
|
| 558 | - throw new EE_Error( |
|
| 559 | - sprintf( |
|
| 560 | - esc_html__( |
|
| 561 | - "Subsection '%'s is not an instanceof EE_Form_Section_Proper on form '%s'", |
|
| 562 | - 'event_espresso' |
|
| 563 | - ), |
|
| 564 | - $name, |
|
| 565 | - get_class($this) |
|
| 566 | - ) |
|
| 567 | - ); |
|
| 568 | - } |
|
| 569 | - return $subsection; |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - |
|
| 573 | - /** |
|
| 574 | - * Gets the value of the specified input. Should be called after receive_form_submission() |
|
| 575 | - * or populate_defaults() on the form, where the normalized value on the input is set. |
|
| 576 | - * |
|
| 577 | - * @param string $name |
|
| 578 | - * @return mixed depending on the input's type and its normalization strategy |
|
| 579 | - * @throws EE_Error |
|
| 580 | - */ |
|
| 581 | - public function get_input_value($name) |
|
| 582 | - { |
|
| 583 | - $input = $this->get_input($name); |
|
| 584 | - return $input->normalized_value(); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * Checks if this form section itself is valid, and then checks its subsections |
|
| 590 | - * |
|
| 591 | - * @throws EE_Error |
|
| 592 | - * @return boolean |
|
| 593 | - */ |
|
| 594 | - public function is_valid() |
|
| 595 | - { |
|
| 596 | - if ($this->is_valid === null) { |
|
| 597 | - if (! $this->has_received_submission()) { |
|
| 598 | - throw new EE_Error( |
|
| 599 | - sprintf( |
|
| 600 | - esc_html__( |
|
| 601 | - 'You cannot check if a form is valid before receiving the form submission using receive_form_submission', |
|
| 602 | - 'event_espresso' |
|
| 603 | - ) |
|
| 604 | - ) |
|
| 605 | - ); |
|
| 606 | - } |
|
| 607 | - if (! parent::is_valid()) { |
|
| 608 | - $this->is_valid = false; |
|
| 609 | - } else { |
|
| 610 | - // ok so no general errors to this entire form section. |
|
| 611 | - // so let's check the subsections, but only set errors if that hasn't been done yet |
|
| 612 | - $this->is_valid = true; |
|
| 613 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 614 | - if (! $subsection->is_valid()) { |
|
| 615 | - $this->is_valid = false; |
|
| 616 | - } |
|
| 617 | - } |
|
| 618 | - } |
|
| 619 | - } |
|
| 620 | - return $this->is_valid; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * gets the default name of this form section if none is specified |
|
| 626 | - * |
|
| 627 | - * @return void |
|
| 628 | - */ |
|
| 629 | - protected function _set_default_name_if_empty() |
|
| 630 | - { |
|
| 631 | - if (! $this->_name) { |
|
| 632 | - $classname = get_class($this); |
|
| 633 | - $default_name = str_replace('EE_', '', $classname); |
|
| 634 | - $this->_name = $default_name; |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Returns the HTML for the form, except for the form opening and closing tags |
|
| 641 | - * (as the form section doesn't know where you necessarily want to send the information to), |
|
| 642 | - * and except for a submit button. Enqueues JS and CSS; if called early enough we will |
|
| 643 | - * try to enqueue them in the header, otherwise they'll be enqueued in the footer. |
|
| 644 | - * Not doing_it_wrong because theoretically this CAN be used properly, |
|
| 645 | - * provided its used during "wp_enqueue_scripts", or it doesn't need to enqueue |
|
| 646 | - * any CSS. |
|
| 647 | - * |
|
| 648 | - * @throws InvalidArgumentException |
|
| 649 | - * @throws InvalidInterfaceException |
|
| 650 | - * @throws InvalidDataTypeException |
|
| 651 | - * @throws EE_Error |
|
| 652 | - */ |
|
| 653 | - public function get_html_and_js() |
|
| 654 | - { |
|
| 655 | - $this->enqueue_js(); |
|
| 656 | - return $this->get_html(); |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - |
|
| 660 | - /** |
|
| 661 | - * returns HTML for displaying this form section. recursively calls display_section() on all subsections |
|
| 662 | - * |
|
| 663 | - * @param bool $display_previously_submitted_data |
|
| 664 | - * @return string |
|
| 665 | - * @throws InvalidArgumentException |
|
| 666 | - * @throws InvalidInterfaceException |
|
| 667 | - * @throws InvalidDataTypeException |
|
| 668 | - * @throws EE_Error |
|
| 669 | - * @throws EE_Error |
|
| 670 | - * @throws EE_Error |
|
| 671 | - */ |
|
| 672 | - public function get_html($display_previously_submitted_data = true) |
|
| 673 | - { |
|
| 674 | - $this->ensure_construct_finalized_called(); |
|
| 675 | - if ($display_previously_submitted_data) { |
|
| 676 | - $this->populate_from_session(); |
|
| 677 | - } |
|
| 678 | - return $this->_form_html_filter |
|
| 679 | - ? $this->_form_html_filter->filterHtml($this->_layout_strategy->layout_form(), $this) |
|
| 680 | - : $this->_layout_strategy->layout_form(); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - |
|
| 684 | - /** |
|
| 685 | - * enqueues JS and CSS for the form. |
|
| 686 | - * It is preferred to call this before wp_enqueue_scripts so the |
|
| 687 | - * scripts and styles can be put in the header, but if called later |
|
| 688 | - * they will be put in the footer (which is OK for JS, but in HTML4 CSS should |
|
| 689 | - * only be in the header; but in HTML5 its ok in the body. |
|
| 690 | - * See http://stackoverflow.com/questions/4957446/load-external-css-file-in-body-tag. |
|
| 691 | - * So if your form enqueues CSS, it's preferred to call this before wp_enqueue_scripts.) |
|
| 692 | - * |
|
| 693 | - * @return void |
|
| 694 | - * @throws EE_Error |
|
| 695 | - */ |
|
| 696 | - public function enqueue_js() |
|
| 697 | - { |
|
| 698 | - $this->_enqueue_and_localize_form_js(); |
|
| 699 | - foreach ($this->subsections() as $subsection) { |
|
| 700 | - $subsection->enqueue_js(); |
|
| 701 | - } |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * adds a filter so that jquery validate gets enqueued in EE_System::wp_enqueue_scripts(). |
|
| 707 | - * This must be done BEFORE wp_enqueue_scripts() gets called, which is on |
|
| 708 | - * the wp_enqueue_scripts hook. |
|
| 709 | - * However, registering the form js and localizing it can happen when we |
|
| 710 | - * actually output the form (which is preferred, seeing how teh form's fields |
|
| 711 | - * could change until it's actually outputted) |
|
| 712 | - * |
|
| 713 | - * @param boolean $init_form_validation_automatically whether or not we want the form validation |
|
| 714 | - * to be triggered automatically or not |
|
| 715 | - * @return void |
|
| 716 | - */ |
|
| 717 | - public static function wp_enqueue_scripts($init_form_validation_automatically = true) |
|
| 718 | - { |
|
| 719 | - wp_register_script( |
|
| 720 | - 'ee_form_section_validation', |
|
| 721 | - EE_GLOBAL_ASSETS_URL . 'scripts' . '/form_section_validation.js', |
|
| 722 | - array('jquery-validate', 'jquery-ui-datepicker', 'jquery-validate-extra-methods'), |
|
| 723 | - EVENT_ESPRESSO_VERSION, |
|
| 724 | - true |
|
| 725 | - ); |
|
| 726 | - wp_localize_script( |
|
| 727 | - 'ee_form_section_validation', |
|
| 728 | - 'ee_form_section_validation_init', |
|
| 729 | - array('init' => $init_form_validation_automatically ? '1' : '0') |
|
| 730 | - ); |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - |
|
| 734 | - /** |
|
| 735 | - * gets the variables used by form_section_validation.js. |
|
| 736 | - * This needs to be called AFTER we've called $this->_enqueue_jquery_validate_script, |
|
| 737 | - * but before the wordpress hook wp_loaded |
|
| 738 | - * |
|
| 739 | - * @throws EE_Error |
|
| 740 | - */ |
|
| 741 | - public function _enqueue_and_localize_form_js() |
|
| 742 | - { |
|
| 743 | - $this->ensure_construct_finalized_called(); |
|
| 744 | - // actually, we don't want to localize just yet. There may be other forms on the page. |
|
| 745 | - // so we need to add our form section data to a static variable accessible by all form sections |
|
| 746 | - // and localize it just before the footer |
|
| 747 | - $this->localize_validation_rules(); |
|
| 748 | - add_action('wp_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'), 2); |
|
| 749 | - add_action('admin_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms')); |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * add our form section data to a static variable accessible by all form sections |
|
| 755 | - * |
|
| 756 | - * @param bool $return_for_subsection |
|
| 757 | - * @return void |
|
| 758 | - * @throws EE_Error |
|
| 759 | - */ |
|
| 760 | - public function localize_validation_rules($return_for_subsection = false) |
|
| 761 | - { |
|
| 762 | - // we only want to localize vars ONCE for the entire form, |
|
| 763 | - // so if the form section doesn't have a parent, then it must be the top dog |
|
| 764 | - if ($return_for_subsection || ! $this->parent_section()) { |
|
| 765 | - EE_Form_Section_Proper::$_js_localization['form_data'][ $this->html_id() ] = array( |
|
| 766 | - 'form_section_id' => $this->html_id(true), |
|
| 767 | - 'validation_rules' => $this->get_jquery_validation_rules(), |
|
| 768 | - 'other_data' => $this->get_other_js_data(), |
|
| 769 | - 'errors' => $this->subsection_validation_errors_by_html_name(), |
|
| 770 | - ); |
|
| 771 | - EE_Form_Section_Proper::$_scripts_localized = true; |
|
| 772 | - } |
|
| 773 | - } |
|
| 774 | - |
|
| 775 | - |
|
| 776 | - /** |
|
| 777 | - * Gets an array of extra data that will be useful for client-side javascript. |
|
| 778 | - * This is primarily data added by inputs and forms in addition to any |
|
| 779 | - * scripts they might enqueue |
|
| 780 | - * |
|
| 781 | - * @param array $form_other_js_data |
|
| 782 | - * @return array |
|
| 783 | - * @throws EE_Error |
|
| 784 | - */ |
|
| 785 | - public function get_other_js_data($form_other_js_data = array()) |
|
| 786 | - { |
|
| 787 | - foreach ($this->subsections() as $subsection) { |
|
| 788 | - $form_other_js_data = $subsection->get_other_js_data($form_other_js_data); |
|
| 789 | - } |
|
| 790 | - return $form_other_js_data; |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - |
|
| 794 | - /** |
|
| 795 | - * Gets a flat array of inputs for this form section and its subsections. |
|
| 796 | - * Keys are their form names, and values are the inputs themselves |
|
| 797 | - * |
|
| 798 | - * @return EE_Form_Input_Base |
|
| 799 | - * @throws EE_Error |
|
| 800 | - */ |
|
| 801 | - public function inputs_in_subsections() |
|
| 802 | - { |
|
| 803 | - $inputs = array(); |
|
| 804 | - foreach ($this->subsections() as $subsection) { |
|
| 805 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
| 806 | - $inputs[ $subsection->html_name() ] = $subsection; |
|
| 807 | - } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
| 808 | - $inputs += $subsection->inputs_in_subsections(); |
|
| 809 | - } |
|
| 810 | - } |
|
| 811 | - return $inputs; |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - |
|
| 815 | - /** |
|
| 816 | - * Gets a flat array of all the validation errors. |
|
| 817 | - * Keys are html names (because those should be unique) |
|
| 818 | - * and values are a string of all their validation errors |
|
| 819 | - * |
|
| 820 | - * @return string[] |
|
| 821 | - * @throws EE_Error |
|
| 822 | - */ |
|
| 823 | - public function subsection_validation_errors_by_html_name() |
|
| 824 | - { |
|
| 825 | - $inputs = $this->inputs(); |
|
| 826 | - $errors = array(); |
|
| 827 | - foreach ($inputs as $form_input) { |
|
| 828 | - if ($form_input instanceof EE_Form_Input_Base && $form_input->get_validation_errors()) { |
|
| 829 | - $errors[ $form_input->html_name() ] = $form_input->get_validation_error_string(); |
|
| 830 | - } |
|
| 831 | - } |
|
| 832 | - return $errors; |
|
| 833 | - } |
|
| 834 | - |
|
| 835 | - |
|
| 836 | - /** |
|
| 837 | - * passes all the form data required by the JS to the JS, and enqueues the few required JS files. |
|
| 838 | - * Should be setup by each form during the _enqueues_and_localize_form_js |
|
| 839 | - * |
|
| 840 | - * @throws InvalidArgumentException |
|
| 841 | - * @throws InvalidInterfaceException |
|
| 842 | - * @throws InvalidDataTypeException |
|
| 843 | - */ |
|
| 844 | - public static function localize_script_for_all_forms() |
|
| 845 | - { |
|
| 846 | - // allow inputs and stuff to hook in their JS and stuff here |
|
| 847 | - do_action('AHEE__EE_Form_Section_Proper__localize_script_for_all_forms__begin'); |
|
| 848 | - EE_Form_Section_Proper::$_js_localization['localized_error_messages'] = EE_Form_Section_Proper::_get_localized_error_messages(); |
|
| 849 | - $email_validation_level = isset(EE_Registry::instance()->CFG->registration->email_validation_level) |
|
| 850 | - ? EE_Registry::instance()->CFG->registration->email_validation_level |
|
| 851 | - : 'wp_default'; |
|
| 852 | - EE_Form_Section_Proper::$_js_localization['email_validation_level'] = $email_validation_level; |
|
| 853 | - wp_enqueue_script('ee_form_section_validation'); |
|
| 854 | - wp_localize_script( |
|
| 855 | - 'ee_form_section_validation', |
|
| 856 | - 'ee_form_section_vars', |
|
| 857 | - EE_Form_Section_Proper::$_js_localization |
|
| 858 | - ); |
|
| 859 | - } |
|
| 860 | - |
|
| 861 | - |
|
| 862 | - /** |
|
| 863 | - * ensure_scripts_localized |
|
| 864 | - * |
|
| 865 | - * @throws EE_Error |
|
| 866 | - */ |
|
| 867 | - public function ensure_scripts_localized() |
|
| 868 | - { |
|
| 869 | - if (! EE_Form_Section_Proper::$_scripts_localized) { |
|
| 870 | - $this->_enqueue_and_localize_form_js(); |
|
| 871 | - } |
|
| 872 | - } |
|
| 873 | - |
|
| 874 | - |
|
| 875 | - /** |
|
| 876 | - * Gets the hard-coded validation error messages to be used in the JS. The convention |
|
| 877 | - * is that the key here should be the same as the custom validation rule put in the JS file |
|
| 878 | - * |
|
| 879 | - * @return array keys are custom validation rules, and values are internationalized strings |
|
| 880 | - */ |
|
| 881 | - private static function _get_localized_error_messages() |
|
| 882 | - { |
|
| 883 | - return array( |
|
| 884 | - 'validUrl' => wp_strip_all_tags(__('This is not a valid absolute URL. Eg, http://domain.com/monkey.jpg', 'event_espresso')), |
|
| 885 | - 'regex' => wp_strip_all_tags(__('Please check your input', 'event_espresso')) |
|
| 886 | - ); |
|
| 887 | - } |
|
| 888 | - |
|
| 889 | - |
|
| 890 | - /** |
|
| 891 | - * @return array |
|
| 892 | - */ |
|
| 893 | - public static function js_localization() |
|
| 894 | - { |
|
| 895 | - return self::$_js_localization; |
|
| 896 | - } |
|
| 897 | - |
|
| 898 | - |
|
| 899 | - /** |
|
| 900 | - * @return void |
|
| 901 | - */ |
|
| 902 | - public static function reset_js_localization() |
|
| 903 | - { |
|
| 904 | - self::$_js_localization = array(); |
|
| 905 | - } |
|
| 906 | - |
|
| 907 | - |
|
| 908 | - /** |
|
| 909 | - * Gets the JS to put inside the jquery validation rules for subsection of this form section. |
|
| 910 | - * See parent function for more... |
|
| 911 | - * |
|
| 912 | - * @return array |
|
| 913 | - * @throws EE_Error |
|
| 914 | - */ |
|
| 915 | - public function get_jquery_validation_rules() |
|
| 916 | - { |
|
| 917 | - $jquery_validation_rules = array(); |
|
| 918 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 919 | - $jquery_validation_rules = array_merge( |
|
| 920 | - $jquery_validation_rules, |
|
| 921 | - $subsection->get_jquery_validation_rules() |
|
| 922 | - ); |
|
| 923 | - } |
|
| 924 | - return $jquery_validation_rules; |
|
| 925 | - } |
|
| 926 | - |
|
| 927 | - |
|
| 928 | - /** |
|
| 929 | - * Sanitizes all the data and sets the sanitized value of each field |
|
| 930 | - * |
|
| 931 | - * @param array $req_data |
|
| 932 | - * @return void |
|
| 933 | - * @throws EE_Error |
|
| 934 | - */ |
|
| 935 | - protected function _normalize($req_data) |
|
| 936 | - { |
|
| 937 | - $this->_received_submission = true; |
|
| 938 | - $this->_validation_errors = array(); |
|
| 939 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 940 | - try { |
|
| 941 | - $subsection->_normalize($req_data); |
|
| 942 | - } catch (EE_Validation_Error $e) { |
|
| 943 | - $subsection->add_validation_error($e); |
|
| 944 | - } |
|
| 945 | - } |
|
| 946 | - } |
|
| 947 | - |
|
| 948 | - |
|
| 949 | - /** |
|
| 950 | - * Performs validation on this form section and its subsections. |
|
| 951 | - * For each subsection, |
|
| 952 | - * calls _validate_{subsection_name} on THIS form (if the function exists) |
|
| 953 | - * and passes it the subsection, then calls _validate on that subsection. |
|
| 954 | - * If you need to perform validation on the form as a whole (considering multiple) |
|
| 955 | - * you would be best to override this _validate method, |
|
| 956 | - * calling parent::_validate() first. |
|
| 957 | - * |
|
| 958 | - * @throws EE_Error |
|
| 959 | - */ |
|
| 960 | - protected function _validate() |
|
| 961 | - { |
|
| 962 | - // reset the cache of whether this form is valid or not- we're re-validating it now |
|
| 963 | - $this->is_valid = null; |
|
| 964 | - foreach ($this->get_validatable_subsections() as $subsection_name => $subsection) { |
|
| 965 | - if (method_exists($this, '_validate_' . $subsection_name)) { |
|
| 966 | - call_user_func_array(array($this, '_validate_' . $subsection_name), array($subsection)); |
|
| 967 | - } |
|
| 968 | - $subsection->_validate(); |
|
| 969 | - } |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - |
|
| 973 | - /** |
|
| 974 | - * Gets all the validated inputs for the form section |
|
| 975 | - * |
|
| 976 | - * @return array |
|
| 977 | - * @throws EE_Error |
|
| 978 | - */ |
|
| 979 | - public function valid_data() |
|
| 980 | - { |
|
| 981 | - $inputs = array(); |
|
| 982 | - foreach ($this->subsections() as $subsection_name => $subsection) { |
|
| 983 | - if ($subsection instanceof EE_Form_Section_Proper) { |
|
| 984 | - $inputs[ $subsection_name ] = $subsection->valid_data(); |
|
| 985 | - } elseif ($subsection instanceof EE_Form_Input_Base) { |
|
| 986 | - $inputs[ $subsection_name ] = $subsection->normalized_value(); |
|
| 987 | - } |
|
| 988 | - } |
|
| 989 | - return $inputs; |
|
| 990 | - } |
|
| 991 | - |
|
| 992 | - |
|
| 993 | - /** |
|
| 994 | - * Gets all the inputs on this form section |
|
| 995 | - * |
|
| 996 | - * @return EE_Form_Input_Base[] |
|
| 997 | - * @throws EE_Error |
|
| 998 | - */ |
|
| 999 | - public function inputs() |
|
| 1000 | - { |
|
| 1001 | - $inputs = array(); |
|
| 1002 | - foreach ($this->subsections() as $subsection_name => $subsection) { |
|
| 1003 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
| 1004 | - $inputs[ $subsection_name ] = $subsection; |
|
| 1005 | - } |
|
| 1006 | - } |
|
| 1007 | - return $inputs; |
|
| 1008 | - } |
|
| 1009 | - |
|
| 1010 | - |
|
| 1011 | - /** |
|
| 1012 | - * Gets all the subsections which are a proper form |
|
| 1013 | - * |
|
| 1014 | - * @return EE_Form_Section_Proper[] |
|
| 1015 | - * @throws EE_Error |
|
| 1016 | - */ |
|
| 1017 | - public function subforms() |
|
| 1018 | - { |
|
| 1019 | - $form_sections = array(); |
|
| 1020 | - foreach ($this->subsections() as $name => $obj) { |
|
| 1021 | - if ($obj instanceof EE_Form_Section_Proper) { |
|
| 1022 | - $form_sections[ $name ] = $obj; |
|
| 1023 | - } |
|
| 1024 | - } |
|
| 1025 | - return $form_sections; |
|
| 1026 | - } |
|
| 1027 | - |
|
| 1028 | - |
|
| 1029 | - /** |
|
| 1030 | - * Gets all the subsections (inputs, proper subsections, or html-only sections). |
|
| 1031 | - * Consider using inputs() or subforms() |
|
| 1032 | - * if you only want form inputs or proper form sections. |
|
| 1033 | - * |
|
| 1034 | - * @param boolean $require_construction_to_be_finalized most client code should |
|
| 1035 | - * leave this as TRUE so that the inputs will be properly |
|
| 1036 | - * configured. However, some client code may be ok with |
|
| 1037 | - * construction finalize being called later |
|
| 1038 | - * (realizing that the subsections' html names might not be |
|
| 1039 | - * set yet, etc.) |
|
| 1040 | - * @return EE_Form_Section_Proper[] |
|
| 1041 | - * @throws EE_Error |
|
| 1042 | - */ |
|
| 1043 | - public function subsections($require_construction_to_be_finalized = true) |
|
| 1044 | - { |
|
| 1045 | - if ($require_construction_to_be_finalized) { |
|
| 1046 | - $this->ensure_construct_finalized_called(); |
|
| 1047 | - } |
|
| 1048 | - return $this->_subsections; |
|
| 1049 | - } |
|
| 1050 | - |
|
| 1051 | - |
|
| 1052 | - /** |
|
| 1053 | - * Returns whether this form has any subforms or inputs |
|
| 1054 | - * @return bool |
|
| 1055 | - */ |
|
| 1056 | - public function hasSubsections() |
|
| 1057 | - { |
|
| 1058 | - return ! empty($this->_subsections); |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - |
|
| 1062 | - /** |
|
| 1063 | - * Returns a simple array where keys are input names, and values are their normalized |
|
| 1064 | - * values. (Similar to calling get_input_value on inputs) |
|
| 1065 | - * |
|
| 1066 | - * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
| 1067 | - * or just this forms' direct children inputs |
|
| 1068 | - * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
| 1069 | - * or allow multidimensional array |
|
| 1070 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
| 1071 | - * with array keys being input names |
|
| 1072 | - * (regardless of whether they are from a subsection or not), |
|
| 1073 | - * and if $flatten is FALSE it can be a multidimensional array |
|
| 1074 | - * where keys are always subsection names and values are either |
|
| 1075 | - * the input's normalized value, or an array like the top-level array |
|
| 1076 | - * @throws EE_Error |
|
| 1077 | - */ |
|
| 1078 | - public function input_values($include_subform_inputs = false, $flatten = false) |
|
| 1079 | - { |
|
| 1080 | - return $this->_input_values(false, $include_subform_inputs, $flatten); |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - |
|
| 1084 | - /** |
|
| 1085 | - * Similar to EE_Form_Section_Proper::input_values(), except this returns the 'display_value' |
|
| 1086 | - * of each input. On some inputs (especially radio boxes or checkboxes), the value stored |
|
| 1087 | - * is not necessarily the value we want to display to users. This creates an array |
|
| 1088 | - * where keys are the input names, and values are their display values |
|
| 1089 | - * |
|
| 1090 | - * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
| 1091 | - * or just this forms' direct children inputs |
|
| 1092 | - * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
| 1093 | - * or allow multidimensional array |
|
| 1094 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
| 1095 | - * with array keys being input names |
|
| 1096 | - * (regardless of whether they are from a subsection or not), |
|
| 1097 | - * and if $flatten is FALSE it can be a multidimensional array |
|
| 1098 | - * where keys are always subsection names and values are either |
|
| 1099 | - * the input's normalized value, or an array like the top-level array |
|
| 1100 | - * @throws EE_Error |
|
| 1101 | - */ |
|
| 1102 | - public function input_pretty_values($include_subform_inputs = false, $flatten = false) |
|
| 1103 | - { |
|
| 1104 | - return $this->_input_values(true, $include_subform_inputs, $flatten); |
|
| 1105 | - } |
|
| 1106 | - |
|
| 1107 | - |
|
| 1108 | - /** |
|
| 1109 | - * Gets the input values from the form |
|
| 1110 | - * |
|
| 1111 | - * @param boolean $pretty Whether to retrieve the pretty value, |
|
| 1112 | - * or just the normalized value |
|
| 1113 | - * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
| 1114 | - * or just this forms' direct children inputs |
|
| 1115 | - * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
| 1116 | - * or allow multidimensional array |
|
| 1117 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array with array keys being |
|
| 1118 | - * input names (regardless of whether they are from a subsection or not), |
|
| 1119 | - * and if $flatten is FALSE it can be a multidimensional array |
|
| 1120 | - * where keys are always subsection names and values are either |
|
| 1121 | - * the input's normalized value, or an array like the top-level array |
|
| 1122 | - * @throws EE_Error |
|
| 1123 | - */ |
|
| 1124 | - public function _input_values($pretty = false, $include_subform_inputs = false, $flatten = false) |
|
| 1125 | - { |
|
| 1126 | - $input_values = array(); |
|
| 1127 | - foreach ($this->subsections() as $subsection_name => $subsection) { |
|
| 1128 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
| 1129 | - $input_values[ $subsection_name ] = $pretty |
|
| 1130 | - ? $subsection->pretty_value() |
|
| 1131 | - : $subsection->normalized_value(); |
|
| 1132 | - } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subform_inputs) { |
|
| 1133 | - $subform_input_values = $subsection->_input_values( |
|
| 1134 | - $pretty, |
|
| 1135 | - $include_subform_inputs, |
|
| 1136 | - $flatten |
|
| 1137 | - ); |
|
| 1138 | - if ($flatten) { |
|
| 1139 | - $input_values = array_merge($input_values, $subform_input_values); |
|
| 1140 | - } else { |
|
| 1141 | - $input_values[ $subsection_name ] = $subform_input_values; |
|
| 1142 | - } |
|
| 1143 | - } |
|
| 1144 | - } |
|
| 1145 | - return $input_values; |
|
| 1146 | - } |
|
| 1147 | - |
|
| 1148 | - |
|
| 1149 | - /** |
|
| 1150 | - * Gets the originally submitted input values from the form |
|
| 1151 | - * |
|
| 1152 | - * @param boolean $include_subforms Whether to include inputs from subforms, |
|
| 1153 | - * or just this forms' direct children inputs |
|
| 1154 | - * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
| 1155 | - * with array keys being input names |
|
| 1156 | - * (regardless of whether they are from a subsection or not), |
|
| 1157 | - * and if $flatten is FALSE it can be a multidimensional array |
|
| 1158 | - * where keys are always subsection names and values are either |
|
| 1159 | - * the input's normalized value, or an array like the top-level array |
|
| 1160 | - * @throws EE_Error |
|
| 1161 | - */ |
|
| 1162 | - public function submitted_values($include_subforms = false) |
|
| 1163 | - { |
|
| 1164 | - $submitted_values = array(); |
|
| 1165 | - foreach ($this->subsections() as $subsection) { |
|
| 1166 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
| 1167 | - // is this input part of an array of inputs? |
|
| 1168 | - if (strpos($subsection->html_name(), '[') !== false) { |
|
| 1169 | - $full_input_name = EEH_Array::convert_array_values_to_keys( |
|
| 1170 | - explode( |
|
| 1171 | - '[', |
|
| 1172 | - str_replace(']', '', $subsection->html_name()) |
|
| 1173 | - ), |
|
| 1174 | - $subsection->raw_value() |
|
| 1175 | - ); |
|
| 1176 | - $submitted_values = array_replace_recursive($submitted_values, $full_input_name); |
|
| 1177 | - } else { |
|
| 1178 | - $submitted_values[ $subsection->html_name() ] = $subsection->raw_value(); |
|
| 1179 | - } |
|
| 1180 | - } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subforms) { |
|
| 1181 | - $subform_input_values = $subsection->submitted_values($include_subforms); |
|
| 1182 | - $submitted_values = array_replace_recursive($submitted_values, $subform_input_values); |
|
| 1183 | - } |
|
| 1184 | - } |
|
| 1185 | - return $submitted_values; |
|
| 1186 | - } |
|
| 1187 | - |
|
| 1188 | - |
|
| 1189 | - /** |
|
| 1190 | - * Indicates whether or not this form has received a submission yet |
|
| 1191 | - * (ie, had receive_form_submission called on it yet) |
|
| 1192 | - * |
|
| 1193 | - * @return boolean |
|
| 1194 | - * @throws EE_Error |
|
| 1195 | - */ |
|
| 1196 | - public function has_received_submission() |
|
| 1197 | - { |
|
| 1198 | - $this->ensure_construct_finalized_called(); |
|
| 1199 | - return $this->_received_submission; |
|
| 1200 | - } |
|
| 1201 | - |
|
| 1202 | - |
|
| 1203 | - /** |
|
| 1204 | - * Equivalent to passing 'exclude' in the constructor's options array. |
|
| 1205 | - * Removes the listed inputs from the form |
|
| 1206 | - * |
|
| 1207 | - * @param array $inputs_to_exclude values are the input names |
|
| 1208 | - * @return void |
|
| 1209 | - */ |
|
| 1210 | - public function exclude(array $inputs_to_exclude = array()) |
|
| 1211 | - { |
|
| 1212 | - foreach ($inputs_to_exclude as $input_to_exclude_name) { |
|
| 1213 | - unset($this->_subsections[ $input_to_exclude_name ]); |
|
| 1214 | - } |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - |
|
| 1218 | - /** |
|
| 1219 | - * Changes these inputs' display strategy to be EE_Hidden_Display_Strategy. |
|
| 1220 | - * @param array $inputs_to_hide |
|
| 1221 | - * @throws EE_Error |
|
| 1222 | - */ |
|
| 1223 | - public function hide(array $inputs_to_hide = array()) |
|
| 1224 | - { |
|
| 1225 | - foreach ($inputs_to_hide as $input_to_hide) { |
|
| 1226 | - $input = $this->get_input($input_to_hide); |
|
| 1227 | - $input->set_display_strategy(new EE_Hidden_Display_Strategy()); |
|
| 1228 | - } |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - |
|
| 1232 | - /** |
|
| 1233 | - * add_subsections |
|
| 1234 | - * Adds the listed subsections to the form section. |
|
| 1235 | - * If $subsection_name_to_target is provided, |
|
| 1236 | - * then new subsections are added before or after that subsection, |
|
| 1237 | - * otherwise to the start or end of the entire subsections array. |
|
| 1238 | - * |
|
| 1239 | - * @param EE_Form_Section_Base[] $new_subsections array of new form subsections |
|
| 1240 | - * where keys are their names |
|
| 1241 | - * @param string $subsection_name_to_target an existing for section that $new_subsections |
|
| 1242 | - * should be added before or after |
|
| 1243 | - * IF $subsection_name_to_target is null, |
|
| 1244 | - * then $new_subsections will be added to |
|
| 1245 | - * the beginning or end of the entire subsections array |
|
| 1246 | - * @param boolean $add_before whether to add $new_subsections, before or after |
|
| 1247 | - * $subsection_name_to_target, |
|
| 1248 | - * or if $subsection_name_to_target is null, |
|
| 1249 | - * before or after entire subsections array |
|
| 1250 | - * @return void |
|
| 1251 | - * @throws EE_Error |
|
| 1252 | - */ |
|
| 1253 | - public function add_subsections($new_subsections, $subsection_name_to_target = null, $add_before = true) |
|
| 1254 | - { |
|
| 1255 | - foreach ($new_subsections as $subsection_name => $subsection) { |
|
| 1256 | - if (! $subsection instanceof EE_Form_Section_Base) { |
|
| 1257 | - EE_Error::add_error( |
|
| 1258 | - sprintf( |
|
| 1259 | - esc_html__( |
|
| 1260 | - "Trying to add a %s as a subsection (it was named '%s') to the form section '%s'. It was removed.", |
|
| 1261 | - 'event_espresso' |
|
| 1262 | - ), |
|
| 1263 | - get_class($subsection), |
|
| 1264 | - $subsection_name, |
|
| 1265 | - $this->name() |
|
| 1266 | - ) |
|
| 1267 | - ); |
|
| 1268 | - unset($new_subsections[ $subsection_name ]); |
|
| 1269 | - } |
|
| 1270 | - } |
|
| 1271 | - $this->_subsections = EEH_Array::insert_into_array( |
|
| 1272 | - $this->_subsections, |
|
| 1273 | - $new_subsections, |
|
| 1274 | - $subsection_name_to_target, |
|
| 1275 | - $add_before |
|
| 1276 | - ); |
|
| 1277 | - if ($this->_construction_finalized) { |
|
| 1278 | - foreach ($this->_subsections as $name => $subsection) { |
|
| 1279 | - $subsection->_construct_finalize($this, $name); |
|
| 1280 | - } |
|
| 1281 | - } |
|
| 1282 | - } |
|
| 1283 | - |
|
| 1284 | - |
|
| 1285 | - /** |
|
| 1286 | - * @param string $subsection_name |
|
| 1287 | - * @param bool $recursive |
|
| 1288 | - * @return bool |
|
| 1289 | - */ |
|
| 1290 | - public function has_subsection($subsection_name, $recursive = false) |
|
| 1291 | - { |
|
| 1292 | - foreach ($this->_subsections as $name => $subsection) { |
|
| 1293 | - if ( |
|
| 1294 | - $name === $subsection_name |
|
| 1295 | - || ( |
|
| 1296 | - $recursive |
|
| 1297 | - && $subsection instanceof EE_Form_Section_Proper |
|
| 1298 | - && $subsection->has_subsection($subsection_name, $recursive) |
|
| 1299 | - ) |
|
| 1300 | - ) { |
|
| 1301 | - return true; |
|
| 1302 | - } |
|
| 1303 | - } |
|
| 1304 | - return false; |
|
| 1305 | - } |
|
| 1306 | - |
|
| 1307 | - |
|
| 1308 | - |
|
| 1309 | - /** |
|
| 1310 | - * Just gets all validatable subsections to clean their sensitive data |
|
| 1311 | - * |
|
| 1312 | - * @throws EE_Error |
|
| 1313 | - */ |
|
| 1314 | - public function clean_sensitive_data() |
|
| 1315 | - { |
|
| 1316 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 1317 | - $subsection->clean_sensitive_data(); |
|
| 1318 | - } |
|
| 1319 | - } |
|
| 1320 | - |
|
| 1321 | - |
|
| 1322 | - /** |
|
| 1323 | - * Sets the submission error message (aka validation error message for this form section and all sub-sections) |
|
| 1324 | - * @param string $form_submission_error_message |
|
| 1325 | - * @param EE_Form_Section_Validatable $form_section unused |
|
| 1326 | - * @throws EE_Error |
|
| 1327 | - */ |
|
| 1328 | - public function set_submission_error_message( |
|
| 1329 | - $form_submission_error_message = '' |
|
| 1330 | - ) { |
|
| 1331 | - $this->_form_submission_error_message = ! empty($form_submission_error_message) |
|
| 1332 | - ? $form_submission_error_message |
|
| 1333 | - : $this->getAllValidationErrorsString(); |
|
| 1334 | - } |
|
| 1335 | - |
|
| 1336 | - |
|
| 1337 | - /** |
|
| 1338 | - * Returns the cached error message. A default value is set for this during _validate(), |
|
| 1339 | - * (called during receive_form_submission) but it can be explicitly set using |
|
| 1340 | - * set_submission_error_message |
|
| 1341 | - * |
|
| 1342 | - * @return string |
|
| 1343 | - */ |
|
| 1344 | - public function submission_error_message() |
|
| 1345 | - { |
|
| 1346 | - return $this->_form_submission_error_message; |
|
| 1347 | - } |
|
| 1348 | - |
|
| 1349 | - |
|
| 1350 | - /** |
|
| 1351 | - * Sets a message to display if the data submitted to the form was valid. |
|
| 1352 | - * @param string $form_submission_success_message |
|
| 1353 | - */ |
|
| 1354 | - public function set_submission_success_message($form_submission_success_message = '') |
|
| 1355 | - { |
|
| 1356 | - $this->_form_submission_success_message = ! empty($form_submission_success_message) |
|
| 1357 | - ? $form_submission_success_message |
|
| 1358 | - : esc_html__('Form submitted successfully', 'event_espresso'); |
|
| 1359 | - } |
|
| 1360 | - |
|
| 1361 | - |
|
| 1362 | - /** |
|
| 1363 | - * Gets a message appropriate for display when the form is correctly submitted |
|
| 1364 | - * @return string |
|
| 1365 | - */ |
|
| 1366 | - public function submission_success_message() |
|
| 1367 | - { |
|
| 1368 | - return $this->_form_submission_success_message; |
|
| 1369 | - } |
|
| 1370 | - |
|
| 1371 | - |
|
| 1372 | - /** |
|
| 1373 | - * Returns the prefix that should be used on child of this form section for |
|
| 1374 | - * their html names. If this form section itself has a parent, prepends ITS |
|
| 1375 | - * prefix onto this form section's prefix. Used primarily by |
|
| 1376 | - * EE_Form_Input_Base::_set_default_html_name_if_empty |
|
| 1377 | - * |
|
| 1378 | - * @return string |
|
| 1379 | - * @throws EE_Error |
|
| 1380 | - */ |
|
| 1381 | - public function html_name_prefix() |
|
| 1382 | - { |
|
| 1383 | - if ($this->parent_section() instanceof EE_Form_Section_Proper) { |
|
| 1384 | - return $this->parent_section()->html_name_prefix() . '[' . $this->name() . ']'; |
|
| 1385 | - } |
|
| 1386 | - return $this->name(); |
|
| 1387 | - } |
|
| 1388 | - |
|
| 1389 | - |
|
| 1390 | - /** |
|
| 1391 | - * Gets the name, but first checks _construct_finalize has been called. If not, |
|
| 1392 | - * calls it (assumes there is no parent and that we want the name to be whatever |
|
| 1393 | - * was set, which is probably nothing, or the classname) |
|
| 1394 | - * |
|
| 1395 | - * @return string |
|
| 1396 | - * @throws EE_Error |
|
| 1397 | - */ |
|
| 1398 | - public function name() |
|
| 1399 | - { |
|
| 1400 | - $this->ensure_construct_finalized_called(); |
|
| 1401 | - return parent::name(); |
|
| 1402 | - } |
|
| 1403 | - |
|
| 1404 | - |
|
| 1405 | - /** |
|
| 1406 | - * @return EE_Form_Section_Proper |
|
| 1407 | - * @throws EE_Error |
|
| 1408 | - */ |
|
| 1409 | - public function parent_section() |
|
| 1410 | - { |
|
| 1411 | - $this->ensure_construct_finalized_called(); |
|
| 1412 | - return parent::parent_section(); |
|
| 1413 | - } |
|
| 1414 | - |
|
| 1415 | - |
|
| 1416 | - /** |
|
| 1417 | - * make sure construction finalized was called, otherwise children might not be ready |
|
| 1418 | - * |
|
| 1419 | - * @return void |
|
| 1420 | - * @throws EE_Error |
|
| 1421 | - */ |
|
| 1422 | - public function ensure_construct_finalized_called() |
|
| 1423 | - { |
|
| 1424 | - if (! $this->_construction_finalized) { |
|
| 1425 | - $this->_construct_finalize($this->_parent_section, $this->_name); |
|
| 1426 | - } |
|
| 1427 | - } |
|
| 1428 | - |
|
| 1429 | - |
|
| 1430 | - /** |
|
| 1431 | - * Checks if any of this form section's inputs, or any of its children's inputs, |
|
| 1432 | - * are in teh form data. If any are found, returns true. Else false |
|
| 1433 | - * |
|
| 1434 | - * @param array $req_data |
|
| 1435 | - * @return boolean |
|
| 1436 | - * @throws EE_Error |
|
| 1437 | - */ |
|
| 1438 | - public function form_data_present_in($req_data = null) |
|
| 1439 | - { |
|
| 1440 | - $req_data = $this->getCachedRequest($req_data); |
|
| 1441 | - foreach ($this->subsections() as $subsection) { |
|
| 1442 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
| 1443 | - if ($subsection->form_data_present_in($req_data)) { |
|
| 1444 | - return true; |
|
| 1445 | - } |
|
| 1446 | - } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
| 1447 | - if ($subsection->form_data_present_in($req_data)) { |
|
| 1448 | - return true; |
|
| 1449 | - } |
|
| 1450 | - } |
|
| 1451 | - } |
|
| 1452 | - return false; |
|
| 1453 | - } |
|
| 1454 | - |
|
| 1455 | - |
|
| 1456 | - /** |
|
| 1457 | - * Gets validation errors for this form section and subsections |
|
| 1458 | - * Similar to EE_Form_Section_Validatable::get_validation_errors() except this |
|
| 1459 | - * gets the validation errors for ALL subsection |
|
| 1460 | - * |
|
| 1461 | - * @return EE_Validation_Error[] |
|
| 1462 | - * @throws EE_Error |
|
| 1463 | - */ |
|
| 1464 | - public function get_validation_errors_accumulated() |
|
| 1465 | - { |
|
| 1466 | - $validation_errors = $this->get_validation_errors(); |
|
| 1467 | - foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 1468 | - if ($subsection instanceof EE_Form_Section_Proper) { |
|
| 1469 | - $validation_errors_on_this_subsection = $subsection->get_validation_errors_accumulated(); |
|
| 1470 | - } else { |
|
| 1471 | - $validation_errors_on_this_subsection = $subsection->get_validation_errors(); |
|
| 1472 | - } |
|
| 1473 | - if ($validation_errors_on_this_subsection) { |
|
| 1474 | - $validation_errors = array_merge($validation_errors, $validation_errors_on_this_subsection); |
|
| 1475 | - } |
|
| 1476 | - } |
|
| 1477 | - return $validation_errors; |
|
| 1478 | - } |
|
| 1479 | - |
|
| 1480 | - /** |
|
| 1481 | - * Fetch validation errors from children and grandchildren and puts them in a single string. |
|
| 1482 | - * This traverses the form section tree to generate this, but you probably want to instead use |
|
| 1483 | - * get_form_submission_error_message() which is usually this message cached (or a custom validation error message) |
|
| 1484 | - * |
|
| 1485 | - * @return string |
|
| 1486 | - * @since 4.9.59.p |
|
| 1487 | - */ |
|
| 1488 | - protected function getAllValidationErrorsString() |
|
| 1489 | - { |
|
| 1490 | - $submission_error_messages = array(); |
|
| 1491 | - // bad, bad, bad registrant |
|
| 1492 | - foreach ($this->get_validation_errors_accumulated() as $validation_error) { |
|
| 1493 | - if ($validation_error instanceof EE_Validation_Error) { |
|
| 1494 | - $form_section = $validation_error->get_form_section(); |
|
| 1495 | - if ($form_section instanceof EE_Form_Input_Base) { |
|
| 1496 | - $label = $validation_error->get_form_section()->html_label_text(); |
|
| 1497 | - } elseif ($form_section instanceof EE_Form_Section_Validatable) { |
|
| 1498 | - $label = $validation_error->get_form_section()->name(); |
|
| 1499 | - } else { |
|
| 1500 | - $label = esc_html__('Unknown', 'event_espresso'); |
|
| 1501 | - } |
|
| 1502 | - $submission_error_messages[] = sprintf( |
|
| 1503 | - esc_html__('%s : %s', 'event_espresso'), |
|
| 1504 | - $label, |
|
| 1505 | - $validation_error->getMessage() |
|
| 1506 | - ); |
|
| 1507 | - } |
|
| 1508 | - } |
|
| 1509 | - return implode('<br>', $submission_error_messages); |
|
| 1510 | - } |
|
| 1511 | - |
|
| 1512 | - |
|
| 1513 | - /** |
|
| 1514 | - * This isn't just the name of an input, it's a path pointing to an input. The |
|
| 1515 | - * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
| 1516 | - * dot-dot-slash (../) means to ascend into the parent section. |
|
| 1517 | - * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
| 1518 | - * which will be returned. |
|
| 1519 | - * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
| 1520 | - * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
| 1521 | - * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
| 1522 | - * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
| 1523 | - * Etc |
|
| 1524 | - * |
|
| 1525 | - * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
| 1526 | - * @return EE_Form_Section_Base |
|
| 1527 | - * @throws EE_Error |
|
| 1528 | - */ |
|
| 1529 | - public function find_section_from_path($form_section_path) |
|
| 1530 | - { |
|
| 1531 | - // check if we can find the input from purely going straight up the tree |
|
| 1532 | - $input = parent::find_section_from_path($form_section_path); |
|
| 1533 | - if ($input instanceof EE_Form_Section_Base) { |
|
| 1534 | - return $input; |
|
| 1535 | - } |
|
| 1536 | - $next_slash_pos = strpos($form_section_path, '/'); |
|
| 1537 | - if ($next_slash_pos !== false) { |
|
| 1538 | - $child_section_name = substr($form_section_path, 0, $next_slash_pos); |
|
| 1539 | - $subpath = substr($form_section_path, $next_slash_pos + 1); |
|
| 1540 | - } else { |
|
| 1541 | - $child_section_name = $form_section_path; |
|
| 1542 | - $subpath = ''; |
|
| 1543 | - } |
|
| 1544 | - $child_section = $this->get_subsection($child_section_name); |
|
| 1545 | - if ($child_section instanceof EE_Form_Section_Base) { |
|
| 1546 | - return $child_section->find_section_from_path($subpath); |
|
| 1547 | - } |
|
| 1548 | - return null; |
|
| 1549 | - } |
|
| 360 | + /** |
|
| 361 | + * retrieves the originally submitted input values in the session |
|
| 362 | + * so that they can be used to repopulate the form if it failed validation |
|
| 363 | + * |
|
| 364 | + * @return array |
|
| 365 | + * @throws InvalidArgumentException |
|
| 366 | + * @throws InvalidInterfaceException |
|
| 367 | + * @throws InvalidDataTypeException |
|
| 368 | + */ |
|
| 369 | + protected function get_submitted_form_data_from_session() |
|
| 370 | + { |
|
| 371 | + $session = EE_Registry::instance()->SSN; |
|
| 372 | + if ($session instanceof EE_Session) { |
|
| 373 | + return $session->get_session_data( |
|
| 374 | + EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY |
|
| 375 | + ); |
|
| 376 | + } |
|
| 377 | + return array(); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * flushed the originally submitted input values from the session |
|
| 383 | + * |
|
| 384 | + * @return boolean whether or not the data was successfully removed from the session |
|
| 385 | + * @throws InvalidArgumentException |
|
| 386 | + * @throws InvalidInterfaceException |
|
| 387 | + * @throws InvalidDataTypeException |
|
| 388 | + */ |
|
| 389 | + public static function flush_submitted_form_data_from_session() |
|
| 390 | + { |
|
| 391 | + return EE_Registry::instance()->SSN->reset_data( |
|
| 392 | + array(EE_Form_Section_Proper::SUBMITTED_FORM_DATA_SSN_KEY) |
|
| 393 | + ); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Populates this form and its subsections with data from the session. |
|
| 399 | + * (Wrapper for EE_Form_Section_Proper::receive_form_submission, so it shows |
|
| 400 | + * validation errors when displaying too) |
|
| 401 | + * Returns true if the form was populated from the session, false otherwise |
|
| 402 | + * |
|
| 403 | + * @return boolean |
|
| 404 | + * @throws InvalidArgumentException |
|
| 405 | + * @throws InvalidInterfaceException |
|
| 406 | + * @throws InvalidDataTypeException |
|
| 407 | + * @throws EE_Error |
|
| 408 | + */ |
|
| 409 | + public function populate_from_session() |
|
| 410 | + { |
|
| 411 | + $form_data_in_session = $this->get_submitted_form_data_from_session(); |
|
| 412 | + if (empty($form_data_in_session)) { |
|
| 413 | + return false; |
|
| 414 | + } |
|
| 415 | + $this->receive_form_submission($form_data_in_session); |
|
| 416 | + add_action('shutdown', array('EE_Form_Section_Proper', 'flush_submitted_form_data_from_session')); |
|
| 417 | + if ($this->form_data_present_in($form_data_in_session)) { |
|
| 418 | + return true; |
|
| 419 | + } |
|
| 420 | + return false; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Populates the default data for the form, given an array where keys are |
|
| 426 | + * the input names, and values are their values (preferably normalized to be their |
|
| 427 | + * proper PHP types, not all strings... although that should be ok too). |
|
| 428 | + * Proper subsections are sub-arrays, the key being the subsection's name, and |
|
| 429 | + * the value being an array formatted in teh same way |
|
| 430 | + * |
|
| 431 | + * @param array $default_data |
|
| 432 | + * @throws EE_Error |
|
| 433 | + */ |
|
| 434 | + public function populate_defaults($default_data) |
|
| 435 | + { |
|
| 436 | + foreach ($this->subsections(false) as $subsection_name => $subsection) { |
|
| 437 | + if (isset($default_data[ $subsection_name ])) { |
|
| 438 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
| 439 | + $subsection->set_default($default_data[ $subsection_name ]); |
|
| 440 | + } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
| 441 | + $subsection->populate_defaults($default_data[ $subsection_name ]); |
|
| 442 | + } |
|
| 443 | + } |
|
| 444 | + } |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * returns true if subsection exists |
|
| 450 | + * |
|
| 451 | + * @param string $name |
|
| 452 | + * @return boolean |
|
| 453 | + */ |
|
| 454 | + public function subsection_exists($name) |
|
| 455 | + { |
|
| 456 | + return isset($this->_subsections[ $name ]) ? true : false; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * Gets the subsection specified by its name |
|
| 462 | + * |
|
| 463 | + * @param string $name |
|
| 464 | + * @param boolean $require_construction_to_be_finalized most client code should leave this as TRUE |
|
| 465 | + * so that the inputs will be properly configured. |
|
| 466 | + * However, some client code may be ok |
|
| 467 | + * with construction finalize being called later |
|
| 468 | + * (realizing that the subsections' html names |
|
| 469 | + * might not be set yet, etc.) |
|
| 470 | + * @return EE_Form_Section_Base |
|
| 471 | + * @throws EE_Error |
|
| 472 | + */ |
|
| 473 | + public function get_subsection($name, $require_construction_to_be_finalized = true) |
|
| 474 | + { |
|
| 475 | + if ($require_construction_to_be_finalized) { |
|
| 476 | + $this->ensure_construct_finalized_called(); |
|
| 477 | + } |
|
| 478 | + return $this->subsection_exists($name) ? $this->_subsections[ $name ] : null; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Gets all the validatable subsections of this form section |
|
| 484 | + * |
|
| 485 | + * @return EE_Form_Section_Validatable[] |
|
| 486 | + * @throws EE_Error |
|
| 487 | + */ |
|
| 488 | + public function get_validatable_subsections() |
|
| 489 | + { |
|
| 490 | + $validatable_subsections = array(); |
|
| 491 | + foreach ($this->subsections() as $name => $obj) { |
|
| 492 | + if ($obj instanceof EE_Form_Section_Validatable) { |
|
| 493 | + $validatable_subsections[ $name ] = $obj; |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + return $validatable_subsections; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * Gets an input by the given name. If not found, or if its not an EE_FOrm_Input_Base child, |
|
| 502 | + * throw an EE_Error. |
|
| 503 | + * |
|
| 504 | + * @param string $name |
|
| 505 | + * @param boolean $require_construction_to_be_finalized most client code should |
|
| 506 | + * leave this as TRUE so that the inputs will be properly |
|
| 507 | + * configured. However, some client code may be ok with |
|
| 508 | + * construction finalize being called later |
|
| 509 | + * (realizing that the subsections' html names might not be |
|
| 510 | + * set yet, etc.) |
|
| 511 | + * @return EE_Form_Input_Base |
|
| 512 | + * @throws EE_Error |
|
| 513 | + */ |
|
| 514 | + public function get_input($name, $require_construction_to_be_finalized = true) |
|
| 515 | + { |
|
| 516 | + $subsection = $this->get_subsection( |
|
| 517 | + $name, |
|
| 518 | + $require_construction_to_be_finalized |
|
| 519 | + ); |
|
| 520 | + if (! $subsection instanceof EE_Form_Input_Base) { |
|
| 521 | + throw new EE_Error( |
|
| 522 | + sprintf( |
|
| 523 | + esc_html__( |
|
| 524 | + "Subsection '%s' is not an instanceof EE_Form_Input_Base on form '%s'. It is a '%s'", |
|
| 525 | + 'event_espresso' |
|
| 526 | + ), |
|
| 527 | + $name, |
|
| 528 | + get_class($this), |
|
| 529 | + $subsection ? get_class($subsection) : esc_html__('NULL', 'event_espresso') |
|
| 530 | + ) |
|
| 531 | + ); |
|
| 532 | + } |
|
| 533 | + return $subsection; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * Like get_input(), gets the proper subsection of the form given the name, |
|
| 539 | + * otherwise throws an EE_Error |
|
| 540 | + * |
|
| 541 | + * @param string $name |
|
| 542 | + * @param boolean $require_construction_to_be_finalized most client code should |
|
| 543 | + * leave this as TRUE so that the inputs will be properly |
|
| 544 | + * configured. However, some client code may be ok with |
|
| 545 | + * construction finalize being called later |
|
| 546 | + * (realizing that the subsections' html names might not be |
|
| 547 | + * set yet, etc.) |
|
| 548 | + * @return EE_Form_Section_Proper |
|
| 549 | + * @throws EE_Error |
|
| 550 | + */ |
|
| 551 | + public function get_proper_subsection($name, $require_construction_to_be_finalized = true) |
|
| 552 | + { |
|
| 553 | + $subsection = $this->get_subsection( |
|
| 554 | + $name, |
|
| 555 | + $require_construction_to_be_finalized |
|
| 556 | + ); |
|
| 557 | + if (! $subsection instanceof EE_Form_Section_Proper) { |
|
| 558 | + throw new EE_Error( |
|
| 559 | + sprintf( |
|
| 560 | + esc_html__( |
|
| 561 | + "Subsection '%'s is not an instanceof EE_Form_Section_Proper on form '%s'", |
|
| 562 | + 'event_espresso' |
|
| 563 | + ), |
|
| 564 | + $name, |
|
| 565 | + get_class($this) |
|
| 566 | + ) |
|
| 567 | + ); |
|
| 568 | + } |
|
| 569 | + return $subsection; |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + |
|
| 573 | + /** |
|
| 574 | + * Gets the value of the specified input. Should be called after receive_form_submission() |
|
| 575 | + * or populate_defaults() on the form, where the normalized value on the input is set. |
|
| 576 | + * |
|
| 577 | + * @param string $name |
|
| 578 | + * @return mixed depending on the input's type and its normalization strategy |
|
| 579 | + * @throws EE_Error |
|
| 580 | + */ |
|
| 581 | + public function get_input_value($name) |
|
| 582 | + { |
|
| 583 | + $input = $this->get_input($name); |
|
| 584 | + return $input->normalized_value(); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + |
|
| 588 | + /** |
|
| 589 | + * Checks if this form section itself is valid, and then checks its subsections |
|
| 590 | + * |
|
| 591 | + * @throws EE_Error |
|
| 592 | + * @return boolean |
|
| 593 | + */ |
|
| 594 | + public function is_valid() |
|
| 595 | + { |
|
| 596 | + if ($this->is_valid === null) { |
|
| 597 | + if (! $this->has_received_submission()) { |
|
| 598 | + throw new EE_Error( |
|
| 599 | + sprintf( |
|
| 600 | + esc_html__( |
|
| 601 | + 'You cannot check if a form is valid before receiving the form submission using receive_form_submission', |
|
| 602 | + 'event_espresso' |
|
| 603 | + ) |
|
| 604 | + ) |
|
| 605 | + ); |
|
| 606 | + } |
|
| 607 | + if (! parent::is_valid()) { |
|
| 608 | + $this->is_valid = false; |
|
| 609 | + } else { |
|
| 610 | + // ok so no general errors to this entire form section. |
|
| 611 | + // so let's check the subsections, but only set errors if that hasn't been done yet |
|
| 612 | + $this->is_valid = true; |
|
| 613 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 614 | + if (! $subsection->is_valid()) { |
|
| 615 | + $this->is_valid = false; |
|
| 616 | + } |
|
| 617 | + } |
|
| 618 | + } |
|
| 619 | + } |
|
| 620 | + return $this->is_valid; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * gets the default name of this form section if none is specified |
|
| 626 | + * |
|
| 627 | + * @return void |
|
| 628 | + */ |
|
| 629 | + protected function _set_default_name_if_empty() |
|
| 630 | + { |
|
| 631 | + if (! $this->_name) { |
|
| 632 | + $classname = get_class($this); |
|
| 633 | + $default_name = str_replace('EE_', '', $classname); |
|
| 634 | + $this->_name = $default_name; |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Returns the HTML for the form, except for the form opening and closing tags |
|
| 641 | + * (as the form section doesn't know where you necessarily want to send the information to), |
|
| 642 | + * and except for a submit button. Enqueues JS and CSS; if called early enough we will |
|
| 643 | + * try to enqueue them in the header, otherwise they'll be enqueued in the footer. |
|
| 644 | + * Not doing_it_wrong because theoretically this CAN be used properly, |
|
| 645 | + * provided its used during "wp_enqueue_scripts", or it doesn't need to enqueue |
|
| 646 | + * any CSS. |
|
| 647 | + * |
|
| 648 | + * @throws InvalidArgumentException |
|
| 649 | + * @throws InvalidInterfaceException |
|
| 650 | + * @throws InvalidDataTypeException |
|
| 651 | + * @throws EE_Error |
|
| 652 | + */ |
|
| 653 | + public function get_html_and_js() |
|
| 654 | + { |
|
| 655 | + $this->enqueue_js(); |
|
| 656 | + return $this->get_html(); |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + |
|
| 660 | + /** |
|
| 661 | + * returns HTML for displaying this form section. recursively calls display_section() on all subsections |
|
| 662 | + * |
|
| 663 | + * @param bool $display_previously_submitted_data |
|
| 664 | + * @return string |
|
| 665 | + * @throws InvalidArgumentException |
|
| 666 | + * @throws InvalidInterfaceException |
|
| 667 | + * @throws InvalidDataTypeException |
|
| 668 | + * @throws EE_Error |
|
| 669 | + * @throws EE_Error |
|
| 670 | + * @throws EE_Error |
|
| 671 | + */ |
|
| 672 | + public function get_html($display_previously_submitted_data = true) |
|
| 673 | + { |
|
| 674 | + $this->ensure_construct_finalized_called(); |
|
| 675 | + if ($display_previously_submitted_data) { |
|
| 676 | + $this->populate_from_session(); |
|
| 677 | + } |
|
| 678 | + return $this->_form_html_filter |
|
| 679 | + ? $this->_form_html_filter->filterHtml($this->_layout_strategy->layout_form(), $this) |
|
| 680 | + : $this->_layout_strategy->layout_form(); |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + |
|
| 684 | + /** |
|
| 685 | + * enqueues JS and CSS for the form. |
|
| 686 | + * It is preferred to call this before wp_enqueue_scripts so the |
|
| 687 | + * scripts and styles can be put in the header, but if called later |
|
| 688 | + * they will be put in the footer (which is OK for JS, but in HTML4 CSS should |
|
| 689 | + * only be in the header; but in HTML5 its ok in the body. |
|
| 690 | + * See http://stackoverflow.com/questions/4957446/load-external-css-file-in-body-tag. |
|
| 691 | + * So if your form enqueues CSS, it's preferred to call this before wp_enqueue_scripts.) |
|
| 692 | + * |
|
| 693 | + * @return void |
|
| 694 | + * @throws EE_Error |
|
| 695 | + */ |
|
| 696 | + public function enqueue_js() |
|
| 697 | + { |
|
| 698 | + $this->_enqueue_and_localize_form_js(); |
|
| 699 | + foreach ($this->subsections() as $subsection) { |
|
| 700 | + $subsection->enqueue_js(); |
|
| 701 | + } |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * adds a filter so that jquery validate gets enqueued in EE_System::wp_enqueue_scripts(). |
|
| 707 | + * This must be done BEFORE wp_enqueue_scripts() gets called, which is on |
|
| 708 | + * the wp_enqueue_scripts hook. |
|
| 709 | + * However, registering the form js and localizing it can happen when we |
|
| 710 | + * actually output the form (which is preferred, seeing how teh form's fields |
|
| 711 | + * could change until it's actually outputted) |
|
| 712 | + * |
|
| 713 | + * @param boolean $init_form_validation_automatically whether or not we want the form validation |
|
| 714 | + * to be triggered automatically or not |
|
| 715 | + * @return void |
|
| 716 | + */ |
|
| 717 | + public static function wp_enqueue_scripts($init_form_validation_automatically = true) |
|
| 718 | + { |
|
| 719 | + wp_register_script( |
|
| 720 | + 'ee_form_section_validation', |
|
| 721 | + EE_GLOBAL_ASSETS_URL . 'scripts' . '/form_section_validation.js', |
|
| 722 | + array('jquery-validate', 'jquery-ui-datepicker', 'jquery-validate-extra-methods'), |
|
| 723 | + EVENT_ESPRESSO_VERSION, |
|
| 724 | + true |
|
| 725 | + ); |
|
| 726 | + wp_localize_script( |
|
| 727 | + 'ee_form_section_validation', |
|
| 728 | + 'ee_form_section_validation_init', |
|
| 729 | + array('init' => $init_form_validation_automatically ? '1' : '0') |
|
| 730 | + ); |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + |
|
| 734 | + /** |
|
| 735 | + * gets the variables used by form_section_validation.js. |
|
| 736 | + * This needs to be called AFTER we've called $this->_enqueue_jquery_validate_script, |
|
| 737 | + * but before the wordpress hook wp_loaded |
|
| 738 | + * |
|
| 739 | + * @throws EE_Error |
|
| 740 | + */ |
|
| 741 | + public function _enqueue_and_localize_form_js() |
|
| 742 | + { |
|
| 743 | + $this->ensure_construct_finalized_called(); |
|
| 744 | + // actually, we don't want to localize just yet. There may be other forms on the page. |
|
| 745 | + // so we need to add our form section data to a static variable accessible by all form sections |
|
| 746 | + // and localize it just before the footer |
|
| 747 | + $this->localize_validation_rules(); |
|
| 748 | + add_action('wp_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms'), 2); |
|
| 749 | + add_action('admin_footer', array('EE_Form_Section_Proper', 'localize_script_for_all_forms')); |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + |
|
| 753 | + /** |
|
| 754 | + * add our form section data to a static variable accessible by all form sections |
|
| 755 | + * |
|
| 756 | + * @param bool $return_for_subsection |
|
| 757 | + * @return void |
|
| 758 | + * @throws EE_Error |
|
| 759 | + */ |
|
| 760 | + public function localize_validation_rules($return_for_subsection = false) |
|
| 761 | + { |
|
| 762 | + // we only want to localize vars ONCE for the entire form, |
|
| 763 | + // so if the form section doesn't have a parent, then it must be the top dog |
|
| 764 | + if ($return_for_subsection || ! $this->parent_section()) { |
|
| 765 | + EE_Form_Section_Proper::$_js_localization['form_data'][ $this->html_id() ] = array( |
|
| 766 | + 'form_section_id' => $this->html_id(true), |
|
| 767 | + 'validation_rules' => $this->get_jquery_validation_rules(), |
|
| 768 | + 'other_data' => $this->get_other_js_data(), |
|
| 769 | + 'errors' => $this->subsection_validation_errors_by_html_name(), |
|
| 770 | + ); |
|
| 771 | + EE_Form_Section_Proper::$_scripts_localized = true; |
|
| 772 | + } |
|
| 773 | + } |
|
| 774 | + |
|
| 775 | + |
|
| 776 | + /** |
|
| 777 | + * Gets an array of extra data that will be useful for client-side javascript. |
|
| 778 | + * This is primarily data added by inputs and forms in addition to any |
|
| 779 | + * scripts they might enqueue |
|
| 780 | + * |
|
| 781 | + * @param array $form_other_js_data |
|
| 782 | + * @return array |
|
| 783 | + * @throws EE_Error |
|
| 784 | + */ |
|
| 785 | + public function get_other_js_data($form_other_js_data = array()) |
|
| 786 | + { |
|
| 787 | + foreach ($this->subsections() as $subsection) { |
|
| 788 | + $form_other_js_data = $subsection->get_other_js_data($form_other_js_data); |
|
| 789 | + } |
|
| 790 | + return $form_other_js_data; |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + |
|
| 794 | + /** |
|
| 795 | + * Gets a flat array of inputs for this form section and its subsections. |
|
| 796 | + * Keys are their form names, and values are the inputs themselves |
|
| 797 | + * |
|
| 798 | + * @return EE_Form_Input_Base |
|
| 799 | + * @throws EE_Error |
|
| 800 | + */ |
|
| 801 | + public function inputs_in_subsections() |
|
| 802 | + { |
|
| 803 | + $inputs = array(); |
|
| 804 | + foreach ($this->subsections() as $subsection) { |
|
| 805 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
| 806 | + $inputs[ $subsection->html_name() ] = $subsection; |
|
| 807 | + } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
| 808 | + $inputs += $subsection->inputs_in_subsections(); |
|
| 809 | + } |
|
| 810 | + } |
|
| 811 | + return $inputs; |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + |
|
| 815 | + /** |
|
| 816 | + * Gets a flat array of all the validation errors. |
|
| 817 | + * Keys are html names (because those should be unique) |
|
| 818 | + * and values are a string of all their validation errors |
|
| 819 | + * |
|
| 820 | + * @return string[] |
|
| 821 | + * @throws EE_Error |
|
| 822 | + */ |
|
| 823 | + public function subsection_validation_errors_by_html_name() |
|
| 824 | + { |
|
| 825 | + $inputs = $this->inputs(); |
|
| 826 | + $errors = array(); |
|
| 827 | + foreach ($inputs as $form_input) { |
|
| 828 | + if ($form_input instanceof EE_Form_Input_Base && $form_input->get_validation_errors()) { |
|
| 829 | + $errors[ $form_input->html_name() ] = $form_input->get_validation_error_string(); |
|
| 830 | + } |
|
| 831 | + } |
|
| 832 | + return $errors; |
|
| 833 | + } |
|
| 834 | + |
|
| 835 | + |
|
| 836 | + /** |
|
| 837 | + * passes all the form data required by the JS to the JS, and enqueues the few required JS files. |
|
| 838 | + * Should be setup by each form during the _enqueues_and_localize_form_js |
|
| 839 | + * |
|
| 840 | + * @throws InvalidArgumentException |
|
| 841 | + * @throws InvalidInterfaceException |
|
| 842 | + * @throws InvalidDataTypeException |
|
| 843 | + */ |
|
| 844 | + public static function localize_script_for_all_forms() |
|
| 845 | + { |
|
| 846 | + // allow inputs and stuff to hook in their JS and stuff here |
|
| 847 | + do_action('AHEE__EE_Form_Section_Proper__localize_script_for_all_forms__begin'); |
|
| 848 | + EE_Form_Section_Proper::$_js_localization['localized_error_messages'] = EE_Form_Section_Proper::_get_localized_error_messages(); |
|
| 849 | + $email_validation_level = isset(EE_Registry::instance()->CFG->registration->email_validation_level) |
|
| 850 | + ? EE_Registry::instance()->CFG->registration->email_validation_level |
|
| 851 | + : 'wp_default'; |
|
| 852 | + EE_Form_Section_Proper::$_js_localization['email_validation_level'] = $email_validation_level; |
|
| 853 | + wp_enqueue_script('ee_form_section_validation'); |
|
| 854 | + wp_localize_script( |
|
| 855 | + 'ee_form_section_validation', |
|
| 856 | + 'ee_form_section_vars', |
|
| 857 | + EE_Form_Section_Proper::$_js_localization |
|
| 858 | + ); |
|
| 859 | + } |
|
| 860 | + |
|
| 861 | + |
|
| 862 | + /** |
|
| 863 | + * ensure_scripts_localized |
|
| 864 | + * |
|
| 865 | + * @throws EE_Error |
|
| 866 | + */ |
|
| 867 | + public function ensure_scripts_localized() |
|
| 868 | + { |
|
| 869 | + if (! EE_Form_Section_Proper::$_scripts_localized) { |
|
| 870 | + $this->_enqueue_and_localize_form_js(); |
|
| 871 | + } |
|
| 872 | + } |
|
| 873 | + |
|
| 874 | + |
|
| 875 | + /** |
|
| 876 | + * Gets the hard-coded validation error messages to be used in the JS. The convention |
|
| 877 | + * is that the key here should be the same as the custom validation rule put in the JS file |
|
| 878 | + * |
|
| 879 | + * @return array keys are custom validation rules, and values are internationalized strings |
|
| 880 | + */ |
|
| 881 | + private static function _get_localized_error_messages() |
|
| 882 | + { |
|
| 883 | + return array( |
|
| 884 | + 'validUrl' => wp_strip_all_tags(__('This is not a valid absolute URL. Eg, http://domain.com/monkey.jpg', 'event_espresso')), |
|
| 885 | + 'regex' => wp_strip_all_tags(__('Please check your input', 'event_espresso')) |
|
| 886 | + ); |
|
| 887 | + } |
|
| 888 | + |
|
| 889 | + |
|
| 890 | + /** |
|
| 891 | + * @return array |
|
| 892 | + */ |
|
| 893 | + public static function js_localization() |
|
| 894 | + { |
|
| 895 | + return self::$_js_localization; |
|
| 896 | + } |
|
| 897 | + |
|
| 898 | + |
|
| 899 | + /** |
|
| 900 | + * @return void |
|
| 901 | + */ |
|
| 902 | + public static function reset_js_localization() |
|
| 903 | + { |
|
| 904 | + self::$_js_localization = array(); |
|
| 905 | + } |
|
| 906 | + |
|
| 907 | + |
|
| 908 | + /** |
|
| 909 | + * Gets the JS to put inside the jquery validation rules for subsection of this form section. |
|
| 910 | + * See parent function for more... |
|
| 911 | + * |
|
| 912 | + * @return array |
|
| 913 | + * @throws EE_Error |
|
| 914 | + */ |
|
| 915 | + public function get_jquery_validation_rules() |
|
| 916 | + { |
|
| 917 | + $jquery_validation_rules = array(); |
|
| 918 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 919 | + $jquery_validation_rules = array_merge( |
|
| 920 | + $jquery_validation_rules, |
|
| 921 | + $subsection->get_jquery_validation_rules() |
|
| 922 | + ); |
|
| 923 | + } |
|
| 924 | + return $jquery_validation_rules; |
|
| 925 | + } |
|
| 926 | + |
|
| 927 | + |
|
| 928 | + /** |
|
| 929 | + * Sanitizes all the data and sets the sanitized value of each field |
|
| 930 | + * |
|
| 931 | + * @param array $req_data |
|
| 932 | + * @return void |
|
| 933 | + * @throws EE_Error |
|
| 934 | + */ |
|
| 935 | + protected function _normalize($req_data) |
|
| 936 | + { |
|
| 937 | + $this->_received_submission = true; |
|
| 938 | + $this->_validation_errors = array(); |
|
| 939 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 940 | + try { |
|
| 941 | + $subsection->_normalize($req_data); |
|
| 942 | + } catch (EE_Validation_Error $e) { |
|
| 943 | + $subsection->add_validation_error($e); |
|
| 944 | + } |
|
| 945 | + } |
|
| 946 | + } |
|
| 947 | + |
|
| 948 | + |
|
| 949 | + /** |
|
| 950 | + * Performs validation on this form section and its subsections. |
|
| 951 | + * For each subsection, |
|
| 952 | + * calls _validate_{subsection_name} on THIS form (if the function exists) |
|
| 953 | + * and passes it the subsection, then calls _validate on that subsection. |
|
| 954 | + * If you need to perform validation on the form as a whole (considering multiple) |
|
| 955 | + * you would be best to override this _validate method, |
|
| 956 | + * calling parent::_validate() first. |
|
| 957 | + * |
|
| 958 | + * @throws EE_Error |
|
| 959 | + */ |
|
| 960 | + protected function _validate() |
|
| 961 | + { |
|
| 962 | + // reset the cache of whether this form is valid or not- we're re-validating it now |
|
| 963 | + $this->is_valid = null; |
|
| 964 | + foreach ($this->get_validatable_subsections() as $subsection_name => $subsection) { |
|
| 965 | + if (method_exists($this, '_validate_' . $subsection_name)) { |
|
| 966 | + call_user_func_array(array($this, '_validate_' . $subsection_name), array($subsection)); |
|
| 967 | + } |
|
| 968 | + $subsection->_validate(); |
|
| 969 | + } |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + |
|
| 973 | + /** |
|
| 974 | + * Gets all the validated inputs for the form section |
|
| 975 | + * |
|
| 976 | + * @return array |
|
| 977 | + * @throws EE_Error |
|
| 978 | + */ |
|
| 979 | + public function valid_data() |
|
| 980 | + { |
|
| 981 | + $inputs = array(); |
|
| 982 | + foreach ($this->subsections() as $subsection_name => $subsection) { |
|
| 983 | + if ($subsection instanceof EE_Form_Section_Proper) { |
|
| 984 | + $inputs[ $subsection_name ] = $subsection->valid_data(); |
|
| 985 | + } elseif ($subsection instanceof EE_Form_Input_Base) { |
|
| 986 | + $inputs[ $subsection_name ] = $subsection->normalized_value(); |
|
| 987 | + } |
|
| 988 | + } |
|
| 989 | + return $inputs; |
|
| 990 | + } |
|
| 991 | + |
|
| 992 | + |
|
| 993 | + /** |
|
| 994 | + * Gets all the inputs on this form section |
|
| 995 | + * |
|
| 996 | + * @return EE_Form_Input_Base[] |
|
| 997 | + * @throws EE_Error |
|
| 998 | + */ |
|
| 999 | + public function inputs() |
|
| 1000 | + { |
|
| 1001 | + $inputs = array(); |
|
| 1002 | + foreach ($this->subsections() as $subsection_name => $subsection) { |
|
| 1003 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
| 1004 | + $inputs[ $subsection_name ] = $subsection; |
|
| 1005 | + } |
|
| 1006 | + } |
|
| 1007 | + return $inputs; |
|
| 1008 | + } |
|
| 1009 | + |
|
| 1010 | + |
|
| 1011 | + /** |
|
| 1012 | + * Gets all the subsections which are a proper form |
|
| 1013 | + * |
|
| 1014 | + * @return EE_Form_Section_Proper[] |
|
| 1015 | + * @throws EE_Error |
|
| 1016 | + */ |
|
| 1017 | + public function subforms() |
|
| 1018 | + { |
|
| 1019 | + $form_sections = array(); |
|
| 1020 | + foreach ($this->subsections() as $name => $obj) { |
|
| 1021 | + if ($obj instanceof EE_Form_Section_Proper) { |
|
| 1022 | + $form_sections[ $name ] = $obj; |
|
| 1023 | + } |
|
| 1024 | + } |
|
| 1025 | + return $form_sections; |
|
| 1026 | + } |
|
| 1027 | + |
|
| 1028 | + |
|
| 1029 | + /** |
|
| 1030 | + * Gets all the subsections (inputs, proper subsections, or html-only sections). |
|
| 1031 | + * Consider using inputs() or subforms() |
|
| 1032 | + * if you only want form inputs or proper form sections. |
|
| 1033 | + * |
|
| 1034 | + * @param boolean $require_construction_to_be_finalized most client code should |
|
| 1035 | + * leave this as TRUE so that the inputs will be properly |
|
| 1036 | + * configured. However, some client code may be ok with |
|
| 1037 | + * construction finalize being called later |
|
| 1038 | + * (realizing that the subsections' html names might not be |
|
| 1039 | + * set yet, etc.) |
|
| 1040 | + * @return EE_Form_Section_Proper[] |
|
| 1041 | + * @throws EE_Error |
|
| 1042 | + */ |
|
| 1043 | + public function subsections($require_construction_to_be_finalized = true) |
|
| 1044 | + { |
|
| 1045 | + if ($require_construction_to_be_finalized) { |
|
| 1046 | + $this->ensure_construct_finalized_called(); |
|
| 1047 | + } |
|
| 1048 | + return $this->_subsections; |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + |
|
| 1052 | + /** |
|
| 1053 | + * Returns whether this form has any subforms or inputs |
|
| 1054 | + * @return bool |
|
| 1055 | + */ |
|
| 1056 | + public function hasSubsections() |
|
| 1057 | + { |
|
| 1058 | + return ! empty($this->_subsections); |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + |
|
| 1062 | + /** |
|
| 1063 | + * Returns a simple array where keys are input names, and values are their normalized |
|
| 1064 | + * values. (Similar to calling get_input_value on inputs) |
|
| 1065 | + * |
|
| 1066 | + * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
| 1067 | + * or just this forms' direct children inputs |
|
| 1068 | + * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
| 1069 | + * or allow multidimensional array |
|
| 1070 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
| 1071 | + * with array keys being input names |
|
| 1072 | + * (regardless of whether they are from a subsection or not), |
|
| 1073 | + * and if $flatten is FALSE it can be a multidimensional array |
|
| 1074 | + * where keys are always subsection names and values are either |
|
| 1075 | + * the input's normalized value, or an array like the top-level array |
|
| 1076 | + * @throws EE_Error |
|
| 1077 | + */ |
|
| 1078 | + public function input_values($include_subform_inputs = false, $flatten = false) |
|
| 1079 | + { |
|
| 1080 | + return $this->_input_values(false, $include_subform_inputs, $flatten); |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + |
|
| 1084 | + /** |
|
| 1085 | + * Similar to EE_Form_Section_Proper::input_values(), except this returns the 'display_value' |
|
| 1086 | + * of each input. On some inputs (especially radio boxes or checkboxes), the value stored |
|
| 1087 | + * is not necessarily the value we want to display to users. This creates an array |
|
| 1088 | + * where keys are the input names, and values are their display values |
|
| 1089 | + * |
|
| 1090 | + * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
| 1091 | + * or just this forms' direct children inputs |
|
| 1092 | + * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
| 1093 | + * or allow multidimensional array |
|
| 1094 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
| 1095 | + * with array keys being input names |
|
| 1096 | + * (regardless of whether they are from a subsection or not), |
|
| 1097 | + * and if $flatten is FALSE it can be a multidimensional array |
|
| 1098 | + * where keys are always subsection names and values are either |
|
| 1099 | + * the input's normalized value, or an array like the top-level array |
|
| 1100 | + * @throws EE_Error |
|
| 1101 | + */ |
|
| 1102 | + public function input_pretty_values($include_subform_inputs = false, $flatten = false) |
|
| 1103 | + { |
|
| 1104 | + return $this->_input_values(true, $include_subform_inputs, $flatten); |
|
| 1105 | + } |
|
| 1106 | + |
|
| 1107 | + |
|
| 1108 | + /** |
|
| 1109 | + * Gets the input values from the form |
|
| 1110 | + * |
|
| 1111 | + * @param boolean $pretty Whether to retrieve the pretty value, |
|
| 1112 | + * or just the normalized value |
|
| 1113 | + * @param boolean $include_subform_inputs Whether to include inputs from subforms, |
|
| 1114 | + * or just this forms' direct children inputs |
|
| 1115 | + * @param boolean $flatten Whether to force the results into 1-dimensional array, |
|
| 1116 | + * or allow multidimensional array |
|
| 1117 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array with array keys being |
|
| 1118 | + * input names (regardless of whether they are from a subsection or not), |
|
| 1119 | + * and if $flatten is FALSE it can be a multidimensional array |
|
| 1120 | + * where keys are always subsection names and values are either |
|
| 1121 | + * the input's normalized value, or an array like the top-level array |
|
| 1122 | + * @throws EE_Error |
|
| 1123 | + */ |
|
| 1124 | + public function _input_values($pretty = false, $include_subform_inputs = false, $flatten = false) |
|
| 1125 | + { |
|
| 1126 | + $input_values = array(); |
|
| 1127 | + foreach ($this->subsections() as $subsection_name => $subsection) { |
|
| 1128 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
| 1129 | + $input_values[ $subsection_name ] = $pretty |
|
| 1130 | + ? $subsection->pretty_value() |
|
| 1131 | + : $subsection->normalized_value(); |
|
| 1132 | + } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subform_inputs) { |
|
| 1133 | + $subform_input_values = $subsection->_input_values( |
|
| 1134 | + $pretty, |
|
| 1135 | + $include_subform_inputs, |
|
| 1136 | + $flatten |
|
| 1137 | + ); |
|
| 1138 | + if ($flatten) { |
|
| 1139 | + $input_values = array_merge($input_values, $subform_input_values); |
|
| 1140 | + } else { |
|
| 1141 | + $input_values[ $subsection_name ] = $subform_input_values; |
|
| 1142 | + } |
|
| 1143 | + } |
|
| 1144 | + } |
|
| 1145 | + return $input_values; |
|
| 1146 | + } |
|
| 1147 | + |
|
| 1148 | + |
|
| 1149 | + /** |
|
| 1150 | + * Gets the originally submitted input values from the form |
|
| 1151 | + * |
|
| 1152 | + * @param boolean $include_subforms Whether to include inputs from subforms, |
|
| 1153 | + * or just this forms' direct children inputs |
|
| 1154 | + * @return array if $flatten is TRUE it will always be a 1-dimensional array |
|
| 1155 | + * with array keys being input names |
|
| 1156 | + * (regardless of whether they are from a subsection or not), |
|
| 1157 | + * and if $flatten is FALSE it can be a multidimensional array |
|
| 1158 | + * where keys are always subsection names and values are either |
|
| 1159 | + * the input's normalized value, or an array like the top-level array |
|
| 1160 | + * @throws EE_Error |
|
| 1161 | + */ |
|
| 1162 | + public function submitted_values($include_subforms = false) |
|
| 1163 | + { |
|
| 1164 | + $submitted_values = array(); |
|
| 1165 | + foreach ($this->subsections() as $subsection) { |
|
| 1166 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
| 1167 | + // is this input part of an array of inputs? |
|
| 1168 | + if (strpos($subsection->html_name(), '[') !== false) { |
|
| 1169 | + $full_input_name = EEH_Array::convert_array_values_to_keys( |
|
| 1170 | + explode( |
|
| 1171 | + '[', |
|
| 1172 | + str_replace(']', '', $subsection->html_name()) |
|
| 1173 | + ), |
|
| 1174 | + $subsection->raw_value() |
|
| 1175 | + ); |
|
| 1176 | + $submitted_values = array_replace_recursive($submitted_values, $full_input_name); |
|
| 1177 | + } else { |
|
| 1178 | + $submitted_values[ $subsection->html_name() ] = $subsection->raw_value(); |
|
| 1179 | + } |
|
| 1180 | + } elseif ($subsection instanceof EE_Form_Section_Proper && $include_subforms) { |
|
| 1181 | + $subform_input_values = $subsection->submitted_values($include_subforms); |
|
| 1182 | + $submitted_values = array_replace_recursive($submitted_values, $subform_input_values); |
|
| 1183 | + } |
|
| 1184 | + } |
|
| 1185 | + return $submitted_values; |
|
| 1186 | + } |
|
| 1187 | + |
|
| 1188 | + |
|
| 1189 | + /** |
|
| 1190 | + * Indicates whether or not this form has received a submission yet |
|
| 1191 | + * (ie, had receive_form_submission called on it yet) |
|
| 1192 | + * |
|
| 1193 | + * @return boolean |
|
| 1194 | + * @throws EE_Error |
|
| 1195 | + */ |
|
| 1196 | + public function has_received_submission() |
|
| 1197 | + { |
|
| 1198 | + $this->ensure_construct_finalized_called(); |
|
| 1199 | + return $this->_received_submission; |
|
| 1200 | + } |
|
| 1201 | + |
|
| 1202 | + |
|
| 1203 | + /** |
|
| 1204 | + * Equivalent to passing 'exclude' in the constructor's options array. |
|
| 1205 | + * Removes the listed inputs from the form |
|
| 1206 | + * |
|
| 1207 | + * @param array $inputs_to_exclude values are the input names |
|
| 1208 | + * @return void |
|
| 1209 | + */ |
|
| 1210 | + public function exclude(array $inputs_to_exclude = array()) |
|
| 1211 | + { |
|
| 1212 | + foreach ($inputs_to_exclude as $input_to_exclude_name) { |
|
| 1213 | + unset($this->_subsections[ $input_to_exclude_name ]); |
|
| 1214 | + } |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + |
|
| 1218 | + /** |
|
| 1219 | + * Changes these inputs' display strategy to be EE_Hidden_Display_Strategy. |
|
| 1220 | + * @param array $inputs_to_hide |
|
| 1221 | + * @throws EE_Error |
|
| 1222 | + */ |
|
| 1223 | + public function hide(array $inputs_to_hide = array()) |
|
| 1224 | + { |
|
| 1225 | + foreach ($inputs_to_hide as $input_to_hide) { |
|
| 1226 | + $input = $this->get_input($input_to_hide); |
|
| 1227 | + $input->set_display_strategy(new EE_Hidden_Display_Strategy()); |
|
| 1228 | + } |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + |
|
| 1232 | + /** |
|
| 1233 | + * add_subsections |
|
| 1234 | + * Adds the listed subsections to the form section. |
|
| 1235 | + * If $subsection_name_to_target is provided, |
|
| 1236 | + * then new subsections are added before or after that subsection, |
|
| 1237 | + * otherwise to the start or end of the entire subsections array. |
|
| 1238 | + * |
|
| 1239 | + * @param EE_Form_Section_Base[] $new_subsections array of new form subsections |
|
| 1240 | + * where keys are their names |
|
| 1241 | + * @param string $subsection_name_to_target an existing for section that $new_subsections |
|
| 1242 | + * should be added before or after |
|
| 1243 | + * IF $subsection_name_to_target is null, |
|
| 1244 | + * then $new_subsections will be added to |
|
| 1245 | + * the beginning or end of the entire subsections array |
|
| 1246 | + * @param boolean $add_before whether to add $new_subsections, before or after |
|
| 1247 | + * $subsection_name_to_target, |
|
| 1248 | + * or if $subsection_name_to_target is null, |
|
| 1249 | + * before or after entire subsections array |
|
| 1250 | + * @return void |
|
| 1251 | + * @throws EE_Error |
|
| 1252 | + */ |
|
| 1253 | + public function add_subsections($new_subsections, $subsection_name_to_target = null, $add_before = true) |
|
| 1254 | + { |
|
| 1255 | + foreach ($new_subsections as $subsection_name => $subsection) { |
|
| 1256 | + if (! $subsection instanceof EE_Form_Section_Base) { |
|
| 1257 | + EE_Error::add_error( |
|
| 1258 | + sprintf( |
|
| 1259 | + esc_html__( |
|
| 1260 | + "Trying to add a %s as a subsection (it was named '%s') to the form section '%s'. It was removed.", |
|
| 1261 | + 'event_espresso' |
|
| 1262 | + ), |
|
| 1263 | + get_class($subsection), |
|
| 1264 | + $subsection_name, |
|
| 1265 | + $this->name() |
|
| 1266 | + ) |
|
| 1267 | + ); |
|
| 1268 | + unset($new_subsections[ $subsection_name ]); |
|
| 1269 | + } |
|
| 1270 | + } |
|
| 1271 | + $this->_subsections = EEH_Array::insert_into_array( |
|
| 1272 | + $this->_subsections, |
|
| 1273 | + $new_subsections, |
|
| 1274 | + $subsection_name_to_target, |
|
| 1275 | + $add_before |
|
| 1276 | + ); |
|
| 1277 | + if ($this->_construction_finalized) { |
|
| 1278 | + foreach ($this->_subsections as $name => $subsection) { |
|
| 1279 | + $subsection->_construct_finalize($this, $name); |
|
| 1280 | + } |
|
| 1281 | + } |
|
| 1282 | + } |
|
| 1283 | + |
|
| 1284 | + |
|
| 1285 | + /** |
|
| 1286 | + * @param string $subsection_name |
|
| 1287 | + * @param bool $recursive |
|
| 1288 | + * @return bool |
|
| 1289 | + */ |
|
| 1290 | + public function has_subsection($subsection_name, $recursive = false) |
|
| 1291 | + { |
|
| 1292 | + foreach ($this->_subsections as $name => $subsection) { |
|
| 1293 | + if ( |
|
| 1294 | + $name === $subsection_name |
|
| 1295 | + || ( |
|
| 1296 | + $recursive |
|
| 1297 | + && $subsection instanceof EE_Form_Section_Proper |
|
| 1298 | + && $subsection->has_subsection($subsection_name, $recursive) |
|
| 1299 | + ) |
|
| 1300 | + ) { |
|
| 1301 | + return true; |
|
| 1302 | + } |
|
| 1303 | + } |
|
| 1304 | + return false; |
|
| 1305 | + } |
|
| 1306 | + |
|
| 1307 | + |
|
| 1308 | + |
|
| 1309 | + /** |
|
| 1310 | + * Just gets all validatable subsections to clean their sensitive data |
|
| 1311 | + * |
|
| 1312 | + * @throws EE_Error |
|
| 1313 | + */ |
|
| 1314 | + public function clean_sensitive_data() |
|
| 1315 | + { |
|
| 1316 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 1317 | + $subsection->clean_sensitive_data(); |
|
| 1318 | + } |
|
| 1319 | + } |
|
| 1320 | + |
|
| 1321 | + |
|
| 1322 | + /** |
|
| 1323 | + * Sets the submission error message (aka validation error message for this form section and all sub-sections) |
|
| 1324 | + * @param string $form_submission_error_message |
|
| 1325 | + * @param EE_Form_Section_Validatable $form_section unused |
|
| 1326 | + * @throws EE_Error |
|
| 1327 | + */ |
|
| 1328 | + public function set_submission_error_message( |
|
| 1329 | + $form_submission_error_message = '' |
|
| 1330 | + ) { |
|
| 1331 | + $this->_form_submission_error_message = ! empty($form_submission_error_message) |
|
| 1332 | + ? $form_submission_error_message |
|
| 1333 | + : $this->getAllValidationErrorsString(); |
|
| 1334 | + } |
|
| 1335 | + |
|
| 1336 | + |
|
| 1337 | + /** |
|
| 1338 | + * Returns the cached error message. A default value is set for this during _validate(), |
|
| 1339 | + * (called during receive_form_submission) but it can be explicitly set using |
|
| 1340 | + * set_submission_error_message |
|
| 1341 | + * |
|
| 1342 | + * @return string |
|
| 1343 | + */ |
|
| 1344 | + public function submission_error_message() |
|
| 1345 | + { |
|
| 1346 | + return $this->_form_submission_error_message; |
|
| 1347 | + } |
|
| 1348 | + |
|
| 1349 | + |
|
| 1350 | + /** |
|
| 1351 | + * Sets a message to display if the data submitted to the form was valid. |
|
| 1352 | + * @param string $form_submission_success_message |
|
| 1353 | + */ |
|
| 1354 | + public function set_submission_success_message($form_submission_success_message = '') |
|
| 1355 | + { |
|
| 1356 | + $this->_form_submission_success_message = ! empty($form_submission_success_message) |
|
| 1357 | + ? $form_submission_success_message |
|
| 1358 | + : esc_html__('Form submitted successfully', 'event_espresso'); |
|
| 1359 | + } |
|
| 1360 | + |
|
| 1361 | + |
|
| 1362 | + /** |
|
| 1363 | + * Gets a message appropriate for display when the form is correctly submitted |
|
| 1364 | + * @return string |
|
| 1365 | + */ |
|
| 1366 | + public function submission_success_message() |
|
| 1367 | + { |
|
| 1368 | + return $this->_form_submission_success_message; |
|
| 1369 | + } |
|
| 1370 | + |
|
| 1371 | + |
|
| 1372 | + /** |
|
| 1373 | + * Returns the prefix that should be used on child of this form section for |
|
| 1374 | + * their html names. If this form section itself has a parent, prepends ITS |
|
| 1375 | + * prefix onto this form section's prefix. Used primarily by |
|
| 1376 | + * EE_Form_Input_Base::_set_default_html_name_if_empty |
|
| 1377 | + * |
|
| 1378 | + * @return string |
|
| 1379 | + * @throws EE_Error |
|
| 1380 | + */ |
|
| 1381 | + public function html_name_prefix() |
|
| 1382 | + { |
|
| 1383 | + if ($this->parent_section() instanceof EE_Form_Section_Proper) { |
|
| 1384 | + return $this->parent_section()->html_name_prefix() . '[' . $this->name() . ']'; |
|
| 1385 | + } |
|
| 1386 | + return $this->name(); |
|
| 1387 | + } |
|
| 1388 | + |
|
| 1389 | + |
|
| 1390 | + /** |
|
| 1391 | + * Gets the name, but first checks _construct_finalize has been called. If not, |
|
| 1392 | + * calls it (assumes there is no parent and that we want the name to be whatever |
|
| 1393 | + * was set, which is probably nothing, or the classname) |
|
| 1394 | + * |
|
| 1395 | + * @return string |
|
| 1396 | + * @throws EE_Error |
|
| 1397 | + */ |
|
| 1398 | + public function name() |
|
| 1399 | + { |
|
| 1400 | + $this->ensure_construct_finalized_called(); |
|
| 1401 | + return parent::name(); |
|
| 1402 | + } |
|
| 1403 | + |
|
| 1404 | + |
|
| 1405 | + /** |
|
| 1406 | + * @return EE_Form_Section_Proper |
|
| 1407 | + * @throws EE_Error |
|
| 1408 | + */ |
|
| 1409 | + public function parent_section() |
|
| 1410 | + { |
|
| 1411 | + $this->ensure_construct_finalized_called(); |
|
| 1412 | + return parent::parent_section(); |
|
| 1413 | + } |
|
| 1414 | + |
|
| 1415 | + |
|
| 1416 | + /** |
|
| 1417 | + * make sure construction finalized was called, otherwise children might not be ready |
|
| 1418 | + * |
|
| 1419 | + * @return void |
|
| 1420 | + * @throws EE_Error |
|
| 1421 | + */ |
|
| 1422 | + public function ensure_construct_finalized_called() |
|
| 1423 | + { |
|
| 1424 | + if (! $this->_construction_finalized) { |
|
| 1425 | + $this->_construct_finalize($this->_parent_section, $this->_name); |
|
| 1426 | + } |
|
| 1427 | + } |
|
| 1428 | + |
|
| 1429 | + |
|
| 1430 | + /** |
|
| 1431 | + * Checks if any of this form section's inputs, or any of its children's inputs, |
|
| 1432 | + * are in teh form data. If any are found, returns true. Else false |
|
| 1433 | + * |
|
| 1434 | + * @param array $req_data |
|
| 1435 | + * @return boolean |
|
| 1436 | + * @throws EE_Error |
|
| 1437 | + */ |
|
| 1438 | + public function form_data_present_in($req_data = null) |
|
| 1439 | + { |
|
| 1440 | + $req_data = $this->getCachedRequest($req_data); |
|
| 1441 | + foreach ($this->subsections() as $subsection) { |
|
| 1442 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
| 1443 | + if ($subsection->form_data_present_in($req_data)) { |
|
| 1444 | + return true; |
|
| 1445 | + } |
|
| 1446 | + } elseif ($subsection instanceof EE_Form_Section_Proper) { |
|
| 1447 | + if ($subsection->form_data_present_in($req_data)) { |
|
| 1448 | + return true; |
|
| 1449 | + } |
|
| 1450 | + } |
|
| 1451 | + } |
|
| 1452 | + return false; |
|
| 1453 | + } |
|
| 1454 | + |
|
| 1455 | + |
|
| 1456 | + /** |
|
| 1457 | + * Gets validation errors for this form section and subsections |
|
| 1458 | + * Similar to EE_Form_Section_Validatable::get_validation_errors() except this |
|
| 1459 | + * gets the validation errors for ALL subsection |
|
| 1460 | + * |
|
| 1461 | + * @return EE_Validation_Error[] |
|
| 1462 | + * @throws EE_Error |
|
| 1463 | + */ |
|
| 1464 | + public function get_validation_errors_accumulated() |
|
| 1465 | + { |
|
| 1466 | + $validation_errors = $this->get_validation_errors(); |
|
| 1467 | + foreach ($this->get_validatable_subsections() as $subsection) { |
|
| 1468 | + if ($subsection instanceof EE_Form_Section_Proper) { |
|
| 1469 | + $validation_errors_on_this_subsection = $subsection->get_validation_errors_accumulated(); |
|
| 1470 | + } else { |
|
| 1471 | + $validation_errors_on_this_subsection = $subsection->get_validation_errors(); |
|
| 1472 | + } |
|
| 1473 | + if ($validation_errors_on_this_subsection) { |
|
| 1474 | + $validation_errors = array_merge($validation_errors, $validation_errors_on_this_subsection); |
|
| 1475 | + } |
|
| 1476 | + } |
|
| 1477 | + return $validation_errors; |
|
| 1478 | + } |
|
| 1479 | + |
|
| 1480 | + /** |
|
| 1481 | + * Fetch validation errors from children and grandchildren and puts them in a single string. |
|
| 1482 | + * This traverses the form section tree to generate this, but you probably want to instead use |
|
| 1483 | + * get_form_submission_error_message() which is usually this message cached (or a custom validation error message) |
|
| 1484 | + * |
|
| 1485 | + * @return string |
|
| 1486 | + * @since 4.9.59.p |
|
| 1487 | + */ |
|
| 1488 | + protected function getAllValidationErrorsString() |
|
| 1489 | + { |
|
| 1490 | + $submission_error_messages = array(); |
|
| 1491 | + // bad, bad, bad registrant |
|
| 1492 | + foreach ($this->get_validation_errors_accumulated() as $validation_error) { |
|
| 1493 | + if ($validation_error instanceof EE_Validation_Error) { |
|
| 1494 | + $form_section = $validation_error->get_form_section(); |
|
| 1495 | + if ($form_section instanceof EE_Form_Input_Base) { |
|
| 1496 | + $label = $validation_error->get_form_section()->html_label_text(); |
|
| 1497 | + } elseif ($form_section instanceof EE_Form_Section_Validatable) { |
|
| 1498 | + $label = $validation_error->get_form_section()->name(); |
|
| 1499 | + } else { |
|
| 1500 | + $label = esc_html__('Unknown', 'event_espresso'); |
|
| 1501 | + } |
|
| 1502 | + $submission_error_messages[] = sprintf( |
|
| 1503 | + esc_html__('%s : %s', 'event_espresso'), |
|
| 1504 | + $label, |
|
| 1505 | + $validation_error->getMessage() |
|
| 1506 | + ); |
|
| 1507 | + } |
|
| 1508 | + } |
|
| 1509 | + return implode('<br>', $submission_error_messages); |
|
| 1510 | + } |
|
| 1511 | + |
|
| 1512 | + |
|
| 1513 | + /** |
|
| 1514 | + * This isn't just the name of an input, it's a path pointing to an input. The |
|
| 1515 | + * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
| 1516 | + * dot-dot-slash (../) means to ascend into the parent section. |
|
| 1517 | + * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
| 1518 | + * which will be returned. |
|
| 1519 | + * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
| 1520 | + * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
| 1521 | + * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
| 1522 | + * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
| 1523 | + * Etc |
|
| 1524 | + * |
|
| 1525 | + * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
| 1526 | + * @return EE_Form_Section_Base |
|
| 1527 | + * @throws EE_Error |
|
| 1528 | + */ |
|
| 1529 | + public function find_section_from_path($form_section_path) |
|
| 1530 | + { |
|
| 1531 | + // check if we can find the input from purely going straight up the tree |
|
| 1532 | + $input = parent::find_section_from_path($form_section_path); |
|
| 1533 | + if ($input instanceof EE_Form_Section_Base) { |
|
| 1534 | + return $input; |
|
| 1535 | + } |
|
| 1536 | + $next_slash_pos = strpos($form_section_path, '/'); |
|
| 1537 | + if ($next_slash_pos !== false) { |
|
| 1538 | + $child_section_name = substr($form_section_path, 0, $next_slash_pos); |
|
| 1539 | + $subpath = substr($form_section_path, $next_slash_pos + 1); |
|
| 1540 | + } else { |
|
| 1541 | + $child_section_name = $form_section_path; |
|
| 1542 | + $subpath = ''; |
|
| 1543 | + } |
|
| 1544 | + $child_section = $this->get_subsection($child_section_name); |
|
| 1545 | + if ($child_section instanceof EE_Form_Section_Base) { |
|
| 1546 | + return $child_section->find_section_from_path($subpath); |
|
| 1547 | + } |
|
| 1548 | + return null; |
|
| 1549 | + } |
|
| 1550 | 1550 | } |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | $job_response = $job_handler->createJob($this->_job_parameters); |
| 118 | 118 | $this->validateResponse('createJob', $this->_job_id, $job_response); |
| 119 | 119 | $success = $this->_job_parameters->save(); |
| 120 | - if (! $success) { |
|
| 120 | + if ( ! $success) { |
|
| 121 | 121 | throw new BatchRequestException( |
| 122 | 122 | sprintf( |
| 123 | 123 | esc_html__( |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | array $request_data = [] |
| 216 | 216 | ): JobHandlerInterface { |
| 217 | 217 | |
| 218 | - if (! class_exists($classname)) { |
|
| 218 | + if ( ! class_exists($classname)) { |
|
| 219 | 219 | throw new BatchRequestException( |
| 220 | 220 | sprintf( |
| 221 | 221 | esc_html__( |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | ); |
| 228 | 228 | } |
| 229 | 229 | $job_handler = $this->loader->getNew($classname); |
| 230 | - if (! $job_handler instanceof JobHandlerInterface) { |
|
| 230 | + if ( ! $job_handler instanceof JobHandlerInterface) { |
|
| 231 | 231 | throw new BatchRequestException( |
| 232 | 232 | sprintf( |
| 233 | 233 | esc_html__( |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | */ |
| 256 | 256 | protected function _get_error_response(Exception $exception, string $method_name): JobStepResponse |
| 257 | 257 | { |
| 258 | - if (! $this->_job_parameters instanceof JobParameters) { |
|
| 258 | + if ( ! $this->_job_parameters instanceof JobParameters) { |
|
| 259 | 259 | $this->_job_parameters = new JobParameters($this->_job_id, esc_html__('__Unknown__', 'event_espresso'), []); |
| 260 | 260 | } |
| 261 | 261 | $this->_job_parameters->set_status(JobParameters::status_error); |
@@ -267,9 +267,9 @@ discard block |
||
| 267 | 267 | ), |
| 268 | 268 | '<h4>', |
| 269 | 269 | get_class($exception), |
| 270 | - '<code>' . 'BatchRunner::' . $method_name . '()</code>', |
|
| 271 | - '</h4><p>' . $exception->getMessage() . '</p>', |
|
| 272 | - '<pre>' . $exception->getTraceAsString() . '</pre>' |
|
| 270 | + '<code>'.'BatchRunner::'.$method_name.'()</code>', |
|
| 271 | + '</h4><p>'.$exception->getMessage().'</p>', |
|
| 272 | + '<pre>'.$exception->getTraceAsString().'</pre>' |
|
| 273 | 273 | ); |
| 274 | 274 | return new JobStepResponse( |
| 275 | 275 | $this->_job_parameters, |
@@ -287,7 +287,7 @@ discard block |
||
| 287 | 287 | */ |
| 288 | 288 | private function validateResponse(string $function, string $job_id, $job_response) |
| 289 | 289 | { |
| 290 | - if (! $job_response instanceof JobStepResponse) { |
|
| 290 | + if ( ! $job_response instanceof JobStepResponse) { |
|
| 291 | 291 | throw new BatchRequestException( |
| 292 | 292 | sprintf( |
| 293 | 293 | esc_html__( |