/** * Get and personalise the content for this newsletter * @since Version 3.10.0 * @return \Railpage\Newsletters\Weekly */ private function personaliseContent() { $replacements = array(); Debug::LogCLI("Looping through " . count($this->recipients) . " users and preparing email decoration"); $this->user_ids = array(); $counter = 0; /** * Loop through our list of users and start to curate the contents */ foreach ($this->recipients as $row) { // Flag this user ID so that we can update the "last sent" timestamp later $user_ids[] = $row['user_id']; // Sanity check : validate the email address first if (!filter_var($row['user_email'], FILTER_VALIDATE_EMAIL)) { Debug::LogCLI("Skipping user ID " . $row['user_id'] . " - \"" . $row['user_email'] . "\" is not a valid email address"); continue; } // Add the recipient $this->Notification->addRecipient($row['user_id'], $row['username'], $row['user_email']); // Assign some decoration $replacements[$row['user_email']] = array("##username##" => $row['username'], "##email##" => $row['user_email'], "##email_encoded##" => urlencode($row['user_email']), "##unsubscribe##" => sprintf("http://railpage.com.au/unsubscribe?email=%s&newsletter=weekly", urlencode($row['user_email']))); /** * Get the custom news feed articles */ Debug::LogCLI("Preparing personalised news for user ID " . $row['user_id']); // Try and create the user object. If it bombs out, we need to know about it but let the newsletter continue try { $User = UserFactory::CreateUser($row['user_id']); } catch (Exception $e) { Debug::LogCLI("Skipped user due to exception: " . $e->getMessage()); continue; } // Create the custom news feed object $Feed = new Feed(); $Feed->setUser($User); $articles = $Feed->addFilter(Feed::FILTER_UNREAD)->addFilter(Feed::FILTER_LAST_30_DAYS)->findArticles(0, 10, "story_hits"); // If the number of personalised articles is less than ten, drop the filter and simply find ten recent and unread articles if (count($articles) < 10) { Debug::LogCLI("Found " . count($articles) . " articles for user ID " . $User->id . " - dropping keyword and topic filter from feed"); $Feed->filter_words = null; $Feed->filter_topics = null; $articles = $Feed->findArticles(0, 10, "story_hits"); } // If we have less than six articles skip this user altogether. if (count($articles) < 6) { Debug::LogCLI("Found " . count($articles) . " articles for user ID " . $User->id . " - skipping"); continue; } Debug::LogCLI("Proceeding with newsletter for user ID " . $User->id); // Loop through each article and normalise the content foreach ($articles as $id => $article) { $article['sid'] = $article['story_id']; $article['catid'] = $article['topic_id']; $article['hometext'] = preg_replace("@(\\[b\\]|\\[\\/b\\])@", "", $article['story_blurb']); $article['informant'] = $article['username']; $article['informant_id'] = $article['user_id']; $article['ForumThreadId'] = $article['forum_topic_id']; $article['topictext'] = ContentUtility::FormatTitle($article['topic_title']); $article['topic'] = $article['topic_id']; $article['featured_image'] = ImageCache::cache($article['story_image']); $article['title'] = $article['story_title']; $article['url'] = NewsletterUtility::CreateUTMParametersForLink($this->Newsletter, $article['url']); $articles[$id] = $article; } $articles = array_values($articles); if (!isset($start)) { $start = 0; } // Loop through the prepended content and assign it to the blocks foreach ($this->prependedContent as $i => $block) { $tmp = ["##block" . $i . ".subtitle##" => $block['title'], "##block" . $i . ".featuredimage##" => $block['featuredimage'], "##block" . $i . ".text##" => strip_tags(wpautop(process_bbcode($block['text'])), "<br><br /><p>"), "##block" . $i . ".link##" => strpos($block['url'], "http") === false ? "http://www.railpage.com.au" . $block['url'] : $block['url'], "##block" . $i . ".alt_title##" => $block['subtitle'], "##block" . $i . ".link_text##" => isset($block['link_text']) && !empty($block['link_text']) ? $block['link_text'] : "Continue reading"]; $replacements[$row['user_email']] = array_merge($replacements[$row['user_email']], $tmp); } // Loop through our content and assign to content blocks for ($i = count($this->prependedContent) + $start; $i < $start + $this->num_items; $i++) { $Date = new DateTime($articles[$i]['story_time']); $tmp = ["##block" . $i . ".subtitle##" => $articles[$i]['story_title'], "##block" . $i . ".featuredimage##" => $articles[$i]['story_image'], "##block" . $i . ".text##" => strip_tags(wpautop(process_bbcode($articles[$i]['story_lead'])), "<br><br /><p>"), "##block" . $i . ".link##" => strpos($articles[$i]['url'], "http") === false ? "http://www.railpage.com.au" . $articles[$i]['url'] : $articles[$i]['url'], "##block" . $i . ".alt_title##" => sprintf("Published %s", $Date->format("F j, Y, g:i a")), "##block" . $i . ".link_text##" => "Continue reading"]; $replacements[$row['user_email']] = array_merge($replacements[$row['user_email']], $tmp); } Debug::LogCLI("Completed personalisation of newsletter for user ID " . $User->id); // Increment our personalised newsletter counter $counter++; /** * Break after 150 recipients. Don't want to be flagged as a spammer, or overload the MTA */ if ($counter == 150) { break; } } $this->replacements = $replacements; return $this; }
/** * Set the hero image * @since Version 3.9.1 * @param \Railpage\Images\Image $Image * @return \Railpage\Newsletters\Newsletter */ public function setHeroImage(Image $Image) { $this->content['hero'] = $Image->getArray(); if (strpos($this->content['hero']['url']['canonical'], ":///") !== false) { $this->content['hero']['url']['canonical'] = sprintf("http://www.railpage.com.au/photos/%d", $this->content['hero']['id']); } $this->content['hero']['url']['canonical'] = Utility\NewsletterUtility::CreateUTMParametersForLink($this, $this->content['hero']['url']['canonical']); return $this; }