themes.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. if (!defined('ABSPATH')) exit;
  3. class NewsletterThemes {
  4. var $module;
  5. var $is_extension = false;
  6. function __construct($module, $is_extension = false) {
  7. $this->module = $module;
  8. $this->is_extension = $is_extension;
  9. }
  10. /** Loads all themes of a module (actually only "emails" module makes sense). Themes are located inside the subfolder
  11. * named as the module on plugin folder and on a subfolder named as the module on wp-content/newsletter folder (which
  12. * must be manually created).
  13. *
  14. * @param type $module
  15. * @return type
  16. */
  17. function get_all() {
  18. $list = array();
  19. $dir = WP_CONTENT_DIR . '/extensions/newsletter/' . $this->module . '/themes';
  20. $handle = @opendir($dir);
  21. if ($handle !== false) {
  22. while ($file = readdir($handle)) {
  23. if ($file == '.' || $file == '..')
  24. continue;
  25. if (!is_file($dir . '/' . $file . '/theme.php'))
  26. continue;
  27. $list[$file] = $file;
  28. }
  29. closedir($handle);
  30. }
  31. if (!$this->is_extension) {
  32. $dir = NEWSLETTER_DIR . '/' . $this->module . '/themes';
  33. $handle = @opendir($dir);
  34. if ($handle !== false) {
  35. while ($file = readdir($handle)) {
  36. if ($file == '.' || $file == '..')
  37. continue;
  38. if (isset($list[$file]))
  39. continue;
  40. if (!is_file($dir . '/' . $file . '/theme.php'))
  41. continue;
  42. $list[$file] = $file;
  43. }
  44. closedir($handle);
  45. }
  46. }
  47. return $list;
  48. }
  49. function themescmp($a, $b) {
  50. $al = strtolower($a['name']);
  51. $bl = strtolower($b['name']);
  52. if ($al == 'default') {
  53. return -1;
  54. }
  55. return (strcmp($al, $bl));
  56. }
  57. function get_all_with_data() {
  58. $list = array();
  59. $dir = WP_CONTENT_DIR . '/extensions/newsletter/' . $this->module . '/themes';
  60. $handle = @opendir($dir);
  61. if ($handle !== false) {
  62. while ($file = readdir($handle)) {
  63. if ($file == '.' || $file == '..') {
  64. continue;
  65. }
  66. if (isset($list[$file])) {
  67. continue;
  68. }
  69. if (!is_file($dir . '/' . $file . '/theme.php')) {
  70. continue;
  71. }
  72. $data = get_file_data($dir . '/' . $file . '/theme.php', array('name' => 'Name', 'type' => 'Type', 'description'=>'Description'));
  73. $data['id'] = $file;
  74. if (empty($data['name'])) {
  75. $data['name'] = $file;
  76. }
  77. if (empty($data['type'])) {
  78. $data['type'] = 'standard';
  79. }
  80. $screenshot = $dir . '/' . $file . '/screenshot.png';
  81. if (is_file($screenshot)) {
  82. $data['screenshot'] = $this->get_theme_url($file) . '/screenshot.png';
  83. } else {
  84. $data['screenshot'] = plugins_url('newsletter') . '/images/theme-screenshot.png';
  85. }
  86. $list[$file] = $data;
  87. }
  88. closedir($handle);
  89. }
  90. if (!$this->is_extension) {
  91. $dir = NEWSLETTER_DIR . '/' . $this->module . '/themes';
  92. $handle = @opendir($dir);
  93. if ($handle !== false) {
  94. while ($file = readdir($handle)) {
  95. if ($file == '.' || $file == '..') {
  96. continue;
  97. }
  98. if (!is_file($dir . '/' . $file . '/theme.php')) {
  99. continue;
  100. }
  101. $data = get_file_data($dir . '/' . $file . '/theme.php', array('name' => 'Name', 'type' => 'Type', 'description'=>'Description'));
  102. $data['id'] = $file;
  103. if (empty($data['name'])) {
  104. $data['name'] = $file;
  105. }
  106. if (empty($data['type'])) {
  107. $data['type'] = 'standard';
  108. }
  109. $screenshot = $dir . '/' . $file . '/screenshot.png';
  110. if (is_file($screenshot)) {
  111. $data['screenshot'] = $this->get_theme_url($file) . '/screenshot.png';
  112. } else {
  113. $data['screenshot'] = plugins_url('newsletter') . '/images/theme-screenshot.png';
  114. }
  115. $list[$file] = $data;
  116. }
  117. closedir($handle);
  118. }
  119. }
  120. usort($list, array($this, "themescmp"));
  121. return $list;
  122. }
  123. /**
  124. *
  125. * @param type $theme
  126. * @param type $options
  127. * @param type $module
  128. */
  129. function save_options($theme, &$options) {
  130. add_option('newsletter_' . $this->module . '_theme_' . $theme, array(), null, 'no');
  131. $theme_options = array();
  132. foreach ($options as $key => &$value) {
  133. if (substr($key, 0, 6) != 'theme_')
  134. continue;
  135. $theme_options[$key] = $value;
  136. }
  137. update_option('newsletter_' . $this->module . '_theme_' . $theme, $theme_options);
  138. }
  139. function get_options($theme) {
  140. $options = get_option('newsletter_' . $this->module . '_theme_' . $theme);
  141. // To avoid merge problems.
  142. if (!is_array($options)) {
  143. $options = array();
  144. }
  145. $file = $this->get_file_path($theme, 'theme-defaults.php');
  146. if (is_file($file)) {
  147. @include $file;
  148. }
  149. if (isset($theme_defaults) && is_array($theme_defaults)) {
  150. $options = array_merge($theme_defaults, $options);
  151. }
  152. return $options;
  153. }
  154. function get_file_path($theme, $file) {
  155. $path = WP_CONTENT_DIR . '/extensions/newsletter/' . $this->module . '/themes/' . $theme . '/' . $file;
  156. if (is_file($path)) {
  157. return $path;
  158. } else {
  159. return NEWSLETTER_DIR . '/' . $this->module . '/themes/' . $theme . '/' . $file;
  160. }
  161. }
  162. function get_theme_url($theme) {
  163. if ($this->is_extension) {
  164. return WP_CONTENT_URL . '/extensions/newsletter/' . $this->module . '/themes/' . $theme;
  165. }
  166. $path = NEWSLETTER_DIR . '/' . $this->module . '/themes/' . $theme;
  167. if (is_dir($path)) {
  168. return plugins_url('newsletter') . '/' . $this->module . '/themes/' . $theme;
  169. } else {
  170. return WP_CONTENT_URL . '/extensions/newsletter/' . $this->module . '/themes/' . $theme;
  171. }
  172. }
  173. function get_default_options() {
  174. if ($this->is_extension) {
  175. $path2 = WP_CONTENT_DIR . '/extensions/newsletter/' . $this->module . '/themes/' . $theme . '/languages';
  176. @include $path2 . '/en_US.php';
  177. @include $path2 . '/' . WPLANG . '.php';
  178. } else {
  179. $path1 = NEWSLETTER_DIR . '/' . $this->module . '/themes/' . $theme . '/languages';
  180. $path2 = WP_CONTENT_DIR . '/extensions/newsletter/' . $this->module . '/themes/' . $theme . '/languages';
  181. @include $path1 . '/en_US.php';
  182. @include $path2 . '/en_US.php';
  183. @include $path1 . '/' . WPLANG . '.php';
  184. @include $path2 . '/' . WPLANG . '.php';
  185. }
  186. if (!is_array($options))
  187. return array();
  188. return $options;
  189. }
  190. }
  191. function nt_option($name, $def = null) {
  192. $options = get_option('newsletter_email');
  193. $option = $options['theme_' . $name];
  194. if (!isset($option))
  195. return $def;
  196. else
  197. return $option;
  198. }