CustomMetaBox.php
9 months ago
CustomerHistory.php
2 months ago
OrderAttribution.php
1 year ago
TaxonomiesMetaBox.php
3 years ago
TaxonomiesMetaBox.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\Admin\Orders\MetaBoxes; |
| 4 | |
| 5 | use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore; |
| 6 | |
| 7 | /** |
| 8 | * TaxonomiesMetaBox class, renders taxonomy sidebar widget on order edit screen. |
| 9 | */ |
| 10 | class TaxonomiesMetaBox { |
| 11 | |
| 12 | /** |
| 13 | * Order Table data store class. |
| 14 | * |
| 15 | * @var OrdersTableDataStore |
| 16 | */ |
| 17 | private $orders_table_data_store; |
| 18 | |
| 19 | /** |
| 20 | * Dependency injection init method. |
| 21 | * |
| 22 | * @param OrdersTableDataStore $orders_table_data_store Order Table data store class. |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | public function init( OrdersTableDataStore $orders_table_data_store ) { |
| 27 | $this->orders_table_data_store = $orders_table_data_store; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Registers meta boxes to be rendered in order edit screen for taxonomies. |
| 32 | * |
| 33 | * Note: This is re-implementation of part of WP core's `register_and_do_post_meta_boxes` function. Since the code block that add meta box for taxonomies is not filterable, we have to re-implement it. |
| 34 | * |
| 35 | * @param string $screen_id Screen ID. |
| 36 | * @param string $order_type Order type to register meta boxes for. |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function add_taxonomies_meta_boxes( string $screen_id, string $order_type ) { |
| 41 | include_once ABSPATH . 'wp-admin/includes/meta-boxes.php'; |
| 42 | $taxonomies = get_object_taxonomies( $order_type ); |
| 43 | // All taxonomies. |
| 44 | foreach ( $taxonomies as $tax_name ) { |
| 45 | $taxonomy = get_taxonomy( $tax_name ); |
| 46 | if ( ! $taxonomy->show_ui || false === $taxonomy->meta_box_cb ) { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | if ( 'post_categories_meta_box' === $taxonomy->meta_box_cb ) { |
| 51 | $taxonomy->meta_box_cb = array( $this, 'order_categories_meta_box' ); |
| 52 | } |
| 53 | |
| 54 | if ( 'post_tags_meta_box' === $taxonomy->meta_box_cb ) { |
| 55 | $taxonomy->meta_box_cb = array( $this, 'order_tags_meta_box' ); |
| 56 | } |
| 57 | |
| 58 | $label = $taxonomy->labels->name; |
| 59 | |
| 60 | if ( ! is_taxonomy_hierarchical( $tax_name ) ) { |
| 61 | $tax_meta_box_id = 'tagsdiv-' . $tax_name; |
| 62 | } else { |
| 63 | $tax_meta_box_id = $tax_name . 'div'; |
| 64 | } |
| 65 | |
| 66 | add_meta_box( |
| 67 | $tax_meta_box_id, |
| 68 | $label, |
| 69 | $taxonomy->meta_box_cb, |
| 70 | $screen_id, |
| 71 | 'side', |
| 72 | 'core', |
| 73 | array( |
| 74 | 'taxonomy' => $tax_name, |
| 75 | '__back_compat_meta_box' => true, |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Save handler for taxonomy data. |
| 83 | * |
| 84 | * @param \WC_Abstract_Order $order Order object. |
| 85 | * @param array|null $taxonomy_input Taxonomy input passed from input. |
| 86 | */ |
| 87 | public function save_taxonomies( \WC_Abstract_Order $order, $taxonomy_input ) { |
| 88 | if ( ! isset( $taxonomy_input ) ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $sanitized_tax_input = $this->sanitize_tax_input( $taxonomy_input ); |
| 93 | |
| 94 | $sanitized_tax_input = $this->orders_table_data_store->init_default_taxonomies( $order, $sanitized_tax_input ); |
| 95 | $this->orders_table_data_store->set_custom_taxonomies( $order, $sanitized_tax_input ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sanitize taxonomy input by calling sanitize callbacks for each registered taxonomy. |
| 100 | * |
| 101 | * @param array|null $taxonomy_data Nonce verified taxonomy input. |
| 102 | * |
| 103 | * @return array Sanitized taxonomy input. |
| 104 | */ |
| 105 | private function sanitize_tax_input( $taxonomy_data ) : array { |
| 106 | $sanitized_tax_input = array(); |
| 107 | if ( ! is_array( $taxonomy_data ) ) { |
| 108 | return $sanitized_tax_input; |
| 109 | } |
| 110 | |
| 111 | // Convert taxonomy input to term IDs, to avoid ambiguity. |
| 112 | foreach ( $taxonomy_data as $taxonomy => $terms ) { |
| 113 | $tax_object = get_taxonomy( $taxonomy ); |
| 114 | if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) { |
| 115 | $sanitized_tax_input[ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return $sanitized_tax_input; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Add the categories meta box to the order screen. This is just a wrapper around the post_categories_meta_box. |
| 124 | * |
| 125 | * @param \WC_Abstract_Order $order Order object. |
| 126 | * @param array $box Meta box args. |
| 127 | * |
| 128 | * @return void |
| 129 | */ |
| 130 | public function order_categories_meta_box( $order, $box ) { |
| 131 | $post = get_post( $order->get_id() ); |
| 132 | post_categories_meta_box( $post, $box ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Add the tags meta box to the order screen. This is just a wrapper around the post_tags_meta_box. |
| 137 | * |
| 138 | * @param \WC_Abstract_Order $order Order object. |
| 139 | * @param array $box Meta box args. |
| 140 | * |
| 141 | * @return void |
| 142 | */ |
| 143 | public function order_tags_meta_box( $order, $box ) { |
| 144 | $post = get_post( $order->get_id() ); |
| 145 | post_tags_meta_box( $post, $box ); |
| 146 | } |
| 147 | } |
| 148 |