Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: RAMP Add & Change Data
- Description: Adds and changes additional data not included by default by RAMP (e.g. image URL updating)
- Version: 0.01
- Author: Brad Trivers
- Author URI: http://sunriseweb.ca
- */
- /**
- * @author Brad Trivers <[email protected]>
- * based on code from http://crowdfavorite.com/wordpress/ramp/docs/developer/extend/
- */
- //Check if RAMP cfd_register_deploy_callback function exists
- // if( function_exists('cfd_register_deploy_callback') ) {
- function rafd_cfd_register_callback() {
- cfd_register_deploy_callback('ramp-addfixdata-id', 'Adds and changes additional data not included by default by RAMP (e.g. image URL updating)', array(
- // batch send
- 'send_callback' => 'rafd_send_callback',
- 'receive_callback' => 'rafd_receive_callback',
- // batch preflight
- 'preflight_send_callback' => 'rafd_preflight_send_callback',
- 'preflight_check_callback' => 'rafd_preflight_check_callback',
- 'preflight_display_callback' => 'rafd_preflight_display_callback'
- ));
- }
- add_action('admin_init', 'rafd_cfd_register_callback');
- // Batch Send
- /**
- * Inspect the Batch data and return back data to processed at the end of import
- *
- * @param array $batch_data
- * @return mixed
- */
- function rafd_send_callback($batch_data) {
- // $ret = array();
- // inspect $batch_data and build a data set to be sent after all items have been imported
- // data will be delivered to the corresponding `receive_callback` method
- $ret = replaceURLs_RAMP($batch_data);
- return $ret;
- }
- /**
- * Process data sent by the corresponding `send_callback` method
- *
- * @param mixed $callback_generated_data
- * @return array
- */
- function rafd_receive_callback($callback_generated_data) {
- // perform operations on the received data
- // $callback_generated_data is the raw data generated by the corresponding `send_callback` method
- // Update post http://homewood.org/accessing-help
- $my_post = array();
- $my_post['ID'] = $callback_generated_data['post_types']['page']['http://homewood.org/accessing-help']['post']['ID'];
- $my_post['post_content'] = $callback_generated_data['post_types']['page']['http://homewood.org/accessing-help']['post']['post_content'];
- $updatePostResult = wp_update_post( $my_post );
- if(count($updatePostResult) == 0) {
- $success = false;
- $message = 'Post update failed.';
- } else {
- $success = true;
- $message = 'Post update successful. URLs replaced.';
- }
- // REQUIRED: must return a message
- return array(
- 'success' => $success, // boolean, whether the callback succeeded or not
- 'message' => $message // a message to go along with the appropriate $success setting
- );
- }
- // Batch Preflight
- /**
- * Inspect the Batch data and return back data to be preflighted
- *
- * @param string $batch_data
- * @return void
- */
- function rafd_preflight_send_callback($batch_data) {
- // $ret = array();
- // inspect $batch_data and build information to be preflighted on the other end
- // data will be delivered to the corresponding `rafd_preflight_check_callback` method
- $ret = replaceURLs_RAMP($batch_data);
- foreach($batch_data as $name => $value) {
- echo "<h4>$name</h4>";
- }
- //Replace current server URL with remote server URL wherever it may exists in element values
- //NOTE: array indices containing the URL are not updated
- // $cfd_settings_array = get_option('cfd_settings');
- // $remote_server = $cfd_settings_array['remote_server'][0]['address']; //Get remote server URL from RAMP settings
- // $this_server = get_option('siteurl'); //Get this server URL from WordPress options
- //Step through $batch_data and replace all instances of this server URL
- // $batch_data = array_replace_string($batch_data, $this_server, $remote_server);
- // print_r($batch_data['post_types']['page']['http://homewood.org/accessing-help']);
- // $ret = $batch_data;
- return $ret;
- }
- /**
- * Return back any notice or error messages to the sending server
- * Returning errors will not allow the batch to be deployed
- * Returning notices will simply give informational data to the user
- *
- * @param string $callback_generated_data
- * @param string $batch_data
- * @return void
- */
- function rafd_preflight_check_callback($callback_generated_data, $batch_data) {
- $messages = array();
- // inspect data and construct messages to be displayed in the preflight screen
- // it is not required to return any messages, in which case return an empty array
- //Update the post with the modified post_content
- // Update post http://homewood.org/accessing-help
- $my_post = array();
- $my_post['ID'] = $callback_generated_data['post_types']['page']['http://homewood.org/accessing-help']['post']['ID'];
- $my_post['post_content'] = $callback_generated_data['post_types']['page']['http://homewood.org/accessing-help']['post']['post_content'];
- wp_update_post( $my_post );
- // To set an error: $messages['error'] = 'foo!';
- // To set a notice: $messages['notice'] = 'bar!';
- // $messages['error'] .= 'rafd - returning error';
- $messages['notice'] .= 'All instances of current URL updated to remote URL';
- // cfd_tmp_dbg('test', $batch_data, 'print', false);
- return $messages;
- }
- /**
- * Manipulate the returned messages from the preflight process
- *
- * @param array $batch_preflight_data
- * @return array
- */
- function rafd_preflight_display_callback($batch_preflight_data) {
- if (!empty($batch_preflight_data['extras']['ramp-addfixdata-id'])) {
- // modify the preflight data if desired
- // ie: if a message needs to be attached to another item in the batch it can be done now
- // $batch_preflight_messages is the ENTIRE preflight messages array, modify carefully and return
- $batch_preflight_messages[] = $messages;
- }
- print_r($batch_preflight_data);
- return $batch_preflight_data;
- }
- // } else {
- // echo "<h1>Function cfd_register_deploy_callback does not exist.</h1>";
- // }
- //Recursively steps through an array of arrays, and replaces oldValue with newValue
- //in all element values.
- //Based on code by topor-grabowski at viversum dot de 05-Apr-2012 09:53 at
- // http://php.net/manual/en/ref.array.php
- if(!function_exists('array_replace_string')) {
- function array_replace_string($array = null, $oldValue = null, $newValue = null) {
- //no array given
- if (!isset($array)) {
- return null;
- }
- //process
- $resultList = array();
- foreach ($array as $entryKey => $entryValue) {
- if (is_array($entryValue)) {
- //entry is array and should be handled again recursive
- $resultList[$entryKey] = array_replace_string($entryValue, $oldValue, $newValue);
- } else {
- $resultList[$entryKey] = str_replace($oldValue,$newValue,$entryValue,$count);
- // if($count > 0) {
- // echo "<h1>$entryKey=$count</h1>";
- // }
- }
- }
- return $resultList;
- }
- }
- //Accepts the array of RAMP batch data
- //Replaces current server URL with remote server URL wherever it may exists in element values
- //NOTE: array indices containing the URL are not updated
- function replaceURLs_RAMP($batch_data) {
- $cfd_settings_array = get_option('cfd_settings');
- $remote_server = $cfd_settings_array['remote_server'][0]['address']; //Get remote server URL from RAMP settings
- $this_server = get_option('siteurl'); //Get this server URL from WordPress options
- //Step through $batch_data and replace all instances of this server URL
- $batch_data = array_replace_string($batch_data, $this_server, $remote_server);
- // print_r($batch_data['post_types']['page']['http://homewood.org/accessing-help']);
- return $batch_data;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment