simplenews.source.inc 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. <?php
  2. /**
  3. * @file
  4. * Contains SimplenewsSource interface and implementations.
  5. */
  6. /**
  7. * The source used to build a newsletter mail.
  8. *
  9. * @ingroup source
  10. */
  11. interface SimplenewsSourceInterface {
  12. /**
  13. * Returns the mail headers.
  14. *
  15. * @param $headers
  16. * The default mail headers.
  17. *
  18. * @return
  19. * Mail headers as an array.
  20. */
  21. function getHeaders(array $headers);
  22. /**
  23. * Returns the mail subject.
  24. */
  25. function getSubject();
  26. /**
  27. * Returns the mail body.
  28. *
  29. * The body should either be plaintext or html, depending on the format.
  30. */
  31. function getBody();
  32. /**
  33. * Returns the plaintext body.
  34. */
  35. function getPlainBody();
  36. /**
  37. * Returns the mail footer.
  38. *
  39. * The footer should either be plaintext or html, depending on the format.
  40. */
  41. function getFooter();
  42. /**
  43. * Returns the plain footer.
  44. */
  45. function getPlainFooter();
  46. /**
  47. * Returns the mail format.
  48. *
  49. * @return
  50. * The mail format as string, either 'plain' or 'html'.
  51. */
  52. function getFormat();
  53. /**
  54. * Returns the recipent of this newsletter mail.
  55. *
  56. * @return
  57. * The recipient mail address(es) of this newsletter as a string.
  58. */
  59. function getRecipient();
  60. /**
  61. * The language that should be used for this newsletter mail.
  62. */
  63. function getLanguage();
  64. /**
  65. * Returns an array of attachments for this newsletter mail.
  66. *
  67. * @return
  68. * An array of managed file objects with properties uri, filemime and so on.
  69. */
  70. function getAttachments();
  71. /**
  72. * Returns the token context to be used with token replacements.
  73. *
  74. * @return
  75. * An array of objects as required by token_replace().
  76. */
  77. function getTokenContext();
  78. /**
  79. * Returns the mail key to be used for drupal_mail().
  80. *
  81. * @return
  82. * The mail key, either test or node.
  83. */
  84. function getKey();
  85. /**
  86. * Returns the formatted from mail address.
  87. */
  88. function getFromFormatted();
  89. /**
  90. * Returns the plain mail address.
  91. */
  92. function getFromAddress();
  93. }
  94. /**
  95. * Source interface based on a node.
  96. *
  97. * This is the interface that needs to be implemented to be compatible with
  98. * the default simplenews spool implementation and therefore exposed in
  99. * hook_simplenews_source_cache_info().
  100. *
  101. * @ingroup source
  102. */
  103. interface SimplenewsSourceNodeInterface extends SimplenewsSourceInterface {
  104. /**
  105. * Create a source based on a node and subscriber.
  106. */
  107. function __construct($node, $subscriber);
  108. /**
  109. * Returns the actually used node of this source.
  110. */
  111. function getNode();
  112. /**
  113. * Returns the subscriber object.
  114. */
  115. function getSubscriber();
  116. }
  117. /**
  118. * Interface for a simplenews source cache implementation.
  119. *
  120. * This is only compatible with the SimplenewsSourceNodeInterface interface.
  121. *
  122. * @ingroup source
  123. */
  124. interface SimplenewsSourceCacheInterface {
  125. /**
  126. * Create a new instance, allows to initialize based on the used
  127. * source.
  128. */
  129. function __construct(SimplenewsSourceNodeInterface $source);
  130. /**
  131. * Return a cached element, if existing.
  132. *
  133. * Although group and key can be used to identify the requested cache, the
  134. * implementations are responsible to create a unique cache key themself using
  135. * the $source. For example based on the node id and the language.
  136. *
  137. * @param $group
  138. * Group of the cache key, which allows cache implementations to decide what
  139. * they want to cache. Currently used groups:
  140. * - data: Raw data, e.g. attachments.
  141. * - build: Built and themed content, before personalizations like tokens.
  142. * - final: The final returned data. Caching this means that newsletter
  143. * can not be personalized anymore.
  144. * @param $key
  145. * Identifies the requested element, e.g. body, footer or attachments.
  146. */
  147. function get($group, $key);
  148. /**
  149. * Write an element to the cache.
  150. *
  151. * Although group and key can be used to identify the requested cache, the
  152. * implementations are responsible to create a unique cache key themself using
  153. * the $source. For example based on the node id and the language.
  154. *
  155. * @param $group
  156. * Group of the cache key, which allows cache implementations to decide what
  157. * they want to cache. Currently used groups:
  158. * - data: Raw data, e.g. attachments.
  159. * - build: Built and themed content, before personalizations like tokens.
  160. * - final: The final returned data. Caching this means that newsletter
  161. * can not be personalized anymore.
  162. * @param $key
  163. * Identifies the requested element, e.g. body, footer or attachments.
  164. * @param $data
  165. * The data to be saved in the cache.
  166. */
  167. function set($group, $key, $data);
  168. }
  169. /**
  170. * A Simplenews spool implementation is a factory for Simplenews sources.
  171. *
  172. * Their main functionility is to return a number of sources based on the passed
  173. * in array of mail spool rows. Additionally, it needs to return the processed
  174. * mail rows after a source was sent.
  175. *
  176. * @todo: Move spool functions into this interface.
  177. *
  178. * @ingroup spool
  179. */
  180. interface SimplenewsSpoolInterface {
  181. /**
  182. * Initalizes the spool implementation.
  183. *
  184. * @param $spool_list
  185. * An array of rows from the {simplenews_mail_spool} table.
  186. */
  187. function __construct($pool_list);
  188. /**
  189. * Returns a Simplenews source to be sent.
  190. *
  191. * A single source may represent any number of mail spool rows, e.g. by
  192. * addressing them as BCC.
  193. */
  194. function nextSource();
  195. /**
  196. * Returns the processed mail spool rows, keyed by the msid.
  197. *
  198. * Only rows that were processed while preparing the previously returned
  199. * source must be returned.
  200. *
  201. * @return
  202. * An array of mail spool rows, keyed by the msid. Can optionally have set
  203. * the following additional properties.
  204. * - actual_nid: In case of content translation, the source node that was
  205. * used for this mail.
  206. * - error: FALSE if the prepration for this row failed. For example set
  207. * when the corresponding node failed to load.
  208. * - status: A simplenews spool status to indicate the status.
  209. */
  210. function getProcessed();
  211. }
  212. /**
  213. * Simplenews Spool implementation.
  214. *
  215. * @ingroup spool
  216. */
  217. class SimplenewsSpool implements SimplenewsSpoolInterface {
  218. /**
  219. * Array with mail spool rows being processed.
  220. *
  221. * @var array
  222. */
  223. protected $spool_list;
  224. /**
  225. * Array of the processed mail spool rows.
  226. */
  227. protected $processed = array();
  228. /**
  229. * Implements SimplenewsSpoolInterface::_construct($spool_list);
  230. */
  231. public function __construct($spool_list) {
  232. $this->spool_list = $spool_list;
  233. }
  234. /**
  235. * Implements SimplenewsSpoolInterface::nextSource();
  236. */
  237. public function nextSource() {
  238. // Get the current mail spool row and update the internal pointer to the
  239. // next row.
  240. $return = each($this->spool_list);
  241. // If we're done, return false.
  242. if (!$return) {
  243. return FALSE;
  244. }
  245. $spool_data = $return['value'];
  246. // Store this spool row as processed.
  247. $this->processed[$spool_data->msid] = $spool_data;
  248. $node = node_load($spool_data->nid);
  249. if (!$node) {
  250. // If node the load failed, set the processed status done and proceed with
  251. // the next mail.
  252. $this->processed[$spool_data->msid]->result = array(
  253. 'status' => SIMPLENEWS_SPOOL_DONE,
  254. 'error' => TRUE
  255. );
  256. return $this->prepareMail();
  257. }
  258. if ($spool_data->data) {
  259. $subscriber = $spool_data->data;
  260. }
  261. else {
  262. $subscriber = simplenews_subscriber_load_by_mail($spool_data->mail);
  263. }
  264. $source_class = $this->getSourceImplementation($spool_data);
  265. $source = new $source_class($node, $subscriber);
  266. // Set which node is actually used. In case of a translation set, this might
  267. // not be the same node.
  268. $this->processed[$spool_data->msid]->actual_nid = $source->getNode()->nid;
  269. return $source;
  270. }
  271. /**
  272. * Implements SimplenewsSpoolInterface::getProcessed();
  273. */
  274. function getProcessed() {
  275. $processed = $this->processed;
  276. $this->processed = array();
  277. return $processed;
  278. }
  279. /**
  280. * Return the Simplenews source implementation for the given mail spool row.
  281. */
  282. protected function getSourceImplementation($spool_data) {
  283. return variable_get('simplenews_source', 'SimplenewsSourceNode');
  284. }
  285. }
  286. /**
  287. * Simplenews source implementation based on nodes for a single subscriber.
  288. *
  289. * @ingroup source
  290. */
  291. class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {
  292. /**
  293. * The node object.
  294. */
  295. protected $node;
  296. /**
  297. * The cached build render array.
  298. */
  299. protected $build;
  300. /**
  301. * The newsletter category.
  302. */
  303. protected $category;
  304. /**
  305. * The subscriber and therefore recipient of this mail.
  306. */
  307. protected $subscriber;
  308. /**
  309. * The mail key used for drupal_mail().
  310. */
  311. protected $key = 'test';
  312. /**
  313. * The simplenews newsletter.
  314. */
  315. protected $newsletter;
  316. /**
  317. * Cache implementation used for this source.
  318. *
  319. * @var SimplenewsSourceCacheInterface
  320. */
  321. protected $cache;
  322. /**
  323. * Implements SimplenewsSourceInterface::_construct();
  324. */
  325. public function __construct($node, $subscriber) {
  326. $this->setSubscriber($subscriber);
  327. $this->setNode($node);
  328. $this->newsletter = simplenews_newsletter_load($node->nid);
  329. $this->category = simplenews_category_load($this->newsletter->tid);
  330. $this->initCache();
  331. }
  332. /**
  333. * Set the node of this source.
  334. *
  335. * If the node is part of a translation set, switch to the node for the
  336. * requested language, if existent.
  337. */
  338. public function setNode($node) {
  339. $langcode = $this->getLanguage();
  340. $nid = $node->nid;
  341. if (module_exists('translation')) {
  342. // If the node has translations and a translation is required
  343. // the equivalent of the node in the required language is used
  344. // or the base node (nid == tnid) is used.
  345. if ($tnid = $node->tnid) {
  346. if ($langcode != $node->language) {
  347. $translations = translation_node_get_translations($tnid);
  348. // A translation is available in the preferred language.
  349. if ($translation = $translations[$langcode]) {
  350. $nid = $translation->nid;
  351. $langcode = $translation->language;
  352. }
  353. else {
  354. // No translation found which matches the preferred language.
  355. foreach ($translations as $translation) {
  356. if ($translation->nid == $tnid) {
  357. $nid = $tnid;
  358. $langcode = $translation->language;
  359. break;
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }
  366. // If a translation of the node is used, load this node.
  367. if ($nid != $node->nid) {
  368. $this->node = node_load($nid);
  369. }
  370. else {
  371. $this->node = $node;
  372. }
  373. }
  374. /**
  375. * Initialize the cache implementation.
  376. */
  377. protected function initCache() {
  378. $class = variable_get('simplenews_source_cache', 'SimplenewsSourceCacheBuild');
  379. $this->cache = new $class($this);
  380. }
  381. /**
  382. * Returns the corresponding category.
  383. */
  384. public function getCategory() {
  385. return $this->category;
  386. }
  387. /**
  388. * Set the active subscriber.
  389. */
  390. public function setSubscriber($subscriber) {
  391. $this->subscriber = $subscriber;
  392. }
  393. /**
  394. * Return the subscriber object.
  395. */
  396. public function getSubscriber() {
  397. return $this->subscriber;
  398. }
  399. /**
  400. * Implements SimplenewsSourceInterface::getHeaders().
  401. */
  402. public function getHeaders(array $headers) {
  403. // If receipt is requested, add headers.
  404. if ($this->category->receipt) {
  405. $headers['Disposition-Notification-To'] = $from;
  406. $headers['X-Confirm-Reading-To'] = $from;
  407. }
  408. // Add priority if set.
  409. switch ($this->category->priority) {
  410. case SIMPLENEWS_PRIORITY_HIGHEST:
  411. $headers['Priority'] = 'High';
  412. $headers['X-Priority'] = '1';
  413. $headers['X-MSMail-Priority'] = 'Highest';
  414. break;
  415. case SIMPLENEWS_PRIORITY_HIGH:
  416. $headers['Priority'] = 'urgent';
  417. $headers['X-Priority'] = '2';
  418. $headers['X-MSMail-Priority'] = 'High';
  419. break;
  420. case SIMPLENEWS_PRIORITY_NORMAL:
  421. $headers['Priority'] = 'normal';
  422. $headers['X-Priority'] = '3';
  423. $headers['X-MSMail-Priority'] = 'Normal';
  424. break;
  425. case SIMPLENEWS_PRIORITY_LOW:
  426. $headers['Priority'] = 'non-urgent';
  427. $headers['X-Priority'] = '4';
  428. $headers['X-MSMail-Priority'] = 'Low';
  429. break;
  430. case SIMPLENEWS_PRIORITY_LOWEST:
  431. $headers['Priority'] = 'non-urgent';
  432. $headers['X-Priority'] = '5';
  433. $headers['X-MSMail-Priority'] = 'Lowest';
  434. break;
  435. }
  436. // Add user specific header data.
  437. $message['headers']['From'] = $this->getFromFormatted();
  438. $message['headers']['List-Unsubscribe'] = '<' . token_replace('[simplenews-subscriber:unsubscribe-url]', $this->getTokenContext(), array('sanitize' => FALSE)) . '>';
  439. // Add general headers
  440. $headers['Precedence'] = 'bulk';
  441. return $headers;
  442. }
  443. /**
  444. * Implements SimplenewsSourceInterface::getTokenContext().
  445. */
  446. function getTokenContext() {
  447. return array(
  448. 'category' => $this->getCategory(),
  449. 'simplenews_subscriber' => $this->getSubscriber(),
  450. 'node' => $this->getNode(),
  451. );
  452. }
  453. /**
  454. * Set the mail key.
  455. */
  456. function setKey($key) {
  457. $this->key = $key;
  458. }
  459. /**
  460. * Implements SimplenewsSourceInterface::getKey().
  461. */
  462. function getKey() {
  463. return $this->key;
  464. }
  465. /**
  466. * Implements SimplenewsSourceInterface::getFromFormatted().
  467. */
  468. function getFromFormatted() {
  469. $name = $this->getCategory()->from_name;
  470. // Windows based PHP systems don't accept formatted emails.
  471. if (drupal_substr(PHP_OS, 0, 3) == 'WIN') {
  472. return $this->getFromAddress();
  473. }
  474. else {
  475. return '"' . $name . '" <' . $this->getFromAddress() . '>';
  476. }
  477. return $formatted_address;
  478. }
  479. /**
  480. * Implements SimplenewsSourceInterface::getFromAddress().
  481. */
  482. function getFromAddress() {
  483. return $this->getCategory()->from_address;
  484. }
  485. /**
  486. * Implements SimplenewsSourceInterface::getRecipient().
  487. */
  488. function getRecipient() {
  489. return $this->getSubscriber()->mail;
  490. }
  491. /**
  492. * Implements SimplenewsSourceInterface::getFormat().
  493. */
  494. function getFormat() {
  495. return $this->getCategory()->format;
  496. }
  497. /**
  498. * Implements SimplenewsSourceInterface::getLanguage().
  499. */
  500. function getLanguage() {
  501. return $this->getSubscriber()->language;
  502. }
  503. /**
  504. * Implements SimplenewsSourceSpoolInterface::getNode().
  505. */
  506. function getNode() {
  507. return $this->node;
  508. }
  509. /**
  510. * Implements SimplenewsSourceInterface::getSubject().
  511. */
  512. function getSubject() {
  513. // Build email subject and perform some sanitizing.
  514. $langcode = $this->getLanguage();
  515. $language_list = language_list();
  516. // Use the requested language if enabled.
  517. $language = isset($language_list[$langcode]) ? $language_list[$langcode] : NULL;
  518. $subject = token_replace($this->getCategory()->email_subject, $this->getTokenContext(), array('sanitize' => FALSE, 'language' => $language));
  519. // Line breaks are removed from the email subject to prevent injection of
  520. // malicious data into the email header.
  521. $subject = str_replace(array("\r", "\n"), '', $subject);
  522. return $subject;
  523. }
  524. /**
  525. * Set up the necessary language and user context.
  526. */
  527. protected function setContext() {
  528. // Switch to the user
  529. if ($this->uid = $this->getSubscriber()->uid) {
  530. simplenews_impersonate_user($this->uid);
  531. }
  532. // Change language if the requested language is enabled.
  533. $language = $this->getLanguage();
  534. $languages = language_list();
  535. if (isset($languages[$language])) {
  536. $this->original_language = $GLOBALS['language'];
  537. $GLOBALS['language'] = $languages[$language];
  538. $GLOBALS['language_url'] = $languages[$language];
  539. // Overwrites the current content language for i18n_select.
  540. if (module_exists('i18n_select')) {
  541. $GLOBALS['language_content'] = $languages[$language];
  542. }
  543. }
  544. }
  545. /**
  546. * Reset the context.
  547. */
  548. protected function resetContext() {
  549. // Switch back to the previous user.
  550. if ($this->uid) {
  551. simplenews_revert_user();
  552. }
  553. // Switch language back.
  554. if (!empty($this->original_language)) {
  555. $GLOBALS['language'] = $this->original_language;
  556. $GLOBALS['language_url'] = $this->original_language;
  557. if (module_exists('i18n_select')) {
  558. $GLOBALS['language_content'] = $this->original_language;
  559. }
  560. }
  561. }
  562. /**
  563. * Build the node object.
  564. *
  565. * The resulting build array is cached as it is used in multiple places.
  566. * @param $format
  567. * (Optional) Override the default format. Defaults to getFormat().
  568. */
  569. protected function build($format = NULL) {
  570. if (empty($format)) {
  571. $format = $this->getFormat();
  572. }
  573. if (!empty($this->build[$format])) {
  574. return $this->build[$format];
  575. }
  576. // Build message body
  577. // Supported view modes: 'email_plain', 'email_html', 'email_textalt'
  578. $build = node_view($this->node, 'email_' . $format);
  579. unset($build['#theme']);
  580. foreach (field_info_instances('node', $this->node->type) as $field_name => $field) {
  581. if (isset($build[$field_name])) {
  582. $build[$field_name]['#theme'] = 'simplenews_field';
  583. }
  584. }
  585. $this->build[$format] = $build;
  586. return $this->build[$format];
  587. }
  588. /**
  589. * Build the themed newsletter body.
  590. *
  591. * @param $format
  592. * (Optional) Override the default format. Defaults to getFormat().
  593. */
  594. protected function buildBody($format = NULL) {
  595. if (empty($format)) {
  596. $format = $this->getFormat();
  597. }
  598. if ($cache = $this->cache->get('build', 'body:' . $format)) {
  599. return $cache;
  600. }
  601. $body = theme('simplenews_newsletter_body', array('build' => $this->build($format), 'category' => $this->getCategory(), 'language' => $this->getLanguage(), 'simplenews_subscriber' => $this->getSubscriber()));
  602. $this->cache->set('build', 'body:' . $format, $body);
  603. return $body;
  604. }
  605. /**
  606. * Implements SimplenewsSourceInterface::getBody().
  607. */
  608. public function getBody() {
  609. return $this->getBodyWithFormat($this->getFormat());
  610. }
  611. /**
  612. * Implements SimplenewsSourceInterface::getBody().
  613. */
  614. public function getPlainBody() {
  615. return $this->getBodyWithFormat('plain');
  616. }
  617. /**
  618. * Get the body with the requested format.
  619. *
  620. * @param $format
  621. * Either html or plain.
  622. *
  623. * @return
  624. * The rendered mail body as a string.
  625. */
  626. protected function getBodyWithFormat($format) {
  627. // Switch to correct user and language context.
  628. $this->setContext();
  629. if ($cache = $this->cache->get('final', 'body:' . $format)) {
  630. return $cache;
  631. }
  632. $body = $this->buildBody($format);
  633. // Build message body, replace tokens.
  634. $body = token_replace($body, $this->getTokenContext(), array('sanitize' => FALSE));
  635. if ($format == 'plain') {
  636. // Convert HTML to text if requested to do so.
  637. $body = simplenews_html_to_text($body, $this->getCategory()->hyperlinks);
  638. }
  639. $this->cache->set('final', 'body:' . $format, $body);
  640. $this->resetContext();
  641. return $body;
  642. }
  643. /**
  644. * Builds the themed footer.
  645. *
  646. * @param $format
  647. * (Optional) Set the format of this footer build, overrides the default
  648. * format.
  649. */
  650. protected function buildFooter($format = NULL) {
  651. if (empty($format)) {
  652. $format = $this->getFormat();
  653. }
  654. if ($cache = $this->cache->get('build', 'footer:' . $format)) {
  655. return $cache;
  656. }
  657. // Build and buffer message footer
  658. $footer = theme('simplenews_newsletter_footer', array(
  659. 'build' => $this->build($format),
  660. 'category' => $this->getCategory(),
  661. 'context' => $this->getTokenContext(),
  662. 'key' => $this->getKey(),
  663. 'language' => $this->getLanguage(),
  664. 'format' => $format,
  665. ));
  666. $this->cache->set('build', 'footer:' . $format, $footer);
  667. return $footer;
  668. }
  669. /**
  670. * Implements SimplenewsSourceInterface::getFooter().
  671. */
  672. public function getFooter() {
  673. return $this->getFooterWithFormat($this->getFormat());
  674. }
  675. /**
  676. * Implements SimplenewsSourceInterface::getPlainFooter().
  677. */
  678. public function getPlainFooter() {
  679. return $this->getFooterWithFormat('plain');
  680. }
  681. /**
  682. * Get the footer in the specified format.
  683. *
  684. * @param $format
  685. * Either html or plain.
  686. *
  687. * @return
  688. * The footer for the requested format.
  689. */
  690. protected function getFooterWithFormat($format) {
  691. // Switch to correct user and language context.
  692. $this->setContext();
  693. if ($cache = $this->cache->get('final', 'footer:' . $format)) {
  694. return $cache;
  695. }
  696. $final_footer = token_replace($this->buildFooter($format), $this->getTokenContext(), array('sanitize' => FALSE));
  697. $this->cache->set('build', 'footer:' . $format, $final_footer);
  698. $this->resetContext();
  699. return $final_footer;
  700. }
  701. /**
  702. * Implements SimplenewsSourceInterface::getAttachments().
  703. */
  704. function getAttachments() {
  705. if ($cache = $this->cache->get('data', 'attachments')) {
  706. return $cache;
  707. }
  708. $attachments = array();
  709. $build = $this->build();
  710. $fids = array();
  711. foreach (field_info_instances('node', $this->node->type) as $field_name => $field_instance) {
  712. // @todo: Find a better way to support more field types.
  713. // Only add fields of type file which are enabled for the current view
  714. // mode as attachments.
  715. $field = field_info_field($field_name);
  716. if ($field['type'] == 'file' && isset($build[$field_name])) {
  717. if ($items = field_get_items('node', $this->node, $field_name)) {
  718. foreach ($items as $item) {
  719. $fids[] = $item['fid'];
  720. }
  721. }
  722. }
  723. }
  724. if (!empty($fids)) {
  725. $attachments = file_load_multiple($fids);
  726. }
  727. $this->cache->set('data', 'attachments', $attachments);
  728. return $attachments;
  729. }
  730. }
  731. /**
  732. * Abstract implementation of the source caching that does static caching.
  733. *
  734. * Subclasses need to implement the abstract function isCacheable() to decide
  735. * what should be cached.
  736. *
  737. * @ingroup source
  738. */
  739. abstract class SimplenewsSourceCacheStatic implements SimplenewsSourceCacheInterface {
  740. /**
  741. * The simplenews source for which this cache is used.
  742. *
  743. * @var SimplenewsSourceNodeInterface
  744. */
  745. protected $source;
  746. /**
  747. * The cache identifier for the given source.
  748. */
  749. protected $cid;
  750. /**
  751. * The static cache.
  752. */
  753. protected static $cache = array();
  754. /**
  755. * Implements SimplenewsSourceNodeInterface::__construct().
  756. */
  757. public function __construct(SimplenewsSourceNodeInterface $source) {
  758. $this->source = $source;
  759. self::$cache = &drupal_static(__CLASS__, array());
  760. }
  761. /**
  762. * Returns the cache identifier for the current source.
  763. */
  764. protected function getCid() {
  765. if (empty($this->cid)) {
  766. $this->cid = $this->source->getNode()->nid . ':' . $this->source->getLanguage();
  767. }
  768. return $this->cid;
  769. }
  770. /**
  771. * Implements SimplenewsSourceNodeInterface::get().
  772. */
  773. public function get($group, $key) {
  774. if (!$this->isCacheable($group, $key)) {
  775. return;
  776. }
  777. if (isset(self::$cache[$this->getCid()][$group][$key])) {
  778. return self::$cache[$this->getCid()][$group][$key];
  779. }
  780. }
  781. /**
  782. * Implements SimplenewsSourceNodeInterface::set().
  783. */
  784. public function set($group, $key, $data) {
  785. if (!$this->isCacheable($group, $key)) {
  786. return;
  787. }
  788. self::$cache[$this->getCid()][$group][$key] = $data;
  789. }
  790. /**
  791. * Return if the requested element should be cached.
  792. *
  793. * @return
  794. * TRUE if it should be cached, FALSE otherwise.
  795. */
  796. abstract function isCacheable($group, $key);
  797. }
  798. /**
  799. * Cache implementation that does not cache anything at all.
  800. *
  801. * @ingroup source
  802. */
  803. class SimplenewsSourceCacheNone extends SimplenewsSourceCacheStatic {
  804. /**
  805. * Implements SimplenewsSourceCacheStatic::set().
  806. */
  807. public function isCacheable($group, $key) {
  808. return FALSE;
  809. }
  810. }
  811. /**
  812. * Source cache implementation that caches build and data element.
  813. *
  814. * @ingroup source
  815. */
  816. class SimplenewsSourceCacheBuild extends SimplenewsSourceCacheStatic {
  817. /**
  818. * Implements SimplenewsSourceCacheStatic::set().
  819. */
  820. function isCacheable($group, $key) {
  821. // Only cache for anon users.
  822. if (user_is_logged_in()) {
  823. return FALSE;
  824. }
  825. // Only cache data and build information.
  826. return in_array($group, array('data', 'build'));
  827. }
  828. }
  829. /**
  830. * Example source implementation used for tests.
  831. *
  832. * @ingroup source
  833. */
  834. class SimplenewsSourceTest implements SimplenewsSourceInterface {
  835. protected $format;
  836. public function __construct($format) {
  837. $this->format = $format;
  838. }
  839. public function getAttachments() {
  840. return array(
  841. array(
  842. 'uri' => 'example://test.png',
  843. 'filemime' => 'x-example',
  844. 'filename' => 'test.png',
  845. ),
  846. );
  847. }
  848. public function getBody() {
  849. return $this->getFormat() == 'plain' ? $this->getPlainBody() : 'the body';
  850. }
  851. public function getFooter() {
  852. return $this->getFormat() == 'plain' ? $this->getPlainFooter() : 'the footer';
  853. }
  854. public function getPlainFooter() {
  855. return 'the plain footer';
  856. }
  857. public function getFormat() {
  858. return $this->format;
  859. }
  860. public function getFromAddress() {
  861. return 'test@example.org';
  862. }
  863. public function getFromFormatted() {
  864. return 'Test <test@example.org>';
  865. }
  866. public function getHeaders(array $headers) {
  867. $headers['X-Simplenews-Test'] = 'OK';
  868. return $headers;
  869. }
  870. public function getKey() {
  871. return 'node';
  872. }
  873. public function getLanguage() {
  874. return 'en';
  875. }
  876. public function getPlainBody() {
  877. return 'the plain body';
  878. }
  879. public function getRecipient() {
  880. return 'recipient@example.org';
  881. }
  882. public function getSubject() {
  883. return 'the subject';
  884. }
  885. public function getTokenContext() {
  886. return array();
  887. }
  888. }