示例#1
0
 /**
  * Sanitize user inputs for Twitter Card data before saving as a post meta value
  *
  * @since 1.0.0
  *
  * @param array $fields POST fields for META_KEY
  *
  * @return array|bool sanizited array or false if none set
  */
 public static function sanitizeFields($fields)
 {
     if (!is_array($fields)) {
         // store nothing
         return false;
     }
     // overwrite everything
     if (empty($fields)) {
         return array();
     }
     $cleaned_fields = array();
     if (isset($fields[static::TITLE_KEY])) {
         $title = \Twitter\WordPress\Cards\Sanitize::sanitizePlainTextString($fields[static::TITLE_KEY], true);
         if ($title) {
             $cleaned_fields[static::TITLE_KEY] = $title;
         }
         unset($title);
     }
     if (isset($fields[static::DESCRIPTION_KEY])) {
         $description = \Twitter\WordPress\Cards\Sanitize::sanitizePlainTextString($fields[static::DESCRIPTION_KEY], true);
         if ($description) {
             $cleaned_fields[static::DESCRIPTION_KEY] = $description;
         }
         unset($description);
     }
     return $cleaned_fields;
 }
示例#2
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;
 }