Skip to content
Snippets Groups Projects
woocommerce-advanced-shipping-by-rules.php 9.31 KiB
<?php
/**
 * Plugin Name: WooCommerce Advanced Shipping By Rules
 * Plugin URI: http://open-tools.net/woocommerce/advanced-shipping-by-rules-for-woocommerce.html
 * Description: Define Shipping cost by very general and flexible (text-based) rules. The advanced version also provides mathematical expressions and functions
 * Version: 1.2.5
 * Author: Open Tools, Reinhold Kainhofer
 * Author URI: http://open-tools.net
 * Text Domain: woocommerce-advanced-shipping-by-rules
 * Domain Path: woocommerce-shipping-by-rules
 * License: GPL2+
 * WC requires at least: 2.2
 * WC tested up to: 2.6
 

 * Copyright (C) 2015 Reinhold Kainhofer
 *
 *     This file is part of WooCommerce Shipping By Rules,
 *     a plugin for WordPress and WooCommerce.
 *
 *     WooCommerce Shipping By Rules is free software:
 *     You can redistribute it and/or modify it under the terms of the
 *     GNU General Public License as published by the Free Software
 *     Foundation, either version 3 of the License, or (at your option)
 *     any later version.
 *
 *     This software is distributed in the hope that
 *     it will be useful, but WITHOUT ANY WARRANTY; without even the
 *     implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 *     PURPOSE. See the GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with WordPress. If not, see <http://www.gnu.org/licenses/>.
 */

// Define a global flag so the basic plugin can deactivate itself if this plugin is loaded
if ( !defined('OPENTOOLS_ADVANCED_SHIPPINGRULES') ) {
	define ('OPENTOOLS_ADVANCED_SHIPPINGRULES', true);
}

// *****************************************************************
// * PLUGIN UPDATES
// *****************************************************************
require 'opentools-update-checker.php';
$myUpdateChecker = new OpenToolsPluginUpdateChecker(
    'http://www.open-tools.net/UpdateServer/index.php?package=WooCommerce&extension=AdvancedShippingByRules',
    __FILE__,
    'woocommerce-advanced-shipping-by-rules'
);
$myUpdateChecker->declareCredentials(array(
	'order_number' => __('Order Number:'), 
	'order_pass'   => __('Order Password:'),
));
$myUpdateChecker->addAccessCheckQueryArgFilter('otsr_addAccessCheckArg');
function otsr_addAccessCheckArg($downloadurl) {
	return $downloadurl . (parse_url($downloadurl, PHP_URL_QUERY) ? '&' : '?') . 'check_access=1';
}

// $myUpdateChecker->checkForUpdates();
// *****************************************************************
 
 
/**
 * Class WooCommerce_Shipping_By_Rules_Advanced.
 *
 * Main Shipping by Rules class, add filters and handling all other files.
 *
 * @class       WooCommerce_Shipping_By_Rules_Advanced
 * @author      Reinhold Kainhofer
 */
class WooCommerce_Shipping_By_Rules_Advanced {
	/**
	 * Version.
	 *
	 * @since 1.0.0
	 * @var string $version Plugin version number.
	 */
	public $version = '1.2.5';


	/**
	 * Instance of WooCommerce_Shipping_By_Rules.
	 *
	 * @since 1.0.0
	 * @access private
	 * @var object $instance The instance of WooCommerce_Shipping_By_Rules.
	 */
	private static $instance;


	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {

		if ( ! function_exists( 'is_plugin_active_for_network' ) )
			require_once( ABSPATH . '/wp-admin/includes/plugin.php' );

		// Check if WooCommerce is active
		if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) :
			if ( ! is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) :
				return;
			endif;
		endif;

		$this->init();

	}


	/**
	 * Instance.
	 *
	 * An global instance of the class. Used to retrieve the instance
	 * to use on other files/plugins/themes.
	 *
	 * @since 1.0.0
	 *
	 * @return object Instance of the class.
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}


	/**
	 * Init.
	 *
	 * Initialize plugin parts.
	 *
	 * @since 1.0.0
	 */
	public function init() {
		$this->hooks();
		$this->load_textdomain();
		$this->update();

		if (!class_exists("RulesShippingFrameworkWooCommerceAdvanced")) {
			require_once( plugin_dir_path( __FILE__ ) . 'includes/rules_shipping_framework_woocommerce_advanced.php');
		}
		$this->helper = RulesShippingFrameworkWooCommerceAdvanced::getHelper();
		
		// Shipping method post type definition:
		if (!class_exists("Shipping_Rules_post_type")) {
			require_once plugin_dir_path( __FILE__ ) . 'includes/rules-shipping-post-type.php';
		}
		$this->post_type = new Shipping_Rules_post_type($this->helper);

		// Links to docs and config in the plugin page
		add_filter( 'plugin_row_meta', array( &$this, 'shippingbyrules_row_meta' ), 30, 2 );
		add_filter( 'plugin_action_links_'.plugin_basename(__FILE__),		array( &$this, 'shippingbyrules_add_settings_link' ) );
	}


	public function shippingbyrules_row_meta($links, $file ) {
		if ($file==plugin_basename(__FILE__)) {
			$links['docs'] = '<a href="' . esc_url(  'http://open-tools.net/documentation/advanced-shipping-by-rules-for-woocommerce.html' ) . '" title="' . esc_attr( $this->helper->__( 'Plugin Documentation' ) ) . '">' . $this->helper->__( 'Plugin Documentation' ) . '</a>';
			$links['support'] = '<a href="' . esc_url( 'http://open-tools.net/support-forum/shiping-by-rules-for-woocommerce.html' ) . '" title="' . esc_attr( $this->helper->__( 'Support Forum' ) ) . '">' . $this->helper->__( 'Support Forum' ) . '</a>';
		}
		return (array)$links;
	}
	/**
	 * Add settings link to plugins page
	 */
	public function shippingbyrules_add_settings_link( $links ) {
		$link = '<a href="admin.php?page=wc-settings&tab=shipping&section=shipping_by_rules">'. $this->helper->__( 'Settings' ) . '</a>';
		array_unshift( $links, $link );
		return $links;
	}
	


	/**
	 * Update.
	 *
	 * Runs when the plugin is updated and checks if there should be
	 * any data updated to be compatible for the new version.
	 *
	 * @since 1.0.0
	 */
	public function update() {

		$db_version = get_option( 'shipping_by_rules_plugin_version', $this->version );

		// Stop current version is up to date
		if ( $db_version >= $this->version ) :
			return;
		endif;

		// Update functions come here:
		// From version 1.x.x to 1.x.y
		// From version 1.x.y to 1.x.z

		update_option( 'shipping_by_rules_plugin_version', $this->version );

	}


	/**
	 * Hooks.
	 *
	 * Initialize all class hooks.
	 *
	 * @since 1.0.0
	 */
	public function hooks() {
		// Initialize shipping method class
		add_action( 'woocommerce_shipping_init', array( $this, 'shipping_by_rules_init' ) );

		// Add shipping method
		add_action( 'woocommerce_shipping_methods', array( $this, 'shipping_by_rules_add_shipping_method' ) );

		// Enqueue scripts
		add_action( 'admin_enqueue_scripts', array( $this, 'shipping_by_rules_admin_enqueue_scripts' ) );

	}


	/**
	 * Textdomain.
	 *
	 * Load the textdomain based on WP language.
	 *
	 * @since 1.0.0
	 */
	public function load_textdomain() {

		// Load textdomain
		load_plugin_textdomain('opentools-shippingrules', false, basename( dirname( __FILE__ ) ) . '/languages' );
	}


	/**
	 * Shipping method.
	 *
	 * Include the WooCommerce shipping method class.
	 *
	 * @since 1.0.0
	 */
	public function shipping_by_rules_init() {
		if (!class_exists('Shipping_by_Rules')) {
			require_once plugin_dir_path( __FILE__ ) . 'includes/rules-shipping-method.php';
		}
		$this->rule_shipping_method = new Shipping_by_Rules();
		$this->rule_shipping_method->setHelper($this->helper);
	}


	/**
	 * Add shipping method.
	 *
	 * Add shipping method to WooCommerce.
	 *
	 * @since 1.0.0
	 */
	public function shipping_by_rules_add_shipping_method( $methods ) {
		if ( class_exists( 'Shipping_by_Rules' ) ) :
			$methods['shipping_by_rules'] = 'Shipping_by_Rules';
		endif;
		// TODO: Figure out a way to add each shipping by rules method as a
		// separate WooCommerce shipping method (ie. their order can be
		// defined in the WC configuration and not only in the Shipping by
		// Rules Method configuration screen.
		return $methods;
	}


	/**
	 * Enqueue scripts.
	 *
	 * Enqueue javascript and stylesheets to the admin area.
	 *
	 * @since 1.0.0
	 */
	public function shipping_by_rules_admin_enqueue_scripts() {
		// For some strange reason, WC does not define the select2.css style...
		$assets_path          = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/';
		wp_enqueue_style( 'select2', $assets_path . 'css/select2.css' );

		wp_enqueue_style( 'shipping_by_rules-style', plugins_url( 'assets/css/admin-styles.css', __FILE__ ), array('select2'), $this->version );
		wp_enqueue_script( 'shipping-by-rules-config', plugins_url( 'assets/js/shipping-by-rules-config.js', __FILE__ ), array( 'jquery', 'jquery-ui-sortable', 'select2', 'wc-enhanced-select' ), $this->version, true );

	}


}


/**
 * The main function responsible for returning the WooCommerce_Shipping_By_Rules_Advanced object.
 *
 * Use this function like you would a global variable, except without needing to declare the global.
 *
 * Example: <?php OpenTools_ShippingByRules()->method_name(); ?>
 *
 * @since 1.0.0
 *
 * @return object WooCommerce_Shipping_By_Rules_Advanced class object.
 */
if ( ! function_exists( 'OpenTools_ShippingByRules_Advanced' ) ) :

 	function OpenTools_ShippingByRules_Advanced() {
		return WooCommerce_Shipping_By_Rules_Advanced::instance();
	}

endif;

OpenTools_ShippingByRules_Advanced();