WP_POSTRECEIVESCOMMENT::post_receives_comment( $comment_id,  $comment_approved,  $commentdata )


Parameters Parameters

$comment_id

(Required)

$comment_approved

(Required)

$commentdata

(Required)


Source Source

File: src/integrations/wp/triggers/wp-postreceivescomment.php

	public function post_receives_comment( $comment_id, $comment_approved, $commentdata ) {
		$recipes            = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$required_post      = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
		$user_id            = get_post_field( 'post_author', (int) $commentdata['comment_post_ID'] );
		$user_obj           = get_user_by( 'ID', (int) $user_id );
		$matched_recipe_ids = [];

		//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 ( - 1 === intval( $required_post[ $recipe_id ][ $trigger_id ] ) ||
				     $required_post[ $recipe_id ][ $trigger_id ] == $commentdata['comment_post_ID'] ) {
					$matched_recipe_ids[] = [
						'recipe_id'  => $recipe_id,
						'trigger_id' => $trigger_id,
					];
				}
			}
		}

		if ( ! empty( $matched_recipe_ids ) ) {
			foreach ( $matched_recipe_ids as $matched_recipe_id ) {
				if ( ! Automator()->is_recipe_completed( $matched_recipe_id['recipe_id'], $user_id ) ) {
					$pass_args = [
						'code'             => $this->trigger_code,
						'meta'             => $this->trigger_meta,
						'user_id'          => $user_id,
						'recipe_to_match'  => $matched_recipe_id['recipe_id'],
						'trigger_to_match' => $matched_recipe_id['trigger_id'],
						'post_id'          => $commentdata['comment_post_ID'],
					];

					$args = Automator()->maybe_add_trigger_entry( $pass_args, false );
					if ( $args ) {
						foreach ( $args as $result ) {
							if ( true === $result['result'] ) {
								$trigger_meta = [
									'user_id'        => (int) $user_id,
									'trigger_id'     => $result['args']['trigger_id'],
									'trigger_log_id' => $result['args']['get_trigger_id'],
									'run_number'     => $result['args']['run_number'],
								];

								$trigger_meta['meta_key']   = 'WPPOSTTYPES';
								$trigger_meta['meta_value'] = maybe_serialize( get_post_field( 'post_type', (int) $commentdata['comment_post_ID'] ) );
								Automator()->insert_trigger_meta( $trigger_meta );

								$trigger_meta['meta_key']   = 'POSTTITLE';
								$trigger_meta['meta_value'] = maybe_serialize( get_post_field( 'post_title', (int) $commentdata['comment_post_ID'] ) );
								Automator()->insert_trigger_meta( $trigger_meta );

								$trigger_meta['meta_key']   = 'POSTAUTHORDN';
								$trigger_meta['meta_value'] = maybe_serialize( $user_obj->display_name );
								Automator()->insert_trigger_meta( $trigger_meta );

								$trigger_meta['meta_key']   = 'POSTCOMMENTCONTENT';
								$trigger_meta['meta_value'] = maybe_serialize( $commentdata['comment_content'] );
								Automator()->insert_trigger_meta( $trigger_meta );

								$trigger_meta['meta_key']   = 'POSTCOMMENTDATE';
								$trigger_meta['meta_value'] = maybe_serialize( $commentdata['comment_date'] );
								Automator()->insert_trigger_meta( $trigger_meta );

								$trigger_meta['meta_key']   = 'POSTCOMMENTERNAME';
								$trigger_meta['meta_value'] = maybe_serialize( $commentdata['comment_author'] );
								Automator()->insert_trigger_meta( $trigger_meta );

								$trigger_meta['meta_key']   = 'POSTCOMMENTEREMAIL';
								$trigger_meta['meta_value'] = maybe_serialize( $commentdata['comment_author_email'] );
								Automator()->insert_trigger_meta( $trigger_meta );

								$trigger_meta['meta_key']   = 'POSTCOMMENTSTATUS';
								$trigger_meta['meta_value'] = maybe_serialize( wp_get_comment_status( (int) $comment_id ) );
								Automator()->insert_trigger_meta( $trigger_meta );

								Automator()->maybe_trigger_complete( $result['args'] );
							}
						}
					}
				}
			}
		}

	}