WP_USERCREATESPOST::post_published( $post_id )

Fires when a post is transitioned from one status to another.


Parameters Parameters

$post_id

(Required)


Source Source

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

	public function post_published( $new_status, $old_status, $post ) {



		$is_draft_to_publish = Automator()->helpers->recipe->wp->is_draft_to_publish( $new_status, $old_status, $post );

		// Bailout if status is not 'draft' to 'publish'.
		if ( ! $is_draft_to_publish ) {
			return false;
		}

		$user_id                = get_current_user_id();
		$recipes                = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$required_post_type     = Automator()->get->meta_from_recipes( $recipes, 'WPPOSTTYPES' );
		$required_post_taxonomy = Automator()->get->meta_from_recipes( $recipes, 'WPTAXONOMIES' );
		$required_post_term     = Automator()->get->meta_from_recipes( $recipes, 'WPTAXONOMYTERM' );

		$matched_recipe_ids = array();
		$terms_list         = array();
		$taxonomy_names     = array();

		$user_obj = get_user_by( 'ID', (int) $post->post_author );

		if ( ! is_wp_error( $user_obj ) ) {
			foreach ( $recipes as $recipe_id => $recipe ) {
				foreach ( $recipe['triggers'] as $trigger ) {

					$trigger_id = $trigger['ID'];

					if ( '0' !== $required_post_term[ $recipe_id ][ $trigger_id ] ) {
						// if the term is specific then tax and post type are also specific
						// check if the post has the required term
						if (
						has_term(
							$required_post_term[ $recipe_id ][ $trigger_id ],
							$required_post_taxonomy[ $recipe_id ][ $trigger_id ],
							$post
						)
						) {
							// Matched the post term
							$matched_recipe_ids[] = array(
								'recipe_id'  => $recipe_id,
								'trigger_id' => $trigger_id,
							);

							// Specific Term
							$term         = get_term( $required_post_term[ $recipe_id ][ $trigger_id ] );
							$terms_list[] = $term->name;
						}
						continue;
					}

					if ( '0' !== $required_post_taxonomy[ $recipe_id ][ $trigger_id ] ) {
						// let check if the post has any term in the selected taxonomy
						if (
						has_term(
							null,
							$required_post_taxonomy[ $recipe_id ][ $trigger_id ],
							$post
						)
						) {
							// Matched the post term
							$matched_recipe_ids[] = array(
								'recipe_id'  => $recipe_id,
								'trigger_id' => $trigger_id,
							);

							// All Post Terms for specific taxonomy
							$terms = wp_get_post_terms( $post->ID, $required_post_taxonomy[ $recipe_id ][ $trigger_id ] );

							foreach ( $terms as $term ) {
								$terms_list [] = $term->name;
							}
						}
						continue;
					}

					$taxonomies = get_object_taxonomies( $post->post_type, 'object' );

					foreach ( $taxonomies as $taxonomy ) {
						$taxonomy_names [] = $taxonomy->name;
					}

					// All Post Terms for specific taxonomy
					$terms = wp_get_post_terms( $post->ID, $taxonomy_names );

					foreach ( $terms as $term ) {
						$terms_list [] = $term->name;
					}

					if ( '0' !== $required_post_type[ $recipe_id ][ $trigger_id ] ) {
						if ( $post->post_type === $required_post_type[ $recipe_id ][ $trigger_id ] ) {
							// Matched the post type
							$matched_recipe_ids[] = array(
								'recipe_id'  => $recipe_id,
								'trigger_id' => $trigger_id,
							);
						}
						continue;
					}

					// All fields are set to "any" by deductive reasoning
					// Matched the post term
					$matched_recipe_ids[] = array(
						'recipe_id'  => $recipe_id,
						'trigger_id' => $trigger_id,
					);
				}
			}
		}

		if ( ! empty( $matched_recipe_ids ) ) {
			foreach ( $matched_recipe_ids as $matched_recipe_id ) {
				$pass_args = array(
					'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'],
					'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 = array(
								'user_id'        => $user_id,
								'trigger_id'     => $result['args']['trigger_id'],
								'trigger_log_id' => $result['args']['get_trigger_id'],
								'run_number'     => $result['args']['run_number'],
							);

							// Post Title Token
							$trigger_meta['meta_key']   = $result['args']['trigger_id'] . ':' . $this->trigger_code . ':POSTTITLE';
							$trigger_meta['meta_value'] = maybe_serialize( $post->post_title );
							Automator()->insert_trigger_meta( $trigger_meta );

							// Post ID Token
							$trigger_meta['meta_key']   = $result['args']['trigger_id'] . ':' . $this->trigger_code . ':POSTID';
							$trigger_meta['meta_value'] = maybe_serialize( $post->ID );
							Automator()->insert_trigger_meta( $trigger_meta );

							// Post URL Token
							$trigger_meta['meta_key']   = $result['args']['trigger_id'] . ':' . $this->trigger_code . ':POSTURL';
							$trigger_meta['meta_value'] = maybe_serialize( get_permalink( $post->ID ) );
							Automator()->insert_trigger_meta( $trigger_meta );

							// Post Content Token
							$trigger_meta['meta_key']   = $result['args']['trigger_id'] . ':' . $this->trigger_code . ':POSTCONTENT';
							$trigger_meta['meta_value'] = maybe_serialize( $post->post_content );
							Automator()->insert_trigger_meta( $trigger_meta );

							// Post Type Token
							$trigger_meta['meta_key']   = $result['args']['trigger_id'] . ':' . $this->trigger_code . ':WPPOSTTYPES';
							$trigger_meta['meta_value'] = maybe_serialize( $post->post_type );
							Automator()->insert_trigger_meta( $trigger_meta );

							// Post terms Token
							// All Post Terms for specific taxonomy
							$terms           = wp_get_post_terms( $post->ID, $taxonomy_names );
							$terms_list_save = array();
							foreach ( $terms as $term ) {
								$terms_list_save [] = $term->name;
							}
							$trigger_meta['meta_key']   = $result['args']['trigger_id'] . ':' . $this->trigger_code . ':WPTAXONOMYTERM';
							$trigger_meta['meta_value'] = maybe_serialize( implode( ', ', $terms_list_save ) );
							Automator()->insert_trigger_meta( $trigger_meta );

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

		return;
	}