-
Notifications
You must be signed in to change notification settings - Fork 2
/
wp-discourse-woocommerce-support.php
64 lines (51 loc) · 1.72 KB
/
wp-discourse-woocommerce-support.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Plugin Name: WP Discourse WooCommerce Support
* Description: Extends the WP Discourse plugin to allow it to be used with WooCommerce
* Version: 0.2
* Author: scossar
*/
namespace WPDiscourse\WooCommerceSupport;
add_action( 'plugins_loaded', __NAMESPACE__ . '\\init' );
function init() {
if ( class_exists( '\WPDiscourse\Discourse\Discourse' ) ) {
$woocommerce_support = new \WPDiscourse\WooCommerceSupport\WooCommerceSupport();
}
}
class WooCommerceSupport {
public function __construct() {
add_filter( 'woocommerce_login_redirect', array( $this, 'set_redirect' ) );
add_filter( 'woocommerce_registration_redirect', array( $this, 'set_redirect' ) );
add_filter( 'woocommerce_product_get_review_count', array( $this, 'comments_number' ) );
}
/**
* Sets the login redirect so that it can include the query parameters required for single sign on with Discourse.
*
* @param string $redirect The redirect URL supplied by WooCommerce.
*
* @return mixed
*/
public function set_redirect( $redirect ) {
if ( isset( $_GET['redirect_to'] ) && esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ) { // Input var okay.
$redirect = esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ); // Input var okay.
return $redirect;
}
return $redirect;
}
/**
* Replaces the WooCommerce comments count with the Discourse comments count.
*
* @param int $count The comments count returned from WooCommerce.
*
* @return mixed
*/
function comments_number( $count ) {
global $post;
$discourse_post_id = get_post_meta( $post->ID, 'discourse_post_id', true );
if ( $discourse_post_id > 0 ) {
$count = get_post_meta( $post->ID, 'discourse_comments_count', true );
return $count;
}
return $count;
}
}