Skip to content

Instantly share code, notes, and snippets.

@uncatcrea
Last active May 12, 2017 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uncatcrea/6e57f81d92e4cf858a7e189ee29aed1b to your computer and use it in GitHub Desktop.
Save uncatcrea/6e57f81d92e4cf858a7e189ee29aed1b to your computer and use it in GitHub Desktop.
/**
* WP-AppKit has no webservice dedicated to data update yet (this is one of the things on our roadmap).
* The now native WP Rest API can be used to achieve that by implementing your own ajax calls to the Rest API
* that you would call manually from your WP-AppKit app.
* But if you want to stay within WP-AppKit possibilities, you can use the WP-AppKit's "liveQuery" web service
* which is designed to retrieve custom content from server but can also work to update data.
*
* The example here shows how to update a post meta from the app, but it can be transposed to update any WordPress data.
* Note: Sent data is not secured here. If you're on https, it's ok. If not, just be aware that the data sent is not encrypted,
* just like when you submit a form on a non https website.
*
* 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 ;)
*/
/**
* First, on app side, here's what could be the function (defined and called in functions.js)
* that sends our meta_data to the server:
*/
//In theme's functions.js:
//Send meta data to the server using the "liveQuery" webservice:
function sendMyPostMetaData( post_id, meta_data ) {
//Define our custom query, that we will retrieve on server side
//using the 'wpak_live_query' hook:
//Query parameters sent to server (can be whatever you want):
var query_args = {
my_action: 'update_my_post_meta',
my_post_id: post_id, //Set your own post id here
my_meta_data: meta_data
};
//Define query options:
var options = {
auto_interpret_result: false, //This is to tell WPAK that we're doing our own custom query
success: function( answer ) { //The meta update webservice call went ok
if ( answer.my_result.ok === 1 ) {
//Meta updated ok:
console.log( 'Meta updated successfully!', answer.my_result.my_feedback_data );
} else {
//Error when updating meta: display error:
console.log( 'Meta update error: ', answer.my_result.error );
}
},
error: function( error ) {
//This is if the web service call failed
}
};
//Send our meta update query to the server:
App.liveQuery( query_args, options );
}
/**
* Then the corresponding PHP code on server side to update post meta data :
* put this in a PHP file in the "php" folder of your theme. If no "php" folder exists, create it.
* The PHP file can be named whatever you like.
*/
//Catch the liveQuery call made in the app, update the post meta and return
//some feedback to the app:
add_filter( 'wpak_live_query', 'update_my_meta', 10, 3 );
function update_my_meta( $service_answer, $query_params ) {
//$query_params contains what was passed in liveQuery's "query_args"
//Check that our 'my_action' action is called:
if ( isset( $query_params['my_action'] ) && $query_params['my_action'] === 'update_my_post_meta' ) {
//Prepare our custom answer:
$result = array( 'ok' => 0, 'error' => '', 'my_feedback_data' => '' );
//Check passed post ID and update the post meta if the corresponding post is OK:
if ( !empty( $query_params['my_post_id'] ) ) {
$post_id = (int)$query_params['my_post_id'];
//Check that the post exists
$my_post = get_post( $post_id );
if ( $my_post && $my_post->post_type === 'post' ) {
//Update our meta:
if ( !empty( $query_params['my_meta_data'] ) ) {
$my_meta_data = $query_params['my_meta_data']; //Probably do some sanitization on my_meta_data here!
update_post_meta( $post_id, 'my_post_meta', $my_meta_data );
//If everything went ok, set webservice answer to ok = 1:
$result['ok'] = 1;
$result['my_feedback_data'] = 'Everything went well :)';
} else {
$result['error'] = 'no-meta-data';
}
} else {
$result['error'] = 'post-not-found';
}
} else {
$result['error'] = 'no-post-id';
}
//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