Action uncanny-automator-pro

uap_wp_comment_approve

Fires when a WooCommerce comment is approved, allowing for custom actions based on the comment and recipe.

add_action( 'uap_wp_comment_approve', $callback, 10, 4 );

Description

Fires when a WooCommerce product review is approved. Developers can use this action to trigger custom automations or modify data based on an approved review. It provides the comment object, matched recipe IDs, and additional arguments.


Usage

add_action( 'uap_wp_comment_approve', 'your_function_name', 10, 4 );

Parameters

$comment (mixed)
This parameter contains the `WP_Comment` object for the approved comment.
$matched_recipe_id (mixed)
This parameter contains the comment object that has been approved.
$matched_recipe_id (mixed)
This parameter contains the ID of the recipe that was matched for the approved comment.
$args (mixed)
This parameter appears to be a duplicate and is likely intended to hold the ID of the matched recipe.

Examples

// Example function to hook into the 'uap_wp_comment_approve' action.
// This function will be executed whenever a WordPress comment is approved.
// It demonstrates how to access the comment object, matched recipe/trigger IDs,
// and additional arguments passed to the action.
function my_custom_comment_approved_handler( $comment, $matched_recipe_id, $matched_trigger_id, $args ) {

    // Ensure the comment object is valid and is a WP_Comment object.
    if ( ! $comment instanceof WP_Comment ) {
        error_log( 'uap_wp_comment_approve hook received invalid comment data.' );
        return;
    }

    // You can perform various actions here based on the approved comment.
    // For example, you might want to log the event, send a notification,
    // or update a meta field related to the post.

    // Log that a comment was approved and which recipe/trigger it matched.
    if ( ! empty( $matched_recipe_id ) && ! empty( $matched_trigger_id ) ) {
        $post_title = get_the_title( $comment->comment_post_ID );
        error_log( sprintf(
            'Comment approved for post "%s" (ID: %d). Matched Recipe ID: %s, Matched Trigger ID: %s.',
            $post_title,
            $comment->comment_post_ID,
            $matched_recipe_id,
            $matched_trigger_id
        ) );
    } else {
        error_log( sprintf(
            'Comment approved for post "%s" (ID: %d). No specific recipe or trigger matched.',
            get_the_title( $comment->comment_post_ID ),
            $comment->comment_post_ID
        ) );
    }

    // Access the additional arguments if they are relevant.
    if ( ! empty( $args ) ) {
        // Example: Check if a specific key exists in the args and use it.
        if ( isset( $args['trigger_type'] ) ) {
            error_log( 'Comment approved. Trigger type from args: ' . $args['trigger_type'] );
        }
    }

    // You could potentially update post meta based on the comment.
    // For example, incrementing a comment count or marking a post as having a new approved comment.
    // Note: This is a simplified example. In a real scenario, you might want to be more specific.
    $current_comment_count = get_post_meta( $comment->comment_post_ID, 'approved_comment_count', true );
    if ( '' === $current_comment_count || false === $current_comment_count ) {
        $current_comment_count = 0;
    }
    update_post_meta( $comment->comment_post_ID, 'approved_comment_count', intval( $current_comment_count ) + 1 );

}

// Add the custom function to the 'uap_wp_comment_approve' action hook.
// The number '10' is the priority (default is 10), and '4' is the number of arguments
// our function expects ($comment, $matched_recipe_id, $matched_trigger_id, $args).
add_action( 'uap_wp_comment_approve', 'my_custom_comment_approved_handler', 10, 4 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-prodreview-approved.php:140
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-prodreview.php:136
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-prodreview-rating.php:167
uncanny-automator-pro/src/integrations/wp/triggers/wp-commentapproved.php:181

public function comment_approved( $new_status, $old_status, $comment ) {
		if ( 'review' !== (string) $comment->comment_type ) {
			return;
		}

		if ( 'approved' !== (string) $new_status ) {
			return;
		}

		if ( ! $comment instanceof WP_Comment ) {
			return;
		}

		if ( isset( $comment->user_id ) && 0 === absint( $comment->user_id ) ) {
			return;
		}

		$recipes            = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$required_post      = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
		$matched_recipe_ids = array();

		//Add where option is set to Any post / specific post
		foreach ( $recipes as $recipe_id => $recipe ) {
			foreach ( $recipe['triggers'] as $trigger ) {
				$trigger_id = $trigger['ID'];
				if ( intval( '-1' ) === intval( $required_post[ $recipe_id ][ $trigger_id ] ) || absint( $required_post[ $recipe_id ][ $trigger_id ] ) === absint( $comment->comment_post_ID ) ) {
					$matched_recipe_ids[] = array(
						'recipe_id'  => $recipe_id,
						'trigger_id' => $trigger_id,
					);
				}
			}
		}

		if ( empty( $matched_recipe_ids ) ) {
			return;
		}

		//	If recipe matches
		foreach ( $matched_recipe_ids as $matched_recipe_id ) {
			$pass_args = array(
				'code'             => $this->trigger_code,
				'meta'             => $this->trigger_meta,
				'user_id'          => $comment->user_id,
				'recipe_to_match'  => $matched_recipe_id['recipe_id'],
				'trigger_to_match' => $matched_recipe_id['trigger_id'],
				'post_id'          => $comment->comment_post_ID,
			);

			$args = Automator()->maybe_add_trigger_entry( $pass_args, false );

			do_action( 'uap_wp_comment_approve', $comment, $matched_recipe_id['recipe_id'], $matched_recipe_id['trigger_id'], $args );
			do_action( 'uap_wc_trigger_save_product_meta', $comment->comment_post_ID, $matched_recipe_id['recipe_id'], $args, 'product' );

			if ( $args ) {
				foreach ( $args as $result ) {
					if ( true === $result['result'] ) {
						Automator()->maybe_trigger_complete( $result['args'] );
					}
				}
			}
		}
	}


Scroll to Top