get_content_width() 공개 정적인 메소드

Get $content_width, but with a twist filter.
public static get_content_width ( )
예제 #1
0
/**
 * Vine embed code:
 * <iframe class="vine-embed" src="https://vine.co/v/bjHh0zHdgZT" width="600" height="600" frameborder="0"></iframe>
 * <script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>
 *
 * URL example:
 * https://vine.co/v/bjHh0zHdgZT/
 *
 * Embed shortcode examples:
 * [embed]https://vine.co/v/bjHh0zHdgZT[/embed]
 * [embed width="300"]https://vine.co/v/bjHh0zHdgZT[/embed]
 * [embed type="postcard" width="300"]https://vine.co/v/bjHh0zHdgZT[/embed]
 **/
function vine_embed_video($matches, $attr, $url, $rawattr)
{
    static $vine_flag_embedded_script;
    $max_height = 300;
    $type = 'simple';
    // Only allow 'postcard' or 'simple' types
    if (isset($rawattr['type']) && $rawattr['type'] === 'postcard') {
        $type = 'postcard';
    }
    $vine_size = Jetpack::get_content_width();
    // If the user enters a value for width or height, we ignore the Jetpack::get_content_width()
    if (isset($rawattr['width']) || isset($rawattr['height'])) {
        // 300 is the minimum size that Vine provides for embeds. Lower than that, the postcard embeds looks weird.
        $vine_size = max($max_height, min($attr['width'], $attr['height']));
    }
    if (empty($vine_size)) {
        $vine_size = $max_height;
    }
    $url = 'https://vine.co/v/' . $matches[1] . '/embed/' . $type;
    $vine_html = sprintf('<span class="embed-vine" style="display: block;"><iframe class="vine-embed" src="%s" width="%s" height="%s" frameborder="0"></iframe></span>', esc_url($url), (int) $vine_size, (int) $vine_size);
    if ($vine_flag_embedded_script !== true) {
        $vine_html .= '<script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>';
        $vine_flag_embedded_script = true;
    }
    return $vine_html;
}
 /**
  * Identify images in post content, and if images are local (uploaded to the current site), pass through Photon.
  *
  * @param string $content
  * @uses self::validate_image_url, apply_filters, jetpack_photon_url, esc_url
  * @filter the_content
  * @return string
  */
 public static function filter_the_content($content)
 {
     $images = Jetpack_Photon::parse_images_from_html($content);
     if (!empty($images)) {
         $content_width = Jetpack::get_content_width();
         $image_sizes = self::image_sizes();
         $upload_dir = wp_upload_dir();
         foreach ($images[0] as $index => $tag) {
             // Default to resize, though fit may be used in certain cases where a dimension cannot be ascertained
             $transform = 'resize';
             // Start with a clean attachment ID each time
             $attachment_id = false;
             // Flag if we need to munge a fullsize URL
             $fullsize_url = false;
             // Identify image source
             $src = $src_orig = $images['img_url'][$index];
             /**
              * Allow specific images to be skipped by Photon.
              *
              * @module photon
              *
              * @since 2.0.3
              *
              * @param bool false Should Photon ignore this image. Default to false.
              * @param string $src Image URL.
              * @param string $tag Image Tag (Image HTML output).
              */
             if (apply_filters('jetpack_photon_skip_image', false, $src, $tag)) {
                 continue;
             }
             // Support Automattic's Lazy Load plugin
             // Can't modify $tag yet as we need unadulterated version later
             if (preg_match('#data-lazy-src=["|\'](.+?)["|\']#i', $images['img_tag'][$index], $lazy_load_src)) {
                 $placeholder_src = $placeholder_src_orig = $src;
                 $src = $src_orig = $lazy_load_src[1];
             } elseif (preg_match('#data-lazy-original=["|\'](.+?)["|\']#i', $images['img_tag'][$index], $lazy_load_src)) {
                 $placeholder_src = $placeholder_src_orig = $src;
                 $src = $src_orig = $lazy_load_src[1];
             }
             // Check if image URL should be used with Photon
             if (self::validate_image_url($src)) {
                 // Find the width and height attributes
                 $width = $height = false;
                 // First, check the image tag
                 if (preg_match('#width=["|\']?([\\d%]+)["|\']?#i', $images['img_tag'][$index], $width_string)) {
                     $width = $width_string[1];
                 }
                 if (preg_match('#height=["|\']?([\\d%]+)["|\']?#i', $images['img_tag'][$index], $height_string)) {
                     $height = $height_string[1];
                 }
                 // Can't pass both a relative width and height, so unset the height in favor of not breaking the horizontal layout.
                 if (false !== strpos($width, '%') && false !== strpos($height, '%')) {
                     $width = $height = false;
                 }
                 // Detect WP registered image size from HTML class
                 if (preg_match('#class=["|\']?[^"\']*size-([^"\'\\s]+)[^"\']*["|\']?#i', $images['img_tag'][$index], $size)) {
                     $size = array_pop($size);
                     if (false === $width && false === $height && 'full' != $size && array_key_exists($size, $image_sizes)) {
                         $width = (int) $image_sizes[$size]['width'];
                         $height = (int) $image_sizes[$size]['height'];
                         $transform = $image_sizes[$size]['crop'] ? 'resize' : 'fit';
                     }
                 } else {
                     unset($size);
                 }
                 // WP Attachment ID, if uploaded to this site
                 if (preg_match('#class=["|\']?[^"\']*wp-image-([\\d]+)[^"\']*["|\']?#i', $images['img_tag'][$index], $attachment_id) && (0 === strpos($src, $upload_dir['baseurl']) || apply_filters('jetpack_photon_image_is_local', false, compact('src', 'tag', 'images', 'index')))) {
                     $attachment_id = intval(array_pop($attachment_id));
                     if ($attachment_id) {
                         $attachment = get_post($attachment_id);
                         // Basic check on returned post object
                         if (is_object($attachment) && !is_wp_error($attachment) && 'attachment' == $attachment->post_type) {
                             $src_per_wp = wp_get_attachment_image_src($attachment_id, isset($size) ? $size : 'full');
                             if (self::validate_image_url($src_per_wp[0])) {
                                 $src = $src_per_wp[0];
                                 $fullsize_url = true;
                                 // Prevent image distortion if a detected dimension exceeds the image's natural dimensions
                                 if (false !== $width && $width > $src_per_wp[1] || false !== $height && $height > $src_per_wp[2]) {
                                     $width = false == $width ? false : min($width, $src_per_wp[1]);
                                     $height = false == $height ? false : min($height, $src_per_wp[2]);
                                 }
                                 // If no width and height are found, max out at source image's natural dimensions
                                 // Otherwise, respect registered image sizes' cropping setting
                                 if (false == $width && false == $height) {
                                     $width = $src_per_wp[1];
                                     $height = $src_per_wp[2];
                                     $transform = 'fit';
                                 } elseif (isset($size) && array_key_exists($size, $image_sizes) && isset($image_sizes[$size]['crop'])) {
                                     $transform = (bool) $image_sizes[$size]['crop'] ? 'resize' : 'fit';
                                 }
                             }
                         } else {
                             unset($attachment_id);
                             unset($attachment);
                         }
                     }
                 }
                 // If image tag lacks width and height arguments, try to determine from strings WP appends to resized image filenames.
                 if (false === $width && false === $height) {
                     list($width, $height) = Jetpack_Photon::parse_dimensions_from_filename($src);
                 }
                 // If width is available, constrain to $content_width
                 if (false !== $width && false === strpos($width, '%') && is_numeric($content_width)) {
                     if ($width > $content_width && false !== $height && false === strpos($height, '%')) {
                         $height = round($content_width * $height / $width);
                         $width = $content_width;
                     } elseif ($width > $content_width) {
                         $width = $content_width;
                     }
                 }
                 // Set a width if none is found and $content_width is available
                 // If width is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing
                 if (false === $width && is_numeric($content_width)) {
                     $width = (int) $content_width;
                     if (false !== $height) {
                         $transform = 'fit';
                     }
                 }
                 // Detect if image source is for a custom-cropped thumbnail and prevent further URL manipulation.
                 if (!$fullsize_url && preg_match_all('#-e[a-z0-9]+(-\\d+x\\d+)?\\.(' . implode('|', self::$extensions) . '){1}$#i', basename($src), $filename)) {
                     $fullsize_url = true;
                 }
                 // Build URL, first maybe removing WP's resized string so we pass the original image to Photon
                 if (!$fullsize_url) {
                     $src = self::strip_image_dimensions_maybe($src);
                 }
                 // Build array of Photon args and expose to filter before passing to Photon URL function
                 $args = array();
                 if (false !== $width && false !== $height && false === strpos($width, '%') && false === strpos($height, '%')) {
                     $args[$transform] = $width . ',' . $height;
                 } elseif (false !== $width) {
                     $args['w'] = $width;
                 } elseif (false !== $height) {
                     $args['h'] = $height;
                 }
                 /**
                  * Filter the array of Photon arguments added to an image when it goes through Photon.
                  * By default, only includes width and height values.
                  * @see https://developer.wordpress.com/docs/photon/api/
                  *
                  * @module photon
                  *
                  * @since 2.0.0
                  *
                  * @param array $args Array of Photon Arguments.
                  * @param array $args {
                  * 	 Array of image details.
                  *
                  * 	 @type $tag Image tag (Image HTML output).
                  * 	 @type $src Image URL.
                  * 	 @type $src_orig Original Image URL.
                  * 	 @type $width Image width.
                  * 	 @type $height Image height.
                  * }
                  */
                 $args = apply_filters('jetpack_photon_post_image_args', $args, compact('tag', 'src', 'src_orig', 'width', 'height'));
                 $photon_url = jetpack_photon_url($src, $args);
                 // Modify image tag if Photon function provides a URL
                 // Ensure changes are only applied to the current image by copying and modifying the matched tag, then replacing the entire tag with our modified version.
                 if ($src != $photon_url) {
                     $new_tag = $tag;
                     // If present, replace the link href with a Photoned URL for the full-size image.
                     if (!empty($images['link_url'][$index]) && self::validate_image_url($images['link_url'][$index])) {
                         $new_tag = preg_replace('#(href=["|\'])' . $images['link_url'][$index] . '(["|\'])#i', '\\1' . jetpack_photon_url($images['link_url'][$index]) . '\\2', $new_tag, 1);
                     }
                     // Supplant the original source value with our Photon URL
                     $photon_url = esc_url($photon_url);
                     $new_tag = str_replace($src_orig, $photon_url, $new_tag);
                     // If Lazy Load is in use, pass placeholder image through Photon
                     if (isset($placeholder_src) && self::validate_image_url($placeholder_src)) {
                         $placeholder_src = jetpack_photon_url($placeholder_src);
                         if ($placeholder_src != $placeholder_src_orig) {
                             $new_tag = str_replace($placeholder_src_orig, esc_url($placeholder_src), $new_tag);
                         }
                         unset($placeholder_src);
                     }
                     // Remove the width and height arguments from the tag to prevent distortion
                     $new_tag = preg_replace('#(?<=\\s)(width|height)=["|\']?[\\d%]+["|\']?\\s?#i', '', $new_tag);
                     // Tag an image for dimension checking
                     $new_tag = preg_replace('#(\\s?/)?>(\\s*</a>)?$#i', ' data-recalc-dims="1"\\1>\\2', $new_tag);
                     // Replace original tag with modified version
                     $content = str_replace($tag, $new_tag, $content);
                 }
             } elseif (preg_match('#^http(s)?://i[\\d]{1}.wp.com#', $src) && !empty($images['link_url'][$index]) && self::validate_image_url($images['link_url'][$index])) {
                 $new_tag = preg_replace('#(href=["|\'])' . $images['link_url'][$index] . '(["|\'])#i', '\\1' . jetpack_photon_url($images['link_url'][$index]) . '\\2', $tag, 1);
                 $content = str_replace($tag, $new_tag, $content);
             }
         }
     }
     return $content;
 }
예제 #3
0
 public function is_wide_theme()
 {
     return Jetpack::get_content_width() > 1000;
 }
예제 #4
0
 /**
  * When on the edit screen, make sure the custom content width
  * setting is applied to the large image size.
  */
 static function editor_max_image_size($dims, $size = 'medium', $context = null)
 {
     list($width, $height) = $dims;
     if ('large' == $size && 'edit' == $context) {
         $width = Jetpack::get_content_width();
     }
     return array($width, $height);
 }
예제 #5
0
 /**
  * Filters an array of image `sizes` values, using $content_width instead of image's full size.
  *
  * @since 4.0.4
  * @since 4.1.0 Returns early for images not within the_content.
  * @param array $sizes An array of media query breakpoints.
  * @param array $size  Width and height of the image
  * @uses Jetpack::get_content_width
  * @return array An array of media query breakpoints.
  */
 public function filter_sizes($sizes, $size)
 {
     if (!doing_filter('the_content')) {
         return $sizes;
     }
     $content_width = Jetpack::get_content_width();
     if (!$content_width) {
         $content_width = 1000;
     }
     if (is_array($size) && $size[0] < $content_width) {
         return $sizes;
     }
     return sprintf('(max-width: %1$dpx) 100vw, %1$dpx', $content_width);
 }
예제 #6
0
 public static function get_content_width()
 {
     $tiled_gallery_content_width = Jetpack::get_content_width();
     if (!$tiled_gallery_content_width) {
         $tiled_gallery_content_width = 500;
     }
     return apply_filters('tiled_gallery_content_width', $tiled_gallery_content_width);
 }
예제 #7
0
 public static function get_content_width()
 {
     $tiled_gallery_content_width = Jetpack::get_content_width();
     if (!$tiled_gallery_content_width) {
         $tiled_gallery_content_width = 500;
     }
     /**
      * Filter overwriting the default content width.
      *
      * @module tiled-gallery
      *
      * @since 2.1.0
      *
      * @param string $tiled_gallery_content_width Default Tiled Gallery content width.
      */
     return apply_filters('tiled_gallery_content_width', $tiled_gallery_content_width);
 }
 /**
  * Currently this filter function gets called on
  * 'template_redirect' action and
  * 'admin_init' action
  */
 static function set_content_width()
 {
     // Don't apply this filter on the Edit CSS page.
     if (isset($_GET['page']) && 'editcss' === $_GET['page'] && is_admin()) {
         return;
     }
     $GLOBALS['content_width'] = Jetpack::get_content_width();
 }