/**
  * Prepare the PayPal IPN fields
  *
  * Details here:
  * https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/
  *
  * @since  1.0.0
  * @return array
  */
 private function prepare_fields()
 {
     $subscription = $this->data['ms_relationship'];
     $membership = $subscription->get_membership();
     if (0 === $membership->price) {
         return;
     }
     $gateway = $this->data['gateway'];
     $invoice = $subscription->get_current_invoice();
     $fields = array('business' => array('id' => 'business', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $gateway->paypal_email), 'cmd' => array('id' => 'cmd', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => '_xclick'), 'bn' => array('id' => 'bn', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => 'incsub_SP'), 'item_number' => array('id' => 'item_number', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $subscription->membership_id), 'item_name' => array('id' => 'item_name', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $membership->name), 'amount' => array('id' => 'amount', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => MS_Helper_Billing::format_price($invoice->total)), 'currency_code' => array('id' => 'currency_code', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $invoice->currency), 'return' => array('id' => 'return', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => esc_url_raw(add_query_arg(array('ms_relationship_id' => $subscription->id), MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REG_COMPLETE, false)))), 'cancel_return' => array('id' => 'cancel_return', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REGISTER)), 'notify_url' => array('id' => 'notify_url', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $gateway->get_return_url()), 'lc' => array('id' => 'lc', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $gateway->paypal_site), 'invoice' => array('id' => 'invoice', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $invoice->id));
     // Don't send to paypal if free
     if (0 === $invoice->total) {
         $fields = array('gateway' => array('id' => 'gateway', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $gateway->id), 'ms_relationship_id' => array('id' => 'ms_relationship_id', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $subscription->id), 'step' => array('id' => 'step', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => MS_Controller_Frontend::STEP_PROCESS_PURCHASE), '_wpnonce' => array('id' => '_wpnonce', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => wp_create_nonce($gateway->id . '_' . $subscription->id)));
         $this->data['action_url'] = null;
     } else {
         if ($gateway->is_live_mode()) {
             $this->data['action_url'] = 'https://www.paypal.com/cgi-bin/webscr';
         } else {
             $this->data['action_url'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
         }
     }
     $fields['submit'] = array('id' => 'submit', 'type' => MS_Helper_Html::INPUT_TYPE_IMAGE, 'value' => 'https://www.paypalobjects.com/en_US/i/btn/x-click-but06.gif', 'alt' => __('PayPal - The safer, easier way to pay online', MS_TEXT_DOMAIN));
     // custom pay button defined in gateway settings
     $custom_label = $gateway->pay_button_url;
     if (!empty($custom_label)) {
         if (false !== strpos($custom_label, '://')) {
             $fields['submit']['value'] = $custom_label;
         } else {
             $fields['submit'] = array('id' => 'submit', 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT, 'value' => $custom_label);
         }
     }
     return apply_filters('ms_gateway_paypalsingle_view_prepare_fields', $fields);
 }
 private function prepare_fields()
 {
     $gateway = $this->data['gateway'];
     $subscription = $this->data['ms_relationship'];
     $membership = $subscription->get_membership();
     $invoice = $subscription->get_current_invoice();
     $member = $subscription->get_member();
     $fields = array('sid' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'sid', 'value' => $gateway->seller_id), 'mode' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'mode', 'value' => '2CO'), 'type' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'li_0_type', 'value' => 'product'), 'name' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'li_0_name', 'value' => $membership->name), 'price' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'li_0_price', 'value' => $invoice->total), 'tangible' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'li_0_tangible', 'value' => 'N'), 'skip_landing' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'skip_landing', 'value' => '1'), 'user_id' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'user_id', 'value' => $member->id), 'email' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'email', 'value' => $member->email), 'currency' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'currency', 'value' => $invoice->currency), 'merchant_order_id' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'merchant_order_id', 'value' => $invoice->id), 'return_url' => array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'x_receipt_link_url', 'value' => esc_url_raw(add_query_arg(array('ms_relationship_id' => $subscription->id), MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REG_COMPLETE, false)))));
     if (MS_Model_Membership::PAYMENT_TYPE_RECURRING == $membership->payment_type) {
         #'li_0_reccurance' = '2 days'   // Can use # Week / # Month / # Year
         #'li_0_duration' = 'Forever'    // Same as _recurrence, with additional "Forever" option
     }
     if (false !== strpos($gateway->pay_button_url, '://')) {
         $fields['submit'] = array('id' => 'submit', 'type' => MS_Helper_Html::INPUT_TYPE_IMAGE, 'value' => $gateway->pay_button_url);
     } else {
         $fields['submit'] = array('id' => 'submit', 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT, 'value' => __('Signup', MS_TEXT_DOMAIN));
         if ($gateway->pay_button_url) {
             $fields['submit']['value'] = $gateway->pay_button_url;
         }
     }
     // Don't send to gateway if free
     if (0 === $invoice->total) {
         $this->data['action_url'] = null;
     } else {
         if ($gateway->is_live_mode()) {
             $this->data['action_url'] = 'https://www.2checkout.com/checkout/purchase';
         } else {
             $this->data['action_url'] = 'https://sandbox.2checkout.com/checkout/purchase';
         }
     }
     return $fields;
 }
    public function to_html()
    {
        $fields = $this->prepare_fields();
        $subscription = $this->data['ms_relationship'];
        $invoice = $subscription->get_current_invoice();
        $gateway = $this->data['gateway'];
        // Force ssl url
        $action_url = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REGISTER, true);
        $action_url = apply_filters('ms_gateway_authorize_view_button_form_action_url', $action_url);
        $row_class = 'gateway_' . $gateway->id;
        if (!$gateway->is_live_mode()) {
            $row_class .= ' sandbox-mode';
        }
        ob_start();
        ?>
		<form action="<?php 
        echo esc_url($action_url);
        ?>
" method="post">
			<?php 
        MS_Helper_Html::html_element($fields['_wpnonce']);
        ?>
			<?php 
        MS_Helper_Html::html_element($fields['gateway']);
        ?>
			<?php 
        MS_Helper_Html::html_element($fields['ms_relationship_id']);
        ?>
			<?php 
        MS_Helper_Html::html_element($fields['step']);
        ?>
			<?php 
        MS_Helper_Html::html_element($fields['submit']);
        ?>
		</form>
		<?php 
        $payment_form = apply_filters('ms_gateway_form', ob_get_clean(), $gateway, $invoice, $this);
        ob_start();
        ?>
		<tr class="<?php 
        echo esc_attr($row_class);
        ?>
">
			<td class="ms-buy-now-column" colspan="2">
				<?php 
        echo $payment_form;
        ?>
			</td>
		</tr>
		<?php 
        $html = ob_get_clean();
        $html = apply_filters('ms_gateway_button-' . $gateway->id, $html, $this);
        $html = apply_filters('ms_gateway_button', $html, $gateway->id, $this);
        return $html;
    }
    public function to_html()
    {
        $fields = $this->prepare_fields();
        $gateway = $this->data['model'];
        ob_start();
        // Render tabbed interface.
        ?>
		<form class="ms-gateway-settings-form ms-form">
			<?php 
        $description = sprintf('%1$s<br />&nbsp;<br />%2$s <strong>%3$s</strong><br />%6$s <strong>%7$s</strong><br /><a href="%4$s" target="_blank">%5$s</a>', __('In order for Membership 2 to function correctly you must setup an INS (Instant Notification Service) URL with 2Checkout. Make sure you add the following URLs to your 2Checkout "Notifications" section as well as the "Approved URL" in the Site Management section. The domain must be the same as the one registered with your Live account for production sites.', 'membership2'), __('Your Global Notifications URL is:', 'membership2'), $this->data['model']->get_return_url(), 'https://www.2checkout.com/documentation/notifications/', __('Instructions &raquo;', 'membership2'), __('Your "Approved URL" is:', 'membership2'), MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REG_COMPLETE));
        MS_Helper_Html::settings_box_header('', $description);
        foreach ($fields as $field) {
            MS_Helper_Html::html_element($field);
        }
        MS_Helper_Html::settings_box_footer();
        ?>
		</form>
		<?php 
        $html = ob_get_clean();
        return $html;
    }
 public function get_success_url()
 {
     if (Pronamic_WP_Pay_Extensions_WPMUDEV_Membership_Extension::is_membership2()) {
         return esc_url_raw(add_query_arg(array('ms_relationship_id' => $this->subscription->id), MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REG_COMPLETE, false)));
     }
     return M_get_registrationcompleted_permalink();
 }
    /**
     * Output the HTML content of a single membership box.
     * This includes the membership name, description, price and the action
     * button (Sign-up, Cancel, etc.)
     *
     * @since  1.0.0
     * @param  MS_Model_Membership $membership
     * @param  string $action
     * @param  string $msg
     * @param  MS_Model_Relationship $subscription
     */
    private function membership_box_html($membership, $action, $msg = null, $subscription = null)
    {
        $fields = $this->prepare_fields($membership->id, $action, $this->data['step'], $membership->_move_from);
        $settings = MS_Factory::load('MS_Model_Settings');
        if (0 == $membership->price) {
            $price = __('Free', MS_TEXT_DOMAIN);
        } else {
            $price = sprintf('%s %s', $settings->currency, MS_Helper_Billing::format_price($membership->total_price));
        }
        $price = apply_filters('ms_membership_price', $price, $membership);
        if (is_user_logged_in()) {
            $current = MS_Model_Pages::MS_PAGE_MEMBERSHIPS;
        } else {
            $current = MS_Model_Pages::MS_PAGE_REGISTER;
        }
        $url = MS_Model_Pages::get_page_url($current);
        $classes = array('ms-membership-details-wrapper', 'ms-signup', 'ms-membership-' . $membership->id, 'ms-type-' . $membership->type, 'ms-payment-' . $membership->payment_type, $membership->trial_period_enabled ? 'ms-with-trial' : 'ms-no-trial', 'ms-status-' . ($subscription ? $subscription->status : 'none'), 'ms-subscription-' . ($subscription ? $subscription->id : 'none'));
        ?>
		<form action="<?php 
        echo esc_url($url);
        ?>
" class="ms-membership-form" method="post">
			<div id="ms-membership-wrapper-<?php 
        echo esc_attr($membership->id);
        ?>
" class="<?php 
        echo esc_attr(implode(' ', $classes));
        ?>
">
				<div class="ms-top-bar">
					<h4><span class="ms-title"><?php 
        echo esc_html($membership->name);
        ?>
</span></h4>
				</div>
				<div class="ms-price-details">
					<div class="ms-description"><?php 
        echo '' . $membership->description;
        ?>
</div>
					<div class="ms-price price"><?php 
        echo esc_html($price);
        ?>
</div>

					<?php 
        if ($msg) {
            ?>
						<div class="ms-bottom-msg"><?php 
            echo '' . $msg;
            ?>
</div>
					<?php 
        }
        ?>
				</div>

				<div class="ms-bottom-bar">
					<?php 
        $class = apply_filters('ms_view_shortcode_membershipsignup_form_button_class', 'ms-signup-button ' . esc_attr($action));
        $button = array('id' => 'submit', 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT, 'value' => esc_html($this->data["{$action}_text"]), 'class' => $class);
        if (MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL === $action) {
            /**
             * PayPal Standard Gateway uses a special Cancel button.
             *
             * @see MS_Controller_Gateway
             */
            $button = apply_filters('ms_view_shortcode_membershipsignup_cancel_button', $button, $subscription, $this);
        } elseif (MS_Helper_Membership::MEMBERSHIP_ACTION_PAY === $action) {
            // Paid membership: Display a Cancel button
            $cancel_action = MS_Helper_Membership::MEMBERSHIP_ACTION_CANCEL;
            $url = $this->get_action_url($membership->id, $cancel_action, '');
            $link = array('url' => $url, 'class' => 'ms-cancel-button button', 'value' => esc_html($this->data["{$cancel_action}_text"]));
            MS_Helper_Html::html_link($link);
        }
        wp_nonce_field($fields['action']['value']);
        foreach ($fields as $field) {
            MS_Helper_Html::html_element($field);
        }
        MS_Helper_Html::html_element($button);
        ?>
				</div>
			</div>
		</form>
		<?php 
        do_action('ms_show_prices');
    }
 /**
  * Ajax handler. Used by shortcode `ms-membership-login` to recover password
  *
  * @since  1.0.0
  * @access public
  */
 public function ajax_lostpass()
 {
     global $wpdb, $wp_hasher;
     $resp = array();
     // First check the nonce, if it fails the function will break
     check_ajax_referer('ms-ajax-lostpass');
     // Nonce is checked, get the POST data and sign user on
     $errors = new WP_Error();
     if (empty($_POST['user_login'])) {
         $resp['error'] = __('Enter a username or e-mail address.', MS_TEXT_DOMAIN);
     } else {
         if (strpos($_POST['user_login'], '@')) {
             $user_data = get_user_by('email', trim($_POST['user_login']));
             if (empty($user_data)) {
                 $resp['error'] = __('There is no user registered with that email address.', MS_TEXT_DOMAIN);
             }
         } else {
             $login = trim($_POST['user_login']);
             $user_data = get_user_by('login', $login);
         }
     }
     do_action('lostpassword_post');
     if (!empty($resp['error'])) {
         $this->respond($resp);
     }
     if (!$user_data) {
         $resp['error'] = __('Invalid username or e-mail.', MS_TEXT_DOMAIN);
         $this->respond($resp);
     }
     // Redefining user_login ensures we return the right case in the email.
     $user_login = $user_data->user_login;
     $user_email = $user_data->user_email;
     do_action('retreive_password', $user_login);
     // Legacy (misspelled)
     do_action('retrieve_password', $user_login);
     $allow = apply_filters('allow_password_reset', true, $user_data->ID);
     if (!$allow) {
         $resp['error'] = __('Password reset is not allowed for this user', MS_TEXT_DOMAIN);
         $this->respond($resp);
     } else {
         if (is_wp_error($allow)) {
             return $allow;
         }
     }
     // Generate something random for a password reset key.
     $key = wp_generate_password(20, false);
     do_action('retrieve_password_key', $user_login, $key);
     // Now insert the key, hashed, into the DB.
     if (empty($wp_hasher)) {
         require_once ABSPATH . WPINC . '/class-phpass.php';
         $wp_hasher = new PasswordHash(8, true);
     }
     $hashed = $wp_hasher->HashPassword($key);
     $wpdb->update($wpdb->users, array('user_activation_key' => $hashed), array('user_login' => $user_login));
     MS_Model_Pages::create_missing_pages();
     $reset_url = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_ACCOUNT);
     $reset_url = esc_url_raw(add_query_arg(array('action' => MS_Controller_Frontend::ACTION_VIEW_RESETPASS, 'key' => $key, 'login' => rawurlencode($user_login)), $reset_url));
     $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
     $message .= network_home_url('/') . "\r\n\r\n";
     $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
     $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
     $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
     $message .= '<' . $reset_url . ">\r\n";
     if (is_multisite()) {
         $blogname = $GLOBALS['current_site']->site_name;
     } else {
         $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
     }
     $title = sprintf(__('[%s] Password Reset'), $blogname);
     $title = apply_filters('retrieve_password_title', $title);
     $message = apply_filters('retrieve_password_message', $message, $key, $reset_url);
     if ($message && !wp_mail($user_email, wp_specialchars_decode($title), $message)) {
         $resp['error'] = __('The e-mail could not be sent.') . '<br />' . __('Possible reason: your host may have disabled the mail() function.');
     } else {
         $resp['success'] = __('Check your e-mail for the confirmation link.', MS_TEXT_DOMAIN);
     }
     $this->respond($resp);
 }
 /**
  * Creates a new password-reset key and stores it in the DB.
  *
  * @since  1.0.2.3
  * @return object {
  *         The reset-key that can be sent to the user.
  *
  *         @var  key   The reset key
  *         @var  url   The full reset URL
  *  }
  */
 public function new_password_reset_key()
 {
     global $wpdb, $wp_hasher;
     // Generate something random for a password reset key.
     $key = wp_generate_password(20, false);
     do_action('retrieve_password_key', $this->username, $key);
     // Now insert a hashed version of the key into the DB.
     // Important: The has needs to include the time() value!
     if (empty($wp_hasher)) {
         require_once ABSPATH . WPINC . '/class-phpass.php';
         $wp_hasher = new PasswordHash(8, true);
     }
     $hashed = time() . ':' . $wp_hasher->HashPassword($key);
     $wpdb->update($wpdb->users, array('user_activation_key' => $hashed), array('user_login' => $this->username));
     MS_Model_Pages::create_missing_pages();
     $reset_url = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_ACCOUNT);
     $reset_url = esc_url_raw(add_query_arg(array('action' => MS_Controller_Frontend::ACTION_VIEW_RESETPASS, 'key' => $key, 'login' => rawurlencode($this->username)), $reset_url));
     return (object) array('key' => $key, 'url' => $reset_url);
 }
 /**
  * Link to the Membership account page shortcode.
  *
  * @since  1.0.0
  *
  * @param mixed[] $atts Shortcode attributes.
  */
 public function membership_account_link($atts)
 {
     MS_Helper_Shortcode::did_shortcode(MS_Helper_Shortcode::SCODE_MS_ACCOUNT_LINK);
     $html = '';
     $data = apply_filters('ms_controller_shortcode_membership_account_link_atts', shortcode_atts(array('label' => __('Visit your account page for more information.', 'membership2')), $atts));
     $html = sprintf('<a href="%1$s">%2$s</a>', MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_ACCOUNT), $data['label']);
     return apply_filters('ms_controller_shortcode_protected_content', $html, $this);
 }
 /**
  * Replace comm_vars with corresponding values.
  *
  * @since  1.0.0
  *
  * @param MS_Model_Relationship $subscription The membership relationship to send message to.
  * @param WP_User $wp_user The wordpress user object to get info from.
  * @return array {
  *     Returns array of ( $var_name => $var_replace ).
  *
  *     @type string $var_name The variable name to replace.
  *     @type string $var_replace The variable corresponding replace string.
  * }
  */
 public function get_comm_vars($subscription, $wp_user)
 {
     $currency = MS_Plugin::instance()->settings->currency . ' ';
     $membership = $subscription->get_membership();
     $invoice = null;
     // First try to fetch the current invoice.
     $invoice = $subscription->get_current_invoice(false);
     $prev_invoice = $subscription->get_previous_invoice();
     // If no current invoice exists then fetch the previous invoice.
     if (empty($invoice)) {
         $invoice = $prev_invoice;
     }
     $comm_vars = apply_filters('ms_model_communication_comm_vars', $this->comm_vars);
     foreach ($comm_vars as $key => $description) {
         switch ($key) {
             case self::COMM_VAR_BLOG_NAME:
                 $comm_vars[$key] = get_option('blogname');
                 break;
             case self::COMM_VAR_BLOG_URL:
                 $comm_vars[$key] = get_option('home');
                 break;
             case self::COMM_VAR_USERNAME:
                 $comm_vars[$key] = $wp_user->user_login;
                 break;
             case self::COMM_VAR_USER_DISPLAY_NAME:
                 $comm_vars[$key] = $wp_user->display_name;
                 break;
             case self::COMM_VAR_USER_FIRST_NAME:
                 $comm_vars[$key] = $wp_user->user_firstname;
                 break;
             case self::COMM_VAR_USER_LAST_NAME:
                 $comm_vars[$key] = $wp_user->user_lastname;
                 break;
             case self::COMM_VAR_NET_NAME:
                 $comm_vars[$key] = get_site_option('site_name');
                 break;
             case self::COMM_VAR_NET_URL:
                 $comm_vars[$key] = get_site_option('siteurl');
                 break;
             case self::COMM_VAR_MS_NAME:
                 if ($membership->name) {
                     $comm_vars[$key] = $membership->name;
                 } else {
                     $comm_vars[$key] = '';
                 }
                 break;
             case self::COMM_VAR_MS_INVOICE:
                 if (isset($invoice) && ($invoice->total > 0 || $invoice->uses_trial)) {
                     $attr = array('post_id' => $invoice->id, 'pay_button' => 0);
                     $scode = MS_Factory::load('MS_Controller_Shortcode');
                     $comm_vars[$key] = $scode->membership_invoice($attr);
                 } else {
                     $comm_vars[$key] = '';
                 }
                 break;
             case self::COMM_VAR_MS_ACCOUNT_PAGE_URL:
                 $comm_vars[$key] = sprintf('<a href="%s">%s</a>', MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_ACCOUNT), __('account page', MS_TEXT_DOMAIN));
                 break;
             case self::COMM_VAR_MS_REMAINING_DAYS:
                 $days = $subscription->get_remaining_period();
                 $comm_vars[$key] = sprintf(__('%s day%s', MS_TEXT_DOMAIN), $days, abs($days) > 1 ? 's' : '');
                 break;
             case self::COMM_VAR_MS_REMAINING_TRIAL_DAYS:
                 $days = $subscription->get_remaining_trial_period();
                 $comm_vars[$key] = sprintf(__('%s day%s', MS_TEXT_DOMAIN), $days, abs($days) > 1 ? 's' : '');
                 break;
             case self::COMM_VAR_MS_EXPIRY_DATE:
                 $comm_vars[$key] = $subscription->expire_date;
                 break;
         }
         $comm_vars[$key] = apply_filters('ms_model_communication_send_message_comm_var_' . $key, $comm_vars[$key], $subscription);
     }
     return apply_filters('ms_model_communication_get_comm_vars', $comm_vars);
 }
 /**
  * Checks member permissions and protects current page.
  *
  * Related Action Hooks:
  * - template_redirect
  *
  * @since  1.0.0
  */
 public function protect_current_page()
 {
     do_action('ms_model_plugin_protect_current_page_before', $this);
     // Admin user has access to everything
     if ($this->member->is_normal_admin()) {
         return;
     }
     $access = $this->get_access_info();
     if (!$access['has_access']) {
         MS_Model_Pages::create_missing_pages();
         $no_access_page_url = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT, false);
         $current_page_url = MS_Helper_Utility::get_current_url();
         // Don't (re-)redirect the protection page.
         if (!MS_Model_Pages::is_membership_page(null, MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT)) {
             $no_access_page_url = esc_url_raw(add_query_arg(array('redirect_to' => urlencode($current_page_url)), $no_access_page_url));
             $no_access_page_url = apply_filters('ms_model_plugin_protected_content_page', $no_access_page_url);
             wp_safe_redirect($no_access_page_url);
             exit;
         }
     }
     do_action('ms_model_plugin_protect_current_page_after', $this);
 }
 /**
  * Prepare the PayPal IPN fields
  *
  * Details here:
  * https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/
  *
  * @since  1.0.0
  * @return array
  */
 private function prepare_fields()
 {
     $subscription = $this->data['ms_relationship'];
     $membership = $subscription->get_membership();
     if (0 === $membership->price) {
         return;
     }
     $gateway = $this->data['gateway'];
     $invoice = $subscription->get_current_invoice();
     $regular_invoice = null;
     $settings = MS_Factory::load('MS_Model_Settings');
     $nonce = wp_create_nonce($gateway->id . '_' . $subscription->id);
     $cancel_url = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REGISTER);
     $return_url = esc_url_raw(add_query_arg(array('ms_relationship_id' => $subscription->id), MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REG_COMPLETE, false)));
     $fields = array('_wpnonce' => array('id' => '_wpnonce', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $nonce), 'charset' => array('id' => 'charset', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => 'utf-8'), 'business' => array('id' => 'business', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $gateway->merchant_id), 'cmd' => array('id' => 'cmd', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => '_xclick-subscriptions'), 'bn' => array('id' => 'bn', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => 'incsub_SP'), 'item_name' => array('id' => 'item_name', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $membership->name), 'item_number' => array('id' => 'item_number', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $membership->id), 'currency_code' => array('id' => 'currency_code', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => MS_Plugin::instance()->settings->currency), 'return' => array('id' => 'return', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $return_url), 'cancel_return' => array('id' => 'cancel_return', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $cancel_url), 'notify_url' => array('id' => 'notify_url', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $gateway->get_return_url()), 'country' => array('id' => 'country', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $gateway->paypal_site), 'no_note' => array('id' => 'no_note', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => 1), 'no_shipping' => array('id' => 'no_shipping', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => 1), 'invoice' => array('id' => 'invoice', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $invoice->id));
     $fields['submit'] = array('id' => 'submit', 'type' => MS_Helper_Html::INPUT_TYPE_IMAGE, 'value' => 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif', 'alt' => __('PayPal - The safer, easier way to pay online', 'membership2'));
     // custom pay button defined in gateway settings
     $custom_label = $gateway->pay_button_url;
     if (!empty($custom_label)) {
         if (false !== strpos($custom_label, '://')) {
             $fields['submit']['value'] = $custom_label;
         } else {
             $fields['submit'] = array('id' => 'submit', 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT, 'value' => $custom_label);
         }
     }
     // Trial period
     if ($subscription->is_trial_eligible()) {
         $fields['a1'] = array('id' => 'a1', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $invoice->trial_price);
         $trial_type = MS_Helper_Period::get_period_value($membership->trial_period, 'period_type');
         $trial_type = strtoupper($trial_type[0]);
         $trial_value = MS_Helper_Period::get_period_value($membership->trial_period, 'period_unit');
         $trial_value = MS_Helper_Period::validate_range($trial_value, $trial_type);
         $fields['p1'] = array('id' => 'p1', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $trial_value);
         $fields['t1'] = array('id' => 't1', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $trial_type);
     }
     // Membership price
     $membership_price = $invoice->total;
     $membership_price = MS_Helper_Billing::format_price($membership_price);
     $fields['a3'] = array('id' => 'a3', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $membership_price);
     $fields['amount'] = array('id' => 'amount', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $membership_price);
     $recurring = 0;
     switch ($membership->payment_type) {
         // == RECURRING PAYMENTS
         case MS_Model_Membership::PAYMENT_TYPE_RECURRING:
             $period_type = MS_Helper_Period::get_period_value($membership->pay_cycle_period, 'period_type');
             $period_type = strtoupper($period_type[0]);
             $period_value = MS_Helper_Period::get_period_value($membership->pay_cycle_period, 'period_unit');
             $period_value = MS_Helper_Period::validate_range($period_value, $period_type);
             $fields['p3'] = array('id' => 'p3', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $period_value);
             $fields['t3'] = array('id' => 't3', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $period_type);
             // This makes the payments recurring!
             $recurring = 1;
             break;
             // == FINITE END DATE
         // == FINITE END DATE
         case MS_Model_Membership::PAYMENT_TYPE_FINITE:
             $period_type = MS_Helper_Period::get_period_value($membership->period, 'period_type');
             $period_type = strtoupper($period_type[0]);
             $period_value = MS_Helper_Period::get_period_value($membership->period, 'period_unit');
             $period_value = MS_Helper_Period::validate_range($period_value, $period_type);
             $fields['p3'] = array('id' => 'p3', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $period_value);
             $fields['t3'] = array('id' => 't3', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $period_type);
             break;
             // == DATE RANGE
         // == DATE RANGE
         case MS_Model_Membership::PAYMENT_TYPE_DATE_RANGE:
             $period_value = MS_Helper_Period::subtract_dates($membership->period_date_end, $membership->period_date_start);
             $period_value = MS_Helper_Period::validate_range($period_value, 'D');
             $fields['p3'] = array('id' => 'p3', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $period_value);
             $fields['t3'] = array('id' => 't3', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => 'D');
             break;
             // == PERMANENT
         // == PERMANENT
         case MS_Model_Membership::PAYMENT_TYPE_PERMANENT:
             /*
              * Permanent membership: Set the subscription range to 5 years!
              * PayPal requires us to provide the subscription range and the
              * maximum possible value is 5 years.
              */
             $period_value = MS_Helper_Period::validate_range(5, 'Y');
             $fields['p3'] = array('id' => 'p3', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $period_value);
             $fields['t3'] = array('id' => 't3', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => 'Y');
             break;
     }
     if (1 == $recurring) {
         if (1 == $membership->pay_cycle_repetitions) {
             $recurring = 0;
         } elseif ($membership->pay_cycle_repetitions > 1) {
             /**
              * Recurring times.
              * The number of times that a recurring payment is made.
              */
             $fields['srt'] = array('id' => 'srt', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $membership->pay_cycle_repetitions);
         }
     }
     /**
      * Recurring field.
      * 0 - one time payment
      * 1 - recurring payments
      */
     $fields['src'] = array('id' => 'src', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $recurring);
     /**
      * Modify current subscription field.
      * value != 0 does not allow trial period.
      * 0 - allows subscribers only to sign up for new subscriptions
      * 1 - allows subscribers to sign up for new subscriptions and modify their current subscriptions
      * 2 - allows subscribers to modify only their current subscriptions
      */
     $modify = !empty($move_from_id);
     $fields['modify'] = array('id' => 'modify', 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'value' => $modify ? 2 : 0);
     if ($gateway->is_live_mode()) {
         $this->data['action_url'] = 'https://www.paypal.com/cgi-bin/webscr';
     } else {
         $this->data['action_url'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
     }
     return apply_filters('ms_gateway_paypalstandard_view_prepare_fields', $fields);
 }
/**
 * Desc:  Filter wp_nav_menu() to add profile link in a specific custom menu e.g. Login or logout
 * author: ajay3085006
 * dated: 27 Dec 2015
 * updated: 30 Dec 2015
 */
function x_nav_menu_signup_logout_link($menu, $args)
{
    //print_r($args);
    if (is_user_logged_in() && $args->theme_location == 'primary') {
        $profilelink = '<li><a href="' . wp_logout_url(get_permalink()) . '">' . __('Logout', 'x') . '</a></li>';
        $menu = $menu . $profilelink;
    } else {
        if ($args->theme_location == 'primary') {
            $url = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REGISTER);
            $profilelink = '<li><a href="' . $url . '?step=register" class="show-register" >' . __('Signup', 'x') . '</a></li>';
            $menu = $menu . $profilelink;
        } else {
        }
    }
    return $menu;
}
 /**
  * Replace comm_vars with corresponding values.
  *
  * @since  1.0.0
  *
  * @param MS_Model_Relationship $subscription The membership relationship to send message to.
  * @param MS_Model_Member $member The member object to get info from.
  * @return array {
  *     Returns array of ( $var_name => $var_replace ).
  *
  *     @type string $var_name The variable name to replace.
  *     @type string $var_replace The variable corresponding replace string.
  * }
  */
 public function get_comm_vars($subscription, $member)
 {
     $currency = MS_Plugin::instance()->settings->currency . ' ';
     $invoice = null;
     $membership = null;
     if ($subscription && $subscription instanceof MS_Model_Relationship) {
         // First try to fetch the current invoice.
         $invoice = $subscription->get_current_invoice(false);
         $prev_invoice = $subscription->get_previous_invoice();
         // If no current invoice exists then fetch the previous invoice.
         if (empty($invoice)) {
             $invoice = $prev_invoice;
         }
         $membership = $subscription->get_membership();
     }
     $comm_vars = apply_filters('ms_model_communication_comm_vars', $this->comm_vars, $this->type, $member, $subscription);
     $wp_user = $member->get_user();
     foreach ($comm_vars as $key => $description) {
         $var_value = '';
         switch ($key) {
             case self::COMM_VAR_BLOG_NAME:
                 $var_value = get_option('blogname');
                 break;
             case self::COMM_VAR_BLOG_URL:
                 $var_value = get_option('home');
                 break;
             case self::COMM_VAR_USERNAME:
                 $var_value = $member->username;
                 break;
             case self::COMM_VAR_PASSWORD:
                 /**
                  * $member->password is ONLY available in the same request
                  * when the new user account was created! After this we only
                  * have the encrypted password in the DB, and the plain-text
                  * version will never be available again in code...
                  *
                  * @since 1.0.1.1
                  */
                 if (self::COMM_TYPE_SIGNUP == $this->type) {
                     $var_value = $member->password;
                 }
                 break;
             case self::COMM_VAR_USER_DISPLAY_NAME:
                 $var_value = $wp_user->display_name;
                 break;
             case self::COMM_VAR_USER_FIRST_NAME:
                 $var_value = $member->first_name;
                 break;
             case self::COMM_VAR_USER_LAST_NAME:
                 $var_value = $member->last_name;
                 break;
             case self::COMM_VAR_NET_NAME:
                 $var_value = get_site_option('site_name');
                 break;
             case self::COMM_VAR_NET_URL:
                 $var_value = get_site_option('siteurl');
                 break;
             case self::COMM_VAR_MS_ACCOUNT_PAGE_URL:
                 $var_value = sprintf('<a href="%s">%s</a>', MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_ACCOUNT), __('account page', MS_TEXT_DOMAIN));
                 break;
                 // Needs: $membership
             // Needs: $membership
             case self::COMM_VAR_MS_NAME:
                 if ($membership && $membership->name) {
                     $var_value = $membership->name;
                 }
                 break;
                 // Needs: $invoice
             // Needs: $invoice
             case self::COMM_VAR_MS_INVOICE:
                 if ($invoice) {
                     if ($invoice->total > 0 || $invoice->uses_trial) {
                         $attr = array('post_id' => $invoice->id, 'pay_button' => 0);
                         $scode = MS_Factory::load('MS_Controller_Shortcode');
                         $var_value = $scode->membership_invoice($attr);
                     }
                 }
                 break;
                 // Needs: $subscription
             // Needs: $subscription
             case self::COMM_VAR_MS_REMAINING_DAYS:
                 if ($subscription) {
                     $days = $subscription->get_remaining_period();
                     $var_value = sprintf(__('%s day%s', MS_TEXT_DOMAIN), $days, abs($days) > 1 ? 's' : '');
                 }
                 break;
                 // Needs: $subscription
             // Needs: $subscription
             case self::COMM_VAR_MS_REMAINING_TRIAL_DAYS:
                 if ($subscription) {
                     $days = $subscription->get_remaining_trial_period();
                     $var_value = sprintf(__('%s day%s', MS_TEXT_DOMAIN), $days, abs($days) > 1 ? 's' : '');
                 }
                 break;
                 // Needs: $subscription
             // Needs: $subscription
             case self::COMM_VAR_MS_EXPIRY_DATE:
                 if ($subscription) {
                     $var_value = $subscription->expire_date;
                 }
                 break;
         }
         $comm_vars[$key] = apply_filters('ms_model_communication_send_message_comm_var-' . $key, $var_value, $this->type, $member, $subscription, $invoice);
     }
     return apply_filters('ms_model_communication_get_comm_vars', $comm_vars, $member);
 }
    public function to_html()
    {
        global $post;
        /**
         * Provide a customized account page.
         *
         * @since  1.0.0
         */
        $html = apply_filters('ms_shortcode_custom_account', '', $this->data);
        if (!empty($html)) {
            return $html;
        } else {
            $html = '';
        }
        $member = MS_Model_Member::get_current_member();
        $fields = $this->prepare_fields();
        // Extract shortcode options.
        extract($this->data);
        ob_start();
        ?>
		<div class="ms-account-wrapper">
			<?php 
        if (MS_Model_Member::is_logged_in()) {
            ?>

				<?php 
            // ================================================= MEMBERSHIPS
            if ($show_membership) {
                ?>
				<div id="account-membership">
				<h2>
					<?php 
                echo $membership_title;
                if ($show_membership_change) {
                    $signup_url = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REGISTER);
                    printf('<a href="%s" class="ms-edit-profile">%s</a>', $signup_url, $membership_change_label);
                }
                ?>
				</h2>
				<?php 
                /**
                 * Add custom content right before the memberships list.
                 *
                 * @since  1.0.0
                 */
                do_action('ms_view_account_memberships_top', $member, $this);
                if (MS_Model_Member::is_admin_user()) {
                    _e('You are an admin user and have access to all memberships', 'membership2');
                } else {
                    if (!empty($this->data['subscription'])) {
                        ?>
						<table>
							<tr>
								<th class="ms-col-membership"><?php 
                        _e('Membership name', 'membership2');
                        ?>
</th>
								<th class="ms-col-status"><?php 
                        _e('Status', 'membership2');
                        ?>
</th>
								<th class="ms-col-expire-date"><?php 
                        _e('Expire date', 'membership2');
                        ?>
</th>
							</tr>
							<?php 
                        $empty = true;
                        // These subscriptions have no expire date
                        $no_expire_list = array(MS_Model_Relationship::STATUS_PENDING, MS_Model_Relationship::STATUS_WAITING, MS_Model_Relationship::STATUS_DEACTIVATED);
                        // These subscriptions display the trial-expire date
                        $trial_expire_list = array(MS_Model_Relationship::STATUS_TRIAL, MS_Model_Relationship::STATUS_TRIAL_EXPIRED);
                        foreach ($this->data['subscription'] as $subscription) {
                            $empty = false;
                            $membership = $subscription->get_membership();
                            $subs_classes = array('ms-subscription-' . $subscription->id, 'ms-status-' . $subscription->status, 'ms-type-' . $membership->type, 'ms-payment-' . $membership->payment_type, 'ms-gateway-' . $subscription->gateway_id, 'ms-membership-' . $subscription->membership_id, $subscription->has_trial() ? 'ms-with-trial' : 'ms-no-trial');
                            ?>
								<tr class="<?php 
                            echo esc_attr(implode(' ', $subs_classes));
                            ?>
">
									<td class="ms-col-membership"><?php 
                            echo esc_html($membership->name);
                            ?>
</td>
									<td class="ms-col-status">
									<?php 
                            if (MS_Model_Relationship::STATUS_PENDING == $subscription->status) {
                                // Display a "Purchase" link when status is Pending
                                $code = sprintf('[%s id="%s" label="%s"]', MS_Helper_Shortcode::SCODE_MS_BUY, $membership->id, __('Pending', 'membership2'));
                                echo do_shortcode($code);
                            } else {
                                echo esc_html($subscription->status_text());
                            }
                            ?>
									</td>
									<td class="ms-col-expire-date"><?php 
                            if (in_array($subscription->status, $no_expire_list)) {
                                echo '&nbsp;';
                            } elseif (in_array($subscription->status, $trial_expire_list)) {
                                echo esc_html(MS_Helper_Period::format_date($subscription->trial_expire_date));
                            } elseif ($subscription->expire_date) {
                                echo esc_html(MS_Helper_Period::format_date($subscription->expire_date));
                            } else {
                                _e('Never', 'membership2');
                            }
                            ?>
</td>
								</tr>
							<?php 
                        }
                        if ($empty) {
                            $cols = 3;
                            if (MS_Model_Addon::is_enabled(MS_Model_Addon::ADDON_TRIAL)) {
                                $cols += 1;
                            }
                            printf('<tr><td colspan="%1$s">%2$s</td></tr>', $cols, __('(No Membership)', 'membership2'));
                        }
                        ?>
						</table>
					<?php 
                    } else {
                        _e('No memberships', 'membership2');
                    }
                }
                /**
                 * Add custom content right after the memberships list.
                 *
                 * @since  1.0.0
                 */
                do_action('ms_view_account_memberships_bottom', $member, $this);
                ?>
				</div>
				<?php 
            }
            // END: if ( $show_membership )
            // =============================================================
            ?>

				<?php 
            // ===================================================== PROFILE
            if ($show_profile) {
                ?>
				<div id="account-profile">
				<h2>
					<?php 
                echo $profile_title;
                if ($show_profile_change) {
                    $edit_url = esc_url_raw(add_query_arg(array('action' => MS_Controller_Frontend::ACTION_EDIT_PROFILE)));
                    printf('<a href="%s" class="ms-edit-profile">%s</a>', $edit_url, $profile_change_label);
                }
                ?>
				</h2>
				<?php 
                /**
                 * Add custom content right before the profile overview.
                 *
                 * @since  1.0.0
                 */
                do_action('ms_view_account_profile_top', $member, $this);
                ?>
				<table>
					<?php 
                foreach ($fields['personal_info'] as $field => $title) {
                    ?>
						<tr>
							<th class="ms-label-title"><?php 
                    echo esc_html($title);
                    ?>
: </th>
							<td class="ms-label-field"><?php 
                    echo esc_html($this->data['member']->{$field});
                    ?>
</td>
						</tr>
					<?php 
                }
                ?>
				</table>
				<?php 
                do_action('ms_view_shortcode_account_card_info', $this->data);
                /**
                 * Add custom content right after the profile overview.
                 *
                 * @since  1.0.0
                 */
                do_action('ms_view_account_profile_bottom', $member, $this);
                ?>
				</div>
				<?php 
            }
            // END: if ( $show_profile )
            // =============================================================
            ?>

				<?php 
            // ==================================================== INVOICES
            if ($show_invoices) {
                ?>
				<div id="account-invoices">
				<h2>
					<?php 
                echo $invoices_title;
                if ($show_all_invoices) {
                    $detail_url = esc_url_raw(add_query_arg(array('action' => MS_Controller_Frontend::ACTION_VIEW_INVOICES)));
                    printf('<a href="%s" class="ms-all-invoices">%s</a>', $detail_url, $invoices_details_label);
                }
                ?>
				</h2>
				<?php 
                /**
                 * Add custom content right before the invoice overview list.
                 *
                 * @since  1.0.0
                 */
                do_action('ms_view_account_invoices_top', $member, $this);
                ?>
				<table>
					<thead>
						<tr>
							<th class="ms-col-invoice-no"><?php 
                _e('Invoice #', 'membership2');
                ?>
</th>
							<th class="ms-col-invoice-status"><?php 
                _e('Status', 'membership2');
                ?>
</th>
							<th class="ms-col-invoice-total"><?php 
                printf('%s (%s)', __('Total', 'membership2'), MS_Plugin::instance()->settings->currency);
                ?>
</th>
							<th class="ms-col-invoice-title"><?php 
                _e('Membership', 'membership2');
                ?>
</th>
							<th class="ms-col-invoice-due"><?php 
                _e('Due date', 'membership2');
                ?>
</th>
						</tr>
					</thead>
					<tbody>
					<?php 
                foreach ($this->data['invoices'] as $invoice) {
                    $inv_membership = MS_Factory::load('MS_Model_Membership', $invoice->membership_id);
                    $inv_classes = array('ms-invoice-' . $invoice->id, 'ms-subscription-' . $invoice->ms_relationship_id, 'ms-invoice-' . $invoice->status, 'ms-gateway-' . $invoice->gateway_id, 'ms-membership-' . $invoice->membership_id, 'ms-type-' . $inv_membership->type, 'ms-payment-' . $inv_membership->payment_type);
                    ?>
						<tr class="<?php 
                    echo esc_attr(implode(' ', $inv_classes));
                    ?>
">
							<td class="ms-col-invoice-no"><?php 
                    printf('<a href="%s">%s</a>', get_permalink($invoice->id), $invoice->get_invoice_number());
                    ?>
</td>
							<td class="ms-col-invoice-status"><?php 
                    echo esc_html($invoice->status_text());
                    ?>
</td>
							<td class="ms-col-invoice-total"><?php 
                    echo esc_html(MS_Helper_Billing::format_price($invoice->total));
                    ?>
</td>
							<td class="ms-col-invoice-title"><?php 
                    echo esc_html($inv_membership->name);
                    ?>
</td>
							<td class="ms-col-invoice-due"><?php 
                    echo esc_html(MS_Helper_Period::format_date($invoice->due_date, __('F j', 'membership2')));
                    ?>
</td>
						</tr>
					<?php 
                }
                ?>
					</tbody>
				</table>
				<?php 
                /**
                 * Add custom content right after the invoices overview list.
                 *
                 * @since  1.0.0
                 */
                do_action('ms_view_account_invoices_bottom', $member, $this);
                ?>
				</div>
				<?php 
            }
            // END: if ( $show_invoices )
            // =============================================================
            ?>

				<?php 
            // ==================================================== ACTIVITY
            if ($show_activity) {
                ?>
				<div id="account-activity">
				<h2>
					<?php 
                echo $activity_title;
                if ($show_all_activities) {
                    $detail_url = esc_url_raw(add_query_arg(array('action' => MS_Controller_Frontend::ACTION_VIEW_ACTIVITIES)));
                    printf('<a href="%s" class="ms-all-activities">%s</a>', $detail_url, $activity_details_label);
                }
                ?>
				</h2>
				<?php 
                /**
                 * Add custom content right before the activities overview list.
                 *
                 * @since  1.0.0
                 */
                do_action('ms_view_account_activity_top', $member, $this);
                ?>
				<table>
					<thead>
						<tr>
							<th class="ms-col-activity-date"><?php 
                _e('Date', 'membership2');
                ?>
</th>
							<th class="ms-col-activity-title"><?php 
                _e('Activity', 'membership2');
                ?>
</th>
						</tr>
					</thead>
					<tbody>
					<?php 
                foreach ($this->data['events'] as $event) {
                    $ev_classes = array('ms-activity-topic-' . $event->topic, 'ms-activity-type-' . $event->type, 'ms-membership-' . $event->membership_id);
                    ?>
						<tr class="<?php 
                    echo esc_attr(implode(' ', $ev_classes));
                    ?>
">
							<td class="ms-col-activity-date"><?php 
                    echo esc_html(MS_Helper_Period::format_date($event->post_modified));
                    ?>
</td>
							<td class="ms-col-activity-title"><?php 
                    echo esc_html($event->description);
                    ?>
</td>
						</tr>
					<?php 
                }
                ?>
					</tbody>
				</table>
				<?php 
                /**
                 * Add custom content right after the activities overview list.
                 *
                 * @since  1.0.0
                 */
                do_action('ms_view_account_activity_bottom', $member, $this);
                ?>
				</div>
				<?php 
            }
            // END: if ( $show_activity )
            // =============================================================
            ?>

			<?php 
        } else {
            $has_login_form = MS_Helper_Shortcode::has_shortcode(MS_Helper_Shortcode::SCODE_LOGIN, $post->post_content);
            if (!$has_login_form) {
                $redirect = esc_url_raw(add_query_arg(array()));
                $title = __('Your account', 'membership2');
                $scode = sprintf('[%1$s redirect="%2$s" title="%3$s"]', MS_Helper_Shortcode::SCODE_LOGIN, esc_url($redirect), esc_attr($title));
                echo do_shortcode($scode);
            }
        }
        ?>
		</div>
		<?php 
        $html = ob_get_clean();
        $html = apply_filters('ms_compact_code', $html);
        return apply_filters('ms_shortcode_account', $html, $this->data);
    }
 /**
  * Redirect user to account page.
  *
  * Only redirect when no previous redirect_to is set or when going to /wp-admin/.
  *
  * @since  1.0.0
  *
  * @param string $redirect_to URL to redirect to.
  * @param string $request URL the user is coming from.
  * @param object $user Logged user's data.
  * @return string The redirect url.
  */
 public function login_redirect($redirect_to, $request, $user)
 {
     if (!empty($user->ID) && !MS_Model_Member::is_admin_user($user->ID) && (empty($redirect_to) || admin_url() == $redirect_to)) {
         $redirect_to = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_ACCOUNT);
     }
     return apply_filters('ms_controller_frontend_login_redirect', $redirect_to, $request, $user, $this);
 }
    /**
     * Returns the HTML code.
     *
     * @since  1.0.0
     * @return string
     */
    public function to_html()
    {
        $fields = $this->prepare_fields();
        $this->add_action('ms_shortcode_register_form_end', 'add_scripts');
        // When redirecting to login form we want to keep the previously submitted form data.
        $url_data = $_POST;
        $url_data['do-login'] = '******';
        $login_url = esc_url_raw(add_query_arg($url_data));
        if (!empty($_REQUEST['do-login'])) {
            $register_url = esc_url_raw(remove_query_arg('do-login'));
            $back_link = array('url' => $register_url, 'class' => 'alignleft', 'value' => __('Cancel', MS_TEXT_DOMAIN));
            $html = do_shortcode(sprintf('[%s show_note=false title="%s"]', MS_Helper_Shortcode::SCODE_LOGIN, __('Login', MS_TEXT_DOMAIN)));
            $html .= MS_Helper_Html::html_link($back_link, true);
            return $html;
        }
        $login_link = array('title' => __('Login', MS_TEXT_DOMAIN), 'url' => $login_url, 'class' => 'alignleft', 'value' => __('Already have a user account?', MS_TEXT_DOMAIN));
        $register_button = array('id' => 'register', 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT, 'value' => $this->data['label_register']);
        $title = $this->data['title'];
        ob_start();
        $reg_url = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REGISTER);
        $reg_url = esc_url_raw(add_query_arg('action', 'register_user', $reg_url));
        // Default WP action hook
        do_action('before_signup_form');
        ?>
		<div class="ms-membership-form-wrapper">
			<?php 
        $this->render_errors();
        ?>
			<form
				id="ms-shortcode-register-user-form"
				class="form-membership"
				action="<?php 
        echo esc_url($reg_url);
        ?>
"
				method="post">

				<?php 
        wp_nonce_field($this->data['action']);
        ?>
				<?php 
        if (!empty($title)) {
            ?>
					<legend>
						<?php 
            echo $title;
            ?>
					</legend>
				<?php 
        }
        ?>

				<?php 
        foreach ($fields as $field) {
            if (is_string($field)) {
                MS_Helper_Html::html_element($field);
            } elseif (MS_Helper_Html::INPUT_TYPE_HIDDEN == $field['type']) {
                MS_Helper_Html::html_element($field);
            } else {
                ?>
						<div class="ms-form-element ms-form-element-<?php 
                echo esc_attr($field['id']);
                ?>
">
							<?php 
                MS_Helper_Html::html_element($field);
                ?>
						</div>
						<?php 
            }
        }
        echo '<div class="ms-extra-fields">';
        /**
         * Trigger default WordPress action to allow other plugins
         * to add custom fields to the registration form.
         *
         * signup_extra_fields Defined in wp-signup.php which is used
         *              for Multisite signup process.
         *
         * register_form Defined in wp-login.php which is only used for
         *              Single site registration process.
         *
         * @since  1.0.0
         */
        if (is_multisite()) {
            $empty_error = new WP_Error();
            do_action('signup_extra_fields', $empty_error);
        } else {
            do_action('register_form');
            // Always on the register form.
        }
        echo '</div>';
        MS_Helper_Html::html_element($register_button);
        if (is_wp_error($this->error)) {
            /**
             * Display registration errors.
             *
             * @since  1.0.0
             */
            do_action('registration_errors', $this->error);
        }
        /**
         * This hook is intended to output hidden fields or JS code
         * at the end of the form tag.
         *
         * @since  1.0.1.0
         */
        do_action('ms_shortcode_register_form_end', $this);
        ?>
			</form>
			<?php 
        if ($this->data['loginlink']) {
            MS_Helper_Html::html_link($login_link);
        }
        ?>
		</div>
		<?php 
        // Default WP action hook.
        do_action('signup_blogform', array());
        do_action('after_signup_form');
        $html = ob_get_clean();
        $html = apply_filters('ms_compact_code', $html);
        return apply_filters('ms_shortcode_register', $html, $this->data);
    }
 /**
  * Payment redirect URL filter.
  *
  * @param string                  $url
  * @param Pronamic_WP_Pay_Payment $payment
  * @return string
  */
 public static function redirect_url($url, $payment)
 {
     // @see https://github.com/wp-plugins/membership/blob/4.0.0.2/app/model/class-ms-model-pages.php#L492-L530
     if (Pronamic_WP_Pay_Class::method_exists('MS_Model_Pages', 'get_page_url')) {
         // @see https://github.com/wp-plugins/membership/blob/4.0.0.2/app/model/class-ms-model-pages.php#L44-L55
         $url = MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REGISTER);
     } elseif (function_exists('M_get_returnurl_permalink')) {
         // @see https://github.com/wp-plugins/membership/blob/3.4.4.3/membershipincludes/includes/functions.php#L598-L622
         $url = M_get_returnurl_permalink();
     }
     switch ($payment->get_status()) {
         case Pronamic_WP_Pay_Statuses::SUCCESS:
             // @see https://github.com/wp-plugins/membership/blob/4.0.0.2/app/model/class-ms-model-pages.php#L492-L530
             if (Pronamic_WP_Pay_Class::method_exists('MS_Model_Pages', 'get_page_url')) {
                 $invoice_id = get_post_meta($payment->get_id(), '_pronamic_payment_membership_invoice_id', true);
                 $invoice = MS_Factory::load('MS_Model_Invoice', $invoice_id);
                 $subscription = $invoice->get_subscription();
                 // @see https://github.com/wp-plugins/membership/blob/4.0.0.2/app/model/class-ms-model-pages.php#L44-L55
                 $url = add_query_arg('ms_relationship_id', $subscription->id, MS_Model_Pages::get_page_url(MS_Model_Pages::MS_PAGE_REG_COMPLETE));
             } elseif (function_exists('M_get_registrationcompleted_permalink')) {
                 // @see https://github.com/wp-plugins/membership/blob/3.4.4.3/membershipincludes/includes/functions.php#L576-L598
                 $url = M_get_registrationcompleted_permalink();
             }
             break;
     }
     return $url;
 }