-
-
Save tommcfarlin/a9f7572fe389a8262121d41d0a2f076b to your computer and use it in GitHub Desktop.
[WordPress] Ajax in WordPress: A Series on Protocols
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 | |
/** | |
* This function is purely for demonstration purposes of a blog post. There is lack of | |
* sanitization, functions that are called that are not referenced, and areas that could | |
* be refactored. | |
* | |
* The point is to be relatively verbose for the sake of showing how multiple protocol | |
* messages by be returned from a function, namely this one, that is called via Ajax | |
* in WordPress. | |
* | |
* The purpose of the function is to take the data in a CSV file and create users | |
* based on the information in the file. | |
*/ | |
public function process_file() { | |
/* Greater sanitization is needed here, but to keep the example succint, | |
* I'm omitting it. Don't use this in production. | |
* | |
* Note that if no file number is specified in the request, then we return an | |
* an error code following our protocol. | |
*/ | |
$file_number = -1; | |
if ( isset( $_POST['file_number'] ) ) { | |
$file_number = intval( $_POST['file_number'] ); | |
} else { | |
echo 'no_file_number:-1'; | |
wp_die(); | |
} | |
/* Greater sanitization is needed here, but to keep the example succint, | |
* I'm omitting it. Don't use this in production. | |
* | |
* Note that if no file number is specified in the request, then we return an | |
* an error code following our protocol. | |
*/ | |
if ( isset( $_POST['csv_file'] ) ) { | |
$csv_file = $_POST['csv_file']; | |
} else { | |
echo "no_file_specified:-1" | |
wp_die(); | |
} | |
// Otherwise, at this point, we're good to go so we'll continue. | |
/* Return the number of users. Assume they have been written to the database | |
* in the function call below. The function returns a collection of WP_User | |
* objects. | |
*/ | |
$users = $this->create_wp_users_from_file( $csv_file ); | |
// Return a protocol-specific response. | |
$total_users = count( $users ); | |
echo "completed:$total_users"; | |
wp_die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment