diff --git a/assets/css/src/common.styl b/assets/css/src/common.styl index 1c49712483..710e3fba81 100644 --- a/assets/css/src/common.styl +++ b/assets/css/src/common.styl @@ -19,6 +19,8 @@ a:focus // select 2 .select2-container + width: 25em !important + // textareas textarea.regular-text width: 25em !important diff --git a/lib/Config/Hooks.php b/lib/Config/Hooks.php index 2f655f9643..6090b900f5 100644 --- a/lib/Config/Hooks.php +++ b/lib/Config/Hooks.php @@ -1,11 +1,39 @@ _getFlags(); // dkim: check if public/private keys have been generated if( @@ -258,10 +259,9 @@ class Menu { $data = array( 'settings' => $settings, - 'segments' => Segment::getPublished() - ->findArray(), + 'segments' => Segment::getPublic()->findArray(), 'pages' => Pages::getAll(), - 'flags' => $this->_getFlags(), + 'flags' => $flags, 'charsets' => Charsets::getAll(), 'current_user' => wp_get_current_user(), 'permissions' => Permissions::getAll(), diff --git a/lib/Form/Subscribe.php b/lib/Form/Subscribe.php new file mode 100644 index 0000000000..3e7fa8f00b --- /dev/null +++ b/lib/Form/Subscribe.php @@ -0,0 +1,92 @@ + + +
+EOL; + echo $html; + } + + static function onCommentSubmit($comment_id, $comment_approved) { + if($comment_approved === 'spam') return; + + if( + isset($_POST['mailpoet']['subscribe_on_comment']) + && + filter_var( + $_POST['mailpoet']['subscribe_on_comment'], + FILTER_VALIDATE_BOOLEAN + ) === true + ) { + if($comment_approved === 0) { + add_comment_meta( + $comment_id, + 'mailpoet', + 'subscribe_on_comment', + true + ); + } else { + $subscribe_settings = Setting::getValue('subscribe'); + $segment_ids = (array)$subscribe_settings['on_comment']['segments']; + + if($subscribe_settings !== null) { + $comment = get_comment($comment_id); + + $result = Subscriber::subscribe( + array( + 'email' => $comment->comment_author_email, + 'first_name' => $comment->comment_author + ), + $segment_ids + ); + } + } + } + } + + static function onCommentStatusUpdate($comment_id, $comment_status) { + $comment_meta = get_comment_meta( + $comment_id, + 'mailpoet', + true + ); + + if( + $comment_status ==='approve' + && $comment_meta === 'subscribe_on_comment' + ) { + $subscribe_settings = Setting::getValue('subscribe'); + $segment_ids = (array)$subscribe_settings['on_comment']['segments']; + + if($subscribe_settings !== null) { + $comment = get_comment($comment_id); + + Subscriber::subscribe( + array( + 'email' => $comment->comment_author_email, + 'first_name' => $comment->comment_author + ), + $segment_ids + ); + } + } + } +} \ No newline at end of file diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index bef0806375..19d78f075c 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -32,6 +32,65 @@ class Subscriber extends Model { return parent::delete(); } + function addToSegments(array $segment_ids = array()) { + $segments = Segment::whereIn('id', $segment_ids)->findMany(); + foreach($segments as $segment) { + $association = SubscriberSegment::create(); + $association->subscriber_id = $this->id; + $association->segment_id = $segment->id; + $association->save(); + } + } + + static function subscribe($subscriber_data = array(), $segment_ids = array()) { + if(empty($subscriber_data) or empty($segment_ids)) { + return false; + } + + $subscriber = static::createOrUpdate($subscriber_data); + + if($subscriber !== false && $subscriber->id() > 0) { + $signup_confirmation = Setting::getValue('signup_confirmation', array()); + $has_signup_confirmation = true; + if(array_key_exists('enabled', $signup_confirmation)) { + $has_signup_confirmation = filter_var( + $signup_confirmation['enabled'], + FILTER_VALIDATE_BOOLEAN + ); + } + + // restore deleted subscriber + if($subscriber->deleted_at !== NULL) { + $subscriber->setExpr('deleted_at', 'NULL'); + } + + if($has_signup_confirmation === false) { + // auto subscribe when signup confirmation is turned off + $subscriber->set('status', 'subscribed'); + } else { + // reset status of existing subscribers if signup confirmation + // is turned on + if($subscriber->isNew() === false) { + // existing subscriber + if($subscriber->status !== 'subscribed') { + $subscriber->set('status', 'unconfirmed'); + } + } + + // send confirmation email to unconfirmed subscribers + if($subscriber->status === 'unconfirmed') { + // TODO: send signup confirmation email + } + } + + if($subscriber->save()) { + $subscriber->addToSegments($segment_ids); + } + } + + return $subscriber; + } + static function search($orm, $search = '') { if(strlen(trim($search) === 0)) { return $orm; @@ -181,17 +240,52 @@ class Subscriber extends Model { $subscriber = false; if(isset($data['id']) && (int)$data['id'] > 0) { - $subscriber = self::findOne((int)$data['id']); + $subscriber = static::findOne((int)$data['id']); unset($data['id']); } + if($subscriber === false && !empty($data['email'])) { + $subscriber = static::where('email', $data['email'])->findOne(); + if($subscriber !== false) { + unset($data['email']); + } + } + if($subscriber === false) { - $subscriber = self::create(); + $subscriber = static::create(); $subscriber->hydrate($data); } else { $subscriber->set($data); } + // TODO: Cf + /* + // custom fields + $custom_fields = array(); + foreach($data as $key => $value) { + if(strpos($key, 'cf_') === 0) { + $custom_fields[substr($key, 3)] = $value; + unset($data[$key]); + } + } + + // add custom fields + if(!empty($custom_fields)) { + foreach($custom_fields as $custom_field_id => $value) { + if(is_array($value)) { + // date + $value = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']); + } + $subscriber_custom_field = SubscriberCustomField::create(); + $subscriber_custom_field->hydrate(array( + 'subscriber_id' => $subscriber->id(), + 'custom_field_id' => $custom_field_id, + 'value' => $value + )); + $subscriber_custom_field->save(); + } + }*/ + $subscriber->save(); return $subscriber; } diff --git a/lib/Router/Subscribers.php b/lib/Router/Subscribers.php index 161e1d6305..6df0f928c8 100644 --- a/lib/Router/Subscribers.php +++ b/lib/Router/Subscribers.php @@ -68,10 +68,10 @@ class Subscribers { function save($data = array()) { $errors = array(); $result = false; - $segments = false; + $segment_ids = array(); if(array_key_exists('segments', $data)) { - $segments = $data['segments']; + $segment_ids = (array)$data['segments']; unset($data['segments']); } @@ -82,18 +82,8 @@ class Subscribers { } else { $result = true; - if($segments !== false) { - SubscriberSegment::where('subscriber_id', $subscriber->id) - ->deleteMany(); - - if(!empty($segments)) { - foreach($segments as $segment_id) { - $relation = SubscriberSegment::create(); - $relation->segment_id = $segment_id; - $relation->subscriber_id = $subscriber->id; - $relation->save(); - } - } + if(!empty($segment_ids)) { + $subscriber->addToSegments($segment_ids); } } wp_send_json(array( @@ -112,109 +102,31 @@ class Subscribers { $errors[] = __('This form does not exist.'); } - if(empty($data['segments'])) { - $errors[] = __('You need to select a list'); - } else { - $segments = Segment::whereIn('id', (array)$data['segments'])->findMany(); - - if(empty($segments)) { - $errors[] = __('You need to select a list'); - } - } + $segment_ids = (!empty($data['segments']) + ? (array)$data['segments'] + : array() + ); unset($data['segments']); - $subscriber = false; + if(empty($segment_ids)) { + $errors[] = __('You need to select a list'); + } + if(!empty($errors)) { wp_send_json(array('errors' => $errors)); - } else { - if(!empty($data['email'])) { - $subscriber = Subscriber::where('email', $data['email'])->findOne(); - } } - $signup_confirmation = Setting::getValue('signup_confirmation', array()); + $subscriber = Subscriber::subscribe($data, $segment_ids); - if($subscriber === false) { - // create new subscriber - $data['status'] = ( - (!empty($signup_confirmation['enabled'])) - ? 'unconfirmed' : 'subscribed' - ); + if($subscriber === false || !$subscriber->id()) { + $errors = array_merge($errors, $subscriber->getValidationErrors()); + } - // custom fields - $custom_fields = array(); - foreach($data as $key => $value) { - if(strpos($key, 'cf_') === 0) { - $custom_fields[substr($key, 3)] = $value; - unset($data[$key]); - } - } - - // insert new subscriber - $subscriber = Subscriber::createOrUpdate($data); - - if($subscriber === false || !$subscriber->id()) { - $errors = array_merge($errors, $subscriber->getValidationErrors()); - } else { - // add custom fields - if(!empty($custom_fields)) { - foreach($custom_fields as $custom_field_id => $value) { - if(is_array($value)) { - // date - $value = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']); - } - $subscriber_custom_field = SubscriberCustomField::create(); - $subscriber_custom_field->hydrate(array( - 'subscriber_id' => $subscriber->id(), - 'custom_field_id' => $custom_field_id, - 'value' => $value - )); - $subscriber_custom_field->save(); - } - } - } - } else { - $subscriber->set('status', ( - !empty($signup_confirmation['enabled']) - ? 'unconfirmed' : 'subscribed' + if(!empty($errors)) { + wp_send_json(array( + 'result' => false, + 'errors' => $errors )); - - // restore deleted subscriber - if($subscriber->deleted_at !== NULL) { - $subscriber->setExpr('deleted_at', 'NULL'); - } - - if(!$subscriber->save()) { - $errors[] = __('An error occurred. Please try again later.'); - } - } - - // get segments - // IDEA: $subscriptions->addToSegments($data['segments']); - $segments_subscribed = array(); - foreach($segments as $segment) { - if($segment->addSubscriber($subscriber->id())) { - $segments_subscribed[] = $segment->id; - } - } - - // if signup confirmation is enabled and the subscriber is unconfirmed - if(!empty($signup_confirmation['enabled']) - && !empty($segments_subscribed) - && $subscriber->status !== 'subscribed' - ) { - // TODO: send confirmation email - // resend confirmation email - $is_sent = true; - /*$is_sent = static::sendSignupConfirmation( - $subscriber->asArray(), - $segments->asArray() - );*/ - - // error message if the email could not be sent - if($is_sent === false) { - $errors[] = __('The signup confirmation email could not be sent. Please check your settings.'); - } } // get success message to display after subscription @@ -223,15 +135,6 @@ class Subscribers { ? unserialize($form->settings) : null ); - if(!empty($errors)) { - wp_send_json(array( - 'result' => false, - 'errors' => $errors - )); - } else { - $result = true; - } - if($form_settings !== null) { $message = $form_settings['success_message']; @@ -274,7 +177,7 @@ class Subscribers { // response depending on context if($doing_ajax === true) { wp_send_json(array( - 'result' => $result, + 'result' => true, 'message' => $message )); } else { diff --git a/views/settings/basics.html b/views/settings/basics.html index 341201578e..24d73e89c5 100644 --- a/views/settings/basics.html +++ b/views/settings/basics.html @@ -104,15 +104,15 @@