__( 'Email templates', 'mailpuff' ), 'public' => false, 'show_ui' => false, 'show_in_rest' => false, 'rewrite' => false, 'query_var' => false, 'supports' => array( 'title' ), 'capability_type' => 'post', ) ); } /** * One template, or null. * * @param int $id Template ID. * @param bool $body With its content and HTML. * @return array|null */ public static function find( $id, $body = true ) { $post = get_post( (int) $id ); if ( ! $post || self::POST_TYPE !== $post->post_type || 'trash' === $post->post_status ) { return null; } $out = array( 'id' => (int) $post->ID, 'name' => (string) $post->post_title, 'created_at' => (string) $post->post_date_gmt, 'updated_at' => (string) $post->post_modified_gmt, ); if ( $body ) { $out['content_blocks'] = (string) get_post_meta( $post->ID, self::META_BLOCKS, true ); $out['content_html'] = (string) get_post_meta( $post->ID, self::META_HTML, true ); } return $out; } /** * Every template, newest first, without their bodies. * * @return array[] */ public static function all() { $out = array(); foreach ( get_posts( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => self::MAX, 'orderby' => 'modified', 'order' => 'DESC', ) ) as $post ) { $out[] = self::find( $post->ID, false ); } return $out; } /** * Keep an email as a template. * * @param string $name Name. * @param string $content_blocks The editor's content, JSON. * @param string $content_html The HTML. * @return int|WP_Error The template's ID. */ public static function create( $name, $content_blocks, $content_html ) { $name = self::name( $name ); if ( is_wp_error( $name ) ) { return $name; } if ( count( self::all() ) >= self::MAX ) { /* translators: %d: the most templates. */ return new WP_Error( 'mailpuff_too_many_templates', sprintf( __( 'A site keeps at most %d templates. Delete one first.', 'mailpuff' ), self::MAX ), array( 'status' => 400 ) ); } $blocks = self::without_preview( (string) $content_blocks ); if ( '' === $blocks && '' === trim( (string) $content_html ) ) { return new WP_Error( 'mailpuff_empty_template', __( 'The email has nothing in it to keep.', 'mailpuff' ), array( 'status' => 400 ) ); } $id = wp_insert_post( array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'post_title' => $name, ), true ); if ( is_wp_error( $id ) ) { return $id; } update_post_meta( $id, self::META_BLOCKS, wp_slash( $blocks ) ); update_post_meta( $id, self::META_HTML, wp_slash( self::strip_preview( (string) $content_html ) ) ); return (int) $id; } /** * Keep a campaign's email as a template. * * @param int $campaign_id Campaign ID. * @param string $name Name. * @return int|WP_Error */ public static function from_campaign( $campaign_id, $name ) { $campaign = Mailpuff_Campaign::find( (int) $campaign_id ); if ( ! $campaign || Mailpuff_System_Emails::is_system_type( (string) $campaign->type ) ) { return new WP_Error( 'mailpuff_invalid_campaign', __( 'No campaign has that id.', 'mailpuff' ), array( 'status' => 404 ) ); } return self::create( $name, (string) $campaign->content_blocks, (string) $campaign->content_html ); } /** * Rename a template. * * @param int $id Template ID. * @param string $name New name. * @return true|WP_Error */ public static function rename( $id, $name ) { if ( ! self::find( $id, false ) ) { return self::missing(); } $name = self::name( $name ); if ( is_wp_error( $name ) ) { return $name; } wp_update_post( array( 'ID' => (int) $id, 'post_title' => $name, ) ); return true; } /** * Delete a template for good. Campaigns made from it keep their email. * * @param int $id Template ID. * @return true|WP_Error */ public static function delete( $id ) { if ( ! self::find( $id, false ) ) { return self::missing(); } wp_delete_post( (int) $id, true ); return true; } /** * A template's body as campaign columns, with a campaign's preview text * (the editor puts it back in its own render when the email is saved). * * @param int $id Template ID. * @param string $preview The campaign's preview text. * @return array{content_html:string,content_blocks:string}|WP_Error */ public static function body( $id, $preview = '' ) { $template = self::find( $id ); if ( ! $template ) { return self::missing(); } $html = $template['content_html']; $preview = trim( (string) $preview ); if ( '' !== $preview ) { $html = (string) preg_replace( '#]*>#i', '$0', $html, 1 ); } return array( 'content_html' => $html, 'content_blocks' => $template['content_blocks'], ); } /** * @return WP_Error No such template. */ public static function missing() { return new WP_Error( 'mailpuff_invalid_template', __( 'No template has that id.', 'mailpuff' ), array( 'status' => 404 ) ); } /** * A clean name: 1 to 100 characters. * * @param string $name Name. * @return string|WP_Error */ private static function name( $name ) { $name = trim( sanitize_text_field( (string) $name ) ); if ( '' === $name ) { return new WP_Error( 'mailpuff_missing_name', __( 'Give the template a name.', 'mailpuff' ), array( 'status' => 400 ) ); } return function_exists( 'mb_substr' ) ? mb_substr( $name, 0, 100 ) : substr( $name, 0, 100 ); } /** * The editor's content without the source campaign's preview text. * * @param string $json `{ v, settings, doc }`. * @return string */ private static function without_preview( $json ) { $data = json_decode( $json, true ); if ( ! is_array( $data ) ) { return ''; } if ( isset( $data['settings'] ) && is_array( $data['settings'] ) ) { unset( $data['settings']['previewText'] ); } return (string) wp_json_encode( $data ); } /** * The HTML without its preview text: MJML's hidden preview block and the * fragment renderer's (Mailpuff_Email_Renderer::preheader()). * * @param string $html HTML. * @return string */ private static function strip_preview( $html ) { return (string) preg_replace( '#
]*>.*?
#s', '', $html, 1 ); } }