woocommerce/classes/integrations/shareyourcart/class-wc-shareyourcart.php

398 lines
13 KiB
PHP
Raw Normal View History

2012-04-30 19:50:35 +00:00
<?php
/**
* ShareYourCart Integration
2012-08-15 18:15:06 +00:00
*
2012-04-30 19:50:35 +00:00
* Enables ShareYourCart integration.
*
* @class WC_ShareYourCart
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_ShareYourCart extends WC_Integration {
2012-08-15 18:15:06 +00:00
var $ShareYourCartWooCommerce;
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
2012-04-30 19:50:35 +00:00
$this->id = 'shareyourcart';
$this->method_title = __( 'ShareYourCart', 'woocommerce' );
$this->method_description = sprintf( __( 'Increase your social media exposure by 10 percent! ShareYourCart helps you get more customers by motivating satisfied customers to talk with their friends about your products. For help with ShareYourCart view the <a href="%s">documentation</a>.', 'woocommerce' ), 'http://www.woothemes.com/woocommerce-docs/user-guide/shareyourcart/' );
// Load the form fields.
$this->init_form_fields();
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
// Load the settings.
$this->init_settings();
// Define user set variables
$this->enabled = $this->settings['enabled'];
$this->client_id = $this->settings['client_id'];
$this->app_key = $this->settings['app_key'];
$this->email = $this->settings['email'];
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
// Actions
add_action( 'woocommerce_update_options_integration_shareyourcart', array( &$this, 'process_admin_options') );
add_action( 'woocommerce_update_options_integration_shareyourcart', array( &$this, 'process_forms') );
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
// Actions if enabled
if ( $this->enabled == 'yes' ) {
add_action( 'wp_enqueue_scripts', array(&$this, 'styles') );
2012-08-15 18:15:06 +00:00
2012-05-01 09:19:56 +00:00
$this->init_share_your_cart();
2012-04-30 19:50:35 +00:00
}
2012-08-15 18:15:06 +00:00
}
2012-04-30 19:50:35 +00:00
/**
* styles function.
2012-08-15 18:15:06 +00:00
*
2012-04-30 19:50:35 +00:00
* @access public
* @return void
*/
public function styles() {
wp_enqueue_style( 'shareyourcart', plugins_url( 'css/style.css', __FILE__ ) );
}
2012-08-15 18:15:06 +00:00
2012-05-01 09:19:56 +00:00
/**
* init_share_your_cart function.
2012-08-15 18:15:06 +00:00
*
2012-05-01 09:19:56 +00:00
* @access public
* @return void
*/
function init_share_your_cart() {
2012-08-15 18:15:06 +00:00
2012-05-10 12:51:35 +00:00
if ( empty( $this->shareYourCartWooCommerce ) ) {
2012-05-01 09:19:56 +00:00
// Share your cart api class
2012-08-15 18:15:06 +00:00
include_once('class-shareyourcart-woocommerce.php');
2012-05-01 09:19:56 +00:00
// Init the class
$this->shareYourCartWooCommerce = new ShareYourCartWooCommerce( $this->settings );
}
2012-08-15 18:15:06 +00:00
2012-05-01 09:19:56 +00:00
}
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
/**
* process_forms function.
2012-08-15 18:15:06 +00:00
*
2012-04-30 19:50:35 +00:00
* @access public
*/
function process_forms() {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
if ( ! empty( $_REQUEST['syc-account'] ) && $_REQUEST['syc-account'] == 'create' ) {
2012-08-15 18:15:06 +00:00
2012-05-01 09:19:56 +00:00
$this->init_share_your_cart();
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$error = $message = '';
2012-08-15 18:15:06 +00:00
$redirect = remove_query_arg( 'saved' );
$redirect = remove_query_arg( 'wc_error', $redirect );
$redirect = remove_query_arg( 'wc_message', $redirect );
2012-04-30 19:50:35 +00:00
if ( ! empty( $_POST['domain'] ) && ! empty( $_POST['email'] ) && ! empty( $_POST['syc-terms-agreement'] ) ) {
if ( ! ( ( $register = $this->shareYourCartWooCommerce->register( $this->shareYourCartWooCommerce->getSecretKey(), $_POST['domain'], $_POST['email'], $message ) ) === false) ) {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$this->shareYourCartWooCommerce->setConfigValue('app_key', $register['app_key']);
$this->shareYourCartWooCommerce->setConfigValue('client_id', $register['client_id']);
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$redirect = remove_query_arg( 'syc-account', $redirect );
} else {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$error = $message;
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
if ( json_decode($error) ) {
$error = json_decode($error);
$error = $error->message;
}
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$message = '';
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
} else {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$error = __( 'Please complete all fields.', 'woocommerce' );
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
if ( $error ) $redirect = add_query_arg( 'wc_error', urlencode( esc_attr( $error ) ), $redirect );
if ( $message ) $redirect = add_query_arg( 'wc_message', urlencode( esc_attr( $message ) ), $redirect );
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
wp_safe_redirect( $redirect );
exit;
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
} elseif ( ! empty( $_REQUEST['syc-account'] ) && $_REQUEST['syc-account'] == 'recover' ) {
2012-08-15 18:15:06 +00:00
2012-05-01 09:19:56 +00:00
$this->init_share_your_cart();
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$error = $message = '';
2012-08-15 18:15:06 +00:00
$redirect = remove_query_arg( 'saved' );
$redirect = remove_query_arg( 'wc_error', $redirect );
$redirect = remove_query_arg( 'wc_message', $redirect );
2012-04-30 19:50:35 +00:00
if ( ! empty( $_POST['domain'] ) && ! empty( $_POST['email'] ) ) {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
if ( ! $this->shareYourCartWooCommerce->recover( $this->shareYourCartWooCommerce->getSecretKey(), $_POST['domain'], $_POST['email'], $message ) ) {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$error = $message;
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
if ( json_decode($error) ) {
$error = json_decode($error);
$error = $error->message;
}
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$message = '';
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
} else {
$redirect = remove_query_arg( 'syc-account', $redirect );
}
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
} else {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
$error = __( 'Please complete all fields.', 'woocommerce' );
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
if ( $error ) $redirect = add_query_arg( 'wc_error', urlencode( esc_attr( $error ) ), $redirect );
if ( $message ) $redirect = add_query_arg( 'wc_message', urlencode( esc_attr( $message ) ), $redirect );
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
wp_safe_redirect( $redirect );
exit;
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
/**
* Admin Options
*
* Setup the gateway settings screen.
* Override this in your gateway.
*
* @since 1.0.0
*/
function admin_options() {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
if ( $this->enabled == 'yes' ) {
// Installation
$this->shareYourCartWooCommerce->install();
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
// Check status
$this->shareYourCartWooCommerce->admin_settings_page();
}
?>
2012-08-15 18:15:06 +00:00
<?php
2012-04-30 19:50:35 +00:00
if ( ! $this->client_id && ! $this->app_key ) {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
if ( ! empty( $_REQUEST['syc-account'] ) && $_REQUEST['syc-account'] == 'create' ) {
?>
<div class="updated">
<h3><?php _e('Create a ShareYourCart account', 'woocommerce') ?></h3>
<table class="form-table">
<tr>
<th><?php _e('Domain', 'woocommerce'); ?></th>
<td><input type="text" name="domain" id="domain" class="regular-text" value="<?php echo (isset($_POST['domain']) ? $_POST['domain'] : site_url()); ?>" /></td>
</tr>
<tr>
<th><?php _e('Email', 'woocommerce'); ?></th>
<td><input type="text" name="email" id="email" class="regular-text" value="<?php echo (isset($_POST['email']) ? $_POST['email'] : get_option('admin_email')); ?>"/></td>
</tr>
<tr>
<th>&nbsp;</th>
<td><input class="buttonCheckbox" name="syc-terms-agreement" id="syc-terms-agreement" type="checkbox" /> <label for="syc-terms-agreement"><?php printf(__('I agree to the <a href="%s">ShareYourCart terms and conditions</a>', 'woocommerce'), 'http://shareyourcart.com/terms'); ?></label></td>
</tr>
</table>
<p class="submit"><input type="submit" class="button button-primary" value="<?php _e('Create Account', 'woocommerce') ?>" /></p>
</div>
<?php
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
} elseif ( ! empty( $_REQUEST['syc-account'] ) && $_REQUEST['syc-account'] == 'recover' ) {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
?>
<div class="updated">
<h3><?php _e('Recover your ShareYourCart account', 'woocommerce') ?></h3>
<table class="form-table">
<tr>
<th><?php _e('Domain', 'woocommerce'); ?></th>
<td><input type="text" name="domain" id="domain" class="regular-text" value="<?php echo (isset($_POST['domain']) ? $_POST['domain'] : site_url()); ?>" /></td>
</tr>
<tr>
<th><?php _e('Email', 'woocommerce'); ?></th>
<td><input type="text" name="email" id="email" class="regular-text" value="<?php echo (isset($_POST['email']) ? $_POST['email'] : get_option('admin_email')); ?>"/></td>
</tr>
</table>
<p class="submit"><input type="submit" class="button button-primary" value="<?php _e('Email me my details', 'woocommerce') ?>" /></p>
</div>
<?php
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
} else {
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
?>
<div id="wc_get_started">
<span class="main"><?php _e('Setup your ShareYourCart account', 'woocommerce'); ?></span>
<span><?php echo $this->method_description; ?></span>
2012-07-29 10:21:17 +00:00
<p><a href="<?php echo add_query_arg( 'syc-account', 'create', admin_url( 'admin.php?page=woocommerce_settings&tab=integration&section=shareyourcart' ) ); ?>" class="button button-primary api-link"><?php _e('Create an account', 'woocommerce'); ?></a> <a href="<?php echo add_query_arg( 'syc-account', 'recover', admin_url( 'admin.php?page=woocommerce_settings&tab=integration&section=shareyourcart' ) ); ?>" class="button api-link"><?php _e('Can\'t access your account?', 'woocommerce'); ?></a></p>
2012-04-30 19:50:35 +00:00
</div>
<?php
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
}
} else {
echo '<h3><a href="http://shareyourcart.com/"><img src="' . plugins_url( 'sdk/img/shareyourcart-logo.png', __FILE__ ) . '" alt="ShareYourCart" /></a></h3>';
echo wpautop( $this->method_description );
}
?>
<table class="form-table">
<?php $this->generate_settings_html(); ?>
<?php if ( $this->client_id && $this->app_key ) : ?>
<tr>
<th><?php _e('Configure ShareYourCart', 'woocommerce'); ?></th>
<td>
<p><?php _e('You can choose how much of a discount to give (in fixed amount, percentage, or free shipping) and to which social media channels it should it be applied. You can also define what the advertisement should say, so that it fully benefits your sales.', 'woocommerce'); ?></p>
<p><a href="http://www.shareyourcart.com/configure/?app_key=<?php echo $this->app_key; ?>&amp;client_id=<?php echo $this->client_id; ?>&amp;email=<?php echo $this->email; ?>" class="button" target="_blank"><?php _e('Configure', 'woocommerce'); ?></a></p>
</td>
</tr>
<?php endif; ?>
</table>
<script type="text/javascript">
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
jQuery('#woocommerce_shareyourcart_button_style').change(function(){
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
var value = jQuery(this).val();
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
if ( value == 'standard_button' ) {
jQuery('.standard_button').closest('tr').show();
jQuery('.image_button').closest('tr').hide();
jQuery('.custom_html').closest('tr').hide();
}
if ( value == 'image_button' ) {
jQuery('.standard_button').closest('tr').hide();
jQuery('.image_button').closest('tr').show();
jQuery('.custom_html').closest('tr').hide();
}
if ( value == 'custom_html' ) {
jQuery('.standard_button').closest('tr').hide();
jQuery('.image_button').closest('tr').hide();
jQuery('.custom_html').closest('tr').show();
}
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
}).change();
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
</script>
<!-- Section -->
<div><input type="hidden" name="section" value="<?php echo $this->id; ?>" /></div>
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
<?php
}
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
/**
* Initialise Settings Form Fields
*/
function init_form_fields() {
2012-08-15 18:15:06 +00:00
$this->form_fields = array(
'enabled' => array(
2012-04-30 19:50:35 +00:00
'title' => __( 'Enable/Disable', 'woocommerce' ),
'label' => __( 'Enable ShareYourCart integration', 'woocommerce' ),
'type' => 'checkbox',
'default' => 'no',
),
2012-08-15 18:15:06 +00:00
'client_id' => array(
2012-04-30 19:50:35 +00:00
'title' => __( 'Client ID', 'woocommerce' ),
'description' => __( 'Get your client ID by creating a ShareYourCart account.', 'woocommerce' ),
'type' => 'text',
'default' => '',
'css' => 'width: 300px'
),
2012-08-15 18:15:06 +00:00
'app_key' => array(
2012-04-30 19:50:35 +00:00
'title' => __( 'App Key', 'woocommerce' ),
'description' => __( 'Get your app key by creating a ShareYourCart account.', 'woocommerce' ),
'type' => 'text',
'default' => '',
'css' => 'width: 300px'
),
2012-08-15 18:15:06 +00:00
'email' => array(
2012-04-30 19:50:35 +00:00
'title' => __( 'Email address', 'woocommerce' ),
'description' => __( 'The email address you used to sign up for ShareYourCart.', 'woocommerce' ),
'type' => 'text',
'default' => get_option('admin_email'),
'css' => 'width: 300px'
),
2012-08-15 18:15:06 +00:00
'show_on_product' => array(
2012-04-30 19:50:35 +00:00
'title' => __( 'Show button by default on:', 'woocommerce' ),
'label' => __( 'Product page', 'woocommerce' ),
'type' => 'checkbox',
'default' => 'yes',
),
2012-08-15 18:15:06 +00:00
'show_on_cart' => array(
2012-04-30 19:50:35 +00:00
'label' => __( 'Cart page', 'woocommerce' ),
'type' => 'checkbox',
'default' => 'yes',
),
'button_style' => array(
'title' => __( 'Button style', 'woocommerce' ),
'description' => __( 'Select a style for your share buttons', 'woocommerce' ),
'default' => 'standard_button',
'type' => 'select',
2012-08-15 18:15:06 +00:00
'options' => array(
2012-04-30 19:50:35 +00:00
'standard_button' => __( 'Standard Button', 'woocommerce' ),
'custom_html' => __( 'Custom HTML', 'woocommerce' )
)
),
2012-08-15 18:15:06 +00:00
'button_skin' => array(
2012-04-30 19:50:35 +00:00
'title' => __( 'Button skin', 'woocommerce' ),
'description' => __( 'Select a skin for your share buttons', 'woocommerce' ),
'default' => 'orange',
'type' => 'select',
2012-08-15 18:15:06 +00:00
'options' => array(
2012-04-30 19:50:35 +00:00
'orange' => __( 'Orange', 'woocommerce' ),
'blue' => __( 'Blue', 'woocommerce' ),
'light' => __( 'Light', 'woocommerce' ),
'dark' => __( 'Dark', 'woocommerce' )
),
'class' => 'standard_button'
),
2012-08-15 18:15:06 +00:00
'button_position' => array(
2012-04-30 19:50:35 +00:00
'title' => __( 'Button position', 'woocommerce' ),
'description' => __( 'Where should the button be positioned?', 'woocommerce' ),
'default' => 'normal',
'type' => 'select',
2012-08-15 18:15:06 +00:00
'options' => array(
2012-04-30 19:50:35 +00:00
'normal' => __( 'Normal', 'woocommerce' ),
'floating' => __( 'Floating', 'woocommerce' )
),
'class' => 'standard_button'
),
2012-08-15 18:15:06 +00:00
'button_html' => array(
2012-04-30 19:50:35 +00:00
'title' => __( 'HTML for the button', 'woocommerce' ),
'description' => __( 'Enter the HTML code for your custom button.', 'woocommerce' ),
'default' => '<button>Get a <div class="shareyourcart-discount"></div> discount</button>',
'type' => 'textarea',
'class' => 'custom_html'
)
);
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
} // End init_form_fields()
2012-08-15 18:15:06 +00:00
2012-04-30 19:50:35 +00:00
}
/**
* Add the integration to WooCommerce
**/
function add_shareyourcart_integration( $integrations ) {
if ( ! class_exists('ShareYourCartAPI') ) // Only allow this integration if we're not already using shareyourcart via another plugin
2012-08-15 18:15:06 +00:00
$integrations[] = 'WC_ShareYourCart';
2012-04-30 19:50:35 +00:00
return $integrations;
}
add_filter('woocommerce_integrations', 'add_shareyourcart_integration' );