php [会员资格] - 设置手动网关。允许将订阅网关从Admin切换到Manual

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [会员资格] - 设置手动网关。允许将订阅网关从Admin切换到Manual相关的知识,希望对你有一定的参考价值。

<?php
/**
* Plugin Name: [Membership] - Set Manual Gateway
* Plugin URI: https://premium.wpmudev.org/
* Description: Allows to switch subscription gateway from Admin to Manual
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'WPMUDEV_MS_Set_Manual_Gateway' ) ) {

    class WPMUDEV_MS_Set_Manual_Gateway {

        private static $_instance 		= null;
        private $gdpr_settings_args 	= array();
        private $items_titles 			= array()       ;
        
		public static function get_instance() {

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

			return self::$_instance;
		}

		private function __construct() {

			add_action( 'wp_ajax_wpmudev_ms_change_sub_gw', array( $this, 'change_gateway' ), 20 );
			add_action( 'admin_footer', array( $this, 'admin_footer' ), 20 );
			add_filter( 'ms_view_member_editor_fields_edit', array( $this, 'filter_member_fields' ), 20 );

		}

		public function change_gateway() {

			check_ajax_referer( 'wpmudev_ms_change_sub_gw', 'security' );

			$return = array(
		        'success' => false,
		    );

		    $subscription_id = isset( $_POST['subscription_id'] ) ? (int) $_POST['subscription_id'] : null;

		    if( ! is_null( $subscription_id ) ){
		    		
		    	$subscription = MS_Factory::load( 'MS_Model_Relationship', $subscription_id );
		    	$subscription->gateway_id = 'manual';
		    	$subscription->save();
		    	
		    	$return = array(
			        'success' => true,
			    );
		    }

		    wp_send_json($return);

		}

		public function admin_footer() {

			$screen = get_current_screen();

			if( ! isset( $screen->base ) || 'membership-2_page_membership2-add-member' != $screen->base ) {
				return;
			}

			?>
			<script type="text/javascript">
				(function(d,$){
					$(d).ready(function(){
						$('.ms-change-sub-gateway').on('click', function(e){
							let me = $(this),
								subscription_id = me.data('subscription-id'),
								conf = confirm("Are you sure you want to change gateway to Manual?");

							e.preventDefault();

							if ( ! conf ) {
								return;
							}

							let data = {
		                        action: 'wpmudev_ms_change_sub_gw',
		                        security: '<?php echo wp_create_nonce( "wpmudev_ms_change_sub_gw" ); ?>',
		                        subscription_id: subscription_id
		                    };

		                    $.post(ajaxurl, data, function(response) {
		                        
		                        if( response.success ){                            
		                            location.reload();
		                        }
		                        else{
		                            alert( 'Error in request' );
		                        }
		                    });

						});
					});
				})(document,jQuery);
			</script>
			<?php

		}

		public function filter_member_fields( $fields ) {

			foreach ( $fields as $field_key => $field_items ) {

				foreach ( $field_items as $item_key => $field_item ) {

					if ( ! isset( $field_item['value'] ) || ! is_array( $field_item['value'] ) || ! isset( $field_item['value'][0][1] ) ) {
						continue;
					}

					$subscription_id = (int) $field_item['value'][0][1];

					foreach ( $field_item['value'] as $pair_key => $pair_value ) {

						if ( 'Payment Gateway' == $pair_value[0] && 'None (Admin)' == $pair_value[1] ) {

							$set_manual_btn = "<a class=\"ms-change-sub-gateway\" data-subscription-id=\"{$subscription_id}\">Set to manual</a>";
							$pair_value[1] .= " | " . $set_manual_btn;
							$field_item['value'][$pair_key] = $pair_value;
							$fields[$field_key][$item_key] = $field_item;
						}

					}

				}

			}

			return $fields;

		}

	}

	if ( ! function_exists( 'wpmudev_ms_set_manual_gateway' ) ) {

		function wpmudev_ms_set_manual_gateway() {
			return WPMUDEV_MS_Set_Manual_Gateway::get_instance();
		}

		add_action( 'plugins_loaded', 'wpmudev_ms_set_manual_gateway', 10 );

	}

}

以上是关于php [会员资格] - 设置手动网关。允许将订阅网关从Admin切换到Manual的主要内容,如果未能解决你的问题,请参考以下文章

php [会员资格] - 重新发送欢迎电子邮件

php [会员资格] - 添加分析电子商务

php 删除OG会员资格

php 查询WooCommerce会员资格和FacetWP

php 查询WooCommerce会员资格和FacetWP

php 查询WooCommerce会员资格和FacetWP