コード例 #1
0
 /**
  * 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();
 }