WP-AppKit’s liveQuery webservice allows to make any custom data query from the app to the server.

Here is an example that shows how to use the liveQuery webservice to send data from the app to the server, handle this data on server side and send feedback data back to the app:

On app side:

//In functions.js or custom js module in your app theme:
//Send data to the server using the "liveQuery" webservice:
// "my_data" can for example be a JSON Object comming from a form submission in the app
function sendMyDataToServer( my_data ) {
	
	//Define your custom query, that you will retrieve on server side
	//using the 'wpak_live_query' hook:
	var query_args = {
		my_action: 'my_livequery_action',
		my_data: my_data
	};

	//Define query options:
	var options = {
		auto_interpret_result: false, //This is to tell WP-AppKit that you're doing your own custom query
		success: function( answer ) { //The liveQuery webservice call went ok
			if ( answer.my_result.ok === 1 ) {
				//Everything went ok. Display some success feedback to the user.
				console.log( 'LiveQuery action went well! Here\'s some feedback: ', answer.my_result.my_feedback_data );
			} else {
				//An error occured in the server's liveQuery handler.
				//Display an error feedback to the user.
				console.log( 'LiveQuery action error: ', answer.my_result.error );
			}
		},
		error: function( error ) {
			//This is if the web service ajax call failed (no network)
			//Display an error feedback to the user.
			console.log( 'LiveQuery ajax failed', error );
		}
	};

	//Send our custom query to the server using liveQuery webservice:
	App.liveQuery( query_args, options );
}

On server side:

//Handle on PHP side the liveQuery call made in the app:
//(To put in a php file created in WP-AppKit theme's "php" folder)
add_filter( 'wpak_live_query', 'handle_my_live_query', 10, 2 );
function handle_my_live_query( $service_answer, $query_params ) {
	//$query_params contains what was passed in liveQuery's "query_args"
	
	//Check that the 'my_action' action set on app side is called:
	if ( isset( $query_params['my_action'] ) && $query_params['my_action'] === 'my_livequery_action' ) {
		
		//Prepare your custom answer:
		$result = array( 'ok' => 0, 'error' => '', 'my_feedback_data' => '' );
		
		//Check passed data:
		if ( !empty( $query_params['my_data'] ) ) {
			
			$sent_data = $query_params['my_data'];
			
			//Check sent data, consume it and send feedback to the app:
			if ( some_check_on_my_data( $sent_data )  ) {
				
				//Do what you need with $sent_data...

				//If everything went ok, set webservice answer to ok = 1:
				$result['ok'] = 1;
				
				//You can add any custom feedback data to the $result array sent
				//back to the app:
				$result['my_feedback_data'] = 'Everything went well :)';
				
			} else {
				$result['error'] = 'my-error';
			}
			
		} else {
			$result['error'] = 'no-data';
		}
		
		//Add your result to the web service answer:
		$service_answer['my_result'] = $result;
	}
	
	return $service_answer;
}

This can also be used for example to send data from a form submission on app side, and save this data on server side.

Published by Mathieu on July 24, 2018

Freelance Senior Web Developer, WordPress since 2009, JS/Backbone since 2012, Custom Back-End devs, WebApps and Plugins for clients and the community.

Leave a Reply

Your email address will not be published. Required fields are marked *

Having questions?

FAQ | Tutorials | Documentation

Or

Contact Us