Skip to content

Instantly share code, notes, and snippets.

@trewknowledge
Created September 14, 2017 18:26
Show Gist options
  • Save trewknowledge/8c4b3d320a4f9579c4217a4aa1fdb165 to your computer and use it in GitHub Desktop.
Save trewknowledge/8c4b3d320a4f9579c4217a4aa1fdb165 to your computer and use it in GitHub Desktop.
Ajax issue
//Add resource to a users Resources in Gigya
$('body').on('click', '.add-resource', function(event){
event.preventDefault();
var link = $(this);
var postID = link.data('id');
$.ajax({
url: COC_Ajax.ajaxurl,
type: 'post',
data: {
action: 'coc-gigya-add-resource',
uid: coc_gigya_cookie,
postID : postID,
nonce: COC_Ajax.ajax_nonce,
crossDomain: true,
xhrFields: {
withCredentials: true
},
},
success: function(response) {
$(link).addClass('remove-resource').removeClass('add-resource').text('Remove From My Classroom');
}
});
});
/**
* Enqueue scripts and styles
*/
function coc_scripts() {
wp_enqueue_script( 'coc-ajax-requests', get_template_directory_uri() . '/assets/js/coc-ajax-requests.js', array( 'jquery' ), true );
wp_localize_script( 'coc-ajax-requests', 'COC_Ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'editProfileNonce' => wp_create_nonce( 'edit-gigya-profile' ),
'ajax_nonce' => wp_create_nonce( 'coc_gigya_ajax_nonce' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'coc_scripts' );
/**
* Adds a resource to a users Resources.
*
*/
add_action( 'wp_ajax_coc-gigya-add-resource', 'coc_gigya_add_resource' );
add_action( 'wp_ajax_nopriv_coc-gigya-add-resource', 'coc_gigya_add_resource' );
function coc_gigya_add_resource() {
if ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'coc_gigya_ajax_nonce' ) ) {
if ( isset( $_POST['uid'] ) && isset( $_POST['postID'] ) ) {
$uid = sanitize_text_field( wp_unslash( $_POST['uid'] ) );
$post_id = sanitize_text_field( wp_unslash( $_POST['postID'] ) );
//Get the users information
$method = 'accounts.getAccountInfo';
$request = new GSRequest( COC_GIGYA_API_KEY, COC_GIGYA_SECRET_KEY, $method, null );
$request->setParam( 'uid', '1234' );
$response = $request->send();
if ( 0 === $response->getErrorCode() ) {
$valid = SigUtils::validateUserSignature( $response->getString( 'UID','' ), $response->getString( 'signatureTimestamp','' ), COC_GIGYA_SECRET_KEY, $response->getString( 'UIDSignature','' ) );
if ( $valid ) {
$response_data = json_decode( $response->getResponseText() );
if ( ! empty( $response_data ) ) {
if ( ! empty( $response_data->data->resources ) ) {
$current_resources = explode( ',', $response_data->data->resources );
if ( ! in_array( $post_id, $current_resources ) ) {
array_unshift( $current_resources, $post_id );
$updated_resources = implode( ',', $current_resources );
} else {
$updated_resources = $current_resources;
}
} else {
$updated_resources = $post_id;
}
//Set the users information with the new post id
$method = 'accounts.setAccountInfo';
$request = new GSRequest( COC_GIGYA_API_KEY, COC_GIGYA_SECRET_KEY, $method, null );
$request->setParam( 'uid', $uid );
$data = new GSObject();
$data->put( 'resources', $updated_resources );
$request->setParam( 'data', $data );
$response = $request->send();
// Step 4 - handling the request's response.
if ( $response->getErrorCode() === 0 ) {
wp_send_json_success( $response );
} else { // Error
wp_send_json_error( $response->getErrorCode() );
}
}
}
} else {
wp_send_json_error( $response->getErrorCode() );
}
}// End if().
}// End if().
wp_die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment