MS_Model

Abstract class for all Models.

Defined (1)

The class is defined in the following location(s).

/app/class-ms-model.php  
  1. class MS_Model extends MS_Hooker { 
  2.  
  3. /** 
  4. * ID of the model object. 
  5. * @since 1.0.0 
  6. * @var int|string 
  7. */ 
  8. protected $id; 
  9.  
  10. /** 
  11. * Model name. 
  12. * @since 1.0.0 
  13. * @var string 
  14. */ 
  15. protected $name; 
  16.  
  17. /** 
  18. * Stores the caching state of the object. 
  19. * This value is ONLY modified by MS_Factory::set_singleton(), so if it is 
  20. * true it means that this object can be accessed via MS_Factory::load() 
  21. * @since 1.0.0 
  22. * @var bool 
  23. */ 
  24. public $_in_cache = false; 
  25.  
  26. /** 
  27. * An array containing the serialized array which is stored in the DB. 
  28. * This data can be used to determine which fields have been changed since 
  29. * the object was loaded from DB. 
  30. * This field is populated by MS_Factory::populate() 
  31. * @var array 
  32. */ 
  33. public $_saved_data = array(); 
  34.  
  35. /** 
  36. * MS_Model Contstuctor 
  37. * @since 1.0.0 
  38. */ 
  39. public function __construct() { 
  40.  
  41. /** 
  42. * Actions to execute when constructing the parent Model. 
  43. * @since 1.0.0 
  44. * @param object $this The MS_Model object. 
  45. */ 
  46. do_action( 'ms_model_construct', $this ); 
  47.  
  48. /** 
  49. * Set field value, bypassing the __set validation. 
  50. * Used for loading from db. 
  51. * @since 1.0.0 
  52. * @param string $field 
  53. * @param mixed $value 
  54. */ 
  55. public function set_field( $field, $value ) { 
  56. // Don't deserialize values of "private" fields. 
  57. if ( '_' !== $field[0] ) { 
  58.  
  59. // Only set values of existing fields, don't create a new field. 
  60. if ( property_exists( $this, $field ) ) { 
  61. $this->$field = $value; 
  62.  
  63. /** 
  64. * Resets the fields value to the value that is stored in the DB. 
  65. * @since 1.0.1.0 
  66. * @param string $field Name of the field. 
  67. * @return mixed The reset value. 
  68. */ 
  69. public function reset_field( $field ) { 
  70. $result = null; 
  71.  
  72. // Don't modify values of "private" fields. 
  73. if ( '_' !== $field[0] ) { 
  74.  
  75. // Only reset values of existing fields. 
  76. if ( property_exists( $this, $field ) 
  77. && isset( $this->_saved_data[$field] ) 
  78. ) { 
  79. $this->$field = $this->_saved_data[$field]; 
  80. $result = $this->$field; 
  81.  
  82. return $result; 
  83.  
  84. /** 
  85. * Called before saving model. 
  86. * @since 1.0.0 
  87. */ 
  88. public function before_save() { 
  89. do_action( 'ms_model_before_save', $this ); 
  90.  
  91. /** 
  92. * Abstract method to save model data. 
  93. * @since 1.0.0 
  94. */ 
  95. public function save() { 
  96. throw new Exception( 'Method to be implemented in child class' ); 
  97.  
  98. /** 
  99. * Set the singleton instance if it is not yet defined. 
  100. * @since 1.0.0 
  101. */ 
  102. public function store_singleton() { 
  103. if ( $this->_in_cache ) { return; } 
  104. MS_Factory::set_singleton( $this ); 
  105.  
  106. /** 
  107. * Called after saving model data. 
  108. * @since 1.0.0 
  109. */ 
  110. public function after_save() { 
  111. do_action( 'ms_model_after_save', $this ); 
  112.  
  113. /** 
  114. * Get object properties. 
  115. * @since 1.0.0 
  116. * @return array of fields. 
  117. */ 
  118. public function get_object_vars() { 
  119. return get_object_vars( $this ); 
  120.  
  121. /** 
  122. * Validate dates used within models. 
  123. * @since 1.0.0 
  124. * @param string $date Date as a PHP date string 
  125. * @param string $format Date format. 
  126. */ 
  127. public function validate_date( $date, $format = 'Y-m-d' ) { 
  128. $valid = null; 
  129.  
  130. $d = new DateTime( $date ); 
  131. if ( $d && $d->format( $format ) == $date ) { 
  132. $valid = $date; 
  133.  
  134. return apply_filters( 
  135. 'ms_model_validate_date',  
  136. $valid,  
  137. $date,  
  138. $format,  
  139. $this 
  140. ); 
  141.  
  142. /** 
  143. * Validate time periods array structure. 
  144. * @since 1.0.0 
  145. * @param string $period Membership period to validate 
  146. * @param int $default_period_unit Number of periods (e.g. number of days) 
  147. * @param string $default_period_type (e.g. days, weeks, years) 
  148. */ 
  149. public function validate_period( $period, $default_period_unit = 0, $default_period_type = MS_Helper_Period::PERIOD_TYPE_DAYS ) { 
  150.  
  151. $default = array( 
  152. 'period_unit' => $default_period_unit,  
  153. 'period_type' => $default_period_type,  
  154. ); 
  155.  
  156. if ( ! empty( $period['period_unit'] ) 
  157. && ! empty( $period['period_type'] ) 
  158. ) { 
  159. $period['period_unit'] = $this->validate_period_unit( $period['period_unit'] ); 
  160. $period['period_type'] = $this->validate_period_type( $period['period_type'] ); 
  161. } else { 
  162. $period = $default; 
  163.  
  164. return apply_filters( 
  165. 'ms_model_validate_period',  
  166. $period,  
  167. $this 
  168. ); 
  169.  
  170. /** 
  171. * Validate period unit. 
  172. * @since 1.0.0 
  173. * @param string $period_unit The period quantity to validate. 
  174. * @param int $default The default value when not validated. Default to 1. 
  175. */ 
  176. public function validate_period_unit( $period_unit, $default = 1 ) { 
  177. $period_unit = intval( $period_unit ); 
  178.  
  179. if ( $period_unit <= 0 ) { 
  180. $period_unit = $default; 
  181.  
  182. return apply_filters( 
  183. 'ms_model_validate_period_unit',  
  184. $period_unit,  
  185. $this 
  186. ); 
  187.  
  188. /** 
  189. * Validate period type. 
  190. * @since 1.0.0 
  191. * @param string $period_type The period type to validate. 
  192. * @param int $default The default value when not validated. Default to days. 
  193. */ 
  194. public function validate_period_type( $period_type, $default = MS_Helper_Period::PERIOD_TYPE_DAYS ) { 
  195. $valid_types = MS_Helper_Period::get_period_types(); 
  196. if ( ! isset( $valid_types[$period_type] ) ) { 
  197. $period_type = $default; 
  198.  
  199. return apply_filters( 
  200. 'ms_model_validate_period_type',  
  201. $period_type,  
  202. $this 
  203. );