| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Abstracts; |
| 4 |
|
| 5 |
/** |
| 6 |
* Base class for code-defined email templates in Template Library. |
| 7 |
*/ |
| 8 |
abstract class BaseTemplate { |
| 9 |
/** |
| 10 |
* Unique identifier for this library template (slug style). |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected $id = ''; |
| 15 |
|
| 16 |
/** |
| 17 |
* YayMail email/template name that this library template applies to. |
| 18 |
* |
| 19 |
* Example: 'new_order', 'customer_completed_order', etc. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $email_type; |
| 24 |
|
| 25 |
/** |
| 26 |
* Human-readable template name shown in the UI. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $name; |
| 31 |
|
| 32 |
/** |
| 33 |
* Short description for the template card. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $description; |
| 38 |
|
| 39 |
/** |
| 40 |
* Optional position for ordering in the UI. |
| 41 |
* |
| 42 |
* @var int |
| 43 |
*/ |
| 44 |
protected $position = 10; |
| 45 |
|
| 46 |
/** |
| 47 |
* Whether this template is currently available. |
| 48 |
* |
| 49 |
* @var bool |
| 50 |
*/ |
| 51 |
protected $available = true; |
| 52 |
|
| 53 |
/** |
| 54 |
* Access level for this template. |
| 55 |
* |
| 56 |
* Supported values: |
| 57 |
* - 'free' => always available |
| 58 |
* - 'pro' => available only when a Pro variant of YayMail is installed |
| 59 |
* - 'coming_soon' => not yet available |
| 60 |
* |
| 61 |
* @var string |
| 62 |
*/ |
| 63 |
protected $access = 'free'; |
| 64 |
|
| 65 |
/** |
| 66 |
* Elements for the template. |
| 67 |
* |
| 68 |
* @var array |
| 69 |
*/ |
| 70 |
protected $elements = []; |
| 71 |
|
| 72 |
public function __construct() { |
| 73 |
} |
| 74 |
|
| 75 |
public function get_email_type() { |
| 76 |
return $this->email_type; |
| 77 |
} |
| 78 |
|
| 79 |
public function get_id() { |
| 80 |
return $this->id; |
| 81 |
} |
| 82 |
|
| 83 |
public function get_name() { |
| 84 |
return $this->name; |
| 85 |
} |
| 86 |
|
| 87 |
public function get_description() { |
| 88 |
return $this->description; |
| 89 |
} |
| 90 |
|
| 91 |
public function get_available() { |
| 92 |
return $this->available; |
| 93 |
} |
| 94 |
|
| 95 |
public function get_position() { |
| 96 |
return $this->position; |
| 97 |
} |
| 98 |
|
| 99 |
public function get_elements() { |
| 100 |
return $this->elements; |
| 101 |
} |
| 102 |
|
| 103 |
public function get_access() { |
| 104 |
return $this->access; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get full template payload including elements. |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
public function get_template_data() { |
| 113 |
// Always recompute availability before exposing data to REST. |
| 114 |
$this->available = $this->determine_availability(); |
| 115 |
|
| 116 |
$data = [ |
| 117 |
'id' => $this->get_id(), |
| 118 |
'email_type' => $this->get_email_type(), |
| 119 |
'name' => $this->get_name(), |
| 120 |
'description' => $this->get_description(), |
| 121 |
'position' => $this->get_position(), |
| 122 |
'available' => $this->get_available(), |
| 123 |
'access' => $this->get_access(), |
| 124 |
'elements' => $this->get_elements(), |
| 125 |
]; |
| 126 |
|
| 127 |
return $data; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Determine whether this template is available |
| 132 |
* |
| 133 |
* @return bool |
| 134 |
*/ |
| 135 |
protected function determine_availability() { |
| 136 |
$access = $this->get_access(); |
| 137 |
|
| 138 |
if ( 'coming_soon' === $access ) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
if ( 'pro' === $access ) { |
| 143 |
return $this->is_pro_version_installed(); |
| 144 |
} |
| 145 |
|
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Check whether a Pro variant of YayMail is installed. |
| 151 |
* |
| 152 |
* @return bool |
| 153 |
*/ |
| 154 |
protected function is_pro_version_installed() { |
| 155 |
static $is_pro_cache = null; |
| 156 |
|
| 157 |
if ( null !== $is_pro_cache ) { |
| 158 |
return (bool) $is_pro_cache; |
| 159 |
} |
| 160 |
|
| 161 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 162 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 163 |
} |
| 164 |
|
| 165 |
$all_plugins = function_exists( 'get_plugins' ) ? get_plugins() : []; |
| 166 |
|
| 167 |
if ( empty( $all_plugins ) || ! is_array( $all_plugins ) ) { |
| 168 |
$is_pro_cache = false; |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
$is_pro_cache = array_key_exists( 'yaymail-pro/yaymail.php', $all_plugins ) |
| 173 |
|| array_key_exists( 'email-customizer-for-woocommerce/yaymail.php', $all_plugins ); |
| 174 |
|
| 175 |
return (bool) $is_pro_cache; |
| 176 |
} |
| 177 |
} |
| 178 |
|