fromArray() public static method

Construct a new Tweet intent object from an options array
Since: 1.0.0
public static fromArray ( array $values ) : __CLASS__
$values array options array { @type string option name @type string|int|bool option value }
return __CLASS__ object initialized based on passed array values
 /**
  * Sanitize an expected comma-separated list of hashtags into an array
  *
  * @since 1.0.0
  *
  * @param string $hashtag_string comma-separated list of hashtags
  *
  * @return array list of hashtags
  */
 public static function sanitizeCommaSeparatedHashtags($hashtag_string)
 {
     if (!is_string($hashtag_string)) {
         return array();
     }
     $hashtag_string = trim($hashtag_string);
     if (!$hashtag_string) {
         return array();
     }
     $intent = \Twitter\Intents\Tweet::fromArray(array('hashtags' => $hashtag_string));
     if (!$intent) {
         return array();
     }
     return $intent->getHashtags();
 }
Example #2
0
 /**
  * Test setting related screen_name with label from a related array
  *
  * @since 1.0.0
  *
  * @covers ::fromArray
  * @small
  *
  * @return void
  */
 public function testRelatedLabelCSVStringFromArray()
 {
     $related = 'twitter:Twitter: main account,twitterdev';
     $this->intent = \Twitter\Intents\Tweet::fromArray(array('related' => $related));
     $property = self::getProperty($this->intent, 'related');
     $this->assertCount(2, $property, 'Failed to set two related screen_names from a related CSV string with a label');
     $this->assertTrue(isset($property['twitter']) && $property['twitter'] === 'Twitter: main account', 'Failed to set a related account label from a CSV string');
     $this->assertArrayHasKey('twitterdev', $property, 'Failed to set a related account screen_name provided after a label in a CSV string');
 }
Example #3
0
 /**
  * Convert shortcode parameters, attributes, and defaults into a clean set of Tweet parameters
  *
  * @since 1.0.0
  *
  * @param array $attributes set of shortcode attribute-value pairs or positional content matching the WordPress shortcode regex {
  *   @type string|int attribute name or positional int
  *   @type mixed shortcode value
  * }
  *
  * @return array cleaned up options ready for comparison {
  *   @type string option name
  *   @type string|bool option value
  * }
  */
 public static function sanitizeShortcodeParameters($attributes = array())
 {
     if (!is_array($attributes)) {
         return array();
     }
     $options = array();
     if (isset($attributes['in_reply_to'])) {
         $tweet_id = \Twitter\WordPress\Shortcodes\EmbeddedTweet::sanitizeTweetID((string) $attributes['in_reply_to']);
         if ($tweet_id) {
             $options['in_reply_to'] = $tweet_id;
         }
         unset($tweet_id);
     }
     if (isset($attributes['text']) && is_string($attributes['text'])) {
         $options['text'] = $attributes['text'];
     }
     foreach (array('url', 'counturl') as $url_param) {
         if (!(isset($attributes[$url_param]) && $attributes[$url_param])) {
             continue;
         }
         // filter the URL
         $url = esc_url_raw(trim($attributes[$url_param]), array('http', 'https'));
         if ($url) {
             $options[$url_param] = $url;
         }
         unset($url);
     }
     if (isset($attributes['related'])) {
         $intent = \Twitter\Intents\Tweet::fromArray(array('related' => $attributes['related']));
         if ($intent) {
             $related = $intent->getRelated();
             if (!empty($related)) {
                 $options['related'] = $related;
             }
             unset($related);
         }
         unset($intent);
     }
     if (isset($attributes['via'])) {
         $via = (new \Twitter\Intents\Tweet())->setVia($attributes['via'])->getVia();
         if ($via) {
             $options['via'] = $via;
         }
         unset($via);
     }
     if (isset($attributes['hashtags'])) {
         $intent = \Twitter\Intents\Tweet::fromArray(array('hashtags' => $attributes['hashtags']));
         if ($intent) {
             $hashtags = $intent->getHashtags();
             if (!empty($hashtags)) {
                 $options['hashtags'] = $hashtags;
             }
             unset($hashtags);
         }
         unset($intent);
     }
     if (isset($attributes['align']) && is_string($attributes['align']) && $attributes['align']) {
         $align = strtolower(trim($attributes['align']));
         if (array_key_exists($align, \Twitter\Widgets\TweetButton::$ALLOWED_ALIGN_VALUES)) {
             $options['align'] = $align;
         }
         unset($align);
     }
     if (isset($attributes['count']) && is_string($attributes['count'])) {
         $count = strtolower(trim($attributes['count']));
         if (array_key_exists($count, \Twitter\Widgets\TweetButton::$ALLOWED_COUNT_VALUES)) {
             $options['count'] = $count;
         }
         unset($count);
     }
     // large is the only option
     if (isset($attributes['size'])) {
         if (is_string($attributes['size']) && in_array(strtolower($attributes['size']), array('large', 'l'))) {
             $options['size'] = 'large';
         }
     }
     return $options;
 }
Example #4
0
 /**
  * Convert shortcode parameters, attributes, and defaults into a clean set of Tweet parameters
  *
  * @since 1.0.0
  *
  * @param array $attributes set of shortcode attribute-value pairs or positional content matching the WordPress shortcode regex {
  *   @type string|int attribute name or positional int
  *   @type mixed shortcode value
  * }
  *
  * @return array cleaned up options ready for comparison {
  *   @type string option name
  *   @type string|bool option value
  * }
  */
 public static function sanitizeShortcodeParameters($attributes = array())
 {
     if (!is_array($attributes)) {
         return array();
     }
     $options = array();
     if (isset($attributes['in_reply_to'])) {
         $tweet_id = \Twitter\WordPress\Shortcodes\EmbeddedTweet::sanitizeTweetID((string) $attributes['in_reply_to']);
         if ($tweet_id) {
             $options['in_reply_to'] = $tweet_id;
         }
         unset($tweet_id);
     }
     if (isset($attributes['text']) && is_string($attributes['text'])) {
         $options['text'] = $attributes['text'];
     }
     if (isset($attributes['url']) && $attributes['url']) {
         $url = esc_url_raw(trim($attributes['url']), array('http', 'https'));
         if ($url) {
             $options['url'] = $url;
         }
         unset($url);
     }
     if (isset($attributes['related'])) {
         $intent = \Twitter\Intents\Tweet::fromArray(array('related' => $attributes['related']));
         if ($intent) {
             $related = $intent->getRelated();
             if (!empty($related)) {
                 $options['related'] = $related;
             }
             unset($related);
         }
         unset($intent);
     }
     if (isset($attributes['via'])) {
         $via = (new \Twitter\Intents\Tweet())->setVia($attributes['via'])->getVia();
         if ($via) {
             $options['via'] = $via;
         }
         unset($via);
     }
     if (isset($attributes['hashtags'])) {
         $intent = \Twitter\Intents\Tweet::fromArray(array('hashtags' => $attributes['hashtags']));
         if ($intent) {
             $hashtags = $intent->getHashtags();
             if (!empty($hashtags)) {
                 $options['hashtags'] = $hashtags;
             }
             unset($hashtags);
         }
         unset($intent);
     }
     // large is the only option
     if (isset($attributes['size'])) {
         if (is_string($attributes['size']) && in_array(strtolower($attributes['size']), array('large', 'l'))) {
             $options['size'] = 'large';
         }
     }
     return $options;
 }