VTMIN_Controller

The VarkTech Minimum Purchase for WooCommerce VTMIN Controller class.

Defined (1)

The class is defined in the following location(s).

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