/
KBContentOption.class.php
108 lines (96 loc) · 3.06 KB
/
KBContentOption.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* KBoard 게시글 옵션
* @link www.cosmosfarm.com
* @copyright Copyright 2021 Cosmosfarm. All rights reserved.
* @license http://www.gnu.org/licenses/gpl.html
*/
class KBContentOption {
var $content_uid;
var $row;
public function __construct($content_uid=''){
$this->row = array();
if($content_uid){
$this->initWithContentUID($content_uid);
}
}
public function __get($key){
$value = '';
$key = sanitize_key($key);
if(isset($this->row[$key])){
$object = $this->row[$key];
if(is_string($this->row[$key])){
$object = @unserialize($this->row[$key]);
}
if($object !== false){
$value = $object;
}
else{
$value = $this->row[$key];
}
}
return apply_filters('kboard_content_option_value', $value, $key, $this);
}
public function __set($key, $value){
global $wpdb, $cosmosfarm_migration_in_progress;
if($this->content_uid){
$key = sanitize_key($key);
$this->row[$key] = $value;
$value = esc_sql($value);
if($value != ''){
$count = 0;
if(!$cosmosfarm_migration_in_progress){
$count = $wpdb->get_var("SELECT COUNT(*) FROM `{$wpdb->prefix}kboard_board_option` WHERE `content_uid`='$this->content_uid' AND `option_key`='$key'");
}
if(is_array($value)){
if($count){
$wpdb->query("DELETE FROM `{$wpdb->prefix}kboard_board_option` WHERE `content_uid`='$this->content_uid' AND `option_key`='$key'");
}
foreach($value as $option){
if(is_array($option)){
$option = serialize($option);
}
$wpdb->query("INSERT INTO `{$wpdb->prefix}kboard_board_option` (`content_uid`, `option_key`, `option_value`) VALUES ('$this->content_uid', '$key', '$option')");
}
}
else{
if($count){
$wpdb->query("UPDATE `{$wpdb->prefix}kboard_board_option` SET `option_value`='$value' WHERE `content_uid`='$this->content_uid' AND `option_key`='$key'");
}
else{
$wpdb->query("INSERT INTO `{$wpdb->prefix}kboard_board_option` (`content_uid`, `option_key`, `option_value`) VALUES ('$this->content_uid', '$key', '$value')");
}
}
}
else if(!$cosmosfarm_migration_in_progress){
$wpdb->query("DELETE FROM `{$wpdb->prefix}kboard_board_option` WHERE `content_uid`='$this->content_uid' AND `option_key`='$key'");
}
}
}
public function initWithContentUID($content_uid){
global $wpdb;
$this->row = array();
$this->content_uid = intval($content_uid);
$results = $wpdb->get_results("SELECT * FROM `{$wpdb->prefix}kboard_board_option` WHERE `content_uid`='$this->content_uid' ORDER BY `uid` ASC");
$wpdb->flush();
$option_list = array();
foreach($results as $row){
if(!isset($option_list[$row->option_key])) $option_list[$row->option_key] = array();
$option_list[$row->option_key][] = $row->option_value;
}
foreach($option_list as $option_key=>$option_value){
if(count($option_value) > 1){
$this->row[$option_key] = $option_value;
}
else{
$this->row[$option_key] = $option_value[0];
}
}
}
public function toArray(){
if($this->content_uid){
return $this->row;
}
return array();
}
}