PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/membership/app_old/membershipincludes/classes/class.urlgroup.php

https://gitlab.com/najomie/fit-hippie
PHP | 349 lines | 253 code | 81 blank | 15 comment | 32 complexity | 86e147a47db9f8c2dd3582059d871d09 MD5 | raw file
  1. <?php
  2. if(!class_exists('M_Urlgroup')) {
  3. class M_Urlgroup {
  4. var $build = 1;
  5. var $db;
  6. var $tables = array('membership_levels', 'membership_rules', 'subscriptions', 'subscriptions_levels', 'membership_relationships', 'membermeta', 'communications', 'urlgroups');
  7. var $membership_levels;
  8. var $membership_rules;
  9. var $membership_relationships;
  10. var $subscriptions;
  11. var $subscriptions_levels;
  12. var $membermeta;
  13. var $communications;
  14. var $urlgroups;
  15. // if the data needs reloaded, or hasn't been loaded yet
  16. var $dirty = true;
  17. var $group;
  18. function __construct( $id = false) {
  19. global $wpdb;
  20. $this->db =& $wpdb;
  21. foreach($this->tables as $table) {
  22. $this->$table = membership_db_prefix($this->db, $table);
  23. }
  24. $this->id = $id;
  25. }
  26. function M_Urlgroup( $id = false ) {
  27. $this->__construct( $id );
  28. }
  29. function get_group() {
  30. $sql = $this->db->prepare( "SELECT * FROM {$this->urlgroups} WHERE id = %d ", $this->id );
  31. return $this->db->get_row( $sql );
  32. }
  33. function editform() {
  34. $this->group = $this->get_group();
  35. echo '<table class="form-table">';
  36. echo '<tr class="form-field form-required">';
  37. echo '<th style="" scope="row" valign="top">' . __('Group name','membership') . '</th>';
  38. echo '<td valign="top"><input name="groupname" type="text" size="50" title="' . __('Group name','membership') . '" style="width: 50%;" value="' . esc_attr(stripslashes($this->group->groupname)) . '" /></td>';
  39. echo '</tr>';
  40. echo '<tr class="form-field form-required">';
  41. echo '<th style="" scope="row" valign="top">' . __('Page URLs','membership') . '</th>';
  42. echo '<td valign="top"><textarea name="groupurls" rows="15" cols="40">' . esc_html(stripslashes($this->group->groupurls)) . '</textarea>';
  43. // Display some instructions for the message.
  44. echo "<br/><em style='font-size:smaller;'>" . __("You should place each page URL or expression on a new line.",'membership') . "</em>";
  45. echo '</td>';
  46. echo '</tr>';
  47. echo '<tr class="form-field form-required">';
  48. echo '<th style="" scope="row" valign="top">' . __('Strip query strings from URL','membership') . '</th>';
  49. echo '<td valign="top" align="left">';
  50. echo '<select name="stripquerystring">';
  51. echo '<option value="0"';
  52. if($this->group->stripquerystring == 0) echo ' selected="selected"';
  53. echo '>' . __('No', 'membership') . '</option>';
  54. echo '<option value="1"';
  55. if($this->group->stripquerystring == 1) echo ' selected="selected"';
  56. echo '>' . __('Yes', 'membership') . '</option>';
  57. echo '</select>';
  58. echo "<br/><em style='font-size:smaller;'>" . __("Remove any query string values prior to checking URL.",'membership') . "</em>";
  59. echo '</td></tr>';
  60. echo '<tr class="form-field form-required">';
  61. echo '<th style="" scope="row" valign="top">' . __('Regular Expression','membership') . '</th>';
  62. echo '<td valign="top" align="left">';
  63. echo '<select name="isregexp">';
  64. echo '<option value="0"';
  65. if($this->group->isregexp == 0) echo ' selected="selected"';
  66. echo '>' . __('No', 'membership') . '</option>';
  67. echo '<option value="1"';
  68. if($this->group->isregexp == 1) echo ' selected="selected"';
  69. echo '>' . __('Yes', 'membership') . '</option>';
  70. echo '</select>';
  71. echo "<br/><em style='font-size:smaller;'>" . __("If any of the page URLs are regular expressions then set this to yes.",'membership') . "</em>";
  72. echo '</td></tr>';
  73. echo '</table>';
  74. }
  75. function addform() {
  76. echo '<table class="form-table">';
  77. echo '<tr class="form-field form-required">';
  78. echo '<th style="" scope="row" valign="top">' . __('Group name','membership') . '</th>';
  79. echo '<td valign="top"><input name="groupname" type="text" size="50" title="' . __('Group name','membership') . '" style="width: 50%;" value="" /></td>';
  80. echo '</tr>';
  81. echo '<tr class="form-field form-required">';
  82. echo '<th style="" scope="row" valign="top">' . __('Page URLs','membership') . '</th>';
  83. echo '<td valign="top"><textarea name="groupurls" rows="15" cols="40"></textarea>';
  84. // Display some instructions for the message.
  85. echo "<br/><em style='font-size:smaller;'>" . __("You should place each page URL or expression on a new line.",'membership') . "</em>";
  86. echo '</td>';
  87. echo '</tr>';
  88. echo '<tr class="form-field form-required">';
  89. echo '<th style="" scope="row" valign="top">' . __('Strip query strings from URL','membership') . '</th>';
  90. echo '<td valign="top" align="left">';
  91. echo '<select name="stripquerystring">';
  92. echo '<option value="0"';
  93. echo '>' . __('No', 'membership') . '</option>';
  94. echo '<option value="1"';
  95. echo '>' . __('Yes', 'membership') . '</option>';
  96. echo '</select>';
  97. echo "<br/><em style='font-size:smaller;'>" . __("Remove any query string values prior to checking URL.",'membership') . "</em>";
  98. echo '</td></tr>';
  99. echo '<tr class="form-field form-required">';
  100. echo '<th style="" scope="row" valign="top">' . __('Regular Expression','membership') . '</th>';
  101. echo '<td valign="top" align="left">';
  102. echo '<select name="isregexp">';
  103. echo '<option value="0"';
  104. echo '>' . __('No', 'membership') . '</option>';
  105. echo '<option value="1"';
  106. echo '>' . __('Yes', 'membership') . '</option>';
  107. echo '</select>';
  108. echo "<br/><em style='font-size:smaller;'>" . __("If any of the page URLs are regular expressions then set this to yes.",'membership') . "</em>";
  109. echo '</td></tr>';
  110. echo '</table>';
  111. }
  112. function add() {
  113. $insert = array(
  114. "groupname" => $_POST['groupname'],
  115. "groupurls" => $_POST['groupurls'],
  116. "isregexp" => $_POST['isregexp'],
  117. "stripquerystring" => $_POST['stripquerystring']
  118. );
  119. return $this->db->insert( $this->urlgroups, $insert );
  120. }
  121. function update() {
  122. $updates = array(
  123. "groupname" => $_POST['groupname'],
  124. "groupurls" => $_POST['groupurls'],
  125. "isregexp" => $_POST['isregexp'],
  126. "stripquerystring" => $_POST['stripquerystring']
  127. );
  128. return $this->db->update( $this->urlgroups, $updates, array( "id" => $this->id) );
  129. }
  130. function delete() {
  131. $sql = $this->db->prepare( "DELETE FROM {$this->urlgroups} WHERE id = %d", $this->id );
  132. return $this->db->query( $sql );
  133. }
  134. // processing
  135. function url_matches( $host, $exclude = array() ) {
  136. $this->group = $this->get_group();
  137. $groups = array_map('strtolower', array_map('trim', explode("\n", $this->group->groupurls)));
  138. if($this->group->stripquerystring == 1 && strpos($host, '?') !== false) {
  139. $host = substr( $host, 0, strpos($host, '?'));
  140. }
  141. if($this->group->isregexp == 0) {
  142. // straight match
  143. $newgroups = array_map('untrailingslashit', $groups);
  144. $groups = array_merge($groups,$newgroups);
  145. if(in_array( strtolower($host), $groups ) ) {
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. } else {
  151. //reg expression match
  152. $matchstring = "";
  153. foreach($groups as $key => $value) {
  154. if($matchstring != "") $matchstring .= "|";
  155. if( stripos($value, '\/') ) {
  156. $matchstring .= stripcslashes($value);
  157. } else {
  158. $matchstring .= $value;
  159. }
  160. }
  161. // switched to using a character that won't be in a url as the start and end markers
  162. $matchstring = "#" . $matchstring . "#i";
  163. if(preg_match($matchstring, $host, $matches) ) {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. function M_create_internal_URL_group( $rule, $post, $level_id ) {
  173. global $wpdb;
  174. switch( $rule ) {
  175. case 'posts': $permalinks = array();
  176. foreach( $_POST[$rule] as $rule ) {
  177. $thelink = get_permalink( $rule );
  178. $thelink = str_replace('http://', 'https?://', $thelink );
  179. $permalinks[] = untrailingslashit($thelink) . '(/.*)';
  180. }
  181. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0,1", '_posts-' . $level_id );
  182. $id = $wpdb->get_var( $sql );
  183. $data = array( "groupname" => '_posts-' . $level_id,
  184. "groupurls" => implode("\n", $permalinks),
  185. "isregexp" => 1,
  186. "stripquerystring" => 1
  187. );
  188. if(!empty($id)) {
  189. // exists so we're going to do an update
  190. $wpdb->update( membership_db_prefix($wpdb, 'urlgroups'), $data, array( "id" => $id) );
  191. } else {
  192. // doesn't exist so we're going to do an add.
  193. $wpdb->insert( membership_db_prefix($wpdb, 'urlgroups'), $data );
  194. }
  195. break;
  196. case 'pages': $permalinks = array();
  197. foreach( $_POST[$rule] as $rule ) {
  198. $thelink = get_permalink( $rule );
  199. $thelink = str_replace('http://', 'https?://', $thelink );
  200. $permalinks[] = untrailingslashit($thelink) . '(/.*)';
  201. }
  202. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0,1", '_pages-' . $level_id );
  203. $id = $wpdb->get_var( $sql );
  204. $data = array( "groupname" => '_pages-' . $level_id,
  205. "groupurls" => implode("\n", $permalinks),
  206. "isregexp" => 1,
  207. "stripquerystring" => 1
  208. );
  209. if(!empty($id)) {
  210. // exists so we're going to do an update
  211. $wpdb->update( membership_db_prefix($wpdb, 'urlgroups'), $data, array( "id" => $id) );
  212. } else {
  213. // doesn't exist so we're going to do an add.
  214. $wpdb->insert( membership_db_prefix($wpdb, 'urlgroups'), $data );
  215. }
  216. break;
  217. case 'bppages': $permalinks = array();
  218. foreach( $_POST[$rule] as $rule ) {
  219. $thelink = get_permalink( $rule );
  220. $thelink = str_replace('http://', 'https?://', $thelink );
  221. $permalinks[] = untrailingslashit($thelink) . '(/.*)';
  222. }
  223. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0,1", '_bppages-' . $level_id);
  224. $id = $wpdb->get_var( $sql );
  225. $data = array( "groupname" => '_bppages-' . $level_id,
  226. "groupurls" => implode("\n", $permalinks),
  227. "isregexp" => 1,
  228. "stripquerystring" => 1
  229. );
  230. if(!empty($id)) {
  231. // exists so we're going to do an update
  232. $wpdb->update( membership_db_prefix($wpdb, 'urlgroups'), $data, array( "id" => $id) );
  233. } else {
  234. // doesn't exist so we're going to do an add.
  235. $wpdb->insert( membership_db_prefix($wpdb, 'urlgroups'), $data );
  236. }
  237. break;
  238. case 'bpgroups': $permalinks = array();
  239. if(function_exists('bp_get_group_permalink')) {
  240. foreach( $_POST[$rule] as $rule ) {
  241. $group = new BP_Groups_Group( $rule );
  242. $thelink = bp_get_group_permalink( $group );
  243. $thelink = str_replace('http://', 'https?://', $thelink );
  244. $permalinks[] = untrailingslashit($thelink) . '(/.*)';
  245. }
  246. }
  247. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0,1", '_bpgroups-' . $level_id);
  248. $id = $wpdb->get_var( $sql );
  249. $data = array( "groupname" => '_bpgroups-' . $level_id,
  250. "groupurls" => implode("\n", $permalinks),
  251. "isregexp" => 1,
  252. "stripquerystring" => 1
  253. );
  254. if(!empty($id)) {
  255. // exists so we're going to do an update
  256. $wpdb->update( membership_db_prefix($wpdb, 'urlgroups'), $data, array( "id" => $id) );
  257. } else {
  258. // doesn't exist so we're going to do an add.
  259. $wpdb->insert( membership_db_prefix($wpdb, 'urlgroups'), $data );
  260. }
  261. break;
  262. }
  263. }
  264. add_action( 'membership_update_positive_rule', 'M_create_internal_URL_group', 10, 3 );
  265. add_action( 'membership_update_negative_rule', 'M_create_internal_URL_group', 10, 3 );
  266. add_action( 'membership_add_positive_rule', 'M_create_internal_URL_group', 10, 3 );
  267. add_action( 'membership_add_negative_rule', 'M_create_internal_URL_group', 10, 3 );
  268. ?>