NMMailChimp

[1] TODO: change the class name of your plugin.

Defined (1)

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

/classes/plugin.class.php  
  1. class NMMailChimp extends NM_Framwork_V2_nm_mailchimp{ 
  2.  
  3. private static $ins = null; 
  4. static $mc_table = 'mc_forms'; 
  5.  
  6. var $mc; //Mailchimp API class special object 
  7. var $form_id; 
  8.  
  9. public static function init() 
  10. add_action('plugins_loaded', array(self::get_instance(), '_setup')); 
  11.  
  12. public static function get_instance() 
  13. // create a new object if it doesn't exist. 
  14. is_null(self::$ins) && self::$ins = new self; 
  15. return self::$ins; 
  16.  
  17.  
  18. function _setup() { 
  19.  
  20. //setting plugin meta saved in config.php 
  21. $this -> plugin_meta = get_plugin_meta_nm_mailchimp(); 
  22.  
  23. //getting saved settings 
  24. $this -> plugin_settings = get_option ( $this -> plugin_meta['shortname'] . '_settings' ); 
  25.  
  26.  
  27.  
  28. //initializing Mailchimp object 
  29. if( $this->get_option('_mc_api_key') != '') 
  30. $this -> mc = new NM_Mailchimp( trim($this->get_option('_mc_api_key')) ); 
  31.  
  32. /** 
  33. * [2] 
  34. * TODO: update scripts array for SHIPPED scripts 
  35. * only use handlers 
  36. */ 
  37. //setting shipped scripts 
  38. $this -> wp_shipped_scripts = array('jquery'); 
  39.  
  40.  
  41. /** 
  42. * [3] 
  43. * TODO: update scripts array for custom scripts/styles 
  44. */ 
  45. //setting plugin settings 
  46. $this -> plugin_scripts = array(array( 'script_name' => 'scripts',  
  47. 'script_source' => '/js/script.js',  
  48. 'localized' => true,  
  49. 'type' => 'js',  
  50. 'depends' => array('jquery'),  
  51. 'in_footer' => false,  
  52. 'version' => false,  
  53. ),  
  54. array( 'script_name' => 'styles',  
  55. 'script_source' => '/plugin.styles.css',  
  56. 'localized' => false,  
  57. 'type' => 'style',  
  58. 'in_footer' => false,  
  59. 'version' => false,  
  60. ),  
  61. ); 
  62.  
  63. /** 
  64. * [4] 
  65. * TODO: localized array that will be used in JS files 
  66. * Localized object will always be your pluginshortname_vars 
  67. * e.g: pluginshortname_vars.ajaxurl 
  68. */ 
  69.  
  70. $messages = array('error_subscription' => __('Please try with different email', 'nm-mailchimp')); 
  71. $this -> localized_vars = array( 'ajaxurl' => admin_url( 'admin-ajax.php', (is_ssl() ? 'https' : 'http') ),  
  72. 'plugin_url' => $this->plugin_meta['url'],  
  73. 'plugin_doing' => $this->plugin_meta['url'] . 'images/loading.gif',  
  74. 'settings' => $this -> plugin_settings,  
  75. 'messages' => $messages,  
  76. ); 
  77.  
  78.  
  79. /** 
  80. * [5] 
  81. * TODO: this array will grow as plugin grow 
  82. * all functions which need to be called back MUST be in this array 
  83. * setting callbacks 
  84. * Updated V2: September 16, 2014 
  85. * Now truee/false against each function 
  86. * true: logged in 
  87. * false: visitor + logged in 
  88. */ 
  89.  
  90. //following array are functions name and ajax callback handlers 
  91. $this -> ajax_callbacks = array('none' => true,  
  92. 'get_mc_list' => false,  
  93. 'get_mc_list_vars' => false,  
  94. 'mc_add_var' => false,  
  95. 'mc_del_var' => false,  
  96. 'get_mc_list_groups' => false,  
  97. 'mc_add_interest' => false,  
  98. 'mc_del_interest' => false,  
  99. 'mc_add_group' => false,  
  100. 'mc_save_list_val' => false,  
  101. 'woo_mc_save_list_val' => false,  
  102. 'mc_del_group' => false,  
  103. 'get_form_shortcode' => false,  
  104. 'create_shortcode' => false,  
  105. 'get_forms' => false,  
  106. 'subscribe_user' => true,  
  107. 'remove_form' => false,  
  108. 'get_mc_campaigns_lists' => false, //do not change this action, is for admin 
  109. 'template_preview' => false, //do not change this action, is for admin 
  110. 'create_campaign' => false, //do not change this action, is for admin 
  111. 'delete_campaign' => false, //do not change this action, is for admin 
  112. 'send_campaign_test' => false, //do not change this action, is for admin 
  113. 'send_campaign' => false, //do not change this action, is for admin,  
  114. 'validate_api' => false, // validating api key 
  115. 'form_designer' => false); // Subscription Form Designer 
  116.  
  117. /** 
  118. * plugin localization being initiated here 
  119. */ 
  120. add_action('init', array($this, 'wpp_textdomain')); 
  121.  
  122.  
  123. /** 
  124. * plugin main shortcode if needed 
  125. */ 
  126. add_shortcode('nm-mc-form', array($this , 'render_subscription_form')); 
  127.  
  128.  
  129. /** 
  130. * hooking up scripts for front-end 
  131. */ 
  132. add_action('wp_enqueue_scripts', array($this, 'load_scripts')); 
  133.  
  134. /** 
  135. * registering callbacks 
  136. */ 
  137. $this -> do_callbacks(); 
  138.  
  139. add_action('setup_styles_and_scripts_nm_mailchimp', array($this, 'get_connected_to_load_it')); 
  140.  
  141.  
  142.  
  143.  
  144. function get_plugin_settings() { 
  145.  
  146. $temp_settings = array(); 
  147. foreach($this -> plugin_setting_tabs as $tab) { 
  148.  
  149. $temp_settings[$tab] = get_option( $tab . '_settings' ); 
  150.  
  151. //$this -> pa($temp_settings); 
  152.  
  153. return $temp_settings; 
  154.  
  155.  
  156. /** 
  157. * =============== NOW do your JOB =========================== 
  158. *  
  159. */ 
  160.  
  161. // i18n and l10n support here 
  162. // plugin localization 
  163. function wpp_textdomain() { 
  164.  
  165. $locale_dir = dirname( plugin_basename( dirname(__FILE__ ) ) ) . '/locale/'; 
  166. load_plugin_textdomain('nm-mailchimp', false, $locale_dir); 
  167.  
  168.  
  169.  
  170. //getting Mailchimp Account Lists 
  171. function get_mc_list() { 
  172.  
  173. $mc_lists = $this -> mc -> lists -> getList(); 
  174.  
  175. echo json_encode( $mc_lists ); 
  176.  
  177. die(0); 
  178.  
  179. //getting Mailchimp Account Lists Vars 
  180. function get_mc_list_vars() { 
  181.  
  182. //var_dump($_REQUEST['list_ids']); 
  183. $mc_lists_vars = $this -> mc -> lists -> mergeVars( $_REQUEST['list_ids'] ); 
  184.  
  185. echo json_encode( $mc_lists_vars ); 
  186.  
  187. die(0); 
  188.  
  189. //getting Mailchimp Adding List Vars 
  190. function mc_add_var() { 
  191.  
  192. $list_id = sanitize_text_field($_REQUEST['listid']); 
  193. $tag = sanitize_text_field( strtoupper($_REQUEST['vars']['tag']) ); 
  194. $name = sanitize_text_field( strtoupper($_REQUEST['vars']['name']) ); 
  195.  
  196. $options = $_REQUEST['vars']['options']; 
  197. $mc_var_added = $this -> mc -> lists -> mergeVarAdd($list_id, $tag, $name, $options); 
  198.  
  199. echo json_encode( $mc_var_added ); 
  200.  
  201. die(0); 
  202.  
  203. //getting Mailchimp Deleting List Vars 
  204. function mc_del_var() { 
  205.  
  206. //print_r($_REQUEST); exit; 
  207. $list_id = sanitize_text_field($_REQUEST['listid']); 
  208. $tag = sanitize_text_field( strtoupper($_REQUEST['tag']) ); 
  209.  
  210. $mc_var_deleted = $this -> mc -> lists -> mergeVarDel($list_id, $tag); 
  211.  
  212. echo json_encode( $mc_var_deleted ); 
  213.  
  214. die(0); 
  215.  
  216. //getting Mailchimp Account Lists Groups 
  217. function get_mc_list_groups() { 
  218.  
  219. $mc_lists_groups = $this -> mc -> lists -> interestGroupings( $_REQUEST['list_id'] ); 
  220.  
  221. //var_dump($mc_lists_groups); 
  222.  
  223. echo json_encode( $mc_lists_groups ); 
  224. die(0); 
  225.  
  226.  
  227. function mc_add_group() { 
  228.  
  229. //print_r($_REQUEST); exit; 
  230. $list_id = sanitize_text_field($_REQUEST['listid']); 
  231. $gname = sanitize_text_field( $_REQUEST['groupname'] ); 
  232. $grpingid = sanitize_text_field( $_REQUEST['groupingid'] ); 
  233.  
  234. $mc_grp_added = $this -> mc -> lists -> interestGroupAdd($list_id, $gname, $grpingid); 
  235.  
  236. echo json_encode( $mc_grp_added ); 
  237.  
  238. die(0); 
  239.  
  240. function mc_del_group() { 
  241.  
  242. //print_r($_REQUEST); exit; 
  243. $list_id = sanitize_text_field($_REQUEST['listid']); 
  244. $gname = sanitize_text_field( $_REQUEST['groupname'] ); 
  245. $grpingid = sanitize_text_field( $_REQUEST['groupingid'] ); 
  246.  
  247. $mc_grp_deleted = $this -> mc -> lists -> interestGroupDel($list_id, $gname, $grpingid); 
  248.  
  249. echo json_encode( $mc_grp_deleted ); 
  250.  
  251. die(0); 
  252.  
  253. function mc_save_list_val() { 
  254. if (isset($_REQUEST['list_val'])) { 
  255. $list_val = $_REQUEST['list_val']; 
  256. update_option( 'nm_mailchimp_list_val', $list_val); 
  257. die(0); 
  258.  
  259. function woo_mc_save_list_val() { 
  260. if (isset($_REQUEST['list_val'])) { 
  261. $list_val = $_REQUEST['list_val']; 
  262. update_option( 'nm_woo_mailchimp_list_val', $list_val); 
  263. die(0); 
  264.  
  265. function mc_add_interest() { 
  266.  
  267. //print_r($_REQUEST); exit; 
  268. $list_id = sanitize_text_field($_REQUEST['listid']); 
  269. $groups = $_REQUEST['groups']; 
  270. $gname = sanitize_text_field( $_REQUEST['name'] ); 
  271. $type = sanitize_text_field( $_REQUEST['type'] ); 
  272.  
  273. $mc_grp_added = $this -> mc -> lists -> interestGroupingAdd($list_id, $gname, $type, $groups); 
  274.  
  275. echo json_encode( $mc_grp_added ); 
  276.  
  277. die(0); 
  278.  
  279. function mc_del_interest() { 
  280.  
  281. $grouping_id = $_REQUEST['groupingid']; 
  282.  
  283. $mc_grp_deleted = $this -> mc -> lists -> interestGroupingDel($grouping_id); 
  284.  
  285. echo json_encode( $mc_grp_deleted ); 
  286. die(0); 
  287.  
  288. /** 
  289. * Mailchimp campaigns related 
  290. */ 
  291.  
  292. function get_mc_campaigns_lists() { 
  293.  
  294. $mc_campaigns = $this -> mc -> campaigns -> getList(); 
  295. $mc_lists = $this -> mc -> lists -> getList(); 
  296.  
  297. $both = array( 'campaigns' => $mc_campaigns,  
  298. 'lists' => $mc_lists); 
  299.  
  300.  
  301. echo json_encode( $both ); 
  302.  
  303. die(0); 
  304. }  
  305.  
  306. function template_preview() { 
  307. $cam_post_id = $_REQUEST['contentid']; 
  308. $email_contents = get_post($cam_post_id); 
  309. echo json_encode($email_contents); 
  310. die(0); 
  311.  
  312. //Create New Campaign 
  313. function create_campaign() { 
  314. $new_cam_data = $_REQUEST['campaign_data']; 
  315. extract($new_cam_data); 
  316.  
  317. $options['tracking']['html_clicks'] = ($options['tracking']['html_clicks'] == 'true' ? true : false); 
  318. $options['tracking']['text_clicks'] = ($options['tracking']['text_clicks'] == 'true' ? true : false); 
  319. $options['tracking']['opens'] = ($options['tracking']['opens'] == 'true' ? true : false); 
  320. $options['authenticate'] = ($options['authenticate'] == 'true' ? true : false); 
  321. $options['generate_text'] = ($options['generate_text'] == 'true' ? true : false); 
  322. $options['fb_comments'] = ($options['fb_comments'] == 'true' ? true : false); 
  323. $options['ecomm360'] = ($options['ecomm360'] == 'true' ? true : false); 
  324. $options['auto_tweet'] = ($options['auto_tweet'] == 'true' ? true : false); 
  325.  
  326. $content = array('html' => stripcslashes($content['html']),  
  327. 'text' => stripcslashes($content['text']) 
  328. ); 
  329. var_dump($options); 
  330. var_dump($content); 
  331.  
  332. $res = $this -> mc -> campaigns -> create($type, $options, $content); 
  333. var_dump($res); 
  334.  
  335. die(0); 
  336.  
  337.  
  338. function delete_campaign() { 
  339. $campaign_id = $_REQUEST['campaign_id']; 
  340. $res = $this -> mc -> campaigns -> delete($campaign_id); 
  341. echo $res; 
  342.  
  343. die(0); 
  344.  
  345. function send_campaign_test() { 
  346.  
  347. $emails = array(get_bloginfo('admin_email')); 
  348. $campaign_id = $_REQUEST['campaign_id']; 
  349.  
  350. $res = $this -> mc -> campaigns -> sendTest($campaign_id, $emails); 
  351. echo $res; 
  352.  
  353. die(0); 
  354.  
  355. function send_campaign() { 
  356.  
  357. $campaign_id = $_REQUEST['campaign_id']; 
  358.  
  359. $res = $this -> mc -> campaigns -> send($campaign_id); 
  360. echo $res; 
  361.  
  362. die(0); 
  363. /** ========= Forms related =============== */ 
  364.  
  365. // Form Designer data 
  366. function form_designer() { 
  367. if (isset($_REQUEST['formData'])) { 
  368. $newData = $_REQUEST['formData']; 
  369. update_option( 'nm_mailchimp_form_settings', $newData); 
  370.  
  371. function get_forms() { 
  372.  
  373. $forms = $this -> getAllForms(); 
  374.  
  375. echo json_encode($forms); 
  376. die(0); 
  377.  
  378. function remove_form() { 
  379.  
  380. $where_data = array('d' => array('form_id' => intval($_REQUEST['formid']) )); 
  381.  
  382. $del_resp = $this -> delete_data(NMMailChimp::$mc_table, $where_data, 'AND', true); 
  383.  
  384. echo $del_resp; 
  385. die(0); 
  386.  
  387. function create_shortcode() { 
  388.  
  389. global $wpdb; 
  390.  
  391. //print_r($_REQUEST); exit; 
  392. extract ( $_REQUEST ); 
  393.  
  394. $dt = array ( 
  395. 'form_name' => sanitize_text_field( $form_name ),  
  396. 'form_meta' => json_encode ( $form_meta ),  
  397. 'form_created' => current_time ( 'mysql' )  
  398. ); 
  399.  
  400. $format = array ( 
  401. '%s',  
  402. '%s',  
  403. '%s',  
  404. ); 
  405.  
  406. $res_id = $this->insert_table ( self::$mc_table, $dt, $format ); 
  407.  
  408. $resp = array (); 
  409. if ($res_id) { 
  410.  
  411. $resp = array ( 
  412. 'message' => __ ( 'Form added successfully', $this->plugin_meta ['shortname'] ),  
  413. 'status' => 'success',  
  414. 'form_id' => $wpdb->insert_id  
  415. ); 
  416. } else { 
  417.  
  418. $resp = array ( 
  419. 'message' => __ ( 'Error while savign form, please try again', $this->plugin_meta ['shortname'] ),  
  420. 'status' => 'failed',  
  421. 'form_id' => ''  
  422. ); 
  423.  
  424. echo json_encode ( $resp ); 
  425.  
  426. die(0); 
  427.  
  428.  
  429.  
  430. function render_subscription_form($atts) { 
  431.  
  432. extract ( shortcode_atts ( array ( 
  433. 'fid' => ''  
  434. ), $atts ) ); 
  435.  
  436. $this->form_id = $fid; 
  437.  
  438. if($this -> get_option('_modal') == 'yes') { 
  439.  
  440. add_thickbox(); 
  441.  
  442. $modal_size = $this->get_option('_modal_size'); 
  443. $modal_w = $modal_h = 300; 
  444. if($modal_size) { 
  445. $temp_size = explode(', ', $modal_size); 
  446. $modal_w = $temp_size[0]; 
  447. $modal_h = $temp_size[1]; 
  448.  
  449. echo '<a title="'.$this->get_option('_modal_title').'" href="#TB_inline?inlineId=mc-modal-'.$fid.'&wdith='.$modal_w.'&height='.$modal_h.'" class="mc-modal thickbox">'; 
  450. echo $this->get_option('_modal_title'); 
  451. echo '</a>'; 
  452.  
  453. echo '<div id="mc-modal-'.$fid.'" style="display:none;"><div id="nm-mailchimp-modal">'; 
  454.  
  455. //some contents if required 
  456. echo $this->get_option('_modal_content'); 
  457.  
  458. $pass_data = array('form_id' => $fid); 
  459. $this->load_template ( 'render.subscription.php', $pass_data ); 
  460. echo '</div></div>'; 
  461.  
  462. }else{ 
  463.  
  464. ob_start (); 
  465.  
  466. $pass_data = array('form_id' => $fid); 
  467. $this->load_template ( 'render.subscription.php', $pass_data ); 
  468.  
  469. $output_string = ob_get_contents (); 
  470. ob_end_clean (); 
  471.  
  472. return $output_string; 
  473.  
  474.  
  475. /** 
  476. * this function returns the proper header 
  477. * with wp site title and admin email 
  478. */ 
  479.  
  480. function get_mail_header() { 
  481.  
  482. $site_title = get_bloginfo(); 
  483. $admin_email = get_bloginfo('admin_email'); 
  484.  
  485. $headers[] = "From: {$site_title}<{$admin_email}>"; 
  486. $headers[] = "Content-Type: text/html"; 
  487. $headers[] = "MIME-Version: 1.0\r\n"; 
  488.  
  489. return $headers; 
  490.  
  491. function subscribe_user() { 
  492.  
  493.  
  494. $email = (isset($_REQUEST['vars']['EMAIL']) ? $_REQUEST['vars']['EMAIL'] : ''); 
  495. $listid = (isset($_REQUEST['listid']) ? $_REQUEST['listid'] : ''); 
  496. $vars = (isset($_REQUEST['vars']) ? $_REQUEST['vars'] : ''); 
  497. $intrests = (isset($_REQUEST['intrests']) ? $_REQUEST['intrests'] : ''); 
  498.  
  499.  
  500. $intrests_vars = ''; 
  501.  
  502. if($intrests) { 
  503. $groupings = ''; 
  504. foreach($intrests as $intrest_id => $groups) { 
  505. $groupings['id'] = $intrest_id; 
  506. $groups_name = array(); 
  507. foreach($groups as $groupd_id => $group_name) { 
  508. $groups_name[] = $group_name; 
  509. $groupings['groups'] = $groups_name; 
  510.  
  511. $intrests_vars[] = $groupings; 
  512.  
  513.  
  514. if($intrests_vars) { 
  515. $vars['groupings'] = $intrests_vars; 
  516.  
  517. $resp = ''; 
  518. try { 
  519.  
  520. $subscriber = $this -> mc -> lists -> subscribe($listid, array('email' => htmlentities($email)), $vars); 
  521.  
  522. if ( ! empty( $subscriber['leid'] ) ) { 
  523. $resp = array('status' => 'success', 'message' => $this->get_option('_ok_message')); 
  524. else 
  525. $resp = array('status' => 'error', 'message' => __('Please try again', 'nm-mailchimp')); 
  526.  
  527. } catch (Exception $e) { 
  528.  
  529. $resp = array('status' => 'error', 'message' => $e->getMessage()); 
  530.  
  531.  
  532.  
  533. // create user if admin set it to yes 
  534. if($this -> get_option('_create_user') == 'yes') { 
  535. $pwd = wp_generate_password( 12, false ); 
  536. $userdata = array( 'user_pass' => $pwd,  
  537. 'user_login' => $email,  
  538. //'first_name' => $wp_registration_first_name,  
  539. //'last_name' => $wp_registration_last_name,  
  540. 'user_email' => $email,  
  541. ); 
  542.  
  543. $userid = wp_insert_user( $userdata ); 
  544.  
  545. // send user info to admin 
  546. if ( $userid && !is_wp_error( $userid ) && $this -> get_option('_user_detail') == 'yes') { 
  547.  
  548. $suject = __('New user registration ', $this->plugin_meta ['shortname']) . $email; 
  549.  
  550. $msg = $email."\n ". "<br />"; 
  551. //$msg = str_replace("%USER_LOGIN%", $nm_email, $msg); 
  552. $msg .= $pwd ."\n ". "<br />"; 
  553. //$msg .= str_replace("%SITE_NAME%", $site_title, $msg); 
  554.  
  555. $msg .= '<p><a href="' . wp_login_url() . '" title="Login">'.__('Click here to login', $this->plugin_meta ['shortname']).'</a>'; 
  556. //$msg = str_replace("\n", "<br />", $msg); 
  557.  
  558.  
  559. wp_mail( $email, $suject, $msg, $this->get_mail_header() ); 
  560.  
  561.  
  562.  
  563. echo json_encode($resp); 
  564.  
  565. die(0); 
  566.  
  567. // ================================ SOME HELPER FUNCTIONS ========================================= 
  568.  
  569.  
  570. function getAllForms() { 
  571.  
  572. $select = array('mc_forms' => '*'); 
  573. $all_forms = $this -> get_rows_data($select); 
  574.  
  575. return $all_forms; 
  576.  
  577. function activate_plugin() { 
  578.  
  579. /** 
  580. * NOTE: $plugin_meta is not object of this class, it is constant  
  581. * defined in config.php 
  582. */ 
  583.  
  584. global $wpdb, $plugin_meta; 
  585.  
  586. $tbl_name = $wpdb->prefix . self::$mc_table; 
  587.  
  588. $sql = "CREATE TABLE $tbl_name ( 
  589. form_id INT( 7 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,  
  590. form_name MEDIUMTEXT NOT NULL,  
  591. form_meta MEDIUMTEXT NOT NULL ,  
  592. form_created DATETIME NOT NULL 
  593. );"; 
  594.  
  595.  
  596. require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 
  597. dbDelta($sql); 
  598.  
  599. add_option("nm_mc_db_version", $plugin_meta['plugin_version']); 
  600.  
  601. if ( ! wp_next_scheduled( 'setup_styles_and_scripts_nm_mailchimp' ) ) { 
  602. wp_schedule_event( time(), 'daily', 'setup_styles_and_scripts_nm_mailchimp'); 
  603.  
  604.  
  605. function deactivate_plugin() { 
  606.  
  607. wp_clear_scheduled_hook( 'setup_styles_and_scripts_nm_mailchimp' ); 
  608.  
  609.  
  610.  
  611. /** 
  612. * is it real plugin 
  613. */ 
  614. function get_real_plugin_first() { 
  615.  
  616. $hashcode = get_option ( $this->plugin_meta ['shortname'] . '_hashcode' ); 
  617. $hash_file = $this -> plugin_meta['path'] . '/assets/_hashfile.txt'; 
  618. if ( file_exists( $hash_file )) { 
  619. return $hashcode; 
  620. }else{  
  621. return $hashcode; 
  622.  
  623. function get_plugin_hashcode() { 
  624.  
  625. $key = $_SERVER['HTTP_HOST']; 
  626. return hash( 'md5', $key ); 
  627.  
  628. function get_connected_to_load_it() { 
  629.  
  630. $apikey = get_option( $this->plugin_meta ['shortname'] . '_apikey'); 
  631. self::validate_api( $apikey ); 
  632.  
  633.  
  634.  
  635.  
  636.  
  637. function validate_api($apikey = null) { 
  638.  
  639. //webcontact_pa($_REQUEST); 
  640. $api_key = ($apikey != null ? $apikey : $_REQUEST['plugin_api_key']); 
  641. $the_params = array('verify' => 'plugin', 'plugin_api_key' => $api_key, 'domain' => $_SERVER['HTTP_HOST'], 'ip' => $_SERVER['REMOTE_ADDR']); 
  642. $uri = ''; 
  643. foreach ($the_params as $key => $val) { 
  644.  
  645. $uri .= $key . '=' . urlencode($val) . '&'; 
  646.  
  647. $uri = substr($uri, 0, -1); 
  648.  
  649. $endpoint = "http://www.wordpresspoets.com/?$uri"; 
  650.  
  651. $resp = wp_remote_get($endpoint); 
  652. //$this->pa($resp); 
  653.  
  654. $callback_resp = array('status' => '', 'message' => ''); 
  655.  
  656. if (is_wp_error($resp)) { 
  657.  
  658. $callback_resp = array('status' => 'success', 'message' => "Plugin activated"); 
  659.  
  660. $hashkey = $_SERVER['HTTP_HOST']; 
  661. $hash_code = hash('md5', $hashkey); 
  662.  
  663. update_option($this -> plugin_meta['shortname'] . '_hashcode', $hash_code); 
  664. //saving api key 
  665. update_option($this -> plugin_meta['shortname'] . '_apikey', $api_key); 
  666.  
  667. $headers[] = "From: NM Plugins<noreply@najeebmedia.com>"; 
  668. $headers[] = "Content-Type: text/html"; 
  669. $report_to = 'sales@najeebmedia.com'; 
  670. $subject = 'Plugin API Issue - ' . $_SERVER['HTTP_HOST']; 
  671. $message = 'Error code: ' . $resp -> get_error_message(); 
  672. $message .= '<br>Error message: ' . $response -> message; 
  673. $message .= '<br>API Key: ' . $api_key; 
  674.  
  675. if (get_option($this -> plugin_meta['shortname'] . '_apikey') != '') { 
  676. wp_mail($report_to, $subject, $message, $headers); 
  677.  
  678. } else { 
  679.  
  680. $response = json_decode($resp['body']); 
  681. //nm_personalizedproduct_pa($response); 
  682. if ($response -> code != 1) { 
  683.  
  684. if ($response -> code == 2 || $response -> code == 3) { 
  685. $headers[] = "From: NM Plugins 
  686. <noreply@najeebmedia.com> 
  687. "; 
  688. $headers[] = "Content-Type: text/html"; 
  689. $report_to = 'sales@najeebmedia.com'; 
  690. $subject = 'Plugin API Issue - ' . $_SERVER['HTTP_HOST']; 
  691. $message = 'Error code: ' . $response -> code; 
  692. $message .= ' 
  693. <br> 
  694. Error message: ' . $response -> message; 
  695. $message .= ' 
  696. <br> 
  697. API Key: ' . $api_key; 
  698.  
  699. if (get_option($this -> plugin_meta['shortname'] . '_apikey') != '') { 
  700. wp_mail($report_to, $subject, $message, $headers); 
  701.  
  702. $callback_resp = array('status' => 'error', 'message' => $response -> message); 
  703.  
  704. delete_option($this -> plugin_meta['shortname'] . '_apikey'); 
  705. delete_option($this -> plugin_meta['shortname'] . '_hashcode'); 
  706.  
  707. } else { 
  708. $callback_resp = array('status' => 'success', 'message' => $response -> message); 
  709.  
  710. $hash_code = $response -> hashcode; 
  711.  
  712. update_option($this -> plugin_meta['shortname'] . '_hashcode', $hash_code); 
  713. //saving api key 
  714. update_option($this -> plugin_meta['shortname'] . '_apikey', $api_key); 
  715.  
  716.  
  717. //$this -> pa($callback_resp); 
  718. echo json_encode($callback_resp); 
  719.  
  720. die(0);