in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true; if ( $this->in_jetpack ) { require "gplus-authorship/admin/ui.php"; $gplus_admin = new GPlus_Authorship_Admin; add_action( 'save_post', array( 'GPlus_Authorship_Admin', 'save_post_meta' ) ); add_action( 'wp_ajax_save_gplus_profile_data', array( $this, 'save_profile_data' ) ); add_filter( 'the_content', array( $this, 'post_output_wrapper' ), 22, 1 ); } else { add_filter( 'post_flair', array( $this, 'post_output_wrapper' ), 22 ); } add_action( 'wp_head', array( $this, 'link_tag_styles_and_scripts' ) ); add_filter( 'the_author', array( $this, 'overwrite_the_author' ) ); add_filter( 'the_content', array( $this, 'overwrite_rel_attrs' ) ); } function show_on_this_post() { global $post; $show = apply_filters( 'gplus_authorship_show', true, $post ); if ( ! is_main_query() || ! in_the_loop() ) $show = false; $author = $this->information( $post->post_author ); if ( empty( $author ) ) $show = false; $meta = get_post_meta( $post->ID, 'gplus_authorship_disabled', true ); if ( isset( $meta ) && true == $meta ) $show = false; return $show; } function information( $author ) { $authors = get_option( 'gplus_authors', array() ); return ( empty( $authors[ $author ] ) ? array() : $authors[ $author ] ); } /** * Both overwrite_rel_attrs and rel_callback remove 'author' from the rel attribute of a link if it is found * and if the post is being viewed on a single page, with G+ authorship enabled. * based on prior art from privatize_link in wp-content/mu-plugins/private-blog.php */ function overwrite_rel_attrs( $content ) { if ( !is_single() ) // don't bother unless we are on a page where the G+ authorship link is actually being displayed return $content; if ( !$this->show_on_this_post() ) // G+ isn't enabled return $content; return preg_replace_callback( '#]+>#i', array( $this, 'rel_callback' ), $content ); } function rel_callback( $link ) { $link = $link[0]; // preg replace returns as array // See if the link contains a rel="author ..." attribute, and if so, remove the author part $link = preg_replace_callback( '/rel\s*=\s*("|\').*author[^"\']*("|\')/i', array( $this, 'rel_attr_callback' ), $link ); // See if we have an empty rel attribute now and remove it if need be $link = preg_replace( '/rel\s*=\s*("|\')\s*("|\')\s*/i', '', $link ); return $link; } function rel_attr_callback( $attr ) { $attr = $attr[0]; $attr = preg_replace( '/author\s*/i', '', $attr ); return $attr; } /** * Jetpack Only */ function save_profile_data() { global $current_user; check_ajax_referer( 'gplus-connect', 'state' ); if ( !is_numeric( $_POST['id'] ) ) return; $connections = get_option( 'gplus_authors', array() ); $connections[ $current_user->ID ]['name'] = stripslashes( $_POST['name'] ); $connections[ $current_user->ID ]['id'] = $_POST['id']; $connections[ $current_user->ID ]['url'] = esc_url_raw( $_POST['url'] ); $connections[ $current_user->ID ]['profile_image'] = esc_url_raw( $_POST['profile_image'] ); update_option( 'gplus_authors', $connections ); } /** * Google+ insits we don't have two different bylines.. * so we will display their G+ username in the byline here too */ function overwrite_the_author( $author_name ) { global $post; if ( !$this->show_on_this_post() ) return $author_name; $author = $this->information( $post->post_author ); return esc_html( $author['name'] ); } function byline( $post ) { $author = $this->information( $post->post_author ); $image = '' . esc_attr( $author['name'] ) . ' '; $byline = sprintf( '%2$s', esc_url( $author['url'] ), $image, esc_html( $author['name'] ) ); return apply_filters( 'gplus_authorship_byline', $byline, $post ); } function link_tag_styles_and_scripts() { if ( !is_single() ) return; if ( get_post_type() != 'post' ) return; if ( !$this->show_on_this_post() ) return; global $post; $author = $this->information( $post->post_author ); echo ' ' . "\n"; if ( $this->in_jetpack ) $css = plugins_url( 'gplus-authorship/style.css', __FILE__ ); else $css = plugins_url( 'gplus/style.css', __FILE__ ); wp_enqueue_style( 'gplus', $css ); wp_enqueue_script( 'plusone', '//apis.google.com/js/plusone.js' ); } function follow_button( $post ) { $author = $this->information( $post->post_author ); return ''; } function post_output_wrapper( $text = '', $echo = false ) { global $post, $wp_current_filter; if ( !is_single() ) return $text; if ( get_post_type() != 'post' ) return $text; if ( true === $this->byline_displayed ) return $text; $author = $this->information( $post->post_author ); if ( empty( $author ) ) return $text; // Don't allow G+ to be added to the_content more than once (prevent infinite loops) $done = false; foreach ( $wp_current_filter as $filter ) { if ( 'the_content' == $filter ) { if ( $done ) return $text; else $done = true; } } if ( !$this->show_on_this_post()) return $text; $output = ''; $output .= '
'; $output .= '

' . __( 'Google+', 'jetpack' ) . '

'; $output .= '
'; $output .= $this->byline( $post ); $output .= $this->follow_button( $post ); $output .= '
'; $output .= '
'; $this->byline_displayed = true; if ( $echo ) echo $text . $output; else return $text . $output; } } ?>