Created
June 5, 2018 09:13
-
-
Save wpmudev-sls/f616ea86bb9c784f30ffa6c74a0748cf to your computer and use it in GitHub Desktop.
[Appointments] - Add provider from BuddyPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment