Last active
July 18, 2017 17:40
-
-
Save uncatcrea/ae163cc0d2f298d5a36a670b3855ad0c to your computer and use it in GitHub Desktop.
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
/** | |
* WP-AppKit has no webservice dedicated to data update yet (this is one of the things on our roadmap). | |
* For now a workaround is to used the "liveQuery" webservice that was designed to retrieve custom | |
* content from server but can also work to update data. | |
* | |
* The example here shows how to use liveQuery to send data from the app to the server | |
* and handle this data on server side. | |
* | |
* The App.liveQuery() webservice used here is pretty convenient to handle any custom request from app to server. | |
* We haven't found time to write doc about it yet but we have hope it will come soon ;) | |
*/ | |
//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 WPAK that you're doing your own custom query | |
success: function( answer ) { //The meta update 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!', answer.my_result ); | |
} 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 meta update query to the server: | |
App.liveQuery( query_args, options ); | |
} | |
//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']; | |
//Update our meta: | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment