make sure ABSPATH is defined before using it - fixes issue #648
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\API;
|
namespace MailPoet\API;
|
||||||
|
|
||||||
use \MailPoet\Util\Security;
|
use \MailPoet\Util\Security;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
|
||||||
|
|
||||||
class API {
|
class API {
|
||||||
function init() {
|
function init() {
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\API;
|
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
namespace MailPoet\API;
|
||||||
|
|
||||||
final class Error {
|
final class Error {
|
||||||
const UNKNOWN = 'unknown';
|
const UNKNOWN = 'unknown';
|
||||||
const BAD_REQUEST = 'bad_request';
|
const BAD_REQUEST = 'bad_request';
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Cron;
|
namespace MailPoet\Cron;
|
||||||
|
|
||||||
use MailPoet\Cron\Workers\Scheduler as SchedulerWorker;
|
use MailPoet\Cron\Workers\Scheduler as SchedulerWorker;
|
||||||
@@ -6,8 +8,6 @@ use MailPoet\Cron\Workers\SendingQueue\SendingQueue as SendingQueueWorker;
|
|||||||
|
|
||||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
|
||||||
|
|
||||||
class Daemon {
|
class Daemon {
|
||||||
public $daemon;
|
public $daemon;
|
||||||
public $request_data;
|
public $request_data;
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Cron\Workers;
|
namespace MailPoet\Cron\Workers;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
@@ -12,7 +14,6 @@ use MailPoet\Newsletter\Scheduler\Scheduler as NewsletterScheduler;
|
|||||||
|
|
||||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
|
||||||
|
|
||||||
class Scheduler {
|
class Scheduler {
|
||||||
public $timer;
|
public $timer;
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
class Checkbox extends Base {
|
class Checkbox extends Base {
|
||||||
@@ -49,5 +51,4 @@ class Checkbox extends Base {
|
|||||||
|
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
class Radio extends Base {
|
class Radio extends Base {
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
class Segment extends Base {
|
class Segment extends Base {
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
class Select extends Base {
|
class Select extends Base {
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
class Submit extends Base {
|
class Submit extends Base {
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
class Text extends Base {
|
class Text extends Base {
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
class Textarea extends Base {
|
class Textarea extends Base {
|
||||||
|
@@ -1,11 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Mailer;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
use MailPoet\Models\Setting;
|
|
||||||
|
|
||||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
namespace MailPoet\Mailer;
|
||||||
|
use MailPoet\Models\Setting;
|
||||||
|
|
||||||
class Mailer {
|
class Mailer {
|
||||||
public $mailer_config;
|
public $mailer_config;
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
use MailPoet\Models\SendingQueue;
|
|
||||||
use MailPoet\Newsletter\Shortcodes\ShortcodesHelper;
|
|
||||||
|
|
||||||
require_once( ABSPATH . "wp-includes/pluggable.php" );
|
require_once( ABSPATH . "wp-includes/pluggable.php" );
|
||||||
|
|
||||||
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
use MailPoet\Models\SendingQueue;
|
||||||
|
use MailPoet\Newsletter\Shortcodes\ShortcodesHelper;
|
||||||
|
|
||||||
class Newsletter {
|
class Newsletter {
|
||||||
static function process($action,
|
static function process($action,
|
||||||
$default_value = false,
|
$default_value = false,
|
||||||
@@ -54,4 +55,4 @@ class Newsletter {
|
|||||||
$posts->posts[0]->to_array() :
|
$posts->posts[0]->to_array() :
|
||||||
false;
|
false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,11 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
use MailPoet\Models\Subscriber as SubscriberModel;
|
|
||||||
use MailPoet\Models\SubscriberCustomField;
|
|
||||||
|
|
||||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||||
|
|
||||||
|
namespace MailPoet\Newsletter\Shortcodes\Categories;
|
||||||
|
use MailPoet\Models\Subscriber as SubscriberModel;
|
||||||
|
use MailPoet\Models\SubscriberCustomField;
|
||||||
|
|
||||||
class Subscriber {
|
class Subscriber {
|
||||||
static function process(
|
static function process(
|
||||||
$action,
|
$action,
|
||||||
@@ -42,4 +43,4 @@ class Subscriber {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Twig;
|
namespace MailPoet\Twig;
|
||||||
|
|
||||||
class Assets
|
class Assets
|
||||||
@@ -68,4 +70,4 @@ class Assets
|
|||||||
public function generateImageUrl($path) {
|
public function generateImageUrl($path) {
|
||||||
return $this->_globals['assets_url'].'/img/'.$path;
|
return $this->_globals['assets_url'].'/img/'.$path;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,11 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Twig;
|
namespace MailPoet\Twig;
|
||||||
|
|
||||||
class Functions extends \Twig_Extension {
|
class Functions extends \Twig_Extension {
|
||||||
|
|
||||||
function __construct() {
|
|
||||||
}
|
|
||||||
|
|
||||||
function getName() {
|
function getName() {
|
||||||
return 'functions';
|
return 'functions';
|
||||||
}
|
}
|
||||||
@@ -129,4 +128,4 @@ class Functions extends \Twig_Extension {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,11 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Twig;
|
namespace MailPoet\Twig;
|
||||||
|
|
||||||
class Handlebars extends \Twig_Extension {
|
class Handlebars extends \Twig_Extension {
|
||||||
|
|
||||||
public function __construct() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getName() {
|
public function getName() {
|
||||||
return 'handlebars';
|
return 'handlebars';
|
||||||
}
|
}
|
||||||
@@ -61,4 +60,4 @@ class Handlebars extends \Twig_Extension {
|
|||||||
}
|
}
|
||||||
return join("\n", $output);
|
return join("\n", $output);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace MailPoet\Twig;
|
namespace MailPoet\Twig;
|
||||||
|
|
||||||
class I18n extends \Twig_Extension {
|
class I18n extends \Twig_Extension {
|
||||||
@@ -87,4 +89,4 @@ class I18n extends \Twig_Extension {
|
|||||||
}
|
}
|
||||||
return $args;
|
return $args;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,8 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Util;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
require_once(ABSPATH . 'wp-includes/pluggable.php');
|
||||||
|
|
||||||
|
namespace MailPoet\Util;
|
||||||
|
|
||||||
class Security {
|
class Security {
|
||||||
static function generateToken() {
|
static function generateToken() {
|
||||||
return wp_create_nonce('mailpoet_token');
|
return wp_create_nonce('mailpoet_token');
|
||||||
|
@@ -1,182 +1,172 @@
|
|||||||
<?php
|
<?php
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
namespace Sudzy;
|
namespace Sudzy;
|
||||||
|
|
||||||
abstract class ValidModel extends \Model
|
abstract class ValidModel extends \Model {
|
||||||
{
|
protected $_validator = null; // Reference to Sudzy validator object
|
||||||
protected $_validator = null; // Reference to Sudzy validator object
|
protected $_validations = array(); // Array of validations
|
||||||
protected $_validations = array(); // Array of validations
|
protected $_validationErrors = array(); // Array of error messages
|
||||||
protected $_validationErrors = array(); // Array of error messages
|
protected $_validationOptions = array(
|
||||||
protected $_validationOptions = array(
|
'indexedErrors' => false, // If True getValidationErrors will return an array with the index
|
||||||
'indexedErrors' => false, // If True getValidationErrors will return an array with the index
|
// being the field name and the value the error. If multiple errors
|
||||||
// being the field name and the value the error. If multiple errors
|
// are triggered for a field only the first will be kept.
|
||||||
// are triggered for a field only the first will be kept.
|
'throw' => self::ON_SAVE // One of self::ON_SET|ON_SAVE|NEVER.
|
||||||
'throw' => self::ON_SAVE // One of self::ON_SET|ON_SAVE|NEVER.
|
// + ON_SET throws immediately when field is set()
|
||||||
// + ON_SET throws immediately when field is set()
|
// + ON_SAVE throws on save()
|
||||||
// + ON_SAVE throws on save()
|
// + NEVER means an exception is never thrown; check for ->getValidationErrors()
|
||||||
// + NEVER means an exception is never thrown; check for ->getValidaionErrors()
|
);
|
||||||
);
|
|
||||||
|
|
||||||
const ON_SET = 'set';
|
const ON_SET = 'set';
|
||||||
const ON_SAVE = 'save';
|
const ON_SAVE = 'save';
|
||||||
const NEVER = null;
|
const NEVER = null;
|
||||||
|
|
||||||
public function __construct($validatorInstance = null) {
|
public function __construct($validatorInstance = null) {
|
||||||
$this->_validator = $validatorInstance;
|
$this->_validator = $validatorInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setValidationOptions($options) {
|
||||||
|
$this->_validationOptions = array_merge($this->_validationOptions, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addValidation($field, $validation, $message) {
|
||||||
|
if(!isset($this->_validations[$field])) {
|
||||||
|
$this->_validations[$field] = array();
|
||||||
}
|
}
|
||||||
|
$this->_validations[$field][] = array(
|
||||||
|
'validation' => $validation,
|
||||||
|
'message' => $message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function setValidationOptions($options)
|
public function addValidations($field, $validators) {
|
||||||
{
|
foreach ($validators as $validation => $message) {
|
||||||
$this->_validationOptions = array_merge($this->_validationOptions, $options);
|
$this->addValidation($field, $validation, $message);
|
||||||
}
|
|
||||||
|
|
||||||
public function addValidation($field, $validation, $message) {
|
|
||||||
if (!isset($this->_validations[$field])) {
|
|
||||||
$this->_validations[$field] = array();
|
|
||||||
}
|
|
||||||
$this->_validations[$field][] = array(
|
|
||||||
'validation' => $validation,
|
|
||||||
'message' => $message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addValidations($field, $validators) {
|
|
||||||
foreach ($validators as $validation => $message) {
|
|
||||||
$this->addValidation($field, $validation, $message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Checks, without throwing exceptions, model fields with validations
|
|
||||||
// *
|
|
||||||
// * @return bool If false, running $this->doValidationError() will respond appropriately
|
|
||||||
// */
|
|
||||||
// public function validate()
|
|
||||||
// {
|
|
||||||
// $fields = array_keys($this->_validations);
|
|
||||||
// $success = true;
|
|
||||||
// foreach ($fields as $f) {
|
|
||||||
// $success = $success && $this->validateField($f, $this->$f);
|
|
||||||
// }
|
|
||||||
// return $success;
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool Will set a message if returning false
|
|
||||||
**/
|
|
||||||
public function validateField($field, $value)
|
|
||||||
{
|
|
||||||
$this->setupValidationEngine();
|
|
||||||
|
|
||||||
if (!isset($this->_validations[$field])) {
|
|
||||||
return true; // No validations, return true by default
|
|
||||||
}
|
|
||||||
|
|
||||||
$success = true;
|
|
||||||
foreach ($this->_validations[$field] as $v) {
|
|
||||||
$checks = explode(' ', $v['validation']);
|
|
||||||
foreach ($checks as $check) {
|
|
||||||
$params = explode('|', $check);
|
|
||||||
$check = array_shift($params);
|
|
||||||
|
|
||||||
if ($this->_validator->executeOne($check, $value, $params)) {
|
|
||||||
$success = $success && true;
|
|
||||||
} else {
|
|
||||||
$this->addValidationError($v['message'], $field);
|
|
||||||
$success = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $success;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getValidationErrors()
|
|
||||||
{
|
|
||||||
return $this->_validationErrors;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function resetValidationErrors()
|
|
||||||
{
|
|
||||||
$this->_validationErrors = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////
|
|
||||||
// Overloaded methods
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Overload __set to call validateAndSet
|
|
||||||
*/
|
|
||||||
public function __set($name, $value)
|
|
||||||
{
|
|
||||||
$this->validateAndSet($name, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Overload save; checks if errors exist before saving
|
|
||||||
*/
|
|
||||||
public function save()
|
|
||||||
{
|
|
||||||
if ($this->isNew()) { //Fields populated by create() or hydrate() don't pass through set()
|
|
||||||
foreach( array_keys($this->_validations) as $field) {
|
|
||||||
$this->validateField($field, $this->$field);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$errs = $this->getValidationErrors();
|
|
||||||
if (!empty($errs)) {
|
|
||||||
$this->doValidationError(self::ON_SAVE);
|
|
||||||
}
|
|
||||||
|
|
||||||
parent::save();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Overload set; to call validateAndSet
|
|
||||||
*/
|
|
||||||
public function set($key, $value = null)
|
|
||||||
{
|
|
||||||
if(is_array($key)) {
|
|
||||||
// multiple values
|
|
||||||
foreach($key as $field => $value) {
|
|
||||||
$this->validateAndSet($field, $value);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$this->validateAndSet($key, $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////
|
|
||||||
// Protected methods
|
|
||||||
protected function doValidationError($context)
|
|
||||||
{
|
|
||||||
if ($context == $this->_validationOptions['throw']) {
|
|
||||||
throw new \Sudzy\ValidationException($this->_validationErrors);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function addValidationError($msg, $field = null)
|
|
||||||
{
|
|
||||||
if ($this->_validationOptions['indexedErrors'] && $field !== null) {
|
|
||||||
// Only keep the first error found on a field
|
|
||||||
if (!isset($this->_validationErrors[$field])) {
|
|
||||||
$this->_validationErrors[$field] = $msg;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$this->_validationErrors[] = $msg;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Overload set; to call validateAndSet
|
|
||||||
*/
|
|
||||||
protected function validateAndSet($name, $value)
|
|
||||||
{
|
|
||||||
if (!$this->validateField($name, $value)) $this->doValidationError(self::ON_SET);
|
|
||||||
parent::set($name, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setupValidationEngine()
|
|
||||||
{
|
|
||||||
if (null == $this->_validator) $this->_validator = new \Sudzy\Engine(); // Is lazy setup worth it?
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * Checks, without throwing exceptions, model fields with validations
|
||||||
|
// *
|
||||||
|
// * @return bool If false, running $this->doValidationError() will respond appropriately
|
||||||
|
// */
|
||||||
|
// public function validate()
|
||||||
|
// {
|
||||||
|
// $fields = array_keys($this->_validations);
|
||||||
|
// $success = true;
|
||||||
|
// foreach ($fields as $f) {
|
||||||
|
// $success = $success && $this->validateField($f, $this->$f);
|
||||||
|
// }
|
||||||
|
// return $success;
|
||||||
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool Will set a message if returning false
|
||||||
|
**/
|
||||||
|
public function validateField($field, $value) {
|
||||||
|
$this->setupValidationEngine();
|
||||||
|
|
||||||
|
if(!isset($this->_validations[$field])) {
|
||||||
|
return true; // No validations, return true by default
|
||||||
|
}
|
||||||
|
|
||||||
|
$success = true;
|
||||||
|
foreach ($this->_validations[$field] as $v) {
|
||||||
|
$checks = explode(' ', $v['validation']);
|
||||||
|
foreach ($checks as $check) {
|
||||||
|
$params = explode('|', $check);
|
||||||
|
$check = array_shift($params);
|
||||||
|
|
||||||
|
if($this->_validator->executeOne($check, $value, $params)) {
|
||||||
|
$success = $success && true;
|
||||||
|
} else {
|
||||||
|
$this->addValidationError($v['message'], $field);
|
||||||
|
$success = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $success;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValidationErrors() {
|
||||||
|
return $this->_validationErrors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resetValidationErrors() {
|
||||||
|
$this->_validationErrors = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////
|
||||||
|
// Overloaded methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overload __set to call validateAndSet
|
||||||
|
*/
|
||||||
|
public function __set($name, $value) {
|
||||||
|
$this->validateAndSet($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overload save; checks if errors exist before saving
|
||||||
|
*/
|
||||||
|
public function save() {
|
||||||
|
if($this->isNew()) { //Fields populated by create() or hydrate() don't pass through set()
|
||||||
|
foreach(array_keys($this->_validations) as $field) {
|
||||||
|
$this->validateField($field, $this->$field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$errs = $this->getValidationErrors();
|
||||||
|
if(!empty($errs)) {
|
||||||
|
$this->doValidationError(self::ON_SAVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::save();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overload set; to call validateAndSet
|
||||||
|
*/
|
||||||
|
public function set($key, $value = null) {
|
||||||
|
if(is_array($key)) {
|
||||||
|
// multiple values
|
||||||
|
foreach($key as $field => $value) {
|
||||||
|
$this->validateAndSet($field, $value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->validateAndSet($key, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////
|
||||||
|
// Protected methods
|
||||||
|
protected function doValidationError($context) {
|
||||||
|
if($context == $this->_validationOptions['throw']) {
|
||||||
|
throw new \Sudzy\ValidationException($this->_validationErrors);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addValidationError($msg, $field = null) {
|
||||||
|
if($this->_validationOptions['indexedErrors'] && $field !== null) {
|
||||||
|
// Only keep the first error found on a field
|
||||||
|
if(!isset($this->_validationErrors[$field])) {
|
||||||
|
$this->_validationErrors[$field] = $msg;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->_validationErrors[] = $msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overload set; to call validateAndSet
|
||||||
|
*/
|
||||||
|
protected function validateAndSet($name, $value) {
|
||||||
|
if(!$this->validateField($name, $value)) $this->doValidationError(self::ON_SET);
|
||||||
|
parent::set($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setupValidationEngine() {
|
||||||
|
if(null == $this->_validator) $this->_validator = new \Sudzy\Engine(); // Is lazy setup worth it?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user