PluginProbe
wpForo Forum / 1.3.1
wpForo Forum v1.3.1
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpf-includes / class-subscribes.php

class-subscribes.php in wpForo Forum 1.3.1, at wpf-includes/class-subscribes.php

183 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if( !defined( 'ABSPATH' ) ) exit;
4
5
6 class wpForoSubscribe{
7
8 private $wpforo;
9 static $cache = array( 'subscribe' => array() );
10
11 function __construct( $wpForo ){
12 if(!isset($this->wpforo)) $this->wpforo = $wpForo;
13 }
14
15 function get_confirm_key(){
16 return substr(md5(rand().time()), 0, 32);
17 }
18
19 function add( $args = array() ){
20 if( empty($args) && empty($_REQUEST['sbscrb']) ) return FALSE;
21 if( empty($args) && !empty($_REQUEST['sbscrb']) ) $args = $_REQUEST['sbscrb'];
22 if( !isset($args['active']) || !$args['active'] ) $args['active'] = 0;
23
24 extract( $args, EXTR_OVERWRITE );
25 if( !isset($itemid) || !$itemid || !isset($userid) || !$userid || !isset($type) || !$type ) return FALSE;
26
27 if( !isset($confirmkey) || (isset($confirmkey) && !$confirmkey ) ) $confirmkey = $this->get_confirm_key();
28
29 if($this->wpforo->db->insert(
30 $this->wpforo->db->prefix . 'wpforo_subscribes',
31 array(
32 'itemid' => intval($itemid),
33 'type' => sanitize_text_field($type),
34 'confirmkey' => sanitize_text_field($confirmkey),
35 'userid' => intval($userid),
36 'active' => $active
37 ),
38 array(
39 '%d',
40 '%s',
41 '%s',
42 '%d',
43 '%d'
44 )
45 )){
46 if( isset($active) && $active == 1 ){
47 $this->wpforo->notice->add('You have been successfully subscribed', 'success');
48 }else{
49 $this->wpforo->notice->add('Success! Thank you. Please check your email and click confirmation link below to complete this step.', 'success');
50 }
51 return $confirmkey;
52 }
53
54 $this->wpforo->notice->add('Can\'t subscribe to this item', 'error');
55 return FALSE;
56 }
57
58 function edit( $confirmkey = '' ){
59 if( !$confirmkey && isset($_REQUEST['key']) && $_REQUEST['key'] ) $confirmkey = $_REQUEST['key'];
60 if( !$confirmkey ){
61 $this->wpforo->notice->add('Invalid request!', 'error');
62 return FALSE;
63 }
64
65 if( $this->wpforo->db->update(
66 $this->wpforo->db->prefix . 'wpforo_subscribes',
67 array( 'active' => 1 ),
68 array( 'confirmkey' => sanitize_text_field($confirmkey) ),
69 array( '%d' ),
70 array( '%s' )
71 ) ){
72 $this->wpforo->notice->add('You have been successfully subscribed', 'success');
73 return TRUE;
74 }
75
76 $this->wpforo->notice->add('Your subscription for this item could not be confirmed', 'error');
77 return FALSE;
78 }
79
80 function delete( $confirmkey = '' ){
81 if( !$confirmkey && isset($_REQUEST['confirmkey']) && $_REQUEST['confirmkey'] ) $confirmkey = $_REQUEST['confirmkey'];
82 if( !$confirmkey ){
83 $this->wpforo->notice->add('Invalid request!', 'error');
84 return FALSE;
85 }
86 if( $this->wpforo->db->delete( $this->wpforo->db->prefix.'wpforo_subscribes', array( 'confirmkey' => sanitize_text_field($confirmkey) ), array( '%s' ) ) ){
87 $this->wpforo->notice->add('You have been successfully unsubscribed', 'success');
88 return TRUE;
89 }
90
91 $this->wpforo->notice->add('Could not be unsubscribe from this item', 'error');
92 return FALSE;
93 }
94
95 function get_subscribe( $args = array() ){
96
97 $cache = $this->wpforo->cache->on('memory_cashe');
98
99 if( is_string($args) ) $args = array("confirmkey" => sanitize_text_field($args));
100 if( empty($args) && !empty($_REQUEST['sbscrb']) ) $args = $_REQUEST['sbscrb'];
101 if( empty($args) ) return FALSE;
102 extract( $args, EXTR_OVERWRITE );
103 if( (!isset($itemid) || !$itemid || !isset($userid) || !$userid || !isset($type) || !$type) && (!isset($confirmkey) || !$confirmkey) ) return FALSE;
104 if( isset($confirmkey) && $confirmkey){
105 $where = " `confirmkey` = '".esc_sql(sanitize_text_field($confirmkey))."'";
106 }elseif( isset($itemid) && $itemid && isset($userid) && $userid && isset($type) && $type ){
107 $where = " `itemid` = ".intval($itemid)." AND `userid` = ".intval($userid)." AND `type` = '".esc_sql(sanitize_text_field($type))."'";
108 }else{
109 return FALSE;
110 }
111 if( $cache && isset(self::$cache['subscribe'][$itemid][$userid][$type]) ){
112 return self::$cache['subscribe'][$itemid][$userid][$type];
113 }
114 $sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_subscribes` WHERE " . $where;
115 $subscribe = $this->wpforo->db->get_row($sql, ARRAY_A);
116 if($cache && !empty($subscribe)){
117 self::$cache['subscribe'][$itemid][$userid][$type] = $subscribe;
118 }
119 return $subscribe;
120 }
121
122 function get_subscribes( $args = array(), &$items_count = 0 ){
123
124 $default = array(
125 'itemid' => NULL,
126 'type' => '', // topic | forum
127 'userid' => NULL, //
128 'active' => 1,
129 'orderby' => 'subid', // order by `field`
130 'order' => 'DESC', // ASC DESC
131 'offset' => NULL, // OFFSET
132 'row_count' => NULL, // ROW COUNT
133 );
134
135 $args = wpforo_parse_args( $args, $default );
136 if(!empty($args)){
137 extract($args, EXTR_OVERWRITE);
138
139 $sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_subscribes`";
140 $wheres = array();
141
142 if( $type ) $wheres[] = " `type` = '" . esc_sql(sanitize_text_field($type)) . "'";
143 $wheres[] = " `active` = " . intval($active);
144 if($itemid != NULL) $wheres[] = " `itemid` = " . intval($itemid);
145 if($userid != NULL) $wheres[] = " `userid` = " . intval($userid);
146
147 if(!empty($wheres)) $sql .= " WHERE " . implode( " AND ", $wheres );
148
149 $item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql);
150 if( $item_count_sql ) $items_count = $this->wpforo->db->get_var($item_count_sql);
151
152 $sql .= " ORDER BY `$orderby` " . $order;
153
154 if($row_count != NULL){
155 if($offset != NULL){
156 $sql .= esc_sql(" LIMIT $offset,$row_count");
157 }else{
158 $sql .= esc_sql(" LIMIT $row_count");
159 }
160 }
161 return $this->wpforo->db->get_results($sql, ARRAY_A);
162
163 }
164 }
165
166 function get_confirm_link($args){
167 if(is_string($args)) return wpforo_home_url( "?wpforo=sbscrbconfirm&key=" . sanitize_text_field($args) );
168
169 if($args['type'] == 'forum'){
170 $url = $this->wpforo->forum->get_forum_url($args['itemid']) . '/';
171 }elseif($args['type'] == 'topic'){
172 $url = $this->wpforo->topic->get_topic_url($args['itemid']) . '/';
173 }else{
174 $url = wpforo_home_url();
175 }
176 return wpforo_home_url( $url . "?wpforo=sbscrbconfirm&key=" . sanitize_text_field($args['confirmkey']) );
177 }
178
179 function get_unsubscribe_link($confirmkey){
180 return wpforo_home_url( "?wpforo=unsbscrb&key=" . sanitize_text_field($confirmkey) );
181 }
182
183 }