/vt-minimum-purchase.php

  1. <?php 
  2. /** 
  3. Plugin Name: VarkTech Minimum Purchase for WooCommerce 
  4. Plugin URI: http://varktech.com 
  5. Description: An e-commerce add-on for WooCommerce, supplying minimum purchase functionality. 
  6. Version: 1.09.8 
  7. Author: Vark 
  8. Author URI: http://varktech.com 
  9. */ 
  10.  
  11.  
  12. /** 
  13. ** define Globals  
  14. */ 
  15. $vtmin_info; //initialized in VTMIN_Parent_Definitions 
  16. $vtmin_rules_set; 
  17. $vtmin_rule; 
  18. $vtmin_cart; 
  19. $vtmin_cart_item; 
  20. $vtmin_setup_options; 
  21. // $vtmin_error_msg; 
  22.  
  23. //initial setup only, overriden later in function vtprd_debug_options 
  24.  
  25. error_reporting(E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR); //v1.09.2 
  26.  
  27. class VTMIN_Controller{ 
  28.  
  29. public function __construct() {  
  30.  
  31. define('VTMIN_VERSION', '1.09.8'); 
  32. define('VTMIN_MINIMUM_PRO_VERSION', '1.08.3');  
  33. define('VTMIN_LAST_UPDATE_DATE', '2017-04-03'); 
  34. define('VTMIN_DIRNAME', ( dirname( __FILE__ ) )); 
  35. define('VTMIN_URL', plugins_url( '', __FILE__ ) ); 
  36. define('VTMIN_EARLIEST_ALLOWED_WP_VERSION', '3.3'); //To pick up wp_get_object_terms fix, which is required for vtmin-parent-functions.php 
  37. define('VTMIN_EARLIEST_ALLOWED_PHP_VERSION', '5'); 
  38. define('VTMIN_PLUGIN_SLUG', plugin_basename(__FILE__)); 
  39. define('VTMIN_PRO_PLUGIN_NAME', 'VarkTech Minimum Purchase Pro for WooCommerce'); //V1.09.2  
  40.  
  41. require ( VTMIN_DIRNAME . '/woo-integration/vtmin-parent-definitions.php'); 
  42.  
  43. /** =============+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 
  44. // these control the rules ui, add/save/trash/modify/delete 
  45. /** =============+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 
  46. add_action('init', array( &$this, 'vtmin_controller_init' ));  
  47. add_action('admin_init', array( &$this, 'vtmin_admin_init' )); 
  48.  
  49. //v1.08 begin 
  50. add_action( 'draft_to_publish', array( &$this, 'vtmin_admin_update_rule' ));  
  51. add_action( 'auto-draft_to_publish', array( &$this, 'vtmin_admin_update_rule' )); 
  52. add_action( 'new_to_publish', array( &$this, 'vtmin_admin_update_rule' ));  
  53. add_action( 'pending_to_publish', array( &$this, 'vtmin_admin_update_rule' ));  
  54. //v1.08 end 
  55.  
  56. //standard mod/del/trash/untrash  
  57. add_action('save_post', array( &$this, 'vtmin_admin_update_rule' )); 
  58. add_action('delete_post', array( &$this, 'vtmin_admin_delete_rule' ));  
  59. add_action('trash_post', array( &$this, 'vtmin_admin_trash_rule' )); 
  60. add_action('untrash_post', array( &$this, 'vtmin_admin_untrash_rule' )); 
  61.  
  62.  
  63. /** =============+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 
  64.  
  65. //get rid of bulk actions on the edit list screen, which aren't compatible with this plugin's actions... 
  66. add_action('bulk_actions-edit-vtmin-rule', array($this, 'vtmin_custom_bulk_actions') );  
  67.  
  68. } //end constructor 
  69.  
  70.  
  71. /** ************************************************ 
  72. ** Overhead and Init 
  73. *************************************************** */ 
  74. public function vtmin_controller_init() { 
  75. global $vtmin_setup_options; 
  76.  
  77.  
  78. load_plugin_textdomain( 'vtmin', null, dirname( plugin_basename( __FILE__ ) ) . '/languages' );  
  79.  
  80. require ( VTMIN_DIRNAME . '/core/vtmin-backbone.php' );  
  81. require ( VTMIN_DIRNAME . '/core/vtmin-rules-classes.php'); 
  82. require ( VTMIN_DIRNAME . '/woo-integration/vtmin-parent-functions.php'); 
  83. require ( VTMIN_DIRNAME . '/woo-integration/vtmin-parent-cart-validation.php'); 
  84.  
  85. //moved here v1.07 
  86. if (get_option( 'vtmin_setup_options' ) ) { 
  87. $vtmin_setup_options = get_option( 'vtmin_setup_options' ); //put the setup_options into the global namespace 
  88. }  
  89. vtmin_debug_options(); //v1.07 
  90.  
  91. if (is_admin()) { 
  92. //fix 02-132013 - register_activation_hook now at bottom of file, after class instantiates 
  93. require ( VTMIN_DIRNAME . '/admin/vtmin-setup-options.php'); 
  94.  
  95. if(defined('VTMIN_PRO_DIRNAME')) { 
  96. require ( VTMIN_PRO_DIRNAME . '/admin/vtmin-rules-ui.php' ); 
  97. require ( VTMIN_PRO_DIRNAME . '/admin/vtmin-rules-update.php'); 
  98. } else { 
  99. require ( VTMIN_DIRNAME . '/admin/vtmin-rules-ui.php' ); 
  100. require ( VTMIN_DIRNAME . '/admin/vtmin-rules-update.php'); 
  101.  
  102. require ( VTMIN_DIRNAME . '/admin/vtmin-checkbox-classes.php'); 
  103. require ( VTMIN_DIRNAME . '/admin/vtmin-rules-delete.php'); 
  104.  
  105. //v1.09.2 begin 
  106. if ( (defined('VTMIN_PRO_DIRNAME')) && 
  107. (version_compare(VTMIN_PRO_VERSION, VTMIN_MINIMUM_PRO_VERSION) < 0) ) { //'<0' = 1st value is lower  
  108. add_action( 'admin_notices', array(&$this, 'vtmin_admin_notice_version_mismatch') );  
  109. //v1.09.2 end  
  110.  
  111.  
  112. //unconditional branch for these resources needed for WOOCommerce, at "place order" button time 
  113. require ( VTMIN_DIRNAME . '/core/vtmin-cart-classes.php'); 
  114.  
  115. if(defined('VTMIN_PRO_DIRNAME')) { 
  116. require ( VTMIN_PRO_DIRNAME . '/core/vtmin-apply-rules.php' ); 
  117. } else { 
  118. require ( VTMIN_DIRNAME . '/core/vtmin-apply-rules.php' ); 
  119.  
  120. wp_enqueue_script('jquery');  
  121.  
  122.  
  123. //***************** 
  124. //v1.09.8 begin 
  125. //***************** 
  126. /** 
  127. wc_clear_notices 
  128. doesn't clear old notices in all cases 
  129. this takes care of it. 
  130.  
  131. CAN'T use is_cart() as it's not yet available. 
  132. */ 
  133.  
  134. if (is_admin()) { 
  135. return; 
  136.  
  137. global $vtmin_info; 
  138. $vtmin_info['woo_cart_url'] = vtmin_woo_get_url('cart');  
  139. $vtmin_info['woo_checkout_url'] = vtmin_woo_get_url('checkout'); 
  140. $vtmin_info['currPageURL'] = vtmin_currPageURL();  
  141.  
  142.  
  143. //ONLY USED on the cart page  
  144. $use_clear_cart_msgs = apply_filters('vtmin_use_clear_cart_msgs', TRUE ); 
  145. if ($use_clear_cart_msgs) { 
  146.  
  147. $currPageURL = $vtmin_info['currPageURL']; 
  148. $woo_cart_url = $vtmin_info['woo_cart_url']; 
  149.  
  150. if (strpos($currPageURL, $woo_cart_url ) !== false) { 
  151. add_action( "wp_enqueue_scripts", array(&$this, 'vtmin_enqueue_cart_resources') );  
  152. }  
  153.  
  154. return; 
  155. //***************** 
  156. //v1.09.8 end 
  157. //***************** 
  158.  
  159.  
  160. //***************** 
  161. //v1.09.8 New Function 
  162. //***************** 
  163. function vtmin_enqueue_cart_resources() { 
  164. wp_register_script('vtmin-clear-cart-msgs', VTMIN_URL.'/woo-integration/js/vtmin-clear-cart-msgs.js' );  
  165. wp_enqueue_script ('vtmin-clear-cart-msgs', array('jquery'), false, true); 
  166. //error_log( print_r( 'vtmin-clear-cart-msgs', true ) );  
  167. wp_register_style( 'vtmin-error-style', VTMIN_URL.'/core/css/vtmin-error-style.css' );  
  168. wp_enqueue_style('vtmin-error-style'); 
  169. }  
  170.  
  171.  
  172. /** ************************************************ 
  173. ** Admin - Remove bulk actions on edit list screen, actions don't work the same way as onesies... 
  174. ***************************************************/  
  175. function vtmin_custom_bulk_actions($actions) { 
  176.  
  177. ?>  
  178. <style type="text/css"> #delete_all {display:none;} /*kill the 'empty trash' buttons, for the same reason*/ </style> 
  179. <?php 
  180.  
  181. unset( $actions['edit'] ); 
  182. unset( $actions['trash'] ); 
  183. unset( $actions['untrash'] ); 
  184. unset( $actions['delete'] ); 
  185. return $actions; 
  186.  
  187. /** ************************************************ 
  188. ** Admin - Show Rule UI Screen 
  189. ***************************************************  
  190. * This function is executed whenever the add/modify screen is presented 
  191. * WP also executes it ++right after the update function, prior to the screen being sent back to the user.  
  192. */  
  193. public function vtmin_admin_init() { 
  194. if ( !current_user_can( 'edit_posts', 'vtmin-rule' ) ) 
  195. return; 
  196.  
  197. $vtmin_rules_ui = new VTMIN_Rules_UI;  
  198.  
  199.  
  200. /** ************************************************ 
  201. ** Admin - Update Rule  
  202. *************************************************** */ 
  203. public function vtmin_admin_update_rule() { 
  204. /** ***************************************************************** 
  205. The delete/trash/untrash actions *will sometimes fire save_post* 
  206. and there is a case structure in the save_post function to handle this. 
  207.  
  208. the delete/trash actions are sometimes fired twice,  
  209. so this can be handled by checking 'did_action' 
  210. ***************************************************************** */ 
  211.  
  212. global $post, $vtmin_rules_set; 
  213. if( !isset( $post ) ) { //v1.09.3  
  214. return; 
  215. }  
  216. if ( !( 'vtmin-rule' == $post->post_type )) { 
  217. return; 
  218. }  
  219. if (( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) { 
  220. return;  
  221. if (isset($_REQUEST['vtmin_nonce']) ) { //nonce created in vtmin-rules-ui.php  
  222. $nonce = $_REQUEST['vtmin_nonce']; 
  223. if(!wp_verify_nonce($nonce, 'vtmin-rule-nonce')) {  
  224. return; 
  225. }  
  226. if ( !current_user_can( 'edit_posts', 'vtmin-rule' ) ) { 
  227. return; 
  228.  
  229.  
  230. /** ****************************************** 
  231. The 'SAVE_POST' action is fired at odd times during updating. 
  232. When it's fired early, there's no post data available. 
  233. So checking for a blank post id is an effective solution. 
  234. *************************************************** */  
  235. if ( !( $post->ID > ' ' ) ) { //a blank post id means no data to proces.... 
  236. return; 
  237. }  
  238. //AND if we're here via an action other than a true save, do the action and exit stage left 
  239. $action_type = $_REQUEST['action']; 
  240. if ( in_array($action_type, array('trash', 'untrash', 'delete') ) ) { 
  241. switch( $action_type ) { 
  242. case 'trash': 
  243. $this->vtmin_admin_trash_rule();  
  244. break; 
  245. case 'untrash': 
  246. $this->vtmin_admin_untrash_rule(); 
  247. break; 
  248. case 'delete': 
  249. $this->vtmin_admin_delete_rule();  
  250. break; 
  251. return; 
  252.  
  253. $vtmin_rule_update = new VTMIN_Rule_update; 
  254.  
  255.  
  256. /** ************************************************ 
  257. ** Admin - Delete Rule 
  258. *************************************************** */ 
  259. public function vtmin_admin_delete_rule() { 
  260. global $post, $vtmin_rules_set;  
  261. if( !isset( $post ) ) { //v1.09.3  
  262. return; 
  263. }  
  264. if ( !( 'vtmin-rule' == $post->post_type )) { 
  265. return; 
  266. }  
  267.  
  268. if ( !current_user_can( 'delete_posts', 'vtmin-rule' ) ) { 
  269. return; 
  270.  
  271. $vtmin_rule_delete = new VTMIN_Rule_delete;  
  272. $vtmin_rule_delete->vtmin_delete_rule(); 
  273.  
  274.  
  275. /** ************************************************ 
  276. ** Admin - Trash Rule 
  277. *************************************************** */  
  278. public function vtmin_admin_trash_rule() { 
  279. global $post, $vtmin_rules_set;  
  280. if( !isset( $post ) ) { //v1.09.3  
  281. return; 
  282. }  
  283. if ( !( 'vtmin-rule' == $post->post_type )) { 
  284. return; 
  285. }  
  286.  
  287. if ( !current_user_can( 'delete_posts', 'vtmin-rule' ) ) { 
  288. return; 
  289. }  
  290.  
  291. if(did_action('trash_post')) {  
  292. return; 
  293.  
  294. $vtmin_rule_delete = new VTMIN_Rule_delete;  
  295. $vtmin_rule_delete->vtmin_trash_rule(); 
  296.  
  297.  
  298.  
  299. /** ************************************************ 
  300. ** Admin - Untrash Rule 
  301. *************************************************** */  
  302. public function vtmin_admin_untrash_rule() { 
  303. global $post, $vtmin_rules_set;  
  304. if( !isset( $post ) ) { //v1.09.3  
  305. return; 
  306. }  
  307. if ( !( 'vtmin-rule' == $post->post_type )) { 
  308. return; 
  309. }  
  310.  
  311. if ( !current_user_can( 'delete_posts', 'vtmin-rule' ) ) { 
  312. return; 
  313. }  
  314. $vtmin_rule_delete = new VTMIN_Rule_delete;  
  315. $vtmin_rule_delete->vtmin_untrash_rule(); 
  316.  
  317.  
  318. /** ************************************************ 
  319. ** Admin - Activation Hook 
  320. *************************************************** */  
  321. function vtmin_activation_hook() { 
  322. //the options are added at admin_init time by the setup_options.php as soon as plugin is activated!!! 
  323.  
  324. //verify the requirements for Vtmin. 
  325. global $wp_version; 
  326. if((float)$wp_version < 3.3) { 
  327. // delete_option('vtmin_setup_options'); 
  328. wp_die( __('<strong>Looks like you\'re running an older version of WordPress, you need to be running at least WordPress 3.3 to use the Varktech Minimum Purchase plugin.</strong>', 'vtmin'), __('VT Minimum Purchase not compatible - WP', 'vtmin'), array('back_link' => true)); 
  329. return; 
  330.  
  331. //fix 2-13-2013 - changed php version_compare, altered error msg  
  332. if (version_compare(PHP_VERSION, VTMIN_EARLIEST_ALLOWED_PHP_VERSION) < 0) { //'<0' = 1st value is lower  
  333. wp_die( __('<strong><em>PLUGIN CANNOT ACTIVATE   -   Varktech Minimum Purchase </em> 
  334. <br><br>     Your installation is running on an older version of PHP  
  335. <br><br>         - your PHP version = ', 'vtmin') .PHP_VERSION. __(' .  
  336. <br><br>     You need to be running **at least PHP version 5** to use this plugin.  
  337. <br><br>     Please contact your host and request an upgrade to PHP 5+ .  
  338. <br><br>     Then activate this plugin following the upgrade.</strong>', 'vtmin'), __('VT Min and Max Purchase not compatible - PHP', 'vtmin'), array('back_link' => true)); 
  339. return;  
  340.  
  341. if(defined('WPSC_VERSION') && (VTMIN_PARENT_PLUGIN_NAME == 'WP E-Commerce') ) {  
  342. $new_version = VTMIN_EARLIEST_ALLOWED_PARENT_VERSION; 
  343. $current_version = WPSC_VERSION; 
  344. if( (version_compare(strval($new_version), strval($current_version), '>') == 1) ) { //'==1' = 2nd value is lower  
  345. // delete_option('vtmin_setup_options'); 
  346. wp_die( __('<strong>Looks like you\'re running an older version of WP E-Commerce. <br>You need to be running at least ** WP E-Commerce 3.8 **, to use the Varktech Minimum Purchase plugin.</strong>', 'vtmin'), __('VT Minimum Purchase not compatible - WPEC', 'vtmin'), array('back_link' => true)); 
  347. return; 
  348. } else  
  349. if (VTMIN_PARENT_PLUGIN_NAME == 'WP E-Commerce') { 
  350. wp_die( __('<strong>Varktech Minimum Purchase for WP E-Commerce requires that WP E-Commerce be installed and activated.</strong>', 'vtmin'), __('WP E-Commerce not installed or activated', 'vtmin'), array('back_link' => true)); 
  351. return; 
  352.  
  353. if(defined('WOOCOMMERCE_VERSION') && (VTMIN_PARENT_PLUGIN_NAME == 'WooCommerce')) {  
  354. $new_version = VTMIN_EARLIEST_ALLOWED_PARENT_VERSION; 
  355. $current_version = WOOCOMMERCE_VERSION; 
  356. if( (version_compare(strval($new_version), strval($current_version), '>') == 1) ) { //'==1' = 2nd value is lower  
  357. // delete_option('vtmin_setup_options'); 
  358. wp_die( __('<strong>Looks like you\'re running an older version of WooCommerce. <br>You need to be running at least ** WooCommerce 1.0 **, to use the Varktech Minimum Purchase plugin.</strong>', 'vtmin'), __('VT Minimum Purchase not compatible - WooCommerce', 'vtmin'), array('back_link' => true)); 
  359. return; 
  360. } else  
  361. if (VTMIN_PARENT_PLUGIN_NAME == 'WooCommerce') { 
  362. wp_die( __('<strong>Varktech Minimum Purchase for WooCommerce requires that WooCommerce be installed and activated.</strong>', 'vtmin'), __('WooCommerce not installed or activated', 'vtmin'), array('back_link' => true)); 
  363. return; 
  364.  
  365.  
  366. //v1.09.2 begin  
  367. function vtmin_admin_notice_version_mismatch() { 
  368. $message = '<strong>' . __('Please also update plugin: ' , 'vtmin') . '   ' .VTMIN_PRO_PLUGIN_NAME . '</strong>' ; 
  369. $message .= '<br>  •  ' . __('Your Pro Version = ' , 'vtmin') .VTMIN_PRO_VERSION. '   ' . __(' The Minimum Required Pro Version = ' , 'vtmin') .VTMIN_MINIMUM_PRO_VERSION ;  
  370. $message .= '<br>  •  ' . __('Please delete the old Pro plugin from your installation via ftp.' , 'vtmin'); 
  371. $message .= '<br>  •  ' . __('Go to ', 'vtmin'); 
  372. $message .= '<a target="_blank" href="http://www.varktech.com/download-pro-plugins/">Varktech Downloads</a>'; 
  373. $message .= __(', download and install the newest <strong>' , 'vtmin') .VTMIN_PRO_PLUGIN_NAME. '</strong>' ; 
  374.  
  375. $admin_notices = '<div id="message" class="error fade" style="background-color: #FFEBE8 !important;"><p>' . $message . ' </p></div>'; 
  376. echo $admin_notices; 
  377.  
  378. //v1.09.2 added 
  379. $plugin = VTMIN_PRO_PLUGIN_SLUG; 
  380. if( is_plugin_active($plugin) ) { 
  381. deactivate_plugins( $plugin ); 
  382. }  
  383.  
  384. return;  
  385.  
  386. }  
  387. //v1.09.2 end  
  388.  
  389. /** ************************************************ 
  390. ** Admin - **Uninstall** Hook and cleanup 
  391. *************************************************** */  
  392. function vtmin_uninstall_hook() { 
  393.  
  394. if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) { 
  395. return; 
  396. //exit (); 
  397.  
  398. delete_option('vtmin_setup_options'); 
  399. $vtmin_nuke = new VTMIN_Rule_delete;  
  400. $vtmin_nuke->vtmin_nuke_all_rules(); 
  401. $vtmin_nuke->vtmin_nuke_all_rule_cats(); 
  402.  
  403.  
  404. } //end class 
  405. $vtmin_controller = new VTMIN_Controller; 
  406.  
  407. //*************************************************************************************** 
  408. //fix 2-13-2013 - problems with activation hook and class, solved herewith... 
  409. // FROM http://website-in-a-weekend.net/tag/register_activation_hook/ 
  410. //*************************************************************************************** 
  411. if (is_admin()) {  
  412. register_activation_hook(__FILE__, array($vtmin_controller, 'vtmin_activation_hook')); 
  413. register_activation_hook(__FILE__, array($vtmin_controller, 'vtmin_uninstall_hook'));  
.