woocommerce/includes/integrations/sharethis/class-wc-sharethis.php

121 lines
3.8 KiB
PHP
Raw Normal View History

2012-04-30 19:50:35 +00:00
<?php
2013-02-20 17:14:46 +00:00
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2012-04-30 19:50:35 +00:00
/**
* ShareThis Integration
2012-08-15 18:15:06 +00:00
*
2012-04-30 19:50:35 +00:00
* Enables ShareThis integration.
*
* @class WC_ShareThis
2012-08-15 18:15:06 +00:00
* @extends WC_Integration
* @version 1.6.4
* @package WooCommerce/Classes/Integrations
* @author WooThemes
2012-04-30 19:50:35 +00:00
*/
class WC_ShareThis extends WC_Integration {
2012-08-15 18:15:06 +00:00
/** @var string Default code for share this */
2012-04-30 19:50:35 +00:00
var $default_code;
2012-08-15 18:15:06 +00:00
/**
* Init and hook in the integration.
*
* @access public
* @return void
*/
public function __construct() {
2012-04-30 19:50:35 +00:00
$this->id = 'sharethis';
$this->method_title = __( 'ShareThis', 'woocommerce' );
$this->method_description = __( 'ShareThis offers a sharing widget which will allow customers to share links to products with their friends.', 'woocommerce' );
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$this->default_code = '<div class="social">
2013-04-08 18:35:10 +00:00
<iframe src="https://www.facebook.com/plugins/like.php?href={permalink}&layout=button_count&show_faces=false&width=100&action=like&colorscheme=light&height=21" style="border:none; overflow:hidden; width:100px; height:21px;"></iframe>
2012-05-16 17:32:11 +00:00
<span class="st_twitter"></span><span class="st_email"></span><span class="st_sharethis" st_image="{image}"></span><span class="st_plusone_button"></span>
2012-04-30 19:50:35 +00:00
</div>';
// Load the settings.
2013-01-03 10:38:59 +00:00
$this->init_form_fields();
2012-04-30 19:50:35 +00:00
$this->init_settings();
// Define user set variables
$this->publisher_id = $this->get_option( 'publisher_id' );
$this->sharethis_code = $this->get_option( 'sharethis_code', $this->default_code );
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
// Actions
add_action( 'woocommerce_update_options_integration_sharethis', array( $this, 'process_admin_options' ) );
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
// Share widget
add_action( 'woocommerce_share', array( $this, 'sharethis_code' ) );
2012-08-15 18:15:06 +00:00
}
/**
2012-04-30 19:50:35 +00:00
* Initialise Settings Form Fields
2012-08-15 18:15:06 +00:00
*
* @access public
* @return void
2012-04-30 19:50:35 +00:00
*/
function init_form_fields() {
2012-08-15 18:15:06 +00:00
$this->form_fields = array(
'publisher_id' => array(
2012-04-30 19:50:35 +00:00
'title' => __( 'ShareThis Publisher ID', 'woocommerce' ),
'description' => sprintf( __( 'Enter your %1$sShareThis publisher ID%2$s to show social sharing buttons on product pages.', 'woocommerce' ), '<a href="http://sharethis.com/account/">', '</a>' ),
'type' => 'text',
'default' => get_option('woocommerce_sharethis')
),
'sharethis_code' => array(
'title' => __( 'ShareThis Code', 'woocommerce' ),
'description' => __( 'You can tweak the ShareThis code by editing this option.', 'woocommerce' ),
'type' => 'textarea',
'default' => $this->default_code
)
);
2012-08-15 18:15:06 +00:00
}
2012-04-30 19:50:35 +00:00
/**
2012-08-15 18:15:06 +00:00
* Output share code.
*
* @access public
* @return void
2012-04-30 19:50:35 +00:00
*/
function sharethis_code() {
global $post;
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
if ( $this->publisher_id ) {
2012-08-15 18:15:06 +00:00
2012-05-16 17:32:11 +00:00
$thumbnail = ( $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) ? current(wp_get_attachment_image_src( $thumbnail_id, 'large' )) : '';
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$sharethis = ( is_ssl() ) ? 'https://ws.sharethis.com/button/buttons.js' : 'http://w.sharethis.com/button/buttons.js';
2012-08-15 18:15:06 +00:00
2012-05-16 17:32:11 +00:00
$sharethis_code = str_replace( '{permalink}', urlencode( get_permalink( $post->ID ) ), $this->sharethis_code );
if ( isset( $thumbnail ) ) $sharethis_code = str_replace( '{image}', urlencode( $thumbnail ), $sharethis_code );
2012-08-15 18:15:06 +00:00
2013-04-08 18:35:10 +00:00
echo str_replace( '&', '&amp;', $sharethis_code );
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
echo '<script type="text/javascript">var switchTo5x=true;</script><script type="text/javascript" src="' . $sharethis . '"></script>';
echo '<script type="text/javascript">stLight.options({publisher:"' . $this->publisher_id . '"});</script>';
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
}
}
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
}
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
/**
2012-08-15 18:15:06 +00:00
* Add the integration to WooCommerce.
*
2012-08-15 18:30:35 +00:00
* @package WooCommerce/Classes/Integrations
2012-08-15 18:15:06 +00:00
* @access public
* @param array $integrations
* @return array
*/
2012-04-30 19:50:35 +00:00
function add_sharethis_integration( $integrations ) {
2012-08-15 18:15:06 +00:00
$integrations[] = 'WC_ShareThis';
return $integrations;
2012-04-30 19:50:35 +00:00
}
2012-08-15 18:15:06 +00:00
add_filter('woocommerce_integrations', 'add_sharethis_integration' );