RpSequel : rfc sql crud with wordpress

I set out to use MsAccess with xhr/ajax to maintain tables I added to my wordpress database, from my desktop. (Because I suck at html forms backends and consider them a waste of time.)

I used a similar technique ten years ago, setting up msaccess as reporting tool for SAP R/3 with RFC dll’s and ActiveX. That remained stable for eight years without maintenance. Hey, I might get lucky with WordPress xml-rpc and xhr/ajax.

I called the example RpSequel.

adding sql rpc functions

To sync the data sets, I will duplicate a list with sequel operations from my desktop database as rfc call to my blog’s xmlrpc-endpoint. To handle that list, I plug a sql crud method into the xml-rpc method array in WordPress :

  1. add_filter( 'xmlrpc_methods', 'rpsequel_methods' );
  2.  
  3. function rpsequel_methods( $methods ) {
  4.     $methods['rpsequel.rpsequelInsert'] = 'rpsequelInsert';
  5.     return $methods;
  6. }
  7.  
  8. function rpsequelInsert($args) {
  9. }

The basic INSERT method itself can be simple:

  1. function rpsequelInsert($args) {
  2.         global $wpdb;
  3.  
  4. //the first parameters
  5.         $blog_id = (int) $args[0];
  6.         $username = $args[1];
  7.         $password = $args[2];
  8.         $tablename      = $args[3];
  9.  
  10. //get the structs
  11.         $structs        = $args[4];
  12.  
  13. //pick the first
  14.         $struct = $structs[0];
  15.  
  16. //compose the mysql insert statement    
  17.         foreach($struct as $key => $value){
  18.             $SqlFields .= " `".$key . "`,";
  19.             $SqlValues .= " '".$value . "',";
  20.         }
  21.  
  22.         $SqlFields=substr($SqlFields, 0, strlen($SqlFields)-1);
  23.         $SqlValues=substr($SqlValues, 0, strlen($SqlValues)-1);
  24.    
  25.         $SqlStatement .= "INSERT INTO ".$wpdb->prefix.$tablename. " (". $SqlFields .  ") VALUES (".$SqlValues . ")";
  26.        
  27. //execute the query        
  28.         $wpdb->query($wpdb->prepare($SqlStatement));
  29.  
  30. //return the record id
  31.        return $wpdb->insert_id;
  32. }

That in itself is not very exciting, it stuffs records in the host’s database. Which is fine, however, I want the blog to respond to individual operations.

I can write rpc-functions for every single function I want the blog to perform, that means truckloads of rpc-functions, on both client and server end. I am incredibly lazy, so I ain’t gonna go there.

adding hooks to sql rpc functions

Lucky for me, WordPress has hooks, hooks are cool.

Adding action hooks (before_insert and after_insert) to the crud method makes it more powerful. Two hooks are enough to separate the business logic of the desktop database from the blogs reporting logic.

  1. function rpsequelInsert($args) {
  2.         global $wpdb;
  3.  
  4. //the first parameters
  5.         $blog_id = (int) $args[0];
  6.         $username = $args[1];
  7.         $password = $args[2];
  8.         $tablename      = $args[3];
  9.  
  10. //get the structs
  11.         $structs        = $args[4];
  12.  
  13. //here is the first action hook,
  14. //it hands the tablename and the records over to any
  15. //function that 'listens' on the action hook
  16.  
  17. do_action('rpsequel_before_insert',  $tablename, $structs);
  18.  
  19. //the rest of the rather dull method
  20.  
  21.         $struct = $structs[0];
  22. //….
  23. //execute the query        
  24.         $wpdb->query($wpdb->prepare($SqlStatement));
  25.  
  26. //here is the second action hook,
  27. //it hands the table name with the new record id
  28. //to any function that 'listens' on the action hook
  29.  
  30. do_action('rpsequel_after_insert',  $tablename, $wpdb->insert_id);
  31.  
  32. //return the record id
  33.        return $wpdb->insert_id;
  34. }

Now it is more exciting.

  • I can send a list of records and a rfc insert-method to the blogs xmlrpc endpoint
  • the rfc crud-plugin can process the records one by one
  • Before and after each insert operation, the method triggers an action.
  • Before inserting, it exposes the record data.
  • After inserting, it exposes the record id.

And the last two, was exactly what I wanted.

adding functionality with plugins

Now I can add tiny plugins, that ‘listen’ on the action hooks in the rfc-methods. If there is an INSERT into the database, my plugins read which table it affects. They can perform actions, either before the insert, with the new record data, or after the insert, with the new record id.

  1. add_action ( 'rpsequel_before_insert', 'before_insert_logic', 10, 2);
  2. add_action ( 'rpsequel_after_insert', 'after_insert_logic', 10, 2);
  3.  
  4. function before_insert_logic($rpc_tablename, $rpc_array) {
  5.    if($rpc_tablename=="ships") {
  6. //do some stuff before inserting incoming records
  7.    }
  8. }
  9.  
  10. function after_insert_logic($rpc_tablename, $rpc_insert_id) {
  11.    if($rpc_tablename=="ships") {
  12. //do some stuff after inserting incoming records
  13.    }
  14. }

That’s basically all it takes. As technique, it has it’s limitations, but it can come in handy sometimes.

Posted in php, wordpress, xml-rpc and tagged .

Leave a Reply

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