| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace YayMail\TemplateLibrary; |
| 4 | 4 | |
| 5 | 5 | use YayMail\Abstracts\BaseTemplate; |
| 6 | +use YayMail\Models\LibraryTemplateModel; | |
| 6 | 7 | use YayMail\Utils\SingletonTrait; |
| 7 | 8 | |
| 8 | 9 | /** |
| 9 | 10 | * Service responsible for registering and exposing code-defined email templates |
| @@ -66,18 +67,91 @@ | ||
| 66 | 67 | |
| 67 | 68 | usort( |
| 68 | 69 | $results, |
| 69 | 70 | function( $a, $b ) { |
| 70 | - $pos_a = isset( $a['position'] ) ? (int) $a['position'] : 10; | |
| 71 | - $pos_b = isset( $b['position'] ) ? (int) $b['position'] : 10; | |
| 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 ) : ''; | |
| 72 | 77 | |
| 73 | - if ( $pos_a === $pos_b ) { | |
| 74 | - return strcmp( (string) ( $a['name'] ?? '' ), (string) ( $b['name'] ?? '' ) ); | |
| 78 | + if ( $day_a === $day_b ) { | |
| 79 | + return strnatcasecmp( (string) ( $a['name'] ?? '' ), (string) ( $b['name'] ?? '' ) ); | |
| 75 | 80 | } |
| 76 | 81 | |
| 77 | - return $pos_a - $pos_b; | |
| 82 | + return strcmp( $day_b, $day_a ); | |
| 78 | 83 | } |
| 79 | 84 | ); |
| 80 | 85 | |
| 81 | 86 | return $results; |
| 82 | 87 | } |
| 83 | -} | |
| 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 | +} | |