/app/class-ms-model.php

  1. <?php 
  2. /** 
  3. * This file defines the MS_Model object. 
  4. * 
  5. * @copyright Incsub (http://incsub.com/) 
  6. * 
  7. * @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0) 
  8. * 
  9. * This program is free software; you can redistribute it and/or modify 
  10. * it under the terms of the GNU General Public License, version 2, as 
  11. * published by the Free Software Foundation. 
  12. * 
  13. * This program is distributed in the hope that it will be useful,  
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  16. * GNU General Public License for more details. 
  17. * 
  18. * You should have received a copy of the GNU General Public License 
  19. * along with this program; if not, write to the Free Software 
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,  
  21. * MA 02110-1301 USA 
  22. * 
  23. */ 
  24.  
  25. /** 
  26. * Abstract class for all Models. 
  27. * 
  28. * All models will extend or inherit from the MS_Model class. 
  29. * Methods of this class will prepare objects for the database and 
  30. * manipulate data to be used in a MS_Controller. 
  31. * 
  32. * @since 1.0.0 
  33. * 
  34. * @package Membership2 
  35. */ 
  36. class MS_Model extends MS_Hooker { 
  37.  
  38. /** 
  39. * ID of the model object. 
  40. * 
  41. * @since 1.0.0 
  42. * 
  43. * @var int|string 
  44. */ 
  45. protected $id; 
  46.  
  47. /** 
  48. * Model name. 
  49. * 
  50. * @since 1.0.0 
  51. * 
  52. * @var string 
  53. */ 
  54. protected $name; 
  55.  
  56. /** 
  57. * Stores the caching state of the object. 
  58. * This value is ONLY modified by MS_Factory::set_singleton(), so if it is 
  59. * true it means that this object can be accessed via MS_Factory::load() 
  60. * 
  61. * @since 1.1.1.4 
  62. * 
  63. * @var bool 
  64. */ 
  65. public $_in_cache = false; 
  66.  
  67. /** 
  68. * MS_Model Contstuctor 
  69. * 
  70. * @since 1.0.0 
  71. */ 
  72. public function __construct() { 
  73.  
  74. /** 
  75. * Actions to execute when constructing the parent Model. 
  76. * 
  77. * @since 1.0.0 
  78. * @param object $this The MS_Model object. 
  79. */ 
  80. do_action( 'ms_model_construct', $this ); 
  81.  
  82. /** 
  83. * Set field value, bypassing the __set validation. 
  84. * 
  85. * Used for loading from db. 
  86. * 
  87. * @since 1.0.0 
  88. * 
  89. * @param string $field 
  90. * @param mixed $value 
  91. */ 
  92. public function set_field( $field, $value ) { 
  93. // Don't deserialize values of "private" fields. 
  94. if ( '_' === $field[0] ) { 
  95. return; 
  96.  
  97. if ( property_exists( $this, $field ) ) { 
  98. $this->$field = $value; 
  99.  
  100. /** 
  101. * Called before saving model. 
  102. * 
  103. * @since 1.0.0 
  104. */ 
  105. public function before_save() { 
  106. do_action( 'ms_model_before_save', $this ); 
  107.  
  108. /** 
  109. * Abstract method to save model data. 
  110. * 
  111. * @since 1.0.0 
  112. */ 
  113. public function save() { 
  114. throw new Exception( 'Method to be implemented in child class' ); 
  115.  
  116. /** 
  117. * Set the singleton instance if it is not yet defined. 
  118. * 
  119. * @since 1.1.1.4 
  120. */ 
  121. public function store_singleton() { 
  122. if ( $this->_in_cache ) { return; } 
  123. MS_Factory::set_singleton( $this ); 
  124.  
  125. /** 
  126. * Called after saving model data. 
  127. * 
  128. * @since 1.0.0 
  129. */ 
  130. public function after_save() { 
  131. do_action( 'ms_model_after_save', $this ); 
  132.  
  133. /** 
  134. * Get object properties. 
  135. * 
  136. * @since 1.0.0 
  137. * 
  138. * @return array of fields. 
  139. */ 
  140. public function get_object_vars() { 
  141. return get_object_vars( $this ); 
  142.  
  143. /** 
  144. * Validate dates used within models. 
  145. * 
  146. * @since 1.0.0 
  147. * 
  148. * @param string $date Date as a PHP date string 
  149. * @param string $format Date format. 
  150. */ 
  151. public function validate_date( $date, $format = 'Y-m-d' ) { 
  152. $valid = null; 
  153.  
  154. $d = new DateTime( $date ); 
  155. if ( $d && $d->format( $format ) == $date ) { 
  156. $valid = $date; 
  157.  
  158. return apply_filters( 
  159. 'ms_model_validate_date',  
  160. $valid,  
  161. $date,  
  162. $format,  
  163. $this 
  164. ); 
  165.  
  166. /** 
  167. * Validate time periods array structure. 
  168. * 
  169. * @since 1.0.0 
  170. * 
  171. * @param string $period Membership period to validate 
  172. * @param int $default_period_unit Number of periods (e.g. number of days) 
  173. * @param string $default_period_type (e.g. days, weeks, years) 
  174. */ 
  175. public function validate_period( $period, $default_period_unit = 0, $default_period_type = MS_Helper_Period::PERIOD_TYPE_DAYS ) { 
  176.  
  177. $default = array( 
  178. 'period_unit' => $default_period_unit,  
  179. 'period_type' => $default_period_type,  
  180. ); 
  181.  
  182. if ( ! empty( $period['period_unit'] ) 
  183. && ! empty( $period['period_type'] ) 
  184. ) { 
  185. $period['period_unit'] = $this->validate_period_unit( $period['period_unit'] ); 
  186. $period['period_type'] = $this->validate_period_type( $period['period_type'] ); 
  187. } else { 
  188. $period = $default; 
  189.  
  190. return apply_filters( 
  191. 'ms_model_validate_period',  
  192. $period,  
  193. $this 
  194. ); 
  195.  
  196. /** 
  197. * Validate period unit. 
  198. * 
  199. * @since 1.0.0 
  200. * 
  201. * @param string $period_unit The period quantity to validate. 
  202. * @param int $default The default value when not validated. Default to 1. 
  203. */ 
  204. public function validate_period_unit( $period_unit, $default = 1 ) { 
  205. $period_unit = intval( $period_unit ); 
  206.  
  207. if ( $period_unit <= 0 ) { 
  208. $period_unit = $default; 
  209.  
  210. return apply_filters( 
  211. 'ms_model_validate_period_unit',  
  212. $period_unit,  
  213. $this 
  214. ); 
  215.  
  216. /** 
  217. * Validate period type. 
  218. * 
  219. * @since 1.0.0 
  220. * 
  221. * @param string $period_type The period type to validate. 
  222. * @param int $default The default value when not validated. Default to days. 
  223. */ 
  224. public function validate_period_type( $period_type, $default = MS_Helper_Period::PERIOD_TYPE_DAYS ) { 
  225. if ( ! in_array( $period_type, MS_Helper_Period::get_period_types() ) ) { 
  226. $period_type = $default; 
  227.  
  228. return apply_filters( 
  229. 'ms_model_validate_period_type',  
  230. $period_type,  
  231. $this 
  232. ); 
.