diff --git a/includes/integrations/woocommerce/wp-zoom-woocommerce-markup-products.php b/includes/integrations/woocommerce/wp-zoom-woocommerce-markup-products.php index 8d877b4..2b233e9 100644 --- a/includes/integrations/woocommerce/wp-zoom-woocommerce-markup-products.php +++ b/includes/integrations/woocommerce/wp-zoom-woocommerce-markup-products.php @@ -670,7 +670,7 @@ function wp_zoom_woocommerce_list_info_action( $args ) { add_query_arg( array( 'add-to-cart' => $args['product'], - 'occurrence_id' => $args['data']['occurrence_id'] ?? null, + 'occurrence_id' => $args['occurrence_id'] ?? null, ), get_permalink( $args['product'] ) ) @@ -680,3 +680,51 @@ function wp_zoom_woocommerce_list_info_action( $args ) { } } add_action( 'wp_zoom_list_after_info_actions', 'wp_zoom_woocommerce_list_info_action' ); + +/** + * Populate occurrence data with associated products + * + * @param array $data Array of occurrences. + * @param array $atts Shortcode attributes. + * @return array + */ +function wp_zoom_woocommerce_list_object_product( $data, $atts ) { + $data = array_map( + function( $object ) { + $object['product'] = null; + + $purchase_product = wp_zoom_get_purchase_product( $object['id'] ); + + $object['product'] = $purchase_product; + + return $object; + }, + $data + ); + + return $data; +} +add_filter( 'wp_zoom_list_shortcode_data', 'wp_zoom_woocommerce_list_object_product', 10, 2 ); + +/** + * Filter list occurrences by product category + * + * @param array $data Array of occurrences. + * @param array $atts Shortcode attributes. + * @return array + */ +function wp_zoom_woocommerce_list_object_category( $data, $atts ) { + if ( $atts['category'] ) { + $categories = array_map( 'trim', explode( ',', $atts['category'] ) ); + + $data = array_filter( + $data, + function( $object ) use ( $categories ) { + return $object['product'] ? is_object_in_term( $object['product'], 'product_cat', $categories ) : false; + } + ); + } + + return $data; +} +add_filter( 'wp_zoom_list_shortcode_data', 'wp_zoom_woocommerce_list_object_category', 15, 2 ); diff --git a/includes/shortcodes/wp-zoom-list-shortcode.php b/includes/shortcodes/wp-zoom-list-shortcode.php index 24f3eab..b659972 100644 --- a/includes/shortcodes/wp-zoom-list-shortcode.php +++ b/includes/shortcodes/wp-zoom-list-shortcode.php @@ -18,6 +18,7 @@ function wp_zoom_list_shortcode( $atts, $content = '' ) { 'type' => 'webinars', 'per_page' => 20, 'show_past' => 0, + 'category' => null, ), $atts ); @@ -29,6 +30,7 @@ function wp_zoom_list_shortcode( $atts, $content = '' ) { $page = get_query_var( 'paged', 1 ); $per_page = intval( $atts['per_page'] ); $data = wp_zoom_get_occurrences( $atts['type'], (bool) $atts['show_past'] ); + $data = apply_filters( 'wp_zoom_list_shortcode_data', $data, $atts ); $total = count( $data ); if ( $page < 2 ) { @@ -39,8 +41,6 @@ function wp_zoom_list_shortcode( $atts, $content = '' ) { $data = array_slice( $data, ( $page - 1 ) * $per_page, $per_page ); } - $data = apply_filters( 'wp_zoom_list_shortcode_data', $data, $atts ); - ob_start(); wp_zoom_load_template( diff --git a/includes/wp-zoom-helper-functions.php b/includes/wp-zoom-helper-functions.php index 086c66f..51864f7 100644 --- a/includes/wp-zoom-helper-functions.php +++ b/includes/wp-zoom-helper-functions.php @@ -70,35 +70,42 @@ function wp_zoom_format_end_date_time( string $datetime, int $duration ) { function wp_zoom_get_purchase_products( string $webinar_id ) { global $wpdb; - $cache = Cache::get( 'wp_zoom_webinar_purchase_products_' . $webinar_id ); - - if ( false !== $cache ) { - return $cache; - } - $webinar_id = (string) intval( $webinar_id ); - - // phpcs:ignore - $products = $wpdb->get_col( - " - SELECT pm.post_id - FROM {$wpdb->postmeta} pm + $products = array(); + $_products = Cache::get( 'wp_zoom_webinar_purchase_products' ); + + if ( false === $_products ) { + // phpcs:ignore + $_products = $wpdb->get_results( + " + SELECT pm.post_id, pm.meta_value + FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->postmeta} pm2 - ON pm2.meta_key = '_wp_zoom_webinars' - AND pm2.meta_value LIKE '%{$webinar_id}%' - AND pm2.post_id = pm.post_id - WHERE pm.meta_key = '_wp_zoom_purchase_url' - AND pm.meta_value = 'yes' - " - ); + ON pm2.post_id = pm.post_id + AND pm2.meta_key = '_wp_zoom_purchase_url' + WHERE pm.meta_key = '_wp_zoom_webinars' + AND pm2.meta_value = 'yes' + GROUP BY pm.post_id + ", + ARRAY_A + ); - if ( empty( $products ) ) { - return null; + if ( empty( $_products ) ) { + return null; + } + + Cache::set( 'wp_zoom_webinar_purchase_products', $_products ); } - Cache::set( 'wp_zoom_webinar_purchase_products_' . $webinar_id, $products ); + foreach ( $_products as $product ) { + $webinars = maybe_unserialize( $product['meta_value'] ); + + if ( in_array( $webinar_id, $webinars ) ) { + $products[] = $product['post_id']; + } + } - return $products; + return array_unique( $products ); } /** @@ -173,7 +180,7 @@ function wp_zoom_get_occurrences( $type = 'webinars', $show_past = false ) { foreach ( $objects[ $type ] as $object ) { // phpcs:ignore WordPress.PHP.StrictComparisons - if ( $type == 8 || $type == 9 ) { + if ( $object['type'] == 8 || $object['type'] == 9 ) { $object = call_user_func_array( array( $wp_zoom, 'get_' . substr( $type, 0, -1 ) ), array( $object['id'] ) ); } diff --git a/templates/shortcodes/list-single.php b/templates/shortcodes/list-single.php index 6e3dbfb..ce10e33 100644 --- a/templates/shortcodes/list-single.php +++ b/templates/shortcodes/list-single.php @@ -7,25 +7,25 @@ ?> -
+
-
-
-
+
+
+

%s', esc_url( $args['url'] ), esc_html( $args['data']['topic'] ) ); + if ( $args['product'] ) { + printf( '%s', esc_url( get_permalink( $args['product'] ) ), esc_html( $args['topic'] ) ); } else { - echo esc_html( $args['data']['topic'] ); + echo esc_html( $args['topic'] ); } ?>

-
+
diff --git a/templates/shortcodes/list.php b/templates/shortcodes/list.php index a0ec92b..4023151 100644 --- a/templates/shortcodes/list.php +++ b/templates/shortcodes/list.php @@ -11,17 +11,10 @@ $object, - 'product' => $purchase_product, - 'url' => $purchase_url, - ) + $object ); } } else {