PluginProbe
PostNL for WooCommerce / 4.4.0
PostNL for WooCommerce v4.4.0
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / admin / settings / class-wcpn-settings-callbacks.php

class-wcpn-settings-callbacks.php in PostNL for WooCommerce 4.4.0, at includes/admin/settings/class-wcpn-settings-callbacks.php

181 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use WPO\WC\PostNL\Entity\SettingsFieldArguments;
4
5 if (! defined('ABSPATH')) {
6 exit;
7 } // Exit if accessed directly
8
9 if (class_exists('WCPN_Settings_Callbacks')) {
10 return new WCPN_Settings_Callbacks();
11 }
12
13 class WCPN_Settings_Callbacks
14 {
15 /**
16 * Validate options.
17 *
18 * @param array $input options to valid.
19 *
20 * @return array validated options.
21 */
22 public function validate($input)
23 {
24 // Create our array for storing the validated options.
25 $output = [];
26
27 if (empty($input) || ! is_array($input)) {
28 return $input;
29 }
30
31 // Loop through each of the incoming options.
32 foreach ($input as $key => $value) {
33 // Check to see if the current option has a value. If so, process it.
34 if (isset($input[$key])) {
35 if (is_array($input[$key])) {
36 foreach ($input[$key] as $sub_key => $sub_value) {
37 $output[$key][$sub_key] = $input[$key][$sub_key];
38 }
39 } else {
40 $output[$key] = $input[$key];
41 }
42 }
43 }
44
45 // Return the array processing any additional functions filtered by this action.
46 return apply_filters('wcpn_settings_validate_input', $input, $input);
47 }
48
49 /**
50 * @param \WPO\WC\PostNL\Entity\SettingsFieldArguments|array $args
51 *
52 * @throws \Exception
53 */
54 public static function enhanced_select($args): void
55 {
56 if (is_array($args)) {
57 $args = new SettingsFieldArguments($args);
58 }
59
60 include("class-wcpn-settings-callbacks-enhanced-select.php");
61 new WCPN_Settings_Callbacks_Enhanced_Select($args);
62 }
63
64 /**
65 * @param array $args
66 */
67 public static function renderSection(array $args): void
68 {
69 if (isset($args["description"])) {
70 echo "<p>{$args["description"]}</p>";
71 }
72 }
73
74 /**
75 * Output a WooCommerce style form field.
76 *
77 * @param SettingsFieldArguments $class
78 */
79 public static function renderField(SettingsFieldArguments $class): void
80 {
81 $arguments = $class->getArguments();
82 $attributes = $class->getCustomAttributes();
83
84 if (isset($arguments["description"])) {
85 $description = $arguments["description"];
86 unset ($arguments["description"]);
87 }
88
89 if (isset($attributes['data-type']) && $attributes['data-type'] === 'toggle') {
90 self::renderToggle($class);
91 } else {
92 woocommerce_form_field(
93 $class->getName(),
94 $arguments,
95 $class->getValue()
96 );
97 }
98
99 if (isset($arguments["append"])) {
100 echo $arguments["append"];
101 }
102
103 // Render the description here instead of inside the above function.
104 if (isset($description)) {
105 WCPN_Settings_Callbacks::renderDescription($description);
106 }
107 }
108
109 /**
110 * Get the order statuses as options array.
111 *
112 * @return array
113 */
114 public static function get_order_status_options(): array
115 {
116 $order_statuses = [];
117
118 if (version_compare(WOOCOMMERCE_VERSION, '2.2', '<')) {
119 $statuses = (array) get_terms('shop_order_status', ['hide_empty' => 0, 'orderby' => 'id']);
120 foreach ($statuses as $status) {
121 $order_statuses[esc_attr($status->slug)] = esc_html__($status->name, 'woocommerce');
122 }
123 } else {
124 $statuses = wc_get_order_statuses();
125 foreach ($statuses as $status_slug => $status) {
126 $status_slug = 'wc-' === substr($status_slug, 0, 3) ? substr($status_slug, 3) : $status_slug;
127
128 $order_statuses[$status_slug] = $status;
129 }
130 }
131
132 return $order_statuses;
133 }
134
135 /**
136 * @param $description
137 */
138 private static function renderDescription($description): void
139 {
140 echo "<p class=\"description\">$description</p>";
141 }
142
143 /**
144 * Render a custom toggle element. Uses classes from WooCommerce but has a custom JS implementation.
145 *
146 * @param \WPO\WC\PostNL\Entity\SettingsFieldArguments $class
147 */
148 private static function renderToggle(SettingsFieldArguments $class): void
149 {
150 $arguments = $class->getArguments();
151 $arguments['type'] = ['hidden'];
152 $arguments['input_class'] = ['wcpn__input--toggle'];
153 unset($arguments['description']);
154
155 echo '<a class="wcpn__toggle wcpn__d--inline-block">';
156
157 printf(
158 '<input type="hidden" name="%s" value="%s" %s>',
159 $class->getName(),
160 $class->getValue(),
161 $class->getCustomAttributesAsString()
162 );
163
164 if (wc_string_to_bool($class->getValue())) {
165 printf(
166 "<span class=\"woocommerce-input-toggle woocommerce-input-toggle--enabled\">%s</span>",
167 esc_attr__('Yes', 'woocommerce')
168 );
169 } else {
170 printf(
171 "<span class=\"woocommerce-input-toggle woocommerce-input-toggle--disabled\">%s</span>",
172 esc_attr__('No', 'woocommerce')
173 );
174 }
175
176 echo "</a>";
177 }
178 }
179
180 return new WCPN_Settings_Callbacks();
181