jimboobrien
7/24/2017 - 11:44 PM

Custom RESTful API for WCATL 2016

Custom RESTful API for WCATL 2016

<?php
	
function wcatl2016_api() {
    if ( !empty( $_GET['wcatl2016'] ) ) {
        switch( strtolower( $_GET['wcatl2016'] ) ) {
	        case 'get-users':
				$response = wcatl2016_api_get_users();
        	break;
	        case 'add-user':
				$response = wcatl2016_api_create_user();
			break;
			default:
				$response = array(
					'http_code' => 502,
					'body' 		=> __( 'Unrecognized Request Sent', 'unipress-api' ),
				);
			break;
        }
        wcatl2016_api_response( $response );
    }
}
add_action( 'init', 'wcatl2016_api' );

function wcatl2016_api_response( $response ) {
    header( 'HTTP/1.1 ' . $response['http_code'] . ' ' . wcatl2016_api_http_code_string( $response['http_code'] ) );
    header( 'Content-type: application/json' );

    // this should be templatized in a real-world solution
    echo json_encode( $response['body'] );
	exit;
}

// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
function wcatl2016_api_http_code_string( $http_code ) {
	switch( $http_code ) {
		case '200':
			return __( 'Success', 'wcatl2016-api' );
		case '201':
			return __( 'Created', 'wcatl2016-api' );
		case '204':
			return __( 'No Content', 'wcatl2016-api' );
		case '400':
			return __( 'Bad Request', 'wcatl2016-api' );
		case '417':
			return __( '417 Expectation Failed', 'wcatl2016-api' );
		case '502':
			return __( 'Bad Gateway', 'wcatl2016-api' );
		default:
			return __( 'Unknown', 'wcatl2016-api' );
	}
}

function wcatl2016_api_get_users() {
	$users_array = get_users();
	$response = array(
		'http_code' => 200,
		'body' 		=> $users_array,
	);
	return $response;
}

function wcatl2016_api_create_user() {
	try {
		$input = file_get_contents( 'php://input' );
		$post = json_decode( $input, TRUE );
		
		if ( empty( $post['username'] ) ) {
			throw new Exception( __( 'Missing Username.', 'wcatl2016-api' ), 400 );
		} else {
			$username = trim( $post['username'] );
		}
		
		if ( empty( $post['password1'] ) ) {
			throw new Exception( __( 'Missing Password.', 'wcatl2016-api' ), 400 );
		} else if ( empty( $post['password2'] ) ) {
			throw new Exception( __( 'Missing Password.', 'wcatl2016-api' ), 400 );
		} else if ( $post['password1'] !== $post['password2'] ) {
			throw new Exception( __( 'Passwords Do Not Match.', 'wcatl2016-api' ), 400 );
		} else {
			$password = $post['password1'];
		}
		
		if ( empty( $post['email'] ) ) {
			throw new Exception( __( 'Missing Email.', 'wcatl2016-api' ), 400 );
		} else if ( !is_email( $post['email'] ) ) {
			throw new Exception( __( 'Invalid Email.', 'wcatl2016-api' ), 400 );
		} else {
			$email = $post['email'];
		}
		
		//Create User
		if ( get_user_by( 'login', $username ) ) {
			throw new Exception( __( 'Username Taken.', 'wcatl2016-api' ), 400 );
		}
		
		if ( get_user_by( 'email', $email ) ) {
			throw new Exception( __( 'Email already used.', 'wcatl2016-api' ), 400 );
		} 
		
        $userdata = array(
			'user_login'		=> $username,
			'user_pass'	 		=> $password,
			'user_email'		=> $email,
			'user_registered'	=> date_i18n( 'Y-m-d H:i:s' ),
		);
		$user_id = wp_insert_user( $userdata );
		if ( !empty( $user_id ) ) {
			$response = array(
				'http_code' => 200,
				'body' 		=> __( 'User Created', 'wcatl2016-api' ),
			);
		} else {
			$response = array(
				'http_code' => 417,
				'body' 		=> __( 'Unable to create user.', 'wcatl2016-api' ),
			);
		}
		
		return $response;
	}
	catch ( Exception $e ) {
		$response = array(
			'http_code' => $e->getCode(),
			'body' 		=> $e->getMessage(),
		);
		return $response;
	}
}