/** * Handle shortcode macro * * @since 1.0.0 * * @param array $attributes shortcode attributes * @param string $content 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\FollowButton::fromArray($options); if (!$follow) { return ''; } $html = $follow->toHTML(_x('Follow %s', 'Follow a Twitter user', 'twitter'), '\\Twitter\\WordPress\\Helpers\\HTMLBuilder'); if (!$html) { return ''; } \Twitter\WordPress\JavaScriptLoaders\Widgets::enqueue(); return '<div class="twitter-follow">' . $html . '</div>'; }
/** * Update a widget instance * * @since 1.0.0 * * @param array $new_instance New settings for this instance as input by the user via form() * @param array $old_instance Old settings for this instance * * @return bool|array settings to save or false to cancel saving */ public function update($new_instance, $old_instance) { $instance = array(); $new_instance = (array) $new_instance; $title = trim(strip_tags($new_instance['title'])); if ($title) { $instance['title'] = $title; } unset($new_instance['title']); foreach (array('show_screen_name', 'show_count') as $bool_option) { if (isset($new_instance[$bool_option]) && $new_instance[$bool_option]) { $new_instance[$bool_option] = true; } else { $new_instance[$bool_option] = false; } } $follow_button = \Twitter\Widgets\FollowButton::fromArray($new_instance); if (!$follow_button) { return false; } $filtered_options = $follow_button->toArray(false); $screen_name = $follow_button->getScreenName(); if ($screen_name) { $filtered_options['screen_name'] = $screen_name; } unset($screen_name); // convert string to bool equivalent if (isset($filtered_options['show-screen-name'])) { if ('false' == $filtered_options['show-screen-name']) { $filtered_options['show_screen_name'] = false; } unset($filtered_options['show-screen-name']); } if (isset($filtered_options['show-count'])) { if ('false' == $filtered_options['show-count']) { $filtered_options['show_count'] = false; } unset($filtered_options['show-count']); } return array_merge($instance, $filtered_options); }
/** * Test setting size from an options array * * @since 1.0.0 * * @covers ::fromArray * @small * * @dataProvider sizeProvider * * @param string $size size * @param bool $expected_valid is the passed size expected to pass? * @param string $message error message * * @return void */ public function testsetSizeFromOptionsArray($size, $expected_valid, $message = '') { $options = self::optionsArraySetUp(); $options['size'] = $size; $this->button = \Twitter\Widgets\FollowButton::fromArray($options); $this->assertNotNull($this->button); $this->setSizeResult($size, $expected_valid, $message); }