/**
  * Publish a LivePress update.
  *
  * @param WP_Post $post WP_Post object.
  */
 public function livepress_publish_post($post)
 {
     $permalink = get_permalink($post->ID);
     $data_to_js = array('title' => $post->post_title, 'author' => get_the_author_meta('login', $post->post_author), 'updated_at_gmt' => $post->post_modified_gmt . 'Z', 'link' => $permalink);
     $params = array('post_id' => $post->ID, 'post_title' => $post->post_title, 'post_link' => $permalink, 'data' => JSON_encode($data_to_js), 'blog_public' => get_option('blog_public'));
     try {
         $return = $this->livepress_communication->send_to_livepress_new_post($params);
         update_option(LP_PLUGIN_NAME . '_new_post', $return['oortle_msg']);
         LivePress_WP_Utils::save_on_post($post->ID, 'feed_link', $return['feed_link']);
     } catch (LivePress_Communication_Exception $e) {
         $e->log('new post');
     }
 }
 /**
  * Send to the livepress webservice a new message with the comment updates
  */
 public function send_to_livepress_new_comment($comment_id, $comment_status = '')
 {
     if (is_int($comment_id)) {
         $comment = get_comment($comment_id);
     } else {
         $comment = $comment_id;
         $comment_id = $comment->comment_ID;
     }
     if (!$comment_status) {
         $comment_status = wp_get_comment_status($comment_id);
     }
     $post = get_post($comment->comment_post_ID);
     $params = array('content' => $comment->comment_content, 'comment_id' => $comment_id, 'comment_url' => get_comment_link($comment), 'comment_gmt' => $comment->comment_date_gmt . 'Z', 'post_id' => $comment->comment_post_ID, 'post_title' => $post->post_title, 'post_link' => get_permalink($post->ID), 'author' => $comment->comment_author, 'author_url' => $comment->comment_author_url, 'avatar_url' => get_avatar($comment->comment_author_email, 30));
     if ($comment_status != 'approved') {
         try {
             $params = array_merge($params, array('status' => $comment_status, 'author_email' => $comment->comment_author_email));
             $this->lp_com->send_to_livepress_new_created_comment($params);
         } catch (LivePress_Communication_Exception $e) {
             $e->log('new comment');
         }
     } else {
         $old_uuid = LivePress_WP_Utils::get_from_post($comment->comment_post_ID, 'comment_update', true);
         $new_uuid = $this->lp_com->new_uuid();
         LivePress_WP_Utils::save_on_post($comment->comment_post_ID, 'comment_update', $new_uuid);
         // Used to fake if the user is logged or not
         global $user_ID;
         $global_user_ID = $user_ID;
         $user_ID = null;
         if (current_user_can('edit_post', $post->ID)) {
             wp_set_current_user(null);
         }
         $comment_template_non_logged = $this->get_comment_list_templated($comment);
         $added_comment_template_non_logged = $this->get_comment_templated($comment, $post);
         $updated_counter_only_template = $this->get_comments_counter_templated($comment_id, $post);
         global $wp_query;
         $wp_query->rewind_comments();
         $user_ID = $post->post_author;
         if (!current_user_can('edit_post', $post->ID)) {
             wp_set_current_user($post->post_author);
         }
         $comment_template = $this->get_comment_list_templated($comment);
         $added_comment_template = $this->get_comment_templated($comment, $post);
         $updated_counter_only_template_logged = $this->get_comments_counter_templated($comment_id, $post);
         try {
             $params = array_merge($params, array('post_author' => get_the_author_meta('login', $post->post_author), 'old_template' => $comment_template_non_logged['old'], 'new_template' => $comment_template_non_logged['new'], 'comment_parent' => $comment->comment_parent, 'comment_html' => $added_comment_template_non_logged, '_ajax_nonce' => $this->options['ajax_comment_nonce'], 'previous_uuid' => $old_uuid, 'uuid' => $new_uuid, 'old_template_logged' => $comment_template['old'], 'new_template_logged' => $comment_template['new'], 'comments_counter_only' => $updated_counter_only_template, 'comments_counter_only_logged' => $updated_counter_only_template_logged, 'comment_html_logged' => $added_comment_template));
             $this->lp_com->send_to_livepress_approved_comment($params);
         } catch (LivePress_Communication_Exception $e) {
             $e->log('approved comment');
         }
         $user_ID = $global_user_ID;
         // Restore changed global $user_ID
     }
 }
 function lp_update_shortlink()
 {
     check_ajax_referer('lp_update_shortlink');
     $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : null;
     $update_id = isset($_REQUEST['update_id']) ? intval($_REQUEST['update_id']) : null;
     $cache_key = 'lp_shortlink_aaaa' . LP_PLUGIN_VERSION . '_' . $update_id;
     $shortlink = LivePress_WP_Utils::get_from_post($post_id, $cache_key, true);
     $status_code = 200;
     if (!$shortlink) {
         global $post;
         $post = get_post($post_id);
         $post_parent_url = get_permalink($post);
         if (0 === preg_match('/\\/\\?/', $post_parent_url)) {
             $post_parent_url .= '?';
         } else {
             $post_parent_url .= '&';
         }
         $canonical_url = $post_parent_url . 'lpup=' . $update_id . '#livepress-update-' . $update_id;
         //$bitly_api = '7ea952a9826d091fbda8a4ca220ba634efe61e31'; // TODO: Allow to set up from web page
         $bitly_api = 'a68d0da03159457bff5f6b287d6cdecb88b108dd';
         // datacompboy
         $get_shortlink = 'https://api-ssl.bitly.com/v3/shorten?access_token=' . $bitly_api . '&domain=bit.ly&longUrl=' . urlencode($canonical_url);
         $url = $get_shortlink;
         if (function_exists('vip_safe_wp_remote_get')) {
             $res = vip_safe_wp_remote_get($url, '', 5, 10, 20, array('reject_unsafe_urls' => false));
         } else {
             $res = wp_remote_get($url, array('reject_unsafe_urls' => false));
         }
         $response = json_decode($res['body'], true);
         $status_code = $response['status_code'];
         if (is_wp_error($res) || 200 !== $status_code) {
             $shortlink = $canonical_url;
         } else {
             if (!$res || !isset($response['data']) || !isset($response['data']['url'])) {
                 $shortlink = $canonical_url;
                 $status_code = 500;
             } else {
                 $shortlink = $response['data']['url'];
                 LivePress_WP_Utils::save_on_post($post_id, $cache_key, $shortlink);
                 $options = get_option(LivePress_Administration::$options_name);
                 $livepress_com = new LivePress_Communication($options['api_key']);
                 $livepress_com->send_to_livepress_broadcast($post_id, array('shortlink' => array($update_id => $shortlink)));
             }
         }
     }
     wp_send_json_success(array('shortlink' => $shortlink, 'code' => $status_code));
 }
 /**
  * Send an update (add/change/delete) to LivePress' API
  *
  * @param string  $op     Operation (append/prepend/replace/delete)
  * @param WP_Post $post   Parent post object
  * @param WP_Post $update Update piece object
  *
  * @return array[] $region Object to send to editor
  */
 protected function send_to_livepress_incremental_post_update($op, $post, $update, $update_meta)
 {
     if (!is_object($post)) {
         $post = get_post($post);
     }
     if (!is_object($update)) {
         $update = get_post($update);
     }
     // FIXME: may be better use $update->post_author there ?
     $user = wp_get_current_user();
     if ($user->ID) {
         if (empty($user->display_name)) {
             $update_author = addslashes($user->user_login);
         }
         $update_author = addslashes($user->display_name);
     } else {
         $update_author = '';
     }
     list($_, $piece_id, $piece_gen) = explode('__', $update->post_title, 3);
     global $wp_filter;
     // Remove all the_content filters so child posts are not filtered
     // removing share, vote and other per-post items from the live update stream.
     // Store the filters first for later restoration so filters still fire outside the update stream
     $stored_wp_filter_the_content = $wp_filter;
     $this->clear_most_the_content_filters();
     $region = array('id' => $piece_id, 'lpg' => $piece_gen, 'op' => $op, 'content' => $update->post_content, 'proceed' => do_shortcode(apply_filters('the_content', $update->post_content)), 'prefix' => sprintf('<div id="livepress-update-%s" data-lpg="%d" class="livepress-update">', $piece_id, $piece_gen), 'suffix' => '</div>');
     // Restore the_content filters and carry on
     $wp_filter = $stored_wp_filter_the_content;
     $message = array('op' => $op, 'post_id' => $post->ID, 'post_title' => $post->post_title, 'post_link' => get_permalink($post->ID), 'post_author' => $update_author, 'update_id' => 'livepress-update-' . $piece_id, 'updated_at' => get_gmt_from_date(current_time('mysql')) . 'Z', 'uuid' => $piece_id . ':' . $piece_gen, 'edit' => json_encode($region));
     if ($op == 'replace') {
         $message['new_data'] = $region['prefix'] . $region['proceed'] . $region['suffix'];
     } elseif ($op == 'delete') {
         $region['content'] = '';
         // remove content from update for delete
         $region['proceed'] = '';
     } else {
         $message['data'] = $region['prefix'] . $region['proceed'] . $region['suffix'];
     }
     if (true !== $update_meta['draft']) {
         try {
             $job_uuid = $this->lp_comm->send_to_livepress_incremental_post_update($op, $message);
             LivePress_WP_Utils::save_on_post($post->ID, 'status_uuid', $job_uuid);
         } catch (livepress_communication_exception $e) {
             $e->log('incremental post update');
         }
     }
     // Set the parent post as having been updated
     $status = array('automatic' => 1, 'live' => 1);
     update_post_meta($post->ID, '_livepress_live_status', $status);
     $region['status'] = $status;
     // add meta to the child update
     update_post_meta($update->ID, '_livepress_update_meta', $update_meta);
     $region['update_meta'] = $update_meta;
     return $region;
 }