← Back
Editing: abstract-custom-field.php
<?php /** * Custom field * * @package SimplePay\Core\Abstracts * @copyright Copyright (c) 2022, Sandhills Development, LLC * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License * @since 3.0.0 */ namespace SimplePay\Core\Abstracts; use SimplePay\Core\Utils; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Class Custom_Field * * @todo Don't use a static methods and use a proper constructor. */ abstract class Custom_Field { /** * Field type. * * @since 3.7.0 * @var string */ protected static $type; /** * Field settings. * * @since 3.7.0 * @var array */ protected static $settings = array(); /** * Form. * * @since 3.7.0 * @var SimplePay\Core\Abstracts\Form */ protected static $form; /** * Static function that we can call from any field and it will in turn call * the correct static function for that field's HTML output. * * @since 3.0.0 * @since 3.7.0 Passes field type as second arugment. * @since 3.7.0 Passes form instance as third argument. * * @param array $settings Field settings. * @param string $type Field type. * @param SimplePay\Core\Abstracts\Form $form Form. * @return string */ public static function html( $settings, $type = null, $form = null ) { self::$settings = $settings; self::$type = $type; self::$form = $form; return static::print_html( $settings, $type, $form ); } /** * Number of times each Payment Form has been output on the current page. * * @since 4.17.7 * * @var array<int|string, int> */ protected static $form_instances = array(); /** * Marks the start of a new Payment Form output on the current page. * * Called once at the start of each form render so subsequent renders of * the same form generate unique field `id` attributes. * * @since 4.17.7 * * @param int|string $form_id Payment Form ID. * @return int The current instance number for the form. */ public static function start_form_instance( $form_id ) { if ( ! isset( self::$form_instances[ $form_id ] ) ) { self::$form_instances[ $form_id ] = 0; } ++self::$form_instances[ $form_id ]; return self::$form_instances[ $form_id ]; } /** * Returns the current instance number for a Payment Form output. * * @since 4.17.7 * * @param int|string $form_id Payment Form ID. * @return int The current instance number for the form. 1 if the form * has not been tracked (e.g. fields rendered outside of a * full form output). */ public static function get_form_instance( $form_id ) { return isset( self::$form_instances[ $form_id ] ) ? self::$form_instances[ $form_id ] : 1; } /** * Returns the suffix that makes a Payment Form output's HTML IDs unique. * * Empty for the first output of a form on a page, so single-instance pages * (the overwhelming majority) render the historical IDs and existing * customizations targeting them keep working. * * @since 4.17.7 * * @param int|string $form_id Payment Form ID. * @return string Instance suffix, or an empty string for the first output. */ public static function get_form_instance_suffix( $form_id ) { $instance = self::get_form_instance( $form_id ); return $instance > 1 ? '-' . $instance : ''; } /** * Creates and returns a stable key that identifies the field. * * Unlike the `id` attribute this does not change when the same form is * output multiple times on a page, so it is safe to use for input names * and metadata keys. * * @example simpay-form-{$form_id}-field-{$field_id} * * @since 4.17.7 * * @return string */ public static function get_field_key() { $id = isset( self::$settings['uid'] ) ? self::$settings['uid'] : ''; return 'simpay-form-' . self::$form->id . '-field-' . $id; } /** * Creates and returns an ID that can be used as an HTML attribute. * * The first output of a form on a page keeps the historical * `simpay-form-{$form_id}-field-{$field_id}` value so existing * customizations targeting it continue to work. Subsequent outputs of * the same form append an instance number to keep IDs unique across the * document (duplicate IDs break label associations and the Date field's * picker). * * @example simpay-form-{$form_id}-field-{$field_id} * @example simpay-form-{$form_id}-field-{$field_id}-{$instance} * * @since 3.9.0 * @since 4.17.7 Appends an instance number when the same form is output * more than once on a page. * * @return string */ public static function get_id_attr() { return self::get_field_key() . self::get_form_instance_suffix( self::$form->id ); } /** * Returns conditional logic data attributes for a field. * * @since 4.17.2 * * @param array $settings Field settings. * @return array<string, string> */ public static function get_conditional_logic_data_attributes( $settings ) { $conditional = isset( $settings['conditional_logic'] ) && is_array( $settings['conditional_logic'] ) ? $settings['conditional_logic'] : array(); $enabled = isset( $conditional['enabled'] ) && 'yes' === $conditional['enabled'] ? 'yes' : 'no'; $action = isset( $conditional['action'] ) ? sanitize_text_field( $conditional['action'] ) : 'show'; $action = in_array( $action, array( 'show', 'hide' ), true ) ? $action : 'show'; $logic = isset( $conditional['logic'] ) ? sanitize_text_field( $conditional['logic'] ) : 'all'; $logic = in_array( $logic, array( 'all', 'any' ), true ) ? $logic : 'all'; $allowed_operators = array( 'equals', 'not_equals', 'contains', 'is_empty', 'is_not_empty', 'greater_than', 'less_than', 'is_checked', 'is_not_checked', 'price_equals', ); $rules = array(); if ( isset( $conditional['rules'] ) && is_array( $conditional['rules'] ) ) { foreach ( $conditional['rules'] as $rule ) { if ( ! is_array( $rule ) ) { continue; } $field = isset( $rule['field'] ) ? absint( $rule['field'] ) : 0; if ( 0 === $field ) { continue; } $operator = isset( $rule['operator'] ) ? sanitize_text_field( $rule['operator'] ) : 'equals'; $operator = in_array( $operator, $allowed_operators, true ) ? $operator : 'equals'; $value = isset( $rule['value'] ) ? sanitize_text_field( $rule['value'] ) : ''; $rules[] = array( 'field' => (string) $field, 'operator' => $operator, 'value' => $value, ); } } $rules_json = wp_json_encode( $rules, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS ); if ( false === $rules_json ) { $rules_json = '[]'; } return array( 'data-conditional-enabled' => $enabled, 'data-conditional-action' => $action, 'data-conditional-logic' => $logic, 'data-conditional-rules' => $rules_json, ); } /** * Returns a filterable default value. * * @since 3.7.0 * * @param string $key Key that stores the default value. * @param mixed $fallback Fallback value. Defaults to empty string. * @param array<string, mixed> $settings Field settings. Overrides current self if set. * @param SimplePay\Core\Abstracts\Form|null $form Form. Overrides current self if set. * @return mixed */ public static function get_default_value( $key = 'default', $fallback = '', $settings = array(), $form = null ) { if ( ! empty( $settings ) ) { self::$settings = $settings; } if ( ! is_null( $form ) ) { self::$form = $form; } $id = isset( self::$settings['uid'] ) ? self::$settings['uid'] : ''; $default = isset( self::$settings[ $key ] ) ? self::$settings[ $key ] : $fallback; $default = self::get_dynamic_default_values( $default ); /** * Filters the default value used on a custom field. * * @since 3.7.0 * * @param mixed $default Default value. Empty string if nothing is set. * @param string $id Field ID. * @param SimplePay\Core\Abstracts\Form * @param string $type Field type. */ $default = apply_filters( 'simpay_form_field_default_value', $default, $id, self::$form, self::$type ); $default = simpay_get_filtered( 'field_' . $id . '_default_value', $default, self::$form->id ); return $default; } /** * Returns the field's label. * * @since 3.9.0 * * @param string $label_setting_key Setting key to look for the label. * @param string $label_fallback Fallback label value. * @return string */ public static function get_label( $label_setting_key = 'label', $label_fallback = '' ) { $id = self::get_id_attr(); $label = isset( self::$settings[ $label_setting_key ] ) ? self::$settings[ $label_setting_key ] : $label_fallback; $classes = array(); if ( empty( $label ) ) { $label = self::$settings['type']; $classes[] = 'screen-reader-text'; } ob_start(); ?> <div class="simpay-<?php echo esc_attr( self::$settings['type'] ); ?>-label simpay-label-wrap"> <label for="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"> <?php echo wp_kses_post( $label ); ?> <?php switch ( self::$type ) : case 'email': case 'radio': case 'card': case 'address': case 'custom_amount': case 'plan_select': $required = true; break; default: $required = isset( self::$settings['required'] ); endswitch; if ( true === $required ) : echo ''; // WPCS: XSS okay. else : echo wp_kses_post( self::get_optional_indicator() ); endif; ?> </label> </div> <?php return ob_get_clean(); } /** * Returns an indicator if the field is optional. * * @since 3.9.0 * * @return string Required indicator, or empty string if the field is not optional. */ public static function get_optional_indicator() { $optional_indicator = '<span class="simpay-optional-indicator">' . esc_html__( ' (optional)', 'stripe' ) . '</span>'; /** * Filters the indicator for optional fields. * * @since 3.9.0 * * @param string $indicator Required indicator. */ $optional_indicator = apply_filters( 'simpay_form_field_optional_indicator', $optional_indicator ); return $optional_indicator; } /** * Replaces dynamic value placeholders with their actual values. * * @since 4.7.6 * * @param string $default Default value. * @return string */ private static function get_dynamic_default_values( $default ) { // Replace any Smart Tags in the default value with their dynamic values. $tags = self::get_default_value_smart_tags(); foreach ( $tags as $tag ) { // Standard {form-id}, {form-title}, etc. Smart Tags. if ( strpos( $default, $tag ) !== false ) { $default = str_replace( $tag, self::get_default_value_smart_tag_value( $tag ), $default ); } } // Replace all instances of {query var=""} Smart Tag with the query var value. if ( strpos( $default, '{query var="' ) !== false ) { $pattern = '/\{query var="([^"]+)"\}/'; $matches = array(); if ( preg_match_all( $pattern, $default, $matches ) ) { foreach ( $matches[0] as $match ) { $default = str_replace( $match, self::get_default_value_smart_tag_value( $match ), $default ); } } } // Force an additional escape _just in case_ because we are accepting user input. // It should still be late-escaped on output. return esc_attr( $default ); } /** * Returns an array of dynamic value tags that may found in the default value. * * @since 4.7.6 * * @param string $default_value Default value. * @return array<string> */ private static function get_default_value_smart_tags() { return array( '{query var=""}', '{form-id}', '{form-title}', '{form-description}', '{page-id}', '{page-title}', '{page-url}', '{user-id}', '{user-email}', '{user-first-name}', '{user-last-name}', '{user-ip}', ); } /** * Returns the value of a dynamic value tag. * * @since 4.7.6 * * @param string $smart_tag Smart Tag. * @return string */ private static function get_default_value_smart_tag_value( $smart_tag ) { $is_query = strpos( $smart_tag, 'query' ) !== false; if ( $is_query ) { $pattern = '/\{query var="([^"]+)"\}/'; $matches = array(); if ( preg_match( $pattern, $smart_tag, $matches ) ) { $query_var = esc_html( $matches[1] ); if ( isset( $_GET[ $query_var ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return esc_attr( wp_unslash( $_GET[ $query_var ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.NonceVerification.Recommended } } return ''; } else { switch ( $smart_tag ) { case '{form-id}': return esc_attr( self::$form->id ); case '{form-title}': return esc_attr( self::$form->company_name ); case '{form-description}': return esc_attr( self::$form->item_description ); case '{page-id}': return esc_attr( get_the_ID() ); case '{page-title}': return esc_attr( get_the_title() ); case '{page-url}': return esc_attr( get_permalink() ); case '{user-id}': return esc_attr( get_current_user_id() ); case '{user-email}': return esc_attr( wp_get_current_user()->user_email ); case '{user-first-name}': return esc_attr( wp_get_current_user()->first_name ); case '{user-last-name}': return esc_attr( wp_get_current_user()->last_name ); case '{user-ip}': return esc_attr( Utils\get_current_ip_address() ); } } return ''; } }
Save File
Cancel