Twitter\WordPress\Shortcodes\Share::sanitizeShortcodeParameters PHP Method

sanitizeShortcodeParameters() public static method

Convert shortcode parameters, attributes, and defaults into a clean set of Tweet parameters
Since: 1.0.0
public static sanitizeShortcodeParameters ( array $attributes = [] ) : array
$attributes array 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;
    }

Usage Example

Example #1
0
 /**
  * Test setting Tweet button sizes from a shortcode attribute
  *
  * @since 1.0.0
  *
  * @covers ::sanitizeShortcodeParameters
  * @small
  *
  * @dataProvider sizeProvider
  *
  * @param string $size           button size configuration
  * @param bool   $expected_valid expected validity
  * @param string $message        error message to display on negative assertion
  *
  * @return void
  */
 public function testSanitizeShortcodeParametersSize($size, $expected_valid, $message = '')
 {
     $options = \Twitter\WordPress\Shortcodes\Share::sanitizeShortcodeParameters(array('size' => $size));
     if ($expected_valid) {
         $this->assertTrue(isset($options['size']) && $options['size'] === 'large', $message);
     } else {
         $this->assertEmpty($options, $message);
     }
 }