* @license http://www.php.net/license/3_01.txt PHP License 3.01 * @version SVN: 1.0 * @link http://www.addthis.com/blog */ $pathParts = pathinfo(__FILE__); $path = $pathParts['dirname']; if (!defined('ADDTHIS_PLUGIN_VERSION')) { define('ADDTHIS_PLUGIN_VERSION', '4.0.1'); } if (!defined('ADDTHIS_ATVERSION')) { define('ADDTHIS_ATVERSION', '300'); } define('ADDTHIS_CSS_PATH', 'css/style.css'); define('ADDTHIS_JS_PATH', 'js/addthis-for-wordpress.js'); define('ADDTHIS_SETTINGS_PAGE_ID', 'addthis_social_widget'); define('ADDTHIS_PLUGIN_FILE', $path.'/addthis_social_widget.php'); define('ADDTHIS_PUBNAME_LIMIT', 255); /** * Class for Addthis wordpress * * @category Class * @package Wordpress_Plugin * @author The AddThis Team * @license http://www.php.net/license/3_01.txt PHP License 3.01 * @version Release: 1.0 * @link http://www.addthis.com/blog */ class Addthis_Wordpress { const ADDTHIS_PROFILE_SETTINGS_PAGE = 'https://www.addthis.com/settings/publisher'; const ADDTHIS_SITE_URL = 'https://www.addthis.com/settings/plugin-pubs'; const ADDTHIS_SITE_URL_WITH_PUB = 'https://www.addthis.com/dashboard#gallery'; const ADDTHIS_REFERER = 'www.addthis.com'; /** PHP $_GET Variables * */ private $_getVariables; /** PHP $_POST Variables * */ private $_postVariables; /** check upgrade or fresh installation **/ private $_upgrade; /** Addthis Profile id **/ private $_pubid; /** Addthis Settings **/ private $_options; /** * Initializes the plugin. * * @param boolean $upgrade check upgrade or fresh installation * * @return null * */ public function __construct($upgrade) { // Save async load settings via ajax request add_action( 'wp_ajax_at_async_loading', array($this, 'addthisAsyncLoading')); $this->_upgrade = $upgrade; $this->_getVariables = $_GET; $this->_postVariables = $_POST; $this->_options = $this->getSettings(); $this->_pubid = (isset($this->_options) && isset($this->_options['profile'])) ? $this->_options['profile'] : null; include_once 'addthis-toolbox.php'; new Addthis_ToolBox; add_action('admin_menu', array($this, 'addthisWordpressMenu')); // Deactivation register_deactivation_hook( ADDTHIS_PLUGIN_FILE, array($this, 'pluginDeactivation') ); // Settings link in plugins page $plugin = 'addthis/addthis_social_widget.php'; add_filter( "plugin_action_links_$plugin", array($this, 'addSettingsLink') ); } /* * Function to add settings link in plugins page * * @return null */ public function addSettingsLink($links) { $settingsLink = 'Settings'; array_push($links, $settingsLink); return $links; } /** * Functions to execute on plugin deactivation * * @return null */ public function pluginDeactivation() { if (get_option('addthis_run_once')) { delete_option('addthis_run_once'); } } /** * Adds sub menu page to the WP settings menu * * @return null */ public function addthisWordpressMenu() { add_options_page( 'AddThis for Wordpress', 'AddThis for Wordpress', 'manage_options', ADDTHIS_SETTINGS_PAGE_ID, array($this, 'addthisWordpressOptions') ); } /** * Manages the WP settings page * * @return null */ public function addthisWordpressOptions() { if (!current_user_can('manage_options')) { wp_die( __('You do not have sufficient permissions to access this page.') ); } $updateResult = null; if ($this->_checkAddPubid()) { $updateResult = $this->updateSettings($this->_postVariables); } wp_enqueue_script( 'addThisScript', plugins_url(ADDTHIS_JS_PATH, __FILE__) ); wp_enqueue_style( 'addThisStylesheet', plugins_url(ADDTHIS_CSS_PATH, __FILE__) ); echo $this->_getHTML($updateResult); } /** * Updates addthis profile id * * @param string $pubId Addthis public id * * @return string */ public function updateSettings($settings) { if(isset($settings['pubid'])){ $this->_options['profile'] = $settings['pubid']; } if(isset($settings['async_loading'])){ $this->_options['addthis_asynchronous_loading'] = $settings['async_loading']; } update_option('addthis_settings', $this->_options); $this->_pubid = $this->_options['profile']; return "
". "AddThis Profile Settings updated successfully!!!". "
"; } /** * Get addthis profile id * * @return string */ public static function getPubid() { $settings = get_option('addthis_settings'); if (isset($settings) && isset($settings['profile'])) { return $settings['profile']; } else { return null; } } /** * Get addthis settings array * * @return string */ public function getSettings() { return get_option('addthis_settings'); } /** * Get referer url * * @return string */ private function _getReferelUrl() { $referer = ''; if (isset($_SERVER['HTTP_REFERER'])) { $parse = parse_url($_SERVER['HTTP_REFERER']); $referer = $parse['host']; } // return $referer; return self::ADDTHIS_REFERER; } /** * Check if there is an addthis profile id return from addthis.com * * @return boolean */ private function _checkPubidFromAddThis() { $referer = $this->_getReferelUrl(); $successReturn = isset ($this->_getVariables['pubid']) && isset ($this->_getVariables['complete']) && $this->_getVariables['complete'] == 'true' && $referer == self::ADDTHIS_REFERER; return $successReturn; } /** * Check if there is request to add addthis profile id * * @return boolean */ private function _checkAddPubid() { $successReturn = isset ($this->_postVariables['pubid']) && isset ($this->_postVariables['submit']); return $successReturn; } /** * Check if there is request to update async loading * * @return boolean */ private function _checkAsyncLoading() { $successReturn = isset ($this->_postVariables['async_loading']); return $successReturn; } public function addthisAsyncLoading() { if ($this->_checkAsyncLoading()) { $updateResult = $this->updateSettings($this->_postVariables); } die; //exit from the ajax request } /** * Check pubid from addthis failure * * @return boolean */ private function _checkAddPubidFailure() { $referer = $this->_getReferelUrl(); $successReturn = (isset ($this->_getVariables['complete']) && $this->_getVariables['complete'] != 'true') || (isset ($this->_getVariables['complete']) && $referer !== self::ADDTHIS_REFERER); return $successReturn; } /** * Get the HTML for addthis settings page * * @param string $updateResult Updated message * * @return string */ private function _getHTML($updateResult) { $html = '
'. '

'. ''. 'AddThis *'. 'for WordPress'. '

'; if ($this->_upgrade && !$this->_pubid) { $html .= $this->_getupdateSuccessMessage(); } if ($this->_checkAddPubidFailure()) { $html .= $this->_getPubIdFromAddthisFailureMessage(); } if ($updateResult) { $html .= $updateResult; } if ($this->_checkPubidFromAddThis() || (isset($this->_getVariables['advanced_settings']) && ($this->_getVariables['advanced_settings'] == 'true')) ) { // Get Confirmation form $html .= $this->_getConfirmationForm(); } else { $html .= $this->_getAddThisLinkButton(); $html .= "
"; } return $html; } /** * Get pubid failure message * * @return */ private static function _getPubIdFromAddthisFailureMessage() { return "
". "Failed to add AddThis Profile ID". "
"; } /** * Get Update Success Message when updating from old plugin * * @return null */ private function _getupdateSuccessMessage() { return "
". "Click on the link below to finish setting up your AddThis tools.". "
"; } /** * Get Link to addthis site * * @return string */ private function _getAddThisLinkButton() { $html = ''; if (!$this->_pubid) { $html = "

You're almost done!

"; } $html .= "
". "Beautiful simple website tools designed to help you get ". "likes, get shares, get follows and get discovered.
"; if (!$this->_pubid) { // Get pub name $pubName = self::_getPubName(); $html .= ""; } else { $html .= "". "To control your AddThis plugins, click here →". ""; } $html .="

If you don’t see your tools after configuring them in the dashboard, please contact ". "AddThis Support

"; $html .= "
 
"; $html .= "Edit Profile Settings"; return $html; } /** * Get the pubname for addthis * * @return string */ private static function _getPubName() { $pubName = get_bloginfo('name'); if (!preg_match('/^[A-Za-z0-9 _\-\(\)]*$/', $pubName)) { // if title not match, get domain $domain = self::getDomain(); if (preg_match( '/(?P[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs )) { $domainArray = explode(".", $regs['domain']); $pubName = $domainArray[0]; } else { $pubName = ''; } $pubName = str_replace('.', '', $pubName); } if (!preg_match('/^[A-Za-z0-9 _\-\(\)]*$/', $pubName) || $pubName == '') { // if domain not match, get loggedin username $currentUser = wp_get_current_user(); $pubName = $currentUser->user_login; } $pubName = substr($pubName, 0, ADDTHIS_PUBNAME_LIMIT); return $pubName; } /** * Get HTML for new users with confirmation * * @return string */ private function _getConfirmationForm() { if (isset($this->_getVariables['advanced_settings'])) { $html = "
"; $html .= "Here you can manually set your AddThis Profile ID - ". "you can get this from your ". "". "Profile Settings"; $html .= "
"; } else { $html = "

You're almost done!

"; $html .= "
". "It's time to connect your AddThis account with Wordpress.". "
"; } $html .= '
'; $html .= "
". "
". "". "Your WordPress Site:". "". "
". "
". "". "". "
". "
". "". "AddThis Profile ID:"; if (isset($this->_getVariables['pubid'])) { $pubId = $this->_getVariables['pubid']; } else { $pubId = $this->_pubid; } $html .= ""; $html .= "
"; $submitButtonValue = "Confirm and Save"; if (isset($this->_getVariables['advanced_settings'])) { $submitButtonValue = "Update"; } $html .= ''; $html .= ''; $html .= "
"; return $html; } /** * Get the plugin's settings page url * * @return string */ public static function getSettingsPageUrl() { return admin_url("options-general.php?page=" . ADDTHIS_SETTINGS_PAGE_ID); } /** * Get the wp domain * * @return string */ public static function getDomain() { $url = get_option('siteurl'); $urlobj = parse_url($url); $domain = $urlobj['host']; return $domain; } } // Setup our shared resources early // addthis_addjs.php is a standard class shared by the various AddThis plugins // to make it easy for us to include our bootstrapping JavaScript only once. // Priority should be lowest for Share plugin. add_action('init', 'Addthis_Wordpress_early', 0); /** * Include addthis js widget * * @global AddThis_addjs $addthis_addjs * @return null */ function Addthis_Wordpress_early() { global $addthis_addjs; if (!isset($addthis_addjs)) { include 'includes/addthis_addjs_new.php'; $addthis_options = get_option('addthis_settings'); $addthis_addjs = new AddThis_addjs($addthis_options); } }