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

shortcodeHandler() public static method

Handle shortcode macro
Since: 1.0.0
public static shortcodeHandler ( array $attributes, string $content = null ) : string
$attributes array shortcode attributes
$content string shortcode content. no effect
return string Tweet button HTML or empty string
    public static function shortcodeHandler($attributes, $content = null)
    {
        // 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);
        // add options shared to post meta
        $options = static::addPostMetaOptions($options);
        //  add parameters based on per-post render context
        if (in_the_loop()) {
            $post = get_post();
            // do not share posts requiring a password to access
            if ($post && !empty($post->post_password)) {
                return '';
            }
            // protect sites from themselves
            // do not display Tweet button for non-public content to avoid leaking content
            $post_status_object = get_post_status_object(get_post_status($post));
            if (!($post_status_object && isset($post_status_object->public) && $post_status_object->public)) {
                return '';
            }
            unset($post_status_object);
            // add parameters based on post data
            $options = static::addPostData($options, $post);
            unset($post);
        }
        if (!(isset($options['via']) && $options['via'])) {
            // attribute the Tweet to the site Twitter username
            $via_username = \Twitter\WordPress\Site\Username::getViaAttribution(in_the_loop() ? get_the_ID() : null);
            if ($via_username) {
                $options['via'] = $via_username;
            }
            unset($via_username);
        }
        $button = \Twitter\Widgets\Buttons\Tweet::fromArray($options);
        if (!$button) {
            return '';
        }
        $html = $button->toHTML(_x('Tweet', 'Tweet verb. Sharing.', 'twitter'), '\\Twitter\\WordPress\\Helpers\\HTMLBuilder');
        if (!$html) {
            return '';
        }
        $html = '<div class="twitter-share">' . $html . '</div>';
        $inline_js = \Twitter\WordPress\JavaScriptLoaders\Widgets::enqueue();
        if ($inline_js) {
            return $html . $inline_js;
        }
        return $html;
    }

Usage Example

Example #1
0
 /**
  * Filter the_content, possibly adding Tweet button(s)
  *
  * @since 1.0.0
  *
  * @param string $content content of the current post
  *
  * @return string $content content of the current post, possibly with Tweet button markup added
  */
 public static function contentFilter($content)
 {
     // do not include Tweet button(s) in an autogenerated excerpt
     // get_the_excerpt filter calls wp_trim_excerpt() at priority 10 which triggers the_content filter on an empty excerpt string
     if (doing_filter('get_the_excerpt')) {
         return $content;
     }
     $options = static::getOption();
     if (isset($options['position'])) {
         $position = $options['position'];
         unset($options['position']);
         $tweet_button = \Twitter\WordPress\Shortcodes\Share::shortcodeHandler($options);
         if ($tweet_button) {
             // wrap in newlines to preserve content scanners looking for adjacent content on its own line
             $tweet_button = "\n" . $tweet_button . "\n";
             if ('before' === $position) {
                 return $tweet_button . $content;
             } else {
                 if ('after' === $position) {
                     return $content . $tweet_button;
                 } else {
                     if ('both' === $position) {
                         return $tweet_button . $content . $tweet_button;
                     }
                 }
             }
         }
     }
     return $content;
 }