to speed up loading */ public static function shareaholic_resource_hints( $hints, $relation_type ) { // Do not load in wp-admin section if ( is_admin() || !is_array( $hints ) || $relation_type != 'dns-prefetch' ) { return $hints; } if ( 'dns-prefetch' === $relation_type ) { if ( $hints ) { array_push( $hints, '//m9m6e2w5.stackpathcdn.com', '//cdn.shareaholic.net', '//www.shareaholic.net', '//analytics.shareaholic.com', '//recs.shareaholic.com', '//partner.shareaholic.com' ); } } return $hints; } /** * Inserts the script code snippet into the head of the page */ public static function script_tag() { if ( ShareaholicUtilities::has_accepted_terms_of_service() && ShareaholicUtilities::get_or_create_api_key() ) { ShareaholicUtilities::load_template( 'script_tag', array( 'api_key' => ShareaholicUtilities::get_option( 'api_key' ), 'base_settings' => ShareaholicPublicJS::get_base_settings(), 'overrides' => ShareaholicPublicJS::get_overrides(), ) ); } } /** * The function that gets called for shortcodes * * @param array $attributes this is passed keys: `id`, `app`, `title`, `link`, `summary` * @param string $content is the enclosed content (if the shortcode is used in its enclosing form) */ public static function shortcode( $attributes, $content = null ) { extract( shortcode_atts( array( 'id' => null, 'id_name' => null, 'app' => 'share_buttons', 'title' => null, 'link' => null, 'summary' => null, ), $attributes, 'shareaholic' ) ); if ( isset( $attributes['id'] ) ) { $id = trim( $attributes['id'] ); } if ( isset( $attributes['id_name'] ) ) { $id_name = trim( $attributes['id_name'] ); } if ( isset( $attributes['app'] ) ) { $app = trim( $attributes['app'] ); } if ( isset( $attributes['title'] ) ) { $title = esc_attr( trim( $attributes['title'] ) ); } if ( isset( $attributes['link'] ) ) { $link = trim( $attributes['link'] ); } if ( isset( $attributes['summary'] ) ) { $summary = esc_attr( trim( $attributes['summary'] ) ); } return self::canvas( $app, $id, $id_name, $title, $link, $summary ); } /** * Draws the shareaholic meta tags. */ private static function shareaholic_tags() { echo "\n\n"; self::draw_site_name_meta_tag(); self::draw_language_meta_tag(); self::draw_url_meta_tag(); self::draw_keywords_meta_tag(); self::draw_article_meta_tag(); self::draw_site_id_meta_tag(); self::draw_plugin_version_meta_tag(); self::draw_image_meta_tag(); echo "\n\n"; } /** * Draws Shareaholic keywords meta tag. */ private static function draw_keywords_meta_tag() { if ( in_array( ShareaholicUtilities::page_type(), array( 'page', 'post' ) ) ) { global $post; $tags = array(); if ( is_attachment() && $post->post_parent ) { $id = $post->post_parent; } else { $id = $post->ID; } // Get post tags $tags = preg_replace( '/^/', 'tag:', ShareaholicUtilities::permalink_keywords( $id ) ); // Get post categories $categories_array = get_the_category( $id ); $categories = array(); if ( $categories_array ) { foreach ( $categories_array as $category ) { if ( $category->cat_name != 'Uncategorized' ) { array_push( $categories, $category->cat_name ); } } $categories = preg_replace( '/^/', 'cat:', $categories ); } $keywords_array = array(); $keywords = ''; // Merge + add page type $keywords_array = array_merge( $tags, $categories ); if ( $post->post_type ) { array_push( $keywords_array, 'type:' . $post->post_type ); } // Unique keywords $keywords_array = array_unique( $keywords_array ); if ( ! empty( $keywords_array[0] ) ) { $keywords = implode( ', ', $keywords_array ); // Encode, lowercase & trim appropriately $keywords = ShareaholicUtilities::normalize_keywords( $keywords ); } if ( $keywords != '' && $keywords != 'array' ) { echo "\n"; } } } /** * Draws Shareaholic article meta tags */ private static function draw_article_meta_tag() { if ( in_array( ShareaholicUtilities::page_type(), array( 'index', 'category' ) ) || is_404() ) { echo "\n"; return; } if ( in_array( ShareaholicUtilities::page_type(), array( 'page', 'post' ) ) ) { global $post; // Article Publish and Modified Time $article_published_time = get_the_date( DATE_W3C ); $article_modified_time = get_the_modified_date( DATE_W3C ); if ( ! empty( $article_published_time ) ) { echo "\n"; } if ( ! empty( $article_modified_time ) ) { echo "\n"; } // Article Visibility $article_visibility = $post->post_status; $article_password = $post->post_password; if ( $article_visibility == 'draft' || $article_visibility == 'auto-draft' || $article_visibility == 'future' || $article_visibility == 'pending' ) { echo "\n"; $article_visibility = 'draft'; } elseif ( $article_visibility == 'private' || $post->post_password != '' ) { echo "\n"; $article_visibility = 'private'; } elseif ( is_attachment() ) { // attachments are shareable but not recommendable echo "\n"; $article_visibility = 'private'; } else { echo "\n"; $article_visibility = null; } // Lookup Metabox value if ( get_post_meta( $post->ID, 'shareaholic_exclude_recommendations', true ) ) { $article_visibility = 'private'; } if ( ! empty( $article_visibility ) ) { echo "\n"; } // Article Author Name if ( $post->post_author ) { $article_author_data = get_userdata( $post->post_author ); if ( $article_author_data ) { $article_author_name = $article_author_data->display_name; } } if ( ! empty( $article_author_name ) ) { echo "\n"; } } } /** * Draws Shareaholic language meta tag. */ private static function draw_language_meta_tag() { $blog_language = get_bloginfo( 'language' ); if ( ! empty( $blog_language ) ) { echo "\n"; } } /** * Draws Shareaholic url meta tag. */ private static function draw_url_meta_tag() { if ( in_array( ShareaholicUtilities::page_type(), array( 'page', 'post' ) ) ) { $url_link = get_permalink(); echo "\n"; } } /** * Draws Shareaholic version meta tag. */ private static function draw_plugin_version_meta_tag() { echo "\n"; } /** * Draws Shareaholic site name meta tag. */ private static function draw_site_name_meta_tag() { $blog_name = get_bloginfo(); if ( ! empty( $blog_name ) ) { echo "\n"; } } /** * Draws Shareaholic site_id meta tag. */ private static function draw_site_id_meta_tag() { $site_id = ShareaholicUtilities::get_option( 'api_key' ); if ( ! empty( $site_id ) ) { echo "\n"; } } /** * Draws Shareaholic image meta tag. Will only run on pages or posts. */ private static function draw_image_meta_tag() { if ( in_array( ShareaholicUtilities::page_type(), array( 'page', 'post' ) ) ) { global $post; $thumbnail_src = ''; if ( is_attachment() ) { $thumbnail_src = wp_get_attachment_thumb_url(); } $thumbnail_src = ShareaholicUtilities::post_featured_image(); if ( $thumbnail_src == null ) { $thumbnail_src = ShareaholicUtilities::post_first_image(); } if ( $thumbnail_src != null ) { echo ""; } } } /** * Draws an open graph image meta tag if they are enabled and exist. Will only run on pages or posts. */ private static function draw_og_tags() { if ( in_array( ShareaholicUtilities::page_type(), array( 'page', 'post' ) ) ) { global $post; $thumbnail_src = ''; $settings = ShareaholicUtilities::get_settings(); if ( ! get_post_meta( $post->ID, 'shareaholic_disable_open_graph_tags', true ) && ( isset( $settings['disable_og_tags'] ) && $settings['disable_og_tags'] == 'off' ) ) { if ( is_attachment() ) { $thumbnail_src = wp_get_attachment_thumb_url(); } if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $post->ID ) ) { $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); if ( is_array( $thumbnail ) ) { $thumbnail_src = esc_attr( $thumbnail[0] ); } } if ( $thumbnail_src == null ) { $thumbnail_src = ShareaholicUtilities::post_first_image(); } if ( $thumbnail_src != null ) { echo "\n\n"; echo ""; echo "\n\n"; } } } } /** * This static function inserts the shareaholic canvas in a post * * @param string $content the WordPress content * @return string the content */ public static function draw_canvases( $content ) { global $wp_current_filter; // Don't add to get_the_excerpt because it's too early and strips tags (adding to the_excerpt is allowed) if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) { // Return early return $content; } if ( is_main_query() && in_the_loop() ) { $settings = ShareaholicUtilities::get_settings(); $page_type = ShareaholicUtilities::page_type(); // Don't allow this function to run more than once for a page load (prevent infinite loops) $has_run = false; foreach ( $wp_current_filter as $filter ) { if ( 'the_content' == $filter ) { if ( $has_run ) { // has already run once, return early return $content; } else { // first run, set flag! $has_run = true; } } } foreach ( array( 'share_buttons', 'recommendations' ) as $app ) { // check Excerpt prefs if ( 'the_excerpt' == current_filter() && isset( $settings[ "{$app}_display_on_excerpts" ] ) && $settings[ "{$app}_display_on_excerpts" ] == 'off' ) { // Return early return $content; } // In-Page App disabled for this singular post? $disabled = get_post_meta( get_the_ID(), "shareaholic_disable_{$app}", true ); $disabled = apply_filters( "shareaholic_disable_{$app}", $disabled ); // check individual post prefs if ( ! $disabled ) { // check if ABOVE location is turned on if ( isset( $settings[ $app ][ "{$page_type}_above_content" ] ) ) { $content = self::canvas( $app, null, "{$page_type}_above_content" ) . $content; } // check if BELOW location is turned on if ( isset( $settings[ $app ][ "{$page_type}_below_content" ] ) ) { $content .= self::canvas( $app, null, "{$page_type}_below_content" ); } } } } // something that uses the_content hook must return the $content return $content; } /** * Draws an individual canvas given a specific location * id and app. The app isn't strictly necessary, but is * being kept for now for backwards compatability. * This method was private, but was made public to be accessed * by the shortcode static function in global_functions.php. * * @param string $app the type of app * @param string $id the location id for configuration * @param string $id_name the location id name for configuration * @param string $title the title of URL * @param string $link url * @param string $summary summary text for URL */ public static function canvas( $app, $id = null, $id_name = null, $title = null, $link = null, $summary = null ) { global $post, $wp_query; $page_type = ShareaholicUtilities::page_type(); $is_list_page = $page_type == 'index' || $page_type == 'category'; $loop_start = did_action( 'loop_start' ); $loop_end = did_action( 'loop_end' ); $in_loop = $loop_start > $loop_end ? true : false; $link = trim( $link ); // Use the $post object to get the title, link, and summary only if the // title, link or summary is not provided AND one of the following is true: // - we are on a non list page // - we are in the WordPress loop if ( trim( $title ) == null && ( ! $is_list_page || $in_loop ) ) { $title = htmlspecialchars( $post->post_title, ENT_QUOTES ); } if ( trim( $link ) == null && ( ! $is_list_page || $in_loop ) ) { $link = get_permalink( $post->ID ); } if ( trim( $summary ) == null && ( ! $is_list_page || $in_loop ) ) { $summary = htmlspecialchars( strip_tags( strip_shortcodes( $post->post_excerpt ) ), ENT_QUOTES ); $summary = ShareaholicUtilities::truncate_text( $summary, 500 ); } $canvas = "