MS_Model_Event

Event model.

Defined (1)

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

/app/model/class-ms-model-event.php  
  1. class MS_Model_Event extends MS_Model_CustomPostType { 
  2.  
  3. /** 
  4. * Model custom post type. 
  5. * Both static and class property are used to handle php 5.2 limitations. 
  6. * @since 1.0.0 
  7. * @var string 
  8. */ 
  9. protected static $POST_TYPE = 'ms_event'; 
  10.  
  11.  
  12. /** 
  13. * Event topic constants. 
  14. * @see $topic 
  15. * @since 1.0.0 
  16. * @var string 
  17. */ 
  18. const TOPIC_MEMBERSHIP = 'membership'; 
  19. const TOPIC_PAYMENT = 'payment'; 
  20. const TOPIC_USER = 'user'; 
  21. const TOPIC_WARNING = 'warning'; 
  22.  
  23. /** 
  24. * Event type constants. 
  25. * @since 1.0.0 
  26. * @var string 
  27. */ 
  28. const TYPE_UPDATED_INFO = 'updated_info'; 
  29. const TYPE_MS_SIGNED_UP = 'signed_up'; 
  30. const TYPE_MS_MOVED = 'moved'; 
  31. const TYPE_MS_EXPIRED = 'expired'; 
  32. const TYPE_MS_TRIAL_EXPIRED = 'trial_expired'; 
  33. const TYPE_MS_DROPPED = 'dropped'; 
  34. const TYPE_MS_RENEWED = 'renewed'; 
  35. const TYPE_MS_DEACTIVATED = 'deactivated'; 
  36. const TYPE_MS_CANCELED = 'canceled'; 
  37. const TYPE_MS_REGISTERED = 'registered'; 
  38. const TYPE_MS_RESETPASSWORD = 'resetpassword'; 
  39. const TYPE_MS_BEFORE_FINISHES = 'before_finishes'; 
  40. const TYPE_MS_AFTER_FINISHES = 'after_finishes'; 
  41. const TYPE_MS_BEFORE_TRIAL_FINISHES = 'before_trial_finishes'; 
  42. const TYPE_MS_TRIAL_FINISHED = 'trial_finished'; 
  43. const TYPE_CREDIT_CARD_EXPIRE = 'credit_card_expire'; 
  44. const TYPE_PAID = 'paid'; 
  45. const TYPE_PAYMENT_FAILED = 'payment_failed'; 
  46. const TYPE_PAYMENT_PENDING = 'payment_pending'; 
  47. const TYPE_PAYMENT_DENIED = 'payment_denied'; 
  48. const TYPE_PAYMENT_BEFORE_DUE = 'payment_before_due'; 
  49. const TYPE_PAYMENT_AFTER_DUE = 'payment_after_made'; 
  50.  
  51. /** 
  52. * Event's membership ID. 
  53. * @since 1.0.0 
  54. * @var int 
  55. */ 
  56. protected $membership_id; 
  57.  
  58. /** 
  59. * Event's ms relationship ID. 
  60. * @since 1.0.0 
  61. * @var int 
  62. */ 
  63. protected $ms_relationship_id; 
  64.  
  65. /** 
  66. * Event topic. 
  67. * Events are grouped by topic. 
  68. * @since 1.0.0 
  69. * @var string 
  70. */ 
  71. protected $topic; 
  72.  
  73. /** 
  74. * Event type. 
  75. * @since 1.0.0 
  76. * @var string 
  77. */ 
  78. protected $type; 
  79.  
  80. /** 
  81. * Event date. 
  82. * @since 1.0.0 
  83. * @var string 
  84. */ 
  85. protected $date; 
  86.  
  87. /** 
  88. * Returns the post-type of the current object. 
  89. * @since 1.0.0 
  90. * @return string The post-type name. 
  91. */ 
  92. public static function get_post_type() { 
  93. return parent::_post_type( self::$POST_TYPE ); 
  94.  
  95. /** 
  96. * Get custom register post type args for this model. 
  97. * @since 1.0.0 
  98. */ 
  99. public static function get_register_post_type_args() { 
  100. $args = array( 
  101. 'label' => __( 'Membership2 Events', 'membership2' ),  
  102. ); 
  103.  
  104. return apply_filters( 
  105. 'ms_customposttype_register_args',  
  106. $args,  
  107. self::get_post_type() 
  108. ); 
  109.  
  110. /** 
  111. * Get Event types. 
  112. * @since 1.0.0 
  113. * @return array { 
  114. * array{ 
  115. * @type string $topic The topic name. 
  116. * @type string $desc The topic description. 
  117. * } 
  118. * } 
  119. */ 
  120. public static function get_event_types() { 
  121. $types = array( 
  122. /** 
  123. * User topic. 
  124. */ 
  125. self::TYPE_MS_REGISTERED => array( 
  126. 'topic' => self::TOPIC_USER,  
  127. 'desc' => __( 'Has registered.', 'membership2' ),  
  128. ),  
  129. self::TYPE_MS_RESETPASSWORD => array( 
  130. 'topic' => self::TOPIC_USER,  
  131. 'desc' => __( 'Reset password.', 'membership2' ),  
  132. ),  
  133. self::TYPE_UPDATED_INFO => array( 
  134. 'topic' => self::TOPIC_USER,  
  135. 'desc' => __( 'Has updated billing information.', 'membership2' ),  
  136. ),  
  137. self::TYPE_CREDIT_CARD_EXPIRE => array( 
  138. 'topic' => self::TOPIC_USER,  
  139. 'desc' => __( "Member's credit card expire warning date.", 'membership2' ),  
  140. ),  
  141.  
  142. /** 
  143. * Membership topic. 
  144. */ 
  145. self::TYPE_MS_SIGNED_UP => array( 
  146. 'topic' => self::TOPIC_MEMBERSHIP,  
  147. 'desc' => __( 'Has signed up to membership %s.', 'membership2' ),  
  148. ),  
  149. self::TYPE_MS_MOVED => array( 
  150. 'topic' => self::TOPIC_MEMBERSHIP,  
  151. 'desc' => __( 'Has moved to membership %s.', 'membership2' ),  
  152. ),  
  153. self::TYPE_MS_EXPIRED => array( 
  154. 'topic' => self::TOPIC_MEMBERSHIP,  
  155. 'desc' => __( 'Membership %s has expired.', 'membership2' ),  
  156. ),  
  157. self::TYPE_MS_DROPPED => array( 
  158. 'topic' => self::TOPIC_MEMBERSHIP,  
  159. 'desc' => __( 'Membership %s dropped.', 'membership2' ),  
  160. ),  
  161. self::TYPE_MS_RENEWED => array( 
  162. 'topic' => self::TOPIC_MEMBERSHIP,  
  163. 'desc' => __( 'Membership %s renewed', 'membership2' ),  
  164. ),  
  165. self::TYPE_MS_DEACTIVATED => array( 
  166. 'topic' => self::TOPIC_MEMBERSHIP,  
  167. 'desc' => __( 'Membership %s deactivated', 'membership2' ),  
  168. ),  
  169. self::TYPE_MS_CANCELED => array( 
  170. 'topic' => self::TOPIC_MEMBERSHIP,  
  171. 'desc' => __( 'Membership %s cancelled.', 'membership2' ),  
  172. ),  
  173.  
  174. /** 
  175. * Warning topic. 
  176. */ 
  177. self::TYPE_MS_BEFORE_FINISHES => array( 
  178. 'topic' => self::TOPIC_WARNING,  
  179. 'desc' => __( 'Membership %s about to finish warning date.', 'membership2' ),  
  180. ),  
  181.  
  182. self::TYPE_MS_AFTER_FINISHES => array( 
  183. 'topic' => self::TOPIC_WARNING,  
  184. 'desc' => __( 'Membership %s finished warning date.', 'membership2' ),  
  185. ),  
  186. self::TYPE_MS_BEFORE_TRIAL_FINISHES => array( 
  187. 'topic' => self::TOPIC_WARNING,  
  188. 'desc' => __( 'Membership % s trial about to finish warning date.', 'membership2' ),  
  189. ),  
  190.  
  191. /** 
  192. * Payment topic. 
  193. */ 
  194. self::TYPE_PAID => array( 
  195. 'topic' => self::TOPIC_PAYMENT,  
  196. 'desc' => __( 'Invoice #%2$s for membership %1$s - Paid.', 'membership2' ),  
  197. ),  
  198. self::TYPE_PAYMENT_FAILED => array( 
  199. 'topic' => self::TOPIC_PAYMENT,  
  200. 'desc' => __( 'Invoice #%2$s for membership %1$s - Payment Failed.', 'membership2' ),  
  201. ),  
  202. self::TYPE_PAYMENT_PENDING => array( 
  203. 'topic' => self::TOPIC_PAYMENT,  
  204. 'desc' => __( 'Invoice #%2$s for membership %1$s - Payment Pending.', 'membership2' ),  
  205. ),  
  206. self::TYPE_PAYMENT_DENIED => array( 
  207. 'topic' => self::TOPIC_PAYMENT,  
  208. 'desc' => __( 'Invoice #%2$s for membership %1$s - Payment Denied.', 'membership2' ),  
  209. ),  
  210. self::TYPE_PAYMENT_BEFORE_DUE => array( 
  211. 'topic' => self::TOPIC_PAYMENT,  
  212. 'desc' => __( 'Invoice #%2$s before due date for membership %1$s warning.', 'membership2' ),  
  213. ),  
  214. self::TYPE_PAYMENT_AFTER_DUE => array( 
  215. 'topic' => self::TOPIC_PAYMENT,  
  216. 'desc' => __( 'Invoice #%2$s after due date for membership %1$s warning.', 'membership2' ),  
  217. ),  
  218. ); 
  219.  
  220. return apply_filters( 'ms_model_news_get_event_types', $types ); 
  221.  
  222. /** 
  223. * Get last event of specified type. 
  224. * @since 1.0.0 
  225. * @param MS_Model_Event The $event to search. 
  226. * @return null|MS_Model_Event The found event, or null. 
  227. */ 
  228. public static function get_last_event_of_type( $event ) { 
  229. $found = null; 
  230.  
  231. $args['posts_per_page'] = 1; 
  232. $args['meta_query']['type'] = array( 
  233. 'key' => 'type',  
  234. 'value' => $event->type,  
  235. ); 
  236. $args['meta_query']['user_id'] = array( 
  237. 'key' => 'user_id',  
  238. 'value' => $event->user_id,  
  239. ); 
  240.  
  241. if ( ! empty( $event->ms_relationship_id ) ) { 
  242. $args['meta_query']['ms_relationship_id'] = array( 
  243. 'key' => 'ms_relationship_id',  
  244. 'value' => $event->ms_relationship_id,  
  245. ); 
  246.  
  247. $events = self::get_events( apply_filters( 'ms_model_events_get_events_args', $args ) ); 
  248.  
  249. if ( ! empty( $events[0] ) ) { 
  250. $found = $events[0]; 
  251.  
  252. return apply_filters( 'ms_model_event_get_last_event_of_type', $found, $event ); 
  253.  
  254. /** 
  255. * Verify if is a valid event type 
  256. * @since 1.0.0 
  257. * @param string $type The event type to validate. 
  258. * @return boolean True if valid. 
  259. */ 
  260. public static function is_valid_type( $type ) { 
  261. $valid = array_key_exists( $type, self::get_event_types() ); 
  262.  
  263. return apply_filters( 'ms_model_event_is_valid_type', $valid, $type ); 
  264.  
  265. /** 
  266. * Get topic from event. 
  267. * @since 1.0.0 
  268. * @param string $type The event type. 
  269. * @return string the topic description. 
  270. */ 
  271. public static function get_topic( $type ) { 
  272. $topic = null; 
  273. $types = self::get_event_types(); 
  274.  
  275. if ( ! empty( $types[ $type ]['topic'] ) ) { 
  276. $topic = $types[ $type ]['topic']; 
  277.  
  278. return apply_filters( 'ms_model_event_get_topic', $topic, $type ); 
  279.  
  280. /** 
  281. * Get event description. 
  282. * @since 1.0.0 
  283. * @param string $type The event type. 
  284. * @return string the event description. 
  285. */ 
  286. public static function get_description( $type ) { 
  287. $desc = ''; 
  288.  
  289. $types = self::get_event_types(); 
  290. if ( ! empty( $types[ $type ]['desc'] ) ) { 
  291. $desc = $types[ $type ]['desc']; 
  292.  
  293. return apply_filters( 'ms_model_event_get_description', $desc, $type ); 
  294.  
  295. /** 
  296. * Get the total event count. 
  297. * For list table pagination. 
  298. * @since 1.0.0 
  299. * @param array $args The default query event args. 
  300. * @return int The total count. 
  301. */ 
  302. public static function get_event_count( $args = null ) { 
  303. MS_Factory::select_blog(); 
  304. $args = self::get_query_args( $args ); 
  305. $query = new WP_Query( $args ); 
  306. MS_Factory::revert_blog(); 
  307.  
  308. return apply_filters( 
  309. 'ms_model_event_get_event_count',  
  310. $query->found_posts,  
  311. $args 
  312. ); 
  313.  
  314. /** 
  315. * Get events. 
  316. * @since 1.0.0 
  317. * @param $args The query post args 
  318. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  319. * @return MS_Model_Event[] The events found. 
  320. */ 
  321. public static function get_events( $args = null ) { 
  322. MS_Factory::select_blog(); 
  323. $args = self::get_query_args( $args ); 
  324. $query = new WP_Query( $args ); 
  325. $items = $query->posts; 
  326. $events = array(); 
  327. MS_Factory::revert_blog(); 
  328.  
  329. foreach ( $items as $item ) { 
  330. $events[] = MS_Factory::load( 'MS_Model_Event', $item ); 
  331.  
  332. return apply_filters( 'ms_model_event_get_events', $events, $args ); 
  333.  
  334. /** 
  335. * Get WP_Query object arguments. 
  336. * Default search arguments for this custom post_type. 
  337. * @since 1.0.0 
  338. * @param $args The query post args 
  339. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  340. * @return array $args The parsed args. 
  341. */ 
  342. public static function get_query_args( $args ) { 
  343. $defaults = array( 
  344. 'post_type' => self::get_post_type(),  
  345. 'posts_per_page' => 10,  
  346. 'fields' => 'ids',  
  347. 'post_status' => 'any',  
  348. 'order' => 'DESC',  
  349. ); 
  350.  
  351. if ( ! empty( $args['topic'] ) ) { 
  352. $args['meta_query']['topic'] = array( 
  353. 'key' => 'topic',  
  354. 'value' => $args['topic'],  
  355. ); 
  356. unset( $args['topic'] ); 
  357.  
  358. if ( ! empty( $args['membership_id'] ) ) { 
  359. $args['meta_query']['membership_id'] = array( 
  360. 'key' => 'membership_id',  
  361. 'value' => $args['membership_id'],  
  362. ); 
  363. unset( $args['membership_id'] ); 
  364.  
  365. if ( ! empty( $args['relationship_id'] ) ) { 
  366. $args['meta_query']['relationship_id'] = array( 
  367. 'key' => 'ms_relationship_id',  
  368. 'value' => $args['relationship_id'],  
  369. ); 
  370. unset( $args['membership_id'] ); 
  371.  
  372. $args = wp_parse_args( $args, $defaults ); 
  373.  
  374. return apply_filters( 'ms_model_event_get_query_args', $args ); 
  375.  
  376. /** 
  377. * Create and Save event. 
  378. * Default search arguments for this custom post_type. 
  379. * @since 1.0.0 
  380. * @param string $type The event type. 
  381. * @return mixed $data The additional data to create an event. 
  382. */ 
  383. public static function save_event( $type, $data ) { 
  384. $event = null; 
  385.  
  386. if ( self::is_valid_type( $type ) ) { 
  387. $event = MS_Factory::create( 'MS_Model_Event' ); 
  388. $event->type = $type; 
  389. $event->topic = self::get_topic( $type ); 
  390. $description = ''; 
  391.  
  392. switch ( $event->topic ) { 
  393. case self::TOPIC_PAYMENT: 
  394. case self::TOPIC_WARNING: 
  395. case self::TOPIC_MEMBERSHIP: 
  396. $subscription = $data; 
  397. if ( $subscription->id > 0 ) { 
  398. $membership = $subscription->get_membership(); 
  399. $member = MS_Factory::load( 
  400. 'MS_Model_Member',  
  401. $subscription->user_id 
  402. ); 
  403. $event->user_id = $subscription->user_id; 
  404. $event->membership_id = $subscription->membership_id; 
  405. $event->ms_relationship_id = $subscription->id; 
  406. $event->name = sprintf( 
  407. 'user: %s, membership: %s, type: %s',  
  408. $member->name,  
  409. $membership->name,  
  410. $type 
  411. ); 
  412.  
  413. if ( self::TOPIC_PAYMENT == $event->topic ) { 
  414. $invoice = $subscription->get_current_invoice( false ); 
  415. $description = sprintf( 
  416. self::get_description( $type ),  
  417. $membership->name,  
  418. $invoice ? $invoice->id : '-' 
  419. ); 
  420. } else { 
  421. $description = sprintf( 
  422. self::get_description( $type ),  
  423. $membership->name 
  424. ); 
  425. } else { 
  426. // The subscription has no ID. 
  427. // Possibly it was not saved yet... 
  428. break; 
  429.  
  430. case self::TOPIC_USER: 
  431. if ( $data instanceof MS_Model_Member ) { 
  432. $member = $data; 
  433. $event->user_id = $member->id; 
  434. $event->name = sprintf( 
  435. 'user: %s, type: %s',  
  436. $member->name,  
  437. $type 
  438. ); 
  439. } elseif ( $data instanceof MS_Model_Relationship ) { 
  440. $subscription = $data; 
  441. $membership = $subscription->get_membership(); 
  442. $member = MS_Factory::load( 
  443. 'MS_Model_Member',  
  444. $subscription->user_id 
  445. ); 
  446. $event->user_id = $subscription->user_id; 
  447. $event->membership_id = $subscription->membership_id; 
  448. $event->ms_relationship_id = $subscription->id; 
  449. $event->name = sprintf( 
  450. 'user: %s, membership: %s, type: %s',  
  451. $member->name,  
  452. $membership->name,  
  453. $type 
  454. ); 
  455. $description = self::get_description( $type ); 
  456. break; 
  457.  
  458. default: 
  459. MS_Helper_Debug::log( "Event topic not implemented: '$event->topic'" ); 
  460. break; 
  461.  
  462. $event->description = apply_filters( 'ms_model_event_description', $description, $type, $data ); 
  463. $event->date = MS_Helper_Period::current_date(); 
  464. $event = apply_filters( 'ms_model_news_record_user_signup_object', $event ); 
  465.  
  466. if ( ! self::is_duplicate( $event, $data ) ) { 
  467. $event->save(); 
  468.  
  469. // Hook to these actions to handle event notifications. 
  470. // e.g. auto communication. 
  471. do_action( 'ms_model_event', $event, $data ); 
  472. do_action( 'ms_model_event_' . $type, $event, $data ); 
  473. } else { 
  474. $event = null; 
  475.  
  476. return apply_filters( 
  477. 'ms_model_event_save_event',  
  478. $event,  
  479. $type,  
  480. $data 
  481. ); 
  482.  
  483.  
  484. /** 
  485. * Verify if a event was already created in the same day. 
  486. * @since 1.0.0 
  487. * @param MS_Model_Event $event The event to verify. 
  488. * @param mixed $data The additional data. 
  489. */ 
  490. public static function is_duplicate( $event, $data ) { 
  491. $is_duplicate = false; 
  492.  
  493. $check_events = apply_filters( 
  494. 'ms_model_event_is_duplicate_check_events',  
  495. array( 
  496. self::TYPE_MS_BEFORE_TRIAL_FINISHES,  
  497. self::TYPE_MS_BEFORE_FINISHES,  
  498. self::TYPE_MS_AFTER_FINISHES,  
  499. self::TYPE_CREDIT_CARD_EXPIRE,  
  500. self::TYPE_PAYMENT_BEFORE_DUE,  
  501. self::TYPE_PAYMENT_AFTER_DUE,  
  502. ); 
  503.  
  504. if ( in_array( $event->type, $check_events ) 
  505. && $last_event = self::get_last_event_of_type( $event ) 
  506. ) { 
  507. $event_date = gmdate( MS_Helper_Period::PERIOD_FORMAT, strtotime( $last_event->date ) ); 
  508. if ( $event_date === MS_Helper_Period::current_date() ) { 
  509. $is_duplicate = true; 
  510.  
  511. return apply_filters( 'ms_model_event_is_duplicate', $is_duplicate, $event, $data );