| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if( !defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
|
| 6 |
class wpForoSubscribe{ |
| 7 |
|
| 8 |
private $wpforo; |
| 9 |
private 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(), $cache = true ){ |
| 96 |
if( is_string($args) ) $args = array("confirmkey" => sanitize_text_field($args)); |
| 97 |
if( empty($args) && !empty($_REQUEST['sbscrb']) ) $args = $_REQUEST['sbscrb']; |
| 98 |
if( empty($args) ) return FALSE; |
| 99 |
extract( $args, EXTR_OVERWRITE ); |
| 100 |
if( (!isset($itemid) || !$itemid || !isset($userid) || !$userid || !isset($type) || !$type) && (!isset($confirmkey) || !$confirmkey) ) return FALSE; |
| 101 |
if( isset($confirmkey) && $confirmkey){ |
| 102 |
$where = " `confirmkey` = '".esc_sql(sanitize_text_field($confirmkey))."'"; |
| 103 |
}elseif( isset($itemid) && $itemid && isset($userid) && $userid && isset($type) && $type ){ |
| 104 |
$where = " `itemid` = ".intval($itemid)." AND `userid` = ".intval($userid)." AND `type` = '".esc_sql(sanitize_text_field($type))."'"; |
| 105 |
}else{ |
| 106 |
return FALSE; |
| 107 |
} |
| 108 |
if( $cache && isset(self::$cache['subscribe'][$itemid][$userid][$type]) ){ |
| 109 |
return self::$cache['subscribe'][$itemid][$userid][$type]; |
| 110 |
} |
| 111 |
$sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_subscribes` WHERE " . $where; |
| 112 |
$subscribe = $this->wpforo->db->get_row($sql, ARRAY_A); |
| 113 |
if($cache && !empty($subscribe)){ |
| 114 |
self::$cache['subscribe'][$itemid][$userid][$type] = $subscribe; |
| 115 |
} |
| 116 |
return $subscribe; |
| 117 |
} |
| 118 |
|
| 119 |
function get_subscribes( $args = array(), &$items_count = 0 ){ |
| 120 |
|
| 121 |
$default = array( |
| 122 |
'itemid' => NULL, |
| 123 |
'type' => '', // topic | forum |
| 124 |
'userid' => NULL, // |
| 125 |
'active' => 1, |
| 126 |
'orderby' => 'subid', // order by `field` |
| 127 |
'order' => 'DESC', // ASC DESC |
| 128 |
'offset' => NULL, // OFFSET |
| 129 |
'row_count' => NULL, // ROW COUNT |
| 130 |
); |
| 131 |
|
| 132 |
$args = wpforo_parse_args( $args, $default ); |
| 133 |
if(!empty($args)){ |
| 134 |
extract($args, EXTR_OVERWRITE); |
| 135 |
|
| 136 |
$sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_subscribes`"; |
| 137 |
$wheres = array(); |
| 138 |
|
| 139 |
if( $type ) $wheres[] = " `type` = '" . esc_sql(sanitize_text_field($type)) . "'"; |
| 140 |
$wheres[] = " `active` = " . intval($active); |
| 141 |
if($itemid != NULL) $wheres[] = " `itemid` = " . intval($itemid); |
| 142 |
if($userid != NULL) $wheres[] = " `userid` = " . intval($userid); |
| 143 |
|
| 144 |
if(!empty($wheres)) $sql .= " WHERE " . implode( " AND ", $wheres ); |
| 145 |
|
| 146 |
$item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql); |
| 147 |
if( $item_count_sql ) $items_count = $this->wpforo->db->get_var($item_count_sql); |
| 148 |
|
| 149 |
$sql .= " ORDER BY `$orderby` " . $order; |
| 150 |
|
| 151 |
if($row_count != NULL){ |
| 152 |
if($offset != NULL){ |
| 153 |
$sql .= esc_sql(" LIMIT $offset,$row_count"); |
| 154 |
}else{ |
| 155 |
$sql .= esc_sql(" LIMIT $row_count"); |
| 156 |
} |
| 157 |
} |
| 158 |
return $this->wpforo->db->get_results($sql, ARRAY_A); |
| 159 |
|
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
function get_confirm_link($args){ |
| 164 |
if(is_string($args)) return WPFORO_BASE_URL . "?wpforo=sbscrbconfirm&key=" . sanitize_text_field($args); |
| 165 |
|
| 166 |
if($args['type'] == 'forum'){ |
| 167 |
$url = $this->wpforo->forum->get_forum_url($args['itemid']) . '/'; |
| 168 |
}elseif($args['type'] == 'topic'){ |
| 169 |
$url = $this->wpforo->topic->get_topic_url($args['itemid']) . '/'; |
| 170 |
}else{ |
| 171 |
$url = WPFORO_BASE_URL; |
| 172 |
} |
| 173 |
return $url . "?wpforo=sbscrbconfirm&key=" . sanitize_text_field($args['confirmkey']); |
| 174 |
} |
| 175 |
|
| 176 |
function get_unsubscribe_link($confirmkey){ |
| 177 |
return WPFORO_BASE_URL . "?wpforo=unsbscrb&key=" . sanitize_text_field($confirmkey); |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
?> |