WPF_ADDEDTOPIC::added_topic( $args )


Source Source

File: src/integrations/wpforo/triggers/wpf-addedtopic.php

	public function added_topic( $args ) {



		if ( isset( $args['forumid'] ) ) {
			$forum_id = absint( $args['forumid'] );
		} else {
			return;
		}

		if ( isset( $args['topicid'] ) ) {
			$topic_id = absint( $args['topicid'] );
		} else {
			return;
		}

		// Get all recipes that have the "$this->trigger_code = 'ADDEDTOPIC'" trigger
		$recipes = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		// Get the specific WPFFORUMID meta data from the recipes
		$recipe_trigger_meta_data = Automator()->get->meta_from_recipes( $recipes, 'WPFFORUMID' );
		$matched_recipe_ids       = array();

		// Loop through recipe
		foreach ( $recipe_trigger_meta_data as $recipe_id => $trigger_meta ) {
			// Loop through recipe WPFFORUMID trigger meta data
			foreach ( $trigger_meta as $trigger_id => $required_forum_id ) {
				if (
					0 === absint( $required_forum_id ) || // Any forum is set as the option
					$forum_id === absint( $required_forum_id ) // Match specific forum
				) {
					$matched_recipe_ids[] = [
						'recipe_id'  => $recipe_id,
						'trigger_id' => $trigger_id,
					];
				}
			}
		}

		if ( ! empty( $matched_recipe_ids ) ) {
			foreach ( $matched_recipe_ids as $matched_recipe_id ) {
				$pass_args = [
					'code'             => $this->trigger_code,
					'meta'             => $this->trigger_meta,
					'user_id'          => get_current_user_id(),
					'recipe_to_match'  => $matched_recipe_id['recipe_id'],
					'trigger_to_match' => $matched_recipe_id['trigger_id'],
					'ignore_post_id'   => true,
				];

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

				if ( $args ) {
					foreach ( $args as $result ) {
						if ( true === $result['result'] ) {

							$trigger_meta = [
								'user_id'        => get_current_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']   = 'WPFORO_TOPIC_ID';
							$trigger_meta['meta_value'] = $topic_id;

							Automator()->insert_trigger_meta( $trigger_meta );

							$trigger_meta['meta_key']   = 'WPFORO_TOPIC_FORUM_ID';
							$trigger_meta['meta_value'] = $forum_id;

							Automator()->insert_trigger_meta( $trigger_meta );

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

		}
	}