| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\TemplateLibrary; |
| 4 |
|
| 5 |
use YayMail\Abstracts\BaseTemplate; |
| 6 |
use YayMail\Models\LibraryTemplateModel; |
| 7 |
use YayMail\Utils\SingletonTrait; |
| 8 |
|
| 9 |
/** |
| 10 |
* Service responsible for registering and exposing code-defined email templates |
| 11 |
* for the Template Library feature. |
| 12 |
*/ |
| 13 |
class TemplateLibraryService { |
| 14 |
use SingletonTrait; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var BaseTemplate[] |
| 18 |
*/ |
| 19 |
protected $templates = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
protected function __construct() { |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Programmatically register a template class. |
| 31 |
* |
| 32 |
* @param BaseTemplate $template Template instance. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function register( BaseTemplate $template ) { |
| 37 |
if ( ! $template instanceof BaseTemplate ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
if ( in_array( $template, $this->templates, true ) ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$template_key = $template->get_id(); |
| 46 |
|
| 47 |
$this->templates[ $template_key ] = $template; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get list of template summaries for given email type |
| 52 |
* |
| 53 |
* @param string $email_type YayMail email/template name. Example: 'new_order'. |
| 54 |
* |
| 55 |
* @return array[] |
| 56 |
*/ |
| 57 |
public function get_list( $email_type ) { |
| 58 |
$results = []; |
| 59 |
|
| 60 |
foreach ( $this->templates as $template ) { |
| 61 |
if ( $template->get_email_type() !== $email_type ) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
$results[] = $template->get_template_data(); |
| 66 |
} |
| 67 |
|
| 68 |
usort( |
| 69 |
$results, |
| 70 |
function( $a, $b ) { |
| 71 |
$date_a = strtotime( (string) ( $a['modified_date'] ?? '' ) ); |
| 72 |
$date_b = strtotime( (string) ( $b['modified_date'] ?? '' ) ); |
| 73 |
$date_a = false === $date_a ? 0 : (int) $date_a; |
| 74 |
$date_b = false === $date_b ? 0 : (int) $date_b; |
| 75 |
$day_a = $date_a > 0 ? date( 'Y-m-d', $date_a ) : ''; |
| 76 |
$day_b = $date_b > 0 ? date( 'Y-m-d', $date_b ) : ''; |
| 77 |
|
| 78 |
if ( $day_a === $day_b ) { |
| 79 |
return strnatcasecmp( (string) ( $a['name'] ?? '' ), (string) ( $b['name'] ?? '' ) ); |
| 80 |
} |
| 81 |
|
| 82 |
return strcmp( $day_b, $day_a ); |
| 83 |
} |
| 84 |
); |
| 85 |
|
| 86 |
return $results; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Built-in + user-saved templates for the library modal. |
| 91 |
* Order: saved (newest first via DB query), then builtin (newest update first, then name). |
| 92 |
* |
| 93 |
* @param string $email_type Email type slug. |
| 94 |
* |
| 95 |
* @return array[] |
| 96 |
*/ |
| 97 |
public function get_merged_list( $email_type ) { |
| 98 |
$builtin = $this->get_list( $email_type ); |
| 99 |
|
| 100 |
$saved = LibraryTemplateModel::list_by_email_type( $email_type ); |
| 101 |
|
| 102 |
$saved = array_map( |
| 103 |
function( $template ) { |
| 104 |
$template['source'] = 'saved'; |
| 105 |
return $template; |
| 106 |
}, |
| 107 |
$saved |
| 108 |
); |
| 109 |
|
| 110 |
return array_merge( $saved, $builtin ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Email types eligible for scope "all" bulk save. |
| 115 |
* Uses registered YayMail emails (same pool as editable customizer emails). |
| 116 |
* |
| 117 |
* @return string[] |
| 118 |
*/ |
| 119 |
public function get_supported_email_types_for_library() { |
| 120 |
$types = []; |
| 121 |
|
| 122 |
if ( ! function_exists( 'yaymail_get_emails' ) ) { |
| 123 |
return $types; |
| 124 |
} |
| 125 |
|
| 126 |
foreach ( yaymail_get_emails() as $email ) { |
| 127 |
$template_id = $email->get_id(); |
| 128 |
if ( ! self::is_library_bulk_email_type( $template_id ) ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
$types[] = $template_id; |
| 132 |
} |
| 133 |
|
| 134 |
return $types; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @param string|null $template_id Template id. |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
private static function is_library_bulk_email_type( $template_id ) { |
| 143 |
if ( empty( $template_id ) || ! is_string( $template_id ) ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
if ( 0 === strpos( $template_id, 'pattern_' ) ) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
if ( 0 === strpos( $template_id, 'wp-core-' ) ) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
} |