/** * Constructor */ public function __construct() { // get wordpress database object (no injection sorry! global $wpdb; // variables $this->database = $wpdb; $this->settings = new \SimpleSubscribe\Settings(SUBSCRIBE_KEY); $this->settingsAll = $this->settings->getSettings(); // we get the table name from class name preg_match('#Repository(\\w+)$#', get_class($this), $class); $tablePreName = \Nette\Utils\Strings::contains($class[1], 'Subscribers') ? $class[1] : 'Subscribers' . $class[1]; $this->tableName = $this->database->prefix . Utils::camelCaseToUnderscore($tablePreName); $this->tableSingleName = \Nette\Utils\Strings::firstUpper($class[1]); $this->count = $this->count(); }
/** * Wordpress Settings Page Form * * @param null $defaults * @return Nette\Forms\Form */ public static function settings($defaults = NULL) { // prep $emailSubjectName = isset($defaults['senderName']) ? $defaults['senderName'] : html_entity_decode(get_option('blogname'), ENT_QUOTES); // Form $form = new Form('adminSettings'); // Cron settings $form->addGroup('Cron Settings'); $formCron = $form->addContainer('cron'); $formCron->addSelect('timing', 'Select when the post e-mail should be sent.', array('When a new post is published. (not recommended)', 'In the morning. (approx. 9AM)', 'In the evening. (approx. 9PM)')); // Misc $form->addGroup('Miscellaneous Settings'); $formMisc = $form->addContainer('misc'); $formMisc->addSelect('deactivation', 'Upon user\'s unsubscription from e-mail digest', array('Delete User', 'Only Deactivate User (that\'s evil)')); $formMisc->addText('senderEmail', 'Sender\'s e-mail address, one used to send e-mail digest from')->setOption('description', 'Default: ' . get_option('admin_email'))->addCondition(Form::FILLED)->addRule(Form::EMAIL, 'Must be valid e-mail address'); $formMisc->addText('senderName', 'Sender\'s name')->setOption('description', 'Default: ' . html_entity_decode(get_option('blogname'), ENT_QUOTES)); $formMisc->addSelect('emailSubject', 'Post digest e-mail subject', array('New Post from ' . $emailSubjectName, 'POST_TITLE by ' . $emailSubjectName, 'POST_TITLE')); $formMisc->addCheckbox('log', 'Enable message log')->setOption('description', '(logs errors, cron schedules, successfully sent digests, etc.)'); // if blog is on different url, allow users to select back button url if (\SimpleSubscribe\Utils::getPostsPageUrl()) { $homeUrlDesc = Html::el('small')->setHtml('Backlink on ' . Html::el('a', array('href' => SUBSCRIBE_API_URL, 'target' => '_blank'))->setText('confirmation / unsubscription page') . ', by default it\'s homepage.'); $formMisc->addSelect('homeUrl', 'Homepage link', array(SUBSCRIBE_HOME_URL, \SimpleSubscribe\Utils::getPostsPageUrl()))->setOption('description', $homeUrlDesc); } // Form fields $form->addGroup('Additional subscribe form fields'); $formForm = $form->addContainer('form'); $formForm->addCheckbox('firstName', 'First Name'); $formForm->addCheckbox('lastName', 'Last Name'); $formForm->addCheckbox('age', 'Age'); $formForm->addCheckbox('interests', 'Interests'); $formForm->addCheckbox('location', 'Location'); // Categories $form->addGroup('Digest Category'); $formCat = $form->addContainer('cat'); // List thru categories $formCat->addCheckbox('0', 'All')->setOption('description', 'If you select All, the other categories will deselect automatically.'); foreach (get_categories(array('hide_empty' => 0)) as $category) { $formCat->addCheckbox($category->term_id, $category->name); } $form->addGroup(); // Js $form->addGroup('Front-end'); $formVal = $form->addContainer('val'); $formVal->addCheckbox('js', 'Use javascript validation in the front-end?'); $formVal->addCheckbox('css', 'Use SimpleSubscribe stylesheet?'); // Submit $form->addSubmit('submit', 'Save')->setAttribute('class', 'button-primary'); // set dafaults if ($defaults) { $form->setDefaults($defaults); } return $form; }
/** * Add subscriber, from admin * * @param array $fields * @return Nette\Forms\Form */ public static function subscribeAdmin($fields = array()) { $form = new Form('addSubscriberAdmin'); // Subscriber $form->addText('email', 'E-mail')->setRequired('E-mail address is requried.')->addRule(Form::EMAIL, 'Must be valid e-mail address'); $form->addCheckbox('active', 'Active')->setDefaultValue(1); if (array_key_exists('firstName', $fields)) { $form->addText('firstName', 'First Name'); } if (array_key_exists('lastName', $fields)) { $form->addText('lastName', 'Last Name'); } if (array_key_exists('age', $fields)) { $form->addText('age', 'Age')->addCondition(Form::FILLED)->addRule(Form::INTEGER, 'Age must be a numeric number.')->addRule(Form::RANGE, 'Age should be between 8 and 120 years :).', array(5, 120)); } if (array_key_exists('interests', $fields)) { $form->addTextarea('interests', 'Interests'); } if (array_key_exists('location', $fields)) { $form->addText('location', 'Location'); } $form->addHidden('date', date('Y-m-d H:i:s')); $form->addText('ip', 'Ip')->setDefaultValue(Utils::getRealIp())->addCondition(Form::FILLED)->addRule(Form::PATTERN, 'Must be valid IP', '((^|\\.)((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]?\\d))){4}$'); // Submit $form->addSubmit('submit', 'Add')->setAttribute('class', 'button-primary'); return $form; }
/** * I thought it was clear from the name, yup, it adds user. * * @param $data */ public function add($data) { if ($this->userByEmailExists($data->email)) { throw new RepositarySubscribersException('We are really sorry, but user with this e-mail address already exists.', 0); } else { $data->active = 0; $data->ip = \SimpleSubscribe\Utils::getRealIp(); if ($this->insert($data)) { try { $email = \SimpleSubscribe\Email::getInstance(); $email->sendConfiramtionEmail($data->email, $this->database->insert_id); } catch (EmailException $e) { throw new RepositarySubscribersException($e->getMessage()); } } return TRUE; } }
/** * Backbutton homepage link * * @return null */ public function getBackLinkUrl() { $postsPage = \SimpleSubscribe\Utils::getPostsPageUrl(); $postsPageSettings = isset($this->options['misc']['homeUrl']) ? TRUE : FALSE; if ($postsPageSettings) { return $postsPage; } return SUBSCRIBE_HOME_URL; }
/** * Post Digest * * @param $post * @return string */ public function getPostDigestBody($post) { // if not set, go with excerpt with featured 0: Short Excerpt, 1: Short Excerpt with Featured image, 2: Full post $digestType = isset($this->settingsAll['emailType']['type']) ? $this->settingsAll['emailType']['type'] : 0; // 0: New Post from, 1: POST_TITLE by, 2: POST_TITLE $digestSubject = isset($this->settingsAll['misc']['emailSubject']) ? $this->settingsAll['misc']['emailSubject'] : 0; // prepare return $return = new \stdClass(); // post variables $postLink = get_permalink($post->ID); $postExcerpt = isset($post->post_excerpt) && !empty($post->post_excerpt) ? $post->post_excerpt : $this->createPostDigestExcerpt($post->post_content, 350); $postTitle = $post->post_title; $postImage = wp_get_attachment_image(get_post_thumbnail_id($post->ID), 'simpleSubscribeEmail'); // digest subject switch ($digestSubject) { case 0: default: $return->subject = 'New Post from ' . $this->senderName; break; case 1: $return->subject = $post->post_title . ' by ' . $this->senderName; break; case 2: $return->subject = $post->post_title; break; } // digest type switch ($digestType) { case 0: $emailBody = Html::el('table border="0" cellpadding="0" cellspacing="0" width="100%"')->add(Html::el('tr')->add(Html::el('td style="width: 100%"')->add(Html::el('h3')->setText($post->post_title))->add(Html::el('p')->setText($postExcerpt)->add(Html::el('p')->add(Html::el('a class="more"')->href(get_permalink($post->ID))->setHtml('Read more …')))))); break; case 1: default: $emailBody = Html::el('table border="0" cellpadding="0" cellspacing="0" width="100%"')->add(Html::el('tr')->add(Html::el('td style="width: 70%"')->add(Html::el('h3')->setText($post->post_title))->add(Html::el('p')->setText($postExcerpt)->add(Html::el('p')->add(Html::el('a class="more"')->href(get_permalink($post->ID))->setHtml('Read more …'))))->add(Html::el('td style="width: 30%"')->setHtml(wp_get_attachment_image(get_post_thumbnail_id($post->ID), 'simpleSubscribeEmail'))))); break; case 2: $emailBody = Html::el('table border="0" cellpadding="0" cellspacing="0" width="100%"')->add(Html::el('tr')->add(Html::el('td style="width: 70%"')->add(Html::el('h3')->setText($post->post_title))->add(Html::el('div')->setHtml(Utils::getPostContent($post->ID))))); break; } if ($this->htmlEmail == TRUE) { $emailBody .= '<br /><br />'; $emailBody .= '<small>If you can\'t click the link above, copy and paste this url into your browser: ' . $postLink . '</small>'; } // return $return->data = array('subject' => $return->subject, 'message' => $emailBody); return $return; }
/** * Schedule Cron Event * * @param null $time * @param $postId */ public static function scheduleCron($time, $postId) { wp_schedule_single_event(\SimpleSubscribe\Utils::closestTime($time), SUBSCRIBE_CRON, array($postId)); }