Guest User

Untitled

a guest
Oct 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. add_action('new_to_pending', 'send_approve_link');
  2. add_action('draft_to_pending', 'send_approve_link');
  3. add_action('auto-draft_to_pending', 'send_approve_link');
  4. add_action('init', 'approve_post');
  5.  
  6. function set_html_content_type() { return 'text/html'; }
  7.  
  8. function send_approve_link( $post ) {
  9. // get author of post
  10. $author = new WP_User($post->post_author);
  11. // create a key for security check and save as meta
  12. $check = md5( $author->post_author . $post->ID );
  13. update_post_meta( $post->ID, 'id', $check);
  14. // set the content for the email
  15. $content = '<p>Please click following link to allow the publishing of your post: "';
  16. $content .= esc_html( get_the_title($post->ID) ) . '"</p>';
  17. // create an url vor verify link with some variables: key, post id, and email
  18. $url = add_query_arg( array('correo_electronico'=>$author->post_author,'approve'=>$post->ID), site_url() );
  19. $content .= '<p><a href="' . $url . '">Click to Confirm</a></p>';
  20. $from = get_bloginfo('nombre');
  21. $from_email = get_option('admin_email');
  22. $headers = 'From: ' . $from . ' <' . $from_email . '>' . "rn";
  23. // sending email in html format
  24. add_filter( 'wp_mail_content_type', 'set_html_content_type' );
  25. wp_mail( $author->post_author, 'Confirm your Post', $content, $headers);
  26. remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
  27. }
  28.  
  29. function approve_post( ) {
  30. if ( isset($_GET['approve']) && isset($_GET['correo_electronico']) ) {
  31. // have we all variable needed?
  32. if ( empty($_GET['approve']) || ! filter_var($_GET['correo_electronico'], FILTER_VALIDATE_EMAIL) ) return;
  33. // get post
  34. $post = get_post($_GET['approve']);
  35. // this post has an author?
  36. if ( ! isset($post->post_author) ) return;
  37. // is the post already published?
  38. if ( $post->post_status == 'publish' ) {
  39. wp_redirect( get_permalink($post->ID) );
  40. exit();
  41. } elseif ( $post->post_status != 'pending' ) { // was the post deleted by admin?
  42. return;
  43. }
  44. // get the author
  45. $author = new WP_User($post->post_author);
  46. // check author variable
  47. if ( ! isset($author->post_author) ) return;
  48. // verify email
  49. if ( $_GET['correo_electronico'] != $author->post_author ) return;
  50. // verify key
  51. $key = get_post_meta( $post->ID, '_approve_key', true);
  52. if ( $key != md5( $author->post_author . $post->ID ) ) return;
  53. // ok, update status
  54. $post_data = array('ID' => $post->ID, 'post_status' => 'publish');
  55. $update = wp_update_post($post_data);
  56. // update failed...
  57. if ( ! $update ) return;
  58. // delete verify key
  59. delete_post_meta($post->ID, '_approve_key');
  60. // work finished, view the post
  61. wp_redirect( get_permalink($post->ID) );
  62. exit();
  63. }
  64. }
Add Comment
Please, Sign In to add comment