* @license GNU General Public License, version 2 * @link http://addthis.com AddThis website */ class AddThisWidget extends WP_Widget { public $toolClassName = null; public $toolClass = null; public static $i18n_domain = 'addthis_domain'; /** * Registers widget with WordPress. * * @param string $toolClassName the class name for this widget that know * how to create this tool's functionality * * @return null */ public function __construct($toolClassName) { if (!class_exists($toolClassName)) { error_log(__METHOD__ . ' class ' . $toolClassName . ' does not exists.'); return null; } $this->toolClassName = $toolClassName; $toolClass = new $toolClassName(); $this->toolClass = $toolClass; $name = __($toolClass->widgetName, self::$i18n_domain); $description = $toolClass->widgetDescription; $translatedDescription = __($description, self::$i18n_domain); $widgetOptions = array( 'description' => $translatedDescription, ); $controlOptions = array(); parent::__construct( $toolClass->widgetBaseId, $name, $widgetOptions, $controlOptions ); } /** * Prints out HTML for the content of the widget * * @param array $args Widget arguments * @param array $instance Saved values from the database for this * instance of the widget * * @return null */ public function widget($args, $instance) { $titleHtml = ''; if (isset($args['before_title'])) { $titleHtml = $titleHtml . $args['before_title']; } if (!empty($instance['title'])) { $titleHtml = $titleHtml . $instance['title']; } if (isset($args['after_title'])) { $titleHtml = $titleHtml . $args['after_title']; } $addThisToolCode = $this->toolClass->getApiCode($instance); if (!isset($args['widget_name'])) { $args['widget_name'] = 'no name'; } if (!isset($args['before_widget'])) { $args['before_widget'] = ''; } if (!isset($args['after_widget'])) { $args['after_widget'] = ''; } $html = ' '.$args['before_widget'].' '.$titleHtml.' '.$addThisToolCode.' '.$args['after_widget'].' '; echo $html; } /** * Prints out HTML for the options form in the WordPress admin Dashboard * * @param array $instance The widget options * * @return null */ public function form($instance) { $titleFieldId = $this->get_field_id('title'); $titleFieldName = $this->get_field_name('title'); $titleLabel = __('Title: ', $this->i18n_domain); $conflict = false; $featureObject = $this->toolClass->getFeatureObject(); $url = $featureObject->getSettingsPageUrl(); if (isset($instance['title'])) { $titleValue = esc_attr($instance['title']); } else { $titleDefault = $this->toolsClass->defaultWidgetTitle; $titleValue = __($titleDefault, self::$i18n_domain); $toolConfigs = $this->toolClass->getToolConfigs(); if (!empty($toolConfigs['conflict'])) { $conflict = true; } } if (!empty($instance['conflict'])) { $conflict = true; } $conflictString = ''; if (!$conflict) { $links = array(); $featureObject = $this->toolClass->getFeatureObject(); if ($featureObject->isEnabled()) { $links[] = 'the plugin\'s settings'; } if (!$featureObject->isEnabled() || $this->toolClass->useBoostApi()) { $profileId = $featureObject->globalOptionsObject->getProfileId(); $dashboardUrl = 'https://www.addthis.com/dashboard#gallery/pub/'.$profileId .'/pco/'.$this->toolClass->settingsSubVariableName; $links[] = 'addthis.com'; } $html = '
To edit the options for this tool, please go to ' . implode(' or ', $links) . '.
'; } else { $html = '
CONFLICT! Some of the configuration options you chose
for this plugin are no longer supported and can not be
upgraded automatically. Please go to the plugin\'s
settings
to update your configuration before adding or editing
this widget.
'.$this->toolClass->eulaText('Save').'
'; echo $html; } /** * Processing widget options on save * * @param array $new_instance options values just sent to be saved * @param array $old_instance previously options values (from database) * * @return array */ public function update($new_instance, $old_instance) { $instance = $old_instance; if (isset($new_instance['title'])) { $instance['title'] = strip_tags($new_instance['title']); } else { $instance['title'] = $this->toolsClass->defaultWidgetTitle; } return $instance; } /** * Creates the class name for this widget that know how to create this * tool's functionality * * @param string $myClassName the name of the Widgets class * * @return string the name of the tool's setting class */ public static function getToolClass($myClassName) { $length = strlen($myClassName); $cutoff = strlen('Widget'); $toolName = substr($myClassName, 0, $length - $cutoff); $toolClassName = $toolName . 'Tool'; return $toolClassName; } } }