Skip to content

Commit

Permalink
feat: allow filtering shortcode list occurrences by woocommerce category
Browse files Browse the repository at this point in the history
  • Loading branch information
dkjensen committed Jun 11, 2021
1 parent 71ad5b0 commit 710f3ae
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'] )
)
Expand All @@ -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 );
4 changes: 2 additions & 2 deletions includes/shortcodes/wp-zoom-list-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function wp_zoom_list_shortcode( $atts, $content = '' ) {
'type' => 'webinars',
'per_page' => 20,
'show_past' => 0,
'category' => null,
),
$atts
);
Expand All @@ -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 ) {
Expand All @@ -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(
Expand Down
55 changes: 31 additions & 24 deletions includes/wp-zoom-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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'] ) );
}

Expand Down
16 changes: 8 additions & 8 deletions templates/shortcodes/list-single.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@

?>

<div class="wp-zoom-list-item" data-id="<?php echo esc_attr( $args['data']['id'] ); ?>">
<div class="wp-zoom-list-item" data-id="<?php echo esc_attr( $args['id'] ); ?>">
<div class="wp-zoom-list-item--date">
<div class="wp-zoom-list-item--calendar">
<div class="wp-zoom-list-item--calendar-month"><?php echo esc_html( wp_zoom_format_date_time( $args['data']['start_time'], '', 'M' ) ); ?></div>
<div class="wp-zoom-list-item--calendar-day"><?php echo esc_html( wp_zoom_format_date_time( $args['data']['start_time'], '', 'j' ) ); ?></div>
<div class="wp-zoom-list-item--calendar-weekday"><?php echo esc_html( wp_zoom_format_date_time( $args['data']['start_time'], '', 'l' ) ); ?></div>
<div class="wp-zoom-list-item--calendar-month"><?php echo esc_html( wp_zoom_format_date_time( $args['start_time'], '', 'M' ) ); ?></div>
<div class="wp-zoom-list-item--calendar-day"><?php echo esc_html( wp_zoom_format_date_time( $args['start_time'], '', 'j' ) ); ?></div>
<div class="wp-zoom-list-item--calendar-weekday"><?php echo esc_html( wp_zoom_format_date_time( $args['start_time'], '', 'l' ) ); ?></div>
</div>
</div>
<div class="wp-zoom-list-item--info">
<h3 class="wp-zoom-list-item--info-topic">
<?php
if ( $args['url'] ) {
printf( '<a href="%s">%s</a>', esc_url( $args['url'] ), esc_html( $args['data']['topic'] ) );
if ( $args['product'] ) {
printf( '<a href="%s">%s</a>', esc_url( get_permalink( $args['product'] ) ), esc_html( $args['topic'] ) );
} else {
echo esc_html( $args['data']['topic'] );
echo esc_html( $args['topic'] );
}
?>
</h3>
<div class="wp-zoom-list-item--info-date"><?php echo esc_html( wp_zoom_format_date_time( $args['data']['start_time'] ) ); ?></div>
<div class="wp-zoom-list-item--info-date"><?php echo esc_html( wp_zoom_format_date_time( $args['start_time'] ) ); ?></div>

<?php do_action( 'wp_zoom_list_after_info', $args ); ?>

Expand Down
9 changes: 1 addition & 8 deletions templates/shortcodes/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@
<?php
if ( ! empty( $args['data'] ) ) {
foreach ( $args['data'] as $object ) {
$purchase_product = wp_zoom_get_purchase_product( $object['id'] );
$purchase_url = $purchase_product ? get_permalink( $purchase_product ) : null;

wp_zoom_load_template(
'shortcodes/list-single.php',
false,
array(
'data' => $object,
'product' => $purchase_product,
'url' => $purchase_url,
)
$object
);
}
} else {
Expand Down

0 comments on commit 710f3ae

Please sign in to comment.