Twitter\WordPress\Shortcodes\Follow::shortcodeHandler PHP Method

shortcodeHandler() public static method

Handle shortcode macro
Since: 1.0.0
public static shortcodeHandler ( array $attributes, string $content = '' ) : string
$attributes array shortcode attributes
$content string shortcode content. no effect
return string Follow button HTML or empty string
    public static function shortcodeHandler($attributes, $content = '')
    {
        // clean up attribute to shortcode option mappings before passing to filter
        // apply the same filter as shortcode_atts
        /** This filter is documented in wp-includes/shortcodes.php */
        $options = apply_filters('shortcode_atts_' . self::SHORTCODE_TAG, array_merge(static::$SHORTCODE_DEFAULTS, static::sanitizeShortcodeParameters((array) $attributes)), static::$SHORTCODE_DEFAULTS, $attributes);
        $screen_name = '';
        if (isset($options['screen_name'])) {
            $screen_name = $options['screen_name'];
            unset($options['screen_name']);
        }
        if (!$screen_name) {
            $screen_name = static::getScreenName();
            // follow target required
            if (!$screen_name) {
                return '';
            }
        }
        // update the options array with the Follow screen name
        $options['screen_name'] = $screen_name;
        $follow = \Twitter\Widgets\Buttons\Follow::fromArray($options);
        if (!$follow) {
            return '';
        }
        $html = $follow->toHTML(_x('Follow %s', 'Follow a Twitter user', 'twitter'), '\\Twitter\\WordPress\\Helpers\\HTMLBuilder');
        if (!$html) {
            return '';
        }
        $html = '<div class="twitter-follow">' . $html . '</div>';
        $inline_js = \Twitter\WordPress\JavaScriptLoaders\Widgets::enqueue();
        if ($inline_js) {
            return $html . $inline_js;
        }
        return $html;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Front-end display of widget
  *
  * @since 1.0.0
  *
  * @param array $args     Display arguments including before_title, after_title, before_widget, and after_widget.
  * @param array $instance The settings for the particular instance of the widget
  *
  * @return void
  */
 public function widget($args, $instance)
 {
     $follow_button_html = \Twitter\WordPress\Shortcodes\Follow::shortcodeHandler($instance);
     if (!$follow_button_html) {
         return;
     }
     echo $args['before_widget'];
     /** This filter is documented in wp-includes/default-widgets.php */
     $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
     if ($title) {
         echo $args['before_title'] . $title . $args['after_title'];
     }
     // escaped in markup builder
     // @codingStandardsIgnoreStart WordPress.XSS.EscapeOutput
     echo $follow_button_html;
     // @codingStandardsIgnoreEnd WordPress.XSS.EscapeOutput
     echo $args['after_widget'];
 }