php [Membership 2 Pro] - 向尚未支付订阅费用的会员添加付款通知

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [Membership 2 Pro] - 向尚未支付订阅费用的会员添加付款通知相关的知识,希望对你有一定的参考价值。

<?php
/*
Plugin Name: Membership - Add payment notices
Plugin URI: https://premium.wpmudev.org/
Description: Adds payment notices to members that have not paid
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'WPMUDEV_MS_Payment_Notices' ) ) {

	class WPMUDEV_MS_Payment_Notices {

		private static $_instance = null;

		public static function get_instance() {

			if( is_null( self::$_instance ) ){
				self::$_instance = new WPMUDEV_MS_Payment_Notices();
			}

			return self::$_instance;
		}


		private function __construct() {

			add_action( 'admin_notices', array( $this, 'show_admin_notice' ), 10 );			
			add_filter( 'ms_model_invoice_changed', array( $this, 'update_memberships_in_debt' ), 10, 1 );

		}


		public function show_admin_notice( $user_id = null ){

			if( is_null( $user_id ) || ! is_numeric( $user_id ) ){
				if( ! is_user_logged_in() ){
					return false;
				}
				else{
					$user_id = get_current_user_id();
				}
			}

			if( $this->show_notice_to_member() ){

				$memberships_with_debt = get_user_meta( $user_id, 'ms2_unpaid_memberships_ids', true );
				$payment_links = '';

				if( is_array( $memberships_with_debt ) && ! empty( $memberships_with_debt ) ){
					foreach( $memberships_with_debt as $membership_id ){

						$membership = MS_Factory::load( 'MS_Model_Membership', intval( $membership_id ) );

						$url = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_MEMBERSHIPS );
						$url = esc_url_raw(
							add_query_arg(
								'membership_id',
								$membership_id,
								$url
							)
						);

						$payment_links .= '<div><a href="' . $url . '"><strong>' . $membership->name . '</strong> '. MS_Helper_Billing::format_price( $membership->price ) .'</a></div>';
					}

				}

				echo '
				<div class="notice notice-warning is-dismissible">
					<p>
						<div>There are some pending subscriptions:</div>
						'. $payment_links .'
					</p>
				</div>';

			}

		}


		public function show_notice_to_member( $user_id = null ){

			if( is_null( $user_id ) ){
				if( ! is_user_logged_in() ){
					return false;
				}
				else{
					$user_id = get_current_user_id();
				}
			}

			$user = get_user_by( 'id', $user_id );

			//Don't show notice to administrators
			if( $user->has_cap( 'manage_options' ) ){
				return false;
			}

			$show_notice = get_user_meta( $user_id, 'ms2_unpaid_memberships_ids', true );

			if( empty( $show_notice ) || ! $show_notice ){
				$show_notice = $this->check_member_debts();
			}
			else{
				$show_notice = true;
			}

			return $show_notice;

		}


		public function check_member_debts( $user_id = null ){

			if( is_null( $user_id ) ){
				if( ! is_user_logged_in() ){
					return false;
				}
				else{
					$user_id = get_current_user_id();
				}
			}

			$global_option = ! is_multisite();
			$member = MS_Factory::load( 'MS_Model_Member', $user_id );
			$has_unpaid_invoices = false;
			$memberships_with_debt = array();


			$subscriptions = MS_Model_Relationship::get_subscriptions(
				array(
					'user_id' => $user_id,
					'status' => 'all',
				)
			);

			foreach( $subscriptions as $subscription ){

				$gateway = $subscription->get_gateway();

				if( $gateway->id == 'admin' ){

					$membership = $subscription->get_membership();
					if( $membership->is_free() ){
						continue;
					}

					//Create an unpaid invoice with Manual payment gateway
					$subscription->gateway_id = 'manual';
					$subscription->status = MS_Model_Invoice::STATUS_PENDING;
					$subscription->save();

					MS_Model_Invoice::create_invoice( $subscription );

				}

				$invoices = $subscription->get_invoices();

				foreach( $invoices as $invoice ){

					if( ! $invoice->is_paid() ){
						$has_unpaid_invoices = true;

						$membership = $invoice->get_membership();
						$memberships_with_debt[] = $membership->id;

					}							

				}


			}

			if( $has_unpaid_invoices && ! empty( $memberships_with_debt ) ){
				update_user_option( $user_id, 'ms2_unpaid_memberships_ids', $memberships_with_debt, $global_option );
			}
			else{
				update_user_option( $user_id, 'ms2_unpaid_memberships_ids', false, $global_option );
			}

			return $has_unpaid_invoices;

		}

		public function update_memberships_in_debt( $invoice ){
			if( $invoice->is_paid() ){
				$this->check_member_debts( $invoice->user_id );
			}

		}


	}



	add_action( 'plugins_loaded', function(){
		$GLOBALS['WPMUDEV_MS_Invoice_Gateways'] = WPMUDEV_MS_Payment_Notices::get_instance();
	}, 10 );

}

以上是关于php [Membership 2 Pro] - 向尚未支付订阅费用的会员添加付款通知的主要内容,如果未能解决你的问题,请参考以下文章

php [Membership 2 Pro]隐藏存档列表中的摘录

php [Membership 2 Pro] - 向尚未支付订阅费用的会员添加付款通知

php [Membership 2 Pro] - 强制管理员对新订阅的待处理状态

php Cron的工作是将wordpress用户添加到特定的Membership 2 Pro会员/订阅中

php integracióndewoocommerce con pay membership pro #wordpress #woocommerce

php 修复Membership2与Impreza主题的冲突