WP_CREATEPOST::create_post( $user_id,  $action_data,  $recipe_id,  $args )

Validation function when the action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)


Source Source

File: src/integrations/wp/actions/wp-createpost.php

	public function create_post( $user_id, $action_data, $recipe_id, $args ) {



		$post_title   = Automator()->parse->text( $action_data['meta']['WPCPOSTTITLE'], $recipe_id, $user_id, $args );
		$post_slug    = Automator()->parse->text( $action_data['meta']['WPCPOSTSLUG'], $recipe_id, $user_id, $args );
		$post_content = Automator()->parse->text( $action_data['meta']['WPCPOSTCONTENT'], $recipe_id, $user_id, $args );
		$post_author  = Automator()->parse->text( $action_data['meta']['WPCPOSTAUTHOR'], $recipe_id, $user_id, $args );
		$post_status  = Automator()->parse->text( $action_data['meta']['WPCPOSTSTATUS'], $recipe_id, $user_id, $args );
		$post_type    = $action_data['meta'][ $this->action_code ];

		$post_args                 = array();
		$post_args['post_title']   = sanitize_text_field( $post_title );
		$post_args['post_name']    = sanitize_title( $post_slug );
		$post_args['post_content'] = $post_content;
		$post_args['post_type']    = $post_type;
		$post_args['post_status']  = $post_status;
		$post_args['post_author']  = 0;
		if ( is_numeric( $post_author ) ) {
			$post_args['post_author'] = absint( $post_author );
		} else {
			// get author by username or email
			$user = get_user_by( 'login', $post_author );
			if ( ! $user ) {
				$user = get_user_by( 'email', $post_author );
			}
			if ( ! $user ) {
				$user = get_user_by( 'slug', $post_author );
			}
			if ( ! empty( $user ) ) {
				$post_args['post_author'] = absint( $user->ID );
			}
		}

		$post_id = wp_insert_post( $post_args );

		if ( $post_id ) {
			$meta_pairs = json_decode( $action_data['meta']['CPMETA_PAIRS'], true );
			if ( ! empty( $meta_pairs ) ) {
				foreach ( $meta_pairs as $pair ) {
					$meta_key   = sanitize_title( $pair['KEY'] );
					$meta_value = sanitize_text_field( Automator()->parse->text( $pair['VALUE'], $recipe_id, $user_id, $args ) );
					update_post_meta( $post_id, $meta_key, $meta_value );
				}
			}
		}

		Automator()->complete_action( $user_id, $action_data, $recipe_id );
	}