public function validate($comment) { $errors = array(); if ($comment['contact_id']) { $user = wa()->getUser(); if ($user->getId() && !$user->get('is_user')) { $user->addToCategory(wa()->getApp()); } } elseif ($comment['auth_provider'] == 'guest') { if (!empty($comment['site']) && strpos($comment['site'], '://') === false) { $comment['site'] = "http://" . $comment['site']; } if (empty($comment['name']) || mb_strlen($comment['name']) == 0) { $errors[]['name'] = _wp('Name can not be left blank'); } if (mb_strlen($comment['name']) > 255) { $errors[]['name'] = _wp('Name length should not exceed 255 symbols'); } if (empty($comment['name']) || mb_strlen($comment['email']) == 0) { $errors[]['email'] = _wp('Email can not be left blank'); } $validator = new waEmailValidator(); if (!$validator->isValid($comment['email'])) { $errors[]['email'] = _wp('Email is not valid'); } $validator = new waUrlValidator(); if (!empty($comment['site']) && !$validator->isValid($comment['site'])) { $errors[]['site'] = _wp('Site URL is not valid'); } if (!wa()->getUser()->isAuth() && !wa()->getCaptcha()->isValid()) { $errors[] = array('captcha' => _wp('Invalid captcha code')); } } else { $auth_adapters = wa()->getAuthAdapters(); if (!isset($auth_adapters[$comment['auth_provider']])) { $errors[] = _w('Invalid auth provider'); } } if (mb_strlen($comment['text']) == 0) { $errors[]['text'] = _wp('Comment text can not be left blank'); } if (mb_strlen($comment['text']) > 4096) { $errors[]['text'] = _wp('Comment length should not exceed 4096 symbols'); } return $errors; }
public function validate($review) { $errors = array(); $config = wa()->getConfig(); if ($review['auth_provider'] == self::AUTH_GUEST) { if ($config->getGeneralSettings('require_authorization', false)) { return array('name' => _w('Only authorized users can post reviews')); } if ($config->getGeneralSettings('require_captcha') && !wa()->getCaptcha()->isValid()) { return array('captcha' => _w('Invalid captcha code')); } if (!empty($review['site']) && strpos($review['site'], '://') === false) { $review['site'] = "http://" . $review['site']; } if (empty($review['name']) || mb_strlen($review['name']) == 0) { $errors['name'] = _w('Name can not be left blank'); } if (mb_strlen($review['name']) > 255) { $errors['name'] = _w('Name length should not exceed 255 symbols'); } if (empty($review['email']) || mb_strlen($review['email']) == 0) { $errors['email'] = _w('Email can not be left blank'); } $validator = new waEmailValidator(); if (!$validator->isValid($review['email'])) { $errors['email'] = _w('Email is not valid'); } $validator = new waUrlValidator(); if (!empty($review['site']) && !$validator->isValid($review['site'])) { $errors['site'] = _w('Site URL is not valid'); } } if (empty($review['parent_id'])) { // review to product if (empty($review['title'])) { $errors['title'] = _w('Review title can not be left blank'); } } else { // comment ot review if (empty($review['text'])) { $errors['text'] = _w('Review text can not be left blank'); } } if (mb_strlen($review['text']) > 4096) { $errors['text'] = _w('Review length should not exceed 4096 symbols'); } return $errors; }
public function validate($comment) { $errors = array(); if (empty($comment['auth_provider'])) { $comment['auth_provider'] = self::AUTH_GUEST; } switch ($comment['auth_provider']) { case self::AUTH_GUEST: if (!empty($comment['site']) && strpos($comment['site'], '://') === false) { $comment['site'] = "http://" . $comment['site']; } if (empty($comment['name']) || mb_strlen($comment['name']) == 0) { $errors[]['name'] = _w('Name can not be left blank'); } if (mb_strlen($comment['name']) > 255) { $errors[]['name'] = _w('Name length should not exceed 255 symbols'); } if (empty($comment['name']) || mb_strlen($comment['email']) == 0) { $errors[]['email'] = _w('Email can not be left blank'); } $validator = new waEmailValidator(); if (!$validator->isValid($comment['email'])) { $errors[]['email'] = _w('Email is not valid'); } $validator = new waUrlValidator(); if (!empty($comment['site']) && !$validator->isValid($comment['site'])) { $errors[]['site'] = _w('Site URL is not valid'); } break; case self::AUTH_USER: $user = wa()->getUser(); if ($user->getId() && !$user->get('is_user')) { $user->addToCategory(wa()->getApp()); } break; default: break; } if (mb_strlen($comment['text']) == 0) { $errors[]['text'] = _w('Comment text can not be left blank'); } if (mb_strlen($comment['text']) > 4096) { $errors[]['text'] = _w('Comment length should not exceed 4096 symbols'); } /** * @event comment_validate * @param array[string]mixed $data * @param array['plugin']['%plugin_id%']mixed plugin data * @return array['%plugin_id%']['field']string error */ $plugin_erros = wa()->event('comment_validate', $comment); if (is_array($plugin_erros)) { foreach ($plugin_erros as $plugin) { if ($plugin !== true) { if ($plugin) { $errors[] = $plugin; } else { $errors[]['text'] = _w('Invalid data'); } } } } return $errors; }