php [约会] - 从BuddyPress添加提供商

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [约会] - 从BuddyPress添加提供商相关的知识,希望对你有一定的参考价值。

<?php
/**
* Plugin Name: [Appointments] - Add provider from BuddyPress
* Plugin URI: https://premium.wpmudev.org/
* Description: Allows to add a provider from BuddyPress
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/

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

if ( ! class_exists( 'WPMUDEV_App_BP_Provider' ) ) {

    class WPMUDEV_App_BP_Provider {

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

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

			return self::$_instance;
		}

		private function __construct() {

			add_action( 'app_after_bp_my_appointments', array( &$this, 'provider_field' ) );
			add_action( 'wp_ajax_wpmudev_app_bp_add_provider', array( &$this, 'add_provider_ajax' ) );
			add_action( 'wp_ajax_nopriv_wpmudev_app_bp_add_provider', array( &$this, 'add_provider_ajax' ) );

		}

		public function add_provider_ajax(){
			
			check_ajax_referer( 'app_add_provider_front', 'nonce' );

			if ( ! isset( $_POST['user_id'] ) ) {
				$return = array(
				    'result'  => '_FAIL_',
				    'message' => 'No user has been set'
				);

				wp_send_json($return);
			}

			if ( ! isset( $_POST['app_services_list'] ) || ! is_array( $_POST['app_services_list'] ) || empty( $_POST['app_services_list'] ) ) {
				$return = array(
				    'result'  => '_FAIL_',
				    'message' => 'No services selected'
				);

				wp_send_json($return);
			}
			
			$user_id = filter_input( INPUT_POST, 'user_id', FILTER_VALIDATE_INT );

			$args = array(
				'ID'				=> $user_id,
				'price'				=> '',
				'services_provided'	=> $_POST['app_services_list'],
				'page'				=> '',
				'dummy'				=> '',
			);
			$worker_id = appointments_insert_worker( $args );

			if ( $worker_id ) {
				$return = array(
				    'result'  => '_SUCCESS_',
				    'message' => 'You are now a rpovider'
				);

				wp_send_json($return);
			}

			$return = array(
			    'result'  => '_FAIL_',
			    'message' => 'Something went wrong'
			);

		}

		public function provider_field( $user_id ) {

			if ( appointments_is_worker( $user_id ) ) {
				return;
			}

			$title 				= __('Make me a provider');
			$services_title 	= __( 'Choose a service: ' );
			$button_title 		= __( 'Go' );
			$services 			= self::list_services( 'checkbox' );			

			$out = "<div class=\"add-new-provider-wrap\">";
				$out = "<form class=\"add-new-provider-form\" id=\"add-new-provider-form\">";
					$out .= "<h3>{$title}</h3>";
					$out .= "<h4>{$services_title}</h4>";
					$out .= $services;
					$out .= "<input type=\"submit\" name=\"add-provider-btn\" id=\"add-provider-btn\" value=\"Go\" />";
					$out .= "<input type=\"hidden\" name=\"action\" id=\"action\" value=\"wpmudev_app_bp_add_provider\" />";
					$out .= "<input type=\"hidden\" name=\"user_id\" id=\"{user_id}\" value=\"{$user_id}\" />";
					$out .= wp_nonce_field( 'app_add_provider_front', 'nonce' );
				$out .= "</form>";
			$out .= "</div>";

			$out .= $this->js();

			echo $out;
		}

		private function js(){
			ob_start();
			?>
			<script type="text/javascript">
				(function(d,$){
					$(d).ready(function(){
						const app_from = $('#add-new-provider-form');

						app_from.on('submit',function(e){
							e.preventDefault();

							let ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>',
								submit = $('#add-provider-btn'),
								orig_btn_txt = submit.val();

							submit.val( 'Please wait ...' ).prop( "disabled", true );

							$.ajax({
								    type: "POST",
								    url: ajaxurl,						    
								    data: app_from.serialize(),
								    dataType: "json",
								    success: function(resp){

								    	if( resp.result == "_SUCCESS_" ) {
								    		notice = $('<div />',{
								    			'class': 'updated notice is-dismissible',
								    			text : '<p>' + resp.message + '</p>'
								    		});

								    		location.reload();
								    	}
								    	else{
								    		notice = $('<div />',{
								    			'class': 'error notice is-dismissible',
								    			'html' : '<p>' + resp.message + '</p>'
								    		});
								    	}

								    	app_from.append(notice);
								    	setTimeout(function(){
										  notice.hide(300, function(){$(this).remove()} );
										}, 5000);
										submit.val(orig_btn_txt).prop( "disabled", false );
								    	
								    },
								    failure: function(errMsg) {
								        console.log(errMsg);
								    }
								});

						});

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

		private static function list_services( $type = 'checkbox', $name = 'app_services_list' ){

			$services = appointments_get_services();
			$services_list = '';

			switch( $type ) {

				case 'checkbox' :
					foreach ( $services as $key => $service ) {
						$services_list .= "<label>";
						$services_list .= "<input type=\"{$type}\" id=\"{$name}\" name=\"{$name}[]\" value=\"{$service->ID}\" /> {$service->name}";
						$services_list .= "</label>";
					}
					break;

			}

			return $services_list;
		}

	}

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

}

以上是关于php [约会] - 从BuddyPress添加提供商的主要内容,如果未能解决你的问题,请参考以下文章

php 添加TOS / PP复选框到BuddyPress注册

php 从BuddyPress组中删除用户,其中用户是特定m2p成员资格的成员或没有成员资格

PHP 在Wordpress MU和Buddypress上的地图上显示用户

php 列出BuddyPress的'情境'的电子邮件自定义帖子类型条款

检查Buddypress是否启用

WordPress/BuddyPress添加用户类型