С версии: 0.2.0
Автор: Federico Ramirez
 /**
  * Add file contents to the MIME request.
  *
  * @param string $filepath
  * @param string $name
  * @return string
  * @access public
  */
 public function add_content_from_file($filepath, $name = 'a_file')
 {
     $filename = \Apple_News::get_filename($filepath);
     $file_content = file_get_contents($filepath);
     $file_mime = $this->get_mime_type_for($filepath);
     return $this->build_attachment($name, $filename, $file_content, $file_mime);
 }
 /**
  * Build the component.
  *
  * @param string $text
  * @access protected
  */
 protected function build($url)
 {
     $filename = \Apple_News::get_filename($url);
     // Save image into bundle
     $this->bundle_source($filename, $url);
     $this->json = array('role' => 'container', 'layout' => 'headerContainerLayout', 'style' => array('fill' => array('type' => 'image', 'URL' => 'bundle://' . $filename, 'fillMode' => 'cover')), 'behavior' => array('type' => 'background_parallax'));
     $this->set_default_layout();
 }
 /**
  * Build the component.
  *
  * @param string $text
  * @access protected
  */
 protected function build($text)
 {
     preg_match_all('/src="([^"]+)"/', $text, $matches);
     $urls = $matches[1];
     $items = array();
     foreach ($urls as $url) {
         // Save to bundle
         $filename = \Apple_News::get_filename($url);
         $this->bundle_source($filename, $url);
         // Collect into to items array
         $items[] = array('URL' => 'bundle://' . $filename);
     }
     $this->json = array('role' => $this->get_setting('gallery_type'), 'items' => $items);
     $this->set_layout();
 }
 /**
  * Build the component.
  *
  * @param string $text
  * @access protected
  */
 protected function build()
 {
     $meta = array();
     // The content's intro is optional. In WordPress, it's a post's
     // excerpt. It's an introduction to the article.
     if ($this->content_intro()) {
         $meta['excerpt'] = $this->content_intro();
     }
     // If the content has a cover, use it as thumb.
     if ($this->content_cover()) {
         $filename = \Apple_News::get_filename($this->content_cover());
         $thumb_url = 'bundle://' . $filename;
         $meta['thumbnailURL'] = $thumb_url;
     }
     return apply_filters('apple_news_metadata', $meta, $this->content_id());
 }
 /**
  * Build the component.
  *
  * @param string $text
  * @access protected
  */
 protected function build($text)
 {
     preg_match('/src="([^"]*?)"/im', $text, $matches);
     $url = esc_url_raw(apply_filters('apple_news_build_image_src', $matches[1], $text));
     $filename = preg_replace('/\\?.*/', '', \Apple_News::get_filename($url));
     // Save image into bundle
     $this->bundle_source($filename, $url);
     $this->json = array('role' => 'photo', 'URL' => 'bundle://' . $filename);
     // IMAGE ALIGNMENT is defined as follows:
     // 1. If there is a left or right alignment specified, respect it.
     // 2. If there is a center alignment specified:
     //	2.a If the image is big enough, use a full-width image
     //	2.b Otherwise auto-align
     // 3. Otherwise, if there is no alignment specified or set to "none", auto-align.
     if (preg_match('#align="left"#', $text) || preg_match('#class=".*?(?:alignleft).*?"#', $text)) {
         $this->set_anchor_position(Component::ANCHOR_LEFT);
     } else {
         if (preg_match('#align="right"#', $text) || preg_match('#class=".*?(?:alignright).*?"#', $text)) {
             $this->set_anchor_position(Component::ANCHOR_RIGHT);
         } else {
             if (preg_match('#align="center"#', $text) || preg_match('#class=".*?(?:aligncenter).*?"#', $text)) {
                 list($width, $height) = getimagesize($url);
                 if ($width < $this->get_setting('layout_width')) {
                     $this->set_anchor_position(Component::ANCHOR_AUTO);
                 } else {
                     $this->set_anchor_position(Component::ANCHOR_NONE);
                 }
             } else {
                 $this->set_anchor_position(Component::ANCHOR_AUTO);
             }
         }
     }
     // Full width images have top margin
     if (Component::ANCHOR_NONE == $this->get_anchor_position()) {
         $this->register_non_anchor_layout();
     } else {
         $this->register_anchor_layout();
     }
     // Check for caption
     if (preg_match('#<figcaption.*?>(.*?)</figcaption>#m', $text, $matches)) {
         $caption = trim($matches[1]);
         $this->json['caption'] = $caption;
         $this->group_component($caption);
     }
 }
 /**
  * Build the component.
  *
  * @param string $text
  * @access protected
  */
 protected function build()
 {
     $meta = array();
     // The content's intro is optional. In WordPress, it's a post's
     // excerpt. It's an introduction to the article.
     if ($this->content_intro()) {
         $meta['excerpt'] = $this->content_intro();
     }
     // If the content has a cover, use it as thumb.
     if ($this->content_cover()) {
         if ('yes' === $this->get_setting('use_remote_images')) {
             $thumb_url = $this->content_cover();
         } else {
             $filename = \Apple_News::get_filename($this->content_cover());
             $thumb_url = 'bundle://' . $filename;
         }
         $meta['thumbnailURL'] = $thumb_url;
     }
     // Add date fields.
     // We need to get the WordPress post for this
     // since the date functions are inconsistent.
     $post = get_post($this->content_id());
     if (!empty($post)) {
         $post_date = date('c', strtotime(get_gmt_from_date($post->post_date)));
         $post_modified = date('c', strtotime(get_gmt_from_date($post->post_modified)));
         $meta['dateCreated'] = $post_date;
         $meta['dateModified'] = $post_modified;
         $meta['datePublished'] = $post_date;
     }
     // Add canonical URL.
     $meta['canonicalURL'] = get_permalink($this->content_id());
     // Add plugin information to the generator metadata
     $plugin_data = apple_news_get_plugin_data();
     // Add generator information
     $meta['generatorIdentifier'] = sanitize_title_with_dashes($plugin_data['Name']);
     $meta['generatorName'] = $plugin_data['Name'];
     $meta['generatorVersion'] = $plugin_data['Version'];
     return apply_filters('apple_news_metadata', $meta, $this->content_id());
 }
Пример #7
0
 /**
  * Build the component.
  *
  * @param string $text
  * @access protected
  */
 protected function build()
 {
     $meta = array();
     // The content's intro is optional. In WordPress, it's a post's
     // excerpt. It's an introduction to the article.
     if ($this->content_intro()) {
         $meta['excerpt'] = $this->content_intro();
     }
     // If the content has a cover, use it as thumb.
     if ($this->content_cover()) {
         $filename = \Apple_News::get_filename($this->content_cover());
         $thumb_url = 'bundle://' . $filename;
         $meta['thumbnailURL'] = $thumb_url;
     }
     // Add canonical URL.
     $meta['canonicalURL'] = get_permalink($this->content_id());
     // Add plugin information to the generator metadata
     $plugin_data = apple_news_get_plugin_data();
     $meta['generatorIdentifier'] = sanitize_title_with_dashes($plugin_data['Name']);
     $meta['generatorName'] = $plugin_data['Name'];
     $meta['generatorVersion'] = $plugin_data['Version'];
     return apply_filters('apple_news_metadata', $meta, $this->content_id());
 }
 /**
  * Add file contents to the MIME request.
  *
  * @param string $filepath
  * @param string $name
  * @return string
  * @access public
  */
 public function add_content_from_file($filepath, $name = null)
 {
     // Get the contents of the file.
     $contents = '';
     // Try wp_remote_get first.
     if (defined('WPCOM_IS_VIP_ENV') && WPCOM_IS_VIP_ENV) {
         $request = vip_safe_wp_remote_get($filepath);
     } else {
         $request = wp_remote_get($filepath);
     }
     if (is_wp_error($request)) {
         // Try file_get_contents instead. This could be a local path.
         $contents = file_get_contents($filepath);
     } else {
         $contents = wp_remote_retrieve_body($request);
     }
     // Attempt to get the size
     $size = strlen($contents);
     // If this fails for some reason, try alternate methods
     if (empty($size)) {
         if (filter_var($filepath, FILTER_VALIDATE_URL)) {
             $headers = get_headers($filepath);
             foreach ($headers as $header) {
                 if (preg_match('/Content-Length: ([0-9]+)/i', $header, $matches)) {
                     $size = intval($matches[1]);
                 }
             }
         } else {
             // This will be the final catch for local files
             $size = filesize($filepath);
         }
     }
     // If the name wasn't specified, build it from the filename
     $filename = \Apple_News::get_filename($filepath);
     if (empty($name)) {
         $name = sanitize_key($filename);
     }
     return $this->build_attachment($name, $filename, $contents, $this->get_mime_type_for($filepath), $size);
 }
 /**
  * Maybe bundles the source based on current settings.
  * Returns the URL to use based on current setings.
  *
  * @param string $source    The path or URL of the resource which is going to
  *                          be bundled
  * @param string $filename  The name of the file to be created
  * @return string 					The URL to use for this asset in the JSON
  */
 protected function maybe_bundle_source($source, $filename = null)
 {
     if ('yes' === $this->get_setting('use_remote_images')) {
         return $source;
     } else {
         if (null === $filename) {
             $filename = \Apple_News::get_filename($source);
         }
         $this->bundle_source($filename, $source);
         return 'bundle://' . $filename;
     }
 }