/** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @return array Updated safe values to be saved. */ public function update($new_instance, $old_instance) { if (!class_exists('Facebook_Recommendations_Box')) { require_once dirname(dirname(__FILE__)) . '/class-facebook-recommendations-box.php'; } $instance = array(); if (!empty($new_instance['title'])) { $instance['title'] = strip_tags($new_instance['title']); } $box = Facebook_Recommendations_Box::fromArray($new_instance); if ($box) { $box_options = $box->toHTMLDataArray(); if (isset($box_options['header'])) { if ($box_options['header'] === 'false') { $box_options['header'] = false; } else { $box_options['header'] = true; } } if (isset($box_options['border-color'])) { $box_options['border_color'] = $box_options['border-color']; unset($box_options['border-color']); } if (isset($box_options['max-age'])) { $box_options['max_age'] = absint($box_options['max-age']); unset($box_options['max-age']); } foreach (array('width', 'height') as $option) { $box_options[$option] = absint($box_options[$option]); } return array_merge($instance, $box_options); } return $instance; }
/** * Convert the class to data-* attribute friendly associative array * will become data-key="value" * Exclude values if default * * @return array associative array */ public function toHTMLDataArray() { $data = parent::toHTMLDataArray(); if (isset($this->recommendations) && $this->recommendations === true) { $data['recommendations'] = 'true'; } if (isset($this->filter)) { $data['filter'] = $this->filter; } return $data; }
/** * convert an options array into an object * * @since 1.1 * @param array $values associative array * @return Facebook_Recommendations_Box recommendations box object */ public static function fromArray($values) { if (!is_array($values) || empty($values)) { return; } $box = new Facebook_Recommendations_Box(); if (isset($values['site'])) { $box->setSite($values['site']); } if (isset($values['action'])) { if (is_string($values['action'])) { $box->addAction($values['action']); } else { if (is_array($values['action'])) { foreach ($values['action'] as $action) { $box->addAction($action); } } } } if (isset($values['app_id'])) { $box->setAppID($values['app_id']); } if (isset($values['width'])) { $box->setWidth(absint($values['width'])); } if (isset($values['height'])) { $box->setHeight(absint($values['height'])); } if (isset($values['header']) && ($values['header'] === true || $values['header'] == 1 || $values['header'] === 'true')) { $box->showHeader(); } else { $box->hideHeader(); } if (isset($values['border_color'])) { $box->setBorderColor($values['border_color']); } if (isset($values['linktarget'])) { $box->setLinkTarget($values['linktarget']); } if (isset($values['max_age'])) { $box->setMaxAge(absint($values['max_age'])); } if (isset($values['font'])) { $box->setFont($values['font']); } if (isset($values['colorscheme'])) { $box->setColorScheme($values['colorscheme']); } if (isset($values['ref'])) { $box->setReference($values['ref']); } return $box; }