MS_Model_Simulate

Membership Simulation model.

Defined (1)

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

/app/model/class-ms-model-simulate.php  
  1. class MS_Model_Simulate extends MS_Model_Transient { 
  2.  
  3. /** 
  4. * The membership ID to simulate. 
  5. * @since 1.0.0 
  6. * @var int 
  7. */ 
  8. protected $membership_id = null; 
  9.  
  10. /** 
  11. * Flag, if the simulation should display a datepicker or not. 
  12. * @since 1.0.0 
  13. * @var bool 
  14. */ 
  15. protected $datepicker = false; 
  16.  
  17. /** 
  18. * The date to simulate. 
  19. * @since 1.0.0 
  20. * @var string 
  21. */ 
  22. protected $date; 
  23.  
  24. /** 
  25. * Holds a reference to the simulated subscription. 
  26. * @var MS_Model_Relationship 
  27. */ 
  28. protected $_subscription = null; 
  29.  
  30. /** 
  31. * Determines if the current user is permitted to even think about using 
  32. * simulation. If not allowed, then most of this class will not be used. 
  33. * @since 1.0.0 
  34. * @return bool 
  35. */ 
  36. public static function can_simulate() { 
  37. if ( defined( 'DOING_CRON' ) && DOING_CRON ) { 
  38. // No simulation during cron jobs... 
  39. return false; 
  40.  
  41. if ( MS_Model_Member::is_admin_user() ) { 
  42. return true; 
  43.  
  44. return false; 
  45.  
  46. /** 
  47. * Called after loading model data. 
  48. * @since 1.0.0 
  49. */ 
  50. public function after_load() { 
  51. if ( ! $this->can_simulate() ) { return false; } 
  52.  
  53. if ( $this->is_simulating() ) { 
  54. if ( empty( $this->date ) ) { 
  55. $this->date = MS_Helper_Period::current_date(); 
  56.  
  57. add_filter( 
  58. 'pre_site_option_site_admins',  
  59. array( $this, 'admin_filter' ) 
  60. ); 
  61.  
  62. add_filter( 
  63. 'ms_model_relationship_get_subscriptions',  
  64. array( $this, 'add_simulation_membership' ),  
  65. 10, 2 
  66. ); 
  67.  
  68. /** 
  69. * Makes the current user a non-admin user during simulation 
  70. * @since 1.0.0 
  71. * @param string $result Set to False to use default WordPress value. 
  72. * @return string Empty value means "no Administrator on this installation". 
  73. */ 
  74. public function admin_filter( $result ) { 
  75. return ''; 
  76.  
  77. /** 
  78. * Add the simulated relationship to the current users memberships. 
  79. * @since 1.0.0 
  80. */ 
  81. public function add_simulation_membership( $subscriptions ) { 
  82. $subscription = false; 
  83.  
  84. if ( ! isset( $subscriptions[ $this->membership_id ] ) ) { 
  85. $this->start_simulation(); 
  86.  
  87. $subscription = MS_Model_Relationship::create_ms_relationship( 
  88. $this->membership_id,  
  89. 0,  
  90. 'simulation' 
  91. ); 
  92.  
  93. if ( is_a( $subscription, 'MS_Model_Relationship' ) ) { 
  94. $membership = $subscription->get_membership(); 
  95. if ( MS_Model_Membership::PAYMENT_TYPE_PERMANENT == $membership->payment_type 
  96. || MS_Model_Membership::PAYMENT_TYPE_RECURRING == $membership->payment_type 
  97. ) { 
  98. $subscription->expire_date = '2999-12-31'; 
  99.  
  100. $key = 'ms_model_relationship--1'; 
  101. MS_Factory::set_singleton( $subscription, $key ); 
  102.  
  103. $this->_subscription = $subscription; 
  104. $subscriptions[ $this->membership_id ] = $subscription; 
  105.  
  106. return $subscriptions; 
  107.  
  108. /** 
  109. * Check simulating status 
  110. * @since 1.0.0 
  111. * @return bool True if currently simulating a membership. 
  112. */ 
  113. public function is_simulating() { 
  114. if ( ! self::can_simulate() ) { 
  115. return false; 
  116.  
  117. return ! empty( $this->membership_id ); 
  118.  
  119. /** 
  120. * Start simulation date. 
  121. * @since 1.0.0 
  122. */ 
  123. private function start_simulation() { 
  124. if ( ! self::can_simulate() ) { 
  125. return false; 
  126.  
  127. if ( $this->datepicker ) { 
  128. $this->add_filter( 
  129. 'ms_helper_period_current_date',  
  130. 'simulate_date_filter' 
  131. ); 
  132.  
  133. // Display some infos on the simulation. 
  134. $this->add_filter( 
  135. 'shutdown',  
  136. 'simulation_infos' 
  137. ); 
  138.  
  139. /** 
  140. * Simulate date. 
  141. * Used Hooks filter/actions: 
  142. * - ms_helper_period_current_date 
  143. * @since 1.0.0 
  144. * @param string $current_date The date to filter. 
  145. * @return string The filtered date. 
  146. */ 
  147. public function simulate_date_filter( $current_date ) { 
  148. if ( ! empty( $this->date ) ) { 
  149. $current_date = $this->date; 
  150.  
  151. return $current_date; 
  152.  
  153. /** 
  154. * Reset simulation. 
  155. * @since 1.0.0 
  156. */ 
  157. public function reset_simulation() { 
  158. if ( null === $this->membership_id ) { return; } 
  159.  
  160. $this->membership_id = null; 
  161. $this->date = null; 
  162. $this->subscription = null; 
  163.  
  164. $this->remove_filter( 
  165. 'ms_helper_period_current_date',  
  166. 'simulate_date_filter' 
  167. ); 
  168.  
  169. $this->save(); 
  170.  
  171. /** 
  172. * Checks if the currently simulated membership needs a datepicker or not. 
  173. * @since 1.0.0 
  174. * @return string True if a datepicker is needed. 
  175. */ 
  176. public function needs_datepicker() { 
  177. $membership = MS_Factory::load( 
  178. 'MS_Model_Membership',  
  179. $this->membership_id 
  180. ); 
  181.  
  182. $m_type = $membership->type; 
  183. $p_type = $membership->payment_type; 
  184. $rep_end = $membership->pay_cycle_repetitions > 0; 
  185. $date_specific = false; 
  186.  
  187. if ( MS_Model_Membership::TYPE_DRIPPED === $m_type ) { $date_specific = true; } 
  188. elseif ( MS_Model_Membership::PAYMENT_TYPE_FINITE === $p_type ) { $date_specific = true; } 
  189. elseif ( MS_Model_Membership::PAYMENT_TYPE_DATE_RANGE === $p_type ) { $date_specific = true; } 
  190. elseif ( MS_Model_Membership::PAYMENT_TYPE_RECURRING === $p_type && $rep_end ) { $date_specific = true; } 
  191.  
  192. $this->datepicker = $date_specific; 
  193.  
  194. return apply_filters( 
  195. 'ms_model_simulate_needs_datepicker',  
  196. $this->datepicker,  
  197. $this 
  198. ); 
  199.  
  200. /** 
  201. * Display some infos on the page on the current simulation 
  202. * @since 1.0.0 
  203. */ 
  204. public function simulation_infos() { 
  205. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return false; } 
  206. if ( defined( 'DOING_CRON' ) && DOING_CRON ) { return false; } 
  207.  
  208. /** 
  209. * The following condition is needed to bypass Upfront virtual pages. 
  210. * By checking for the WordPress filters we can find out if the current 
  211. * page is rendered by WordPress or not (...not means Upfront) 
  212. */ 
  213. $show_infos = true; 
  214. if ( ! is_admin() && ! did_action( 'wp_print_footer_scripts' ) ) { 
  215. $show_infos = false; 
  216. } elseif ( is_admin() && ! did_action( 'admin_print_footer_scripts' ) ) { 
  217. $show_infos = false; 
  218.  
  219. if ( $show_infos ) { 
  220. $data = array(); 
  221. $data['membership_id'] = $this->membership_id; 
  222. $data['subscription'] = $this->_subscription; 
  223. $data['simulate_date'] = $this->date; 
  224. $data['datepicker'] = $this->datepicker; 
  225.  
  226. $view = MS_Factory::create( 'MS_View_Adminbar' ); 
  227. $view->data = apply_filters( 'ms_view_admin_bar_data', $data ); 
  228. $html = $view->to_html(); 
  229.  
  230. echo $html; 
  231.  
  232. /** 
  233. * Returns property associated with the render. 
  234. * @since 1.0.0 
  235. * @param string $property The name of a property. 
  236. * @return mixed Returns mixed value of a property or NULL if a property doesn't exist. 
  237. */ 
  238. public function __get( $property ) { 
  239. $value = null; 
  240.  
  241. if ( property_exists( $this, $property ) ) { 
  242. switch ( $property ) { 
  243. case 'date': 
  244. if ( empty( $this->date ) ) { 
  245. $this->date = MS_Helper_Period::current_date(); 
  246. $value = $this->date; 
  247. break; 
  248.  
  249. default: 
  250. $value = $this->$property; 
  251. break; 
  252.  
  253. return apply_filters( 
  254. 'ms_model_simulate__get',  
  255. $value,  
  256. $property,  
  257. $this 
  258. ); 
  259.  
  260. /** 
  261. * Validate specific property before set. 
  262. * @since 1.0.0 
  263. * @param string $name The name of a property to associate. 
  264. * @param mixed $value The value of a property. 
  265. */ 
  266. public function __set( $property, $value ) { 
  267. if ( property_exists( $this, $property ) ) { 
  268. switch ( $property ) { 
  269. case 'membership_id': 
  270. $this->membership_id = null; 
  271. $id = absint( $value ); 
  272. if ( ! empty( $id ) ) { 
  273. if ( MS_Model_Membership::is_valid_membership( $id ) ) { 
  274. $this->membership_id = $id; 
  275. $this->needs_datepicker(); 
  276. break; 
  277.  
  278. case 'date': 
  279. if ( $date = $this->validate_date( $value ) ) { 
  280. $this->date = $value; 
  281. break; 
  282.  
  283. default: 
  284. $this->$property = $value; 
  285. break; 
  286.  
  287. do_action( 
  288. 'ms_model_simulate__set_after',  
  289. $property,  
  290. $value,  
  291. $this 
  292. );