@@ -36,7 +36,7 @@ |
||
36 | 36 | add_meta_box( 'wpinv-invoice-payment-form-details', __( 'Payment Form Details', 'invoicing' ), 'WPInv_Meta_Box_Payment_Form::output_details', 'wpi_invoice', 'side', 'high' ); |
37 | 37 | } |
38 | 38 | |
39 | - remove_meta_box('wpseo_meta', 'wpi_invoice', 'normal'); |
|
39 | + remove_meta_box('wpseo_meta', 'wpi_invoice', 'normal'); |
|
40 | 40 | } |
41 | 41 | add_action( 'add_meta_boxes', 'wpinv_add_meta_boxes', 30, 2 ); |
42 | 42 |
@@ -708,13 +708,13 @@ discard block |
||
708 | 708 | * @param mixed $value Value. |
709 | 709 | */ |
710 | 710 | function getpaid_maybe_define_constant( $name, $value ) { |
711 | - if ( ! defined( $name ) ) { |
|
712 | - define( $name, $value ); |
|
713 | - } |
|
711 | + if ( ! defined( $name ) ) { |
|
712 | + define( $name, $value ); |
|
713 | + } |
|
714 | 714 | } |
715 | 715 | |
716 | 716 | function wpinv_get_php_arg_separator_output() { |
717 | - return ini_get( 'arg_separator.output' ); |
|
717 | + return ini_get( 'arg_separator.output' ); |
|
718 | 718 | } |
719 | 719 | |
720 | 720 | function wpinv_rgb_from_hex( $color ) { |
@@ -1058,11 +1058,11 @@ discard block |
||
1058 | 1058 | * @return array Sanitized array of values. |
1059 | 1059 | */ |
1060 | 1060 | function wpinv_parse_list( $list ) { |
1061 | - if ( ! is_array( $list ) ) { |
|
1062 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
1063 | - } |
|
1061 | + if ( ! is_array( $list ) ) { |
|
1062 | + return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
1063 | + } |
|
1064 | 1064 | |
1065 | - return $list; |
|
1065 | + return $list; |
|
1066 | 1066 | } |
1067 | 1067 | |
1068 | 1068 | /** |
@@ -1082,9 +1082,9 @@ discard block |
||
1082 | 1082 | } |
1083 | 1083 | |
1084 | 1084 | $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" ); |
1085 | - wp_cache_set( "wpinv-$key", $data, 'wpinv' ); |
|
1085 | + wp_cache_set( "wpinv-$key", $data, 'wpinv' ); |
|
1086 | 1086 | |
1087 | - return $data; |
|
1087 | + return $data; |
|
1088 | 1088 | } |
1089 | 1089 | |
1090 | 1090 | /** |
@@ -1113,17 +1113,17 @@ discard block |
||
1113 | 1113 | */ |
1114 | 1114 | function wpinv_clean( $var ) { |
1115 | 1115 | |
1116 | - if ( is_array( $var ) ) { |
|
1117 | - return array_map( 'wpinv_clean', $var ); |
|
1116 | + if ( is_array( $var ) ) { |
|
1117 | + return array_map( 'wpinv_clean', $var ); |
|
1118 | 1118 | } |
1119 | 1119 | |
1120 | 1120 | if ( is_object( $var ) ) { |
1121 | - $object_vars = get_object_vars( $var ); |
|
1122 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
1123 | - $var->$property_name = wpinv_clean( $property_value ); |
|
1121 | + $object_vars = get_object_vars( $var ); |
|
1122 | + foreach ( $object_vars as $property_name => $property_value ) { |
|
1123 | + $var->$property_name = wpinv_clean( $property_value ); |
|
1124 | 1124 | } |
1125 | 1125 | return $var; |
1126 | - } |
|
1126 | + } |
|
1127 | 1127 | |
1128 | 1128 | return is_string( $var ) ? sanitize_text_field( $var ) : $var; |
1129 | 1129 | } |
1130 | 1130 | \ No newline at end of file |
@@ -13,9 +13,9 @@ discard block |
||
13 | 13 | ) ); |
14 | 14 | echo '<div class="' . implode( ' ', $classes ) . '">'; |
15 | 15 | // Loop error codes and display errors |
16 | - foreach ( $errors as $error_id => $error ) { |
|
16 | + foreach ( $errors as $error_id => $error ) { |
|
17 | 17 | echo '<p class="wpinv_error" id="wpinv_error_' . $error_id . '"><strong>' . __( 'Error', 'invoicing' ) . '</strong>: ' . $error . '</p>'; |
18 | - } |
|
18 | + } |
|
19 | 19 | echo '</div>'; |
20 | 20 | wpinv_clear_errors(); |
21 | 21 | } |
@@ -79,13 +79,13 @@ discard block |
||
79 | 79 | */ |
80 | 80 | function getpaid_doing_it_wrong( $function, $message, $version ) { |
81 | 81 | |
82 | - $message .= ' Backtrace: ' . wp_debug_backtrace_summary(); |
|
82 | + $message .= ' Backtrace: ' . wp_debug_backtrace_summary(); |
|
83 | 83 | |
84 | - if ( is_ajax() || defined( 'REST_REQUEST' ) ) { |
|
85 | - do_action( 'doing_it_wrong_run', $function, $message, $version ); |
|
86 | - error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); |
|
87 | - } else { |
|
88 | - _doing_it_wrong( $function, $message, $version ); |
|
89 | - } |
|
84 | + if ( is_ajax() || defined( 'REST_REQUEST' ) ) { |
|
85 | + do_action( 'doing_it_wrong_run', $function, $message, $version ); |
|
86 | + error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); |
|
87 | + } else { |
|
88 | + _doing_it_wrong( $function, $message, $version ); |
|
89 | + } |
|
90 | 90 | |
91 | 91 | } |
@@ -11,121 +11,121 @@ |
||
11 | 11 | */ |
12 | 12 | class GetPaid_Cache_Helper { |
13 | 13 | |
14 | - /** |
|
15 | - * Transients to delete on shutdown. |
|
16 | - * |
|
17 | - * @var array Array of transient keys. |
|
18 | - */ |
|
19 | - private static $delete_transients = array(); |
|
20 | - |
|
21 | - /** |
|
22 | - * Hook in methods. |
|
23 | - */ |
|
24 | - public static function init() { |
|
25 | - add_action( 'shutdown', array( __CLASS__, 'delete_transients_on_shutdown' ), 10 ); |
|
26 | - add_action( 'wp', array( __CLASS__, 'prevent_caching' ) ); |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Add a transient to delete on shutdown. |
|
31 | - * |
|
32 | - * @since 1.0.19 |
|
33 | - * @param string|array $keys Transient key or keys. |
|
34 | - */ |
|
35 | - public static function queue_delete_transient( $keys ) { |
|
36 | - self::$delete_transients = array_unique( array_merge( is_array( $keys ) ? $keys : array( $keys ), self::$delete_transients ) ); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Transients that don't need to be cleaned right away can be deleted on shutdown to avoid repetition. |
|
41 | - * |
|
42 | - * @since 1.0.19 |
|
43 | - */ |
|
44 | - public static function delete_transients_on_shutdown() { |
|
45 | - if ( self::$delete_transients ) { |
|
46 | - foreach ( self::$delete_transients as $key ) { |
|
47 | - delete_transient( $key ); |
|
48 | - } |
|
49 | - self::$delete_transients = array(); |
|
50 | - } |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once. |
|
55 | - * |
|
56 | - * @param string $group Group of cache to get. |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public static function get_cache_prefix( $group ) { |
|
60 | - // Get cache key. |
|
61 | - $prefix = wp_cache_get( 'getpaid_' . $group . '_cache_prefix', $group ); |
|
62 | - |
|
63 | - if ( false === $prefix ) { |
|
64 | - $prefix = microtime(); |
|
65 | - wp_cache_set( 'getpaid_' . $group . '_cache_prefix', $prefix, $group ); |
|
66 | - } |
|
67 | - |
|
68 | - return 'getpaid_cache_' . $prefix . '_'; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Invalidate cache group. |
|
73 | - * |
|
74 | - * @param string $group Group of cache to clear. |
|
75 | - * @since 1.0.19 |
|
76 | - */ |
|
77 | - public static function invalidate_cache_group( $group ) { |
|
78 | - wp_cache_set( 'getpaid_' . $group . '_cache_prefix', microtime(), $group ); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Prevent caching on certain pages |
|
83 | - */ |
|
84 | - public static function prevent_caching() { |
|
85 | - if ( ! is_blog_installed() ) { |
|
86 | - return; |
|
87 | - } |
|
88 | - |
|
89 | - if ( wpinv_is_checkout() || wpinv_is_success_page() || wpinv_is_invoice_history_page() || wpinv_is_subscriptions_history_page() ) { |
|
90 | - self::set_nocache_constants(); |
|
91 | - nocache_headers(); |
|
92 | - } |
|
93 | - |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get transient version. |
|
98 | - * |
|
99 | - * |
|
100 | - * @param string $group Name for the group of transients we need to invalidate. |
|
101 | - * @param boolean $refresh true to force a new version. |
|
102 | - * @return string transient version based on time(), 10 digits. |
|
103 | - */ |
|
104 | - public static function get_transient_version( $group, $refresh = false ) { |
|
105 | - $transient_name = $group . '-transient-version'; |
|
106 | - $transient_value = get_transient( $transient_name ); |
|
107 | - |
|
108 | - if ( false === $transient_value || true === $refresh ) { |
|
109 | - $transient_value = (string) time(); |
|
110 | - |
|
111 | - set_transient( $transient_name, $transient_value ); |
|
112 | - } |
|
113 | - |
|
114 | - return $transient_value; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Set constants to prevent caching by some plugins. |
|
119 | - * |
|
120 | - * @param mixed $return Value to return. Previously hooked into a filter. |
|
121 | - * @return mixed |
|
122 | - */ |
|
123 | - public static function set_nocache_constants( $return = true ) { |
|
124 | - getpaid_maybe_define_constant( 'DONOTCACHEPAGE', true ); |
|
125 | - getpaid_maybe_define_constant( 'DONOTCACHEOBJECT', true ); |
|
126 | - getpaid_maybe_define_constant( 'DONOTCACHEDB', true ); |
|
127 | - return $return; |
|
128 | - } |
|
14 | + /** |
|
15 | + * Transients to delete on shutdown. |
|
16 | + * |
|
17 | + * @var array Array of transient keys. |
|
18 | + */ |
|
19 | + private static $delete_transients = array(); |
|
20 | + |
|
21 | + /** |
|
22 | + * Hook in methods. |
|
23 | + */ |
|
24 | + public static function init() { |
|
25 | + add_action( 'shutdown', array( __CLASS__, 'delete_transients_on_shutdown' ), 10 ); |
|
26 | + add_action( 'wp', array( __CLASS__, 'prevent_caching' ) ); |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Add a transient to delete on shutdown. |
|
31 | + * |
|
32 | + * @since 1.0.19 |
|
33 | + * @param string|array $keys Transient key or keys. |
|
34 | + */ |
|
35 | + public static function queue_delete_transient( $keys ) { |
|
36 | + self::$delete_transients = array_unique( array_merge( is_array( $keys ) ? $keys : array( $keys ), self::$delete_transients ) ); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Transients that don't need to be cleaned right away can be deleted on shutdown to avoid repetition. |
|
41 | + * |
|
42 | + * @since 1.0.19 |
|
43 | + */ |
|
44 | + public static function delete_transients_on_shutdown() { |
|
45 | + if ( self::$delete_transients ) { |
|
46 | + foreach ( self::$delete_transients as $key ) { |
|
47 | + delete_transient( $key ); |
|
48 | + } |
|
49 | + self::$delete_transients = array(); |
|
50 | + } |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once. |
|
55 | + * |
|
56 | + * @param string $group Group of cache to get. |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public static function get_cache_prefix( $group ) { |
|
60 | + // Get cache key. |
|
61 | + $prefix = wp_cache_get( 'getpaid_' . $group . '_cache_prefix', $group ); |
|
62 | + |
|
63 | + if ( false === $prefix ) { |
|
64 | + $prefix = microtime(); |
|
65 | + wp_cache_set( 'getpaid_' . $group . '_cache_prefix', $prefix, $group ); |
|
66 | + } |
|
67 | + |
|
68 | + return 'getpaid_cache_' . $prefix . '_'; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Invalidate cache group. |
|
73 | + * |
|
74 | + * @param string $group Group of cache to clear. |
|
75 | + * @since 1.0.19 |
|
76 | + */ |
|
77 | + public static function invalidate_cache_group( $group ) { |
|
78 | + wp_cache_set( 'getpaid_' . $group . '_cache_prefix', microtime(), $group ); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Prevent caching on certain pages |
|
83 | + */ |
|
84 | + public static function prevent_caching() { |
|
85 | + if ( ! is_blog_installed() ) { |
|
86 | + return; |
|
87 | + } |
|
88 | + |
|
89 | + if ( wpinv_is_checkout() || wpinv_is_success_page() || wpinv_is_invoice_history_page() || wpinv_is_subscriptions_history_page() ) { |
|
90 | + self::set_nocache_constants(); |
|
91 | + nocache_headers(); |
|
92 | + } |
|
93 | + |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get transient version. |
|
98 | + * |
|
99 | + * |
|
100 | + * @param string $group Name for the group of transients we need to invalidate. |
|
101 | + * @param boolean $refresh true to force a new version. |
|
102 | + * @return string transient version based on time(), 10 digits. |
|
103 | + */ |
|
104 | + public static function get_transient_version( $group, $refresh = false ) { |
|
105 | + $transient_name = $group . '-transient-version'; |
|
106 | + $transient_value = get_transient( $transient_name ); |
|
107 | + |
|
108 | + if ( false === $transient_value || true === $refresh ) { |
|
109 | + $transient_value = (string) time(); |
|
110 | + |
|
111 | + set_transient( $transient_name, $transient_value ); |
|
112 | + } |
|
113 | + |
|
114 | + return $transient_value; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Set constants to prevent caching by some plugins. |
|
119 | + * |
|
120 | + * @param mixed $return Value to return. Previously hooked into a filter. |
|
121 | + * @return mixed |
|
122 | + */ |
|
123 | + public static function set_nocache_constants( $return = true ) { |
|
124 | + getpaid_maybe_define_constant( 'DONOTCACHEPAGE', true ); |
|
125 | + getpaid_maybe_define_constant( 'DONOTCACHEOBJECT', true ); |
|
126 | + getpaid_maybe_define_constant( 'DONOTCACHEDB', true ); |
|
127 | + return $return; |
|
128 | + } |
|
129 | 129 | |
130 | 130 | } |
131 | 131 |
@@ -12,217 +12,217 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Data_Store_WP { |
14 | 14 | |
15 | - /** |
|
16 | - * Meta type. This should match up with |
|
17 | - * the types available at https://developer.wordpress.org/reference/functions/add_metadata/. |
|
18 | - * WP defines 'post', 'user', 'comment', and 'term'. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $meta_type = 'post'; |
|
23 | - |
|
24 | - /** |
|
25 | - * This only needs set if you are using a custom metadata type (for example payment tokens. |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $object_id_field_for_meta = ''; |
|
30 | - |
|
31 | - /** |
|
32 | - * Data stored in meta keys, but not considered "meta" for an object. |
|
33 | - * |
|
34 | - * @since 1.0.19 |
|
35 | - * |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $internal_meta_keys = array(); |
|
39 | - |
|
40 | - /** |
|
41 | - * Meta data which should exist in the DB, even if empty. |
|
42 | - * |
|
43 | - * @since 1.0.19 |
|
44 | - * |
|
45 | - * @var array |
|
46 | - */ |
|
47 | - protected $must_exist_meta_keys = array(); |
|
48 | - |
|
49 | - /** |
|
50 | - * Returns an array of meta for an object. |
|
51 | - * |
|
52 | - * @since 1.0.19 |
|
53 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
54 | - * @return array |
|
55 | - */ |
|
56 | - public function read_meta( &$object ) { |
|
57 | - global $wpdb; |
|
58 | - $db_info = $this->get_db_info(); |
|
59 | - $raw_meta_data = $wpdb->get_results( |
|
60 | - $wpdb->prepare( |
|
61 | - "SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value |
|
15 | + /** |
|
16 | + * Meta type. This should match up with |
|
17 | + * the types available at https://developer.wordpress.org/reference/functions/add_metadata/. |
|
18 | + * WP defines 'post', 'user', 'comment', and 'term'. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $meta_type = 'post'; |
|
23 | + |
|
24 | + /** |
|
25 | + * This only needs set if you are using a custom metadata type (for example payment tokens. |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $object_id_field_for_meta = ''; |
|
30 | + |
|
31 | + /** |
|
32 | + * Data stored in meta keys, but not considered "meta" for an object. |
|
33 | + * |
|
34 | + * @since 1.0.19 |
|
35 | + * |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $internal_meta_keys = array(); |
|
39 | + |
|
40 | + /** |
|
41 | + * Meta data which should exist in the DB, even if empty. |
|
42 | + * |
|
43 | + * @since 1.0.19 |
|
44 | + * |
|
45 | + * @var array |
|
46 | + */ |
|
47 | + protected $must_exist_meta_keys = array(); |
|
48 | + |
|
49 | + /** |
|
50 | + * Returns an array of meta for an object. |
|
51 | + * |
|
52 | + * @since 1.0.19 |
|
53 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
54 | + * @return array |
|
55 | + */ |
|
56 | + public function read_meta( &$object ) { |
|
57 | + global $wpdb; |
|
58 | + $db_info = $this->get_db_info(); |
|
59 | + $raw_meta_data = $wpdb->get_results( |
|
60 | + $wpdb->prepare( |
|
61 | + "SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value |
|
62 | 62 | FROM {$db_info['table']} |
63 | 63 | WHERE {$db_info['object_id_field']} = %d |
64 | 64 | ORDER BY {$db_info['meta_id_field']}", |
65 | - $object->get_id() |
|
66 | - ) |
|
67 | - ); |
|
68 | - |
|
69 | - $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
70 | - $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
71 | - return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Deletes meta based on meta ID. |
|
76 | - * |
|
77 | - * @since 1.0.19 |
|
78 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
79 | - * @param stdClass $meta (containing at least ->id). |
|
80 | - */ |
|
81 | - public function delete_meta( &$object, $meta ) { |
|
82 | - delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Add new piece of meta. |
|
87 | - * |
|
88 | - * @since 1.0.19 |
|
89 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
90 | - * @param stdClass $meta (containing ->key and ->value). |
|
91 | - * @return int meta ID |
|
92 | - */ |
|
93 | - public function add_meta( &$object, $meta ) { |
|
94 | - return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Update meta. |
|
99 | - * |
|
100 | - * @since 1.0.19 |
|
101 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
102 | - * @param stdClass $meta (containing ->id, ->key and ->value). |
|
103 | - */ |
|
104 | - public function update_meta( &$object, $meta ) { |
|
105 | - update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Table structure is slightly different between meta types, this function will return what we need to know. |
|
110 | - * |
|
111 | - * @since 1.0.19 |
|
112 | - * @return array Array elements: table, object_id_field, meta_id_field |
|
113 | - */ |
|
114 | - protected function get_db_info() { |
|
115 | - global $wpdb; |
|
116 | - |
|
117 | - $meta_id_field = 'meta_id'; // for some reason users calls this umeta_id so we need to track this as well. |
|
118 | - $table = $wpdb->prefix; |
|
119 | - |
|
120 | - // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
|
121 | - if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
122 | - $table .= 'getpaid_'; |
|
123 | - } |
|
124 | - |
|
125 | - $table .= $this->meta_type . 'meta'; |
|
126 | - $object_id_field = $this->meta_type . '_id'; |
|
127 | - |
|
128 | - // Figure out our field names. |
|
129 | - if ( 'user' === $this->meta_type ) { |
|
130 | - $meta_id_field = 'umeta_id'; |
|
131 | - $table = $wpdb->usermeta; |
|
132 | - } |
|
133 | - |
|
134 | - if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
135 | - $object_id_field = $this->object_id_field_for_meta; |
|
136 | - } |
|
137 | - |
|
138 | - return array( |
|
139 | - 'table' => $table, |
|
140 | - 'object_id_field' => $object_id_field, |
|
141 | - 'meta_id_field' => $meta_id_field, |
|
142 | - ); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Internal meta keys we don't want exposed as part of meta_data. This is in |
|
147 | - * addition to all data props with _ prefix. |
|
148 | - * |
|
149 | - * @since 1.0.19 |
|
150 | - * |
|
151 | - * @param string $key Prefix to be added to meta keys. |
|
152 | - * @return string |
|
153 | - */ |
|
154 | - protected function prefix_key( $key ) { |
|
155 | - return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Callback to remove unwanted meta data. |
|
160 | - * |
|
161 | - * @param object $meta Meta object to check if it should be excluded or not. |
|
162 | - * @return bool |
|
163 | - */ |
|
164 | - protected function exclude_internal_meta_keys( $meta ) { |
|
165 | - return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Gets a list of props and meta keys that need updated based on change state |
|
170 | - * or if they are present in the database or not. |
|
171 | - * |
|
172 | - * @param GetPaid_Data $object The GetPaid_Data object. |
|
173 | - * @param array $meta_key_to_props A mapping of meta keys => prop names. |
|
174 | - * @param string $meta_type The internal WP meta type (post, user, etc). |
|
175 | - * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
|
176 | - */ |
|
177 | - protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
178 | - $props_to_update = array(); |
|
179 | - $changed_props = $object->get_changes(); |
|
180 | - |
|
181 | - // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
182 | - foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
183 | - if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
184 | - $props_to_update[ $meta_key ] = $prop; |
|
185 | - } |
|
186 | - } |
|
187 | - |
|
188 | - return $props_to_update; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Update meta data in, or delete it from, the database. |
|
193 | - * |
|
194 | - * Avoids storing meta when it's either an empty string or empty array or null. |
|
195 | - * Other empty values such as numeric 0 should still be stored. |
|
196 | - * Data-stores can force meta to exist using `must_exist_meta_keys`. |
|
197 | - * |
|
198 | - * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist. |
|
199 | - * |
|
200 | - * @param GetPaid_Data $object The GetPaid_Data object. |
|
201 | - * @param string $meta_key Meta key to update. |
|
202 | - * @param mixed $meta_value Value to save. |
|
203 | - * |
|
204 | - * @since 1.0.19 Added to prevent empty meta being stored unless required. |
|
205 | - * |
|
206 | - * @return bool True if updated/deleted. |
|
207 | - */ |
|
208 | - protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
209 | - if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
210 | - $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
211 | - } else { |
|
212 | - $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
213 | - } |
|
214 | - |
|
215 | - return (bool) $updated; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Return list of internal meta keys. |
|
220 | - * |
|
221 | - * @since 1.0.19 |
|
222 | - * @return array |
|
223 | - */ |
|
224 | - public function get_internal_meta_keys() { |
|
225 | - return $this->internal_meta_keys; |
|
226 | - } |
|
65 | + $object->get_id() |
|
66 | + ) |
|
67 | + ); |
|
68 | + |
|
69 | + $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
70 | + $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
71 | + return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Deletes meta based on meta ID. |
|
76 | + * |
|
77 | + * @since 1.0.19 |
|
78 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
79 | + * @param stdClass $meta (containing at least ->id). |
|
80 | + */ |
|
81 | + public function delete_meta( &$object, $meta ) { |
|
82 | + delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Add new piece of meta. |
|
87 | + * |
|
88 | + * @since 1.0.19 |
|
89 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
90 | + * @param stdClass $meta (containing ->key and ->value). |
|
91 | + * @return int meta ID |
|
92 | + */ |
|
93 | + public function add_meta( &$object, $meta ) { |
|
94 | + return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Update meta. |
|
99 | + * |
|
100 | + * @since 1.0.19 |
|
101 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
102 | + * @param stdClass $meta (containing ->id, ->key and ->value). |
|
103 | + */ |
|
104 | + public function update_meta( &$object, $meta ) { |
|
105 | + update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Table structure is slightly different between meta types, this function will return what we need to know. |
|
110 | + * |
|
111 | + * @since 1.0.19 |
|
112 | + * @return array Array elements: table, object_id_field, meta_id_field |
|
113 | + */ |
|
114 | + protected function get_db_info() { |
|
115 | + global $wpdb; |
|
116 | + |
|
117 | + $meta_id_field = 'meta_id'; // for some reason users calls this umeta_id so we need to track this as well. |
|
118 | + $table = $wpdb->prefix; |
|
119 | + |
|
120 | + // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
|
121 | + if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
122 | + $table .= 'getpaid_'; |
|
123 | + } |
|
124 | + |
|
125 | + $table .= $this->meta_type . 'meta'; |
|
126 | + $object_id_field = $this->meta_type . '_id'; |
|
127 | + |
|
128 | + // Figure out our field names. |
|
129 | + if ( 'user' === $this->meta_type ) { |
|
130 | + $meta_id_field = 'umeta_id'; |
|
131 | + $table = $wpdb->usermeta; |
|
132 | + } |
|
133 | + |
|
134 | + if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
135 | + $object_id_field = $this->object_id_field_for_meta; |
|
136 | + } |
|
137 | + |
|
138 | + return array( |
|
139 | + 'table' => $table, |
|
140 | + 'object_id_field' => $object_id_field, |
|
141 | + 'meta_id_field' => $meta_id_field, |
|
142 | + ); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Internal meta keys we don't want exposed as part of meta_data. This is in |
|
147 | + * addition to all data props with _ prefix. |
|
148 | + * |
|
149 | + * @since 1.0.19 |
|
150 | + * |
|
151 | + * @param string $key Prefix to be added to meta keys. |
|
152 | + * @return string |
|
153 | + */ |
|
154 | + protected function prefix_key( $key ) { |
|
155 | + return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Callback to remove unwanted meta data. |
|
160 | + * |
|
161 | + * @param object $meta Meta object to check if it should be excluded or not. |
|
162 | + * @return bool |
|
163 | + */ |
|
164 | + protected function exclude_internal_meta_keys( $meta ) { |
|
165 | + return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Gets a list of props and meta keys that need updated based on change state |
|
170 | + * or if they are present in the database or not. |
|
171 | + * |
|
172 | + * @param GetPaid_Data $object The GetPaid_Data object. |
|
173 | + * @param array $meta_key_to_props A mapping of meta keys => prop names. |
|
174 | + * @param string $meta_type The internal WP meta type (post, user, etc). |
|
175 | + * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
|
176 | + */ |
|
177 | + protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
178 | + $props_to_update = array(); |
|
179 | + $changed_props = $object->get_changes(); |
|
180 | + |
|
181 | + // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
182 | + foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
183 | + if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
184 | + $props_to_update[ $meta_key ] = $prop; |
|
185 | + } |
|
186 | + } |
|
187 | + |
|
188 | + return $props_to_update; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Update meta data in, or delete it from, the database. |
|
193 | + * |
|
194 | + * Avoids storing meta when it's either an empty string or empty array or null. |
|
195 | + * Other empty values such as numeric 0 should still be stored. |
|
196 | + * Data-stores can force meta to exist using `must_exist_meta_keys`. |
|
197 | + * |
|
198 | + * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist. |
|
199 | + * |
|
200 | + * @param GetPaid_Data $object The GetPaid_Data object. |
|
201 | + * @param string $meta_key Meta key to update. |
|
202 | + * @param mixed $meta_value Value to save. |
|
203 | + * |
|
204 | + * @since 1.0.19 Added to prevent empty meta being stored unless required. |
|
205 | + * |
|
206 | + * @return bool True if updated/deleted. |
|
207 | + */ |
|
208 | + protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
209 | + if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
210 | + $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
211 | + } else { |
|
212 | + $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
213 | + } |
|
214 | + |
|
215 | + return (bool) $updated; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Return list of internal meta keys. |
|
220 | + * |
|
221 | + * @since 1.0.19 |
|
222 | + * @return array |
|
223 | + */ |
|
224 | + public function get_internal_meta_keys() { |
|
225 | + return $this->internal_meta_keys; |
|
226 | + } |
|
227 | 227 | |
228 | 228 | } |
@@ -15,104 +15,104 @@ |
||
15 | 15 | */ |
16 | 16 | class GetPaid_Meta_Data implements JsonSerializable { |
17 | 17 | |
18 | - /** |
|
19 | - * Current data for metadata |
|
20 | - * |
|
21 | - * @since 1.0.19 |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $current_data; |
|
18 | + /** |
|
19 | + * Current data for metadata |
|
20 | + * |
|
21 | + * @since 1.0.19 |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $current_data; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Metadata data |
|
28 | - * |
|
29 | - * @since 1.0.19 |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $data; |
|
26 | + /** |
|
27 | + * Metadata data |
|
28 | + * |
|
29 | + * @since 1.0.19 |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $data; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Constructor. |
|
36 | - * |
|
37 | - * @param array $meta Data to wrap behind this function. |
|
38 | - */ |
|
39 | - public function __construct( $meta = array() ) { |
|
40 | - $this->current_data = $meta; |
|
41 | - $this->apply_changes(); |
|
42 | - } |
|
34 | + /** |
|
35 | + * Constructor. |
|
36 | + * |
|
37 | + * @param array $meta Data to wrap behind this function. |
|
38 | + */ |
|
39 | + public function __construct( $meta = array() ) { |
|
40 | + $this->current_data = $meta; |
|
41 | + $this->apply_changes(); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * When converted to JSON. |
|
46 | - * |
|
47 | - * @return object|array |
|
48 | - */ |
|
49 | - public function jsonSerialize() { |
|
50 | - return $this->get_data(); |
|
51 | - } |
|
44 | + /** |
|
45 | + * When converted to JSON. |
|
46 | + * |
|
47 | + * @return object|array |
|
48 | + */ |
|
49 | + public function jsonSerialize() { |
|
50 | + return $this->get_data(); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Merge changes with data and clear. |
|
55 | - */ |
|
56 | - public function apply_changes() { |
|
57 | - $this->data = $this->current_data; |
|
58 | - } |
|
53 | + /** |
|
54 | + * Merge changes with data and clear. |
|
55 | + */ |
|
56 | + public function apply_changes() { |
|
57 | + $this->data = $this->current_data; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Creates or updates a property in the metadata object. |
|
62 | - * |
|
63 | - * @param string $key Key to set. |
|
64 | - * @param mixed $value Value to set. |
|
65 | - */ |
|
66 | - public function __set( $key, $value ) { |
|
67 | - $this->current_data[ $key ] = $value; |
|
68 | - } |
|
60 | + /** |
|
61 | + * Creates or updates a property in the metadata object. |
|
62 | + * |
|
63 | + * @param string $key Key to set. |
|
64 | + * @param mixed $value Value to set. |
|
65 | + */ |
|
66 | + public function __set( $key, $value ) { |
|
67 | + $this->current_data[ $key ] = $value; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Checks if a given key exists in our data. This is called internally |
|
72 | - * by `empty` and `isset`. |
|
73 | - * |
|
74 | - * @param string $key Key to check if set. |
|
75 | - * |
|
76 | - * @return bool |
|
77 | - */ |
|
78 | - public function __isset( $key ) { |
|
79 | - return array_key_exists( $key, $this->current_data ); |
|
80 | - } |
|
70 | + /** |
|
71 | + * Checks if a given key exists in our data. This is called internally |
|
72 | + * by `empty` and `isset`. |
|
73 | + * |
|
74 | + * @param string $key Key to check if set. |
|
75 | + * |
|
76 | + * @return bool |
|
77 | + */ |
|
78 | + public function __isset( $key ) { |
|
79 | + return array_key_exists( $key, $this->current_data ); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Returns the value of any property. |
|
84 | - * |
|
85 | - * @param string $key Key to get. |
|
86 | - * @return mixed Property value or NULL if it does not exists |
|
87 | - */ |
|
88 | - public function __get( $key ) { |
|
89 | - if ( array_key_exists( $key, $this->current_data ) ) { |
|
90 | - return $this->current_data[ $key ]; |
|
91 | - } |
|
92 | - return null; |
|
93 | - } |
|
82 | + /** |
|
83 | + * Returns the value of any property. |
|
84 | + * |
|
85 | + * @param string $key Key to get. |
|
86 | + * @return mixed Property value or NULL if it does not exists |
|
87 | + */ |
|
88 | + public function __get( $key ) { |
|
89 | + if ( array_key_exists( $key, $this->current_data ) ) { |
|
90 | + return $this->current_data[ $key ]; |
|
91 | + } |
|
92 | + return null; |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Return data changes only. |
|
97 | - * |
|
98 | - * @return array |
|
99 | - */ |
|
100 | - public function get_changes() { |
|
101 | - $changes = array(); |
|
102 | - foreach ( $this->current_data as $id => $value ) { |
|
103 | - if ( ! array_key_exists( $id, $this->data ) || $value !== $this->data[ $id ] ) { |
|
104 | - $changes[ $id ] = $value; |
|
105 | - } |
|
106 | - } |
|
107 | - return $changes; |
|
108 | - } |
|
95 | + /** |
|
96 | + * Return data changes only. |
|
97 | + * |
|
98 | + * @return array |
|
99 | + */ |
|
100 | + public function get_changes() { |
|
101 | + $changes = array(); |
|
102 | + foreach ( $this->current_data as $id => $value ) { |
|
103 | + if ( ! array_key_exists( $id, $this->data ) || $value !== $this->data[ $id ] ) { |
|
104 | + $changes[ $id ] = $value; |
|
105 | + } |
|
106 | + } |
|
107 | + return $changes; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Return all data as an array. |
|
112 | - * |
|
113 | - * @return array |
|
114 | - */ |
|
115 | - public function get_data() { |
|
116 | - return $this->data; |
|
117 | - } |
|
110 | + /** |
|
111 | + * Return all data as an array. |
|
112 | + * |
|
113 | + * @return array |
|
114 | + */ |
|
115 | + public function get_data() { |
|
116 | + return $this->data; |
|
117 | + } |
|
118 | 118 | } |
@@ -7,40 +7,40 @@ |
||
7 | 7 | * Bail if we are not in WP. |
8 | 8 | */ |
9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
10 | - exit; |
|
10 | + exit; |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | /** |
14 | 14 | * Set the version only if its the current newest while loading. |
15 | 15 | */ |
16 | 16 | add_action('after_setup_theme', function () { |
17 | - global $ayecode_ui_version,$ayecode_ui_file_key; |
|
18 | - $this_version = "0.1.18"; |
|
19 | - if(version_compare($this_version , $ayecode_ui_version, '>')){ |
|
20 | - $ayecode_ui_version = $this_version ; |
|
21 | - $ayecode_ui_file_key = wp_hash( __FILE__ ); |
|
22 | - } |
|
17 | + global $ayecode_ui_version,$ayecode_ui_file_key; |
|
18 | + $this_version = "0.1.18"; |
|
19 | + if(version_compare($this_version , $ayecode_ui_version, '>')){ |
|
20 | + $ayecode_ui_version = $this_version ; |
|
21 | + $ayecode_ui_file_key = wp_hash( __FILE__ ); |
|
22 | + } |
|
23 | 23 | },0); |
24 | 24 | |
25 | 25 | /** |
26 | 26 | * Load this version of WP Bootstrap Settings only if the file hash is the current one. |
27 | 27 | */ |
28 | 28 | add_action('after_setup_theme', function () { |
29 | - global $ayecode_ui_file_key; |
|
30 | - if($ayecode_ui_file_key && $ayecode_ui_file_key == wp_hash( __FILE__ )){ |
|
31 | - include_once( dirname( __FILE__ ) . '/includes/class-aui.php' ); |
|
32 | - include_once( dirname( __FILE__ ) . '/includes/ayecode-ui-settings.php' ); |
|
33 | - } |
|
29 | + global $ayecode_ui_file_key; |
|
30 | + if($ayecode_ui_file_key && $ayecode_ui_file_key == wp_hash( __FILE__ )){ |
|
31 | + include_once( dirname( __FILE__ ) . '/includes/class-aui.php' ); |
|
32 | + include_once( dirname( __FILE__ ) . '/includes/ayecode-ui-settings.php' ); |
|
33 | + } |
|
34 | 34 | },1); |
35 | 35 | |
36 | 36 | /** |
37 | 37 | * Add the function that calls the class. |
38 | 38 | */ |
39 | 39 | if(!function_exists('aui')){ |
40 | - function aui(){ |
|
41 | - if(!class_exists("AUI",false)){ |
|
42 | - return false; |
|
43 | - } |
|
44 | - return AUI::instance(); |
|
45 | - } |
|
40 | + function aui(){ |
|
41 | + if(!class_exists("AUI",false)){ |
|
42 | + return false; |
|
43 | + } |
|
44 | + return AUI::instance(); |
|
45 | + } |
|
46 | 46 | } |
47 | 47 | \ No newline at end of file |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
4 | - exit; // Exit if accessed directly |
|
4 | + exit; // Exit if accessed directly |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,214 +11,214 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class AUI_Component_Helper { |
13 | 13 | |
14 | - /** |
|
15 | - * A component helper for generating a input name. |
|
16 | - * |
|
17 | - * @param $text |
|
18 | - * |
|
19 | - * @return string |
|
20 | - */ |
|
21 | - public static function name($text,$multiple = false){ |
|
22 | - $output = ''; |
|
23 | - |
|
24 | - if($text){ |
|
25 | - $is_multiple = $multiple ? '[]' : ''; |
|
26 | - $output = ' name="'.sanitize_html_class($text).$is_multiple.'" '; |
|
27 | - } |
|
28 | - |
|
29 | - return $output; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * A component helper for generating a item id. |
|
34 | - * |
|
35 | - * @param $text string The text to be used as the value. |
|
36 | - * |
|
37 | - * @return string The sanitized item. |
|
38 | - */ |
|
39 | - public static function id($text){ |
|
40 | - $output = ''; |
|
41 | - |
|
42 | - if($text){ |
|
43 | - $output = ' id="'.sanitize_html_class($text).'" '; |
|
44 | - } |
|
45 | - |
|
46 | - return $output; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * A component helper for generating a item title. |
|
51 | - * |
|
52 | - * @param $text string The text to be used as the value. |
|
53 | - * |
|
54 | - * @return string The sanitized item. |
|
55 | - */ |
|
56 | - public static function title($text){ |
|
57 | - $output = ''; |
|
58 | - |
|
59 | - if($text){ |
|
60 | - $output = ' title="'.esc_attr($text).'" '; |
|
61 | - } |
|
62 | - |
|
63 | - return $output; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * A component helper for generating a item value. |
|
68 | - * |
|
69 | - * @param $text string The text to be used as the value. |
|
70 | - * |
|
71 | - * @return string The sanitized item. |
|
72 | - */ |
|
73 | - public static function value($text){ |
|
74 | - $output = ''; |
|
75 | - |
|
76 | - if($text){ |
|
77 | - $output = ' value="'.sanitize_text_field($text).'" '; |
|
78 | - } |
|
79 | - |
|
80 | - return $output; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * A component helper for generating a item class attribute. |
|
85 | - * |
|
86 | - * @param $text string The text to be used as the value. |
|
87 | - * |
|
88 | - * @return string The sanitized item. |
|
89 | - */ |
|
90 | - public static function class_attr($text){ |
|
91 | - $output = ''; |
|
92 | - |
|
93 | - if($text){ |
|
94 | - $classes = self::esc_classes($text); |
|
95 | - if(!empty($classes)){ |
|
96 | - $output = ' class="'.$classes.'" '; |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - return $output; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Escape a string of classes. |
|
105 | - * |
|
106 | - * @param $text |
|
107 | - * |
|
108 | - * @return string |
|
109 | - */ |
|
110 | - public static function esc_classes($text){ |
|
111 | - $output = ''; |
|
112 | - |
|
113 | - if($text){ |
|
114 | - $classes = explode(" ",$text); |
|
115 | - $classes = array_map("trim",$classes); |
|
116 | - $classes = array_map("sanitize_html_class",$classes); |
|
117 | - if(!empty($classes)){ |
|
118 | - $output = implode(" ",$classes); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - return $output; |
|
123 | - |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param $args |
|
128 | - * |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public static function data_attributes($args){ |
|
132 | - $output = ''; |
|
133 | - |
|
134 | - if(!empty($args)){ |
|
135 | - |
|
136 | - foreach($args as $key => $val){ |
|
137 | - if(substr( $key, 0, 5 ) === "data-"){ |
|
138 | - $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
139 | - } |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - return $output; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @param $args |
|
148 | - * |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public static function aria_attributes($args){ |
|
152 | - $output = ''; |
|
153 | - |
|
154 | - if(!empty($args)){ |
|
155 | - |
|
156 | - foreach($args as $key => $val){ |
|
157 | - if(substr( $key, 0, 5 ) === "aria-"){ |
|
158 | - $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
159 | - } |
|
160 | - } |
|
161 | - } |
|
162 | - |
|
163 | - return $output; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Build a font awesome icon from a class. |
|
168 | - * |
|
169 | - * @param $class |
|
170 | - * @param bool $space_after |
|
171 | - * |
|
172 | - * @return string |
|
173 | - */ |
|
174 | - public static function icon($class,$space_after = false){ |
|
175 | - $output = ''; |
|
176 | - |
|
177 | - if($class){ |
|
178 | - $classes = self::esc_classes($class); |
|
179 | - if(!empty($classes)){ |
|
180 | - $output = '<i class="'.$classes.'" ></i>'; |
|
181 | - if($space_after){ |
|
182 | - $output .= " "; |
|
183 | - } |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - return $output; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * @param $args |
|
192 | - * |
|
193 | - * @return string |
|
194 | - */ |
|
195 | - public static function extra_attributes($args){ |
|
196 | - $output = ''; |
|
197 | - |
|
198 | - if(!empty($args) && is_array($args) ){ |
|
199 | - |
|
200 | - foreach($args as $key => $val){ |
|
201 | - $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
202 | - } |
|
203 | - } |
|
204 | - |
|
205 | - return $output; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * @param $args |
|
210 | - * |
|
211 | - * @return string |
|
212 | - */ |
|
213 | - public static function help_text($text){ |
|
214 | - $output = ''; |
|
215 | - |
|
216 | - if($text){ |
|
217 | - $output .= '<small class="form-text text-muted">'.wp_kses_post($text).'</small>'; |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - return $output; |
|
222 | - } |
|
14 | + /** |
|
15 | + * A component helper for generating a input name. |
|
16 | + * |
|
17 | + * @param $text |
|
18 | + * |
|
19 | + * @return string |
|
20 | + */ |
|
21 | + public static function name($text,$multiple = false){ |
|
22 | + $output = ''; |
|
23 | + |
|
24 | + if($text){ |
|
25 | + $is_multiple = $multiple ? '[]' : ''; |
|
26 | + $output = ' name="'.sanitize_html_class($text).$is_multiple.'" '; |
|
27 | + } |
|
28 | + |
|
29 | + return $output; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * A component helper for generating a item id. |
|
34 | + * |
|
35 | + * @param $text string The text to be used as the value. |
|
36 | + * |
|
37 | + * @return string The sanitized item. |
|
38 | + */ |
|
39 | + public static function id($text){ |
|
40 | + $output = ''; |
|
41 | + |
|
42 | + if($text){ |
|
43 | + $output = ' id="'.sanitize_html_class($text).'" '; |
|
44 | + } |
|
45 | + |
|
46 | + return $output; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * A component helper for generating a item title. |
|
51 | + * |
|
52 | + * @param $text string The text to be used as the value. |
|
53 | + * |
|
54 | + * @return string The sanitized item. |
|
55 | + */ |
|
56 | + public static function title($text){ |
|
57 | + $output = ''; |
|
58 | + |
|
59 | + if($text){ |
|
60 | + $output = ' title="'.esc_attr($text).'" '; |
|
61 | + } |
|
62 | + |
|
63 | + return $output; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * A component helper for generating a item value. |
|
68 | + * |
|
69 | + * @param $text string The text to be used as the value. |
|
70 | + * |
|
71 | + * @return string The sanitized item. |
|
72 | + */ |
|
73 | + public static function value($text){ |
|
74 | + $output = ''; |
|
75 | + |
|
76 | + if($text){ |
|
77 | + $output = ' value="'.sanitize_text_field($text).'" '; |
|
78 | + } |
|
79 | + |
|
80 | + return $output; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * A component helper for generating a item class attribute. |
|
85 | + * |
|
86 | + * @param $text string The text to be used as the value. |
|
87 | + * |
|
88 | + * @return string The sanitized item. |
|
89 | + */ |
|
90 | + public static function class_attr($text){ |
|
91 | + $output = ''; |
|
92 | + |
|
93 | + if($text){ |
|
94 | + $classes = self::esc_classes($text); |
|
95 | + if(!empty($classes)){ |
|
96 | + $output = ' class="'.$classes.'" '; |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + return $output; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Escape a string of classes. |
|
105 | + * |
|
106 | + * @param $text |
|
107 | + * |
|
108 | + * @return string |
|
109 | + */ |
|
110 | + public static function esc_classes($text){ |
|
111 | + $output = ''; |
|
112 | + |
|
113 | + if($text){ |
|
114 | + $classes = explode(" ",$text); |
|
115 | + $classes = array_map("trim",$classes); |
|
116 | + $classes = array_map("sanitize_html_class",$classes); |
|
117 | + if(!empty($classes)){ |
|
118 | + $output = implode(" ",$classes); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + return $output; |
|
123 | + |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param $args |
|
128 | + * |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public static function data_attributes($args){ |
|
132 | + $output = ''; |
|
133 | + |
|
134 | + if(!empty($args)){ |
|
135 | + |
|
136 | + foreach($args as $key => $val){ |
|
137 | + if(substr( $key, 0, 5 ) === "data-"){ |
|
138 | + $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
139 | + } |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + return $output; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @param $args |
|
148 | + * |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public static function aria_attributes($args){ |
|
152 | + $output = ''; |
|
153 | + |
|
154 | + if(!empty($args)){ |
|
155 | + |
|
156 | + foreach($args as $key => $val){ |
|
157 | + if(substr( $key, 0, 5 ) === "aria-"){ |
|
158 | + $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
159 | + } |
|
160 | + } |
|
161 | + } |
|
162 | + |
|
163 | + return $output; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Build a font awesome icon from a class. |
|
168 | + * |
|
169 | + * @param $class |
|
170 | + * @param bool $space_after |
|
171 | + * |
|
172 | + * @return string |
|
173 | + */ |
|
174 | + public static function icon($class,$space_after = false){ |
|
175 | + $output = ''; |
|
176 | + |
|
177 | + if($class){ |
|
178 | + $classes = self::esc_classes($class); |
|
179 | + if(!empty($classes)){ |
|
180 | + $output = '<i class="'.$classes.'" ></i>'; |
|
181 | + if($space_after){ |
|
182 | + $output .= " "; |
|
183 | + } |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + return $output; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * @param $args |
|
192 | + * |
|
193 | + * @return string |
|
194 | + */ |
|
195 | + public static function extra_attributes($args){ |
|
196 | + $output = ''; |
|
197 | + |
|
198 | + if(!empty($args) && is_array($args) ){ |
|
199 | + |
|
200 | + foreach($args as $key => $val){ |
|
201 | + $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
202 | + } |
|
203 | + } |
|
204 | + |
|
205 | + return $output; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * @param $args |
|
210 | + * |
|
211 | + * @return string |
|
212 | + */ |
|
213 | + public static function help_text($text){ |
|
214 | + $output = ''; |
|
215 | + |
|
216 | + if($text){ |
|
217 | + $output .= '<small class="form-text text-muted">'.wp_kses_post($text).'</small>'; |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + return $output; |
|
222 | + } |
|
223 | 223 | |
224 | 224 | } |
225 | 225 | \ No newline at end of file |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
4 | - exit; // Exit if accessed directly |
|
4 | + exit; // Exit if accessed directly |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,238 +11,238 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class AUI_Component_Input { |
13 | 13 | |
14 | - /** |
|
15 | - * Build the component. |
|
16 | - * |
|
17 | - * @param array $args |
|
18 | - * |
|
19 | - * @return string The rendered component. |
|
20 | - */ |
|
21 | - public static function input($args = array()){ |
|
22 | - $defaults = array( |
|
23 | - 'type' => 'text', |
|
24 | - 'name' => '', |
|
25 | - 'class' => '', |
|
26 | - 'id' => '', |
|
27 | - 'placeholder'=> '', |
|
28 | - 'title' => '', |
|
29 | - 'value' => '', |
|
30 | - 'required' => false, |
|
31 | - 'label' => '', |
|
32 | - 'label_after'=> false, |
|
33 | - 'label_class'=> '', |
|
34 | - 'label_type' => '', // sets the lable type, horizontal |
|
35 | - 'help_text' => '', |
|
36 | - 'validation_text' => '', |
|
37 | - 'validation_pattern' => '', |
|
38 | - 'no_wrap' => false, |
|
39 | - 'input_group_right' => '', |
|
40 | - 'input_group_left' => '', |
|
41 | - 'input_group_right_inside' => false, // forces the input group inside the input |
|
42 | - 'input_group_left_inside' => false, // forces the input group inside the input |
|
43 | - 'step' => '', |
|
44 | - 'switch' => false, // to show checkbox as a switch |
|
45 | - 'checked' => false, // set a checkbox or radio as selected |
|
46 | - 'password_toggle' => true, // toggle view/hide password |
|
47 | - 'extra_attributes' => array() // an array of extra attributes |
|
48 | - ); |
|
49 | - |
|
50 | - /** |
|
51 | - * Parse incoming $args into an array and merge it with $defaults |
|
52 | - */ |
|
53 | - $args = wp_parse_args( $args, $defaults ); |
|
54 | - $output = ''; |
|
55 | - if ( ! empty( $args['type'] ) ) { |
|
56 | - $type = sanitize_html_class( $args['type'] ); |
|
57 | - |
|
58 | - $help_text = ''; |
|
59 | - $label = ''; |
|
60 | - $label_after = $args['label_after']; |
|
61 | - $label_args = array( |
|
62 | - 'title'=> $args['label'], |
|
63 | - 'for'=> $args['id'], |
|
64 | - 'class' => $args['label_class']." ", |
|
65 | - 'label_type' => $args['label_type'] |
|
66 | - ); |
|
67 | - |
|
68 | - // Some special sauce for files |
|
69 | - if($type=='file' ){ |
|
70 | - $label_after = true; // if type file we need the label after |
|
71 | - $args['class'] .= ' custom-file-input '; |
|
72 | - }elseif($type=='checkbox'){ |
|
73 | - $label_after = true; // if type file we need the label after |
|
74 | - $args['class'] .= ' custom-control-input '; |
|
75 | - }elseif($type=='datepicker' || $type=='timepicker'){ |
|
76 | - $type = 'text'; |
|
77 | - $args['class'] .= ' aui-flatpickr bg-initial '; |
|
78 | - |
|
79 | - // enqueue the script |
|
80 | - $aui_settings = AyeCode_UI_Settings::instance(); |
|
81 | - $aui_settings->enqueue_flatpickr(); |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - // open/type |
|
86 | - $output .= '<input type="' . $type . '" '; |
|
87 | - |
|
88 | - // name |
|
89 | - if(!empty($args['name'])){ |
|
90 | - $output .= ' name="'.esc_attr($args['name']).'" '; |
|
91 | - } |
|
92 | - |
|
93 | - // id |
|
94 | - if(!empty($args['id'])){ |
|
95 | - $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
|
96 | - } |
|
97 | - |
|
98 | - // placeholder |
|
99 | - if(!empty($args['placeholder'])){ |
|
100 | - $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
|
101 | - } |
|
102 | - |
|
103 | - // title |
|
104 | - if(!empty($args['title'])){ |
|
105 | - $output .= ' title="'.esc_attr($args['title']).'" '; |
|
106 | - } |
|
107 | - |
|
108 | - // value |
|
109 | - if(!empty($args['value'])){ |
|
110 | - $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
|
111 | - } |
|
112 | - |
|
113 | - // checked, for radio and checkboxes |
|
114 | - if( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ){ |
|
115 | - $output .= ' checked '; |
|
116 | - } |
|
117 | - |
|
118 | - // validation text |
|
119 | - if(!empty($args['validation_text'])){ |
|
120 | - $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
|
121 | - $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
122 | - } |
|
123 | - |
|
124 | - // validation_pattern |
|
125 | - if(!empty($args['validation_pattern'])){ |
|
126 | - $output .= ' pattern="'.$args['validation_pattern'].'" '; |
|
127 | - } |
|
128 | - |
|
129 | - // step (for numbers) |
|
130 | - if(!empty($args['step'])){ |
|
131 | - $output .= ' step="'.$args['step'].'" '; |
|
132 | - } |
|
133 | - |
|
134 | - // required |
|
135 | - if(!empty($args['required'])){ |
|
136 | - $output .= ' required '; |
|
137 | - } |
|
138 | - |
|
139 | - // class |
|
140 | - $class = !empty($args['class']) ? $args['class'] : ''; |
|
141 | - $output .= ' class="form-control '.$class.'" '; |
|
142 | - |
|
143 | - // data-attributes |
|
144 | - $output .= AUI_Component_Helper::data_attributes($args); |
|
145 | - |
|
146 | - // extra attributes |
|
147 | - if(!empty($args['extra_attributes'])){ |
|
148 | - $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
149 | - } |
|
150 | - |
|
151 | - // close |
|
152 | - $output .= ' >'; |
|
153 | - |
|
154 | - |
|
155 | - // label |
|
156 | - if(!empty($args['label'])){ |
|
157 | - if($type == 'file'){$label_args['class'] .= 'custom-file-label';} |
|
158 | - elseif($type == 'checkbox'){$label_args['class'] .= 'custom-control-label';} |
|
159 | - $label = self::label( $label_args, $type ); |
|
160 | - } |
|
161 | - |
|
162 | - // help text |
|
163 | - if(!empty($args['help_text'])){ |
|
164 | - $help_text = AUI_Component_Helper::help_text($args['help_text']); |
|
165 | - } |
|
166 | - |
|
167 | - |
|
168 | - // set help text in the correct possition |
|
169 | - if($label_after){ |
|
170 | - $output .= $label . $help_text; |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - |
|
14 | + /** |
|
15 | + * Build the component. |
|
16 | + * |
|
17 | + * @param array $args |
|
18 | + * |
|
19 | + * @return string The rendered component. |
|
20 | + */ |
|
21 | + public static function input($args = array()){ |
|
22 | + $defaults = array( |
|
23 | + 'type' => 'text', |
|
24 | + 'name' => '', |
|
25 | + 'class' => '', |
|
26 | + 'id' => '', |
|
27 | + 'placeholder'=> '', |
|
28 | + 'title' => '', |
|
29 | + 'value' => '', |
|
30 | + 'required' => false, |
|
31 | + 'label' => '', |
|
32 | + 'label_after'=> false, |
|
33 | + 'label_class'=> '', |
|
34 | + 'label_type' => '', // sets the lable type, horizontal |
|
35 | + 'help_text' => '', |
|
36 | + 'validation_text' => '', |
|
37 | + 'validation_pattern' => '', |
|
38 | + 'no_wrap' => false, |
|
39 | + 'input_group_right' => '', |
|
40 | + 'input_group_left' => '', |
|
41 | + 'input_group_right_inside' => false, // forces the input group inside the input |
|
42 | + 'input_group_left_inside' => false, // forces the input group inside the input |
|
43 | + 'step' => '', |
|
44 | + 'switch' => false, // to show checkbox as a switch |
|
45 | + 'checked' => false, // set a checkbox or radio as selected |
|
46 | + 'password_toggle' => true, // toggle view/hide password |
|
47 | + 'extra_attributes' => array() // an array of extra attributes |
|
48 | + ); |
|
49 | + |
|
50 | + /** |
|
51 | + * Parse incoming $args into an array and merge it with $defaults |
|
52 | + */ |
|
53 | + $args = wp_parse_args( $args, $defaults ); |
|
54 | + $output = ''; |
|
55 | + if ( ! empty( $args['type'] ) ) { |
|
56 | + $type = sanitize_html_class( $args['type'] ); |
|
57 | + |
|
58 | + $help_text = ''; |
|
59 | + $label = ''; |
|
60 | + $label_after = $args['label_after']; |
|
61 | + $label_args = array( |
|
62 | + 'title'=> $args['label'], |
|
63 | + 'for'=> $args['id'], |
|
64 | + 'class' => $args['label_class']." ", |
|
65 | + 'label_type' => $args['label_type'] |
|
66 | + ); |
|
67 | + |
|
68 | + // Some special sauce for files |
|
69 | + if($type=='file' ){ |
|
70 | + $label_after = true; // if type file we need the label after |
|
71 | + $args['class'] .= ' custom-file-input '; |
|
72 | + }elseif($type=='checkbox'){ |
|
73 | + $label_after = true; // if type file we need the label after |
|
74 | + $args['class'] .= ' custom-control-input '; |
|
75 | + }elseif($type=='datepicker' || $type=='timepicker'){ |
|
76 | + $type = 'text'; |
|
77 | + $args['class'] .= ' aui-flatpickr bg-initial '; |
|
78 | + |
|
79 | + // enqueue the script |
|
80 | + $aui_settings = AyeCode_UI_Settings::instance(); |
|
81 | + $aui_settings->enqueue_flatpickr(); |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + // open/type |
|
86 | + $output .= '<input type="' . $type . '" '; |
|
87 | + |
|
88 | + // name |
|
89 | + if(!empty($args['name'])){ |
|
90 | + $output .= ' name="'.esc_attr($args['name']).'" '; |
|
91 | + } |
|
92 | + |
|
93 | + // id |
|
94 | + if(!empty($args['id'])){ |
|
95 | + $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
|
96 | + } |
|
97 | + |
|
98 | + // placeholder |
|
99 | + if(!empty($args['placeholder'])){ |
|
100 | + $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
|
101 | + } |
|
102 | + |
|
103 | + // title |
|
104 | + if(!empty($args['title'])){ |
|
105 | + $output .= ' title="'.esc_attr($args['title']).'" '; |
|
106 | + } |
|
107 | + |
|
108 | + // value |
|
109 | + if(!empty($args['value'])){ |
|
110 | + $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
|
111 | + } |
|
112 | + |
|
113 | + // checked, for radio and checkboxes |
|
114 | + if( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ){ |
|
115 | + $output .= ' checked '; |
|
116 | + } |
|
117 | + |
|
118 | + // validation text |
|
119 | + if(!empty($args['validation_text'])){ |
|
120 | + $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
|
121 | + $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
122 | + } |
|
123 | + |
|
124 | + // validation_pattern |
|
125 | + if(!empty($args['validation_pattern'])){ |
|
126 | + $output .= ' pattern="'.$args['validation_pattern'].'" '; |
|
127 | + } |
|
128 | + |
|
129 | + // step (for numbers) |
|
130 | + if(!empty($args['step'])){ |
|
131 | + $output .= ' step="'.$args['step'].'" '; |
|
132 | + } |
|
133 | + |
|
134 | + // required |
|
135 | + if(!empty($args['required'])){ |
|
136 | + $output .= ' required '; |
|
137 | + } |
|
138 | + |
|
139 | + // class |
|
140 | + $class = !empty($args['class']) ? $args['class'] : ''; |
|
141 | + $output .= ' class="form-control '.$class.'" '; |
|
142 | + |
|
143 | + // data-attributes |
|
144 | + $output .= AUI_Component_Helper::data_attributes($args); |
|
145 | + |
|
146 | + // extra attributes |
|
147 | + if(!empty($args['extra_attributes'])){ |
|
148 | + $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
149 | + } |
|
150 | + |
|
151 | + // close |
|
152 | + $output .= ' >'; |
|
153 | + |
|
154 | + |
|
155 | + // label |
|
156 | + if(!empty($args['label'])){ |
|
157 | + if($type == 'file'){$label_args['class'] .= 'custom-file-label';} |
|
158 | + elseif($type == 'checkbox'){$label_args['class'] .= 'custom-control-label';} |
|
159 | + $label = self::label( $label_args, $type ); |
|
160 | + } |
|
161 | + |
|
162 | + // help text |
|
163 | + if(!empty($args['help_text'])){ |
|
164 | + $help_text = AUI_Component_Helper::help_text($args['help_text']); |
|
165 | + } |
|
166 | + |
|
167 | + |
|
168 | + // set help text in the correct possition |
|
169 | + if($label_after){ |
|
170 | + $output .= $label . $help_text; |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + |
|
175 | 175 | |
176 | 176 | |
177 | - // some input types need a separate wrap |
|
178 | - if($type == 'file') { |
|
179 | - $output = self::wrap( array( |
|
180 | - 'content' => $output, |
|
181 | - 'class' => 'form-group custom-file' |
|
182 | - ) ); |
|
183 | - }elseif($type == 'checkbox'){ |
|
184 | - $wrap_class = $args['switch'] ? 'custom-switch' : 'custom-checkbox'; |
|
185 | - $output = self::wrap( array( |
|
186 | - 'content' => $output, |
|
187 | - 'class' => 'custom-control '.$wrap_class |
|
188 | - ) ); |
|
189 | - |
|
190 | - if($args['label_type']=='horizontal'){ |
|
191 | - $output = '<div class="col-sm-2 col-form-label"></div><div class="col-sm-10">' . $output . '</div>'; |
|
192 | - } |
|
193 | - }elseif($type == 'password' && $args['password_toggle'] && !$args['input_group_right']){ |
|
194 | - |
|
177 | + // some input types need a separate wrap |
|
178 | + if($type == 'file') { |
|
179 | + $output = self::wrap( array( |
|
180 | + 'content' => $output, |
|
181 | + 'class' => 'form-group custom-file' |
|
182 | + ) ); |
|
183 | + }elseif($type == 'checkbox'){ |
|
184 | + $wrap_class = $args['switch'] ? 'custom-switch' : 'custom-checkbox'; |
|
185 | + $output = self::wrap( array( |
|
186 | + 'content' => $output, |
|
187 | + 'class' => 'custom-control '.$wrap_class |
|
188 | + ) ); |
|
189 | + |
|
190 | + if($args['label_type']=='horizontal'){ |
|
191 | + $output = '<div class="col-sm-2 col-form-label"></div><div class="col-sm-10">' . $output . '</div>'; |
|
192 | + } |
|
193 | + }elseif($type == 'password' && $args['password_toggle'] && !$args['input_group_right']){ |
|
194 | + |
|
195 | 195 | |
196 | - // allow password field to toggle view |
|
197 | - $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
|
196 | + // allow password field to toggle view |
|
197 | + $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
|
198 | 198 | onclick="var $el = jQuery(this).find(\'i\');$el.toggleClass(\'fa-eye fa-eye-slash\'); |
199 | 199 | var $eli = jQuery(this).parent().parent().find(\'input\'); |
200 | 200 | if($el.hasClass(\'fa-eye\')) |
201 | 201 | {$eli.attr(\'type\',\'text\');} |
202 | 202 | else{$eli.attr(\'type\',\'password\');}" |
203 | 203 | ><i class="far fa-fw fa-eye-slash"></i></span>'; |
204 | - } |
|
205 | - |
|
206 | - // input group wraps |
|
207 | - if($args['input_group_left'] || $args['input_group_right']){ |
|
208 | - $w100 = strpos($args['class'], 'w-100') !== false ? ' w-100' : ''; |
|
209 | - if($args['input_group_left']){ |
|
210 | - $output = self::wrap( array( |
|
211 | - 'content' => $output, |
|
212 | - 'class' => $args['input_group_left_inside'] ? 'input-group-inside'.$w100 : 'input-group', |
|
213 | - 'input_group_left' => $args['input_group_left'], |
|
214 | - 'input_group_left_inside' => $args['input_group_left_inside'] |
|
215 | - ) ); |
|
216 | - } |
|
217 | - if($args['input_group_right']){ |
|
218 | - $output = self::wrap( array( |
|
219 | - 'content' => $output, |
|
220 | - 'class' => $args['input_group_right_inside'] ? 'input-group-inside'.$w100 : 'input-group', |
|
221 | - 'input_group_right' => $args['input_group_right'], |
|
222 | - 'input_group_right_inside' => $args['input_group_right_inside'] |
|
223 | - ) ); |
|
224 | - } |
|
225 | - |
|
226 | - // Labels need to be on the outside of the wrap |
|
204 | + } |
|
205 | + |
|
206 | + // input group wraps |
|
207 | + if($args['input_group_left'] || $args['input_group_right']){ |
|
208 | + $w100 = strpos($args['class'], 'w-100') !== false ? ' w-100' : ''; |
|
209 | + if($args['input_group_left']){ |
|
210 | + $output = self::wrap( array( |
|
211 | + 'content' => $output, |
|
212 | + 'class' => $args['input_group_left_inside'] ? 'input-group-inside'.$w100 : 'input-group', |
|
213 | + 'input_group_left' => $args['input_group_left'], |
|
214 | + 'input_group_left_inside' => $args['input_group_left_inside'] |
|
215 | + ) ); |
|
216 | + } |
|
217 | + if($args['input_group_right']){ |
|
218 | + $output = self::wrap( array( |
|
219 | + 'content' => $output, |
|
220 | + 'class' => $args['input_group_right_inside'] ? 'input-group-inside'.$w100 : 'input-group', |
|
221 | + 'input_group_right' => $args['input_group_right'], |
|
222 | + 'input_group_right_inside' => $args['input_group_right_inside'] |
|
223 | + ) ); |
|
224 | + } |
|
225 | + |
|
226 | + // Labels need to be on the outside of the wrap |
|
227 | 227 | // $label = self::label( $label_args, $type ); |
228 | 228 | // $output = $label . str_replace($label,"",$output); |
229 | - } |
|
229 | + } |
|
230 | 230 | |
231 | - if(!$label_after){ |
|
232 | - $output .= $help_text; |
|
233 | - } |
|
231 | + if(!$label_after){ |
|
232 | + $output .= $help_text; |
|
233 | + } |
|
234 | 234 | |
235 | 235 | |
236 | - if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
|
237 | - $output = self::wrap( array( |
|
238 | - 'content' => $output, |
|
239 | - 'class' => 'col-sm-10', |
|
240 | - ) ); |
|
241 | - } |
|
236 | + if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
|
237 | + $output = self::wrap( array( |
|
238 | + 'content' => $output, |
|
239 | + 'class' => 'col-sm-10', |
|
240 | + ) ); |
|
241 | + } |
|
242 | 242 | |
243 | - if(!$label_after){ |
|
244 | - $output = $label . $output; |
|
245 | - } |
|
243 | + if(!$label_after){ |
|
244 | + $output = $label . $output; |
|
245 | + } |
|
246 | 246 | |
247 | 247 | // // maybe horizontal label |
248 | 248 | // if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
@@ -253,662 +253,662 @@ discard block |
||
253 | 253 | // $output .= '</div>'; |
254 | 254 | // } |
255 | 255 | |
256 | - // wrap |
|
257 | - if(!$args['no_wrap']){ |
|
258 | - $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
259 | - $output = self::wrap(array( |
|
260 | - 'content' => $output, |
|
261 | - 'class' => $wrap_class, |
|
262 | - )); |
|
263 | - } |
|
264 | - |
|
265 | - |
|
266 | - |
|
267 | - } |
|
268 | - |
|
269 | - return $output; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Build the component. |
|
274 | - * |
|
275 | - * @param array $args |
|
276 | - * |
|
277 | - * @return string The rendered component. |
|
278 | - */ |
|
279 | - public static function textarea($args = array()){ |
|
280 | - $defaults = array( |
|
281 | - 'name' => '', |
|
282 | - 'class' => '', |
|
283 | - 'id' => '', |
|
284 | - 'placeholder'=> '', |
|
285 | - 'title' => '', |
|
286 | - 'value' => '', |
|
287 | - 'required' => false, |
|
288 | - 'label' => '', |
|
289 | - 'label_class' => '', |
|
290 | - 'label_type' => '', // sets the lable type, horizontal |
|
291 | - 'help_text' => '', |
|
292 | - 'validation_text' => '', |
|
293 | - 'validation_pattern' => '', |
|
294 | - 'no_wrap' => false, |
|
295 | - 'rows' => '', |
|
296 | - 'wysiwyg' => false, |
|
297 | - ); |
|
298 | - |
|
299 | - /** |
|
300 | - * Parse incoming $args into an array and merge it with $defaults |
|
301 | - */ |
|
302 | - $args = wp_parse_args( $args, $defaults ); |
|
303 | - $output = ''; |
|
304 | - |
|
305 | - // label |
|
306 | - if(!empty($args['label']) && is_array($args['label'])){ |
|
307 | - }elseif(!empty($args['label'])){ |
|
308 | - $label_args = array( |
|
309 | - 'title'=> $args['label'], |
|
310 | - 'for'=> $args['id'], |
|
311 | - 'class' => $args['label_class']." ", |
|
312 | - 'label_type' => $args['label_type'] |
|
313 | - ); |
|
314 | - $output .= self::label( $label_args ); |
|
315 | - } |
|
316 | - |
|
317 | - // maybe horizontal label |
|
318 | - if($args['label_type']=='horizontal'){ |
|
319 | - $output .= '<div class="col-sm-10">'; |
|
320 | - } |
|
321 | - |
|
322 | - if(!empty($args['wysiwyg'])){ |
|
323 | - ob_start(); |
|
324 | - $content = $args['value']; |
|
325 | - $editor_id = !empty($args['id']) ? sanitize_html_class($args['id']) : 'wp_editor'; |
|
326 | - $settings = array( |
|
327 | - 'textarea_rows' => !empty(absint($args['rows'])) ? absint($args['rows']) : 4, |
|
328 | - 'quicktags' => false, |
|
329 | - 'media_buttons' => false, |
|
330 | - 'editor_class' => 'form-control', |
|
331 | - 'textarea_name' => !empty($args['name']) ? sanitize_html_class($args['name']) : sanitize_html_class($args['id']), |
|
332 | - 'teeny' => true, |
|
333 | - ); |
|
334 | - |
|
335 | - // maybe set settings if array |
|
336 | - if(is_array($args['wysiwyg'])){ |
|
337 | - $settings = wp_parse_args( $args['wysiwyg'], $settings ); |
|
338 | - } |
|
339 | - |
|
340 | - wp_editor( $content, $editor_id, $settings ); |
|
341 | - $output .= ob_get_clean(); |
|
342 | - }else{ |
|
343 | - |
|
344 | - // open |
|
345 | - $output .= '<textarea '; |
|
346 | - |
|
347 | - // name |
|
348 | - if(!empty($args['name'])){ |
|
349 | - $output .= ' name="'.sanitize_html_class($args['name']).'" '; |
|
350 | - } |
|
351 | - |
|
352 | - // id |
|
353 | - if(!empty($args['id'])){ |
|
354 | - $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
|
355 | - } |
|
356 | - |
|
357 | - // placeholder |
|
358 | - if(!empty($args['placeholder'])){ |
|
359 | - $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
|
360 | - } |
|
361 | - |
|
362 | - // title |
|
363 | - if(!empty($args['title'])){ |
|
364 | - $output .= ' title="'.esc_attr($args['title']).'" '; |
|
365 | - } |
|
366 | - |
|
367 | - // validation text |
|
368 | - if(!empty($args['validation_text'])){ |
|
369 | - $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
|
370 | - $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
371 | - } |
|
372 | - |
|
373 | - // validation_pattern |
|
374 | - if(!empty($args['validation_pattern'])){ |
|
375 | - $output .= ' pattern="'.$args['validation_pattern'].'" '; |
|
376 | - } |
|
377 | - |
|
378 | - // required |
|
379 | - if(!empty($args['required'])){ |
|
380 | - $output .= ' required '; |
|
381 | - } |
|
382 | - |
|
383 | - // rows |
|
384 | - if(!empty($args['rows'])){ |
|
385 | - $output .= ' rows="'.absint($args['rows']).'" '; |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - // class |
|
390 | - $class = !empty($args['class']) ? $args['class'] : ''; |
|
391 | - $output .= ' class="form-control '.$class.'" '; |
|
392 | - |
|
393 | - |
|
394 | - // close tag |
|
395 | - $output .= ' >'; |
|
396 | - |
|
397 | - // value |
|
398 | - if(!empty($args['value'])){ |
|
399 | - $output .= sanitize_textarea_field($args['value']); |
|
400 | - } |
|
401 | - |
|
402 | - // closing tag |
|
403 | - $output .= '</textarea>'; |
|
404 | - |
|
405 | - } |
|
406 | - |
|
407 | - // help text |
|
408 | - if(!empty($args['help_text'])){ |
|
409 | - $output .= AUI_Component_Helper::help_text($args['help_text']); |
|
410 | - } |
|
411 | - |
|
412 | - // maybe horizontal label |
|
413 | - if($args['label_type']=='horizontal'){ |
|
414 | - $output .= '</div>'; |
|
415 | - } |
|
416 | - |
|
417 | - |
|
418 | - // wrap |
|
419 | - if(!$args['no_wrap']){ |
|
420 | - $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
421 | - $output = self::wrap(array( |
|
422 | - 'content' => $output, |
|
423 | - 'class' => $wrap_class, |
|
424 | - )); |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - return $output; |
|
429 | - } |
|
430 | - |
|
431 | - public static function label($args = array(), $type = ''){ |
|
432 | - //<label for="exampleInputEmail1">Email address</label> |
|
433 | - $defaults = array( |
|
434 | - 'title' => 'div', |
|
435 | - 'for' => '', |
|
436 | - 'class' => '', |
|
437 | - 'label_type' => '', // horizontal |
|
438 | - ); |
|
256 | + // wrap |
|
257 | + if(!$args['no_wrap']){ |
|
258 | + $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
259 | + $output = self::wrap(array( |
|
260 | + 'content' => $output, |
|
261 | + 'class' => $wrap_class, |
|
262 | + )); |
|
263 | + } |
|
264 | + |
|
265 | + |
|
266 | + |
|
267 | + } |
|
268 | + |
|
269 | + return $output; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Build the component. |
|
274 | + * |
|
275 | + * @param array $args |
|
276 | + * |
|
277 | + * @return string The rendered component. |
|
278 | + */ |
|
279 | + public static function textarea($args = array()){ |
|
280 | + $defaults = array( |
|
281 | + 'name' => '', |
|
282 | + 'class' => '', |
|
283 | + 'id' => '', |
|
284 | + 'placeholder'=> '', |
|
285 | + 'title' => '', |
|
286 | + 'value' => '', |
|
287 | + 'required' => false, |
|
288 | + 'label' => '', |
|
289 | + 'label_class' => '', |
|
290 | + 'label_type' => '', // sets the lable type, horizontal |
|
291 | + 'help_text' => '', |
|
292 | + 'validation_text' => '', |
|
293 | + 'validation_pattern' => '', |
|
294 | + 'no_wrap' => false, |
|
295 | + 'rows' => '', |
|
296 | + 'wysiwyg' => false, |
|
297 | + ); |
|
298 | + |
|
299 | + /** |
|
300 | + * Parse incoming $args into an array and merge it with $defaults |
|
301 | + */ |
|
302 | + $args = wp_parse_args( $args, $defaults ); |
|
303 | + $output = ''; |
|
304 | + |
|
305 | + // label |
|
306 | + if(!empty($args['label']) && is_array($args['label'])){ |
|
307 | + }elseif(!empty($args['label'])){ |
|
308 | + $label_args = array( |
|
309 | + 'title'=> $args['label'], |
|
310 | + 'for'=> $args['id'], |
|
311 | + 'class' => $args['label_class']." ", |
|
312 | + 'label_type' => $args['label_type'] |
|
313 | + ); |
|
314 | + $output .= self::label( $label_args ); |
|
315 | + } |
|
316 | + |
|
317 | + // maybe horizontal label |
|
318 | + if($args['label_type']=='horizontal'){ |
|
319 | + $output .= '<div class="col-sm-10">'; |
|
320 | + } |
|
321 | + |
|
322 | + if(!empty($args['wysiwyg'])){ |
|
323 | + ob_start(); |
|
324 | + $content = $args['value']; |
|
325 | + $editor_id = !empty($args['id']) ? sanitize_html_class($args['id']) : 'wp_editor'; |
|
326 | + $settings = array( |
|
327 | + 'textarea_rows' => !empty(absint($args['rows'])) ? absint($args['rows']) : 4, |
|
328 | + 'quicktags' => false, |
|
329 | + 'media_buttons' => false, |
|
330 | + 'editor_class' => 'form-control', |
|
331 | + 'textarea_name' => !empty($args['name']) ? sanitize_html_class($args['name']) : sanitize_html_class($args['id']), |
|
332 | + 'teeny' => true, |
|
333 | + ); |
|
334 | + |
|
335 | + // maybe set settings if array |
|
336 | + if(is_array($args['wysiwyg'])){ |
|
337 | + $settings = wp_parse_args( $args['wysiwyg'], $settings ); |
|
338 | + } |
|
339 | + |
|
340 | + wp_editor( $content, $editor_id, $settings ); |
|
341 | + $output .= ob_get_clean(); |
|
342 | + }else{ |
|
343 | + |
|
344 | + // open |
|
345 | + $output .= '<textarea '; |
|
346 | + |
|
347 | + // name |
|
348 | + if(!empty($args['name'])){ |
|
349 | + $output .= ' name="'.sanitize_html_class($args['name']).'" '; |
|
350 | + } |
|
351 | + |
|
352 | + // id |
|
353 | + if(!empty($args['id'])){ |
|
354 | + $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
|
355 | + } |
|
356 | + |
|
357 | + // placeholder |
|
358 | + if(!empty($args['placeholder'])){ |
|
359 | + $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
|
360 | + } |
|
361 | + |
|
362 | + // title |
|
363 | + if(!empty($args['title'])){ |
|
364 | + $output .= ' title="'.esc_attr($args['title']).'" '; |
|
365 | + } |
|
366 | + |
|
367 | + // validation text |
|
368 | + if(!empty($args['validation_text'])){ |
|
369 | + $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
|
370 | + $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
371 | + } |
|
372 | + |
|
373 | + // validation_pattern |
|
374 | + if(!empty($args['validation_pattern'])){ |
|
375 | + $output .= ' pattern="'.$args['validation_pattern'].'" '; |
|
376 | + } |
|
377 | + |
|
378 | + // required |
|
379 | + if(!empty($args['required'])){ |
|
380 | + $output .= ' required '; |
|
381 | + } |
|
382 | + |
|
383 | + // rows |
|
384 | + if(!empty($args['rows'])){ |
|
385 | + $output .= ' rows="'.absint($args['rows']).'" '; |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + // class |
|
390 | + $class = !empty($args['class']) ? $args['class'] : ''; |
|
391 | + $output .= ' class="form-control '.$class.'" '; |
|
392 | + |
|
393 | + |
|
394 | + // close tag |
|
395 | + $output .= ' >'; |
|
396 | + |
|
397 | + // value |
|
398 | + if(!empty($args['value'])){ |
|
399 | + $output .= sanitize_textarea_field($args['value']); |
|
400 | + } |
|
401 | + |
|
402 | + // closing tag |
|
403 | + $output .= '</textarea>'; |
|
404 | + |
|
405 | + } |
|
406 | + |
|
407 | + // help text |
|
408 | + if(!empty($args['help_text'])){ |
|
409 | + $output .= AUI_Component_Helper::help_text($args['help_text']); |
|
410 | + } |
|
411 | + |
|
412 | + // maybe horizontal label |
|
413 | + if($args['label_type']=='horizontal'){ |
|
414 | + $output .= '</div>'; |
|
415 | + } |
|
416 | + |
|
417 | + |
|
418 | + // wrap |
|
419 | + if(!$args['no_wrap']){ |
|
420 | + $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
421 | + $output = self::wrap(array( |
|
422 | + 'content' => $output, |
|
423 | + 'class' => $wrap_class, |
|
424 | + )); |
|
425 | + } |
|
426 | + |
|
427 | + |
|
428 | + return $output; |
|
429 | + } |
|
430 | + |
|
431 | + public static function label($args = array(), $type = ''){ |
|
432 | + //<label for="exampleInputEmail1">Email address</label> |
|
433 | + $defaults = array( |
|
434 | + 'title' => 'div', |
|
435 | + 'for' => '', |
|
436 | + 'class' => '', |
|
437 | + 'label_type' => '', // horizontal |
|
438 | + ); |
|
439 | 439 | |
440 | - /** |
|
441 | - * Parse incoming $args into an array and merge it with $defaults |
|
442 | - */ |
|
443 | - $args = wp_parse_args( $args, $defaults ); |
|
444 | - $output = ''; |
|
440 | + /** |
|
441 | + * Parse incoming $args into an array and merge it with $defaults |
|
442 | + */ |
|
443 | + $args = wp_parse_args( $args, $defaults ); |
|
444 | + $output = ''; |
|
445 | 445 | |
446 | - if($args['title']){ |
|
446 | + if($args['title']){ |
|
447 | 447 | |
448 | - // maybe hide labels //@todo set a global option for visibility class |
|
449 | - if($type == 'file' || $type == 'checkbox' || $type == 'radio' || !empty($args['label_type']) ){ |
|
450 | - $class = $args['class']; |
|
451 | - }else{ |
|
452 | - $class = 'sr-only '.$args['class']; |
|
453 | - } |
|
448 | + // maybe hide labels //@todo set a global option for visibility class |
|
449 | + if($type == 'file' || $type == 'checkbox' || $type == 'radio' || !empty($args['label_type']) ){ |
|
450 | + $class = $args['class']; |
|
451 | + }else{ |
|
452 | + $class = 'sr-only '.$args['class']; |
|
453 | + } |
|
454 | 454 | |
455 | - // maybe horizontal |
|
456 | - if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
|
457 | - $class .= ' col-sm-2 col-form-label'; |
|
458 | - } |
|
459 | - |
|
460 | - // open |
|
461 | - $output .= '<label '; |
|
462 | - |
|
463 | - // for |
|
464 | - if(!empty($args['for'])){ |
|
465 | - $output .= ' for="'.sanitize_text_field($args['for']).'" '; |
|
466 | - } |
|
467 | - |
|
468 | - // class |
|
469 | - $output .= ' class="'.$class.'" '; |
|
470 | - |
|
471 | - // close |
|
472 | - $output .= '>'; |
|
473 | - |
|
474 | - |
|
475 | - // title, don't escape fully as can contain html |
|
476 | - if(!empty($args['title'])){ |
|
477 | - $output .= wp_kses_post($args['title']); |
|
478 | - } |
|
479 | - |
|
480 | - // close wrap |
|
481 | - $output .= '</label>'; |
|
482 | - |
|
483 | - |
|
484 | - } |
|
485 | - |
|
486 | - |
|
487 | - return $output; |
|
488 | - } |
|
489 | - |
|
490 | - public static function wrap($args = array()){ |
|
491 | - $defaults = array( |
|
492 | - 'type' => 'div', |
|
493 | - 'class' => 'form-group', |
|
494 | - 'content' => '', |
|
495 | - 'input_group_left' => '', |
|
496 | - 'input_group_right' => '', |
|
497 | - 'input_group_left_inside' => false, |
|
498 | - 'input_group_right_inside' => false, |
|
499 | - ); |
|
500 | - |
|
501 | - /** |
|
502 | - * Parse incoming $args into an array and merge it with $defaults |
|
503 | - */ |
|
504 | - $args = wp_parse_args( $args, $defaults ); |
|
505 | - $output = ''; |
|
506 | - if($args['type']){ |
|
507 | - |
|
508 | - // open |
|
509 | - $output .= '<'.sanitize_html_class($args['type']); |
|
510 | - |
|
511 | - // class |
|
512 | - $class = !empty($args['class']) ? $args['class'] : ''; |
|
513 | - $output .= ' class="'.$class.'" '; |
|
514 | - |
|
515 | - // close wrap |
|
516 | - $output .= ' >'; |
|
517 | - |
|
518 | - |
|
519 | - // Input group left |
|
520 | - if(!empty($args['input_group_left'])){ |
|
521 | - $position_class = !empty($args['input_group_left_inside']) ? 'position-absolute' : ''; |
|
522 | - $input_group_left = strpos($args['input_group_left'], '<') !== false ? $args['input_group_left'] : '<span class="input-group-text">'.$args['input_group_left'].'</span>'; |
|
523 | - $output .= '<div class="input-group-prepend '.$position_class.'">'.$input_group_left.'</div>'; |
|
524 | - } |
|
525 | - |
|
526 | - // content |
|
527 | - $output .= $args['content']; |
|
528 | - |
|
529 | - // Input group right |
|
530 | - if(!empty($args['input_group_right'])){ |
|
531 | - $position_class = !empty($args['input_group_left_inside']) ? 'position-absolute' : ''; |
|
532 | - $input_group_right = strpos($args['input_group_right'], '<') !== false ? $args['input_group_right'] : '<span class="input-group-text">'.$args['input_group_right'].'</span>'; |
|
533 | - $output .= '<div class="input-group-append '.$position_class.'">'.$input_group_right.'</div>'; |
|
534 | - } |
|
535 | - |
|
536 | - |
|
537 | - // close wrap |
|
538 | - $output .= '</'.sanitize_html_class($args['type']).'>'; |
|
539 | - |
|
540 | - |
|
541 | - }else{ |
|
542 | - $output = $args['content']; |
|
543 | - } |
|
544 | - |
|
545 | - return $output; |
|
546 | - } |
|
547 | - |
|
548 | - /** |
|
549 | - * Build the component. |
|
550 | - * |
|
551 | - * @param array $args |
|
552 | - * |
|
553 | - * @return string The rendered component. |
|
554 | - */ |
|
555 | - public static function select($args = array()){ |
|
556 | - $defaults = array( |
|
557 | - 'class' => '', |
|
558 | - 'id' => '', |
|
559 | - 'title' => '', |
|
560 | - 'value' => '', // can be an array or a string |
|
561 | - 'required' => false, |
|
562 | - 'label' => '', |
|
563 | - 'label_class' => '', |
|
564 | - 'label_type' => '', // sets the lable type, horizontal |
|
565 | - 'help_text' => '', |
|
566 | - 'placeholder'=> '', |
|
567 | - 'options' => array(), |
|
568 | - 'icon' => '', |
|
569 | - 'multiple' => false, |
|
570 | - 'select2' => false, |
|
571 | - 'no_wrap' => false, |
|
572 | - 'extra_attributes' => array() // an array of extra attributes |
|
573 | - ); |
|
574 | - |
|
575 | - /** |
|
576 | - * Parse incoming $args into an array and merge it with $defaults |
|
577 | - */ |
|
578 | - $args = wp_parse_args( $args, $defaults ); |
|
579 | - $output = ''; |
|
580 | - |
|
581 | - // Maybe setup select2 |
|
582 | - $is_select2 = false; |
|
583 | - if(!empty($args['select2'])){ |
|
584 | - $args['class'] .= ' aui-select2'; |
|
585 | - $is_select2 = true; |
|
586 | - }elseif( strpos($args['class'], 'aui-select2') !== false){ |
|
587 | - $is_select2 = true; |
|
588 | - } |
|
589 | - |
|
590 | - // select2 tags |
|
591 | - if( !empty($args['select2']) && $args['select2'] === 'tags'){ // triple equlas needed here for some reason |
|
592 | - $args['data-tags'] = 'true'; |
|
593 | - $args['data-token-separators'] = "[',']"; |
|
594 | - $args['multiple'] = true; |
|
595 | - } |
|
596 | - |
|
597 | - // select2 placeholder |
|
598 | - if($is_select2 && !empty($args['placeholder']) && empty($args['data-placeholder'])){ |
|
599 | - $args['data-placeholder'] = esc_attr($args['placeholder']); |
|
600 | - $args['data-allow-clear'] = empty($args['data-allow-clear']) ? true : esc_attr($args['data-allow-clear']); |
|
601 | - } |
|
602 | - |
|
603 | - // label |
|
604 | - if(!empty($args['label']) && is_array($args['label'])){ |
|
605 | - }elseif(!empty($args['label'])){ |
|
606 | - $label_args = array( |
|
607 | - 'title'=> $args['label'], |
|
608 | - 'for'=> $args['id'], |
|
609 | - 'class' => $args['label_class']." ", |
|
610 | - 'label_type' => $args['label_type'] |
|
611 | - ); |
|
612 | - $output .= self::label($label_args); |
|
613 | - } |
|
614 | - |
|
615 | - // maybe horizontal label |
|
616 | - if($args['label_type']=='horizontal'){ |
|
617 | - $output .= '<div class="col-sm-10">'; |
|
618 | - } |
|
619 | - |
|
620 | - // open/type |
|
621 | - $output .= '<select '; |
|
622 | - |
|
623 | - // style |
|
624 | - if($is_select2){ |
|
625 | - $output .= " style='width:100%;' "; |
|
626 | - } |
|
627 | - |
|
628 | - // class |
|
629 | - $class = !empty($args['class']) ? $args['class'] : ''; |
|
630 | - $output .= AUI_Component_Helper::class_attr('custom-select '.$class); |
|
631 | - |
|
632 | - // name |
|
633 | - if(!empty($args['name'])){ |
|
634 | - $output .= AUI_Component_Helper::name($args['name'],$args['multiple']); |
|
635 | - } |
|
636 | - |
|
637 | - // id |
|
638 | - if(!empty($args['id'])){ |
|
639 | - $output .= AUI_Component_Helper::id($args['id']); |
|
640 | - } |
|
641 | - |
|
642 | - // title |
|
643 | - if(!empty($args['title'])){ |
|
644 | - $output .= AUI_Component_Helper::title($args['title']); |
|
645 | - } |
|
646 | - |
|
647 | - // data-attributes |
|
648 | - $output .= AUI_Component_Helper::data_attributes($args); |
|
649 | - |
|
650 | - // aria-attributes |
|
651 | - $output .= AUI_Component_Helper::aria_attributes($args); |
|
652 | - |
|
653 | - // extra attributes |
|
654 | - if(!empty($args['extra_attributes'])){ |
|
655 | - $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
656 | - } |
|
657 | - |
|
658 | - // required |
|
659 | - if(!empty($args['required'])){ |
|
660 | - $output .= ' required '; |
|
661 | - } |
|
662 | - |
|
663 | - // multiple |
|
664 | - if(!empty($args['multiple'])){ |
|
665 | - $output .= ' multiple '; |
|
666 | - } |
|
667 | - |
|
668 | - // close opening tag |
|
669 | - $output .= ' >'; |
|
670 | - |
|
671 | - // placeholder |
|
672 | - if(!empty($args['placeholder']) && !$is_select2){ |
|
673 | - $output .= '<option value="" disabled selected hidden>'.esc_attr($args['placeholder']).'</option>'; |
|
674 | - } |
|
675 | - |
|
676 | - // Options |
|
677 | - if(!empty($args['options'])){ |
|
678 | - foreach($args['options'] as $val => $name){ |
|
679 | - $selected = ''; |
|
680 | - if(is_array($name)){ |
|
681 | - if (isset($name['optgroup']) && ($name['optgroup'] == 'start' || $name['optgroup'] == 'end')) { |
|
682 | - $option_label = isset($name['label']) ? $name['label'] : ''; |
|
683 | - |
|
684 | - $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr($option_label) . '">' : '</optgroup>'; |
|
685 | - } else { |
|
686 | - $option_label = isset($name['label']) ? $name['label'] : ''; |
|
687 | - $option_value = isset($name['value']) ? $name['value'] : ''; |
|
688 | - if(!empty($args['multiple']) && !empty($args['value'])){ |
|
689 | - $selected = in_array($option_value, stripslashes_deep($args['value'])) ? "selected" : ""; |
|
690 | - } elseif(!empty($args['value'])) { |
|
691 | - $selected = selected($option_value,stripslashes_deep($args['value']), false); |
|
692 | - } |
|
693 | - |
|
694 | - $output .= '<option value="' . esc_attr($option_value) . '" ' . $selected . '>' . $option_label . '</option>'; |
|
695 | - } |
|
696 | - }else{ |
|
697 | - if(!empty($args['value'])){ |
|
698 | - if(is_array($args['value'])){ |
|
699 | - $selected = in_array($val,$args['value']) ? 'selected="selected"' : ''; |
|
700 | - } elseif(!empty($args['value'])) { |
|
701 | - $selected = selected( $args['value'], $val, false); |
|
702 | - } |
|
703 | - } |
|
704 | - $output .= '<option value="'.esc_attr($val).'" '.$selected.'>'.esc_attr($name).'</option>'; |
|
705 | - } |
|
706 | - } |
|
707 | - |
|
708 | - } |
|
709 | - |
|
710 | - // closing tag |
|
711 | - $output .= '</select>'; |
|
712 | - |
|
713 | - // help text |
|
714 | - if(!empty($args['help_text'])){ |
|
715 | - $output .= AUI_Component_Helper::help_text($args['help_text']); |
|
716 | - } |
|
717 | - |
|
718 | - // maybe horizontal label |
|
719 | - if($args['label_type']=='horizontal'){ |
|
720 | - $output .= '</div>'; |
|
721 | - } |
|
722 | - |
|
723 | - |
|
724 | - // wrap |
|
725 | - if(!$args['no_wrap']){ |
|
726 | - $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
727 | - $output = self::wrap(array( |
|
728 | - 'content' => $output, |
|
729 | - 'class' => $wrap_class, |
|
730 | - )); |
|
731 | - } |
|
732 | - |
|
733 | - |
|
734 | - return $output; |
|
735 | - } |
|
736 | - |
|
737 | - /** |
|
738 | - * Build the component. |
|
739 | - * |
|
740 | - * @param array $args |
|
741 | - * |
|
742 | - * @return string The rendered component. |
|
743 | - */ |
|
744 | - public static function radio($args = array()){ |
|
745 | - $defaults = array( |
|
746 | - 'class' => '', |
|
747 | - 'id' => '', |
|
748 | - 'title' => '', |
|
749 | - 'horizontal' => false, // sets the lable horizontal |
|
750 | - 'value' => '', |
|
751 | - 'label' => '', |
|
752 | - 'label_class'=> '', |
|
753 | - 'label_type' => '', // sets the lable type, horizontal |
|
754 | - 'inline' => true, |
|
755 | - 'required' => false, |
|
756 | - 'options' => array(), |
|
757 | - 'icon' => '', |
|
758 | - 'no_wrap' => false, |
|
759 | - 'extra_attributes' => array() // an array of extra attributes |
|
760 | - ); |
|
761 | - |
|
762 | - /** |
|
763 | - * Parse incoming $args into an array and merge it with $defaults |
|
764 | - */ |
|
765 | - $args = wp_parse_args( $args, $defaults ); |
|
766 | - |
|
767 | - $label_args = array( |
|
768 | - 'title'=> $args['label'], |
|
769 | - 'class' => $args['label_class']." pt-0 ", |
|
770 | - 'label_type' => $args['label_type'] |
|
771 | - ); |
|
772 | - |
|
773 | - $output = ''; |
|
774 | - |
|
775 | - |
|
776 | - |
|
777 | - // label before |
|
778 | - if(!empty($args['label'])){ |
|
779 | - $output .= self::label( $label_args, 'radio' ); |
|
780 | - } |
|
781 | - |
|
782 | - // maybe horizontal label |
|
783 | - if($args['label_type']=='horizontal'){ |
|
784 | - $output .= '<div class="col-sm-10">'; |
|
785 | - } |
|
786 | - |
|
787 | - if(!empty($args['options'])){ |
|
788 | - $count = 0; |
|
789 | - foreach($args['options'] as $value => $label){ |
|
790 | - $option_args = $args; |
|
791 | - $option_args['value'] = $value; |
|
792 | - $option_args['label'] = $label; |
|
793 | - $option_args['checked'] = $value == $args['value'] ? true : false; |
|
794 | - $output .= self::radio_option($option_args,$count); |
|
795 | - $count++; |
|
796 | - } |
|
797 | - } |
|
798 | - |
|
799 | - // maybe horizontal label |
|
800 | - if($args['label_type']=='horizontal'){ |
|
801 | - $output .= '</div>'; |
|
802 | - } |
|
803 | - |
|
804 | - |
|
805 | - // wrap |
|
806 | - $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
807 | - $output = self::wrap(array( |
|
808 | - 'content' => $output, |
|
809 | - 'class' => $wrap_class, |
|
810 | - )); |
|
811 | - |
|
812 | - |
|
813 | - return $output; |
|
814 | - } |
|
815 | - |
|
816 | - /** |
|
817 | - * Build the component. |
|
818 | - * |
|
819 | - * @param array $args |
|
820 | - * |
|
821 | - * @return string The rendered component. |
|
822 | - */ |
|
823 | - public static function radio_option($args = array(),$count = ''){ |
|
824 | - $defaults = array( |
|
825 | - 'class' => '', |
|
826 | - 'id' => '', |
|
827 | - 'title' => '', |
|
828 | - 'value' => '', |
|
829 | - 'required' => false, |
|
830 | - 'inline' => true, |
|
831 | - 'label' => '', |
|
832 | - 'options' => array(), |
|
833 | - 'icon' => '', |
|
834 | - 'no_wrap' => false, |
|
835 | - 'extra_attributes' => array() // an array of extra attributes |
|
836 | - ); |
|
837 | - |
|
838 | - /** |
|
839 | - * Parse incoming $args into an array and merge it with $defaults |
|
840 | - */ |
|
841 | - $args = wp_parse_args( $args, $defaults ); |
|
842 | - |
|
843 | - $output = ''; |
|
844 | - |
|
845 | - // open/type |
|
846 | - $output .= '<input type="radio"'; |
|
847 | - |
|
848 | - // class |
|
849 | - $output .= ' class="form-check-input" '; |
|
850 | - |
|
851 | - // name |
|
852 | - if(!empty($args['name'])){ |
|
853 | - $output .= AUI_Component_Helper::name($args['name']); |
|
854 | - } |
|
855 | - |
|
856 | - // id |
|
857 | - if(!empty($args['id'])){ |
|
858 | - $output .= AUI_Component_Helper::id($args['id'].$count); |
|
859 | - } |
|
860 | - |
|
861 | - // title |
|
862 | - if(!empty($args['title'])){ |
|
863 | - $output .= AUI_Component_Helper::title($args['title']); |
|
864 | - } |
|
865 | - |
|
866 | - // value |
|
867 | - if(!empty($args['value'])){ |
|
868 | - $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
|
869 | - } |
|
870 | - |
|
871 | - // checked, for radio and checkboxes |
|
872 | - if( $args['checked'] ){ |
|
873 | - $output .= ' checked '; |
|
874 | - } |
|
875 | - |
|
876 | - // data-attributes |
|
877 | - $output .= AUI_Component_Helper::data_attributes($args); |
|
878 | - |
|
879 | - // aria-attributes |
|
880 | - $output .= AUI_Component_Helper::aria_attributes($args); |
|
881 | - |
|
882 | - // extra attributes |
|
883 | - if(!empty($args['extra_attributes'])){ |
|
884 | - $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
885 | - } |
|
886 | - |
|
887 | - // required |
|
888 | - if(!empty($args['required'])){ |
|
889 | - $output .= ' required '; |
|
890 | - } |
|
891 | - |
|
892 | - // close opening tag |
|
893 | - $output .= ' >'; |
|
894 | - |
|
895 | - // label |
|
896 | - if(!empty($args['label']) && is_array($args['label'])){ |
|
897 | - }elseif(!empty($args['label'])){ |
|
898 | - $output .= self::label(array('title'=>$args['label'],'for'=>$args['id'].$count,'class'=>'form-check-label'),'radio'); |
|
899 | - } |
|
900 | - |
|
901 | - // wrap |
|
902 | - if(!$args['no_wrap']){ |
|
903 | - $wrap_class = $args['inline'] ? 'form-check form-check-inline' : 'form-check'; |
|
904 | - $output = self::wrap(array( |
|
905 | - 'content' => $output, |
|
906 | - 'class' => $wrap_class |
|
907 | - )); |
|
908 | - } |
|
909 | - |
|
910 | - |
|
911 | - return $output; |
|
912 | - } |
|
455 | + // maybe horizontal |
|
456 | + if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
|
457 | + $class .= ' col-sm-2 col-form-label'; |
|
458 | + } |
|
459 | + |
|
460 | + // open |
|
461 | + $output .= '<label '; |
|
462 | + |
|
463 | + // for |
|
464 | + if(!empty($args['for'])){ |
|
465 | + $output .= ' for="'.sanitize_text_field($args['for']).'" '; |
|
466 | + } |
|
467 | + |
|
468 | + // class |
|
469 | + $output .= ' class="'.$class.'" '; |
|
470 | + |
|
471 | + // close |
|
472 | + $output .= '>'; |
|
473 | + |
|
474 | + |
|
475 | + // title, don't escape fully as can contain html |
|
476 | + if(!empty($args['title'])){ |
|
477 | + $output .= wp_kses_post($args['title']); |
|
478 | + } |
|
479 | + |
|
480 | + // close wrap |
|
481 | + $output .= '</label>'; |
|
482 | + |
|
483 | + |
|
484 | + } |
|
485 | + |
|
486 | + |
|
487 | + return $output; |
|
488 | + } |
|
489 | + |
|
490 | + public static function wrap($args = array()){ |
|
491 | + $defaults = array( |
|
492 | + 'type' => 'div', |
|
493 | + 'class' => 'form-group', |
|
494 | + 'content' => '', |
|
495 | + 'input_group_left' => '', |
|
496 | + 'input_group_right' => '', |
|
497 | + 'input_group_left_inside' => false, |
|
498 | + 'input_group_right_inside' => false, |
|
499 | + ); |
|
500 | + |
|
501 | + /** |
|
502 | + * Parse incoming $args into an array and merge it with $defaults |
|
503 | + */ |
|
504 | + $args = wp_parse_args( $args, $defaults ); |
|
505 | + $output = ''; |
|
506 | + if($args['type']){ |
|
507 | + |
|
508 | + // open |
|
509 | + $output .= '<'.sanitize_html_class($args['type']); |
|
510 | + |
|
511 | + // class |
|
512 | + $class = !empty($args['class']) ? $args['class'] : ''; |
|
513 | + $output .= ' class="'.$class.'" '; |
|
514 | + |
|
515 | + // close wrap |
|
516 | + $output .= ' >'; |
|
517 | + |
|
518 | + |
|
519 | + // Input group left |
|
520 | + if(!empty($args['input_group_left'])){ |
|
521 | + $position_class = !empty($args['input_group_left_inside']) ? 'position-absolute' : ''; |
|
522 | + $input_group_left = strpos($args['input_group_left'], '<') !== false ? $args['input_group_left'] : '<span class="input-group-text">'.$args['input_group_left'].'</span>'; |
|
523 | + $output .= '<div class="input-group-prepend '.$position_class.'">'.$input_group_left.'</div>'; |
|
524 | + } |
|
525 | + |
|
526 | + // content |
|
527 | + $output .= $args['content']; |
|
528 | + |
|
529 | + // Input group right |
|
530 | + if(!empty($args['input_group_right'])){ |
|
531 | + $position_class = !empty($args['input_group_left_inside']) ? 'position-absolute' : ''; |
|
532 | + $input_group_right = strpos($args['input_group_right'], '<') !== false ? $args['input_group_right'] : '<span class="input-group-text">'.$args['input_group_right'].'</span>'; |
|
533 | + $output .= '<div class="input-group-append '.$position_class.'">'.$input_group_right.'</div>'; |
|
534 | + } |
|
535 | + |
|
536 | + |
|
537 | + // close wrap |
|
538 | + $output .= '</'.sanitize_html_class($args['type']).'>'; |
|
539 | + |
|
540 | + |
|
541 | + }else{ |
|
542 | + $output = $args['content']; |
|
543 | + } |
|
544 | + |
|
545 | + return $output; |
|
546 | + } |
|
547 | + |
|
548 | + /** |
|
549 | + * Build the component. |
|
550 | + * |
|
551 | + * @param array $args |
|
552 | + * |
|
553 | + * @return string The rendered component. |
|
554 | + */ |
|
555 | + public static function select($args = array()){ |
|
556 | + $defaults = array( |
|
557 | + 'class' => '', |
|
558 | + 'id' => '', |
|
559 | + 'title' => '', |
|
560 | + 'value' => '', // can be an array or a string |
|
561 | + 'required' => false, |
|
562 | + 'label' => '', |
|
563 | + 'label_class' => '', |
|
564 | + 'label_type' => '', // sets the lable type, horizontal |
|
565 | + 'help_text' => '', |
|
566 | + 'placeholder'=> '', |
|
567 | + 'options' => array(), |
|
568 | + 'icon' => '', |
|
569 | + 'multiple' => false, |
|
570 | + 'select2' => false, |
|
571 | + 'no_wrap' => false, |
|
572 | + 'extra_attributes' => array() // an array of extra attributes |
|
573 | + ); |
|
574 | + |
|
575 | + /** |
|
576 | + * Parse incoming $args into an array and merge it with $defaults |
|
577 | + */ |
|
578 | + $args = wp_parse_args( $args, $defaults ); |
|
579 | + $output = ''; |
|
580 | + |
|
581 | + // Maybe setup select2 |
|
582 | + $is_select2 = false; |
|
583 | + if(!empty($args['select2'])){ |
|
584 | + $args['class'] .= ' aui-select2'; |
|
585 | + $is_select2 = true; |
|
586 | + }elseif( strpos($args['class'], 'aui-select2') !== false){ |
|
587 | + $is_select2 = true; |
|
588 | + } |
|
589 | + |
|
590 | + // select2 tags |
|
591 | + if( !empty($args['select2']) && $args['select2'] === 'tags'){ // triple equlas needed here for some reason |
|
592 | + $args['data-tags'] = 'true'; |
|
593 | + $args['data-token-separators'] = "[',']"; |
|
594 | + $args['multiple'] = true; |
|
595 | + } |
|
596 | + |
|
597 | + // select2 placeholder |
|
598 | + if($is_select2 && !empty($args['placeholder']) && empty($args['data-placeholder'])){ |
|
599 | + $args['data-placeholder'] = esc_attr($args['placeholder']); |
|
600 | + $args['data-allow-clear'] = empty($args['data-allow-clear']) ? true : esc_attr($args['data-allow-clear']); |
|
601 | + } |
|
602 | + |
|
603 | + // label |
|
604 | + if(!empty($args['label']) && is_array($args['label'])){ |
|
605 | + }elseif(!empty($args['label'])){ |
|
606 | + $label_args = array( |
|
607 | + 'title'=> $args['label'], |
|
608 | + 'for'=> $args['id'], |
|
609 | + 'class' => $args['label_class']." ", |
|
610 | + 'label_type' => $args['label_type'] |
|
611 | + ); |
|
612 | + $output .= self::label($label_args); |
|
613 | + } |
|
614 | + |
|
615 | + // maybe horizontal label |
|
616 | + if($args['label_type']=='horizontal'){ |
|
617 | + $output .= '<div class="col-sm-10">'; |
|
618 | + } |
|
619 | + |
|
620 | + // open/type |
|
621 | + $output .= '<select '; |
|
622 | + |
|
623 | + // style |
|
624 | + if($is_select2){ |
|
625 | + $output .= " style='width:100%;' "; |
|
626 | + } |
|
627 | + |
|
628 | + // class |
|
629 | + $class = !empty($args['class']) ? $args['class'] : ''; |
|
630 | + $output .= AUI_Component_Helper::class_attr('custom-select '.$class); |
|
631 | + |
|
632 | + // name |
|
633 | + if(!empty($args['name'])){ |
|
634 | + $output .= AUI_Component_Helper::name($args['name'],$args['multiple']); |
|
635 | + } |
|
636 | + |
|
637 | + // id |
|
638 | + if(!empty($args['id'])){ |
|
639 | + $output .= AUI_Component_Helper::id($args['id']); |
|
640 | + } |
|
641 | + |
|
642 | + // title |
|
643 | + if(!empty($args['title'])){ |
|
644 | + $output .= AUI_Component_Helper::title($args['title']); |
|
645 | + } |
|
646 | + |
|
647 | + // data-attributes |
|
648 | + $output .= AUI_Component_Helper::data_attributes($args); |
|
649 | + |
|
650 | + // aria-attributes |
|
651 | + $output .= AUI_Component_Helper::aria_attributes($args); |
|
652 | + |
|
653 | + // extra attributes |
|
654 | + if(!empty($args['extra_attributes'])){ |
|
655 | + $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
656 | + } |
|
657 | + |
|
658 | + // required |
|
659 | + if(!empty($args['required'])){ |
|
660 | + $output .= ' required '; |
|
661 | + } |
|
662 | + |
|
663 | + // multiple |
|
664 | + if(!empty($args['multiple'])){ |
|
665 | + $output .= ' multiple '; |
|
666 | + } |
|
667 | + |
|
668 | + // close opening tag |
|
669 | + $output .= ' >'; |
|
670 | + |
|
671 | + // placeholder |
|
672 | + if(!empty($args['placeholder']) && !$is_select2){ |
|
673 | + $output .= '<option value="" disabled selected hidden>'.esc_attr($args['placeholder']).'</option>'; |
|
674 | + } |
|
675 | + |
|
676 | + // Options |
|
677 | + if(!empty($args['options'])){ |
|
678 | + foreach($args['options'] as $val => $name){ |
|
679 | + $selected = ''; |
|
680 | + if(is_array($name)){ |
|
681 | + if (isset($name['optgroup']) && ($name['optgroup'] == 'start' || $name['optgroup'] == 'end')) { |
|
682 | + $option_label = isset($name['label']) ? $name['label'] : ''; |
|
683 | + |
|
684 | + $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr($option_label) . '">' : '</optgroup>'; |
|
685 | + } else { |
|
686 | + $option_label = isset($name['label']) ? $name['label'] : ''; |
|
687 | + $option_value = isset($name['value']) ? $name['value'] : ''; |
|
688 | + if(!empty($args['multiple']) && !empty($args['value'])){ |
|
689 | + $selected = in_array($option_value, stripslashes_deep($args['value'])) ? "selected" : ""; |
|
690 | + } elseif(!empty($args['value'])) { |
|
691 | + $selected = selected($option_value,stripslashes_deep($args['value']), false); |
|
692 | + } |
|
693 | + |
|
694 | + $output .= '<option value="' . esc_attr($option_value) . '" ' . $selected . '>' . $option_label . '</option>'; |
|
695 | + } |
|
696 | + }else{ |
|
697 | + if(!empty($args['value'])){ |
|
698 | + if(is_array($args['value'])){ |
|
699 | + $selected = in_array($val,$args['value']) ? 'selected="selected"' : ''; |
|
700 | + } elseif(!empty($args['value'])) { |
|
701 | + $selected = selected( $args['value'], $val, false); |
|
702 | + } |
|
703 | + } |
|
704 | + $output .= '<option value="'.esc_attr($val).'" '.$selected.'>'.esc_attr($name).'</option>'; |
|
705 | + } |
|
706 | + } |
|
707 | + |
|
708 | + } |
|
709 | + |
|
710 | + // closing tag |
|
711 | + $output .= '</select>'; |
|
712 | + |
|
713 | + // help text |
|
714 | + if(!empty($args['help_text'])){ |
|
715 | + $output .= AUI_Component_Helper::help_text($args['help_text']); |
|
716 | + } |
|
717 | + |
|
718 | + // maybe horizontal label |
|
719 | + if($args['label_type']=='horizontal'){ |
|
720 | + $output .= '</div>'; |
|
721 | + } |
|
722 | + |
|
723 | + |
|
724 | + // wrap |
|
725 | + if(!$args['no_wrap']){ |
|
726 | + $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
727 | + $output = self::wrap(array( |
|
728 | + 'content' => $output, |
|
729 | + 'class' => $wrap_class, |
|
730 | + )); |
|
731 | + } |
|
732 | + |
|
733 | + |
|
734 | + return $output; |
|
735 | + } |
|
736 | + |
|
737 | + /** |
|
738 | + * Build the component. |
|
739 | + * |
|
740 | + * @param array $args |
|
741 | + * |
|
742 | + * @return string The rendered component. |
|
743 | + */ |
|
744 | + public static function radio($args = array()){ |
|
745 | + $defaults = array( |
|
746 | + 'class' => '', |
|
747 | + 'id' => '', |
|
748 | + 'title' => '', |
|
749 | + 'horizontal' => false, // sets the lable horizontal |
|
750 | + 'value' => '', |
|
751 | + 'label' => '', |
|
752 | + 'label_class'=> '', |
|
753 | + 'label_type' => '', // sets the lable type, horizontal |
|
754 | + 'inline' => true, |
|
755 | + 'required' => false, |
|
756 | + 'options' => array(), |
|
757 | + 'icon' => '', |
|
758 | + 'no_wrap' => false, |
|
759 | + 'extra_attributes' => array() // an array of extra attributes |
|
760 | + ); |
|
761 | + |
|
762 | + /** |
|
763 | + * Parse incoming $args into an array and merge it with $defaults |
|
764 | + */ |
|
765 | + $args = wp_parse_args( $args, $defaults ); |
|
766 | + |
|
767 | + $label_args = array( |
|
768 | + 'title'=> $args['label'], |
|
769 | + 'class' => $args['label_class']." pt-0 ", |
|
770 | + 'label_type' => $args['label_type'] |
|
771 | + ); |
|
772 | + |
|
773 | + $output = ''; |
|
774 | + |
|
775 | + |
|
776 | + |
|
777 | + // label before |
|
778 | + if(!empty($args['label'])){ |
|
779 | + $output .= self::label( $label_args, 'radio' ); |
|
780 | + } |
|
781 | + |
|
782 | + // maybe horizontal label |
|
783 | + if($args['label_type']=='horizontal'){ |
|
784 | + $output .= '<div class="col-sm-10">'; |
|
785 | + } |
|
786 | + |
|
787 | + if(!empty($args['options'])){ |
|
788 | + $count = 0; |
|
789 | + foreach($args['options'] as $value => $label){ |
|
790 | + $option_args = $args; |
|
791 | + $option_args['value'] = $value; |
|
792 | + $option_args['label'] = $label; |
|
793 | + $option_args['checked'] = $value == $args['value'] ? true : false; |
|
794 | + $output .= self::radio_option($option_args,$count); |
|
795 | + $count++; |
|
796 | + } |
|
797 | + } |
|
798 | + |
|
799 | + // maybe horizontal label |
|
800 | + if($args['label_type']=='horizontal'){ |
|
801 | + $output .= '</div>'; |
|
802 | + } |
|
803 | + |
|
804 | + |
|
805 | + // wrap |
|
806 | + $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
807 | + $output = self::wrap(array( |
|
808 | + 'content' => $output, |
|
809 | + 'class' => $wrap_class, |
|
810 | + )); |
|
811 | + |
|
812 | + |
|
813 | + return $output; |
|
814 | + } |
|
815 | + |
|
816 | + /** |
|
817 | + * Build the component. |
|
818 | + * |
|
819 | + * @param array $args |
|
820 | + * |
|
821 | + * @return string The rendered component. |
|
822 | + */ |
|
823 | + public static function radio_option($args = array(),$count = ''){ |
|
824 | + $defaults = array( |
|
825 | + 'class' => '', |
|
826 | + 'id' => '', |
|
827 | + 'title' => '', |
|
828 | + 'value' => '', |
|
829 | + 'required' => false, |
|
830 | + 'inline' => true, |
|
831 | + 'label' => '', |
|
832 | + 'options' => array(), |
|
833 | + 'icon' => '', |
|
834 | + 'no_wrap' => false, |
|
835 | + 'extra_attributes' => array() // an array of extra attributes |
|
836 | + ); |
|
837 | + |
|
838 | + /** |
|
839 | + * Parse incoming $args into an array and merge it with $defaults |
|
840 | + */ |
|
841 | + $args = wp_parse_args( $args, $defaults ); |
|
842 | + |
|
843 | + $output = ''; |
|
844 | + |
|
845 | + // open/type |
|
846 | + $output .= '<input type="radio"'; |
|
847 | + |
|
848 | + // class |
|
849 | + $output .= ' class="form-check-input" '; |
|
850 | + |
|
851 | + // name |
|
852 | + if(!empty($args['name'])){ |
|
853 | + $output .= AUI_Component_Helper::name($args['name']); |
|
854 | + } |
|
855 | + |
|
856 | + // id |
|
857 | + if(!empty($args['id'])){ |
|
858 | + $output .= AUI_Component_Helper::id($args['id'].$count); |
|
859 | + } |
|
860 | + |
|
861 | + // title |
|
862 | + if(!empty($args['title'])){ |
|
863 | + $output .= AUI_Component_Helper::title($args['title']); |
|
864 | + } |
|
865 | + |
|
866 | + // value |
|
867 | + if(!empty($args['value'])){ |
|
868 | + $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
|
869 | + } |
|
870 | + |
|
871 | + // checked, for radio and checkboxes |
|
872 | + if( $args['checked'] ){ |
|
873 | + $output .= ' checked '; |
|
874 | + } |
|
875 | + |
|
876 | + // data-attributes |
|
877 | + $output .= AUI_Component_Helper::data_attributes($args); |
|
878 | + |
|
879 | + // aria-attributes |
|
880 | + $output .= AUI_Component_Helper::aria_attributes($args); |
|
881 | + |
|
882 | + // extra attributes |
|
883 | + if(!empty($args['extra_attributes'])){ |
|
884 | + $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
885 | + } |
|
886 | + |
|
887 | + // required |
|
888 | + if(!empty($args['required'])){ |
|
889 | + $output .= ' required '; |
|
890 | + } |
|
891 | + |
|
892 | + // close opening tag |
|
893 | + $output .= ' >'; |
|
894 | + |
|
895 | + // label |
|
896 | + if(!empty($args['label']) && is_array($args['label'])){ |
|
897 | + }elseif(!empty($args['label'])){ |
|
898 | + $output .= self::label(array('title'=>$args['label'],'for'=>$args['id'].$count,'class'=>'form-check-label'),'radio'); |
|
899 | + } |
|
900 | + |
|
901 | + // wrap |
|
902 | + if(!$args['no_wrap']){ |
|
903 | + $wrap_class = $args['inline'] ? 'form-check form-check-inline' : 'form-check'; |
|
904 | + $output = self::wrap(array( |
|
905 | + 'content' => $output, |
|
906 | + 'class' => $wrap_class |
|
907 | + )); |
|
908 | + } |
|
909 | + |
|
910 | + |
|
911 | + return $output; |
|
912 | + } |
|
913 | 913 | |
914 | 914 | } |
915 | 915 | \ No newline at end of file |