Няма описание

class-wc-settings-shipping.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * WooCommerce Shipping Settings
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 2.6.0
  7. */
  8. use Automattic\Jetpack\Constants;
  9. defined( 'ABSPATH' ) || exit;
  10. if ( class_exists( 'WC_Settings_Shipping', false ) ) {
  11. return new WC_Settings_Shipping();
  12. }
  13. /**
  14. * WC_Settings_Shipping.
  15. */
  16. class WC_Settings_Shipping extends WC_Settings_Page {
  17. /**
  18. * Constructor.
  19. */
  20. public function __construct() {
  21. $this->id = 'shipping';
  22. $this->label = __( 'Shipping', 'woocommerce' );
  23. parent::__construct();
  24. }
  25. /**
  26. * Add this page to settings.
  27. *
  28. * @param array $pages Current pages.
  29. * @return array|mixed
  30. */
  31. public function add_settings_page( $pages ) {
  32. return wc_shipping_enabled() ? parent::add_settings_page( $pages ) : $pages;
  33. }
  34. /**
  35. * Get own sections.
  36. *
  37. * @return array
  38. */
  39. protected function get_own_sections() {
  40. $sections = array(
  41. '' => __( 'Shipping zones', 'woocommerce' ),
  42. 'options' => __( 'Shipping options', 'woocommerce' ),
  43. 'classes' => __( 'Shipping classes', 'woocommerce' ),
  44. );
  45. if ( ! $this->wc_is_installing() ) {
  46. // Load shipping methods so we can show any global options they may have.
  47. $shipping_methods = $this->get_shipping_methods();
  48. foreach ( $shipping_methods as $method ) {
  49. if ( ! $method->has_settings() ) {
  50. continue;
  51. }
  52. $title = empty( $method->method_title ) ? ucfirst( $method->id ) : $method->method_title;
  53. $sections[ strtolower( $method->id ) ] = esc_html( $title );
  54. }
  55. }
  56. return $sections;
  57. }
  58. /**
  59. * Is WC_INSTALLING constant defined?
  60. * This method exists to ease unit testing.
  61. *
  62. * @return bool True is the WC_INSTALLING constant is defined.
  63. */
  64. protected function wc_is_installing() {
  65. return Constants::is_defined( 'WC_INSTALLING' );
  66. }
  67. /**
  68. * Get the currently available shipping methods.
  69. * This method exists to ease unit testing.
  70. *
  71. * @return array Currently available shipping methods.
  72. */
  73. protected function get_shipping_methods() {
  74. return WC()->shipping()->get_shipping_methods();
  75. }
  76. /**
  77. * Get settings for the default section.
  78. *
  79. * The original implementation of 'get_settings' was returning the settings for the "Options" section
  80. * when the supplied value for $current_section was ''.
  81. *
  82. * @return array
  83. */
  84. protected function get_settings_for_default_section() {
  85. return $this->get_settings_for_options_section();
  86. }
  87. /**
  88. * Get settings for the options section.
  89. *
  90. * @return array
  91. */
  92. protected function get_settings_for_options_section() {
  93. $settings =
  94. array(
  95. array(
  96. 'title' => __( 'Shipping options', 'woocommerce' ),
  97. 'type' => 'title',
  98. 'id' => 'shipping_options',
  99. ),
  100. array(
  101. 'title' => __( 'Calculations', 'woocommerce' ),
  102. 'desc' => __( 'Enable the shipping calculator on the cart page', 'woocommerce' ),
  103. 'id' => 'woocommerce_enable_shipping_calc',
  104. 'default' => 'yes',
  105. 'type' => 'checkbox',
  106. 'checkboxgroup' => 'start',
  107. 'autoload' => false,
  108. ),
  109. array(
  110. 'desc' => __( 'Hide shipping costs until an address is entered', 'woocommerce' ),
  111. 'id' => 'woocommerce_shipping_cost_requires_address',
  112. 'default' => 'no',
  113. 'type' => 'checkbox',
  114. 'checkboxgroup' => 'end',
  115. ),
  116. array(
  117. 'title' => __( 'Shipping destination', 'woocommerce' ),
  118. 'desc' => __( 'This controls which shipping address is used by default.', 'woocommerce' ),
  119. 'id' => 'woocommerce_ship_to_destination',
  120. 'default' => 'billing',
  121. 'type' => 'radio',
  122. 'options' => array(
  123. 'shipping' => __( 'Default to customer shipping address', 'woocommerce' ),
  124. 'billing' => __( 'Default to customer billing address', 'woocommerce' ),
  125. 'billing_only' => __( 'Force shipping to the customer billing address', 'woocommerce' ),
  126. ),
  127. 'autoload' => false,
  128. 'desc_tip' => true,
  129. 'show_if_checked' => 'option',
  130. ),
  131. array(
  132. 'title' => __( 'Debug mode', 'woocommerce' ),
  133. 'desc' => __( 'Enable debug mode', 'woocommerce' ),
  134. 'desc_tip' => __( 'Enable shipping debug mode to show matching shipping zones and to bypass shipping rate cache.', 'woocommerce' ),
  135. 'id' => 'woocommerce_shipping_debug_mode',
  136. 'default' => 'no',
  137. 'type' => 'checkbox',
  138. ),
  139. array(
  140. 'type' => 'sectionend',
  141. 'id' => 'shipping_options',
  142. ),
  143. );
  144. return apply_filters( 'woocommerce_shipping_settings', $settings );
  145. }
  146. /**
  147. * Output the settings.
  148. */
  149. public function output() {
  150. global $current_section, $hide_save_button;
  151. // Load shipping methods so we can show any global options they may have.
  152. $shipping_methods = $this->get_shipping_methods();
  153. if ( '' === $current_section ) {
  154. $this->output_zones_screen();
  155. } elseif ( 'classes' === $current_section ) {
  156. $hide_save_button = true;
  157. $this->output_shipping_class_screen();
  158. } else {
  159. $is_shipping_method = false;
  160. foreach ( $shipping_methods as $method ) {
  161. if ( in_array( $current_section, array( $method->id, sanitize_title( get_class( $method ) ) ), true ) && $method->has_settings() ) {
  162. $is_shipping_method = true;
  163. $method->admin_options();
  164. }
  165. }
  166. if ( ! $is_shipping_method ) {
  167. parent::output();
  168. }
  169. }
  170. }
  171. /**
  172. * Save settings.
  173. */
  174. public function save() {
  175. global $current_section;
  176. switch ( $current_section ) {
  177. case 'options':
  178. $this->save_settings_for_current_section();
  179. $this->do_update_options_action();
  180. break;
  181. case 'classes':
  182. $this->do_update_options_action();
  183. break;
  184. case '':
  185. break;
  186. default:
  187. $is_shipping_method = false;
  188. foreach ( $this->get_shipping_methods() as $method_id => $method ) {
  189. if ( in_array( $current_section, array( $method->id, sanitize_title( get_class( $method ) ) ), true ) ) {
  190. $is_shipping_method = true;
  191. $this->do_update_options_action( $method->id );
  192. }
  193. }
  194. if ( ! $is_shipping_method ) {
  195. $this->save_settings_for_current_section();
  196. }
  197. break;
  198. }
  199. // Increments the transient version to invalidate cache.
  200. WC_Cache_Helper::get_transient_version( 'shipping', true );
  201. }
  202. /**
  203. * Handles output of the shipping zones page in admin.
  204. */
  205. protected function output_zones_screen() {
  206. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  207. global $hide_save_button;
  208. if ( isset( $_REQUEST['zone_id'] ) ) {
  209. $hide_save_button = true;
  210. $this->zone_methods_screen( wc_clean( wp_unslash( $_REQUEST['zone_id'] ) ) );
  211. } elseif ( isset( $_REQUEST['instance_id'] ) ) {
  212. $this->instance_settings_screen( absint( wp_unslash( $_REQUEST['instance_id'] ) ) );
  213. } else {
  214. $hide_save_button = true;
  215. $this->zones_screen();
  216. }
  217. // phpcs:enable WordPress.Security.NonceVerification.Recommended
  218. }
  219. /**
  220. * Show method for a zone
  221. *
  222. * @param int $zone_id Zone ID.
  223. */
  224. protected function zone_methods_screen( $zone_id ) {
  225. if ( 'new' === $zone_id ) {
  226. $zone = new WC_Shipping_Zone();
  227. } else {
  228. $zone = WC_Shipping_Zones::get_zone( absint( $zone_id ) );
  229. }
  230. if ( ! $zone ) {
  231. wp_die( esc_html__( 'Zone does not exist!', 'woocommerce' ) );
  232. }
  233. $allowed_countries = WC()->countries->get_shipping_countries();
  234. $shipping_continents = WC()->countries->get_shipping_continents();
  235. // Prepare locations.
  236. $locations = array();
  237. $postcodes = array();
  238. foreach ( $zone->get_zone_locations() as $location ) {
  239. if ( 'postcode' === $location->type ) {
  240. $postcodes[] = $location->code;
  241. } else {
  242. $locations[] = $location->type . ':' . $location->code;
  243. }
  244. }
  245. wp_localize_script(
  246. 'wc-shipping-zone-methods',
  247. 'shippingZoneMethodsLocalizeScript',
  248. array(
  249. 'methods' => $zone->get_shipping_methods( false, 'json' ),
  250. 'zone_name' => $zone->get_zone_name(),
  251. 'zone_id' => $zone->get_id(),
  252. 'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),
  253. 'strings' => array(
  254. 'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
  255. 'save_changes_prompt' => __( 'Do you wish to save your changes first? Your changed data will be discarded if you choose to cancel.', 'woocommerce' ),
  256. 'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ),
  257. 'add_method_failed' => __( 'Shipping method could not be added. Please retry.', 'woocommerce' ),
  258. 'yes' => __( 'Yes', 'woocommerce' ),
  259. 'no' => __( 'No', 'woocommerce' ),
  260. 'default_zone_name' => __( 'Zone', 'woocommerce' ),
  261. ),
  262. )
  263. );
  264. wp_enqueue_script( 'wc-shipping-zone-methods' );
  265. include_once dirname( __FILE__ ) . '/views/html-admin-page-shipping-zone-methods.php';
  266. }
  267. /**
  268. * Show zones
  269. */
  270. protected function zones_screen() {
  271. $method_count = wc_get_shipping_method_count( false, true );
  272. wp_localize_script(
  273. 'wc-shipping-zones',
  274. 'shippingZonesLocalizeScript',
  275. array(
  276. 'zones' => WC_Shipping_Zones::get_zones( 'json' ),
  277. 'default_zone' => array(
  278. 'zone_id' => 0,
  279. 'zone_name' => '',
  280. 'zone_order' => null,
  281. ),
  282. 'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),
  283. 'strings' => array(
  284. 'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
  285. 'delete_confirmation_msg' => __( 'Are you sure you want to delete this zone? This action cannot be undone.', 'woocommerce' ),
  286. 'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ),
  287. 'no_shipping_methods_offered' => __( 'No shipping methods offered to this zone.', 'woocommerce' ),
  288. ),
  289. )
  290. );
  291. wp_enqueue_script( 'wc-shipping-zones' );
  292. include_once dirname( __FILE__ ) . '/views/html-admin-page-shipping-zones.php';
  293. }
  294. /**
  295. * Show instance settings
  296. *
  297. * @param int $instance_id Shipping instance ID.
  298. */
  299. protected function instance_settings_screen( $instance_id ) {
  300. $zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id );
  301. $shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id );
  302. if ( ! $shipping_method ) {
  303. wp_die( esc_html__( 'Invalid shipping method!', 'woocommerce' ) );
  304. }
  305. if ( ! $zone ) {
  306. wp_die( esc_html__( 'Zone does not exist!', 'woocommerce' ) );
  307. }
  308. if ( ! $shipping_method->has_settings() ) {
  309. wp_die( esc_html__( 'This shipping method does not have any settings to configure.', 'woocommerce' ) );
  310. }
  311. if ( ! empty( $_POST['save'] ) ) {
  312. // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  313. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'woocommerce-settings' ) ) {
  314. echo '<div class="updated error"><p>' . esc_html__( 'Edit failed. Please try again.', 'woocommerce' ) . '</p></div>';
  315. }
  316. $shipping_method->process_admin_options();
  317. $shipping_method->display_errors();
  318. }
  319. include_once dirname( __FILE__ ) . '/views/html-admin-page-shipping-zones-instance.php';
  320. }
  321. /**
  322. * Handles output of the shipping class settings screen.
  323. */
  324. protected function output_shipping_class_screen() {
  325. $wc_shipping = WC_Shipping::instance();
  326. wp_localize_script(
  327. 'wc-shipping-classes',
  328. 'shippingClassesLocalizeScript',
  329. array(
  330. 'classes' => $wc_shipping->get_shipping_classes(),
  331. 'default_shipping_class' => array(
  332. 'term_id' => 0,
  333. 'name' => '',
  334. 'description' => '',
  335. ),
  336. 'wc_shipping_classes_nonce' => wp_create_nonce( 'wc_shipping_classes_nonce' ),
  337. 'strings' => array(
  338. 'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
  339. 'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ),
  340. ),
  341. )
  342. );
  343. wp_enqueue_script( 'wc-shipping-classes' );
  344. // Extendable columns to show on the shipping classes screen.
  345. $shipping_class_columns = apply_filters(
  346. 'woocommerce_shipping_classes_columns',
  347. array(
  348. 'wc-shipping-class-name' => __( 'Shipping class', 'woocommerce' ),
  349. 'wc-shipping-class-slug' => __( 'Slug', 'woocommerce' ),
  350. 'wc-shipping-class-description' => __( 'Description', 'woocommerce' ),
  351. 'wc-shipping-class-count' => __( 'Product count', 'woocommerce' ),
  352. )
  353. );
  354. include_once dirname( __FILE__ ) . '/views/html-admin-page-shipping-classes.php';
  355. }
  356. }
  357. return new WC_Settings_Shipping();