PluginProbe
PostNL for WooCommerce / 4.0.0
PostNL for WooCommerce v4.0.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-wcpost-settings.php

class-wcpost-settings.php in PostNL for WooCommerce 4.0.0, at includes/admin/settings/class-wcpost-settings.php

367 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MyParcelNL\Sdk\src\Model\Consignment\PostNLConsignment;
4 use MyParcelNL\Sdk\src\Support\Arr;
5
6 if (! defined('ABSPATH')) {
7 exit;
8 } // Exit if accessed directly
9
10 if (class_exists('WCPOST_Settings')) {
11 return new WCPOST_Settings();
12 }
13
14 /**
15 * Create & render settings page
16 */
17 class WCPOST_Settings
18 {
19 public const SETTINGS_MENU_SLUG = "wcpn_settings";
20
21 public const SETTINGS_GENERAL = "general";
22 public const SETTINGS_CHECKOUT = "checkout";
23 public const SETTINGS_EXPORT_DEFAULTS = "export_defaults";
24 public const SETTINGS_POSTNL = PostNLConsignment::CARRIER_NAME;
25
26 /**
27 * General
28 */
29 public const SETTING_API_KEY = "api_key";
30 public const SETTING_AUTOMATIC_ORDER_STATUS = "automatic_order_status";
31 public const SETTING_BARCODE_IN_NOTE = "barcode_in_note";
32 public const SETTING_BARCODE_IN_NOTE_TITLE = "barcode_in_note_title";
33 public const SETTING_DOWNLOAD_DISPLAY = "download_display";
34 public const SETTING_ERROR_LOGGING = "error_logging";
35 public const SETTING_LABEL_FORMAT = "label_format";
36 public const SETTING_ORDER_STATUS_AUTOMATION = "order_status_automation";
37 public const SETTING_ASK_FOR_PRINT_POSITION = "ask_for_print_position";
38 public const SETTING_PROCESS_DIRECTLY = "process_directly";
39 public const SETTING_TRACK_TRACE_EMAIL = "track_trace_email";
40 public const SETTING_TRACK_TRACE_MY_ACCOUNT = "track_trace_my_account";
41
42 /**
43 * Export defaults
44 */
45 public const SETTING_SHIPPING_METHODS_PACKAGE_TYPES = "shipping_methods_package_types";
46 public const SETTING_CONNECT_PHONE = "connect_phone";
47 public const SETTING_LABEL_DESCRIPTION = "label_description";
48 public const SETTING_EMPTY_PARCEL_WEIGHT = "empty_parcel_weight";
49 public const SETTING_HS_CODE = "hs_code";
50 public const SETTING_PACKAGE_CONTENT = "package_contents";
51 public const SETTING_COUNTRY_OF_ORIGIN = "country_of_origin";
52
53 /**
54 * Checkout
55 */
56 public const SETTING_DELIVERY_OPTIONS_CUSTOM_CSS = "delivery_options_custom_css";
57 public const SETTING_DELIVERY_OPTIONS_DISPLAY = "delivery_options_display";
58 public const SETTING_DELIVERY_OPTIONS_ENABLED = "delivery_options_enabled";
59 public const SETTING_DELIVERY_OPTIONS_POSITION = "delivery_options_position";
60 public const SETTING_SHOW_DELIVERY_DAY = "show_delivery_day";
61 public const SETTING_DELIVERY_TITLE = "delivery_title";
62 public const SETTING_HEADER_DELIVERY_OPTIONS_TITLE = "header_delivery_options_title";
63 public const SETTING_PICKUP_TITLE = "pickup_title";
64 public const SETTING_MORNING_DELIVERY_TITLE = "morning_title";
65 public const SETTING_EVENING_DELIVERY_TITLE = "evening_title";
66 public const SETTING_ONLY_RECIPIENT_TITLE = "only_recipient_title";
67 public const SETTING_SIGNATURE_TITLE = "signature_title";
68 public const SETTING_STANDARD_TITLE = "standard_title";
69 public const SETTING_USE_SPLIT_ADDRESS_FIELDS = "use_split_address_fields";
70
71 /*
72 * Carrier settings, these will be prefixed with carrier names.
73 *
74 * e.g. cutoff_time => postnl_cutoff_time
75 */
76 // Defaults
77 public const SETTING_CARRIER_DEFAULT_EXPORT_SIGNATURE = "export_signature";
78 public const SETTING_CARRIER_DEFAULT_EXPORT_ONLY_RECIPIENT = "export_only_recipient";
79 public const SETTING_CARRIER_DEFAULT_EXPORT_AGE_CHECK = "export_age_check";
80 public const SETTING_CARRIER_DEFAULT_EXPORT_RETURN = "export_return_shipments";
81 public const SETTING_CARRIER_DEFAULT_EXPORT_INSURED = "export_insured";
82 public const SETTING_CARRIER_DEFAULT_EXPORT_INSURED_AMOUNT = "export_insured_amount";
83 public const SETTING_CARRIER_DEFAULT_EXPORT_INSURED_FROM_PRICE = "export_insured_from_price";
84
85
86 // Delivery options settings
87 public const SETTING_CARRIER_CUTOFF_TIME = "cutoff_time";
88 public const SETTING_CARRIER_DELIVERY_DAYS_WINDOW = "delivery_days_window";
89 public const SETTING_CARRIER_DELIVERY_ENABLED = "delivery_enabled";
90 public const SETTING_CARRIER_DROP_OFF_DAYS = "drop_off_days";
91 public const SETTING_CARRIER_DROP_OFF_DELAY = "drop_off_delay";
92 public const SETTING_CARRIER_PICKUP_ENABLED = "pickup_enabled";
93 public const SETTING_CARRIER_PICKUP_FEE = "pickup_fee";
94 public const SETTING_CARRIER_PICKUP_TITLE = "pickup_title";
95 public const SETTING_CARRIER_ONLY_RECIPIENT_ENABLED = "only_recipient_enabled";
96 public const SETTING_CARRIER_ONLY_RECIPIENT_FEE = "only_recipient_fee";
97 public const SETTING_CARRIER_SIGNATURE_ENABLED = "signature_enabled";
98 public const SETTING_CARRIER_SIGNATURE_FEE = "signature_fee";
99 public const SETTING_CARRIER_DELIVERY_MORNING_ENABLED = "delivery_morning_enabled";
100 public const SETTING_CARRIER_DELIVERY_MORNING_FEE = "delivery_morning_fee";
101 public const SETTING_CARRIER_DELIVERY_EVENING_ENABLED = "delivery_evening_enabled";
102 public const SETTING_CARRIER_DELIVERY_EVENING_FEE = "delivery_evening_fee";
103
104 // Saturday delivery
105 // TODO; Currently not implemented
106 public const SETTING_CARRIER_FRIDAY_CUTOFF_TIME = "friday_cutoff_time";
107 public const SETTING_CARRIER_SATURDAY_DELIVERY_ENABLED = "saturday_delivery_enabled";
108 public const SETTING_CARRIER_SATURDAY_DELIVERY_FEE = "saturday_delivery_fee";
109
110 // Monday delivery
111 public const SETTING_CARRIER_MONDAY_DELIVERY_ENABLED = "monday_delivery_enabled";
112 public const SETTING_CARRIER_MONDAY_DELIVERY_FEE = "monday_delivery_fee";
113 public const SETTING_CARRIER_SATURDAY_CUTOFF_TIME = "saturday_cutoff_time";
114
115 public function __construct()
116 {
117 add_action("admin_menu", [$this, "menu"]);
118 add_filter(
119 "plugin_action_links_" . WCPOST()->plugin_basename,
120 [
121 $this,
122 "add_settings_link",
123 ]
124 );
125
126 /**
127 * Add the new screen to the woocommerce screen ids to make wc tooltips work.
128 */
129 add_filter(
130 "woocommerce_screen_ids",
131 function($ids) {
132 $ids[] = "woocommerce_page_" . self::SETTINGS_MENU_SLUG;
133
134 return $ids;
135 }
136 );
137
138 // Create the admin settings
139 require_once("class-wcpn-settings-data.php");
140
141 // notice for WooCommerce PostNL plugin
142 add_action("woocommerce_postnl_before_settings_page", [$this, "postnl_country_notice"], 10, 1);
143 }
144
145 /**
146 * Add settings item to WooCommerce menu
147 */
148 public function menu()
149 {
150 add_submenu_page(
151 "woocommerce",
152 __("PostNL", "woocommerce-postnl"),
153 __("PostNL", "woocommerce-postnl"),
154 "manage_options",
155 self::SETTINGS_MENU_SLUG,
156 [$this, "settings_page"]
157 );
158 }
159
160 /**
161 * Add settings link to plugins page
162 *
163 * @param array $links
164 *
165 * @return array
166 */
167 public function add_settings_link(array $links): array
168 {
169 $url = admin_url("admin.php?page=" . self::SETTINGS_MENU_SLUG);
170
171 array_push(
172 $links,
173 sprintf(
174 '<a href="%s">%s</a>',
175 $url,
176 __("Settings", "woocommerce-postnl")
177 )
178 );
179
180 return $links;
181 }
182
183 /**
184 * Output the settings pages.
185 */
186 public function settings_page()
187 {
188 $settings_tabs = apply_filters(
189 self::SETTINGS_MENU_SLUG . "_tabs",
190 WCPN_Settings_Data::getTabs()
191 );
192
193 $active_tab = isset($_GET["tab"]) ? $_GET["tab"] : self::SETTINGS_GENERAL;
194 ?>
195 <div class="wrap woocommerce">
196 <h1><?php _e("WooCommerce PostNL Settings", "woocommerce-postnl"); ?></h1>
197 <h2 class="nav-tab-wrapper">
198 <?php
199 foreach ($settings_tabs as $tab_slug => $tab_title) :
200 printf(
201 '<a href="?page='
202 . self::SETTINGS_MENU_SLUG
203 . '&tab=%1$s" class="nav-tab nav-tab-%1$s %2$s">%3$s</a>',
204 $tab_slug,
205 (($active_tab === $tab_slug) ? "nav-tab-active" : ""),
206 $tab_title
207 );
208 endforeach;
209 ?>
210 </h2>
211 <?php do_action("woocommerce_postnl_before_settings_page", $active_tab); ?>
212 <form
213 method="post"
214 action="options.php"
215 id="<?php echo self::SETTINGS_MENU_SLUG; ?>">
216 <?php
217 do_action("woocommerce_postnl_before_settings", $active_tab);
218 settings_fields(self::getOptionId($active_tab));
219 $this->render_settings_sections(self::getOptionId($active_tab));
220 do_action("woocommerce_postnl_after_settings", $active_tab);
221
222 submit_button();
223 ?>
224 </form>
225 <?php do_action("woocommerce_postnl_after_settings_page", $active_tab); ?>
226 </div>
227 <?php
228 }
229
230 /**
231 * Show the user a notice if they might be using the wrong plugin.
232 */
233 public function postnl_country_notice()
234 {
235 $base_country = WC()->countries->get_base_country();
236
237 // save or check option to hide notice
238 if (Arr::get($_GET, "postnl_hide_be_notice")) {
239 update_option("postnl_hide_be_notice", true);
240 $hide_notice = true;
241 } else {
242 $hide_notice = get_option("postnl_hide_be_notice");
243 }
244
245 // link to hide message when one of the premium extensions is installed
246 if (! $hide_notice && $base_country === "BE") {
247 $postnl_nl_link =
248 '<a href="https://wordpress.org/plugins/woocommerce-postnl/" target="blank">WC PostNL Netherlands</a>';
249 $text = sprintf(
250 __(
251 "It looks like your shop is based in Netherlands. This plugin is for PostNL. If you are using PostNL Netherlands, download the %s plugin instead!",
252 "woocommerce-postnl"
253 ),
254 $postnl_nl_link
255 );
256 $dismiss_button = sprintf(
257 '<a href="%s" style="display:inline-block; margin-top: 10px;">%s</a>',
258 add_query_arg('postnl_hide_be_notice', 'true'),
259 __("Hide this message", "woocommerce-postnl")
260 );
261 printf('<div class="notice notice-warning"><p>%s %s</p></div>', $text, $dismiss_button);
262 }
263 }
264
265 /**
266 * @param string $option
267 *
268 * @return string
269 */
270 public static function getOptionId(string $option)
271 {
272 return "woocommerce_postnl_{$option}_settings";
273 }
274
275 /**
276 * Render the settings sections. Mostly taken from the WordPress equivalent but done like this so parts can
277 * be overridden/changed easily.
278 *
279 * @param string $page - Page ID
280 *
281 * @see \do_settings_sections
282 */
283 private function render_settings_sections(string $page)
284 {
285 global $wp_settings_sections, $wp_settings_fields;
286
287 if (! isset($wp_settings_sections[$page])) {
288 return;
289 }
290
291 foreach ((array) $wp_settings_sections[$page] as $section) {
292 echo '<div class="wcpn__settings-section">';
293 $id = Arr::get($section, "id");
294 $title = Arr::get($section, "title");
295 $callback = Arr::get($section, "callback");
296
297 if ($title) {
298 printf('<h2 id="%s">%s</h2>', $id, $title);
299 }
300
301 if ($callback) {
302 call_user_func($callback, $section);
303 }
304
305 if (! isset($wp_settings_fields)
306 || ! isset($wp_settings_fields[$page])
307 || ! isset($wp_settings_fields[$page][$id])) {
308 continue;
309 }
310 echo '<table class="form-table" role="presentation">';
311 $this->render_settings_fields($page, $id);
312 echo "</table>";
313 echo "</div>";
314 }
315 }
316
317 /**
318 * Mostly copied from the WordPress function.
319 *
320 * @param $page
321 * @param $section
322 *
323 * @see \do_settings_fields
324 */
325 private function render_settings_fields($page, $section): void
326 {
327 global $wp_settings_fields;
328
329 if (! Arr::get($wp_settings_fields, "$page.$section")) {
330 return;
331 }
332
333 foreach (Arr::get($wp_settings_fields, "$page.$section") as $field) {
334 $class = Arr::get($field, "args.class") ?? "";
335
336 if ($class) {
337 $class = is_array($class) ? implode(" ", $class) : $class;
338 $class = wc_implode_html_attributes(["class" => esc_attr($class)]);
339 }
340
341 echo "<tr {$class}>";
342
343 $helpText = Arr::get($field, "args.help_text");
344 $label = Arr::get($field, "args.label_for");
345
346 printf('<th scope="row""><label class="wcpn__white-space--nowrap" %s>%s%s</label></th>',
347 $label ? "for=\"" . esc_attr($label) . "\"" : "",
348 Arr::get($field, "title"),
349 $helpText ? wc_help_tip($helpText) : ""
350 );
351
352 // Pass the option id as argument
353 Arr::set($field, "args.option_id", $page);
354
355 echo '<td>';
356 call_user_func(
357 Arr::get($field, "callback"),
358 Arr::get($field, "args")
359 );
360 echo '</td>';
361 echo '</tr>';
362 }
363 }
364 }
365
366 return new WCPOST_Settings();
367