/classes/class-mc-casl.php

  1. <?php 
  2.  
  3. /************************************************************** 
  4.   
  5. COPYRIGHT 2014, BENOIT TOUCHETTE (DRAEKKO). 
  6. This program comes with ABSOLUTELY NO WARRANTY; 
  7. https://www.gnu.org/licenses/gpl-3.0.html 
  8. https://www.gnu.org/licenses/quick-guide-gplv3.html 
  9. Licensed GPLv3. 
  10.   
  11. **************************************************************/ 
  12.  
  13.  
  14. /** Exit if accessed directly */ 
  15. if ( ! defined( 'ABSPATH' ) ) { 
  16. exit; 
  17.  
  18. if ( ! defined( 'MC_CASL_TRUE' ) ) { 
  19. define('MC_CASL_TRUE', 1, true ); 
  20. if ( ! defined( 'MC_CASL_FALSE' ) ) { 
  21. define('MC_CASL_FALSE', 0, true ); 
  22.  
  23. class WC_Integration_MC_CASL extends WC_Integration { 
  24.  
  25. const VERSION = '1.0.0'; 
  26.  
  27. /******************************************************************************************************************/ 
  28.  
  29. public function __construct() { 
  30. if ( !class_exists( 'MailChimp' ) ) { 
  31. include_once( 'class-MCAPI-v2.php' ); 
  32.  
  33. if ( !class_exists( 'MailChimp_CASL_DB' ) ) { 
  34. include_once( 'class-mc-casl-db.php' ); 
  35.  
  36. $this->id = 'mailchimp_casl'; 
  37.  
  38. $this->disclaimer = "<strong>Disclaimer!</strong> This application and documentation and the information in it "; 
  39. $this->disclaimer .= "does not constitute legal advice. It is also is not a substitute for legal "; 
  40. $this->disclaimer .= "or other professional advice. Users should consult their own legal counsel "; 
  41. $this->disclaimer .= "for advice regarding the application of the law and this application as it "; 
  42. $this->disclaimer .= "applies to you and/or your business. This program is distributed in the hope "; 
  43. $this->disclaimer .= "that it will be useful, but <strong>WITHOUT ANY WARRANTY; without even the implied "; 
  44. $this->disclaimer .= "warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE</strong>. Continued "; 
  45. $this->disclaimer .= "use consitutes agreement of these terms. See the GNU General Public License "; 
  46. $this->disclaimer .= "for more details. <a target='_blank' href='https://www.gnu.org/licenses/gpl-3.0.html'>https://www.gnu.org/licenses/gpl-3.0.html</a>"; 
  47.  
  48. $this->method_title = __( 'MailChimp CASL', 'wc_mailchimp_casl' ); 
  49. $this->method_description = __( 'MailChimp is a popular email marketing service.', 'wc_mailchimp_casl' ); 
  50.  
  51. $this->init_settings(); 
  52. $this->api_key = $this->get_option( 'mc_casl_api_key' ); 
  53. $this->attrib_groups_key = ''; 
  54. $this->init_form_fields(); 
  55.  
  56. /** OPTIONS */  
  57. $this->enabled = $this->get_option( 'mc_casl_enabled' ); 
  58. $this->template_enabled = $this->get_option( 'mc_casl_template_enabled' ); 
  59. $this->displaynamerequest = $this->get_option( 'mc_casl_display_name' ); 
  60. $this->requirenamerequest = $this->get_option( 'mc_casl_require_name' ); 
  61. $this->checkdob = $this->get_option( 'mc_casl_request_dob' ); 
  62. $this->minage = $this->get_option( 'mc_casl_minimum_age' ); 
  63. $this->logged_in_template_only = $this->get_option( 'mc_casl_logged_in_template_only' ); 
  64. $this->newsletter_title_label = $this->get_option( 'mc_casl_newsletter_title_label' ); 
  65. $this->newsletter_email_label = $this->get_option( 'mc_casl_newsletter_email_label' ); 
  66. $this->newsletter_message_label = $this->get_option( 'mc_casl_newsletter_message_label' ); 
  67. $this->newsletter_optin_label = $this->get_option( 'mc_casl_newsletter_optin_label' ); 
  68. $this->newsletter_thankyou_label = $this->get_option( 'mc_casl_newsletter_thankyou_label' ); 
  69. $this->ip_database = $this->get_option( 'mc_casl_ip_database' ); 
  70. $this->occurs = $this->get_option( 'mc_casl_occurs' ); 
  71. $this->list = $this->get_option( 'mc_casl_list' ); 
  72. $this->double_optin = $this->get_option( 'mc_casl_double_optin' ); 
  73. $this->display_opt_in = $this->get_option( 'mc_casl_display_opt_in' ); 
  74. $this->opt_in_label = $this->get_option( 'mc_casl_opt_in_label' ); 
  75. $this->opt_in_checkbox_display_location = $this->get_option( 'mc_casl_opt_in_checkbox_display_location' ); 
  76. $this->display_user_date_list = $this->get_option( 'mc_casl_display_user_date_list' ); 
  77. $this->server_timezone = $this->get_option( 'mc_casl_server_timezone' ); 
  78.  
  79. /** Hooks */ 
  80. add_action( 'admin_notices', array( &$this, 'enable_check' ) ); 
  81. add_action( 'woocommerce_update_options_integration', array( $this, 'process_admin_options') ); 
  82. add_action( 'woocommerce_update_options_integration_' . $this->id, array( $this, 'process_admin_options') );  
  83.  
  84. /**  
  85. We would use the 'woocommerce_new_order' action but first name, last name and email address  
  86. (order meta) is not yet available, so instead we use the 'woocommerce_checkout_update_order_meta'  
  87. action hook which fires after the checkout process on the "thank you" page  
  88. */ 
  89. add_action( 'woocommerce_checkout_update_order_meta', array( &$this, 'mc_casl_order_status_changed' ), 1000, 1 ); 
  90.  
  91. /** hook into woocommerce order status changed hook to handle the desired subscription event trigger */ 
  92. add_action( 'woocommerce_order_status_changed', array( &$this, 'mc_casl_order_status_changed' ), 10, 3 ); 
  93.  
  94. /** Maybe add an "opt-in" field to the checkout */ 
  95. add_filter( 'woocommerce_checkout_fields', array( &$this, 'mc_casl_add_checkout_fields' ) ); 
  96.  
  97. /** Maybe save the "opt-in" field on the checkout */ 
  98. add_action( 'woocommerce_checkout_update_order_meta', array( &$this, 'mc_casl_save_checkout_fields' ) ); 
  99.  
  100. //add_action( 'admin_enqueue_scripts', array( $this, 'do_style_action' ) ); 
  101. add_action( 'admin_enqueue_scripts', array( $this, 'do_script_action' ) ); 
  102.  
  103. /******************************************************************************************************************/ 
  104.  
  105. /** HACK TO GET CSS ON ADMIN PAGE */ 
  106. public function do_css_admin_init() { 
  107. $style_id = 'wc_style_mc_casl_options-css'; 
  108. $style_path = str_replace( 'classes/', '', plugins_url( 'css/mc_casl_profile.css', __FILE__ ) ); 
  109. echo "<link rel='stylesheet' id='".$style_id."' href='".$style_path."?ver=".self::VERSION."' type='text/css' media='all' />"; 
  110.  
  111. public function do_style_action() { 
  112. $style_id = 'wc_style_mc_casl_options-css'; 
  113. $style_path = str_replace( 'classes/', '', plugins_url( 'css/mc_casl_profile.css', __FILE__ ) ); 
  114. wp_enqueue_style( $style_id, $style_path, array( 'jquery' ), self::VERSION ); 
  115.  
  116. /******************************************************************************************************************/ 
  117.  
  118. public function do_script_action() { 
  119. $script_path = str_replace( 'classes/', '', plugins_url( 'scripts/mailchimp_casl_wc.js', __FILE__ ) ); 
  120. wp_enqueue_script('wc_script_mc_casl_download-csv', $script_path, array( 'jquery' ), self::VERSION ); 
  121. $this->do_css_admin_init(); 
  122.  
  123. /******************************************************************************************************************/ 
  124.  
  125. public function get_plugin_option( $option ) { 
  126. return $this->get_option($option); 
  127.  
  128. /******************************************************************************************************************/ 
  129.  
  130. function enable_check() { 
  131. global $woocommerce; 
  132.  
  133. if ( $this->enabled == 'yes' ) { 
  134. if ( ! $this->api_key ) { 
  135. echo '<div class="error"><p>' . sprintf( __('MailChimp CASL error: Please enter your api key <a href="%s">here</a>', 'wc_mailchimp_casl'), admin_url('admin.php?page=woocommerce&tab=integration§ion=mailchimp' ) ) . '</p></div>'; 
  136. return; 
  137.  
  138. /******************************************************************************************************************/ 
  139.  
  140. public function has_list() { 
  141. if ( $this->list ) { 
  142. return true; 
  143. return false; 
  144.  
  145. /******************************************************************************************************************/ 
  146.  
  147. public function has_api_key() { 
  148. if ( $this->api_key ) { 
  149. return true; 
  150. return false; 
  151.  
  152. /******************************************************************************************************************/ 
  153.  
  154. public function is_valid() { 
  155. if ( $this->enabled == 'yes' && $this->has_api_key() && $this->has_list() ) { 
  156. return true; 
  157. return false; 
  158.  
  159. /******************************************************************************************************************/ 
  160.  
  161. function init_form_fields() { 
  162.  
  163. if ( is_admin() ) { 
  164.  
  165. $lists = $this->get_lists(); 
  166. if ($lists === false ) { 
  167. $lists = array (); 
  168.  
  169. $this->list = $this->get_option( 'mc_casl_list' ); 
  170. $mailchimp_lists = $this->has_api_key() ? array_merge( array( '' => __('Select a list...', 'wc_mailchimp_casl' ) ), $lists ) : array( '' => __( 'Enter your key and save to see your lists', 'wc_mailchimp_casl' ) ); 
  171.  
  172. $timezones = $this->get_timezone_from_db(); 
  173.  
  174. $this->form_fields = array( 
  175. 'mc_casl_enabled' => array( 
  176. 'title' => __( 'Enable/Disable', 'wc_mailchimp_casl' ),  
  177. 'label' => __( 'Enable MailChimp CASL', 'wc_mailchimp_casl' ),  
  178. 'type' => 'checkbox',  
  179. 'description' => '',  
  180. 'default' => 'no' 
  181. ),  
  182. 'mc_casl_double_optin' => array( 
  183. 'title' => __( 'Double Opt-In', 'wc_mailchimp_casl' ),  
  184. 'label' => __( 'Enable Double Opt-In', 'wc_mailchimp_casl' ),  
  185. 'type' => 'checkbox',  
  186. 'description' => __( 'If enabled, customers will receive an email prompting them to confirm their subscription to the list above.', 'wc_mailchimp_casl' ),  
  187. 'default' => 'no' 
  188. ),  
  189. 'mc_casl_ip_database' => array( 
  190. 'title' => __( 'IP to Country Database', 'wc_mailchimp_casl' ),  
  191. 'type' => 'select',  
  192. 'description' => __( 'Select which database to use to find what country an ip belongs to. This plugin includes GeoLite2 data created by MaxMind, available from <a href="http://www.maxmind.com">MaxMind</a> as well as data from <a href="http://www.ip2nation.com">Ip2Nation</a>. The web services data is provided byte <a href="http://www.ipinfo.io">IpInfo</a> which has a max call rate of 1000 lookups a day for free, and from <a href="http://www.geoplugin.com/">GeoPlugin</a>. Find information on creating the sqlite database files from the original source at <a href="https://github.com/draekko/databases">https://github.com/draekko/databases</a>.', 'wc_mailchimp_casl' ),  
  193. 'default' => 'geolite2',  
  194. 'options' => array( 
  195. 'geolite2' => __( 'MaxMind GeoLite2 [sqlite]', 'wc_mailchimp_casl' ),  
  196. 'ip2nation' => __( 'ip2nation [sqlite]', 'wc_mailchimp_casl' ),  
  197. 'ipinfo' => __( 'ipinfo.io [web]', 'wc_mailchimp_casl' ),  
  198. 'geoplugin' => __( 'geoplugin.net [web]', 'wc_mailchimp_casl' ),  
  199. ),  
  200. ),  
  201. 'mc_casl_api_key' => array( 
  202. 'title' => __( 'API Key', 'wc_mailchimp_casl' ),  
  203. 'type' => 'text',  
  204. 'description' => __( '<a href="https://login.mailchimp.com/" target="_blank">Login to mailchimp</a> to look up your api key.', 'wc_mailchimp_casl' ),  
  205. 'default' => '' 
  206. ),  
  207. 'mc_casl_list' => array( 
  208. 'title' => __( 'Main List', 'wc_mailchimp_casl' ),  
  209. 'type' => 'select',  
  210. 'description' => __( 'All customers will be added to this list.', 'wc_mailchimp_casl' ),  
  211. 'default' => '',  
  212. 'options' => $mailchimp_lists,  
  213. ),  
  214. 'mc_casl_request_dob' => array( 
  215. 'title' => __( 'Request date of birth', 'wc_mailchimp_casl' ),  
  216. 'label' => __( 'Enable to request date of birth when subscribing to list', 'wc_mailchimp_casl' ),  
  217. 'type' => 'checkbox',  
  218. 'description' => '',  
  219. 'default' => 'no' 
  220. ),  
  221. 'mc_casl_minimum_age' => array( 
  222. 'title' => __( 'Minimum Age required', 'wc_mailchimp_casl' ),  
  223. 'type' => 'text',  
  224. 'description' => __( 'Set minimum age to sign up for emails, set to 0 to disable.', 'wc_mailchimp_casl' ),  
  225. 'default' => __( '0', 'wc_mailchimp_casl' ),  
  226. ),  
  227. 'mc_casl_display_name' => array( 
  228. 'title' => __( 'Display name request', 'wc_mailchimp_casl' ),  
  229. 'label' => __( 'Enable to ask display asking for name when subscribing to list', 'wc_mailchimp_casl' ),  
  230. 'type' => 'checkbox',  
  231. 'description' => '',  
  232. 'default' => 'no' 
  233. ),  
  234. 'mc_casl_require_name' => array( 
  235. 'title' => __( 'Require name', 'wc_mailchimp_casl' ),  
  236. 'label' => __( 'Enable to request name when subscribing to list', 'wc_mailchimp_casl' ),  
  237. 'type' => 'checkbox',  
  238. 'description' => 'Set name request to either optional or required.',  
  239. 'default' => 'no' 
  240. ),  
  241. 'mc_casl_template_enabled' => array( 
  242. 'title' => __( 'Template Integration', 'wc_mailchimp_casl' ),  
  243. 'label' => __( 'Enable MailChimp for template', 'wc_mailchimp_casl' ),  
  244. 'type' => 'checkbox',  
  245. 'description' => '',  
  246. 'default' => 'no' 
  247. ),  
  248. 'mc_casl_logged_in_template_only' => array( 
  249. 'title' => __( 'Allow Template Use Logged In Only', 'wc_mailchimp_casl' ),  
  250. 'label' => __( 'Only enable mailing list for logged in users', 'wc_mailchimp_casl' ),  
  251. 'type' => 'checkbox',  
  252. 'description' => '',  
  253. 'default' => 'no' 
  254. ),  
  255. 'mc_casl_newsletter_title_label' => array( 
  256. 'title' => __( 'Subscription Title Label', 'wc_mailchimp_casl' ),  
  257. 'type' => 'textarea',  
  258. 'description' => __( '', 'wc_mailchimp_casl' ),  
  259. 'default' => __( 'Email Subscription', 'wc_mailchimp_casl' ),  
  260. ),  
  261. 'mc_casl_newsletter_email_label' => array( 
  262. 'title' => __( 'Subscription Email Label', 'wc_mailchimp_casl' ),  
  263. 'type' => 'textarea',  
  264. 'description' => __( '', 'wc_mailchimp_casl' ),  
  265. 'default' => __( 'Enter your email address', 'wc_mailchimp_casl' ),  
  266. ),  
  267. 'mc_casl_newsletter_message_label' => array( 
  268. 'title' => __( 'Subscription Message Label', 'wc_mailchimp_casl' ),  
  269. 'type' => 'textarea',  
  270. 'description' => __( '', 'wc_mailchimp_casl' ),  
  271. 'default' => __( 'Keep up with latest news, receive updates, and marketing messages. Read our latest newsletter.', 'wc_mailchimp_casl' ),  
  272. ),  
  273. 'mc_casl_newsletter_optin_label' => array( 
  274. 'title' => __( 'Subscription Opt-In Message Label', 'wc_mailchimp_casl' ),  
  275. 'type' => 'textarea',  
  276. 'description' => __( '', 'wc_mailchimp_casl' ),  
  277. 'default' => __( 'I agree to receive newsletters as well as other documentation, and information related to products and services offered on this site. I agree to be contacted until I withdraw consent by unsubscribing or opting out from the link provided with each message. Refer to our privacy and terms of use policies or contact us for more details.', 'wc_mailchimp_casl' ),  
  278. ),  
  279. 'mc_casl_newsletter_thankyou_label' => array( 
  280. 'title' => __( 'Subscription \'Thank You\' Message Label', 'wc_mailchimp_casl' ),  
  281. 'type' => 'textarea',  
  282. 'description' => __( '', 'wc_mailchimp_casl' ),  
  283. 'default' => __( 'Thank you for subscribing and have a great day.', 'wc_mailchimp_casl' ),  
  284. ),  
  285. 'mc_casl_display_opt_in' => array( 
  286. 'title' => __( 'Display Opt-In Field', 'wc_mailchimp_casl' ),  
  287. 'label' => __( 'Display an Opt-In Field on Checkout', 'wc_mailchimp_casl' ),  
  288. 'type' => 'checkbox',  
  289. 'description' => __( 'If enabled, customers will be presented with a "Opt-in" checkbox during checkout and will only be added to the list above if they opt-in.', 'wc_mailchimp_casl' ),  
  290. 'default' => 'no',  
  291. ),  
  292. 'mc_casl_opt_in_label' => array( 
  293. 'title' => __( 'Opt-In Field Label', 'wc_mailchimp_casl' ),  
  294. 'type' => 'textarea',  
  295. 'description' => __( 'Optional: customize the label displayed next to the opt-in checkbox.', 'wc_mailchimp_casl' ),  
  296. 'default' => __( 'Add me to the newsletter (we will never share your email).', 'wc_mailchimp_casl' ),  
  297. ),  
  298. 'mc_casl_occurs' => array( 
  299. 'title' => __( 'Subscribe Event', 'wc_mailchimp_casl' ),  
  300. 'type' => 'select',  
  301. 'description' => __( 'When should customers be subscribed to lists?', 'wc_mailchimp_casl' ),  
  302. 'default' => 'pending',  
  303. 'options' => array( 
  304. 'pending' => __( 'Order Created', 'wc_mailchimp_casl' ),  
  305. 'completed' => __( 'Order Completed', 'wc_mailchimp_casl' ),  
  306. ),  
  307. ),  
  308. 'mc_casl_opt_in_checkbox_display_location' => array( 
  309. 'title' => __( 'Opt-In Checkbox Display Location', 'wc_mailchimp_casl' ),  
  310. 'type' => 'select',  
  311. 'description' => __( 'Where to display the opt-in checkbox on the checkout page (under Billing info or Order info).', 'wc_mailchimp_casl' ),  
  312. 'default' => 'billing',  
  313. 'options' => array( 'billing' => __( 'Billing', 'wc_mailchimp_casl' ), 'order' => __( 'Order', 'wc_mailchimp_casl' ) ) 
  314. ),  
  315. 'mc_casl_display_user_date_list' => array( 
  316. 'title' => __( 'Display user data for admin only', 'wc_mailchimp_casl' ),  
  317. 'label' => __( 'Enable/Disable profile users data.', 'wc_mailchimp_casl' ),  
  318. 'type' => 'checkbox',  
  319. 'description' => 'This will toggle showing dates of subscriptions for admin only if enabled.',  
  320. 'default' => 'yes',  
  321. ),  
  322. 'mc_casl_server_timezone' => array( 
  323. 'title' => __( 'Set Timezone', 'wc_mailchimp_casl' ),  
  324. 'type' => 'select',  
  325. 'description' => __( 'Select timezone for reporting time and date (default is server timezone).', 'wc_mailchimp_casl' ),  
  326. 'default' => '',  
  327. 'options' =>$timezones,  
  328. ),  
  329. 'mc_casl_csv_export' => array( 
  330. 'title' => __( 'Export DB to CSV', 'wc_mailchimp_casl' ),  
  331. 'type' => 'button',  
  332. 'name' => 'mc_casl_csv_export',  
  333. 'value' => 'Download DB',  
  334. 'description' => 'Save data to a csv file for download.',  
  335. 'default' => '',  
  336. ),  
  337. ); 
  338.  
  339. $this->wc_enqueue_js(" 
  340. jQuery('#woocommerce_mailchimp_display_opt_in').change(function() { 
  341.  
  342. jQuery('#mainform [id^=woocommerce_mailchimp_opt_in]').closest('tr').hide('fast'); 
  343.  
  344. if ( jQuery(this).prop('checked') == true ) { 
  345. jQuery('#mainform [id^=woocommerce_mailchimp_opt_in]').closest('tr').show('fast'); 
  346. } else { 
  347. jQuery('#mainform [id^=woocommerce_mailchimp_opt_in]').closest('tr').hide('fast'); 
  348.  
  349. }).change(); 
  350. "); 
  351.  
  352. /******************************************************************************************************************/ 
  353.  
  354. private function get_timezone_from_db() { 
  355. $tz_rows = array(); 
  356. $i = 0; 
  357. $timezones = array(); 
  358. $plugin_path = str_replace( 'classes/', '', plugin_dir_path( __FILE__ ) ) . 'db/timezone.db'; 
  359. $db = new SQLite3( $plugin_path ); 
  360. $query = $db->query( 'SELECT zone_name FROM zone ORDER BY zone_name ASC;' ); 
  361. if ( $query ) { 
  362. while ( $res = $query->fetchArray(SQLITE3_ASSOC) ) { 
  363. $tz_rows[$res['zone_name']] = $res['zone_name']; 
  364. $i++; 
  365. $db->close(); 
  366. $timezones = array_merge( array( '' => __('Server default time zone', 'wc_mailchimp_casl' ) ), $tz_rows ); 
  367. return $timezones; 
  368. }  
  369.  
  370. /******************************************************************************************************************/ 
  371.  
  372. private function wc_enqueue_js( $code ) { 
  373. if ( function_exists( 'wc_enqueue_js' ) ) { 
  374. wc_enqueue_js( $code ); 
  375. } else { 
  376. global $woocommerce; 
  377. $woocommerce->add_inline_js( $code ); 
  378.  
  379. /******************************************************************************************************************/ 
  380.  
  381. function mc_casl_add_checkout_fields( $checkout_fields ) { 
  382. $opt_in_checkbox_display_location = $this->opt_in_checkbox_display_location; 
  383.  
  384. if ( empty( $opt_in_checkbox_display_location ) ) { 
  385. $opt_in_checkbox_display_location = 'billing'; 
  386.  
  387. if ( 'yes' == $this->display_opt_in ) { 
  388. $checkout_fields[$opt_in_checkbox_display_location]['mailchimp_casl_opt_in_subscription'] = array( 
  389. 'type' => 'checkbox',  
  390. 'class' => array('form-row-wide', 'shipping_address'),  
  391. 'label' => esc_attr( $this->opt_in_label ),  
  392. 'default' => 0,  
  393. ); 
  394.  
  395. return $checkout_fields; 
  396.  
  397. /******************************************************************************************************************/ 
  398.  
  399. function mc_casl_save_checkout_fields( $order_id ) { 
  400. if ( 'yes' == $this->display_opt_in ) { 
  401. $opt_in = isset( $_POST['mailchimp_casl_opt_in_subscription'] ) ? 'yes' : 'no'; 
  402. update_post_meta( $order_id, 'mailchimp_casl_opt_in_subscription', $opt_in ); 
  403.  
  404. /******************************************************************************************************************/ 
  405.  
  406. public function mc_casl_order_status_changed( $id, $status = 'new', $new_status = 'pending' ) { 
  407. echo "<br><h2>process mailing list [".$new_status."] [".$this->occurs."] [".$this->is_valid()."]</h2><br>"; 
  408. if ( $this->is_valid() && $new_status == $this->occurs ) { 
  409. $order = new WC_Order( $id ); 
  410.  
  411. $mailchimp_casl_opt_in_subscription = get_post_meta( $id, 'mailchimp_casl_opt_in_subscription', true ); 
  412.  
  413. if ( ! isset( $mailchimp_casl_opt_in_subscription ) || empty( $mailchimp_casl_opt_in_subscription ) || 'yes' == $mailchimp_casl_opt_in_subscription ) { 
  414. $this->mailchimp_casl_subscribe( $id, $order->billing_first_name, $order->billing_last_name, $order->billing_email, $this->list ); 
  415. }  
  416.  
  417. /******************************************************************************************************************/ 
  418.  
  419. public function mailchimp_casl_subscribe( $order_id, $first_name, $last_name, $email, $listid = 'false' ) { 
  420. wp_debug_mode(); 
  421. $retstatus = true; 
  422.  
  423. /** Email is required */ 
  424. if ( ! $email ) { 
  425. return false;  
  426.  
  427. $MailChimp = new MailChimp( $this->api_key ); 
  428.  
  429. if ( $listid == 'false' ) { 
  430. $listid = $this->list; 
  431.  
  432. $double_optin = ( $this->double_optin == 'yes' ? mc_casl_true : mc_casl_false ); 
  433. $update_existing = mc_casl_true; 
  434. $replace_interests = mc_casl_false; 
  435. $send_welcome = mc_casl_true; 
  436.  
  437. $merge_vars = array('FNAME' => $first_name, 'LNAME'=>$last_name); 
  438.  
  439. $subscribe_data = array( 
  440. 'id' => $listid,  
  441. 'email' => array('email' => $email),  
  442. 'merge_vars' => $merge_vars,  
  443. 'double_optin' => $double_optin,  
  444. 'update_existing' => $update_existing,  
  445. 'replace_interests' => $replace_interests,  
  446. 'send_welcome' => $send_welcome,  
  447. ); 
  448.  
  449. $result = $MailChimp->call('lists/subscribe', $subscribe_data); 
  450. if ( $result['status'] == 'error' ) { 
  451. do_action( 'wc_mailchimp_casl_subscribed', $email ); 
  452. wp_mail( get_option('admin_email'), __( 'WooCommerce MailChimp subscription failed', 'wc_mailchimp_casl' ), '(' . $retval['code'] . ') ' . $retval['error'] ); 
  453. //return false; 
  454. $retstatus = false; 
  455.  
  456. $loggedin = is_user_logged_in() ? mc_casl_true : mc_casl_false; 
  457.  
  458. $timezone = $this->get_option( 'mc_casl_server_timezone' ); 
  459. if ($timezone == '' || empty($timezone)) { 
  460. $timezone = date_default_timezone_get(); 
  461. } else { 
  462. date_default_timezone_set($timezone); 
  463. $current_time = date('H:i:s', time()); 
  464. $current_date = date('Y-m-d', time()); 
  465. $current_ip = mc_casl_client_ip(); 
  466.  
  467. $ip_127 = filter_var($current_ip, FILTER_CALLBACK, array('options' => 'FILTER_FLAG_NO_LOOPBACK_RANGE')) && 
  468. $ip_10_192_172 = filter_var($current_ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE); 
  469. if ($ip_10_192_172 && $ip_127) { 
  470. $merge_vars['optin_ip'] = $current_ip; 
  471.  
  472. $current_country = ucwords( strtolower( mc_casl_client_country( $current_ip ) ) ); 
  473. $language = $merge_vars['mc_language'] = 'en'; 
  474. $merge_vars['optin_time'] = $current_date . ' ' . $current_time; 
  475.  
  476. $useragent = $_SERVER['HTTP_USER_AGENT']; 
  477.  
  478. $useremail = ''; 
  479. $userphone = ''; 
  480. $usercompany = ''; 
  481. $userfname = ''; 
  482. $userlname = ''; 
  483. $username = ''; 
  484. $userid = ''; 
  485. $tnd = $current_date . " " . $current_time; 
  486. $timestamp = strtotime($tnd); 
  487. $loggedin = mc_casl_false; 
  488. if ( is_user_logged_in() ) { 
  489. $userinfo = wp_get_current_user(); 
  490. $loggedin = mc_casl_true; 
  491. $useremail = $userinfo->user_email; 
  492. $userphone = get_user_meta( $userinfo->ID, 'billing_phone', true ); 
  493. $usercompany = get_user_meta( $userinfo->ID, 'billing_company', true ); 
  494. $userfname = $userinfo->user_firstname; 
  495. $userlname = $userinfo->user_lastname; 
  496. $username = $userinfo->user_login; 
  497. $userid = $userinfo->ID; 
  498.  
  499. $status = mc_casl_true; 
  500. if ( $retstatus == false ) { 
  501. $status = mc_casl_false; 
  502.  
  503. $db_data = array( 
  504. 'language' => $language,  
  505. 'ip' => $current_ip,  
  506. 'country' => $current_country,  
  507. 'loggedin' => $loggedin,  
  508. 'frontform' => mc_casl_false,  
  509. 'checkoutform' => mc_casl_true,  
  510. 'email' => $email,  
  511. 'status' => $status,  
  512. 'date' => $current_date,  
  513. 'time' => $current_time,  
  514. 'timestamp' => $timestamp,  
  515. 'dob' => '',  
  516. 'fname' => $first_name,  
  517. 'lname' => $last_name,  
  518. 'useremail' => $useremail,  
  519. 'userphone' => $userphone,  
  520. 'usercompany' => $usercompany,  
  521. 'userfname' => $userfname,  
  522. 'userlname' => $userlname,  
  523. 'username' => $username,  
  524. 'useragent' => $useragent,  
  525. 'userid' => $userid,  
  526. ); 
  527.  
  528. $uld = wp_upload_dir(); 
  529. $storagepath = $uld['basedir'] . "/mailchimp_casl"; 
  530. $dbfile = 'mailchimp_casl.db'; 
  531. $dbfilepath = $storagepath . "/" . $dbfile; 
  532. $db = new MailChimp_CASL_DB( $dbfilepath, false ); 
  533. $err = $db->savedata( $db_data ); 
  534. $db->closedb(); 
  535.  
  536. return $retstatus; 
  537.  
  538. /******************************************************************************************************************/ 
  539.  
  540. public function get_lists() { 
  541. if ( ! $mailchimp_lists = get_transient( 'wc_mailchimp_casl_list_' . md5( $this->api_key ) ) ) { 
  542. $mailchimp_lists = array(); 
  543. $mailchimp_casl = new MailChimp( $this->api_key ); 
  544. $retval = $mailchimp_casl->call('lists/list'); 
  545.  
  546. if ( !isset( $retval ) || !is_array( $retval ) ) { 
  547. echo '<div class="error"><p>Unable to load lists() from MailChimp CASL: (-999) Unknown error wc_mailchimp_casl</p></div>'; 
  548. return false; 
  549. } else if ( isset( $retval['status'] ) && $retval['status'] == 'error' ) { 
  550. echo '<div class="error"><p>' . sprintf( __( 'Unable to load lists() from MailChimp CASL: (%s) %s', 'wc_mailchimp_casl' ), $retval['code'], $retval['error'] ) . '</p></div>'; 
  551. return false; 
  552. } else { 
  553. foreach ( $retval['data'] as $list ) { 
  554. $mailchimp_lists[ $list['id'] ] = $list['name']; 
  555. if ( sizeof( $mailchimp_lists ) > 0 ) { 
  556. set_transient( 'wc_mailchimp_casl_list_' . md5( $this->api_key ), $mailchimp_lists, 60*60*1 ); 
  557. return $mailchimp_lists; 
  558.  
  559. /******************************************************************************************************************/ 
  560.  
  561. static function log( $message ) { 
  562. if ( WP_DEBUG === true ) { 
  563. if ( is_array( $message ) || is_object( $message ) ) { 
  564. error_log( print_r( $message, true ) ); 
  565. } else { 
  566. error_log( $message ); 
  567.  
  568. /******************************************************************************************************************/ 
  569.  
  570. function admin_options() { ?> 
  571. <div class='mc_casl_admin_options'> 
  572. <h3><?php _e( 'MailChimp CASL', 'wc_mailchimp_casl' ); ?></h3> 
  573. <p><?php _e( 'Enter your MailChimp CASL settings below to control how your theme and WooCommerce integration with your MailChimp lists.', 'wc_mailchimp_casl' ); ?></p> 
  574. <?php echo "<p class='mc_casl_disclaimer'>".$this->disclaimer."</p>"; ?> 
  575. <table class="form-table"> 
  576. <?php $this->mc_casl_generate_settings_html(); ?> 
  577. </table> 
  578. </div> 
  579. <?php 
  580.  
  581. /*******************************************************************************************************************/ 
  582. /** WOOCOMMERCE HACKS **/ 
  583. /*******************************************************************************************************************/ 
  584.  
  585. public function mc_casl_generate_settings_html( $form_fields = false ) { 
  586. $html = ''; 
  587. if ( ! $form_fields ) { 
  588. $form_fields = $this->get_form_fields(); 
  589. foreach ( $form_fields as $k => $v ) { 
  590. if ( ! isset( $v['type'] ) || ( $v['type'] == '' ) ) { 
  591. $v['type'] = 'text';  
  592.  
  593. if ( method_exists( $this, 'mc_casl_generate_' . $v['type'] . '_html' ) ) { 
  594. $html .= $this->{'mc_casl_generate_' . $v['type'] . '_html'}( $k, $v ); 
  595. } else { 
  596. $html .= $this->{'mc_casl_generate_text_html'}( $k, $v ); 
  597. echo $html; 
  598.  
  599. /******************************************************************************************************************/ 
  600.  
  601. public function mc_casl_generate_checkbox_html( $key, $data ) { 
  602. $field = $this->plugin_id . $this->id . '_' . $key; 
  603. $defaults = array( 
  604. 'title' => '',  
  605. 'label' => '',  
  606. 'disabled' => false,  
  607. 'class' => '',  
  608. 'css' => '',  
  609. 'type' => 'text',  
  610. 'desc_tip' => false,  
  611. 'description' => '',  
  612. 'custom_attributes' => array() 
  613. ); 
  614.  
  615. $data = wp_parse_args( $data, $defaults ); 
  616. if ( ! $data['label'] ) { 
  617. $data['label'] = $data['title']; 
  618.  
  619. ob_start(); 
  620. ?> 
  621. <tr valign="top"> 
  622. <th scope="row" class="titledesc"> 
  623. <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  624. <?php echo $this->get_tooltip_html( $data ); ?> 
  625. </th> 
  626. <td class="forminp"> 
  627. <fieldset> 
  628. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  629. <label for="<?php echo esc_attr( $field ); ?>"> 
  630. <input <?php disabled( $data['disabled'], true ); ?> class="<?php echo esc_attr( $data['class'] ); ?>" type="checkbox" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="1" <?php checked( $this->get_option( $key ), 'yes' ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> <?php echo wp_kses_post( $data['label'] ); ?></label><br/> 
  631. <?php echo $this->get_description_html( $data ); ?> 
  632. </fieldset> 
  633. </td> 
  634. </tr> 
  635. <?php 
  636. return ob_get_clean(); 
  637.  
  638. /******************************************************************************************************************/ 
  639.  
  640. public function mc_casl_generate_text_html( $key, $data ) { 
  641. $field = $this->plugin_id . $this->id . '_' . $key; 
  642. $defaults = array( 
  643. 'title' => '',  
  644. 'disabled' => false,  
  645. 'class' => '',  
  646. 'css' => '',  
  647. 'placeholder' => '',  
  648. 'type' => 'text',  
  649. 'desc_tip' => false,  
  650. 'description' => '',  
  651. 'custom_attributes' => array() 
  652. ); 
  653.  
  654. $data = wp_parse_args( $data, $defaults ); 
  655.  
  656. ob_start(); 
  657. ?> 
  658. <tr valign="top"> 
  659. <th scope="row" class="titledesc"> 
  660. <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  661. <?php echo $this->get_tooltip_html( $data ); ?> 
  662. </th> 
  663. <td class="forminp"> 
  664. <fieldset> 
  665. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  666. <input class="input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> 
  667. <?php echo $this->get_description_html( $data ); ?> 
  668. </fieldset> 
  669. </td> 
  670. </tr> 
  671. <?php 
  672. return ob_get_clean(); 
  673.  
  674. /******************************************************************************************************************/ 
  675.  
  676. public function mc_casl_generate_select_html( $key, $data ) { 
  677. $field = $this->plugin_id . $this->id . '_' . $key; 
  678. $defaults = array( 
  679. 'title' => '',  
  680. 'disabled' => false,  
  681. 'class' => '',  
  682. 'css' => '',  
  683. 'placeholder' => '',  
  684. 'type' => 'text',  
  685. 'desc_tip' => false,  
  686. 'description' => '',  
  687. 'custom_attributes' => array(),  
  688. 'options' => array() 
  689. ); 
  690.  
  691. $data = wp_parse_args( $data, $defaults ); 
  692.  
  693. ob_start(); 
  694. ?> 
  695. <tr valign="top"> 
  696. <th scope="row" class="titledesc"> 
  697. <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  698. <?php echo $this->get_tooltip_html( $data ); ?> 
  699. </th> 
  700. <td class="forminp"> 
  701. <fieldset> 
  702. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  703. <select class="select <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>> 
  704. <?php foreach ( (array) $data['options'] as $option_key => $option_value ) :  
  705. $selected = selected( $option_key, esc_attr( $this->get_option( $key ) ) ); 
  706. $attrib_key = esc_attr( $option_key ); 
  707. $attrib_value = esc_attr( $option_value ); 
  708. /**if ($key == 'mc_casl_list_groupings') { 
  709. if ($selected != '') { 
  710. $this->attrib_groups_key = $option_key;  
  711. } 
  712. }*/ 
  713. ?> 
  714. <option value="<?php echo $attrib_key; ?>" <?php echo $selected; ?>><?php echo $attrib_value; ?></option> 
  715. <?php endforeach; ?> 
  716. </select> 
  717. <?php echo $this->get_description_html( $data ); ?> 
  718. </fieldset> 
  719. </td> 
  720. </tr> 
  721. <?php 
  722. return ob_get_clean(); 
  723.  
  724. /******************************************************************************************************************/ 
  725.  
  726. public function mc_casl_generate_textarea_html( $key, $data ) { 
  727. $field = $this->plugin_id . $this->id . '_' . $key; 
  728. $defaults = array( 
  729. 'title' => '',  
  730. 'disabled' => false,  
  731. 'class' => '',  
  732. 'css' => '',  
  733. 'placeholder' => '',  
  734. 'type' => 'text',  
  735. 'desc_tip' => false,  
  736. 'description' => '',  
  737. 'custom_attributes' => array() 
  738. ); 
  739.  
  740. $data = wp_parse_args( $data, $defaults ); 
  741.  
  742. ob_start(); 
  743. ?> 
  744. <tr valign="top"> 
  745. <th scope="row" class="titledesc"> 
  746. <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  747. <?php echo $this->get_tooltip_html( $data ); ?> 
  748. </th> 
  749. <td class="forminp"> 
  750. <fieldset> 
  751. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  752. <textarea rows="3" cols="80" class="input-text wide-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>><?php echo esc_textarea( $this->get_option( $key ) ); ?></textarea> 
  753. <?php echo $this->get_description_html( $data ); ?> 
  754. </fieldset> 
  755. </td> 
  756. </tr> 
  757. <?php 
  758. return ob_get_clean(); 
  759.  
  760. /******************************************************************************************************************/ 
  761.  
  762. public function mc_casl_generate_button_html( $key, $data ) { 
  763. $field = $this->plugin_id . $this->id . '_' . $key; 
  764. $defaults = array( 
  765. 'title' => '',  
  766. 'name' => '',  
  767. 'disabled' => false,  
  768. 'class' => '',  
  769. 'css' => '',  
  770. 'value' => '',  
  771. 'type' => 'text',  
  772. 'desc_tip' => false,  
  773. 'description' => '',  
  774. 'custom_attributes' => array() 
  775. ); 
  776.  
  777. $data = wp_parse_args( $data, $defaults ); 
  778. ob_start(); 
  779. ?> 
  780. <tr valign="top"> 
  781. <th scope="row" class="titledesc"> 
  782. <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  783. <?php echo $this->get_tooltip_html( $data ); ?> 
  784. </th> 
  785. <td class="forminp"> 
  786. <fieldset> 
  787. <input name="<?php echo wp_kses_post( $data['name'] ); ?>" id="<?php echo wp_kses_post( $data['name'] ); ?>" class="button-secondary" type="button" value="<?php echo wp_kses_post( $data['value'] ); ?>"> 
  788. <?php echo $this->get_description_html( $data ); ?> 
  789. </fieldset> 
  790. </td> 
  791. </tr> 
  792. <?php 
  793. return ob_get_clean(); 
  794.  
  795. ?> 
.