bradtrivers

RAMP Add & Change Data

Apr 20th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.92 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: RAMP Add & Change Data
  4. Description: Adds and changes additional data not included by default by RAMP (e.g. image URL updating)
  5. Version: 0.01
  6. Author: Brad Trivers
  7. Author URI: http://sunriseweb.ca
  8. */
  9.  
  10. /**
  11.  * @author Brad Trivers <[email protected]>
  12.  * based on code from http://crowdfavorite.com/wordpress/ramp/docs/developer/extend/
  13.  */
  14.  
  15. //Check if RAMP cfd_register_deploy_callback function exists
  16. // if( function_exists('cfd_register_deploy_callback') ) {
  17.   function rafd_cfd_register_callback() {
  18.         cfd_register_deploy_callback('ramp-addfixdata-id', 'Adds and changes additional data not included by default by RAMP (e.g. image URL updating)', array(
  19.             // batch send
  20.             'send_callback' => 'rafd_send_callback',
  21.             'receive_callback' => 'rafd_receive_callback',
  22.             // batch preflight
  23.             'preflight_send_callback' => 'rafd_preflight_send_callback',
  24.             'preflight_check_callback' => 'rafd_preflight_check_callback',
  25.             'preflight_display_callback' => 'rafd_preflight_display_callback'
  26.         ));
  27.     }
  28.     add_action('admin_init', 'rafd_cfd_register_callback');
  29.  
  30.     // Batch Send
  31.  
  32.         /**
  33.          * Inspect the Batch data and return back data to processed at the end of import
  34.          *
  35.          * @param array $batch_data
  36.          * @return mixed
  37.          */
  38.         function rafd_send_callback($batch_data) {
  39. //          $ret = array();
  40.  
  41.             // inspect $batch_data and build a data set to be sent after all items have been imported
  42.             // data will be delivered to the corresponding `receive_callback` method
  43.      
  44.       $ret = replaceURLs_RAMP($batch_data);
  45.      
  46.             return $ret;
  47.         }
  48.  
  49.         /**
  50.          * Process data sent by the corresponding `send_callback` method
  51.          *
  52.          * @param mixed $callback_generated_data
  53.          * @return array
  54.          */
  55.         function rafd_receive_callback($callback_generated_data) {
  56.             // perform operations on the received data
  57.             // $callback_generated_data is the raw data generated by the corresponding `send_callback` method
  58.             // Update post http://homewood.org/accessing-help
  59.       $my_post = array();
  60.       $my_post['ID'] = $callback_generated_data['post_types']['page']['http://homewood.org/accessing-help']['post']['ID'];
  61.       $my_post['post_content'] = $callback_generated_data['post_types']['page']['http://homewood.org/accessing-help']['post']['post_content'];
  62.       $updatePostResult = wp_update_post( $my_post );
  63.            
  64.             if(count($updatePostResult) == 0) {
  65.         $success = false;
  66.         $message = 'Post update failed.';
  67.       } else {
  68.         $success = true;
  69.         $message = 'Post update successful.  URLs replaced.';
  70.       }
  71.            
  72.             // REQUIRED: must return a message
  73.             return array(
  74.                 'success' => $success, // boolean, whether the callback succeeded or not
  75.                 'message' => $message  // a message to go along with the appropriate $success setting
  76.             );
  77.         }
  78.  
  79.     // Batch Preflight
  80.  
  81.         /**
  82.          * Inspect the Batch data and return back data to be preflighted
  83.          *
  84.          * @param string $batch_data
  85.          * @return void
  86.          */
  87.         function rafd_preflight_send_callback($batch_data) {
  88. //          $ret = array();
  89.  
  90.             // inspect $batch_data and build information to be preflighted on the other end
  91.             // data will be delivered to the corresponding `rafd_preflight_check_callback` method
  92.       $ret = replaceURLs_RAMP($batch_data);
  93.      
  94.             foreach($batch_data as $name => $value) {
  95.         echo "<h4>$name</h4>";
  96.       }
  97.      
  98.       //Replace current server URL with remote server URL wherever it may exists in element values
  99.       //NOTE: array indices containing the URL are not updated
  100. //       $cfd_settings_array = get_option('cfd_settings');  
  101. //       $remote_server = $cfd_settings_array['remote_server'][0]['address']; //Get remote server URL from RAMP settings  
  102. //       $this_server = get_option('siteurl'); //Get this server URL from WordPress options
  103.      
  104.       //Step through $batch_data and replace all instances of this server URL
  105. //       $batch_data = array_replace_string($batch_data, $this_server, $remote_server);
  106. //       print_r($batch_data['post_types']['page']['http://homewood.org/accessing-help']);
  107.  
  108. //       $ret = $batch_data;
  109.             return $ret;
  110.         }
  111.  
  112.         /**
  113.          * Return back any notice or error messages to the sending server
  114.          * Returning errors will not allow the batch to be deployed
  115.          * Returning notices will simply give informational data to the user
  116.          *
  117.          * @param string $callback_generated_data
  118.          * @param string $batch_data
  119.          * @return void
  120.          */
  121.         function rafd_preflight_check_callback($callback_generated_data, $batch_data) {
  122.             $messages = array();
  123.  
  124.             // inspect data and construct messages to be displayed in the preflight screen
  125.             // it is not required to return any messages, in which case return an empty array
  126.      
  127.       //Update the post with the modified post_content
  128.       // Update post http://homewood.org/accessing-help
  129.       $my_post = array();
  130.       $my_post['ID'] = $callback_generated_data['post_types']['page']['http://homewood.org/accessing-help']['post']['ID'];
  131.       $my_post['post_content'] = $callback_generated_data['post_types']['page']['http://homewood.org/accessing-help']['post']['post_content'];
  132.       wp_update_post( $my_post );
  133.  
  134.             // To set an error: $messages['error'] = 'foo!';
  135.             // To set a notice: $messages['notice'] = 'bar!';
  136.            
  137. //          $messages['error'] .= 'rafd - returning error';
  138.       $messages['notice'] .= 'All instances of current URL updated to remote URL';
  139. //       cfd_tmp_dbg('test', $batch_data, 'print', false);
  140.             return $messages;
  141.         }
  142.  
  143.         /**
  144.          * Manipulate the returned messages from the preflight process
  145.          *
  146.          * @param array $batch_preflight_data
  147.          * @return array
  148.          */
  149.         function rafd_preflight_display_callback($batch_preflight_data) {
  150.             if (!empty($batch_preflight_data['extras']['ramp-addfixdata-id'])) {
  151.                 // modify the preflight data if desired
  152.                 // ie: if a message needs to be attached to another item in the batch it can be done now
  153.                 // $batch_preflight_messages is the ENTIRE preflight messages array, modify carefully and return
  154.                 $batch_preflight_messages[] = $messages;
  155.             }
  156.             print_r($batch_preflight_data);
  157.             return $batch_preflight_data;
  158.         }
  159. // } else {
  160. //   echo "<h1>Function cfd_register_deploy_callback does not exist.</h1>";
  161. // }
  162.  
  163. //Recursively steps through an array of arrays, and replaces oldValue with newValue
  164. //in all element values.
  165. //Based on code by topor-grabowski at viversum dot de 05-Apr-2012 09:53 at
  166. //   http://php.net/manual/en/ref.array.php
  167. if(!function_exists('array_replace_string')) {
  168.   function array_replace_string($array = null, $oldValue = null, $newValue = null) {
  169.     //no array given
  170.     if (!isset($array)) {
  171.       return null;
  172.     }
  173.     //process
  174.     $resultList = array();
  175.     foreach ($array as $entryKey => $entryValue) {
  176.       if (is_array($entryValue)) {
  177.         //entry is array and should be handled again recursive
  178.         $resultList[$entryKey] = array_replace_string($entryValue, $oldValue, $newValue);
  179.       } else {
  180.         $resultList[$entryKey] = str_replace($oldValue,$newValue,$entryValue,$count);
  181. //         if($count > 0) {
  182. //           echo "<h1>$entryKey=$count</h1>";
  183. //         }
  184.       }
  185.     }
  186.     return $resultList;
  187.   }
  188. }
  189.  
  190. //Accepts the array of RAMP batch data
  191. //Replaces current server URL with remote server URL wherever it may exists in element values
  192. //NOTE: array indices containing the URL are not updated
  193. function replaceURLs_RAMP($batch_data) {
  194.   $cfd_settings_array = get_option('cfd_settings');  
  195.   $remote_server = $cfd_settings_array['remote_server'][0]['address']; //Get remote server URL from RAMP settings  
  196.   $this_server = get_option('siteurl'); //Get this server URL from WordPress options
  197.  
  198.   //Step through $batch_data and replace all instances of this server URL
  199.   $batch_data = array_replace_string($batch_data, $this_server, $remote_server);
  200. //       print_r($batch_data['post_types']['page']['http://homewood.org/accessing-help']);
  201.   return $batch_data;
  202. }
  203. ?>
Advertisement
Add Comment
Please, Sign In to add comment