addThisConfigs = $addThisConfigs;
$this->cmsConnector = $cmsConnector;
if ( did_action('addthis_addjs_created') !== 0){
_doing_it_wrong( 'addthis_addjs', 'Only one instance of this class should be initialized. Look for the $addthis_addjs global first',1 );
}
// We haven't added our JS yet. Or at least better not have.
$this->_js_added = false;
$this->_options = $addThisConfigs->getConfigs();
// Version of AddThis code to use
$this->atversion = $this->_options['atversion'];
// If the footer option isn't set, check for it
if (! isset($this->_options['wpfooter']) && current_user_can('manage_options'))
{
add_action('admin_init',array($this, 'update_wpfooter'));
}
// on theme swich, check for footer again
add_action('switch_theme', array($this, 'switch_theme'),15);
// In order for our wp_footer magic to work, we need to sometimes add our stuff
add_action('init', array($this, 'maybe_add_footer_comment'));
// for adding option for show/hide addthis sharing button in admin post add/edit page.
add_action('admin_init', array($this, 'register_post_at_flag'));
// for saving custom field value for show/hide addthis sharing button in admin post add/edit page.
add_action('save_post', array($this, 'save_at_flag'));
// Footer
if ( isset($this->_options['wpfooter']) && $this->_options['wpfooter'])
add_action('wp_footer', array($this, 'output_script') );
else
add_filter('the_content', array($this, 'output_script_filter') );
do_action('addthis_addjs_created');
}
function switch_theme(){
$footer = $this->check_for_footer();
$this->_options['wpfooter'] = $footer;
$this->_options = $this->addThisConfigs->saveConfigs($this->_options);
}
function output_script(){
if ($this->_js_added != true)
{
$this->wrapJs();
$this->addWidgetToJs();
$this->addAfterToJs();
echo $this->jsToAdd;
$this->_js_added = true;
$this->jsToAdd = false;
} else {
$this->addAfterToJs();
echo $this->jsToAdd;
$this->jsToAdd = false;
}
}
function output_script_filter($content){
if ($this->_js_added != true && ! is_admin() && ! is_feed() )
{
$this->wrapJs();
$this->addWidgetToJs();
$this->addAfterToJs();
$content = $content . $this->jsToAdd;
$this->_js_added = true;
}
return $content;
}
function wrapJs(){
$this->jsToAdd = '';
}
/* testing for wp_footer in a theme stuff */
function update_wpfooter(){
$footer = $this->check_for_footer();
$options = $this->_options;
$options['wpfooter'] = $footer;
$this->_options = $this->addThisConfigs->saveConfigs($options);
}
function check_for_footer(){
$url = home_url();
$response = wp_remote_get( $url, array( 'sslverify' => false ) );
$code = (int) wp_remote_retrieve_response_code( $response );
if ( $code == 200 ) {
$html = preg_replace( '/[
s]/', '', wp_remote_retrieve_body( $response ) );
return (bool)( strstr( $html, '' ) );
}
}
function maybe_add_footer_comment(){
add_action( 'wp_footer', array($this, 'test_footer' ), 99999 ); // Some obscene priority, make sure we run last
}
/* END testing for wp_footer in a theme stuff */
function addToScript($newData){
$this->jsToAdd .= $newData;
}
function addAfterScript($newData){
if ( $this->_js_added != true )
{
$this->jsAfterAdd .= $newData;
} else {
$this->jsAfterAdd = $newData;
}
}
function addWidgetToJs(){
if (!is_404()) {
//Load addthis script only if the page is not 404
$addthis_settings_options = $this->addThisConfigs->getConfigs();
$async = '';
if (!empty($addthis_settings_options['addthis_asynchronous_loading'])) {
$async = 'async="async"';
}
/**
* Load client script based on the enviornment variable
* Admin can enable debug mode in adv settings by adding url param debug=true
*/
$script_domain = '//s7.addthis.com/js/';
if (!empty($addthis_settings_options['addthis_environment'])) {
$at_env = $addthis_settings_options['addthis_environment'];
$script_domain = '//cache-'.$at_env.'.addthis.com/cachefly/js/';
}
$url = $script_domain .
$this->atversion .
'/addthis_widget.js#pubid=' .
urlencode($this->addThisConfigs->getUsableProfileId());
$addthis_share = $this->addThisConfigs->createAddThisShareVariable();
$addthis_share_js = '';
if (!empty($addthis_share)) {
$addthis_share_js = 'var addthis_share = '. json_encode($addthis_share) .';';
}
$addthis_config = $this->addThisConfigs->createAddThisConfigVariable();
$addthis_config_js = '';
if (!empty($addthis_config)) {
$addthis_config_js = 'var addthis_config = '. json_encode($addthis_config) .';';
}
$this->jsToAdd .= '
';
}
}
function addAfterToJs(){
if (! empty($this->jsAfterAdd)) {
$this->jsToAdd .= '';
$this->jsAfterAdd = NULL;
}
}
function setProfileId($profile){
$this->_options['addthis_profile'] = sanitize_text_field($profile);
$this->_options = $this->addThisConfigs->saveConfigs($this->_options);
}
function test_footer(){
echo '';
}
function getAtPluginPromoText(){
// Included not to break the other plugins(smartlayer)
if (! did_action('admin_init') && ! current_filter('admin_init'))
{
_doing_it_wrong('getAtPluginPromoText', 'This function should only be called on an admin page load and no earlier the admin_init', 1);
return null;
}
return null;
}
/*
* Function to add checkbox to show/hide Addthis sharing buttons in admin post add/edit page.
*/
public function register_post_at_flag() {
add_meta_box('at_widget', __('AddThis For Wordpress'), array($this, 'add_at_flag_meta_box'), 'post', 'advanced', 'high');
add_meta_box('at_widget', __('AddThis For Wordpress'), array($this, 'add_at_flag_meta_box'), 'page', 'advanced', 'high');
}
/*
* Function to add checkbox to show/hide Addthis sharing buttons in admin post add/edit page.
*/
public function add_at_flag_meta_box($post){
$at_flag = get_post_meta($post->ID, '_at_widget', true);
echo "";
if($at_flag == '' || $at_flag == '1'){
echo "";
} else if($at_flag == '0'){
echo "";
}
}
/*
* Function to save the value of checkbox to show/hide Addthis sharing buttons in admin post add/edit page.
*/
function save_at_flag(){
global $post;
//Ignore if trigger is by theme specific actions without post object
if (!isset($post)) {
return;
}
if(isset($_POST['_at_widget']))
update_post_meta($post->ID, '_at_widget', $_POST['_at_widget']);
else
update_post_meta($post->ID, '_at_widget', '0');
}
}