/** * Recommendations Bar markup for use with Facebook JavaScript SDK * * @since 1.1 * @param array $options stored options * @return string HTML div markup or empty string */ function facebook_get_recommendations_bar($options = array()) { if (!class_exists('Facebook_Recommendations_Bar')) { require_once dirname(__FILE__) . '/class-facebook-recommendations-bar.php'; } $bar = Facebook_Recommendations_Bar::fromArray($options); if (!$bar) { return ''; } $html = $bar->asHTML(array('class' => array('fb-social-plugin'))); if ($html) { return "\n" . $html . "\n"; } return ''; }
/** * convert an options array into an object. * * @since 1.1 * * @param array $values associative array * @return Facebook_Recommendations_Bar recommendations bar object */ public static function fromArray($values) { if (!is_array($values) || empty($values)) { return; } $bar = new Facebook_Recommendations_Bar(); if (isset($values['href'])) { $bar->setURL($values['href']); } if (isset($values['trigger'])) { $bar->setTrigger($values['trigger']); } if (isset($values['read_time'])) { $bar->setReadTime(absint($values['read_time'])); } if (isset($values['action'])) { $bar->setAction($values['action']); } if (isset($values['side'])) { $bar->setSide($values['side']); } if (isset($values['site'])) { if (is_string($values['site'])) { $bar->addAction($values['site']); } else { if (is_array($values['site'])) { foreach ($values['site'] as $action) { $bar->addAction($action); } } } } if (isset($values['num_recommendations'])) { $bar->setNumRecommendations(absint($values['num_recommendations'])); } if (isset($values['max_age'])) { $bar->setMaxAge(absint($values['max_age'])); } if (isset($values['ref'])) { $bar->setReference($values['ref']); } return $bar; }
/** * Sanitize Recommendations Bar settings before they are saved to the database. * * @since 1.1 * * @param array $options recommendation bar options * @return array clean option sets. note: we remove Recommendation Button social plugin default options, storing only custom settings (e.g. recommend action preference value stored, like is not stored) */ public static function sanitize_options($options) { if (!is_array($options) || empty($options)) { return array(); } if (isset($options['trigger']) && $options['trigger'] === 'pct') { $pct = 0; if (isset($options['trigger_pct'])) { $pct = absint($options['trigger_pct']); unset($options['trigger_pct']); } if ($pct > 0) { $options['trigger'] = $pct . '%'; } else { $options['trigger'] = 'onvisible'; } } foreach (array('read_time', 'num_recommendations', 'max_age') as $option) { if (isset($options[$option])) { $options[$option] = absint($options[$option]); } } self::require_recommendations_bar_builder(); // Handle like button display preferences first $clean_options = parent::sanitize_options($options); if (isset($clean_options['show_on'])) { self::update_display_conditionals('recommendations_bar', $clean_options['show_on'], self::get_show_on_choices()); unset($clean_options['show_on']); } unset($options['show_on']); $bar = Facebook_Recommendations_Bar::fromArray($options); if ($bar) { return array_merge($clean_options, self::html_data_to_options($bar->toHTMLDataArray())); } return $clean_options; }