Skip to content

Instantly share code, notes, and snippets.

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 jerclarke/08800924bafee5e948769df2013b7949 to your computer and use it in GitHub Desktop.
Save jerclarke/08800924bafee5e948769df2013b7949 to your computer and use it in GitHub Desktop.
Edit Flow "plugin" (hack) to autosave unsubmitted Editorial Comment text when post is saved
<?php
/**
* EDIT FLOW: wp_insert_post action to autosave "draft" editorial comments in post meta field
*
* Currently (2016-10-06) Edit Flow has no handling of unsubmitted editorial comments in the textarea
* when the post is saved, so saving your post deletes your comment unless you submitted it, which is awful.
*
* This function hooks into wp_insert_post where the value of the textarea is present (but ignored by default)
* and if found, saves the text (and ID of parent comment if it was a specific reply) to a postmeta field
* keyed on the user who saves the post.
*
* Effect is that we can then re-insert the value when the page loads, and give the effect of EF having
* saved the comment draft while the post was being saved.
*
* NOTE: THIS IS HOPEFULLY TEMPORARY WHILE EDIT FLOW ADDS THIS FEATURE TO THE CORE PLUGIN. REMOVE WHEN POSSIBLE.
*
* @see gv_edit_flow_comment_autosave_js() which handles and inserts the postmeta value with jQuery
* @see https://github.com/Automattic/Edit-Flow/issues/379 Feature request for core Edit Flow plugin
* @param integer $post_id
* @param array $post
* @return type
*/
function gv_edit_flow_comment_autosave_wp_insert_post($post_id, $post) {
/**
* 'replycontent' Text entered in the editorial comment textarea
* 'ef-post_id' EF-version of the post ID, seems ideal to avoid collisions as it should match EF form
* 'user_ID' ID of editing user who submitted form
*/
if (empty($_POST['replycontent']) OR empty($_POST['ef-post_id']) OR empty($_POST['user_ID']))
return;
/**
* Array of "draft" comment data to save in postmeta
*/
$saved_editorial_comment = array(
'text' => $_POST['replycontent'],
'parent' => '',
);
/**
* 'comment_parent' is ID of parent editorial comment
*/
if (!empty($_POST['ef-comment_parent']))
$saved_editorial_comment['parent'] = $_POST['ef-comment_parent'];
/**
* Prepare the postmeta key with user ID
*/
$editing_user_id = $_POST['user_ID'];
$postmeta_key = 'ef_ec_draft_' . $editing_user_id;
/**
* Update/add the postmeta (intentionally replaces old value if present, shouldn't happen)
*/
$existing_postmeta = get_post_meta($post_id, $postmeta_key, 'single');
if ($existing_postmeta)
update_post_meta($post_id, $postmeta_key, $saved_editorial_comment, $existing_postmeta);
else
add_post_meta($post_id, $postmeta_key, $saved_editorial_comment, 'unique');
}
add_action('wp_insert_post', 'gv_edit_flow_comment_autosave_wp_insert_post', '', 2);
/**
* EDIT FLOW: Output jQuery on post editor screen to insert previously-saved "draft" editorial comment
*
* Checks post meta for a field containing saved EF editorial comment text and if found, outputs
* jQuery that clicks the correct "reply" button then inserts the text.
*
* Effect is as if the comment text was saved naturally during post save (it's removed by default).
*
* NOTE: THIS IS HOPEFULLY TEMPORARY WHILE EDIT FLOW ADDS THIS FEATURE TO THE CORE PLUGIN. REMOVE WHEN POSSIBLE.
*
* @see gv_edit_flow_comment_autosave_wp_insert_post() Which hooks into wp_insert_post to save postmeta field
* @see https://github.com/Automattic/Edit-Flow/issues/379 Feature request for core Edit Flow plugin
* @global $post
*/
function gv_edit_flow_comment_autosave_js() {
global $post;
/**
* Get currently-editing user ID
*/
$editing_user_id = get_current_user_id();
if (!$editing_user_id OR is_wp_error($editing_user_id))
return;
/**
* Get the comment draft as saved by gv_edit_flow_wp_insert_post()
* Will only exist if editing user had text in the form before saving
*/
$postmeta_key = 'ef_ec_draft_' . $editing_user_id;
$editorial_comment_draft = get_post_meta($post->ID, $postmeta_key, 'single');
/**
* If the comment draft is invalid exit
*/
if (!is_array($editorial_comment_draft) OR !isset($editorial_comment_draft['text'])) :
// If the postmeta existed (it's invalid) delete it
if ($editorial_comment_draft)
delete_post_meta($post->ID, $postmeta_key);
return;
endif;
/**
* Handle click_selector based on parentage
*/
// Default click selector is main "reply" button (no parent)
$click_selector = "#ef-comment_respond";
// If parent ID is present use it's sub-reply button
$parent_id = '';
if (!empty($editorial_comment_draft['parent']))
$parent_id = $editorial_comment_draft['parent'];
if ($parent_id) :
$click_selector = "#comment-$parent_id .reply a";
endif;
/**
* Output jquery to click the appropriate submit button then insert the content
*/
$draft_text_json_encoded = json_encode($editorial_comment_draft['text']);
?>
<script>
jQuery(document).ready(function(){
function gv_ef_insert_comment_draft() {
jQuery("<?php echo $click_selector; ?>").click();
jQuery("#ef-replycontent").val(<?php echo $draft_text_json_encoded; ?>);
}
gv_ef_insert_comment_draft();
});
</script>
<?php
/**
* Delete the postmeta
*/
delete_post_meta($post->ID, $postmeta_key);
}
add_action( 'admin_head-post.php', 'gv_edit_flow_comment_autosave_js');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment