M_Ping

The Membership 2 M Ping class.

Defined (1)

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

/app_old/membershipincludes/classes/class.ping.php  
  1. class M_Ping { 
  2.  
  3. var $build = 1; 
  4.  
  5. var $db; 
  6. var $tables = array('membership_levels', 'membership_rules', 'subscriptions', 'subscriptions_levels', 'membership_relationships', 'membermeta', 'communications', 'urlgroups', 'ping_history', 'pings'); 
  7.  
  8. var $membership_levels; 
  9. var $membership_rules; 
  10. var $membership_relationships; 
  11. var $subscriptions; 
  12. var $subscriptions_levels; 
  13. var $membermeta; 
  14. var $communications; 
  15. var $urlgroups; 
  16. var $ping_history; 
  17. var $pings; 
  18.  
  19. // if the data needs reloaded, or hasn't been loaded yet 
  20. var $dirty = true; 
  21.  
  22. var $ping; 
  23. var $id; 
  24.  
  25. var $pingconstants = array( '%blogname%' => '',  
  26. '%blogurl%' => '',  
  27. '%username%' => '',  
  28. '%usernicename%' => '',  
  29. '%useremail%' => '',  
  30. '%userid%' => '',  
  31. '%networkname%' => '',  
  32. '%networkurl%' => '',  
  33. '%subscriptionname%' => '',  
  34. '%levelname%' => '',  
  35. '%timestamp%' => '' 
  36. ); 
  37.  
  38. function __construct( $id = false) { 
  39.  
  40. global $wpdb; 
  41.  
  42. $this->db =& $wpdb; 
  43.  
  44. foreach($this->tables as $table) { 
  45. $this->$table = membership_db_prefix($this->db, $table); 
  46.  
  47. $this->id = $id; 
  48.  
  49.  
  50. function M_Ping( $id = false ) { 
  51. $this->__construct( $id ); 
  52.  
  53. function ping_name() { 
  54. $this->ping = $this->get_ping(); 
  55.  
  56. return $this->ping->pingname; 
  57.  
  58. function ping_url() { 
  59. $this->ping = $this->get_ping(); 
  60.  
  61. return $this->ping->pingurl; 
  62.  
  63. function get_ping( $force = false ) { 
  64.  
  65. if(!empty($this->ping) && !$force) { 
  66. return $this->ping; 
  67. } else { 
  68. $sql = $this->db->prepare( "SELECT * FROM {$this->pings} WHERE id = %d ", $this->id ); 
  69.  
  70. return $this->db->get_row( $sql ); 
  71.  
  72.  
  73. function get_specifc_ping( $id ) { 
  74.  
  75. $sql = $this->db->prepare( "SELECT * FROM {$this->pings} WHERE id = %d ", $id ); 
  76.  
  77. return $this->db->get_row( $sql ); 
  78.  
  79.  
  80. function editform() { 
  81.  
  82. $this->ping = $this->get_ping(); 
  83.  
  84. echo '<table class="form-table">'; 
  85.  
  86. echo '<tr class="form-field form-required">'; 
  87. echo '<th style="" scope="row" valign="top">' . __('Ping name', 'membership') . '</th>'; 
  88. echo '<td valign="top"><input name="pingname" type="text" size="50" title="' . __('Ping name', 'membership') . '" style="width: 50%;" value="' . esc_attr(stripslashes($this->ping->pingname)) . '" /></td>'; 
  89. echo '</tr>'; 
  90.  
  91. echo '<tr class="form-field form-required">'; 
  92. echo '<th style="" scope="row" valign="top">' . __('Ping URL', 'membership') . '</th>'; 
  93. echo '<td valign="top"><input name="pingurl" type="text" size="50" title="' . __('Ping URL', 'membership') . '" style="width: 50%;" value="' . esc_attr(stripslashes($this->ping->pingurl)) . '" /></td>'; 
  94. echo '</tr>'; 
  95.  
  96. echo '<tr class="form-field form-required">'; 
  97. echo '<th style="" scope="row" valign="top">' . __('Ping data', 'membership') . '</th>'; 
  98. echo '<td valign="top"><textarea name="pinginfo" rows="15" cols="40" style="float: left; width: 40%;">' . esc_html(stripslashes($this->ping->pinginfo)) . '</textarea>'; 
  99. // Display some instructions for the message. 
  100. echo '<div class="instructions" style="float: left; width: 40%; margin-left: 10px;">'; 
  101. echo __('You can use the following constants within the message body to embed database information.', 'membership'); 
  102. echo '<br /><em>'; 
  103.  
  104. echo implode('<br/>', array_keys(apply_filters('membership_ping_constants_list', $this->pingconstants)) ); 
  105.  
  106. echo '</em><br/>' . __('One entry per line. e.g. key=value', 'membership'); 
  107. echo '</div>'; 
  108.  
  109. echo '</td>'; 
  110. echo '</tr>'; 
  111.  
  112. echo '<tr class="form-field form-required">'; 
  113. echo '<th style="" scope="row" valign="top">' . __('Ping method', 'membership') . '</th>'; 
  114. echo '<td valign="top" align="left">'; 
  115. echo '<select name="pingtype">'; 
  116. echo '<option value="GET"'; 
  117. if($this->ping->pingtype == 'GET') echo ' selected="selected"'; 
  118. echo '>' . __('GET', 'membership') . '</option>'; 
  119. echo '<option value="POST"'; 
  120. if($this->ping->pingtype == 'POST') echo ' selected="selected"'; 
  121. echo '>' . __('POST', 'membership') . '</option>'; 
  122. echo '</select>'; 
  123. echo '</td></tr>'; 
  124.  
  125. echo '</table>'; 
  126.  
  127.  
  128. function addform() { 
  129.  
  130. echo '<table class="form-table">'; 
  131.  
  132. echo '<tr class="form-field form-required">'; 
  133. echo '<th style="" scope="row" valign="top">' . __('Ping name', 'membership') . '</th>'; 
  134. echo '<td valign="top"><input name="pingname" type="text" size="50" title="' . __('Ping name', 'membership') . '" style="width: 50%;" value="" /></td>'; 
  135. echo '</tr>'; 
  136.  
  137. echo '<tr class="form-field form-required">'; 
  138. echo '<th style="" scope="row" valign="top">' . __('Ping URL', 'membership') . '</th>'; 
  139. echo '<td valign="top"><input name="pingurl" type="text" size="50" title="' . __('Ping URL', 'membership') . '" style="width: 50%;" value="" /></td>'; 
  140. echo '</tr>'; 
  141.  
  142. echo '<tr class="form-field form-required">'; 
  143. echo '<th style="" scope="row" valign="top">' . __('Ping data', 'membership') . '</th>'; 
  144. echo '<td valign="top"><textarea name="pinginfo" rows="15" cols="40" style="float: left; width: 40%;"></textarea>'; 
  145. // Display some instructions for the message. 
  146. echo '<div class="instructions" style="float: left; width: 40%; margin-left: 10px;">'; 
  147. echo __('You can use the following constants within the message body to embed database information.', 'membership'); 
  148. echo '<br /><em>'; 
  149.  
  150. echo implode('<br/>', array_keys(apply_filters('membership_ping_constants_list', $this->pingconstants)) ); 
  151.  
  152. echo '</em><br/>' . __('One entry per line. e.g. key=value', 'membership'); 
  153.  
  154. echo '</div>'; 
  155. echo '</td>'; 
  156. echo '</tr>'; 
  157.  
  158. echo '<tr class="form-field form-required">'; 
  159. echo '<th style="" scope="row" valign="top">' . __('Ping method', 'membership') . '</th>'; 
  160. echo '<td valign="top" align="left">'; 
  161. echo '<select name="pingtype">'; 
  162. echo '<option value="GET"'; 
  163. echo '>' . __('GET', 'membership') . '</option>'; 
  164. echo '<option value="POST"'; 
  165. echo '>' . __('POST', 'membership') . '</option>'; 
  166. echo '</select>'; 
  167. echo '</td></tr>'; 
  168.  
  169. echo '</table>'; 
  170.  
  171.  
  172. function add() { 
  173.  
  174. $insert = array( 
  175. "pingname" => $_POST['pingname'],  
  176. "pingurl" => $_POST['pingurl'],  
  177. "pinginfo" => $_POST['pinginfo'],  
  178. "pingtype" => $_POST['pingtype'] 
  179. ); 
  180.  
  181. return $this->db->insert( $this->pings, $insert ); 
  182.  
  183.  
  184. function update() { 
  185.  
  186. $updates = array( 
  187. "pingname" => $_POST['pingname'],  
  188. "pingurl" => $_POST['pingurl'],  
  189. "pinginfo" => $_POST['pinginfo'],  
  190. "pingtype" => $_POST['pingtype'] 
  191. ); 
  192.  
  193. return $this->db->update( $this->pings, $updates, array( "id" => $this->id) ); 
  194.  
  195.  
  196. function delete() { 
  197.  
  198. $sql = $this->db->prepare( "DELETE FROM {$this->pings} WHERE id = %d", $this->id ); 
  199.  
  200. return $this->db->query( $sql ); 
  201.  
  202.  
  203. // History 
  204. function get_history() { 
  205. $sql = $this->db->prepare( "SELECT * FROM {$this->ping_history} WHERE ping_id = %d ORDER BY ping_sent DESC LIMIT 0, 50 ", $this->id ); 
  206.  
  207. return $this->db->get_results( $sql ); 
  208.  
  209. function get_history_item( $history_id ) { 
  210. $sql = $this->db->prepare( "SELECT * FROM {$this->ping_history} WHERE id = %d ", $history_id ); 
  211.  
  212. return $this->db->get_row( $sql ); 
  213.  
  214. function add_history( $sent, $return ) { 
  215.  
  216. $insert = array( 
  217. "ping_id" => $this->id,  
  218. "ping_sent" => gmdate( 'Y-m-d H:i:s' ),  
  219. "ping_info" => serialize($sent),  
  220. "ping_return" => serialize($return) 
  221. ); 
  222.  
  223. return $this->db->insert( $this->ping_history, $insert ); 
  224.  
  225. function update_history( $history_id, $sent, $return ) { 
  226. $update = array( 
  227. "ping_id" => $this->id,  
  228. "ping_sent" => gmdate( 'Y-m-d H:i:s' ),  
  229. "ping_info" => serialize($sent),  
  230. "ping_return" => serialize($return) 
  231. ); 
  232.  
  233. return $this->db->update( $this->ping_history, $update, array( "id" => $history_id ) ); 
  234.  
  235. // processing 
  236. function send_ping( $sub_id = false, $level_id = false, $user_id = false ) { 
  237.  
  238. $this->ping = $this->get_ping(); 
  239.  
  240. if( !class_exists( 'WP_Http' ) ) { 
  241. include_once( ABSPATH . WPINC. '/class-http.php' ); 
  242.  
  243. $pingdata = $this->pingconstants; 
  244.  
  245. if(empty($user_id)) { 
  246. $user = wp_get_current_user(); 
  247. $member = new M_Membership( $user->ID ); 
  248. } else { 
  249. $member = new M_Membership( $user_id ); 
  250.  
  251. foreach($pingdata as $key => $value) { 
  252. switch($key) { 
  253. case '%blogname%': $pingdata[$key] = get_option('blogname'); 
  254. break; 
  255.  
  256. case '%blogurl%': $pingdata[$key] = get_option('home'); 
  257. break; 
  258.  
  259. case '%username%': $pingdata[$key] = $member->user_login; 
  260. break; 
  261.  
  262. case '%usernicename%': $pingdata[$key] = $member->user_nicename; 
  263. break; 
  264.  
  265. case '%useremail%': $pingdata[$key] = $member->user_email; 
  266. break; 
  267.  
  268. case '%userid%': $pingdata[$key] = $member->ID; 
  269. break; 
  270.  
  271. case '%networkname%': $pingdata[$key] = get_site_option('site_name'); 
  272. break; 
  273.  
  274. case '%networkurl%': $pingdata[$key] = get_site_option('siteurl'); 
  275. break; 
  276.  
  277. case '%subscriptionname%': if(!$sub_id) { 
  278. $ids = $member->get_subscription_ids(); 
  279. if(!empty($ids)) { 
  280. $sub_id = $ids[0]; 
  281.  
  282. if(!empty($sub_id)) { 
  283. $sub = new M_Subscription( $sub_id ); 
  284. $pingdata[$key] = $sub->sub_name(); 
  285. } else { 
  286. $pingdata[$key] = ''; 
  287.  
  288. break; 
  289.  
  290. case '%levelname%': if(!$level_id) { 
  291. $ids = $member->get_level_ids(); 
  292. if(!empty($ids)) { 
  293. $levels = $ids[0]; 
  294.  
  295. if(!empty($levels->level_id)) { 
  296. $level = new M_Level( $levels->level_id ); 
  297. $pingdata[$key] = $level->level_title(); 
  298. } else { 
  299. $pingdata[$key] = ''; 
  300. break; 
  301.  
  302. case '%timestamp%': $pingdata[$key] = time(); 
  303. break; 
  304.  
  305. default: $pingdata[$key] = apply_filter( 'membership_pingfield_' . $key, '' ); 
  306. break; 
  307.  
  308. $url = $this->ping->pingurl; 
  309.  
  310. // Globally replace the values in the ping and then make it into an array to send 
  311. $pingmessage = str_replace(array_keys($pingdata), array_values($pingdata), $this->ping->pinginfo); 
  312. $pingmessage = array_map( 'trim', explode("\n", $pingmessage) ); 
  313.  
  314. // make the ping message into a sendable bit of text 
  315. $pingtosend = array(); 
  316. foreach($pingmessage as $key => $value) { 
  317. $temp = explode("=", $value); 
  318. $pingtosend[$temp[0]] = $temp[1]; 
  319.  
  320. // Send the request 
  321. if( class_exists( 'WP_Http' ) ) { 
  322. $request = new WP_Http; 
  323.  
  324. switch( $this->ping->pingtype ) { 
  325. case 'GET': $url = untrailingslashit($url) . "?"; 
  326. foreach($pingtosend as $key => $val) { 
  327. if(substr($url, -1) != '?') $url .= "&"; 
  328. $url .= $key . "=" . urlencode($val); 
  329. $result = $request->request( $url, array( 'method' => 'GET', 'body' => '' ) ); 
  330. break; 
  331.  
  332. case 'POST': $result = $request->request( $url, array( 'method' => 'POST', 'body' => $pingtosend ) ); 
  333. break; 
  334.  
  335. /** 
  336. 'headers': an array of response headers, such as "x-powered-by" => "PHP/5.2.1" 
  337. 'body': the response string sent by the server, as you would see it with you web browser 
  338. 'response': an array of HTTP response codes. Typically, you'll want to have array('code'=>200, 'message'=>'OK') 
  339. 'cookies': an array of cookie information 
  340. */ 
  341.  
  342. $this->add_history( $pingtosend, $result ); 
  343.  
  344.  
  345.  
  346. function resend_historic_ping( $history_id, $rewrite ) { 
  347. $history = $this->get_history_item( $history_id ); 
  348.  
  349. if(!empty($history)) { 
  350. $this->id = $history->ping_id; 
  351. $ping = $this->get_specifc_ping( $history->ping_id ); 
  352.  
  353. if( !class_exists( 'WP_Http' ) ) { 
  354. include_once( ABSPATH . WPINC. '/class-http.php' ); 
  355.  
  356. $url = $ping->pingurl; 
  357. $pingtosend = unserialize($history->ping_info); 
  358.  
  359. // Send the request 
  360. if( class_exists( 'WP_Http' ) ) { 
  361. $request = new WP_Http; 
  362.  
  363. switch( $ping->pingtype ) { 
  364. case 'GET': $url = untrailingslashit($url) . "?"; 
  365. foreach($pingtosend as $key => $val) { 
  366. if(substr($url, -1) != '?') $url .= "&"; 
  367. $url .= $key . "=" . urlencode($val); 
  368. $result = $request->request( $url, array( 'method' => 'GET', 'body' => '' ) ); 
  369. break; 
  370.  
  371. case 'POST': $result = $request->request( $url, array( 'method' => 'POST', 'body' => $pingtosend ) ); 
  372. break; 
  373.  
  374. /** 
  375. 'headers': an array of response headers, such as "x-powered-by" => "PHP/5.2.1" 
  376. 'body': the response string sent by the server, as you would see it with you web browser 
  377. 'response': an array of HTTP response codes. Typically, you'll want to have array('code'=>200, 'message'=>'OK') 
  378. 'cookies': an array of cookie information 
  379. */ 
  380.  
  381. if($rewrite) { 
  382. $this->add_history( $pingtosend, $result ); 
  383. } else { 
  384. $this->update_history( $history_id, $pingtosend, $result ); 
  385.