/wp-content/plugins/pushpress/send-ping.php

https://github.com/jedverity/m2m · PHP · 97 lines · 71 code · 19 blank · 7 comment · 10 complexity · 53167cc8de8fa229c79d6cdb35628cff MD5 · raw file

  1. <?php
  2. add_action( 'pushpress_scheduled_ping', 'pushpress_send_ping', 10, 4 );
  3. if ( !function_exists( 'pushpress_send_ping' ) ) {
  4. function pushpress_send_ping( $callback, $post_id, $feed_type, $secret ) {
  5. global $pushpress, $current_user;
  6. // Do all WP_Query calcs and send feeds as logged-out user.
  7. $old_user_id = $current_user->ID;
  8. wp_set_current_user( 0 );
  9. // Need to make sure that the PuSHPress options are initialized
  10. $pushpress->init( );
  11. do_action( 'pushpress_send_ping' );
  12. $remote_opt = array(
  13. 'headers' => array(
  14. 'format' => $feed_type
  15. ),
  16. 'sslverify' => FALSE,
  17. 'timeout' => $pushpress->http_timeout,
  18. 'user-agent' => $pushpress->http_user_agent
  19. );
  20. $post = get_post( $post_id );
  21. $post_status_obj = get_post_status_object( $post->post_status );
  22. if ( !$post_status_obj->public ) {
  23. do_action( 'pushpress_nonpublic_post', $post_id );
  24. wp_set_current_user( $old_user_id );
  25. return false;
  26. }
  27. do_enclose( $post->post_content, $post_id );
  28. update_postmeta_cache( array( $post_id ) );
  29. // make sure the channel title stays consistent
  30. // without this it would append the post title as well
  31. add_filter( 'wp_title', '__return_false', 999 );
  32. query_posts( "p={$post_id}" );
  33. ob_start( );
  34. $feed_url = FALSE;
  35. if ( $feed_type == 'rss2' ) {
  36. do_action( 'pushpress_send_ping_rss2' );
  37. $feed_url = get_bloginfo( 'rss2_url' );
  38. $remote_opt['headers']['Content-Type'] = 'application/rss+xml';
  39. $remote_opt['headers']['Content-Type'] .= '; charset=' . get_option( 'blog_charset' );
  40. @load_template( ABSPATH . WPINC . '/feed-rss2.php' );
  41. } elseif ( $feed_type == 'atom' ) {
  42. do_action( 'pushpress_send_ping_atom' );
  43. $feed_url = get_bloginfo( 'atom_url' );
  44. $remote_opt['headers']['Content-Type'] = 'application/atom+xml';
  45. $remote_opt['headers']['Content-Type'] .= '; charset=' . get_option( 'blog_charset' );
  46. @load_template( ABSPATH . WPINC . '/feed-atom.php' );
  47. }
  48. $remote_opt['body'] = ob_get_contents( );
  49. ob_end_clean( );
  50. // Figure out the signatur header if we have a secret on
  51. // on file for this callback
  52. if ( !empty( $secret ) ) {
  53. $remote_opt['headers']['X-Hub-Signature'] = 'sha1=' . hash_hmac(
  54. 'sha1', $remote_opt['body'], $secret
  55. );
  56. }
  57. $response = wp_remote_post( $callback, $remote_opt );
  58. // look for failures
  59. if ( is_wp_error( $response ) ) {
  60. do_action( 'pushpress_ping_wp_error' );
  61. wp_set_current_user( $old_user_id );
  62. return FALSE;
  63. }
  64. if ( isset( $response->errors['http_request_failed'][0] ) ) {
  65. do_action( 'pushpress_ping_http_failure' );
  66. wp_set_current_user( $old_user_id );
  67. return FALSE;
  68. }
  69. $status_code = (int) $response['response']['code'];
  70. if ( $status_code < 200 || $status_code > 299 ) {
  71. do_action( 'pushpress_ping_not_2xx_failure' );
  72. $pushpress->unsubscribe_callback( $feed_url, $callback );
  73. wp_set_current_user( $old_user_id );
  74. return FALSE;
  75. }
  76. wp_set_current_user( $old_user_id );
  77. } // function send_ping
  78. } // if !function_exists