MailPoet\EmailEditor\Engine

User_Theme::ensure_theme_post()privateWC 1.0

Ensures that the user theme post exists and is loaded.

Method of the class: User_Theme{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->ensure_theme_post(): void;

User_Theme::ensure_theme_post() code WC 9.8.1

private function ensure_theme_post(): void {
	if ( $this->user_theme_post ) {
		return;
	}
	$this->user_theme_post = get_page_by_path( self::USER_THEME_POST_NAME, OBJECT, 'wp_global_styles' );
	if ( $this->user_theme_post instanceof WP_Post ) {
		return;
	}
	$post_data = array(
		'post_title'   => __( 'Custom Email Styles', 'woocommerce' ),
		'post_name'    => self::USER_THEME_POST_NAME,
		'post_content' => (string) wp_json_encode( self::INITIAL_THEME_DATA, JSON_FORCE_OBJECT ),
		'post_status'  => 'publish',
		'post_type'    => 'wp_global_styles',
	);

	/**
	 * The doc is needed since PHPStan thinks that wp_insert_post can't return WP_Error.
	 *
	 * @var int|\WP_Error $post_id
	 */
	$post_id = wp_insert_post( $post_data );

	if ( is_wp_error( $post_id ) ) {
		throw new \Exception( 'Error creating user theme post: ' . esc_html( $post_id->get_error_message() ) );
	}

	$this->user_theme_post = get_post( $post_id );
}