/** * Removes live blogging from a post * * <Post ID> * : ID of the post to remove live blogging from. * * * ## EXAMPLES * * wp livepress remove 1 * * @synopsis */ public function remove($args, $assoc_args = null) { $current_post_id = null; if (!isset($args[0])) { WP_CLI::error('Post ID Missing'); return; } else { $current_post_id = $args[0]; } $blogging_tools = $this->get_blogging_tools(); if (false === $blogging_tools->get_post_live_status($current_post_id)) { WP_CLI::error('Post with ID: ' . $current_post_id . ' is not a live blog post'); return; } // copied from livepress-administration.php / function deactivate_livepress() $lp_updater = LivePress_PF_Updates::get_instance(); // Merge children posts $lp_updater->merge_children($current_post_id); // Turn off live $blogging_tools->set_post_live_status($current_post_id, false); WP_CLI::success('Post with ID:' . $current_post_id . ' is now a NOT a live blog'); }
/** * Set up JS config for output. * * @param string $hook Hook suffix for the current screen. */ function add_js_config($hook = null) { global $post; // If we are in the admin, only load LivePress JS on the // post page and the livepress settings page if (is_admin() && $hook != null && $hook != 'post-new.php' && $hook != 'post.php' && $hook != 'settings_page_livepress-settings') { return; } $is_live = isset($post) ? LivePress_Updater::instance()->blogging_tools->get_post_live_status($post->ID) : false; // On the front end, only load LivePress JS on pages that are live, // or when the WordPress admin bar is showing if (!is_admin() && !is_admin_bar_showing() && !$is_live) { return; } $ljsc = new LivePress_JavaScript_Config(); if ($this->livepress_config->debug() || $this->livepress_config->script_debug()) { $ljsc->new_value('debug', true, Livepress_Configuration_Item::$BOOLEAN); } $ljsc->new_value('ajax_nonce', $this->options['ajax_nonce']); $ljsc->new_value('ajax_comment_nonce', $this->options['ajax_comment_nonce']); $ljsc->new_value('ajax_status_nonce', $this->options['ajax_status_nonce']); $ljsc->new_value('lp_add_live_update_tags_nonce', $this->options['lp_add_live_update_tags_nonce']); $ljsc->new_value('ajax_twitter_search_nonce', $this->options['ajax_twitter_search_nonce']); $ljsc->new_value('ajax_twitter_follow_nonce', $this->options['ajax_twitter_follow_nonce']); $ljsc->new_value('ajax_api_validate_nonce', $this->options['ajax_api_validate_nonce']); $ljsc->new_value('ajax_lp_post_to_twitter', $this->options['ajax_lp_post_to_twitter']); $ljsc->new_value('ajax_check_oauth', $this->options['ajax_check_oauth']); $ljsc->new_value('lp_update_shortlink_nonce', $this->options['lp_update_shortlink_nonce']); $ljsc->new_value('ajax_lp_collaboration_comments', $this->options['ajax_lp_collaboration_comments']); $ljsc->new_value('ajax_get_live_edition_data', $this->options['ajax_get_live_edition_data']); $ljsc->new_value('ajax_lp_im_integration', $this->options['ajax_lp_im_integration']); $ljsc->new_value('ajax_render_tabs', $this->options['ajax_render_tabs']); $ljsc->new_value('ajax_update_live_comments', $this->options['ajax_update_live_comments']); $ljsc->new_value('ajax_new_im_follower', $this->options['ajax_new_im_follower']); $ljsc->new_value('ajax_start_editor', $this->options['ajax_start_editor']); $ljsc->new_value('ver', LP_PLUGIN_VERSION); $ljsc->new_value('oover', $this->livepress_config->lp_ver(), Livepress_Configuration_Item::$ARRAY); global $wp_scripts; if ($wp_scripts === null) { $wp_scripts = new WP_Scripts(); wp_default_scripts($wp_scripts); } if (is_a($wp_scripts, 'WP_Scripts')) { $src = $wp_scripts->query('jquery'); $src = $src->src; if (!preg_match('|^https?://|', $src) && !($wp_scripts->content_url && 0 === strpos($src, $wp_scripts->content_url))) { $src = $wp_scripts->base_url . $src; } $ljsc->new_value('jquery_url', $src . '?'); } if (function_exists('get_current_screen')) { $screen = get_current_screen(); if (is_a($screen, 'WP_Screen')) { $ljsc->new_value('current_screen', array('base' => $screen->base, 'id' => $screen->id), Livepress_Configuration_Item::$ARRAY); } } $ljsc->new_value('wpstatic_url', LP_PLUGIN_URL); $ljsc->new_value('static_url', $this->livepress_config->static_host()); $ljsc->new_value('lp_plugin_url', LP_PLUGIN_URL); $ljsc->new_value('blog_gmt_offset', get_option('gmt_offset'), Livepress_Configuration_Item::$LITERAL); $theme_name = strtolower(wp_get_theme()->Name); $ljsc->new_value('theme_name', $theme_name); try { if (isset($this->title_css_selectors[$theme_name])) { $title_css_selector = $this->title_css_selectors[$theme_name]; $title_css_selector = apply_filters('livepress_title_css_selector', $title_css_selector, $theme_name); $ljsc->new_value('custom_title_css_selector', $title_css_selector); } } catch (livepress_invalid_option_exception $e) { } try { if (isset($this->background_colors[$theme_name])) { $background_color = $this->background_colors[$theme_name]; $background_color = apply_filters('livepress_background_color', $background_color, $theme_name); $ljsc->new_value('custom_background_color', $background_color); } } catch (livepress_invalid_option_exception $e) { } if (is_home()) { $page_type = 'home'; } elseif (is_page()) { $page_type = 'page'; } elseif (is_single()) { $page_type = 'single'; } elseif (is_admin()) { $page_type = 'admin'; } else { $page_type = 'undefined'; } $ljsc->new_value('page_type', $page_type); // Comments if (isset($post->ID)) { $args = array('post_id' => isset($post->ID) ? $post->ID : null); $post_comments = get_comments($args); $old_comments = isset($GLOBALS['wp_query']->comments) ? $GLOBALS['wp_query']->comments : null; $GLOBALS['wp_query']->comments = $post_comments; $comments_per_page = get_comment_pages_count($post_comments, get_option('comments_per_page')); $GLOBALS['wp_query']->comments = $old_comments; $this->lp_comment->js_config($ljsc, $post, intval(get_query_var('cpage')), $comments_per_page); // Fetch rexisting shortlinks $sl = array(); foreach (get_post_meta($post->ID) as $k => $m) { if (preg_match('/^_livepress_shortlink_([0-9]+)$/', $k, $r)) { $sl[$r[1]] = $m[0]; } } $ljsc->new_value('shortlink', $sl, Livepress_Configuration_Item::$ARRAY); $ljsc->new_value('post_url', get_permalink($post->ID)); $ljsc->new_value('post_title', $post->post_title); } $ljsc->new_value('new_post_msg_id', get_option(LP_PLUGIN_NAME . '_new_post')); $notifications = isset($this->options['notifications']) ? $this->options['notifications'] : array(); $ljsc->new_value('sounds_default', in_array('audio', $notifications), Livepress_Configuration_Item::$BOOLEAN); $ljsc->new_value('autoscroll', in_array('scroll', $notifications), Livepress_Configuration_Item::$BOOLEAN); $ljsc->new_value('effects', in_array('effects', $notifications), Livepress_Configuration_Item::$BOOLEAN); // colors used to highlight changes $ljsc->new_value('oortle_diff_inserted', apply_filters('livepress_effects_inserted', '#55C64D')); $ljsc->new_value('oortle_diff_changed', apply_filters('livepress_effects_changed', '#55C64D')); $ljsc->new_value('oortle_diff_inserted_block', apply_filters('livepress_effects_inserted_block', '#ffff66')); $ljsc->new_value('oortle_diff_removed_block', apply_filters('livepress_effects_removed_block', '#C63F32')); $ljsc->new_value('oortle_diff_removed', apply_filters('livepress_effects_removed', '#C63F32')); if (is_admin() || $is_live) { if (isset($post->ID) && $post->ID) { $args = array('post_id' => $post->ID); $post_comments = get_comments($args); if (!empty($post_comments)) { $ljsc->new_value('comment_pages_count', get_comment_pages_count($post_comments, get_option('comments_per_page')), Livepress_Configuration_Item::$LITERAL); } $feed_link = $this->get_current_post_feed_link(); if (sizeof($feed_link)) { $ljsc->new_value('feed_sub_link', $feed_link[0]); $ljsc->new_value('feed_title', LivePress_Feed::feed_title()); } $ljsc->new_value('post_id', $post->ID); $ljsc->new_value('post_id', $post->ID); $ljsc->new_value('post_live_status', $is_live); $pf = LivePress_PF_Updates::get_instance(); if (!$pf->near_uuid) { $pf->cache_pieces($post); } $ljsc->new_value('post_update_msg_id', $pf->near_uuid); } $author = ''; $user = wp_get_current_user(); if ($user->ID) { if (empty($user->display_name)) { $author = $user->user_login; } else { $author = $user->display_name; } } $ljsc->new_value('current_user', $author); if (is_admin()) { // Is post_from turned on if ($this->user_options['remote_post']) { $ljsc->new_value('remote_post', true, Livepress_Configuration_Item::$BOOLEAN); } else { $ljsc->new_value('remote_post', false, Livepress_Configuration_Item::$BOOLEAN); } $ljsc->new_value('PostMetainfo', '', Livepress_Configuration_Item::$BLOCK); // Set if the live updates should display the timestamp $timestamp = isset($this->options['timestamp']) ? $this->options['timestamp'] : false; if ($timestamp) { $template = LivePress_Live_Update::timestamp_template(); $ljsc->new_value('timestamp_template', $template); } // Set url for the avatar $include_avatar = isset($this->options['include_avatar']) ? $this->options['include_avatar'] : false; $ljsc->new_value('has_avatar', $include_avatar, Livepress_Configuration_Item::$BOOLEAN); // Set the author name $update_author = isset($this->options['update_author']) ? $this->options['update_author'] : false; if ($update_author) { $use_default_author = true === apply_filters('livepress_use_default_author', true) ? true : false; $ljsc->new_value('use_default_author', $use_default_author, Livepress_Configuration_Item::$BOOLEAN); $author_display_name = $use_default_author ? LivePress_Live_Update::get_author_display_name($this->options) : ''; $ljsc->new_value('author_display_name', $author_display_name); $user = wp_get_current_user(); $author_id = isset($user->ID) ? $user->ID : 0; $ljsc->new_value('author_id', $use_default_author ? $author_id : ''); } // The last attribute shouldn't have a comma // Set where the live updates should be inserted (top|bottom) $feed_order = isset($this->options['feed_order']) ? $this->options['feed_order'] : false; $ljsc->new_value('placement_of_updates', $feed_order); $ljsc->new_value("PostMetainfo", "", Livepress_Configuration_Item::$ENDBLOCK); $ljsc->new_value('allowed_chars_on_post_update_id', implode(LivePress_Post::$ALLOWED_CHARS_ON_ID)); $ljsc->new_value('num_chars_on_post_update_id', LivePress_Post::$NUM_CHARS_ID, Livepress_Configuration_Item::$LITERAL); } } $ljsc->new_value('site_url', site_url()); $ljsc->new_value('ajax_url', site_url() . '/wp-admin/admin-ajax.php'); $ljsc->new_value('locale', get_locale()); if (function_exists('wpcom_vip_noncdn_uri')) { $ljsc->new_value('noncdn_url', wpcom_vip_noncdn_uri(dirname(dirname(__FILE__)))); } else { $ljsc->new_value('noncdn_url', LP_PLUGIN_URL); } // Check if we have the Facebook embed plugin so we have to load // the FB js on livepress.admin.js: if (class_exists('Facebook_Loader')) { $ljsc->new_value('facebook', 'yes'); } $settings = get_option('livepress'); if (isset($settings['update_format'])) { $ljsc->new_value('timestamp_format', $settings['timestamp_format']); } // Check for Facebook App ID for sharing UI: if (isset($settings['facebook_app_id'])) { $ljsc->new_value('facebook_app_id', $settings['facebook_app_id']); } if (isset($settings['sharing_ui'])) { $ljsc->new_value('sharing_ui', $settings['sharing_ui']); } if (isset($post->ID)) { $ljsc->new_value('post_url', get_permalink($post->ID)); } // Localize `LivepressConfig` in admin and on front end live posts if (is_admin()) { // Add the update_formatting if (isset($settings['update_format'])) { $ljsc->new_value('update_format', $settings['update_format']); } if (isset($settings['show'])) { $ljsc->new_value('show', $settings['show']); } // Add existing livetags $live_update_tags = get_terms(array('livetags'), array('hide_empty' => false)); if (!empty($live_update_tags) && !is_wp_error($live_update_tags)) { $update_tags = wp_list_pluck($live_update_tags, 'name'); } else { $update_tags = ''; } $ljsc->new_value('live_update_tags', $update_tags); $ljsc->new_value('current_screen', get_current_screen()); wp_localize_script('lp-admin', 'LivepressConfig', $ljsc->get_values()); } else { wp_localize_script('livepress-plugin-loader', 'LivepressConfig', $ljsc->get_values()); } $ljsc->flush(); }
/** * 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']); } $update_id = LivePress_PF_Updates::get_instance()->add_update($post, $new_posts['new']['content']); // 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; }
/** * Run on deactivation of plugin - merges children of all live posts * * @since 1.0.7 */ public static function deactivate_livepress() { $lp_updater = LivePress_PF_Updates::get_instance(); $blogging_tools = new LivePress_Blogging_Tools(); $live_posts = $blogging_tools->get_all_live_posts(); // Go thru each live post // code used in livepress_cli.php foreach ($live_posts as $the_post) { // Merge children posts $lp_updater->merge_children($the_post); // Turn off live $blogging_tools->set_post_live_status($the_post, false); } }
/** * Static method used to retrieve the current class instance. * * @static * * @return LivePress_PF_Updates */ public static function get_instance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; }
/** * 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; }