← All changes
|
includes/my-wordpress/integrations/woocommerce.php
+79
-15
1.0.0
→
1.1.12
View file →
| @@ -325,9 +325,9 @@ | ||
| 325 | 325 | $order = 100; |
| 326 | 326 | foreach ( ( is_wp_error( $terms ) ? array() : (array) $terms ) as $term ) { |
| 327 | 327 | $bands[] = array( |
| 328 | 328 | 'id' => 'cat:' . $term->slug, |
| 329 | - 'label' => $term->name, | |
| 329 | + 'label' => openstation_plain_text_title( $term->name ), | |
| 330 | 330 | 'order' => $order, |
| 331 | 331 | 'category' => $term->slug, |
| 332 | 332 | ); |
| 333 | 333 | $order += 10; |
| @@ -1374,9 +1374,9 @@ | ||
| 1374 | 1374 | 'rating' => (float) $product->get_average_rating(), |
| 1375 | 1375 | 'reviews' => (int) $product->get_review_count(), |
| 1376 | 1376 | 'productType' => $type_label, |
| 1377 | 1377 | 'variations' => $product->is_type( 'variable' ) ? count( $product->get_children() ) : 0, |
| 1378 | - 'categories' => is_wp_error( $categories ) ? array() : array_values( $categories ), | |
| 1378 | + 'categories' => is_wp_error( $categories ) ? array() : array_map( 'openstation_plain_text_title', array_values( $categories ) ), | |
| 1379 | 1379 | 'permalink' => (string) $product->get_permalink(), |
| 1380 | 1380 | 'editUrl' => (string) get_edit_post_link( $product->get_id(), 'raw' ), |
| 1381 | 1381 | ); |
| 1382 | 1382 | } |
| @@ -1405,9 +1405,9 @@ | ||
| 1405 | 1405 | $product_id = method_exists( $item, 'get_product_id' ) ? (int) $item->get_product_id() : 0; |
| 1406 | 1406 | // Variations edit through their parent product's screen. |
| 1407 | 1407 | $edit_id = $product_id; |
| 1408 | 1408 | $items[] = array( |
| 1409 | - 'name' => $item->get_name(), | |
| 1409 | + 'name' => openstation_plain_text_title( $item->get_name() ), | |
| 1410 | 1410 | 'quantity' => (int) $item->get_quantity(), |
| 1411 | 1411 | 'total' => openstation_my_wordpress_woo_price( $item->get_total(), $order->get_currency() ), |
| 1412 | 1412 | 'id' => $product_id, |
| 1413 | 1413 | // A line item whose product has since been deleted has no |
| @@ -1555,9 +1555,9 @@ | ||
| 1555 | 1555 | foreach ( $ids as $id ) { |
| 1556 | 1556 | $term = get_term( (int) $id, $taxonomy ); |
| 1557 | 1557 | if ( $term && ! is_wp_error( $term ) ) { |
| 1558 | 1558 | $out[] = array( |
| 1559 | - 'label' => $term->name, | |
| 1559 | + 'label' => openstation_plain_text_title( $term->name ), | |
| 1560 | 1560 | 'editUrl' => (string) get_edit_term_link( $term->term_id, $taxonomy ), |
| 1561 | 1561 | ); |
| 1562 | 1562 | } |
| 1563 | 1563 | } |
| @@ -1569,9 +1569,9 @@ | ||
| 1569 | 1569 | foreach ( $ids as $id ) { |
| 1570 | 1570 | $product = wc_get_product( (int) $id ); |
| 1571 | 1571 | if ( $product ) { |
| 1572 | 1572 | $out[] = array( |
| 1573 | - 'label' => $product->get_name(), | |
| 1573 | + 'label' => openstation_plain_text_title( $product->get_name() ), | |
| 1574 | 1574 | 'editUrl' => (string) get_edit_post_link( $product->get_id(), 'raw' ), |
| 1575 | 1575 | ); |
| 1576 | 1576 | } |
| 1577 | 1577 | } |
| @@ -1899,37 +1899,63 @@ | ||
| 1899 | 1899 | |
| 1900 | 1900 | /** |
| 1901 | 1901 | * Register the integration bundle. |
| 1902 | 1902 | * |
| 1903 | + * Cache-busted by `filemtime`, like the window bundle it rides along | |
| 1904 | + * with (see `openstation_my_wordpress_register_assets()`). The bundle | |
| 1905 | + * is fetched lazily by URL, so a `ver` that only moves on release | |
| 1906 | + * would let a browser's cached copy outlive builds within one — a | |
| 1907 | + * stale companion against a fresh WP Explorer bundle is a contract | |
| 1908 | + * drift no error message points at. | |
| 1909 | + * | |
| 1903 | 1910 | * @return void |
| 1904 | 1911 | */ |
| 1905 | 1912 | function openstation_my_wordpress_woo_register_assets() { |
| 1913 | + $js_path = OPENSTATION_DIR . 'assets/js/my-wordpress-woocommerce' . openstation_asset_suffix() . '.js'; | |
| 1906 | 1914 | wp_register_script( |
| 1907 | 1915 | 'os-my-wordpress-woocommerce', |
| 1908 | - OPENSTATION_URL . 'assets/js/my-wordpress-woocommerce' . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ) . '.js', | |
| 1916 | + OPENSTATION_URL . 'assets/js/my-wordpress-woocommerce' . openstation_asset_suffix() . '.js', | |
| 1909 | 1917 | array( 'wp-hooks' ), |
| 1910 | - OPENSTATION_VERSION, | |
| 1918 | + file_exists( $js_path ) ? (string) filemtime( $js_path ) : OPENSTATION_VERSION, | |
| 1911 | 1919 | true |
| 1912 | 1920 | ); |
| 1913 | 1921 | wp_set_script_translations( 'os-my-wordpress-woocommerce', 'desktop-mode' ); |
| 1914 | 1922 | |
| 1923 | + $css_path = OPENSTATION_DIR . 'assets/css/my-wordpress-woocommerce.css'; | |
| 1915 | 1924 | wp_register_style( |
| 1916 | 1925 | 'os-my-wordpress-woocommerce', |
| 1917 | 1926 | OPENSTATION_URL . 'assets/css/my-wordpress-woocommerce.css', |
| 1918 | 1927 | array( 'desktop-mode-my-wordpress' ), |
| 1919 | - OPENSTATION_VERSION | |
| 1928 | + file_exists( $css_path ) ? (string) filemtime( $css_path ) : OPENSTATION_VERSION | |
| 1920 | 1929 | ); |
| 1921 | 1930 | } |
| 1922 | 1931 | add_action( 'init', 'openstation_my_wordpress_woo_register_assets', 5 ); |
| 1923 | 1932 | |
| 1924 | 1933 | /** |
| 1925 | - * Enqueue the integration bundle. | |
| 1934 | + * Attach the integration's config to its script handle. | |
| 1926 | 1935 | * |
| 1936 | + * NOTHING is enqueued here, and that is the point. The bundle | |
| 1937 | + * subscribes to the WP Explorer app's `preview-extras` / | |
| 1938 | + * `group-extras` actions, so it has to be in the tab before the app's | |
| 1939 | + * client view paints — but not one moment sooner. It travels as a | |
| 1940 | + * companion of the app window (see | |
| 1941 | + * `openstation_my_wordpress_woo_app_window_args()` below), which | |
| 1942 | + * means the shell loads it when the window first opens and a merchant | |
| 1943 | + * who never opens the explorer never downloads it at all. The | |
| 1944 | + * stylesheet travels the same way (the `styles` arg): every selector | |
| 1945 | + * in it is scoped to surfaces inside the explorer or the Customer | |
| 1946 | + * window, so on any document not showing those — | |
| 1947 | + * it was pure parse weight. | |
| 1948 | + * | |
| 1927 | 1949 | * Only for users who can open the site window on a store — everyone |
| 1928 | - * else pays nothing. The bundle subscribes to the site window's | |
| 1929 | - * `preview-extras` / `group-extras` actions, which is why it has to be | |
| 1930 | - * present before the (lazily loaded) window bundle paints. | |
| 1950 | + * else pays nothing. | |
| 1931 | 1951 | * |
| 1952 | + * Runs at priority 5, ahead of `openstation_enqueue_assets()` at 10: | |
| 1953 | + * that is where the boot payload is built, and it harvests this | |
| 1954 | + * inline blob off the registered handle so the lazy loader can | |
| 1955 | + * replay it around the script tag. Attaching later would ship the | |
| 1956 | + * bundle with no config. | |
| 1957 | + * | |
| 1932 | 1958 | * @return void |
| 1933 | 1959 | */ |
| 1934 | 1960 | function openstation_my_wordpress_woo_enqueue() { |
| 1935 | 1961 | if ( ! openstation_my_wordpress_woo_active() ) { |
| @@ -1941,10 +1967,8 @@ | ||
| 1941 | 1967 | if ( ! openstation_my_wordpress_user_can_use() ) { |
| 1942 | 1968 | return; |
| 1943 | 1969 | } |
| 1944 | 1970 | |
| 1945 | - wp_enqueue_script( 'os-my-wordpress-woocommerce' ); | |
| 1946 | - wp_enqueue_style( 'os-my-wordpress-woocommerce' ); | |
| 1947 | 1971 | wp_add_inline_script( |
| 1948 | 1972 | 'os-my-wordpress-woocommerce', |
| 1949 | 1973 | sprintf( |
| 1950 | 1974 | 'window.openStationWooConfig=%s;', |
| @@ -1954,8 +1978,10 @@ | ||
| 1954 | 1978 | 'restNonce' => wp_create_nonce( 'wp_rest' ), |
| 1955 | 1979 | 'canOrders' => true === openstation_my_wordpress_woo_orders_permission(), |
| 1956 | 1980 | 'canCustomers' => true === openstation_my_wordpress_woo_customers_permission(), |
| 1957 | 1981 | 'orderBands' => openstation_my_wordpress_woo_order_bands(), |
| 1982 | + // `wc-` slug → label, for the Orders list view. | |
| 1983 | + 'orderStatuses' => wc_get_order_statuses(), | |
| 1958 | 1984 | 'productBands' => openstation_my_wordpress_woo_product_bands(), |
| 1959 | 1985 | 'couponBands' => openstation_my_wordpress_woo_coupon_bands_with_counts(), |
| 1960 | 1986 | // Only built for a viewer who may see them — the |
| 1961 | 1987 | // band counts are money, and the plan behind them |
| @@ -1975,5 +2001,43 @@ | ||
| 1975 | 2001 | ), |
| 1976 | 2002 | 'before' |
| 1977 | 2003 | ); |
| 1978 | 2004 | } |
| 1979 | -add_action( 'admin_enqueue_scripts', 'openstation_my_wordpress_woo_enqueue', 20 ); | |
| 2005 | +add_action( 'admin_enqueue_scripts', 'openstation_my_wordpress_woo_enqueue', 5 ); | |
| 2006 | + | |
| 2007 | +/** | |
| 2008 | + * Attach the bundle and stylesheet to the WP Explorer app. | |
| 2009 | + * | |
| 2010 | + * The app fires the `os.my-wordpress.*` seams this bundle subscribes | |
| 2011 | + * to (`preview-extras`, `group-extras`, `list-tile`, the banding and | |
| 2012 | + * user filters), and its rows carry the `openstation_woo` / | |
| 2013 | + * `openstation_woo_customer` facts. `scripts` handles load in order | |
| 2014 | + * immediately before the window's own script, so the integration is | |
| 2015 | + * listening by the time the app's client view fires the seams — as a | |
| 2016 | + * first-open companion, costing nothing until the window opens. The | |
| 2017 | + * config blob rides the handle (see | |
| 2018 | + * `openstation_my_wordpress_woo_enqueue()`), so it arrives with the | |
| 2019 | + * bundle. The `styles` handle keeps its `wp_register_style` | |
| 2020 | + * dependency on the shared explorer sheet, so its equal-specificity | |
| 2021 | + * overrides (the ribbon and panel chrome) still win by source order. | |
| 2022 | + * | |
| 2023 | + * @param array $window_args Args passed to `openstation_register_window()`. | |
| 2024 | + * @param string $app_id App id. | |
| 2025 | + * @return array | |
| 2026 | + */ | |
| 2027 | +function openstation_my_wordpress_woo_app_window_args( $window_args, $app_id ) { | |
| 2028 | + if ( 'my-wordpress' !== (string) $app_id || ! is_array( $window_args ) || ! openstation_my_wordpress_woo_active() ) { | |
| 2029 | + return $window_args; | |
| 2030 | + } | |
| 2031 | + | |
| 2032 | + $scripts = isset( $window_args['scripts'] ) ? (array) $window_args['scripts'] : array(); | |
| 2033 | + $scripts[] = 'os-my-wordpress-woocommerce'; | |
| 2034 | + | |
| 2035 | + $styles = isset( $window_args['styles'] ) ? (array) $window_args['styles'] : array(); | |
| 2036 | + $styles[] = 'os-my-wordpress-woocommerce'; | |
| 2037 | + | |
| 2038 | + $window_args['scripts'] = $scripts; | |
| 2039 | + $window_args['styles'] = $styles; | |
| 2040 | + | |
| 2041 | + return $window_args; | |
| 2042 | +} | |
| 2043 | +add_filter( 'openstation_app_window_args', 'openstation_my_wordpress_woo_app_window_args', 10, 2 ); | |