Example #1
0
function wpcom_amp_add_blavatar($metadata, $post)
{
    if (!function_exists('blavatar_domain')) {
        return $metadata;
    }
    if (!isset($metadata['publisher'])) {
        return $metadata;
    }
    if (isset($metadata['publisher']['logo'])) {
        return $metadata;
    }
    $size = 60;
    $blavatar_domain = blavatar_domain(site_url());
    if (blavatar_exists($blavatar_domain)) {
        $metadata['publisher']['logo'] = array('@type' => 'ImageObject', 'url' => blavatar_url($blavatar_domain, 'img', $size, false, true), 'width' => $size, 'height' => $size);
    }
    return $metadata;
}
function jetpack_og_get_image($width = 200, $height = 200, $max_images = 4)
{
    // Facebook requires thumbnails to be a minimum of 200x200
    $image = '';
    if (is_singular() && !is_home()) {
        global $post;
        $image = '';
        // Grab obvious image if $post is an attachment page for an image
        if (is_attachment($post->ID) && 'image' == substr($post->post_mime_type, 0, 5)) {
            $image = wp_get_attachment_url($post->ID);
        }
        // Attempt to find something good for this post using our generalized PostImages code
        if (!$image && class_exists('Jetpack_PostImages')) {
            $post_images = Jetpack_PostImages::get_images($post->ID, array('width' => $width, 'height' => $height));
            if ($post_images && !is_wp_error($post_images)) {
                $image = array();
                foreach ((array) $post_images as $post_image) {
                    $image['src'] = $post_image['src'];
                    if (isset($post_image['src_width'], $post_image['src_height'])) {
                        $image['width'] = $post_image['src_width'];
                        $image['height'] = $post_image['src_height'];
                    }
                }
            }
        }
    } else {
        if (is_author()) {
            $author = get_queried_object();
            if (function_exists('get_avatar_url')) {
                // Prefer the core function get_avatar_url() if available, WP 4.2+
                $image['src'] = get_avatar_url($author->user_email, array('size' => $width));
            } else {
                $has_filter = has_filter('pre_option_show_avatars', '__return_true');
                if (!$has_filter) {
                    add_filter('pre_option_show_avatars', '__return_true');
                }
                $avatar = get_avatar($author->user_email, $width);
                if (!$has_filter) {
                    remove_filter('pre_option_show_avatars', '__return_true');
                }
                if (!empty($avatar) && !is_wp_error($avatar)) {
                    if (preg_match('/src=["\']([^"\']+)["\']/', $avatar, $matches)) {
                    }
                    $image['src'] = wp_specialchars_decode($matches[1], ENT_QUOTES);
                }
            }
        }
    }
    if (empty($image)) {
        $image = array();
    } else {
        if (!is_array($image)) {
            $image = array('src' => $image);
        }
    }
    // First fall back, blavatar
    if (empty($image) && function_exists('blavatar_domain')) {
        $blavatar_domain = blavatar_domain(site_url());
        if (blavatar_exists($blavatar_domain)) {
            $image['src'] = blavatar_url($blavatar_domain, 'img', $width, false, true);
            $image['width'] = $width;
            $image['height'] = $height;
        }
    }
    // Second fall back, Site Logo
    if (empty($image) && (function_exists('jetpack_has_site_logo') && jetpack_has_site_logo())) {
        $image['src'] = jetpack_get_site_logo('url');
        $image_dimensions = jetpack_get_site_logo_dimensions();
        if (!empty($image_dimensions)) {
            $image['width'] = $image_dimensions['width'];
            $image['height'] = $image_dimensions['height'];
        }
    }
    // Third fall back, Site Icon
    if (empty($image) && (function_exists('jetpack_has_site_icon') && jetpack_has_site_icon())) {
        $image['src'] = jetpack_site_icon_url(null, '512');
        $image['width'] = '512';
        $image['height'] = '512';
    }
    // Fourth fall back, Core Site Icon. Added in WP 4.3.
    if (empty($image) && (function_exists('has_site_icon') && has_site_icon())) {
        $image['src'] = get_site_icon_url(null, '512');
    }
    // Finally fall back, blank image
    if (empty($image)) {
        /**
         * Filter the default Open Graph Image tag, used when no Image can be found in a post.
         *
         * @since 3.0.0
         *
         * @param string $str Default Image URL.
         */
        $image['src'] = apply_filters('jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg');
    }
    return $image;
}
function jetpack_og_get_image($width = 200, $height = 200, $max_images = 4)
{
    // Facebook requires thumbnails to be a minimum of 200x200
    $image = '';
    if (is_singular() && !is_home() && !is_front_page()) {
        global $post;
        $image = '';
        // Attempt to find something good for this post using our generalized PostImages code
        if (class_exists('Jetpack_PostImages')) {
            $post_images = Jetpack_PostImages::get_images($post->ID, array('width' => $width, 'height' => $height));
            if ($post_images && !is_wp_error($post_images)) {
                $image = array();
                foreach ((array) $post_images as $post_image) {
                    $image[] = $post_image['src'];
                }
            }
        }
    } else {
        if (is_author()) {
            $author = get_queried_object();
            if (function_exists('get_avatar_url')) {
                $avatar = get_avatar_url($author->user_email, $width);
                if (!empty($avatar)) {
                    if (is_array($avatar)) {
                        $image = $avatar[0];
                    } else {
                        $image = $avatar;
                    }
                }
            } else {
                $has_filter = has_filter('pre_option_show_avatars', '__return_true');
                if (!$has_filter) {
                    add_filter('pre_option_show_avatars', '__return_true');
                }
                $avatar = get_avatar($author->user_email, $width);
                if (!$has_filter) {
                    remove_filter('pre_option_show_avatars', '__return_true');
                }
                if (!empty($avatar) && !is_wp_error($avatar)) {
                    if (preg_match('/src=["\']([^"\']+)["\']/', $avatar, $matches)) {
                    }
                    $image = wp_specialchars_decode($matches[1], ENT_QUOTES);
                }
            }
        }
    }
    // Fallback to Blavatar if available
    if (function_exists('blavatar_domain')) {
        $blavatar_domain = blavatar_domain(site_url());
        if (empty($image) && blavatar_exists($blavatar_domain)) {
            $image = blavatar_url($blavatar_domain, 'img', $width);
        }
    }
    return $image;
}
 static function twitter_cards_define_type_based_on_image_count($og_tags, $extract)
 {
     $card_type = 'summary';
     $img_count = $extract['count']['image'];
     if (empty($img_count)) {
         // No images, use Blavatar as a thumbnail for the summary type.
         if (function_exists('blavatar_domain')) {
             $blavatar_domain = blavatar_domain(site_url());
             if (blavatar_exists($blavatar_domain)) {
                 $og_tags['twitter:image'] = blavatar_url($blavatar_domain, 'img', 240);
             }
         }
         // Not falling back on Gravatar, because there's no way to know if we end up with an auto-generated one.
     } else {
         if (1 == $img_count && ('image' == $extract['type'] || 'gallery' == $extract['type'])) {
             // 1 image = photo
             // Test for $extract['type'] to limit to image and gallery, so we don't send a potential fallback image like a Gravatar as a photo post.
             $card_type = 'photo';
             $og_tags['twitter:image'] = add_query_arg('w', 1400, empty($extract['images']) ? $extract['image'] : $extract['images'][0]['url']);
         } else {
             if ($img_count <= 3) {
                 // 2-3 images = summary with small thumbnail
                 $og_tags['twitter:image'] = add_query_arg('w', 240, empty($extract['images']) ? $extract['image'] : $extract['images'][0]['url']);
             } else {
                 if ($img_count >= 4) {
                     // >= 4 images = gallery
                     $card_type = 'gallery';
                     $og_tags = self::twitter_cards_gallery($extract, $og_tags);
                 }
             }
         }
     }
     return array($og_tags, $card_type);
 }
 /**
  * @param    int $post_id The post ID to check
  * @param    int $size
  * @return Array containing details of the image, or empty array if none.
  */
 static function from_blavatar($post_id, $size = 96)
 {
     if (!function_exists('blavatar_domain') || !function_exists('blavatar_exists') || !function_exists('blavatar_url')) {
         return array();
     }
     $permalink = get_permalink($post_id);
     $domain = blavatar_domain($permalink);
     if (!blavatar_exists($domain)) {
         return array();
     }
     $url = blavatar_url($domain, 'img', $size);
     return array(array('type' => 'image', 'from' => 'blavatar', 'src' => $url, 'src_width' => $size, 'src_height' => $size, 'href' => $permalink));
 }
 /**
  * @param    int $post_id The post ID to check
  * @param    int $size
  * @return Array containing details of the image, or empty array if none.
  */
 static function from_blavatar($post_id, $size = 96)
 {
     $permalink = get_permalink($post_id);
     if (function_exists('blavatar_domain') && function_exists('blavatar_exists') && function_exists('blavatar_url')) {
         $domain = blavatar_domain($permalink);
         if (!blavatar_exists($domain)) {
             return array();
         }
         $url = blavatar_url($domain, 'img', $size);
     } elseif (function_exists('jetpack_has_site_icon') && jetpack_has_site_icon()) {
         $url = jetpack_site_icon_url(null, $size, $default = false);
     } else {
         return array();
     }
     return array(array('type' => 'image', 'from' => 'blavatar', 'src' => $url, 'src_width' => $size, 'src_height' => $size, 'href' => $permalink));
 }
 static function twitter_cards_define_type_based_on_image_count($og_tags, $extract)
 {
     $card_type = 'summary';
     $img_count = $extract['count']['image'];
     if (empty($img_count)) {
         // No images, use Blavatar as a thumbnail for the summary type.
         if (function_exists('blavatar_domain')) {
             $blavatar_domain = blavatar_domain(site_url());
             if (blavatar_exists($blavatar_domain)) {
                 $og_tags['twitter:image'] = blavatar_url($blavatar_domain, 'img', 240);
             }
         }
         // Second fall back, Site Logo
         if (empty($og_tags['twitter:image']) && (function_exists('jetpack_has_site_logo') && jetpack_has_site_logo())) {
             $og_tags['twitter:image'] = jetpack_get_site_logo('url');
         }
         // Third fall back, Site Icon
         if (empty($og_tags['twitter:image']) && (function_exists('jetpack_has_site_icon') && jetpack_has_site_icon())) {
             $og_tags['twitter:image'] = jetpack_site_icon_url(null, '240');
         }
         // Not falling back on Gravatar, because there's no way to know if we end up with an auto-generated one.
     } elseif ($img_count && ('image' == $extract['type'] || 'gallery' == $extract['type'])) {
         // Test for $extract['type'] to limit to image and gallery, so we don't send a potential fallback image like a Gravatar as a photo post.
         $card_type = 'summary_large_image';
         $og_tags['twitter:image'] = add_query_arg('w', 1400, empty($extract['images']) ? $extract['image'] : $extract['images'][0]['url']);
     }
     return array($og_tags, $card_type);
 }
 /**
  * Collects the necessary information to return for a site's response.
  *
  * @return (array)
  */
 public function build_current_site_response()
 {
     global $wpdb, $wp_version;
     $response_format = self::$site_format;
     $is_user_logged_in = is_user_logged_in();
     $visible = array();
     if ($is_user_logged_in) {
         $current_user = wp_get_current_user();
         $visible = get_user_meta($current_user->ID, 'blog_visibility', true);
         if (!is_array($visible)) {
             $visible = array();
         }
     }
     $blog_id = (int) $this->api->get_blog_id_for_output();
     /** This filter is documented in class.json-api-endpoints.php */
     $is_jetpack = true === apply_filters('is_jetpack_site', false, $blog_id);
     $site_url = get_option('siteurl');
     if ($is_jetpack) {
         remove_filter('option_stylesheet', 'fix_theme_location');
         if ('https' !== parse_url($site_url, PHP_URL_SCHEME)) {
             add_filter('set_url_scheme', array($this, 'force_http'), 10, 3);
         }
     }
     foreach (array_keys($response_format) as $key) {
         switch ($key) {
             case 'ID':
                 $response[$key] = $blog_id;
                 break;
             case 'name':
                 $response[$key] = (string) htmlspecialchars_decode(get_bloginfo('name'), ENT_QUOTES);
                 break;
             case 'description':
                 $response[$key] = (string) htmlspecialchars_decode(get_bloginfo('description'), ENT_QUOTES);
                 break;
             case 'URL':
                 $response[$key] = (string) home_url();
                 break;
             case 'jetpack':
                 $response[$key] = $is_jetpack;
                 // jetpack magic affects this value
                 break;
             case 'is_private':
                 if (defined('IS_WPCOM') && IS_WPCOM) {
                     $public_setting = get_option('blog_public');
                     if (-1 == $public_setting) {
                         $response[$key] = true;
                     } else {
                         $response[$key] = false;
                     }
                 } else {
                     $response[$key] = false;
                     // magic
                 }
                 break;
             case 'visible':
                 if ($is_user_logged_in) {
                     $is_visible = true;
                     if (isset($visible[$blog_id])) {
                         $is_visible = (bool) $visible[$blog_id];
                     }
                     // null and true are visible
                     $response[$key] = $is_visible;
                 }
                 break;
             case 'post_count':
                 if ($is_user_logged_in) {
                     $response[$key] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = 'publish'");
                 }
                 break;
             case 'lang':
                 if ($is_user_logged_in) {
                     $response[$key] = (string) get_bloginfo('language');
                 }
                 break;
             case 'icon':
                 if (function_exists('blavatar_domain') && function_exists('blavatar_exists') && function_exists('blavatar_url')) {
                     $domain = blavatar_domain(home_url());
                     if (blavatar_exists($domain)) {
                         $response[$key] = array('img' => (string) remove_query_arg('s', blavatar_url($domain, 'img')), 'ico' => (string) remove_query_arg('s', blavatar_url($domain, 'ico')));
                     } else {
                         // This is done so that we can access the updated blavatar on .com via the /me/sites endpoint
                         if (is_jetpack_site()) {
                             $site_icon_url = get_option('jetpack_site_icon_url');
                             if ($site_icon_url) {
                                 $response[$key] = array('img' => (string) jetpack_photon_url($site_icon_url, array(), 'https'), 'ico' => (string) jetpack_photon_url($site_icon_url, array('w' => 16), 'https'));
                             }
                         }
                     }
                 } elseif (function_exists('jetpack_site_icon_url') && function_exists('jetpack_photon_url')) {
                     $response[$key] = array('img' => (string) jetpack_photon_url(jetpack_site_icon_url(get_current_blog_id(), 80), array('w' => 80), 'https'), 'ico' => (string) jetpack_photon_url(jetpack_site_icon_url(get_current_blog_id(), 16), array('w' => 16), 'https'));
                 }
                 break;
             case 'logo':
                 // Set an empty response array.
                 $response[$key] = array('id' => (int) 0, 'sizes' => array(), 'url' => '');
                 // Get current site logo values.
                 $logo = get_option('site_logo');
                 // Update the response array if there's a site logo currenty active.
                 if ($logo && 0 != $logo['id']) {
                     $response[$key]['id'] = $logo['id'];
                     $response[$key]['url'] = $logo['url'];
                     foreach ($logo['sizes'] as $size => $properties) {
                         $response[$key]['sizes'][$size] = $properties;
                     }
                 }
                 break;
             case 'subscribers_count':
                 if (function_exists('wpcom_subs_total_wpcom_subscribers')) {
                     $total_wpcom_subs = wpcom_subs_total_wpcom_subscribers(array('blog_id' => $blog_id));
                     $response[$key] = $total_wpcom_subs;
                 } else {
                     $response[$key] = 0;
                     // magic
                 }
                 break;
             case 'is_following':
                 $response[$key] = (bool) $this->api->is_following($blog_id);
                 break;
             case 'options':
                 // Figure out if the blog supports VideoPress, have to do some extra checking for JP blogs
                 $has_videopress = false;
                 if (get_option('video_upgrade') == '1') {
                     $has_videopress = true;
                 } else {
                     if (class_exists('Jetpack_Options')) {
                         $videopress = Jetpack_Options::get_option('videopress', array());
                         if (isset($videopress['blog_id']) && $videopress['blog_id'] > 0) {
                             $has_videopress = true;
                         }
                     }
                 }
                 // deprecated - see separate endpoint. get a list of supported post formats
                 $all_formats = get_post_format_strings();
                 $supported = get_theme_support('post-formats');
                 $supported_formats = array();
                 if (isset($supported[0])) {
                     foreach ($supported[0] as $format) {
                         $supported_formats[$format] = $all_formats[$format];
                     }
                 }
                 // determine if sharing buttons should be visible by default
                 $default_sharing_status = false;
                 if (class_exists('Sharing_Service')) {
                     $ss = new Sharing_Service();
                     $blog_services = $ss->get_blog_services();
                     $default_sharing_status = !empty($blog_services['visible']);
                 }
                 $is_mapped_domain = false;
                 if (function_exists('get_primary_redirect')) {
                     $primary_redirect = strtolower(get_primary_redirect());
                     if (false === strpos($primary_redirect, '.wordpress.com')) {
                         $is_mapped_domain = true;
                     }
                 }
                 $is_redirect = false;
                 if (function_exists('get_primary_domain_mapping_record')) {
                     if (get_primary_domain_mapping_record()->type == 1) {
                         $is_redirect = true;
                     }
                 }
                 if (function_exists('get_mime_types')) {
                     $allowed_file_types = get_mime_types();
                 } else {
                     // http://codex.wordpress.org/Uploading_Files
                     $mime_types = get_allowed_mime_types();
                     foreach ($mime_types as $type => $mime_type) {
                         $extras = explode('|', $type);
                         foreach ($extras as $extra) {
                             $allowed_file_types[] = $extra;
                         }
                     }
                 }
                 if (function_exists('get_blog_details')) {
                     $blog_details = get_blog_details();
                     if (!empty($blog_details->registered)) {
                         $registered_date = $blog_details->registered;
                     }
                 }
                 $upgraded_filetypes_enabled = false;
                 if ($is_jetpack || get_option('use_upgraded_upload_filetypes')) {
                     $upgraded_filetypes_enabled = true;
                 }
                 $wordads = false;
                 if (function_exists('has_any_blog_stickers')) {
                     $wordads = has_any_blog_stickers(array('wordads-approved', 'wordads-approved-misfits'), $blog_id);
                 }
                 $response[$key] = array('timezone' => (string) get_option('timezone_string'), 'gmt_offset' => (double) get_option('gmt_offset'), 'videopress_enabled' => $has_videopress, 'upgraded_filetypes_enabled' => $upgraded_filetypes_enabled, 'login_url' => wp_login_url(), 'admin_url' => get_admin_url(), 'is_mapped_domain' => $is_mapped_domain, 'is_redirect' => $is_redirect, 'unmapped_url' => get_site_url($blog_id), 'featured_images_enabled' => current_theme_supports('post-thumbnails'), 'theme_slug' => get_option('stylesheet'), 'header_image' => get_theme_mod('header_image_data'), 'background_color' => get_theme_mod('background_color'), 'image_default_link_type' => get_option('image_default_link_type'), 'image_thumbnail_width' => (int) get_option('thumbnail_size_w'), 'image_thumbnail_height' => (int) get_option('thumbnail_size_h'), 'image_thumbnail_crop' => get_option('thumbnail_crop'), 'image_medium_width' => (int) get_option('medium_size_w'), 'image_medium_height' => (int) get_option('medium_size_h'), 'image_large_width' => (int) get_option('large_size_w'), 'image_large_height' => (int) get_option('large_size_h'), 'permalink_structure' => get_option('permalink_structure'), 'post_formats' => $supported_formats, 'default_post_format' => get_option('default_post_format'), 'default_category' => (int) get_option('default_category'), 'allowed_file_types' => $allowed_file_types, 'show_on_front' => get_option('show_on_front'), 'default_likes_enabled' => (bool) apply_filters('wpl_is_enabled_sitewide', !get_option('disabled_likes')), 'default_sharing_status' => (bool) $default_sharing_status, 'default_comment_status' => 'closed' == get_option('default_comment_status') ? false : true, 'default_ping_status' => 'closed' == get_option('default_ping_status') ? false : true, 'software_version' => $wp_version, 'created_at' => !empty($registered_date) ? $this->format_date($registered_date) : '0000-00-00T00:00:00+00:00', 'wordads' => $wordads);
                 if ('page' === get_option('show_on_front')) {
                     $response['options']['page_on_front'] = (int) get_option('page_on_front');
                     $response['options']['page_for_posts'] = (int) get_option('page_for_posts');
                 }
                 if ($is_jetpack) {
                     $response['options']['jetpack_version'] = get_option('jetpack_version');
                     if (get_option('jetpack_main_network_site')) {
                         $response['options']['main_network_site'] = (string) rtrim(get_option('jetpack_main_network_site'), '/');
                     }
                     if (is_array(Jetpack_Options::get_option('active_modules'))) {
                         $response['options']['active_modules'] = (array) array_values(Jetpack_Options::get_option('active_modules'));
                     }
                     if ($jetpack_wp_version = get_option('jetpack_wp_version')) {
                         $response['options']['software_version'] = (string) $jetpack_wp_version;
                     } else {
                         if ($jetpack_update = get_option('jetpack_updates')) {
                             if (is_array($jetpack_update) && isset($jetpack_update['wp_version'])) {
                                 $response['options']['software_version'] = (string) $jetpack_update['wp_version'];
                             } else {
                                 $response['options']['software_version'] = null;
                             }
                         } else {
                             $response['options']['software_version'] = null;
                         }
                     }
                     $response['options']['max_upload_size'] = get_option('jetpack_max_upload_size', false);
                     // Sites have to prove that they are not main_network site.
                     // If the sync happends right then we should be able to see that we are not dealing with a network site
                     $response['options']['is_multi_network'] = (bool) get_option('jetpack_is_main_network', true);
                     $response['options']['is_multi_site'] = (bool) get_option('jetpack_is_multi_site', true);
                 }
                 if (!current_user_can('edit_posts')) {
                     unset($response[$key]);
                 }
                 break;
             case 'meta':
                 /**
                  * Filters the URL scheme used when querying your site's REST API endpoint.
                  *
                  * @module json-api
                  *
                  * @since 3.2.0
                  *
                  * @param string parse_url( get_option( 'home' ), PHP_URL_SCHEME ) URL scheme parsed from home URL.
                  */
                 $xmlrpc_scheme = apply_filters('wpcom_json_api_xmlrpc_scheme', parse_url(get_option('home'), PHP_URL_SCHEME));
                 $xmlrpc_url = site_url('xmlrpc.php', $xmlrpc_scheme);
                 $response[$key] = (object) array('links' => (object) array('self' => (string) $this->get_site_link($blog_id), 'help' => (string) $this->get_site_link($blog_id, 'help'), 'posts' => (string) $this->get_site_link($blog_id, 'posts/'), 'comments' => (string) $this->get_site_link($blog_id, 'comments/'), 'xmlrpc' => (string) $xmlrpc_url));
                 break;
         }
     }
     if ($is_jetpack) {
         // Add the updates only make them visible if the user has manage options permission.
         $jetpack_update = (array) get_option('jetpack_updates');
         if (!empty($jetpack_update) && current_user_can('manage_options')) {
             if (isset($jetpack_update['wp_version'])) {
                 // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates
                 unset($jetpack_update['wp_version']);
             }
             if (isset($jetpack_update['site_is_version_controlled'])) {
                 // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates
                 unset($jetpack_update['site_is_version_controlled']);
             }
             $response['updates'] = (array) $jetpack_update;
         }
         add_filter('option_stylesheet', 'fix_theme_location');
         if ('https' !== parse_url($site_url, PHP_URL_SCHEME)) {
             remove_filter('set_url_scheme', array($this, 'force_http'), 10, 3);
         }
     }
     return $response;
 }
 /**
  * Collects the necessary information to return for a site's response.
  *
  * @return (array)
  */
 public function build_current_site_response()
 {
     global $wpdb, $wp_version;
     $response_format = self::$site_format;
     $is_user_logged_in = is_user_logged_in();
     $visible = array();
     if ($is_user_logged_in) {
         $current_user = wp_get_current_user();
         $visible = get_user_meta($current_user->ID, 'blog_visibility', true);
         if (!is_array($visible)) {
             $visible = array();
         }
     }
     $blog_id = (int) $this->api->get_blog_id_for_output();
     $is_jetpack = true === apply_filters('is_jetpack_site', false, $blog_id);
     $site_url = get_option('siteurl');
     if ($is_jetpack) {
         remove_filter('option_stylesheet', 'fix_theme_location');
         if ('https' !== parse_url($site_url, PHP_URL_SCHEME)) {
             add_filter('set_url_scheme', array($this, 'force_http'), 10, 3);
         }
     }
     foreach (array_keys($response_format) as $key) {
         switch ($key) {
             case 'ID':
                 $response[$key] = $blog_id;
                 break;
             case 'name':
                 $response[$key] = (string) htmlspecialchars_decode(get_bloginfo('name'), ENT_QUOTES);
                 break;
             case 'description':
                 $response[$key] = (string) htmlspecialchars_decode(get_bloginfo('description'), ENT_QUOTES);
                 break;
             case 'URL':
                 $response[$key] = (string) home_url();
                 break;
             case 'jetpack':
                 $response[$key] = $is_jetpack;
                 // jetpack magic affects this value
                 break;
             case 'is_private':
                 if (defined('IS_WPCOM') && IS_WPCOM) {
                     $public_setting = get_option('blog_public');
                     if (-1 == $public_setting) {
                         $response[$key] = true;
                     } else {
                         $response[$key] = false;
                     }
                 } else {
                     $response[$key] = false;
                     // magic
                 }
                 break;
             case 'visible':
                 if ($is_user_logged_in) {
                     $is_visible = true;
                     if (isset($visible[$blog_id])) {
                         $is_visible = $visible[$blog_id];
                     }
                     // null and true are visible
                     $response[$key] = $is_visible;
                 }
                 break;
             case 'post_count':
                 if ($is_user_logged_in) {
                     $response[$key] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = 'publish'");
                 }
                 break;
             case 'lang':
                 if ($is_user_logged_in) {
                     $response[$key] = (string) get_bloginfo('language');
                 }
                 break;
             case 'icon':
                 if (function_exists('blavatar_domain') && function_exists('blavatar_exists') && function_exists('blavatar_url')) {
                     $domain = blavatar_domain(home_url());
                     if (blavatar_exists($domain)) {
                         $response[$key] = array('img' => (string) remove_query_arg('s', blavatar_url($domain, 'img')), 'ico' => (string) remove_query_arg('s', blavatar_url($domain, 'ico')));
                     }
                 } elseif (function_exists('jetpack_site_icon_url') && function_exists('jetpack_photon_url')) {
                     $response[$key] = array('img' => (string) jetpack_photon_url(jetpack_site_icon_url(get_current_blog_id(), 80), array('w' => 80), 'https'), 'ico' => (string) jetpack_photon_url(jetpack_site_icon_url(get_current_blog_id(), 16), array('w' => 16), 'https'));
                 }
                 break;
             case 'logo':
                 // Set an empty response array.
                 $response[$key] = array('id' => (int) 0, 'sizes' => array(), 'url' => '');
                 // Get current site logo values.
                 $logo = get_option('site_logo');
                 // Update the response array if there's a site logo currenty active.
                 if ($logo && 0 != $logo['id']) {
                     $response[$key]['id'] = $logo['id'];
                     $response[$key]['url'] = $logo['url'];
                     foreach ($logo['sizes'] as $size => $properties) {
                         $response[$key]['sizes'][$size] = $properties;
                     }
                 }
                 break;
             case 'subscribers_count':
                 if (function_exists('wpcom_subs_total_wpcom_subscribers')) {
                     $total_wpcom_subs = wpcom_subs_total_wpcom_subscribers(array('blog_id' => $blog_id));
                     $response[$key] = $total_wpcom_subs;
                 } else {
                     $response[$key] = 0;
                     // magic
                 }
                 break;
             case 'is_following':
                 $response[$key] = (bool) $this->api->is_following($blog_id);
                 break;
             case 'options':
                 // Figure out if the blog supports VideoPress, have to do some extra checking for JP blogs
                 $has_videopress = false;
                 if (get_option('video_upgrade') == '1') {
                     $has_videopress = true;
                 } else {
                     if (class_exists('Jetpack_Options')) {
                         $videopress = Jetpack_Options::get_option('videopress', array());
                         if ($videopress['blog_id'] > 0) {
                             $has_videopress = true;
                         }
                     }
                 }
                 // deprecated - see separate endpoint. get a list of supported post formats
                 $all_formats = get_post_format_strings();
                 $supported = get_theme_support('post-formats');
                 $supported_formats = array();
                 if (isset($supported[0])) {
                     foreach ($supported[0] as $format) {
                         $supported_formats[$format] = $all_formats[$format];
                     }
                 }
                 // determine if sharing buttons should be visible by default
                 $default_sharing_status = false;
                 if (class_exists('Sharing_Service')) {
                     $ss = new Sharing_Service();
                     $blog_services = $ss->get_blog_services();
                     $default_sharing_status = !empty($blog_services['visible']);
                 }
                 $is_mapped_domain = false;
                 if (function_exists('get_primary_redirect')) {
                     $primary_redirect = strtolower(get_primary_redirect());
                     if (false === strpos($primary_redirect, '.wordpress.com')) {
                         $is_mapped_domain = true;
                     }
                 }
                 if (function_exists('get_mime_types')) {
                     $allowed_file_types = get_mime_types();
                 } else {
                     // http://codex.wordpress.org/Uploading_Files
                     $mime_types = get_allowed_mime_types();
                     foreach ($mime_types as $type => $mime_type) {
                         $extras = explode('|', $type);
                         foreach ($extras as $extra) {
                             $allowed_file_types[] = $extra;
                         }
                     }
                 }
                 $response[$key] = array('timezone' => (string) get_option('timezone_string'), 'gmt_offset' => (double) get_option('gmt_offset'), 'videopress_enabled' => $has_videopress, 'login_url' => wp_login_url(), 'admin_url' => get_admin_url(), 'is_mapped_domain' => $is_mapped_domain, 'unmapped_url' => get_site_url($blog_id), 'featured_images_enabled' => current_theme_supports('post-thumbnails'), 'header_image' => get_theme_mod('header_image_data'), 'background_color' => get_theme_mod('background_color'), 'image_default_link_type' => get_option('image_default_link_type'), 'image_thumbnail_width' => (int) get_option('thumbnail_size_w'), 'image_thumbnail_height' => (int) get_option('thumbnail_size_h'), 'image_thumbnail_crop' => get_option('thumbnail_crop'), 'image_medium_width' => (int) get_option('medium_size_w'), 'image_medium_height' => (int) get_option('medium_size_h'), 'image_large_width' => (int) get_option('large_size_w'), 'image_large_height' => (int) get_option('large_size_h'), 'post_formats' => $supported_formats, 'allowed_file_types' => $allowed_file_types, 'default_likes_enabled' => (bool) apply_filters('wpl_is_enabled_sitewide', !get_option('disabled_likes')), 'default_sharing_status' => (bool) $default_sharing_status, 'default_comment_status' => 'closed' == get_option('default_comment_status') ? false : true, 'default_ping_status' => 'closed' == get_option('default_ping_status') ? false : true, 'software_version' => $wp_version);
                 if (!current_user_can('edit_posts')) {
                     unset($response[$key]);
                 }
                 break;
             case 'meta':
                 $xmlrpc_scheme = apply_filters('wpcom_json_api_xmlrpc_scheme', parse_url(get_option('home'), PHP_URL_SCHEME));
                 $xmlrpc_url = site_url('xmlrpc.php', $xmlrpc_scheme);
                 $response[$key] = (object) array('links' => (object) array('self' => (string) $this->get_site_link($blog_id), 'help' => (string) $this->get_site_link($blog_id, 'help'), 'posts' => (string) $this->get_site_link($blog_id, 'posts/'), 'comments' => (string) $this->get_site_link($blog_id, 'comments/'), 'xmlrpc' => (string) $xmlrpc_url));
                 break;
         }
     }
     if ($is_jetpack) {
         add_filter('option_stylesheet', 'fix_theme_location');
         if ('https' !== parse_url($site_url, PHP_URL_SCHEME)) {
             remove_filter('set_url_scheme', array($this, 'force_http'), 10, 3);
         }
     }
     return $response;
 }
 /**
  * Collects the necessary information to return for a site's response.
  *
  * @return (array)
  */
 public function build_current_site_response()
 {
     global $wpdb, $wp_version;
     $response_format = self::$site_format;
     $is_user_logged_in = is_user_logged_in();
     $visible = array();
     if ($is_user_logged_in) {
         $current_user = wp_get_current_user();
         $visible = get_user_meta($current_user->ID, 'blog_visibility', true);
         if (!is_array($visible)) {
             $visible = array();
         }
     }
     $blog_id = (int) $this->api->get_blog_id_for_output();
     foreach (array_keys($response_format) as $key) {
         switch ($key) {
             case 'ID':
                 $response[$key] = $blog_id;
                 break;
             case 'name':
                 $response[$key] = (string) htmlspecialchars_decode(get_bloginfo('name'), ENT_QUOTES);
                 break;
             case 'description':
                 $response[$key] = (string) htmlspecialchars_decode(get_bloginfo('description'), ENT_QUOTES);
                 break;
             case 'URL':
                 $response[$key] = (string) home_url();
                 break;
             case 'jetpack':
                 $response[$key] = false;
                 // magic
                 break;
             case 'is_private':
                 if (defined('IS_WPCOM') && IS_WPCOM) {
                     $public_setting = get_option('blog_public');
                     if (-1 == $public_setting) {
                         $response[$key] = true;
                     } else {
                         $response[$key] = false;
                     }
                 } else {
                     $response[$key] = false;
                     // magic
                 }
                 break;
             case 'visible':
                 if ($is_user_logged_in) {
                     $is_visible = true;
                     if (isset($visible[$blog_id])) {
                         $is_visible = $visible[$blog_id];
                     }
                     // null and true are visible
                     $response[$key] = $is_visible;
                 }
                 break;
             case 'post_count':
                 if ($is_user_logged_in) {
                     $response[$key] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = 'publish'");
                 }
                 break;
             case 'lang':
                 if ($is_user_logged_in) {
                     $response[$key] = (string) get_bloginfo('language');
                 }
                 break;
             case 'icon':
                 if (function_exists('blavatar_domain') && function_exists('blavatar_exists') && function_exists('blavatar_url')) {
                     $domain = blavatar_domain(home_url());
                     if (blavatar_exists($domain)) {
                         $response[$key] = array('img' => (string) remove_query_arg('s', blavatar_url($domain, 'img')), 'ico' => (string) remove_query_arg('s', blavatar_url($domain, 'ico')));
                     }
                 }
                 break;
             case 'subscribers_count':
                 if (function_exists('wpcom_subs_total_wpcom_subscribers')) {
                     $total_wpcom_subs = wpcom_subs_total_wpcom_subscribers(array('blog_id' => $blog_id));
                     $response[$key] = $total_wpcom_subs;
                 } else {
                     $response[$key] = 0;
                     // magic
                 }
                 break;
             case 'is_following':
                 $response[$key] = (bool) $this->api->is_following($blog_id);
                 break;
             case 'options':
                 // Figure out if the blog supports VideoPress, have to do some extra checking for JP blogs
                 $has_videopress = false;
                 if (get_option('video_upgrade') == '1') {
                     $has_videopress = true;
                 } else {
                     if (class_exists('Jetpack_Options')) {
                         $videopress = Jetpack_Options::get_option('videopress', array());
                         if ($videopress['blog_id'] > 0) {
                             $has_videopress = true;
                         }
                     }
                 }
                 // Get a list of supported post formats
                 $all_formats = get_post_format_strings();
                 $supported = get_theme_support('post-formats');
                 $supported_formats = array();
                 if (isset($supported[0])) {
                     foreach ($supported[0] as $format) {
                         $supported_formats[$format] = $all_formats[$format];
                     }
                 }
                 // determine if sharing buttons should be visible by default
                 $default_sharing_status = false;
                 if (class_exists('Sharing_Service')) {
                     $ss = new Sharing_Service();
                     $blog_services = $ss->get_blog_services();
                     $default_sharing_status = !empty($blog_services['visible']);
                 }
                 $response[$key] = array('timezone' => (string) get_option('timezone_string'), 'gmt_offset' => (double) get_option('gmt_offset'), 'videopress_enabled' => $has_videopress, 'login_url' => wp_login_url(), 'admin_url' => get_admin_url(), 'featured_images_enabled' => current_theme_supports('post-thumbnails'), 'header_image' => get_theme_mod('header_image_data'), 'image_default_link_type' => get_option('image_default_link_type'), 'image_thumbnail_width' => (int) get_option('thumbnail_size_w'), 'image_thumbnail_height' => (int) get_option('thumbnail_size_h'), 'image_thumbnail_crop' => get_option('thumbnail_crop'), 'image_medium_width' => (int) get_option('medium_size_w'), 'image_medium_height' => (int) get_option('medium_size_h'), 'image_large_width' => (int) get_option('large_size_w'), 'image_large_height' => (int) get_option('large_size_h'), 'post_formats' => $supported_formats, 'default_likes_enabled' => (bool) apply_filters('wpl_is_enabled_sitewide', !get_option('disabled_likes')), 'default_sharing_status' => (bool) $default_sharing_status, 'default_comment_status' => 'closed' == get_option('default_comment_status') ? false : true, 'default_ping_status' => 'closed' == get_option('default_ping_status') ? false : true, 'software_version' => $wp_version);
                 if (!current_user_can('publish_posts')) {
                     unset($response[$key]);
                 }
                 break;
             case 'meta':
                 $xmlrpc_url = site_url('xmlrpc.php');
                 $response[$key] = (object) array('links' => (object) array('self' => (string) $this->get_site_link($blog_id), 'help' => (string) $this->get_site_link($blog_id, 'help'), 'posts' => (string) $this->get_site_link($blog_id, 'posts/'), 'comments' => (string) $this->get_site_link($blog_id, 'comments/'), 'xmlrpc' => (string) $xmlrpc_url));
                 break;
         }
     }
     return $response;
 }
function jetpack_og_get_image($width = 200, $height = 200, $max_images = 4)
{
    // Facebook requires thumbnails to be a minimum of 200x200
    $image = '';
    if (is_singular() && !is_home()) {
        global $post;
        $image = '';
        // Attempt to find something good for this post using our generalized PostImages code
        if (class_exists('Jetpack_PostImages')) {
            $post_images = Jetpack_PostImages::get_images($post->ID, array('width' => $width, 'height' => $height));
            if ($post_images && !is_wp_error($post_images)) {
                $image = array();
                foreach ((array) $post_images as $post_image) {
                    $image[] = $post_image['src'];
                }
            }
        }
    } else {
        if (is_author()) {
            $author = get_queried_object();
            if (function_exists('get_avatar_url')) {
                // Prefer the core function get_avatar_url() if available, WP 4.2+
                $image = get_avatar_url($author->user_email, array('size' => $width));
            } else {
                $has_filter = has_filter('pre_option_show_avatars', '__return_true');
                if (!$has_filter) {
                    add_filter('pre_option_show_avatars', '__return_true');
                }
                $avatar = get_avatar($author->user_email, $width);
                if (!$has_filter) {
                    remove_filter('pre_option_show_avatars', '__return_true');
                }
                if (!empty($avatar) && !is_wp_error($avatar)) {
                    if (preg_match('/src=["\']([^"\']+)["\']/', $avatar, $matches)) {
                    }
                    $image = wp_specialchars_decode($matches[1], ENT_QUOTES);
                }
            }
        }
    }
    if (empty($image)) {
        $image = array();
    } else {
        if (!is_array($image)) {
            $image = array($image);
        }
    }
    // First fall back, blavatar
    if (empty($image) && function_exists('blavatar_domain')) {
        $blavatar_domain = blavatar_domain(site_url());
        if (blavatar_exists($blavatar_domain)) {
            $image[] = blavatar_url($blavatar_domain, 'img', $width);
        }
    }
    // Second fall back, Site Logo
    if (empty($image) && (function_exists('jetpack_has_site_logo') && jetpack_has_site_logo())) {
        $image[] = jetpack_get_site_logo('url');
    }
    // Third fall back, Site Icon
    if (empty($image) && (function_exists('jetpack_has_site_icon') && jetpack_has_site_icon())) {
        $image[] = jetpack_site_icon_url(null, '512');
    }
    // Fourth fall back, blank image
    if (empty($image)) {
        $image[] = apply_filters('jetpack_open_graph_image_default', 'https://s0.wp.com/i/blank.jpg');
    }
    return $image;
}
Example #12
0
?>
</head>
<body>
<div id="wrapper">
	
	<div id="masthead" class="fix">
		<h1><a href="<?php 
echo get_settings('home');
?>
/"><?php 
bloginfo('name');
?>
</a></h1>
		<div id="blogLead">
			<?php 
if (blavatar_exists(blavatar_current_domain())) {
    ?>
				<img src="<?php 
    echo clean_url(blavatar_url(blavatar_current_domain(), 'img', 48, false, 'invalidate-cache'));
    ?>
" alt=""/>
			<?php 
} else {
    ?>
				<img src="<?php 
    bloginfo('template_directory');
    ?>
/images/avatar.png?<?php 
    echo time();
    ?>
" alt="Icon" />