コード例 #1
0
 /**
  * Instance.
  *
  * @static
  * @return LivePress_Live_Update
  */
 public static function instance()
 {
     if (self::$instance == null) {
         self::$instance = new LivePress_Live_Update();
     }
     return self::$instance;
 }
コード例 #2
0
 /**
  * Initialize live updates.
  *
  * @return LivePress_Live_Update
  */
 function init_live_update()
 {
     $live_update = LivePress_Live_Update::instance();
     if (isset($this->custom_author_name)) {
         $live_update->use_custom_author_name($this->custom_author_name);
     }
     if (isset($this->custom_timestamp)) {
         $live_update->use_custom_timestamp($this->custom_timestamp);
     }
     if (isset($this->custom_avatar_url)) {
         $live_update->use_custom_avatar_url($this->custom_avatar_url);
     }
     return $live_update;
 }
コード例 #3
0
 /**
  * Short circut the update mechanism.
  *
  * Logic branches
  * - If this is a new post, do nothing.
  * - If this is a change to an existing post, do nothing.
  * - If this is an addition to an existing post, remove the addition and store it as
  *   a LivePress update instead.
  *
  * @param array $post_data
  * @param array $content_struct
  */
 function insert_post_data($post_data, $content_struct)
 {
     // Only fire on XML-RPC requests
     if (!defined('XMLRPC_REQUEST') || false == XMLRPC_REQUEST || defined('LP_FILTERED') && true == LP_FILTERED) {
         return $post_data;
     }
     define('LP_FILTERED', true);
     $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0, 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '');
     $unparsed_data = wp_parse_args($content_struct, $defaults);
     // If this isn't an update, exit early so we don't walk all over ourselves.
     if (empty($unparsed_data['ID'])) {
         return $post_data;
     }
     // Get the post we're updating so we can compare the new content with the old content.
     $post_id = $unparsed_data['ID'];
     $post = get_post($post_id);
     $live_posts = get_option('livepress_live_posts', array());
     $is_live = in_array($post_id, $live_posts);
     if (!$is_live) {
         return $post_data;
     }
     $original_posts = $this->get_existing_updates($post);
     $new_posts = $this->parse_updates($unparsed_data['post_content']);
     // First, add the new update if there is one
     if (isset($new_posts['new'])) {
         if (1 === count($new_posts)) {
             // Todo: If the only post sent up is "new" create a diff against the existing post so we can process the update.
             $clean_content = preg_replace('/\\<\\!--livepress(.+)--\\>/', '', $post->post_content);
             $new_posts['new']['content'] = str_replace($clean_content, '', $new_posts['new']['content']);
         }
         $live_update = LivePress_Live_Update::instance();
         $options = $live_update->options;
         // Add timestamp to update - this is optional on the editor, but
         // we're adding it here and can be customized later on in case
         // it is requested:
         $new_posts['new']['content'] = str_replace('[livepress_metainfo', '[livepress_metainfo show_timestmp="1"', $new_posts['new']['content']);
         // get the author to pass to avatar function
         preg_match('/authors="(.*)"/s', $new_posts['new']['content'], $author);
         $avater_class = in_array('AVATAR', $options['show']) ? 'lp_avatar_shown' : 'lp_avatar_hidden';
         $avater_class = 'lp_avatar_hidden';
         // TODO: until we have the avatar code working
         // default is the inline version
         if ('default' === $options['update_format']) {
             if ('lp_avatar_shown' == $avater_class) {
                 $new_posts['new']['content'] = $this->avatar_html($author[1]) . $new_posts['new']['content'];
             }
             $new_posts['new']['content'] = $new_posts['new']['content'] . PHP_EOL . ' [/livepress_metainfo]' . PHP_EOL;
             if (false === strpos($new_posts['new']['content'], 'livepress-update-outer-wrapper')) {
                 $new_posts['new']['content'] = '<div class="livepress-update-outer-wrapper ' . $avater_class . '">' . PHP_EOL . PHP_EOL . $new_posts['new']['content'] . PHP_EOL . PHP_EOL . '<\\/div>';
             }
         } else {
             $bits = explode(']', $new_posts['new']['content']);
             if (false === strpos($new_posts['new']['content'], 'livepress-update-inner-wrapper')) {
                 $new_posts['new']['content'] = '<div class="livepress-update-inner-wrapper ' . $avater_class . '">' . PHP_EOL . PHP_EOL . $bits[1] . PHP_EOL . PHP_EOL . '<\\/div>';
             }
             if ('lp_avatar_shown' == $avater_class) {
                 $new_posts['new']['content'] = $this->avatar_html($author[1]) . $new_posts['new']['content'];
             }
             $new_posts['new']['content'] = $bits[0] . ']' . $new_posts['new']['content'];
         }
         $new_posts['new']['content'] = $live_update->fill_livepress_shortcodes($new_posts['new']['content']);
         $update_id = LivePress_PF_Updates::get_instance()->add_update($post, $new_posts['new']['content'], array());
         // Remove the new post from the array so we don't double-process by mistake.
         unset($new_posts['new']);
     }
     // Second, update the content of any posts that have been changed.
     // You cannot *delete* an update via XMLRPC.  For that, you need to actually use the WordPress UI.
     foreach ($original_posts as $original_id => $original_post) {
         // Skip the parent post
         if ($post_id === $original_id) {
             continue;
         }
         // Skip if no changes were passed in for this post
         if (!isset($new_posts[$original_id])) {
             continue;
         }
         $updated = $new_posts[$original_id];
         // Do we actually need to do an update?
         $md5 = md5($updated['content']);
         if ($updated['md5'] === $md5) {
             continue;
         }
         $original_post['post_content'] = $updated['content'];
         wp_update_post($original_post);
     }
     // Update post data for the parent post.
     if (isset($new_posts[$post_id])) {
         $post_data['post_content'] = $new_posts[$post_id]['content'];
     } else {
         $post_data['post_content'] = $post->post_content;
     }
     return $post_data;
 }