wc-admin-functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * WooCommerce Admin Functions
  4. *
  5. * @author WooThemes
  6. * @category Core
  7. * @package WooCommerce/Admin/Functions
  8. * @version 2.4.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit; // Exit if accessed directly
  12. }
  13. /**
  14. * Get all WooCommerce screen ids.
  15. *
  16. * @return array
  17. */
  18. function wc_get_screen_ids() {
  19. $wc_screen_id = sanitize_title( __( 'WooCommerce', 'woocommerce' ) );
  20. $screen_ids = array(
  21. 'toplevel_page_' . $wc_screen_id,
  22. $wc_screen_id . '_page_wc-reports',
  23. $wc_screen_id . '_page_wc-shipping',
  24. $wc_screen_id . '_page_wc-settings',
  25. $wc_screen_id . '_page_wc-status',
  26. $wc_screen_id . '_page_wc-addons',
  27. 'toplevel_page_wc-reports',
  28. 'product_page_product_attributes',
  29. 'product_page_product_exporter',
  30. 'product_page_product_importer',
  31. 'edit-product',
  32. 'product',
  33. 'edit-shop_coupon',
  34. 'shop_coupon',
  35. 'edit-product_cat',
  36. 'edit-product_tag',
  37. 'profile',
  38. 'user-edit',
  39. );
  40. foreach ( wc_get_order_types() as $type ) {
  41. $screen_ids[] = $type;
  42. $screen_ids[] = 'edit-' . $type;
  43. }
  44. if ( $attributes = wc_get_attribute_taxonomies() ) {
  45. foreach ( $attributes as $attribute ) {
  46. $screen_ids[] = 'edit-' . wc_attribute_taxonomy_name( $attribute->attribute_name );
  47. }
  48. }
  49. return apply_filters( 'woocommerce_screen_ids', $screen_ids );
  50. }
  51. /**
  52. * Create a page and store the ID in an option.
  53. *
  54. * @param mixed $slug Slug for the new page
  55. * @param string $option Option name to store the page's ID
  56. * @param string $page_title (default: '') Title for the new page
  57. * @param string $page_content (default: '') Content for the new page
  58. * @param int $post_parent (default: 0) Parent for the new page
  59. * @return int page ID
  60. */
  61. function wc_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
  62. global $wpdb;
  63. $option_value = get_option( $option );
  64. if ( $option_value > 0 && ( $page_object = get_post( $option_value ) ) ) {
  65. if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
  66. // Valid page is already in place
  67. return $page_object->ID;
  68. }
  69. }
  70. if ( strlen( $page_content ) > 0 ) {
  71. // Search for an existing page with the specified page content (typically a shortcode)
  72. $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
  73. } else {
  74. // Search for an existing page with the specified page slug
  75. $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) );
  76. }
  77. $valid_page_found = apply_filters( 'woocommerce_create_page_id', $valid_page_found, $slug, $page_content );
  78. if ( $valid_page_found ) {
  79. if ( $option ) {
  80. update_option( $option, $valid_page_found );
  81. }
  82. return $valid_page_found;
  83. }
  84. // Search for a matching valid trashed page
  85. if ( strlen( $page_content ) > 0 ) {
  86. // Search for an existing page with the specified page content (typically a shortcode)
  87. $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
  88. } else {
  89. // Search for an existing page with the specified page slug
  90. $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) );
  91. }
  92. if ( $trashed_page_found ) {
  93. $page_id = $trashed_page_found;
  94. $page_data = array(
  95. 'ID' => $page_id,
  96. 'post_status' => 'publish',
  97. );
  98. wp_update_post( $page_data );
  99. } else {
  100. $page_data = array(
  101. 'post_status' => 'publish',
  102. 'post_type' => 'page',
  103. 'post_author' => 1,
  104. 'post_name' => $slug,
  105. 'post_title' => $page_title,
  106. 'post_content' => $page_content,
  107. 'post_parent' => $post_parent,
  108. 'comment_status' => 'closed',
  109. );
  110. $page_id = wp_insert_post( $page_data );
  111. }
  112. if ( $option ) {
  113. update_option( $option, $page_id );
  114. }
  115. return $page_id;
  116. }
  117. /**
  118. * Output admin fields.
  119. *
  120. * Loops though the woocommerce options array and outputs each field.
  121. *
  122. * @param array $options Opens array to output
  123. */
  124. function woocommerce_admin_fields( $options ) {
  125. if ( ! class_exists( 'WC_Admin_Settings', false ) ) {
  126. include dirname( __FILE__ ) . '/class-wc-admin-settings.php';
  127. }
  128. WC_Admin_Settings::output_fields( $options );
  129. }
  130. /**
  131. * Update all settings which are passed.
  132. *
  133. * @param array $options
  134. * @param array $data
  135. */
  136. function woocommerce_update_options( $options, $data = null ) {
  137. if ( ! class_exists( 'WC_Admin_Settings', false ) ) {
  138. include dirname( __FILE__ ) . '/class-wc-admin-settings.php';
  139. }
  140. WC_Admin_Settings::save_fields( $options, $data );
  141. }
  142. /**
  143. * Get a setting from the settings API.
  144. *
  145. * @param mixed $option_name
  146. * @param mixed $default
  147. * @return string
  148. */
  149. function woocommerce_settings_get_option( $option_name, $default = '' ) {
  150. if ( ! class_exists( 'WC_Admin_Settings', false ) ) {
  151. include dirname( __FILE__ ) . '/class-wc-admin-settings.php';
  152. }
  153. return WC_Admin_Settings::get_option( $option_name, $default );
  154. }
  155. /**
  156. * Save order items. Uses the CRUD.
  157. *
  158. * @since 2.2
  159. * @param int $order_id Order ID
  160. * @param array $items Order items to save
  161. */
  162. function wc_save_order_items( $order_id, $items ) {
  163. // Allow other plugins to check change in order items before they are saved.
  164. do_action( 'woocommerce_before_save_order_items', $order_id, $items );
  165. // Line items and fees.
  166. if ( isset( $items['order_item_id'] ) ) {
  167. $data_keys = array(
  168. 'line_tax' => array(),
  169. 'line_subtotal_tax' => array(),
  170. 'order_item_name' => null,
  171. 'order_item_qty' => null,
  172. 'order_item_tax_class' => null,
  173. 'line_total' => null,
  174. 'line_subtotal' => null,
  175. );
  176. foreach ( $items['order_item_id'] as $item_id ) {
  177. if ( ! $item = WC_Order_Factory::get_order_item( absint( $item_id ) ) ) {
  178. continue;
  179. }
  180. $item_data = array();
  181. foreach ( $data_keys as $key => $default ) {
  182. $item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_clean( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default;
  183. }
  184. if ( '0' === $item_data['order_item_qty'] ) {
  185. $item->delete();
  186. continue;
  187. }
  188. $item->set_props(
  189. array(
  190. 'name' => $item_data['order_item_name'],
  191. 'quantity' => $item_data['order_item_qty'],
  192. 'tax_class' => $item_data['order_item_tax_class'],
  193. 'total' => $item_data['line_total'],
  194. 'subtotal' => $item_data['line_subtotal'],
  195. 'taxes' => array(
  196. 'total' => $item_data['line_tax'],
  197. 'subtotal' => $item_data['line_subtotal_tax'],
  198. ),
  199. )
  200. );
  201. if ( 'fee' === $item->get_type() ) {
  202. $item->set_amount( $item_data['line_total'] );
  203. }
  204. if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) {
  205. foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) {
  206. $meta_key = substr( wp_unslash( $meta_key ), 0, 255 );
  207. $meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : '';
  208. if ( '' === $meta_key && '' === $meta_value ) {
  209. if ( ! strstr( $meta_id, 'new-' ) ) {
  210. $item->delete_meta_data_by_mid( $meta_id );
  211. }
  212. } elseif ( strstr( $meta_id, 'new-' ) ) {
  213. $item->add_meta_data( $meta_key, $meta_value, false );
  214. } else {
  215. $item->update_meta_data( $meta_key, $meta_value, $meta_id );
  216. }
  217. }
  218. }
  219. $item->save();
  220. }
  221. }
  222. // Shipping Rows
  223. if ( isset( $items['shipping_method_id'] ) ) {
  224. $data_keys = array(
  225. 'shipping_method' => null,
  226. 'shipping_method_title' => null,
  227. 'shipping_cost' => 0,
  228. 'shipping_taxes' => array(),
  229. );
  230. foreach ( $items['shipping_method_id'] as $item_id ) {
  231. if ( ! $item = WC_Order_Factory::get_order_item( absint( $item_id ) ) ) {
  232. continue;
  233. }
  234. $item_data = array();
  235. foreach ( $data_keys as $key => $default ) {
  236. $item_data[ $key ] = isset( $items[ $key ][ $item_id ] ) ? wc_clean( wp_unslash( $items[ $key ][ $item_id ] ) ) : $default;
  237. }
  238. $item->set_props(
  239. array(
  240. 'method_id' => $item_data['shipping_method'],
  241. 'method_title' => $item_data['shipping_method_title'],
  242. 'total' => $item_data['shipping_cost'],
  243. 'taxes' => array(
  244. 'total' => $item_data['shipping_taxes'],
  245. ),
  246. )
  247. );
  248. if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) {
  249. foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) {
  250. $meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? wp_unslash( $items['meta_value'][ $item_id ][ $meta_id ] ) : '';
  251. if ( '' === $meta_key && '' === $meta_value ) {
  252. if ( ! strstr( $meta_id, 'new-' ) ) {
  253. $item->delete_meta_data_by_mid( $meta_id );
  254. }
  255. } elseif ( strstr( $meta_id, 'new-' ) ) {
  256. $item->add_meta_data( $meta_key, $meta_value, false );
  257. } else {
  258. $item->update_meta_data( $meta_key, $meta_value, $meta_id );
  259. }
  260. }
  261. }
  262. $item->save();
  263. }
  264. }
  265. $order = wc_get_order( $order_id );
  266. $order->update_taxes();
  267. $order->calculate_totals( false );
  268. // Inform other plugins that the items have been saved
  269. do_action( 'woocommerce_saved_order_items', $order_id, $items );
  270. }
  271. /**
  272. * Get HTML for some action buttons. Used in list tables.
  273. *
  274. * @since 3.3.0
  275. * @param array $actions Actions to output.
  276. * @return string
  277. */
  278. function wc_render_action_buttons( $actions ) {
  279. $actions_html = '';
  280. foreach ( $actions as $action ) {
  281. if ( isset( $action['group'] ) ) {
  282. $actions_html .= '<div class="wc-action-button-group"><label>' . $action['group'] . '</label> <span class="wc-action-button-group__items">' . wc_render_action_buttons( $action['actions'] ) . '</span></div>';
  283. } elseif ( isset( $action['action'], $action['url'], $action['name'] ) ) {
  284. $actions_html .= sprintf( '<a class="button wc-action-button wc-action-button-%1$s %1$s" href="%2$s" aria-label="%3$s" title="%3$s">%4$s</a>', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( isset( $action['title'] ) ? $action['title'] : $action['name'] ), esc_html( $action['name'] ) );
  285. }
  286. }
  287. return $actions_html;
  288. }