-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp_optionLib.php
163 lines (146 loc) · 4.68 KB
/
wp_optionLib.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Library for exploring wordpress options table
*
* @uses wordpress core 3.0.x
* @category Options
* @package Options
* @subpackage Option
* @example _example_wp_optionLib.php
*
* @version 0.0.1 18desc2010
*
* @access public
* @todo create function to list non-autoload/autoload option
* @todo translate every phpdoc comment into english
* @author uwiuw
* @copyright 2010 uwiuw
*/
class wp_optionLib {
public function wp_optionLib(){
}
/**
* membuat perintah sql yg akan meretrieve field2x yg berada pada table wp_options
*
* @global object $wpdb
* @param string $field
* @param string $where
* @param bool $echo
* @return string
*
* @todo Cari perintah sql untuk meretrieve nama2x field pd sebuah table
* @todo buat param $where bisa berbentuk array
*/
public function get_optiontable_sql($field ='*' , $where='', $echo = FALSE){
global $wpdb;
$sql ="SELECT $field FROM " . $wpdb->prefix . "options";
if ($where != '') {
$sql .= ' WHERE ' . $where;
}
if (!$echo){
return $sql;
} else {
echo $sql;
}
}
/**
* meretrieve isi table options berdasar prasayat sql
*
* @example $this->get_optiontable() will retrieve all information from the
* option table cause default field is *
*
* @global object $wpdb
* @param string $field
* @param string $where
* @param bool $echo
* @return array
*/
public function get_optiontable($field ='*', $where='', $echo = FALSE){
global $wpdb;
$sql = $this->get_optiontable_sql($field, $where, $echo);
$outputs = $wpdb->get_results($sql, ARRAY_A);
if (count($outputs) > 0) {
if (!$echo){
return $outputs;
} else {
echo $outputs;
}
}
}
/**
* retrieve value sebuah option name dan unserialize value itu bila berbentuk array
*
* Sebagai catatan, wordpress option_name bisa memiliki berbagai macam value. bisa berbentuk
* string, serialiaze array, numerical string. Dan semuanya disimpan ke dalam format string
* sql hanya bisa menyimpan format string. Jadi kita @todo membuat function yg bisa
* menerjemahkan option kedalam format format asalnya
*
* @global $wpdb
* @param string $option_name
* @param bool $serialize if true and the $output is array then serialize them
* @param bool $echo
* @return mixed
*/
public function get_option_value($option_name, $serialize=FALSE, $echo = FALSE){
global $wpdb;
$field = 'option_value';
$where = "option_name='$option_name'";
$sql = $this->get_optiontable_sql($field, $where);
$outputs = $wpdb->get_results($sql, ARRAY_A);
/*
* check whether $outputs is an array
*/
if (count($outputs) > 0) {
if ($serialize) {
if (count($outputs) > 1) {
/*
* if total count result lebih dari satu
*/
foreach ($outputs as $output ) {
$outputs_temp[] = unserialize($output['option_value']);
}
$outputs = $outputs_temp;
unset($outputs_temp);
} else {
/*
* bila total count cuma ada satu
*/
$outputs = unserialize($outputs[0]['option_value']);
}
}
// else {
// /**
// * by passing
// */
// }
if (!$echo){
return $outputs;
} else {
echo $outputs;
}
}
}
/**
* retrieve value dari sekumpulan option value *
*
* @global $wpdb
* @param array $option_names_args pertiap index-array memiliki 2 option, pertama nama dan yg kedua
* apakah hendak diserialiaze atawa tidak
* @param bool $echo
* @return mixed
*/
public function get_options_value($option_names_args=array(), $echo = FALSE){
if (is_array($option_names_args) && count($option_names_args) > 0 ) {
foreach ($option_names_args as $key_optionName =>$serial) {
if ($hasil = $this->get_option_value($key_optionName, $serial)) {
$outputs[$key_optionName] = $hasil;
}
}
if (!$echo){
return $outputs;
} else {
echo $outputs;
}
}
}
}