| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpforo\modules\bookmarks; |
| 4 |
|
| 5 |
use stdClass; |
| 6 |
use wpforo\modules\bookmarks\classes\Template; |
| 7 |
use wpforo\modules\bookmarks\classes\Actions; |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( ! defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class Bookmarks { |
| 13 |
public $default; |
| 14 |
/* @var Template */ public $Template; |
| 15 |
/* @var Actions */ public $Actions; |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
$this->init_defaults(); |
| 19 |
$this->init_classes(); |
| 20 |
} |
| 21 |
|
| 22 |
private function init_classes() { |
| 23 |
$this->Template = new Template(); |
| 24 |
$this->Actions = new Actions(); |
| 25 |
} |
| 26 |
|
| 27 |
private function init_defaults() { |
| 28 |
$this->default = new stdClass(); |
| 29 |
$this->default->bookmark = [ |
| 30 |
'bookmarkid' => 0, |
| 31 |
'userid' => 0, |
| 32 |
'boardid' => 0, |
| 33 |
'postid' => 0, |
| 34 |
'created' => time(), |
| 35 |
'status' => 1, |
| 36 |
]; |
| 37 |
$this->default->bookmark_format = [ |
| 38 |
'bookmarkid' => '%d', |
| 39 |
'userid' => '%d', |
| 40 |
'boardid' => '%d', |
| 41 |
'postid' => '%d', |
| 42 |
'created' => '%d', |
| 43 |
'status' => '%d', |
| 44 |
]; |
| 45 |
$this->default->sql_select_args = [ |
| 46 |
'bookmarkid' => null, |
| 47 |
'userid' => null, |
| 48 |
'userid_include' => [], |
| 49 |
'userid_exclude' => [], |
| 50 |
'boardid' => null, |
| 51 |
'boardid_include' => [], |
| 52 |
'boardid_exclude' => [], |
| 53 |
'postid' => null, |
| 54 |
'postid_include' => [], |
| 55 |
'postid_exclude' => [], |
| 56 |
'status' => null, |
| 57 |
'orderby' => null, |
| 58 |
'offset' => null, |
| 59 |
'row_count' => null, |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @param $bookmark |
| 65 |
* |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
public function decode( $bookmark ) { |
| 69 |
$bookmark = array_merge( $this->default->bookmark, (array) $bookmark ); |
| 70 |
$bookmark['bookmarkid'] = wpforo_bigintval( $bookmark['bookmarkid'] ); |
| 71 |
$bookmark['userid'] = wpforo_bigintval( $bookmark['userid'] ); |
| 72 |
$bookmark['boardid'] = intval( $bookmark['boardid'] ); |
| 73 |
$bookmark['postid'] = wpforo_bigintval( $bookmark['postid'] ); |
| 74 |
$bookmark['created'] = intval( $bookmark['created'] ); |
| 75 |
$bookmark['status'] = (bool) intval( $bookmark['status'] ); |
| 76 |
|
| 77 |
return $bookmark; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param $bookmark |
| 82 |
* |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
private function encode( $bookmark ) { |
| 86 |
$bookmark = $this->decode( $bookmark ); |
| 87 |
$bookmark['status'] = intval( $bookmark['status'] ); |
| 88 |
return $bookmark; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param $bookmark |
| 93 |
* |
| 94 |
* @return false|int |
| 95 |
*/ |
| 96 |
public function add( $bookmark ) { |
| 97 |
$bookmark = $this->encode( $bookmark ); |
| 98 |
unset( $bookmark['bookmarkid'] ); |
| 99 |
$bookmark = wpforo_array_ordered_intersect_key( $bookmark, $this->default->bookmark_format ); |
| 100 |
if( WPF()->db->insert( |
| 101 |
WPF()->tables->bookmarks, |
| 102 |
$bookmark, |
| 103 |
wpforo_array_ordered_intersect_key( $this->default->bookmark_format, $bookmark ) |
| 104 |
) ) { |
| 105 |
$bookmark['bookmarkid'] = WPF()->db->insert_id; |
| 106 |
do_action( 'wpforo_after_add_bookmark', $bookmark ); |
| 107 |
|
| 108 |
return $bookmark['bookmarkid']; |
| 109 |
} |
| 110 |
|
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @param array $fields |
| 116 |
* @param array|int $where |
| 117 |
* |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
public function edit( $fields, $where ) { |
| 121 |
if( is_numeric( $where ) ) $where = [ 'bookmarkid' => wpforo_bigintval( $where ) ]; |
| 122 |
$fields = wpforo_array_ordered_intersect_key( $fields, $this->default->bookmark_format ); |
| 123 |
if( false !== WPF()->db->update( |
| 124 |
WPF()->tables->bookmarks, |
| 125 |
$fields = wpforo_array_ordered_intersect_key( $this->encode( $fields ), $fields ), |
| 126 |
$where = wpforo_array_ordered_intersect_key( $where, $this->default->bookmark_format ), |
| 127 |
wpforo_array_ordered_intersect_key( $this->default->bookmark_format, $fields ), |
| 128 |
wpforo_array_ordered_intersect_key( $this->default->bookmark_format, $where ) |
| 129 |
) ) { |
| 130 |
do_action( 'wpforo_after_edit_bookmark', $fields, $where ); |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @param array|int $args |
| 140 |
* @param string $operator |
| 141 |
* |
| 142 |
* @return bool |
| 143 |
*/ |
| 144 |
public function delete( $args, $operator = 'AND' ) { |
| 145 |
if( is_numeric( $args ) ) $args = [ 'bookmarkid' => wpforo_bigintval( $args ) ]; |
| 146 |
$operator = trim( strtoupper( (string) $operator ) ); |
| 147 |
if( ! in_array( $operator, [ 'AND', 'OR' ], true ) ) $operator = 'AND'; |
| 148 |
|
| 149 |
do_action( 'wpforo_before_delete_bookmark', $args, $operator ); |
| 150 |
|
| 151 |
$sql = "DELETE FROM " . WPF()->tables->bookmarks; |
| 152 |
if( $wheres = $this->build_sql_wheres( $args ) ) $sql .= " WHERE " . implode( " $operator ", $wheres ); |
| 153 |
|
| 154 |
$args = $this->parse_args( $args ); |
| 155 |
if( $args['orderby'] ) $sql .= " ORDER BY " . $args['orderby']; |
| 156 |
if( $args['row_count'] ) $sql .= " LIMIT " . intval( $args['row_count'] ); |
| 157 |
|
| 158 |
$r = WPF()->db->query( $sql ); |
| 159 |
|
| 160 |
do_action( 'wpforo_after_delete_bookmark', $args, $operator ); |
| 161 |
|
| 162 |
return false !== $r; |
| 163 |
} |
| 164 |
|
| 165 |
private function parse_args( $args ) { |
| 166 |
$args = wpforo_parse_args( $args, $this->default->sql_select_args ); |
| 167 |
$args = wpforo_array_ordered_intersect_key( $args, $this->default->sql_select_args ); |
| 168 |
$args['userid_include'] = wpforo_parse_args( $args['userid_include'] ); |
| 169 |
$args['userid_exclude'] = wpforo_parse_args( $args['userid_exclude'] ); |
| 170 |
$args['boardid_include'] = wpforo_parse_args( $args['boardid_include'] ); |
| 171 |
$args['boardid_exclude'] = wpforo_parse_args( $args['boardid_exclude'] ); |
| 172 |
$args['postid_include'] = wpforo_parse_args( $args['postid_include'] ); |
| 173 |
$args['postid_exclude'] = wpforo_parse_args( $args['postid_exclude'] ); |
| 174 |
$args['orderby'] = sanitize_sql_orderby( (string) $args['orderby'] ); |
| 175 |
|
| 176 |
return $args; |
| 177 |
} |
| 178 |
|
| 179 |
private function build_sql_wheres( $args ) { |
| 180 |
$args = $this->parse_args( $args ); |
| 181 |
$wheres = []; |
| 182 |
|
| 183 |
if( ! is_null( $args['bookmarkid'] ) ) $wheres[] = "`bookmarkid` = '" . wpforo_bigintval( $args['bookmarkid'] ) . "'"; |
| 184 |
if( ! is_null( $args['userid'] ) ) $wheres[] = "`userid` = '" . wpforo_bigintval( $args['userid'] ) . "'"; |
| 185 |
if( ! is_null( $args['boardid'] ) ) $wheres[] = "`boardid` = '" . intval( $args['boardid'] ) . "'"; |
| 186 |
if( ! is_null( $args['postid'] ) ) $wheres[] = "`postid` = '" . wpforo_bigintval( $args['postid'] ) . "'"; |
| 187 |
if( ! is_null( $args['status'] ) ) $wheres[] = "`status` = '" . intval( $args['status'] ) . "'"; |
| 188 |
|
| 189 |
if( ! empty( $args['userid_include'] ) ) $wheres[] = "`userid` IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['userid_include'] ) ) . ")"; |
| 190 |
if( ! empty( $args['userid_exclude'] ) ) $wheres[] = "`userid` NOT IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['userid_exclude'] ) ) . ")"; |
| 191 |
|
| 192 |
if( ! empty( $args['boardid_include'] ) ) $wheres[] = "`boardid` IN(" . implode( ',', array_map( 'intval', $args['boardid_include'] ) ) . ")"; |
| 193 |
if( ! empty( $args['boardid_exclude'] ) ) $wheres[] = "`boardid` NOT IN(" . implode( ',', array_map( 'intval', $args['boardid_exclude'] ) ) . ")"; |
| 194 |
|
| 195 |
if( ! empty( $args['postid_include'] ) ) $wheres[] = "`postid` IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['postid_include'] ) ) . ")"; |
| 196 |
if( ! empty( $args['postid_exclude'] ) ) $wheres[] = "`postid` NOT IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['postid_exclude'] ) ) . ")"; |
| 197 |
|
| 198 |
return $wheres; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* @param $args |
| 203 |
* @param $select |
| 204 |
* @param $operator |
| 205 |
* |
| 206 |
* @return string |
| 207 |
*/ |
| 208 |
private function build_sql_select( $args, $select = '', $operator = 'AND' ) { |
| 209 |
if( ! $select ) $select = '*'; |
| 210 |
$operator = trim( strtoupper( (string) $operator ) ); |
| 211 |
if( ! in_array( $operator, [ 'AND', 'OR' ], true ) ) $operator = 'AND'; |
| 212 |
|
| 213 |
$sql = "SELECT $select FROM " . WPF()->tables->bookmarks; |
| 214 |
if( $wheres = $this->build_sql_wheres( $args ) ) $sql .= " WHERE " . implode( " $operator ", $wheres ); |
| 215 |
|
| 216 |
$args = $this->parse_args( $args ); |
| 217 |
if( $args['orderby'] ) $sql .= " ORDER BY " . $args['orderby']; |
| 218 |
if( $args['row_count'] ) $sql .= " LIMIT " . intval( $args['offset'] ) . "," . intval( $args['row_count'] ); |
| 219 |
|
| 220 |
return $sql; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* @param array|numeric $args |
| 225 |
* |
| 226 |
* @return array |
| 227 |
*/ |
| 228 |
public function _get_bookmark( $args, $operator = 'AND' ) { |
| 229 |
if( is_numeric( $args ) ) $args = [ 'bookmarkid' => wpforo_bigintval( $args ) ]; |
| 230 |
if( ! wpfkey( $args, 'orderby' ) ) $args['orderby'] = '`bookmarkid` DESC'; |
| 231 |
$bookmark = (array) WPF()->db->get_row( $this->build_sql_select( $args, '*', $operator ), ARRAY_A ); |
| 232 |
if( $bookmark ) $bookmark = $this->decode( $bookmark ); |
| 233 |
|
| 234 |
return $bookmark; |
| 235 |
} |
| 236 |
|
| 237 |
public function get_bookmark( $args, $operator = 'AND' ) { |
| 238 |
return wpforo_ram_get( [ $this, '_get_bookmark' ], $args, $operator ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* @param array $args |
| 243 |
* |
| 244 |
* @return array |
| 245 |
*/ |
| 246 |
public function _get_bookmarks( $args = [], $operator = 'AND' ) { |
| 247 |
return array_map( [ $this, 'decode' ], (array) WPF()->db->get_results( $this->build_sql_select( $args, '*', $operator ), ARRAY_A ) ); |
| 248 |
} |
| 249 |
|
| 250 |
public function get_bookmarks( $args = [], $operator = 'AND' ) { |
| 251 |
return wpforo_ram_get( [ $this, '_get_bookmarks' ], $args, $operator ); |
| 252 |
} |
| 253 |
|
| 254 |
public function _get_bookmarks_col( $col, $args = [], $operator = 'AND' ){ |
| 255 |
$r = WPF()->db->get_col( $this->build_sql_select( $args, "`$col`", $operator ) ); |
| 256 |
if( $this->default->bookmark_format[$col] === '%d' ) $r = array_map( 'wpforo_bigintval', $r ); |
| 257 |
return $r; |
| 258 |
} |
| 259 |
|
| 260 |
public function get_bookmarks_col( $col, $args = [], $operator = 'AND' ){ |
| 261 |
return wpforo_ram_get( [ $this, '_get_bookmarks_col' ], $col, $args, $operator ); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* @param array $args |
| 266 |
* |
| 267 |
* @return int |
| 268 |
*/ |
| 269 |
public function _get_count( $args = [], $operator = 'AND' ) { |
| 270 |
return (int) WPF()->db->get_var( $this->build_sql_select( $args, 'COUNT(*)', $operator ) ); |
| 271 |
} |
| 272 |
|
| 273 |
public function get_count( $args = [], $operator = 'AND' ) { |
| 274 |
return wpforo_ram_get( [$this, '_get_count'], $args, $operator ); |
| 275 |
} |
| 276 |
|
| 277 |
public function get_user_bookmarks_count( $userid ){ |
| 278 |
return $this->get_count( [ 'userid' => $userid, 'status' => true ] ); |
| 279 |
} |
| 280 |
|
| 281 |
public function get_board_post_bookmarks_count( $boardid, $postid ) { |
| 282 |
return $this->get_count( [ 'boardid' => $boardid, 'postid' => $postid, 'status' => true ] ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* @param int $postid |
| 287 |
* @param int $userid |
| 288 |
* @param int $boardid |
| 289 |
* |
| 290 |
* @return array|null |
| 291 |
*/ |
| 292 |
public function get_user_bookmark( $postid, $userid = null, $boardid = null ){ |
| 293 |
if( ! ( $userid = wpforo_bigintval( $userid ) ) ) { |
| 294 |
$userid = WPF()->current_userid; |
| 295 |
} |
| 296 |
if( is_null( $boardid ) ) $boardid = WPF()->board->get_current( 'boardid' ); |
| 297 |
$boardid = intval( $boardid ); |
| 298 |
|
| 299 |
if( $bookmark = $this->get_bookmark( [ 'userid' => $userid, 'boardid' => $boardid, 'postid' => $postid ] ) ) return $bookmark; |
| 300 |
return null; |
| 301 |
} |
| 302 |
|
| 303 |
public function is_user_bookmarked_this( $postid, $userid = null, $boardid = null ) { |
| 304 |
$bookmark = $this->get_user_bookmark( $postid, $userid, $boardid ); |
| 305 |
return (bool) wpfval( $bookmark, 'status' ); |
| 306 |
} |
| 307 |
} |
| 308 |
|