/** * Output custom column content * * @since 1.0.0 * @param string $column * @param int $post_id */ public function custom_column_content($column, $post_id) { $status = new WC_Order_Status_Manager_Order_Status($post_id); switch ($column) { case 'icon': $color = $status->get_color(); $icon = $status->get_icon(); $style = ''; if ($color) { if ($icon) { $style = 'color: ' . $color . ';'; } else { $style = 'background-color: ' . $color . '; color: ' . wc_order_status_manager()->icons->get_contrast_text_color($color) . ';'; } } if (is_numeric($icon)) { $icon_src = wp_get_attachment_image_src($icon, 'wc_order_status_icon'); if ($icon_src) { $style .= 'background-image: url( ' . $icon_src[0] . ');'; } } printf('<mark class="%s %s tips" style="%s" data-tip="%s">%s</mark>', sanitize_title($status->get_slug()), $icon ? 'has-icon ' . $icon : '', $style, esc_attr($status->get_name()), esc_html($status->get_name())); break; case 'slug': echo esc_html($status->get_slug()); break; case 'description': echo esc_html($status->get_description()); break; case 'type': printf('<span class="badge %s">%s</span>', sanitize_title($status->get_type()), esc_html($status->get_type())); break; } }
/** * Handle deleting an order status * * Will assign all orders that have the to-be deleted status * a replacement status, which defaults to `wc-on-hold`. * Also removes the status form any next statuses. * * @since 1.0.0 * @param int $post_id the order status post id */ public function handle_order_status_delete($post_id) { global $wpdb; // Bail out if not an order status or not published if ('wc_order_status' !== get_post_type($post_id) || 'publish' !== get_post_status($post_id)) { return; } $order_status = new WC_Order_Status_Manager_Order_Status($post_id); if (!$order_status->get_id()) { return; } /** * Filter the replacement status when an order status is deleted * * This filter is applied just before the order status is deleted, * but after the order status meta has already been deleted. * * @since 1.0.0 * * @param string $replacement Replacement order status slug. * @param string $original Original order status slug. */ $replacement_status = apply_filters('wc_order_status_manager_deleted_status_replacement', 'on-hold', $order_status->get_slug()); $replacement_status = str_replace('wc-', '', $replacement_status); $old_status_name = $order_status->get_name(); $order_rows = $wpdb->get_results($wpdb->prepare("\n\t\t\tSELECT ID FROM {$wpdb->posts}\n\t\t\tWHERE post_type = 'shop_order' AND post_status = %s\n\t\t", $order_status->get_slug(true)), ARRAY_A); $num_updated = 0; if (!empty($order_rows)) { foreach ($order_rows as $order_row) { $order = wc_get_order($order_row['ID']); $order->update_status($replacement_status, __("Order status updated because the previous status was deleted.", WC_Order_Status_Manager::TEXT_DOMAIN)); $num_updated++; } } // If any other order statuses have specified this status // as a 'next status', remove it from there $rows = $wpdb->get_results($wpdb->prepare("\n\t\t\tSELECT pm.post_id\n\t\t\tFROM {$wpdb->postmeta} pm\n\t\t\tRIGHT JOIN {$wpdb->posts} p ON pm.post_id = p.ID\n\t\t\tWHERE post_type = 'wc_order_status'\n\t\t\tAND meta_key = '_next_statuses'\n\t\t\tAND meta_value LIKE %s\n\t\t", '%' . $wpdb->esc_like($order_status->get_slug()) . '%')); if ($rows) { foreach ($rows as $row) { $next_statuses = get_post_meta($row->post_id, '_next_statuses', true); // Remove the next status slug if (($key = array_search($order_status->get_slug(), $next_statuses)) !== false) { unset($next_statuses[$key]); } update_post_meta($row->post_id, '_next_statuses', $next_statuses); } } // Add admin notice if ($num_updated && is_admin() && !defined('DOING_AJAX')) { $new_status = new WC_Order_Status_Manager_Order_Status($replacement_status); $message = sprintf(_n('%d order that was previously %s is now %s.', '%d orders that were previously %s are now %s.', $num_updated, WC_Order_Status_Manager::TEXT_DOMAIN), $num_updated, esc_html($old_status_name), esc_html($new_status->get_name())); wc_order_status_manager()->get_message_handler()->add_message($message); } }