/** * Print the feed link tag. */ public function feed_link() { $post_title = self::feed_title(); $feed_link = LivePress_Updater::instance()->get_current_post_feed_link(); if (count($feed_link) > 0) { $tag = '<link rel="alternate" type="' . esc_attr(feed_content_type('rss2')) . '" '; $tag .= 'title="' . esc_attr(get_bloginfo('name')) . ' » ' . esc_attr($post_title) . '" '; $tag .= 'href="' . esc_url($feed_link[0]) . '" />' . "\n"; echo $tag; } }
/** * Inject update box into theme. */ function inject_updatebox() { static $did_output = false; if (!defined('LIVEPRESS_THEME') || !constant('LIVEPRESS_THEME')) { if (!$did_output && (is_single() || is_home())) { if (is_single()) { LivePress_Updater::instance()->inject_widget(true); } $did_output = true; } } }
/** * Add user options to shortcode. * * @param string $content Post content. */ public function fill_livepress_shortcodes($content) { global $post; $is_live = isset($post) ? LivePress_Updater::instance()->blogging_tools->get_post_live_status($post->ID) : false; if (!$is_live) { return $content; } $options = $this->options; $new_shortcode = PHP_EOL . PHP_EOL . '[livepress_metainfo'; // do we have a short code $has_shortcode = false === strpos($content, '[livepress_metainfo') ? false : true; preg_match('/\\[livepress_metainfo show_timestmp=.?"(.).?".*\\]/s', $content, $show_timestmp); if (!empty($show_timestmp[1]) || !$has_shortcode) { $current_time_attr = ' time="' . $this->format_timestamp(current_time('timestamp', 1)) . '" '; if ($options['timestamp']) { $new_shortcode .= ' show_timestmp="1" '; if (isset($this->custom_timestamp)) { $custom_timestamp = strtotime($this->custom_timestamp); $new_shortcode .= ' time="' . $this->format_timestamp($custom_timestamp) . '"'; $new_shortcode .= ' timestamp="' . date('c', $custom_timestamp) . '"'; } else { $new_shortcode .= $current_time_attr; $new_shortcode .= ' timestamp="' . date('c', current_time('timestamp', 1)) . '"'; } } } preg_match('/\\[livepress_metainfo.*authors=.?"(.*).?".?\\]/', $content, $author_block); if (empty($author_block) || 0 == strlen($author_block[1])) { $current_authors = isset($_POST['authors']) ? $_POST['authors'] : array(); $custom_author_names = ''; $separator = ''; foreach ($current_authors as $author) { $custom_author_names .= $separator . $author['text']; $separator = ' - '; } } else { $custom_author_names = $author_block[1]; } $custom_names = explode('\\"', $custom_author_names); // remove any trailing \" if ('' !== trim($custom_names[0])) { $authorname = $custom_names[0]; } else { $authorname = self::get_author_display_name($options); } if ($authorname) { $new_shortcode .= ' authors="' . $authorname . '"'; } // look to see if we have an avatar and hide the author name if we have preg_match('/\\[livepress_metainfo.*avatar_block=.?"shown.?".*\\]/', $content, $avatar_block); if (!empty($avatar_block)) { $new_shortcode .= ' avatar_block="shown" '; } if ($options['include_avatar']) { $new_shortcode .= ' has_avatar="1"'; if (isset($this->custom_avatar_url)) { $new_shortcode .= ' avatar_url="' . $this->custom_avatar_url . '"'; } } // Pass the update header thru to processed shortcode preg_match('/.*update_header=.?"(.*).?".?\\]/', $content, $update_header); if (isset($update_header[1]) && 'undefined' !== $update_header[1]) { $new_shortcode .= ' update_header="' . $update_header[1] . '"'; } $new_shortcode .= ']' . PHP_EOL . PHP_EOL; // Replace empty livepress_metainfo with calculated one $content = preg_replace('/\\[livepress_metainfo[^\\]]*]/', $new_shortcode, $content . PHP_EOL); //unload the filter so it does run again remove_filter('content_save_pre', array($this, 'fill_livepress_shortcodes'), 5); // Replace POSTTIME inside livepress_metainfo with current time if (!empty($show_timestmp[1])) { $content = preg_replace('/(\\[livepress_metainfo[^\\]]*)POSTTIME([^\\]]*\\])/s', '$1' . $current_time_attr . '$2', $content); } return $content; }
/** * 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(); }
/** * Called by livepress webservice instead of editPost as we need to save * display_author (if passed) before the actual post save method so we know the * real author of update. * Also, we want to fetch post and append given content to it here, as doing it * in livepress webservice can cause race conditions. * This handles passed values, updates the arguments and passes them to * xmlrpc.php so it works as nothing happened. * * Handles custom_fields correclty * * @param array $args * * @return bool */ function livepress_append_to_post($args) { global $wp_xmlrpc_server, $wpdb; $wp_xmlrpc_server->escape($args); $username = esc_sql($args[1]); $password = esc_sql($args[2]); $post_id = (int) $args[0]; $content_struct = $args[3]; if (!($user = _livepress_authenticate($username, $password))) { return $wp_xmlrpc_server->error; } // at this point the user is authenticated // Check that the user can edit the post if (!user_can($user, 'edit_post', $post_id)) { return false; } // Verify that the post is live $blogging_tools = new LivePress_Blogging_Tools(); $is_live = $blogging_tools->get_post_live_status($post_id); if (!$is_live) { return false; } wp_set_current_user($user->ID); // Allow updates if (isset($content_struct['display_author'])) { LivePress_Updater::instance()->set_custom_author_name($content_struct['display_author']); } if (isset($content_struct['created_at'])) { LivePress_Updater::instance()->set_custom_timestamp($content_struct['created_at']); } if (isset($content_struct['avatar_url'])) { LivePress_Updater::instance()->set_custom_avatar_url($content_struct['avatar_url']); } $plugin_options = get_option(LivePress_Administration::$options_name); // Adds metainfo shortcode $content_struct['description'] = "[livepress_metainfo] " . $content_struct['description']; $content_struct['ID'] = $args[0]; $content_struct['post_content'] = $content_struct['description']; return wp_update_post($content_struct); }
/** * Add content CSS. * * @param $init * @return mixed */ function add_content_css($init) { if (!LivePress_Updater::instance()->blogging_tools->get_post_live_status(get_the_ID())) { return $init; } $css_for_tinymce = LP_PLUGIN_URL . 'tinymce/css/inside.css'; $css = LP_PLUGIN_URL . 'css/livepress.css'; $init['content_css'] .= ',' . $css . ',' . $css_for_tinymce; return $init; }
/** * LivePress update box output. */ function livepress_update_box() { static $called = 0; if ($called++) { return; } if (LivePress_Updater::instance()->has_livepress_enabled()) { echo '<div id="lp-update-box"></div>'; } }
/** * Populate the pieces array based on a given parent post. * * @param object $parent Parent post for which we're assembling pieces */ protected function assemble_pieces($parent, $cache = false) { 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(); if (!is_object($parent)) { $parent = get_post($parent); } if ($cache) { $this->cache = $parent->ID; } elseif ($this->cache == $parent->ID) { return; } $this->post_modified_gmt = $parent->post_modified_gmt; $this->near_uuid = ''; $min_uuid_ts = 2 * 60; // not earlier, than 2 minutes $near_uuid_ts = 0; $now = new DateTime(); $pieces = array(); // Set up child posts $children = get_children(array('post_type' => 'post', 'post_parent' => $parent->ID, 'suppress_filters' => false)); $child_pieces = array(); $live_tags = array(); $update_count = 0; $child_count = count($children); $user_id = get_current_user_id(); if ($child_count > 0) { foreach ($children as $child) { $update_meta = get_post_meta($child->ID, '_livepress_update_meta', true); $is_draft = false !== $update_meta && is_array($update_meta) && array_key_exists('draft', $update_meta) && true == $update_meta['draft'] ? true : false; // if this is a draft only include the current authors posts if ($is_draft && $user_id !== absint($child->post_author)) { continue; } $update_count++; $post = $child; list($_, $piece_id, $piece_gen) = explode('__', $child->post_title, 3); $this->post_modified_gmt = max($this->post_modified_gmt, $child->post_modified_gmt); $modified = new DateTime($child->post_modified_gmt, new DateTimeZone('UTC')); $since = $now->format('U') - $modified->format('U'); if ($since > $min_uuid_ts && ($since < $near_uuid_ts || $near_uuid_ts == 0)) { $near_uuid_ts = $since; $this->near_uuid = $piece_id . ':' . $piece_gen; } // Grab and integrate any live update tags $update_tags = get_the_terms($child->ID, 'livetags'); $update_tag_classes = $is_draft ? ' livepress-draft ' : ''; if (!empty($update_tags)) { foreach ($update_tags as $a_tag) { $live_tag_name = $a_tag->name; $update_tag_classes .= ' live-update-livetag-' . sanitize_title_with_dashes($live_tag_name); if (!in_array($live_tag_name, $live_tags)) { array_push($live_tags, $live_tag_name); } } $update_tag_classes .= ' livepress-has-tags '; } $pin_header = LivePress_Updater::instance()->blogging_tools->is_post_header_enabled($parent->ID); $piece = array('id' => $piece_id, 'lpg' => $piece_gen, 'content' => $child->post_content, 'proceed' => apply_filters('the_content', $child->post_content), 'prefix' => sprintf('<div id="livepress-update-%s" data-lpg="%d" class="livepress-update%s %s">', $piece_id, $piece_gen, $update_tag_classes, $child_count == $update_count && $pin_header ? 'pinned-first-live-update' : ''), 'suffix' => '</div>', 'meta' => $update_meta); $child_pieces[] = $piece; } } // Display posts oldest-newest by default $child_pieces = array_reverse($child_pieces); $pieces = array_merge($pieces, $child_pieces); if (0 !== count($pieces)) { if ('top' == $this->order) { // If the header is pinned and the order reversed, ensure the first post remains first $pin_header = LivePress_Updater::instance()->blogging_tools->is_post_header_enabled($parent->ID); if ($pin_header) { $first = array_shift($pieces); $pieces = array_reverse($pieces); array_unshift($pieces, $first); } else { $pieces = array_reverse($pieces); } } } $this->pieces = $pieces; $this->livetags = $live_tags; // Restore the_content filters and carry on $wp_filter = $stored_wp_filter_the_content; }
/** * Called by filter in the CoSchedule Plugin * Returns false to stop an update from being posted to the CoSchedule system * as only parent post needs to be scheduled * * @param bool $state * @param int $post_id * * @return bool */ static function tm_coschedule_save_post_callback_filter($state, $post_id) { $parent_id = wp_get_post_parent_id(abs($post_id)); if (LivePress_Updater::instance()->blogging_tools->get_post_live_status($parent_id)) { $state = false; } // really make sure that we return a bool return false === $state ? false : true; }