示例#1
0
 /**
  * Add explicit Tweet button data related to the post and its author
  *
  * @since 1.0.0
  *
  * @param array   $options Tweet button options {
  *   @type string      option name
  *   @type string|bool option value
  * }
  * @param WP_Post $post    post of interest
  *
  * @return array Tweet button options with possible additions based on post data {
  *   @type string      option name
  *   @type string|bool option value
  * }
  */
 protected static function addPostData($options, $post)
 {
     if (!is_array($options)) {
         $options = array();
     }
     // explicitly define post URL
     // maintains Tweet button context on a page listing multiple posts
     if (!(isset($options['url']) && $options['url'])) {
         /**
          * Filter the URL shared in Tweet text
          *
          * All URLs are wrapped in Twitter's t.co link wrapper
          *
          * @since 1.0.0
          *
          * @param string $url The URL returned by get_permalink() when in the loop
          */
         $url = apply_filters('twitter_url', get_permalink($post));
         if ($url) {
             $options['url'] = $url;
         }
         unset($url);
     }
     $author_id = get_the_author_meta('ID');
     if ($author_id) {
         $author_twitter_username = \Twitter\WordPress\User\Meta::getTwitterUsername($author_id);
         if ($author_twitter_username) {
             $author_display_name = trim(get_the_author_meta('display_name', $author_id));
             if (!isset($options['related']) || !is_array($options['related'])) {
                 $options['related'] = array();
             }
             if (!isset($options['related'][$author_twitter_username])) {
                 $options['related'][$author_twitter_username] = $author_display_name;
             }
             unset($author_display_name);
         }
         unset($author_twitter_username);
     }
     unset($author_id);
     return $options;
 }
示例#2
0
 /**
  * Get the Twitter screen name of the author of the current post
  *
  * @since 1.0.0
  *
  * @return string Twitter screen name or empty if no screen name stored
  */
 public static function getScreenName()
 {
     if (!in_the_loop()) {
         return '';
     }
     $screen_name = \Twitter\WordPress\User\Meta::getTwitterUsername(get_the_author_meta('ID'));
     if (!$screen_name) {
         return '';
     }
     return $screen_name;
 }
示例#3
0
 /**
  * Build a card for a single-post view
  *
  * @since 1.0.0
  *
  * @return \Twitter\Cards\Card|null Twitter Card object or null
  */
 public static function buildPostCard()
 {
     $post = get_post();
     if (!$post || !isset($post->ID)) {
         return;
     }
     setup_postdata($post);
     // do not publish card markup for password-protected posts
     if (!empty($post->post_password)) {
         return;
     }
     // only publish card markup for public posts
     $post_status_object = get_post_status_object(get_post_status($post->ID));
     if (!($post_status_object && isset($post_status_object->public) && $post_status_object->public)) {
         return;
     }
     // only output Twitter Card markup for public post types
     // don't waste page generation time if the page is not meant to be consumed by TwitterBot
     $post_type = get_post_type($post);
     if (!$post_type) {
         return;
     }
     $post_type_object = get_post_type_object($post_type);
     if (!($post_type_object && isset($post_type_object->public) && $post_status_object->public)) {
         return;
     }
     $card_type = 'summary';
     if (has_post_format('image', $post->ID)) {
         $card_type = 'photo';
     } else {
         if (has_post_format('gallery', $post->ID)) {
             $card_type = 'gallery';
         }
     }
     $query_type = 'post';
     $card = static::getCardObject($query_type, $post->ID, $card_type);
     if (!$card) {
         return;
     }
     $card_class = get_class($card);
     if (!$card_class) {
         return;
     }
     // get post-specific overrides
     $cards_post_meta = get_post_meta($post->ID, \Twitter\WordPress\Admin\Post\TwitterCard::META_KEY, true);
     // all cards support title
     if (post_type_supports($post_type, 'title')) {
         $title = '';
         if (isset($cards_post_meta['title']) && $cards_post_meta['title']) {
             // do not pass an explicitly defined Twitter Card title through the title filter
             $title = $cards_post_meta['title'];
         } else {
             /** This filter is documented in ::buildHomepageCard */
             $title = apply_filters('twitter_card_title', get_the_title($post->ID), $query_type, $post->ID);
         }
         if ($title) {
             $card->setTitle(\Twitter\WordPress\Cards\Sanitize::sanitizePlainTextString($title));
         }
         unset($title);
     }
     // add description if card supports
     if (method_exists($card, 'setDescription') && post_type_supports($post_type, 'excerpt')) {
         $description = '';
         if (isset($cards_post_meta['description'])) {
             // do not pass an explicitly defined Twitter Card description through the description filter
             $description = $cards_post_meta['description'];
         } else {
             if (!empty($post->post_excerpt)) {
                 /** This filter is documented in wp-includes/post-template.php */
                 $description = apply_filters('get_the_excerpt', $post->post_excerpt);
                 /** This filter is documented in ::buildHomepageCard */
                 $description = apply_filters('twitter_card_description', $description, $query_type, $post->ID);
             } else {
                 /** This filter is documented in ::buildHomepageCard */
                 $description = apply_filters('twitter_card_description', $post->post_content, $query_type, $post->ID);
             }
         }
         $description = \Twitter\WordPress\Cards\Sanitize::sanitizeDescription($description);
         if ($description) {
             $card->setDescription($description);
         }
         unset($description);
     }
     if (defined($card_class . '::MIN_IMAGE_WIDTH') && defined($card_class . '::MIN_IMAGE_HEIGHT')) {
         if (method_exists($card, 'setImage')) {
             // single image card type
             $cards_image_handler = new \Twitter\WordPress\Cards\ImageHandler();
             $cards_image_handler->setLimit(1);
             $cards_image_handler->setMinWidth($card::MIN_IMAGE_WIDTH);
             $cards_image_handler->setMinHeight($card::MIN_IMAGE_HEIGHT);
             // discover images associated with the post
             $cards_image_handler->addPostImages($post);
             $images = $cards_image_handler->getTwitterCardImages();
             if (!empty($images)) {
                 $card->setImage(array_shift($images));
             }
             unset($images);
             unset($cards_image_handler);
         } else {
             if (defined($card_class . '::MAX_IMAGES') && method_exists($card, 'addImage')) {
                 // multiple image card type
                 $cards_image_handler = new \Twitter\WordPress\Cards\ImageHandler();
                 $cards_image_handler->setLimit($card::MAX_IMAGES);
                 $cards_image_handler->setMinWidth($card::MIN_IMAGE_WIDTH);
                 $cards_image_handler->setMinHeight($card::MIN_IMAGE_HEIGHT);
                 // discover images associated with the post
                 $cards_image_handler->addPostImages($post);
                 $images = $cards_image_handler->getTwitterCardImages();
                 if (!empty($images)) {
                     array_walk($images, array($card, 'addImage'));
                 }
                 unset($images);
                 unset($cards_image_handler);
             }
         }
     }
     if (post_type_supports($post_type, 'author') && isset($post->post_author) && method_exists($card, 'setCreator')) {
         $author_twitter_username = \Twitter\WordPress\User\Meta::getTwitterUsername($post->post_author);
         if ($author_twitter_username) {
             $card->setCreator(\Twitter\Cards\Components\Account::fromScreenName($author_twitter_username));
         }
         unset($author_twitter_username);
     }
     return $card;
 }
示例#4
0
 /**
  * Get the Periscope username of the author of the current post
  *
  * @since 1.3.0
  *
  * @return string Periscope username or empty if no screen name stored
  */
 public static function getUsername()
 {
     if (!in_the_loop()) {
         return '';
     }
     $username = \Twitter\WordPress\User\Meta::getPeriscopeUsername(get_the_author_meta('ID'));
     if (!$username) {
         return '';
     }
     return $username;
 }