Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion includes/Listeners/Commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function register_hooks() {
add_action( 'woocommerce_order_status_processing', array( $this, 'on_payment' ), 10, 2 );
add_filter( 'newfold_wp_data_module_cron_data_filter', array( $this, 'products_count' ) );
add_filter( 'newfold_wp_data_module_cron_data_filter', array( $this, 'orders_count' ) );
add_filter( 'newfold_wp_data_module_cron_data_filter', array( $this, 'reviews_count' ) );
add_filter( 'woocommerce_before_cart', array( $this, 'site_cart_views' ) );
add_filter( 'woocommerce_before_checkout_form', array( $this, 'checkout_views' ) );
add_filter( 'woocommerce_thankyou', array( $this, 'thank_you_page' ) );
Expand Down Expand Up @@ -88,6 +89,50 @@ public function orders_count( $data ) {

return $data;
}
/**
* Product reviews count
*
* @param string $data Array of data to be sent to Hiive
*
* @return string Array of data
*/
public function reviews_count( $data ) {
if ( ! isset( $data['meta'] ) ) {
$data['meta'] = array();
}

global $wpdb;
$review_count = array( 'total' => 0 );
$status_map = array(
'0' => 'pending',
'1' => 'approved',
'trash' => 'trash',
'spam' => 'spam',
);
$counts = $wpdb->get_results(
"
SELECT comment_approved, COUNT(*) AS num_reviews
FROM {$wpdb->comments}
WHERE comment_type = 'review'
GROUP BY comment_approved
",
ARRAY_A
);

if ( ! empty( $counts ) ) {
foreach ( $counts as $count ) {
$status = $count['comment_approved'];
if ( array_key_exists( $status, $status_map ) ) {
$review_count[ $status_map[ $status ] ] = $count['num_reviews'];
}
$review_count['total'] += $count['num_reviews'];
}
$data['meta']['product_reviews_count'] = (int) $review_count['total'];
}

return $data;
}


/**
* Site Cart View, send data to Hiive
Expand Down Expand Up @@ -293,7 +338,7 @@ public function ecomdash_connected( $new_option, $old_option ) {
public function product_created_or_updated( $product_id, $product ) {
$data = array(
'label_key' => 'product_type',
'product_type' => $product->product_type,
'product_type' => $product->get_type(),
'post_id' => $product_id,
);

Expand Down
38 changes: 15 additions & 23 deletions includes/Listeners/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function register_hooks() {
// Post status transitions
add_action( 'transition_post_status', array( $this, 'post_status' ), 10, 3 );

// transition comment status
add_action( 'transition_comment_status', array( $this, 'comment_status' ), 10, 3 );
add_filter( 'newfold_wp_data_module_cron_data_filter', array( $this, 'comments_count' ) );
}

/**
Expand All @@ -38,7 +37,8 @@ public function post_status( $new_status, $old_status, $post ) {
/**
* Ignore all post types that aren't public
*/
if ( ! $post_type || $post_type->public !== true ) {

if ( ! $post_type || true !== $post_type->public ) {
return;
}

Expand Down Expand Up @@ -92,29 +92,21 @@ public function count_posts() {
}

/**
* Comment status transition
* Comments reviews count
*
* @param string $new_status The new comment status
* @param string $old_status The new comment status
* @param WP_Comment $comment Comment object
* @param string $data Array of data to be sent to Hiive
*
* @return void
* @return string Array of data
*/
public function comment_status( $new_status, $old_status, $comment ) {
$allowed_statuses = array(
'deleted',
'approved',
'unapproved',
'spam',
);
if ( $new_status !== $old_status && in_array( $new_status, $allowed_statuses, true ) ) {
$data = array(
'label_key' => 'new_status',
'old_status' => $old_status,
'new_status' => $new_status,
'comment' => $comment,
);
$this->push( 'comment_status', $data );
public function comments_count( $data ) {
if ( ! isset( $data['meta'] ) ) {
$data['meta'] = array();
}

$comments = wp_count_comments();
if ( isset( $comments->all ) ) {
$data['meta']['post_comments_count'] = (int) $comments->all;
}
return $data;
}
}
6 changes: 0 additions & 6 deletions includes/Listeners/SiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,5 @@ public function tests_run( $value ) {
'score' => SiteHealthHelper::calculate_score( $value ),
)
);
$this->push(
'site_health_debug',
array(
'debug_data' => wp_json_encode( SiteHealthHelper::get_safe_data() ),
)
);
}
}
42 changes: 35 additions & 7 deletions includes/Listeners/WPMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,40 @@ public function register_hooks() {
* @return void
*/
public function mail_succeeded( $mail_data ) {
$this->push(
'wp_mail',
array(
'label_key' => 'subject',
'subject' => $mail_data['subject'],
)
);

$send_event = false;
$site_admin_email = get_option( 'admin_email' );

$recipients = $mail_data['to'];

if ( ! is_array( $recipients ) ) {
$recipients = array( $recipients );
}

foreach ( $recipients as $email ) {
$email = trim( $email );

if ( $email === $site_admin_email ) {
$send_event = true;
break;
}

$user = get_user_by( 'email', $email );

if ( $user && ( in_array( 'administrator', $user->roles, true ) || in_array( 'editor', $user->roles, true ) ) ) {
$send_event = true;
break;
}
}

if ( $send_event ) {
$this->push(
'wp_mail',
array(
'label_key' => 'subject',
'subject' => $mail_data['subject'],
)
);
}
}
}