-
-
Save richardW8k/9d7bd4140055bebf70addd22830eeaad 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
add_action( 'gravityflow_loaded', function () { | |
class RW_Gravity_Flow_Step_Feed_Update_Post extends Gravity_Flow_Step_Feed_Post_Creation { | |
public $_step_type = 'update_apc_post'; | |
/** | |
* Returns the step label. | |
* | |
* @return string | |
*/ | |
public function get_label() { | |
return 'Update Post'; | |
} | |
/** | |
* Returns an array of settings for this step type. | |
* | |
* @return array | |
*/ | |
public function get_settings() { | |
$settings = parent::get_settings(); | |
if ( ! $this->is_supported() ) { | |
return $settings; | |
} | |
/** @var GF_Advanced_Post_Creation $add_on */ | |
$add_on = $this->get_add_on_instance(); | |
$fields = array( | |
array( | |
'name' => 'post_status', | |
'label' => __( 'Post Status', 'gravityflow' ), | |
'type' => 'select', | |
'choices' => array_merge( array( | |
array( | |
'label' => 'Retain existing status', | |
'value' => '' | |
) | |
), $add_on->get_post_statuses_as_choices() ), | |
), | |
array( | |
'name' => 'post_author', | |
'label' => __( 'Post Author', 'gravityflow' ), | |
'type' => 'select', | |
'choices' => array( | |
array( 'label' => 'Use feed setting', 'value' => '' ), | |
array( | |
'label' => 'Select a User field', | |
'choices' => gravity_flow()->get_form_fields_as_choices( $this->get_form(), array( | |
'disable_first_choice' => true, | |
'input_types' => array( 'workflow_user' ), | |
) ) | |
) | |
), | |
), | |
); | |
$settings['fields'] = array_merge( $settings['fields'], $fields ); | |
return $settings; | |
} | |
/** | |
* Processes the given feed for the add-on. | |
* | |
* @param array $feed The add-on feed properties. | |
* | |
* @return bool Is feed processing complete? | |
*/ | |
public function process_feed( $feed ) { | |
$post_id = $this->get_update_post_id( (int) $feed['id'] ); | |
if ( $post_id && $post = get_post( $post_id, ARRAY_A ) ) { | |
add_filter( 'gravityflow_discussion_items_display_toggle', '__return_false', 99 ); | |
$this->update_post( $post, $feed ); | |
remove_filter( 'gravityflow_discussion_items_display_toggle', '__return_false', 99 ); | |
} | |
return true; | |
} | |
/** | |
* Returns the ID of the post previously created by the current feed. | |
* | |
* @param int $feed_id The current feed ID. | |
* | |
* @return int|null | |
*/ | |
public function get_update_post_id( $feed_id ) { | |
/** @var GF_Advanced_Post_Creation $add_on */ | |
$add_on = $this->get_add_on_instance(); | |
$post_ids = gform_get_meta( $this->get_entry_id(), $add_on->get_slug() . '_post_id' ); | |
if ( is_array( $post_ids ) ) { | |
foreach ( $post_ids as $id ) { | |
$post_feed_id = (int) rgar( $id, 'feed_id' ); | |
if ( $post_feed_id === $feed_id ) { | |
$add_on->set_current_media( rgar( $id, 'media', array() ) ); | |
return $id['post_id']; | |
} | |
} | |
} | |
return null; | |
} | |
/** | |
* Updates the supplied post based on the given feed configuration. | |
* | |
* @param array $post The post to be updated. | |
* @param array $feed The feed being processed. | |
*/ | |
public function update_post( $post, $feed ) { | |
$post_id = $post['ID']; | |
$this->log_debug( __METHOD__ . '(): Running for post #' . $post_id ); | |
/** | |
* Allow custom actions to be performed before the post is updated. | |
* | |
* @param array $post The post to be updated. | |
* @param array $feed The feed being processed. | |
* @param RW_Gravity_Flow_Step_Feed_Update_Post $current_step The current step. | |
*/ | |
do_action( 'gravityflow_pre_update_apc_post', $post, $feed, $current_step = $this ); | |
/** @var GF_Advanced_Post_Creation $add_on */ | |
$add_on = $this->get_add_on_instance(); | |
$form = $this->get_form(); | |
$entry = $this->get_entry(); | |
$media_before = $add_on->get_current_media(); | |
$this->maybe_remove_old_media( $media_before ); | |
$post_author = $this->post_author ? rgar( $entry, $this->post_author ) : null; | |
$add_on->set_post_author( $post_author, $feed, $entry ); | |
$post['post_title'] = $add_on->get_post_title( $feed, $entry, $form ); | |
$post = $add_on->set_post_data( $post, $feed, $entry, $form ); | |
if ( $this->post_status ) { | |
$post['post_status'] = $this->post_status; | |
} | |
$result = wp_update_post( $post, true ); | |
if ( is_wp_error( $result ) ) { | |
$this->log_debug( __METHOD__ . '(): Error updating post: ' . $result->get_error_message() ); | |
} else { | |
$this->log_debug( __METHOD__ . '(): Post updated.' ); | |
if ( $meta_fields = rgars( $feed, 'meta/postMetaFields' ) ) { | |
foreach ( $meta_fields as $meta_field ) { | |
$meta_key = 'gf_custom' === $meta_field['key'] ? $meta_field['custom_key'] : $meta_field['key']; | |
delete_post_meta( $post_id, $meta_key ); | |
} | |
} | |
$add_on->maybe_set_post_thumbnail( $post_id, $feed, $entry, $form ); | |
$add_on->maybe_handle_post_media( $post_id, $feed, $entry ); | |
$add_on->maybe_set_post_meta( $post_id, $feed, $entry, $form ); | |
$add_on->maybe_set_post_taxonomies( $post_id, $feed, $entry, $form ); | |
$media_after = $add_on->get_current_media(); | |
if ( $media_after != $media_before ) { | |
$post_ids = gform_get_meta( $entry['id'], $add_on->get_slug() . '_post_id' ); | |
foreach ( $post_ids as &$id ) { | |
if ( $post_id == $id['post_id'] ) { | |
$id['media'] = $media_after; | |
break; | |
} | |
} | |
gform_update_meta( $entry['id'], $add_on->get_slug() . '_post_id', $post_ids ); | |
} | |
/** | |
* Allow custom actions to be performed after the post is updated. | |
* | |
* @param array $post The post which was updated. | |
* @param array $feed The feed which was processed. | |
* @param RW_Gravity_Flow_Step_Feed_Update_Post $current_step The current step. | |
*/ | |
do_action( 'gravityflow_post_update_apc_post', $post, $feed, $current_step = $this ); | |
} | |
} | |
/** | |
* Deletes media created from files which have been deleted from the entry. | |
* | |
* @param array $media An array of files attached to the current post. Defaults to an empty array. | |
*/ | |
public function maybe_remove_old_media( $media ) { | |
if ( empty( $media ) ) { | |
return; | |
} | |
$files = $this->get_current_files(); | |
$dirty = false; | |
foreach ( $media as $file_url => $media_id ) { | |
if ( ! in_array( $file_url, $files ) ) { | |
$this->log_debug( __METHOD__ . '(): deleting: ' . $media_id ); | |
wp_delete_attachment( $media_id ); | |
unset( $media[ $file_url ] ); | |
$dirty = true; | |
} | |
} | |
if ( $dirty ) { | |
/** @var GF_Advanced_Post_Creation $add_on */ | |
$add_on = $this->get_add_on_instance(); | |
$add_on->set_current_media( $media ); | |
} | |
} | |
/** | |
* Returns an array of uploaded file URLs for the current entry. | |
* | |
* @return array | |
*/ | |
public function get_current_files() { | |
$fields = GFAPI::get_fields_by_type( $this->get_form(), array( 'fileupload' ) ); | |
$files = array(); | |
if ( empty( $fields ) ) { | |
return $files; | |
} | |
$entry = $this->get_entry(); | |
foreach ( $fields as $field ) { | |
$value = rgar( $entry, strval( $field->id ) ); | |
if ( ! empty( $value ) ) { | |
if ( $field->multipleFiles ) { | |
$files = array_merge( $files, json_decode( $value, true ) ); | |
} else { | |
$files[] = $value; | |
} | |
} | |
} | |
return $files; | |
} | |
} | |
Gravity_Flow_Steps::register( new RW_Gravity_Flow_Step_Feed_Update_Post() ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment