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