| 1 |
<?php |
| 2 |
/** |
| 3 |
* Email Template Custom Post Type |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\EmailTemplates |
| 6 |
* @since 4.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Forge12\DoubleOptIn\EmailTemplates; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class EmailTemplatePostType |
| 17 |
* |
| 18 |
* Registers and manages the doi_email_template custom post type. |
| 19 |
*/ |
| 20 |
class EmailTemplatePostType { |
| 21 |
|
| 22 |
/** |
| 23 |
* Post type slug. |
| 24 |
*/ |
| 25 |
const POST_TYPE = 'doi_email_template'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Meta key for blocks JSON. |
| 29 |
*/ |
| 30 |
const META_BLOCKS_JSON = '_doi_blocks_json'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Meta key for global styles. |
| 34 |
*/ |
| 35 |
const META_GLOBAL_STYLES = '_doi_global_styles'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Meta key for thumbnail. |
| 39 |
*/ |
| 40 |
const META_THUMBNAIL = '_doi_thumbnail'; |
| 41 |
|
| 42 |
/** |
| 43 |
* Initialize the custom post type. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function init(): void { |
| 48 |
add_action( 'init', array( $this, 'register' ) ); |
| 49 |
add_action( 'init', array( $this, 'registerMeta' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Register the custom post type. |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function register(): void { |
| 58 |
$labels = array( |
| 59 |
'name' => _x( 'Email Templates', 'Post type general name', 'double-opt-in' ), |
| 60 |
'singular_name' => _x( 'Email Template', 'Post type singular name', 'double-opt-in' ), |
| 61 |
'menu_name' => _x( 'Email Templates', 'Admin Menu text', 'double-opt-in' ), |
| 62 |
'name_admin_bar' => _x( 'Email Template', 'Add New on Toolbar', 'double-opt-in' ), |
| 63 |
'add_new' => __( 'Add New', 'double-opt-in' ), |
| 64 |
'add_new_item' => __( 'Add New Email Template', 'double-opt-in' ), |
| 65 |
'new_item' => __( 'New Email Template', 'double-opt-in' ), |
| 66 |
'edit_item' => __( 'Edit Email Template', 'double-opt-in' ), |
| 67 |
'view_item' => __( 'View Email Template', 'double-opt-in' ), |
| 68 |
'all_items' => __( 'All Email Templates', 'double-opt-in' ), |
| 69 |
'search_items' => __( 'Search Email Templates', 'double-opt-in' ), |
| 70 |
'parent_item_colon' => __( 'Parent Email Templates:', 'double-opt-in' ), |
| 71 |
'not_found' => __( 'No email templates found.', 'double-opt-in' ), |
| 72 |
'not_found_in_trash' => __( 'No email templates found in Trash.', 'double-opt-in' ), |
| 73 |
'featured_image' => _x( 'Email Template Cover Image', 'Overrides the "Featured Image" phrase', 'double-opt-in' ), |
| 74 |
'set_featured_image' => _x( 'Set cover image', 'Overrides the "Set featured image" phrase', 'double-opt-in' ), |
| 75 |
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the "Remove featured image" phrase', 'double-opt-in' ), |
| 76 |
'use_featured_image' => _x( 'Use as cover image', 'Overrides the "Use as featured image" phrase', 'double-opt-in' ), |
| 77 |
'archives' => _x( 'Email Template archives', 'The post type archive label', 'double-opt-in' ), |
| 78 |
'insert_into_item' => _x( 'Insert into email template', 'Overrides the "Insert into post" phrase', 'double-opt-in' ), |
| 79 |
'uploaded_to_this_item' => _x( 'Uploaded to this email template', 'Overrides the "Uploaded to this post" phrase', 'double-opt-in' ), |
| 80 |
'filter_items_list' => _x( 'Filter email templates list', 'Screen reader text', 'double-opt-in' ), |
| 81 |
'items_list_navigation' => _x( 'Email templates list navigation', 'Screen reader text', 'double-opt-in' ), |
| 82 |
'items_list' => _x( 'Email templates list', 'Screen reader text', 'double-opt-in' ), |
| 83 |
); |
| 84 |
|
| 85 |
$args = array( |
| 86 |
'labels' => $labels, |
| 87 |
'public' => false, |
| 88 |
'publicly_queryable' => false, |
| 89 |
'show_ui' => false, // We use custom UI |
| 90 |
'show_in_menu' => false, |
| 91 |
'query_var' => false, |
| 92 |
'rewrite' => false, |
| 93 |
'capability_type' => 'post', |
| 94 |
'has_archive' => false, |
| 95 |
'hierarchical' => false, |
| 96 |
'menu_position' => null, |
| 97 |
'supports' => array( 'title' ), |
| 98 |
'show_in_rest' => true, |
| 99 |
'rest_base' => 'doi-email-templates', |
| 100 |
'rest_controller_class' => 'WP_REST_Posts_Controller', |
| 101 |
); |
| 102 |
|
| 103 |
register_post_type( self::POST_TYPE, $args ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Register post meta fields. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function registerMeta(): void { |
| 112 |
register_post_meta( |
| 113 |
self::POST_TYPE, |
| 114 |
self::META_BLOCKS_JSON, |
| 115 |
array( |
| 116 |
'type' => 'string', |
| 117 |
'description' => 'Block structure as JSON', |
| 118 |
'single' => true, |
| 119 |
'show_in_rest' => true, |
| 120 |
'sanitize_callback' => array( self::class, 'sanitizeJson' ), |
| 121 |
'auth_callback' => function () { |
| 122 |
return current_user_can( 'manage_options' ); |
| 123 |
}, |
| 124 |
) |
| 125 |
); |
| 126 |
|
| 127 |
register_post_meta( |
| 128 |
self::POST_TYPE, |
| 129 |
self::META_GLOBAL_STYLES, |
| 130 |
array( |
| 131 |
'type' => 'string', |
| 132 |
'description' => 'Global styles (fonts, colors)', |
| 133 |
'single' => true, |
| 134 |
'show_in_rest' => true, |
| 135 |
'sanitize_callback' => array( self::class, 'sanitizeJson' ), |
| 136 |
'auth_callback' => function () { |
| 137 |
return current_user_can( 'manage_options' ); |
| 138 |
}, |
| 139 |
) |
| 140 |
); |
| 141 |
|
| 142 |
register_post_meta( |
| 143 |
self::POST_TYPE, |
| 144 |
self::META_THUMBNAIL, |
| 145 |
array( |
| 146 |
'type' => 'string', |
| 147 |
'description' => 'Preview thumbnail (Base64 or URL)', |
| 148 |
'single' => true, |
| 149 |
'show_in_rest' => true, |
| 150 |
'sanitize_callback' => 'sanitize_text_field', |
| 151 |
'auth_callback' => function () { |
| 152 |
return current_user_can( 'manage_options' ); |
| 153 |
}, |
| 154 |
) |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Sanitize JSON input. |
| 160 |
* |
| 161 |
* @param string $value The value to sanitize. |
| 162 |
* @return string Sanitized JSON string. |
| 163 |
*/ |
| 164 |
public static function sanitizeJson( $value ): string { |
| 165 |
if ( empty( $value ) ) { |
| 166 |
return ''; |
| 167 |
} |
| 168 |
|
| 169 |
// If it's already a valid JSON string, return it as-is |
| 170 |
// We trust the input since it comes from our own code |
| 171 |
$decoded = json_decode( $value, true ); |
| 172 |
if ( json_last_error() === JSON_ERROR_NONE ) { |
| 173 |
// Valid JSON - return the original to preserve formatting |
| 174 |
return $value; |
| 175 |
} |
| 176 |
|
| 177 |
// Invalid JSON — return empty. |
| 178 |
return ''; |
| 179 |
} |
| 180 |
} |
| 181 |
|