← All changes
|
includes/my-wordpress/integrations/woocommerce-relations.php
+58
-16
1.1.6
→
1.1.12
View file →
| @@ -335,8 +335,36 @@ | ||
| 335 | 335 | return $links; |
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | /** |
| 339 | + * The human label for an order: its number, plus the billing name | |
| 340 | + * when the order has one. | |
| 341 | + * | |
| 342 | + * @param WC_Abstract_Order $order Order to label. | |
| 343 | + * @return string | |
| 344 | + */ | |
| 345 | +function openstation_my_wordpress_woo_order_title( $order ) { | |
| 346 | + $name = method_exists( $order, 'get_formatted_billing_full_name' ) | |
| 347 | + ? trim( $order->get_formatted_billing_full_name() ) | |
| 348 | + : ''; | |
| 349 | + | |
| 350 | + if ( '' !== $name ) { | |
| 351 | + return sprintf( | |
| 352 | + /* translators: 1: order number, 2: customer name. */ | |
| 353 | + __( 'Order #%1$s · %2$s', 'desktop-mode' ), | |
| 354 | + $order->get_order_number(), | |
| 355 | + $name | |
| 356 | + ); | |
| 357 | + } | |
| 358 | + | |
| 359 | + return sprintf( | |
| 360 | + /* translators: %s: order number. */ | |
| 361 | + __( 'Order #%s', 'desktop-mode' ), | |
| 362 | + $order->get_order_number() | |
| 363 | + ); | |
| 364 | +} | |
| 365 | + | |
| 366 | +/** | |
| 339 | 367 | * Announce an identity for WooCommerce's own screens, and hang the |
| 340 | 368 | * shop's links off the identities the built-in detection already |
| 341 | 369 | * produces. |
| 342 | 370 | * |
| @@ -356,27 +384,12 @@ | ||
| 356 | 384 | // branch), and it arrives with no links: an order's content is |
| 357 | 385 | // empty, so the hyperlink/media/term extractor finds nothing. |
| 358 | 386 | $order = openstation_my_wordpress_woo_current_order(); |
| 359 | 387 | if ( $order && openstation_my_wordpress_woo_can_read_orders() ) { |
| 360 | - $name = method_exists( $order, 'get_formatted_billing_full_name' ) | |
| 361 | - ? trim( $order->get_formatted_billing_full_name() ) | |
| 362 | - : ''; | |
| 363 | - | |
| 364 | 388 | $identity = array( |
| 365 | 389 | 'type' => 'shop_order', |
| 366 | 390 | 'id' => (int) $order->get_id(), |
| 367 | - 'label' => '' !== $name | |
| 368 | - ? sprintf( | |
| 369 | - /* translators: 1: order number, 2: customer name. */ | |
| 370 | - __( 'Order #%1$s · %2$s', 'desktop-mode' ), | |
| 371 | - $order->get_order_number(), | |
| 372 | - $name | |
| 373 | - ) | |
| 374 | - : sprintf( | |
| 375 | - /* translators: %s: order number. */ | |
| 376 | - __( 'Order #%s', 'desktop-mode' ), | |
| 377 | - $order->get_order_number() | |
| 378 | - ), | |
| 391 | + 'label' => openstation_my_wordpress_woo_order_title( $order ), | |
| 379 | 392 | ); |
| 380 | 393 | |
| 381 | 394 | $links = openstation_my_wordpress_woo_order_refs( $order ); |
| 382 | 395 | if ( ! empty( $links ) ) { |
| @@ -1141,8 +1154,36 @@ | ||
| 1141 | 1154 | return $related; |
| 1142 | 1155 | } |
| 1143 | 1156 | |
| 1144 | 1157 | /** |
| 1158 | + * Name the order-edit window after the order it shows. | |
| 1159 | + * | |
| 1160 | + * @return void | |
| 1161 | + */ | |
| 1162 | +function openstation_my_wordpress_woo_order_window_title() { | |
| 1163 | + if ( ! openstation_my_wordpress_woo_active() || ! openstation_my_wordpress_woo_can_read_orders() ) { | |
| 1164 | + return; | |
| 1165 | + } | |
| 1166 | + | |
| 1167 | + $order = openstation_my_wordpress_woo_current_order(); | |
| 1168 | + if ( ! $order ) { | |
| 1169 | + return; | |
| 1170 | + } | |
| 1171 | + | |
| 1172 | + // `JSON_HEX_TAG` neutralises a `</script>` smuggled in through the billing name. | |
| 1173 | + $title_json = wp_json_encode( | |
| 1174 | + openstation_my_wordpress_woo_order_title( $order ), | |
| 1175 | + JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | |
| 1176 | + ); | |
| 1177 | + if ( false === $title_json ) { | |
| 1178 | + return; | |
| 1179 | + } | |
| 1180 | + wp_print_inline_script_tag( | |
| 1181 | + 'window.parent.postMessage({type:"os-title-change",title:' . $title_json . '},window.location.origin);' | |
| 1182 | + ); | |
| 1183 | +} | |
| 1184 | + | |
| 1185 | +/** | |
| 1145 | 1186 | * Boot the relations wiring. |
| 1146 | 1187 | * |
| 1147 | 1188 | * Priority 20 on the identity filter so a site that overrides the |
| 1148 | 1189 | * order identity for its own reasons still wins. |
| @@ -1151,6 +1192,7 @@ | ||
| 1151 | 1192 | */ |
| 1152 | 1193 | function openstation_my_wordpress_woo_relations_boot() { |
| 1153 | 1194 | add_filter( 'openstation_window_content_identity', 'openstation_my_wordpress_woo_content_identity', 20, 2 ); |
| 1154 | 1195 | add_filter( 'openstation_window_related_entities', 'openstation_my_wordpress_woo_related_entities', 20, 3 ); |
| 1196 | + add_action( 'openstation_chromeless_after', 'openstation_my_wordpress_woo_order_window_title' ); | |
| 1155 | 1197 | } |
| 1156 | 1198 | openstation_my_wordpress_woo_relations_boot(); |