/app/controller/class-ms-controller-shortcode.php

  1. <?php 
  2. /** 
  3. * Controller for managing Plugin Shortcodes. 
  4. * 
  5. * @since 1.0.0 
  6. * 
  7. * @package Membership2 
  8. * @subpackage Controller 
  9. */ 
  10. class MS_Controller_Shortcode extends MS_Controller { 
  11.  
  12. /** 
  13. * Prepare the shortcode hooks. 
  14. * 
  15. * @since 1.0.0 
  16. */ 
  17. public function __construct() { 
  18. parent::__construct(); 
  19. $this->run_action( 'init', 'init' ); 
  20.  
  21. /** 
  22. * Initialize the Shortcodes after we have determined the current user. 
  23. * 
  24. * @since 1.0.0 
  25. */ 
  26. public function init() { 
  27. // By default assume no content for the protected-content code 
  28. add_shortcode( 
  29. MS_Helper_Shortcode::SCODE_PROTECTED,  
  30. array( $this, '__return_null' ) 
  31. ); 
  32.  
  33. if ( MS_Plugin::is_enabled() ) { 
  34. add_shortcode( 
  35. MS_Helper_Shortcode::SCODE_REGISTER_USER,  
  36. array( $this, 'membership_register_user' ) 
  37. ); 
  38.  
  39. add_shortcode( 
  40. MS_Helper_Shortcode::SCODE_SIGNUP,  
  41. array( $this, 'membership_signup' ) 
  42. ); 
  43.  
  44. add_shortcode( 
  45. MS_Helper_Shortcode::SCODE_MS_TITLE,  
  46. array( $this, 'membership_title' ) 
  47. ); 
  48.  
  49. add_shortcode( 
  50. MS_Helper_Shortcode::SCODE_MS_PRICE,  
  51. array( $this, 'membership_price' ) 
  52. ); 
  53.  
  54. add_shortcode( 
  55. MS_Helper_Shortcode::SCODE_MS_BUY,  
  56. array( $this, 'membership_buy' ) 
  57. ); 
  58.  
  59. add_shortcode( 
  60. MS_Helper_Shortcode::SCODE_MS_DETAILS,  
  61. array( $this, 'membership_details' ) 
  62. ); 
  63.  
  64. add_shortcode( 
  65. MS_Helper_Shortcode::SCODE_LOGIN,  
  66. array( $this, 'membership_login' ) 
  67. ); 
  68.  
  69. add_shortcode( 
  70. MS_Helper_Shortcode::SCODE_LOGOUT,  
  71. array( $this, 'membership_logout' ) 
  72. ); 
  73.  
  74. add_shortcode( 
  75. MS_Helper_Shortcode::SCODE_MS_ACCOUNT,  
  76. array( $this, 'membership_account' ) 
  77. ); 
  78.  
  79. add_shortcode( 
  80. MS_Helper_Shortcode::SCODE_MS_ACCOUNT_LINK,  
  81. array( $this, 'membership_account_link' ) 
  82. ); 
  83.  
  84. add_shortcode( 
  85. MS_Helper_Shortcode::SCODE_MS_INVOICE,  
  86. array( $this, 'membership_invoice' ) 
  87. ); 
  88.  
  89. add_shortcode( 
  90. MS_Helper_Shortcode::SCODE_NOTE,  
  91. array( $this, 'ms_note' ) 
  92. ); 
  93.  
  94. add_shortcode( 
  95. MS_Helper_Shortcode::SCODE_GREEN_NOTE,  
  96. array( $this, 'ms_green_note' ) 
  97. ); 
  98.  
  99. add_shortcode( 
  100. MS_Helper_Shortcode::SCODE_RED_NOTE,  
  101. array( $this, 'ms_red_note' ) 
  102. ); 
  103.  
  104. add_shortcode( 
  105. MS_Helper_Shortcode::SCODE_USER,  
  106. array( $this, 'show_to_user' ) 
  107. ); 
  108.  
  109. add_shortcode( 
  110. MS_Helper_Shortcode::SCODE_MEMBER_INFO,  
  111. array( $this, 'ms_member_info' ) 
  112. ); 
  113.  
  114. if ( MS_Model_Member::is_normal_admin() ) { 
  115. add_shortcode( 
  116. MS_Rule_Shortcode_Model::PROTECT_CONTENT_SHORTCODE,  
  117. array( 'MS_Rule_Shortcode_Model', 'debug_protect_content_shortcode') 
  118. ); 
  119. } else { 
  120. $shortcodes = array( 
  121. MS_Helper_Shortcode::SCODE_REGISTER_USER,  
  122. MS_Helper_Shortcode::SCODE_SIGNUP,  
  123. MS_Helper_Shortcode::SCODE_MS_TITLE,  
  124. MS_Helper_Shortcode::SCODE_MS_PRICE,  
  125. MS_Helper_Shortcode::SCODE_MS_DETAILS,  
  126. MS_Helper_Shortcode::SCODE_LOGIN,  
  127. MS_Helper_Shortcode::SCODE_LOGOUT,  
  128. MS_Helper_Shortcode::SCODE_MS_ACCOUNT,  
  129. MS_Helper_Shortcode::SCODE_MS_ACCOUNT_LINK,  
  130. MS_Helper_Shortcode::SCODE_MS_INVOICE,  
  131. MS_Helper_Shortcode::SCODE_NOTE,  
  132. MS_Helper_Shortcode::SCODE_GREEN_NOTE,  
  133. MS_Helper_Shortcode::SCODE_RED_NOTE,  
  134. ); 
  135.  
  136. foreach ( $shortcodes as $shortcode ) { 
  137. add_shortcode( $shortcode, array( $this, 'ms_no_value' ) ); 
  138.  
  139. add_shortcode( 
  140. MS_Rule_Shortcode_Model::PROTECT_CONTENT_SHORTCODE,  
  141. array( $this, 'hide_shortcode') 
  142. ); 
  143.  
  144. add_shortcode( 
  145. MS_Helper_Shortcode::SCODE_USER,  
  146. array( $this, 'hide_shortcode') 
  147. ); 
  148.  
  149. /** 
  150. * Set up the protected-content shortcode to display the protection message. 
  151. * 
  152. * This function is only called from the Frontend-Controller when the 
  153. * Membership Page "Membership2" is displayed. 
  154. * 
  155. * @since 1.0.0 
  156. */ 
  157. public function page_is_protected() { 
  158. remove_shortcode( 
  159. MS_Helper_Shortcode::SCODE_PROTECTED,  
  160. array( $this, '__return_null' ) 
  161. ); 
  162.  
  163. add_shortcode( 
  164. MS_Helper_Shortcode::SCODE_PROTECTED,  
  165. array( $this, 'protected_content' ) 
  166. ); 
  167.  
  168.  
  169. /**========================================*\ 
  170. ============================================ 
  171. == == 
  172. == SHORTCODE HANDLERS == 
  173. == == 
  174. ============================================ 
  175. \*========================================*/ 
  176.  
  177.  
  178.  
  179. /** 
  180. * Membership register callback function. 
  181. * 
  182. * @since 1.0.0 
  183. * 
  184. * @param mixed[] $atts Shortcode attributes. 
  185. */ 
  186. public function membership_register_user( $atts ) { 
  187. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_REGISTER_USER ); 
  188.  
  189. lib3()->array->equip_request( 
  190. 'first_name',  
  191. 'last_name',  
  192. 'username',  
  193. 'email',  
  194. 'membership_id' 
  195. ); 
  196.  
  197. $data = apply_filters( 
  198. 'ms_controller_shortcode_membership_register_user_atts',  
  199. shortcode_atts( 
  200. array( 
  201. 'first_name' => substr( trim( $_REQUEST['first_name'] ), 0, 50 ),  
  202. 'last_name' => substr( trim( $_REQUEST['last_name'] ), 0, 50 ),  
  203. 'username' => substr( trim( $_REQUEST['username'] ), 0, 50 ),  
  204. 'email' => substr( trim( $_REQUEST['email'] ), 0, 50 ),  
  205. 'membership_id' => trim( $_REQUEST['membership_id'] ),  
  206. 'label_first_name' => __( 'First Name', 'membership2' ),  
  207. 'label_last_name' => __( 'Last Name', 'membership2' ),  
  208. 'label_username' => __( 'Choose a Username', 'membership2' ),  
  209. 'label_email' => __( 'Email Address', 'membership2' ),  
  210. 'label_password' => __( 'Password', 'membership2' ),  
  211. 'label_password2' => __( 'Confirm Password', 'membership2' ),  
  212. 'label_register' => __( 'Register My Account', 'membership2' ),  
  213. 'hint_first_name' => '',  
  214. 'hint_last_name' => '',  
  215. 'hint_username' => '',  
  216. 'hint_email' => '',  
  217. 'hint_password' => '',  
  218. 'hint_password2' => '',  
  219. 'title' => __( 'Create an Account', 'membership2' ),  
  220. 'loginlink' => true,  
  221. ),  
  222. $atts 
  223. ); 
  224. $data['action'] = 'register_user'; 
  225. $data['step'] = MS_Controller_Frontend::STEP_REGISTER_SUBMIT; 
  226. $data['loginlink'] = lib3()->is_true( $data['loginlink'] ); 
  227.  
  228. $view = MS_Factory::create( 'MS_View_Shortcode_RegisterUser' ); 
  229. $view->data = apply_filters( 'ms_view_shortcode_registeruser_data', $data, $this ); 
  230.  
  231. return $view->to_html(); 
  232.  
  233. /** 
  234. * Membership signup callback function. 
  235. * 
  236. * @since 1.0.0 
  237. * 
  238. * @param mixed[] $atts Shortcode attributes. 
  239. */ 
  240. public function membership_signup( $atts ) { 
  241. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_SIGNUP ); 
  242.  
  243. $data = apply_filters( 
  244. 'ms_controller_shortcode_membership_signup_atts',  
  245. shortcode_atts( 
  246. array( 
  247. MS_Helper_Membership::MEMBERSHIP_ACTION_SIGNUP . '_text' => __( 'Signup', 'membership2' ),  
  248. MS_Helper_Membership::MEMBERSHIP_ACTION_MOVE . '_text' => __( 'Change', 'membership2' ),  
  249. MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL . '_text' => __( 'Cancel', 'membership2' ),  
  250. MS_Helper_Membership::MEMBERSHIP_ACTION_RENEW . '_text' => __( 'Renew', 'membership2' ),  
  251. MS_Helper_Membership::MEMBERSHIP_ACTION_PAY . '_text' => __( 'Complete Payment', 'membership2' ),  
  252. ),  
  253. $atts 
  254. ); 
  255.  
  256. $member = MS_Model_Member::get_current_member(); 
  257. $data['member'] = $member; 
  258. $data['subscriptions'] = array(); 
  259. $exclude = array(); 
  260.  
  261. if ( $member->is_valid() ) { 
  262. // Get member's memberships, including pending relationships. 
  263. $data['subscriptions'] = MS_Model_Relationship::get_subscriptions( 
  264. array( 
  265. 'user_id' => $data['member']->id,  
  266. 'status' => 'valid',  
  267. ); 
  268.  
  269. foreach ( $data['subscriptions'] as $key => $subscription ) { 
  270. $exclude[] = $subscription->membership_id; 
  271. if ( ! $member->can_subscribe_to( $subscription->membership_id ) ) { 
  272. unset( $data['subscriptions'][$key] ); 
  273.  
  274. $memberships = MS_Model_Membership::get_signup_membership_list( 
  275. null,  
  276. $exclude 
  277. ); 
  278.  
  279. $data['memberships'] = $memberships; 
  280. $move_from_ids = array(); 
  281.  
  282. // When Multiple memberships is not enabled, a member should move to another membership. 
  283. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MULTI_MEMBERSHIPS ) ) { 
  284. // Membership Relationship status which can move to another one 
  285. $valid_status = array( 
  286. MS_Model_Relationship::STATUS_TRIAL,  
  287. MS_Model_Relationship::STATUS_ACTIVE,  
  288. MS_Model_Relationship::STATUS_EXPIRED,  
  289. ); 
  290.  
  291. foreach ( $data['member']->subscriptions as $subscription ) { 
  292. if ( $subscription->is_system() ) { continue; } 
  293.  
  294. if ( in_array( $subscription->status, $valid_status ) ) { 
  295. $move_from_ids[] = $subscription->membership_id; 
  296. foreach ( $data['memberships'] as $key => $membership ) { 
  297. $data['memberships'][$key]->_move_from = $move_from_ids; 
  298. } else { 
  299. foreach ( $data['memberships'] as $key => $membership ) { 
  300. $move_from_ids = $member->cancel_ids_on_subscription( 
  301. $membership->id 
  302. ); 
  303.  
  304. $data['memberships'][$key]->_move_from = $move_from_ids; 
  305.  
  306. $data['action'] = MS_Helper_Membership::MEMBERSHIP_ACTION_SIGNUP; 
  307. $data['step'] = MS_Controller_Frontend::STEP_PAYMENT_TABLE; 
  308.  
  309. $view = MS_Factory::create( 'MS_View_Shortcode_MembershipSignup' ); 
  310. $view->data = apply_filters( 
  311. 'ms_view_shortcode_membershipsignup_data',  
  312. $data,  
  313. $this 
  314. ); 
  315.  
  316. return $view->to_html(); 
  317.  
  318. /** 
  319. * Membership title shortcode callback function. 
  320. * 
  321. * @since 1.0.0 
  322. * 
  323. * @param mixed[] $atts Shortcode attributes. 
  324. */ 
  325. public function membership_title( $atts ) { 
  326. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_MS_TITLE ); 
  327.  
  328. $code = ''; 
  329.  
  330. $data = apply_filters( 
  331. 'ms_controller_shortcode_membership_title_atts',  
  332. shortcode_atts( 
  333. array( 
  334. 'id' => 0,  
  335. 'label' => __( 'Membership title:', 'membership2' ),  
  336. 'title' => '', // deprecated @since 1.0.0 
  337. ),  
  338. $atts 
  339. ); 
  340. extract( $data ); 
  341.  
  342. if ( ! empty( $id ) ) { 
  343. $membership = MS_Factory::load( 'MS_Model_Membership', $id ); 
  344. $code = sprintf( 
  345. '%1$s %2$s',  
  346. $label,  
  347. $membership->name 
  348. ); 
  349.  
  350. $code = trim( $code ); 
  351. } else { 
  352. $code = $title; 
  353.  
  354. $code = sprintf( 
  355. '<span class="ms-membership-title ms-membership-%1$s">%2$s</span>',  
  356. esc_attr( $id ),  
  357. $code 
  358. ); 
  359.  
  360. return apply_filters( 
  361. 'ms_controller_shortcode_membership_title',  
  362. $code,  
  363. $atts,  
  364. $this 
  365. ); 
  366.  
  367. /** 
  368. * Membership price shortcode callback function. 
  369. * 
  370. * @since 1.0.0 
  371. * 
  372. * @param mixed[] $atts Shortcode attributes. 
  373. */ 
  374. public function membership_price( $atts ) { 
  375. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_MS_PRICE ); 
  376.  
  377. $code = 0; 
  378.  
  379. $data = apply_filters( 
  380. 'ms_controller_shortcode_membership_price_atts',  
  381. shortcode_atts( 
  382. array( 
  383. 'id' => 0,  
  384. 'currency' => true,  
  385. 'label' => __( 'Membership price:', 'membership2' ),  
  386. ),  
  387. $atts 
  388. ); 
  389. extract( $data ); 
  390.  
  391. if ( ! empty( $id ) ) { 
  392. if ( lib3()->is_true( $currency ) ) { 
  393. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  394. $currency = $settings->currency; 
  395. } else { 
  396. $currency = ''; 
  397.  
  398. $membership = MS_Factory::load( 'MS_Model_Membership', $id ); 
  399. $code = sprintf( 
  400. '%1$s %2$s %3$s',  
  401. $label,  
  402. $currency,  
  403. $membership->total_price 
  404. ); 
  405.  
  406. $code = trim( $code ); 
  407.  
  408. $code = sprintf( 
  409. '<span class="ms-membership-price ms-membership-%1$s price">%2$s</span>',  
  410. esc_attr( $id ),  
  411. $code 
  412. ); 
  413.  
  414. do_action( 'ms_show_prices' ); 
  415. return apply_filters( 
  416. 'ms_controller_shortcode_membership_price',  
  417. $code,  
  418. $atts,  
  419. $this 
  420. ); 
  421.  
  422. /** 
  423. * Buy membership button. 
  424. * 
  425. * @since 1.0.0 
  426. * 
  427. * @param mixed[] $atts Shortcode attributes. 
  428. */ 
  429. public function membership_buy( $atts ) { 
  430. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_MS_BUY ); 
  431.  
  432. $code = ''; 
  433.  
  434. $data = apply_filters( 
  435. 'ms_controller_shortcode_membership_buy_atts',  
  436. shortcode_atts( 
  437. array( 
  438. 'id' => 0,  
  439. 'label' => __( 'Signup', 'membership2' ),  
  440. ),  
  441. $atts 
  442. ); 
  443. extract( $data ); 
  444.  
  445. if ( ! empty( $id ) ) { 
  446. $membership = MS_Factory::load( 'MS_Model_Membership', $id ); 
  447. $data['action'] = MS_Helper_Membership::MEMBERSHIP_ACTION_SIGNUP; 
  448. $data['step'] = MS_Controller_Frontend::STEP_PAYMENT_TABLE; 
  449.  
  450. $view = MS_Factory::create( 'MS_View_Shortcode_MembershipSignup' ); 
  451. $view->data = apply_filters( 
  452. 'ms_view_shortcode_membershipsignup_data',  
  453. $data,  
  454. $this 
  455. ); 
  456.  
  457. $code = $view->signup_form( $membership, $label ); 
  458.  
  459. $code = sprintf( 
  460. '<span class="ms-membership-buy ms-membership-%1$s">%2$s</span>',  
  461. esc_attr( $id ),  
  462. $code 
  463. ); 
  464.  
  465. return apply_filters( 
  466. 'ms_controller_shortcode_membership_buy',  
  467. $code,  
  468. $atts,  
  469. $this 
  470. ); 
  471.  
  472. /** 
  473. * Membership details shortcode callback function. 
  474. * 
  475. * @since 1.0.0 
  476. * 
  477. * @param mixed[] $atts Shortcode attributes. 
  478. */ 
  479. public function membership_details( $atts ) { 
  480. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_MS_DETAILS ); 
  481.  
  482. $code = ''; 
  483.  
  484. $data = apply_filters( 
  485. 'ms_controller_shortcode_membership_details_atts',  
  486. shortcode_atts( 
  487. array( 
  488. 'id' => 0,  
  489. 'label' => __( 'Membership details:', 'membership2' ),  
  490. ),  
  491. $atts 
  492. ); 
  493. extract( $data ); 
  494.  
  495. if ( ! empty( $id ) ) { 
  496. $membership = MS_Factory::load( 'MS_Model_Membership', $id ); 
  497. $code = sprintf( 
  498. '%1$s %2$s',  
  499. $label,  
  500. $membership->get_description() 
  501. ); 
  502.  
  503. $code = trim( $code ); 
  504.  
  505. $code = sprintf( 
  506. '<span class="ms-membership-details ms-membership-%1$s">%2$s</span>',  
  507. esc_attr( $id ),  
  508. $code 
  509. ); 
  510.  
  511. return apply_filters( 
  512. 'ms_controller_shortcode_membership_details',  
  513. $code,  
  514. $atts,  
  515. $this 
  516. ); 
  517.  
  518. /** 
  519. * Display the "Membership2" message. 
  520. * 
  521. * @since 1.0.0 
  522. * 
  523. * @param mixed[] $atts Shortcode attributes. 
  524. */ 
  525. public function protected_content( $atts ) { 
  526. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_PROTECTED ); 
  527.  
  528. global $post; 
  529.  
  530. $setting = MS_Plugin::instance()->settings; 
  531. $member = MS_Model_Member::get_current_member(); 
  532.  
  533. if ( count( $member->subscriptions ) ) { 
  534. $sub = $member->get_subscription( 'priority' ); 
  535. $protection_msg = $setting->get_protection_message( 
  536. MS_Model_Settings::PROTECTION_MSG_CONTENT,  
  537. $sub->membership_id 
  538. ); 
  539. } else { 
  540. $protection_msg = $setting->get_protection_message( 
  541. MS_Model_Settings::PROTECTION_MSG_CONTENT 
  542. ); 
  543.  
  544. $html = '<div class="ms-protected-content">'; 
  545. if ( ! empty( $protection_msg ) ) { 
  546. $html .= $protection_msg; 
  547.  
  548. if ( ! MS_Model_Member::is_logged_in() ) { 
  549. $has_login_form = MS_Helper_Shortcode::has_shortcode( 
  550. MS_Helper_Shortcode::SCODE_LOGIN,  
  551. $post->post_content 
  552. ); 
  553.  
  554. if ( ! $has_login_form ) { 
  555. $scode = '[' . MS_Helper_Shortcode::SCODE_LOGIN . ']'; 
  556. $html .= do_shortcode( $scode ); 
  557. $html .= '</div>'; 
  558.  
  559. return apply_filters( 
  560. 'ms_controller_shortcode_protected_content',  
  561. $html,  
  562. $this 
  563. ); 
  564.  
  565. /** 
  566. * Membership login shortcode callback function. 
  567. * 
  568. * @since 1.0.0 
  569. * 
  570. * @param mixed[] $atts Shortcode attributes. 
  571. */ 
  572. public function membership_login( $atts ) { 
  573. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_LOGIN ); 
  574.  
  575. $data = apply_filters( 
  576. 'ms_controller_shortcode_membership_login_atts',  
  577. shortcode_atts( 
  578. array( 
  579. 'holder' => 'div',  
  580. 'holderclass' => 'ms-login-form',  
  581. 'item' => '',  
  582. 'itemclass' => '',  
  583. 'postfix' => '',  
  584. 'prefix' => '',  
  585. 'wrapwith' => '',  
  586. 'wrapwithclass' => '',  
  587. 'redirect_login' => MS_Model_Pages::get_url_after_login(),  
  588. 'redirect_logout' => MS_Model_Pages::get_url_after_logout(),  
  589. 'header' => true,  
  590. 'register' => true,  
  591. 'title' => '',  
  592. 'show_note' => true, // Show the "you are not logged in" note? 
  593. 'form' => '', // [login|lost|reset|logout] 
  594. 'show_labels' => false,  
  595. 'autofocus' => true,  
  596. 'nav_pos' => 'top', // [top|bottom] 
  597.  
  598. // form="login" 
  599. 'show_remember' => true,  
  600. 'label_username' => __( 'Username', 'membership2' ),  
  601. 'label_password' => __( 'Password', 'membership2' ),  
  602. 'label_remember' => __( 'Remember Me', 'membership2' ),  
  603. 'label_log_in' => __( 'Log In', 'membership2' ),  
  604. 'id_login_form' => 'loginform',  
  605. 'id_username' => 'user_login',  
  606. 'id_password' => 'user_pass',  
  607. 'id_remember' => 'rememberme',  
  608. 'id_login' => 'wp-submit',  
  609. 'value_username' => '',  
  610. 'value_remember' => false,  
  611.  
  612. // form="lost" 
  613. 'label_lost_username' => __( 'Username or E-mail', 'membership2' ),  
  614. 'label_lostpass' => __( 'Reset Password', 'membership2' ),  
  615. 'id_lost_form' => 'lostpasswordform',  
  616. 'id_lost_username' => 'user_login',  
  617. 'id_lostpass' => 'wp-submit',  
  618. 'value_username' => '',  
  619. ),  
  620. $atts 
  621. ); 
  622.  
  623. $data['header'] = lib3()->is_true( $data['header'] ); 
  624. $data['register'] = lib3()->is_true( $data['register'] ); 
  625. $data['show_note'] = lib3()->is_true( $data['show_note'] ); 
  626. $data['show_labels'] = lib3()->is_true( $data['show_labels'] ); 
  627. $data['show_remember'] = lib3()->is_true( $data['show_remember'] ); 
  628. $data['value_remember'] = lib3()->is_true( $data['value_remember'] ); 
  629. $data['autofocus'] = lib3()->is_true( $data['autofocus'] ); 
  630.  
  631. $view = MS_Factory::create( 'MS_View_Shortcode_Login' ); 
  632. $view->data = apply_filters( 'ms_view_shortcode_login_data', $data, $this ); 
  633.  
  634. return $view->to_html(); 
  635.  
  636. /** 
  637. * Membership logout shortcode callback function. 
  638. * 
  639. * @since 1.0.0 
  640. * 
  641. * @param mixed[] $atts Shortcode attributes. 
  642. */ 
  643. public function membership_logout( $atts ) { 
  644. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_LOGOUT ); 
  645.  
  646. $data = apply_filters( 
  647. 'ms_controller_shortcode_membership_logout_atts',  
  648. shortcode_atts( 
  649. array( 
  650. 'holder' => 'div',  
  651. 'holderclass' => 'ms-logout-form',  
  652. 'redirect' => MS_Model_Pages::get_url_after_logout(),  
  653. ),  
  654. $atts 
  655. ); 
  656.  
  657. // The form attribute triggers the logout-link to be displayed. 
  658. $data['form'] = 'logout'; 
  659. $data['redirect_logout'] = $data['redirect']; 
  660.  
  661. $view = MS_Factory::create( 'MS_View_Shortcode_Login' ); 
  662. $view->data = apply_filters( 'ms_view_shortcode_logout_data', $data, $this ); 
  663.  
  664. return $view->to_html(); 
  665.  
  666. /** 
  667. * Membership account page shortcode callback function. 
  668. * 
  669. * @since 1.0.0 
  670. * 
  671. * @param mixed[] $atts Shortcode attributes. 
  672. */ 
  673. public function membership_account( $atts ) { 
  674. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_MS_ACCOUNT ); 
  675.  
  676. $data = apply_filters( 
  677. 'ms_controller_shortcode_membership_account_atts',  
  678. shortcode_atts( 
  679. array( 
  680. 'show_membership' => true,  
  681. 'show_membership_change' => true,  
  682. 'membership_title' => __( 'Your Membership', 'membership2' ),  
  683. 'membership_change_label' => __( 'Change', 'membership2' ),  
  684. 'show_profile' => true,  
  685. 'show_profile_change' => true,  
  686. 'profile_title' => __( 'Personal details', 'membership2' ),  
  687. 'profile_change_label' => __( 'Edit', 'membership2' ),  
  688. 'show_invoices' => true,  
  689. 'limit_invoices' => 10,  
  690. 'show_all_invoices' => true,  
  691. 'invoices_title' => __( 'Invoices', 'membership2' ),  
  692. 'invoices_details_label' => __( 'View all', 'membership2' ),  
  693. 'show_activity' => true,  
  694. 'limit_activities' => 10,  
  695. 'show_all_activities' => true,  
  696. 'activity_title' => __( 'Activities', 'membership2' ),  
  697. 'activity_details_label' => __( 'View all', 'membership2' ),  
  698. ),  
  699. $atts 
  700. ); 
  701.  
  702. $data['show_membership'] = lib3()->is_true( $data['show_membership'] ); 
  703. $data['show_membership_change'] = lib3()->is_true( $data['show_membership_change'] ); 
  704. $data['show_profile'] = lib3()->is_true( $data['show_profile'] ); 
  705. $data['show_profile_change'] = lib3()->is_true( $data['show_profile_change'] ); 
  706. $data['show_invoices'] = lib3()->is_true( $data['show_invoices'] ); 
  707. $data['show_all_invoices'] = lib3()->is_true( $data['show_all_invoices'] ); 
  708. $data['show_activity'] = lib3()->is_true( $data['show_activity'] ); 
  709. $data['show_all_activities'] = lib3()->is_true( $data['show_all_activities'] ); 
  710.  
  711. $data['limit_invoices'] = absint( $data['limit_invoices'] ); 
  712. $data['limit_activities'] = absint( $data['limit_activities'] ); 
  713.  
  714. $data['member'] = MS_Model_Member::get_current_member(); 
  715. $data['membership'] = array(); 
  716.  
  717. $subscriptions = MS_Model_Relationship::get_subscriptions( 
  718. array( 
  719. 'user_id' => $data['member']->id,  
  720. 'status' => 'all',  
  721. ); 
  722. if ( is_array( $subscriptions ) ) { 
  723. foreach ( $subscriptions as $subscription ) { 
  724. // Do not display system-memberships in Account 
  725. if ( $subscription->is_system() ) { continue; } 
  726.  
  727. // Do not display deactivated memberships in Account 
  728. if ( $subscription->get_status() == MS_Model_Relationship::STATUS_DEACTIVATED ) { continue; } 
  729.  
  730. $data['subscription'][] = $subscription; 
  731.  
  732. $data['invoices'] = MS_Model_Invoice::get_public_invoices( 
  733. $data['member']->id,  
  734. $data['limit_invoices'] 
  735. ); 
  736.  
  737. $data['events'] = MS_Model_Event::get_events( 
  738. array( 
  739. 'author' => $data['member']->id,  
  740. 'posts_per_page' => $data['limit_activities'],  
  741. ); 
  742.  
  743. $view = MS_Factory::create( 'MS_View_Shortcode_Account' ); 
  744. $view->data = apply_filters( 
  745. 'ms_view_shortcode_account_data',  
  746. $data,  
  747. $this 
  748. ); 
  749.  
  750. return $view->to_html(); 
  751.  
  752. /** 
  753. * Link to the Membership account page shortcode. 
  754. * 
  755. * @since 1.0.0 
  756. * 
  757. * @param mixed[] $atts Shortcode attributes. 
  758. */ 
  759. public function membership_account_link( $atts ) { 
  760. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_MS_ACCOUNT_LINK ); 
  761.  
  762. $html = ''; 
  763.  
  764. $data = apply_filters( 
  765. 'ms_controller_shortcode_membership_account_link_atts',  
  766. shortcode_atts( 
  767. array( 
  768. 'label' => __( 'Visit your account page for more information.', 'membership2' ),  
  769. ),  
  770. $atts 
  771. ); 
  772.  
  773. $html = sprintf( 
  774. '<a href="%1$s">%2$s</a>',  
  775. MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_ACCOUNT ),  
  776. $data['label'] 
  777. ); 
  778.  
  779. return apply_filters( 
  780. 'ms_controller_shortcode_protected_content',  
  781. $html,  
  782. $this 
  783. ); 
  784.  
  785. /** 
  786. * Membership invoice shortcode callback function. 
  787. * 
  788. * @since 1.0.0 
  789. * 
  790. * @param mixed[] $atts Shortcode attributes. 
  791. */ 
  792. public function membership_invoice( $atts ) { 
  793. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_MS_INVOICE ); 
  794.  
  795. $data = apply_filters( 
  796. 'ms_controller_shortcode_invoice_atts',  
  797. shortcode_atts( 
  798. array( 
  799. 'post_id' => 0,  
  800. 'id' => 0,  
  801. 'pay_button' => 1,  
  802. ),  
  803. $atts,  
  804. MS_Helper_Shortcode::SCODE_MS_INVOICE 
  805. ); 
  806.  
  807. if ( ! empty( $data['id'] ) ) { 
  808. $data['post_id'] = $data['id']; 
  809.  
  810. if ( ! empty( $data['post_id'] ) ) { 
  811. $invoice = MS_Factory::load( 'MS_Model_Invoice', $data['post_id'] ); 
  812. $subscription = MS_Factory::load( 'MS_Model_Relationship', $invoice->ms_relationship_id ); 
  813.  
  814. $data['invoice'] = $invoice; 
  815. $data['member'] = MS_Factory::load( 'MS_Model_Member', $invoice->user_id ); 
  816. $data['ms_relationship'] = $subscription; 
  817. $data['membership'] = $subscription->get_membership(); 
  818. $data['gateway'] = MS_Model_Gateway::factory( $invoice->gateway_id ); 
  819.  
  820. $view = MS_Factory::create( 'MS_View_Shortcode_Invoice' ); 
  821. $view->data = apply_filters( 
  822. 'ms_view_shortcode_invoice_data',  
  823. $data,  
  824. $this 
  825. ); 
  826.  
  827. return $view->to_html(); 
  828.  
  829. /** 
  830. * Text note shortcode callback function. 
  831. * 
  832. * @since 1.0.0 
  833. * 
  834. * @param mixed[] $atts Shortcode attributes. 
  835. */ 
  836. public function ms_note( $atts, $content = '' ) { 
  837. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_NOTE ); 
  838.  
  839. $atts = apply_filters( 
  840. 'ms_controller_shortcode_note_atts',  
  841. shortcode_atts( 
  842. array( 
  843. 'type' => 'info',  
  844. 'class' => '',  
  845. ),  
  846. $atts,  
  847. MS_Helper_Shortcode::SCODE_NOTE 
  848. ); 
  849.  
  850. $class = $atts['class']; 
  851.  
  852. switch ( $atts['type'] ) { 
  853. case 'info': 
  854. case 'success': 
  855. $class .= ' ms-alert-success'; 
  856. break; 
  857.  
  858. case 'error': 
  859. case 'warning': 
  860. $class .= ' ms-alert-error'; 
  861. break; 
  862.  
  863. /** 
  864. * The $content of the notice is translated! 
  865. * This gives translators the option to translate even custom messages 
  866. * that are entered into the shortcode! 
  867. * 
  868. * @since 1.0.0 
  869. */ 
  870. $content = sprintf( 
  871. '<p class="ms-alert-box %1$s">%2$s</p> ',  
  872. $class,  
  873. __( $content, 'membership2' ) 
  874. ); 
  875.  
  876. $content = do_shortcode( $content ); 
  877.  
  878. return apply_filters( 
  879. 'ms_controller_shortcode_ms_note',  
  880. $content,  
  881. $this 
  882. ); 
  883.  
  884. /** 
  885. * Display a green text note. 
  886. * 
  887. * @since 1.0.0 
  888. * @deprecated since 1.0.4.5 
  889. * 
  890. * @param mixed[] $atts Shortcode attributes. 
  891. */ 
  892. public function ms_green_note( $atts, $content = '' ) { 
  893. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_GREEN_NOTE ); 
  894.  
  895. $content = $this->ms_note( array( 'type' => 'info' ), $content ); 
  896.  
  897. return apply_filters( 
  898. 'ms_controller_shortcode_ms_green_note',  
  899. $content,  
  900. $this 
  901. ); 
  902.  
  903. /** 
  904. * Display a red text note. 
  905. * 
  906. * @since 1.0.0 
  907. * @deprecated since 1.0.4.5 
  908. * 
  909. * @param mixed[] $atts Shortcode attributes. 
  910. */ 
  911. public function ms_red_note( $atts, $content = '' ) { 
  912. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_RED_NOTE ); 
  913.  
  914. $content = $this->ms_note( array( 'type' => 'warning' ), $content ); 
  915.  
  916. return apply_filters( 
  917. 'ms_controller_shortcode_ms_red_note',  
  918. $content,  
  919. $this 
  920. ); 
  921.  
  922. /** 
  923. * Shortcode callback: Show message only to certain users. 
  924. * 
  925. * @since 1.0.0 
  926. * 
  927. * @param mixed[] $atts Shortcode attributes. 
  928. */ 
  929. public function show_to_user( $atts, $content = '' ) { 
  930. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_USER ); 
  931.  
  932. $data = apply_filters( 
  933. 'ms_controller_shortcode_show_to_user_atts',  
  934. shortcode_atts( 
  935. array( 
  936. 'type' => 'loggedin',  
  937. 'msg' => '',  
  938. ),  
  939. $atts 
  940. ); 
  941.  
  942. extract( $data ); 
  943. $access = false; 
  944.  
  945. $user_type = 'guest'; 
  946. if ( is_user_logged_in() ) { 
  947. $user_type = 'loggedin'; 
  948.  
  949. if ( MS_Model_Member::is_admin_user() ) { 
  950. $user_type = 'admin'; 
  951.  
  952. $class = 'ms-user-is-' . $user_type; 
  953.  
  954. switch ( $type ) { 
  955. case 'all': 
  956. $access = true; 
  957. break; 
  958.  
  959. case 'loggedin': 
  960. case 'login': 
  961. $access = in_array( $user_type, array( 'loggedin', 'admin' ) ); 
  962. break; 
  963.  
  964. case 'guest': 
  965. $access = ($user_type === 'guest' ); 
  966. break; 
  967.  
  968. case 'admin': 
  969. $access = ( $user_type === 'admin' ); 
  970. break; 
  971.  
  972. if ( ! $access ) { 
  973. $content = $msg; 
  974. $class .= ' ms-user-not-' . $type; 
  975.  
  976. $content = sprintf( 
  977. '<div class="ms-user %1$s">%2$s</div>',  
  978. esc_attr( $class ),  
  979. do_shortcode( $content ) 
  980. ); 
  981.  
  982. return apply_filters( 
  983. 'ms_controller_shortcode_show_to_user',  
  984. $content,  
  985. $this 
  986. ); 
  987.  
  988. /** 
  989. * Shortcode callback: Show member infos. 
  990. * 
  991. * @since 1.0.0 
  992. * 
  993. * @param mixed[] $atts Shortcode attributes. 
  994. */ 
  995. public function ms_member_info( $atts, $content = '' ) { 
  996. MS_Helper_Shortcode::did_shortcode( MS_Helper_Shortcode::SCODE_MEMBER_INFO ); 
  997.  
  998. $data = apply_filters( 
  999. 'ms_controller_shortcode_member_info_atts',  
  1000. shortcode_atts( 
  1001. array( 
  1002. 'value' => 'fullname',  
  1003. 'before' => '<span>',  
  1004. 'after' => '</span>',  
  1005. 'default' => '',  
  1006. 'custom_field' => '', // used for: custom 
  1007. 'list_before' => '', // used for: membership 
  1008. 'list_after' => '', // used for: membership 
  1009. 'list_separator' => ', ', // used for: membership 
  1010. 'user' => 0, // user-id; 0 = current user 
  1011. ),  
  1012. $atts 
  1013. ); 
  1014.  
  1015. $data['user'] = intval( $data['user'] ); 
  1016. if ( $data['user'] < 1 ) { 
  1017. $data['user'] = get_current_user_id(); 
  1018. $member = MS_Factory::load( 'MS_Model_Member', $data['user'] ); 
  1019. $content = ''; 
  1020.  
  1021. switch ( $data['value'] ) { 
  1022. case 'email': 
  1023. $content = $member->email; 
  1024. break; 
  1025.  
  1026. case 'firstname': 
  1027. case 'first name': 
  1028. $content = $member->first_name; 
  1029. break; 
  1030.  
  1031. case 'lastname': 
  1032. case 'last name': 
  1033. $content = $member->last_name; 
  1034. break; 
  1035.  
  1036. case 'fullname': 
  1037. case 'full name': 
  1038. $content = $member->first_name . ' ' . $member->last_name; 
  1039. break; 
  1040.  
  1041. case 'memberships': 
  1042. case 'membership': 
  1043. $ids = $member->get_membership_ids(); 
  1044. $content = array(); 
  1045. foreach ( $ids as $id ) { 
  1046. $membership = MS_Factory::load( 'MS_Model_Membership', $id ); 
  1047. if ( $membership->is_system() ) { continue; } 
  1048. $content[] = $membership->name; 
  1049. break; 
  1050.  
  1051. case 'custom': 
  1052. $content = $member->get_custom_data( $data['custom_field'] ); 
  1053. break; 
  1054.  
  1055. if ( is_array( $content ) ) { 
  1056. if ( $content ) { 
  1057. $content = 
  1058. $data['list_before'] . 
  1059. implode( $data['list_separator'], $content ) . 
  1060. $data['list_after']; 
  1061. } else { 
  1062. $content = ''; 
  1063. } else { 
  1064. $content = (string) $content; 
  1065.  
  1066. $content = trim( $content ); 
  1067. if ( $content ) { 
  1068. $content = $data['before'] . 
  1069. $content . 
  1070. $data['after']; 
  1071. } else { 
  1072. $content = $data['default']; 
  1073.  
  1074. return apply_filters( 
  1075. 'ms_controller_shortcode_member_info',  
  1076. $content,  
  1077. $this 
  1078. ); 
  1079.  
  1080.  
  1081. /** 
  1082. * Special Shortcode Callback: Replace shortcodes with empty value. 
  1083. * 
  1084. * All Shortcodes use this callback function 
  1085. * when Content Protection is DISABLED! 
  1086. * 
  1087. * @since 1.0.0 
  1088. * @param mixed[] $atts Shortcode attributes. 
  1089. * @param string $content 
  1090. * @return string 
  1091. */ 
  1092. public function ms_no_value( $atts, $content = '' ) { 
  1093. static $Done = false; 
  1094.  
  1095. if ( $Done ) { return ''; } 
  1096. $Done = true; 
  1097.  
  1098. if ( MS_Model_Member::is_admin_user() ) { 
  1099. $content = sprintf( 
  1100. '<p class="ms-alert-box ms-alert-error ms-unprotected">%s<br /><br /><em>(%s)</em></p>',  
  1101. __( 
  1102. 'Content Protection is disabled. Please enable the protection to see this shortcode.',  
  1103. 'membership2' 
  1104. ),  
  1105. __( 
  1106. 'This message is only displayed to Site Administrators',  
  1107. 'membership2' 
  1108. ); 
  1109. } else { 
  1110. $content = ''; 
  1111.  
  1112. return apply_filters( 
  1113. 'ms_controller_shortcode_ms_no_value',  
  1114. $content,  
  1115. $this 
  1116. ); 
  1117.  
  1118. /** 
  1119. * Special Shortcode Callback: Strip the shortcode tag without changing the 
  1120. * original content. 
  1121. * 
  1122. * This is used for Admin users to strip all content-protection tags. 
  1123. * 
  1124. * @since 1.0.0 
  1125. * @param mixed[] $atts Shortcode attributes. 
  1126. * @param string $content 
  1127. * @return string 
  1128. */ 
  1129. public function hide_shortcode( $atts, $content = '' ) { 
  1130. return do_shortcode( $content ); 
  1131.  
.