-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
188 lines (171 loc) · 3.31 KB
/
functions.php
File metadata and controls
188 lines (171 loc) · 3.31 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* The functions.php file consolidates reusable functions that can be used across various parts of the project
*
* @link https://https://github.com/developerbayazid
* @since 1.0.0
* @package functions
*/
/**
* Insert data to database
*
* @param array $args ti will return a array with values.
* @return int
*/
function lp_insert_book_shelf( $args = array() ) {
global $wpdb;
$defaults = array(
'shelf_name' => '',
'capacity' => 1,
'shelf_location' => '',
'status' => 1,
'created_by' => get_current_user_id(),
'created_at' => current_time( 'mysql' ),
);
$data = wp_parse_args( $args, $defaults );
$inserted = $wpdb->insert(
"{$wpdb->prefix}library_press_tbl_book_shelf",
$data,
array(
'%s',
'%d',
'%s',
'%d',
'%d',
'%s',
),
);
if ( ! $inserted ) {
return new \WP_Error( 'failed-to-insert', __( 'Failed to insert data', 'library-press' ) );
}
return $wpdb->insert_id;
}
/**
* Insert data to database
*
* @param array $args ti will return a array with values.
* @return int
*/
function lp_insert_book( $args = array() ) {
global $wpdb;
$defaults = array(
'name' => '',
'email' => '',
'publication' => '',
'amount' => 1,
'description' => '',
'book_image' => '',
'shelf_id' => 0,
'status' => 1,
'created_by' => get_current_user_id(),
'created_at' => current_time( 'mysql' ),
);
$data = wp_parse_args( $args, $defaults );
$inserted = $wpdb->insert(
"{$wpdb->prefix}library_press_tbl_books",
$data,
array(
'%s',
'%s',
'%s',
'%d',
'%s',
'%s',
'%d',
'%d',
'%d',
'%s',
),
);
if ( ! $inserted ) {
return new \WP_Error( 'failed-to-insert', __( 'Failed to insert data', 'library-press' ) );
}
return $wpdb->insert_id;
}
/**
* Fetch data
*
* @param string $table_name table name here.
* @return array
*/
function lp_get_data( $table_name ) {
global $wpdb;
$items = $wpdb->get_results(
$wpdb->prepare(
'SELECT * FROM %i ORDER BY id DESC',
esc_sql( $wpdb->prefix . $table_name )
)
);
return $items;
}
/**
* Delete data
*
* @param int $id which id will be delete.
* @param string $table_name where data will be delete.
* @return void
*/
function lp_delete_data( $id, $table_name ) {
global $wpdb;
$wpdb->delete(
$wpdb->prefix . $table_name,
array(
'ID' => $id,
),
array(
'%d',
)
);
}
/**
* Get books by bookShelf
*
* @param int $shelf_id shelf id.
* @return object
*/
function get_books_by_shelf( $shelf_id ) {
global $wpdb;
// Query to fetch books for a specific shelf.
$books = $wpdb->get_results(
$wpdb->prepare(
"
SELECT
book.id AS book_id,
book.name AS book_name,
book.email,
book.publication,
book.amount,
book.description,
book.book_image
FROM
{$wpdb->prefix}library_press_tbl_books AS book
INNER JOIN
{$wpdb->prefix}library_press_tbl_book_shelf AS shelf
ON
book.shelf_id = shelf.id
WHERE
shelf.id = %d
",
$shelf_id
)
);
return $books;
}
/**
* Get single address
*
* @param int $id row id.
* @param string $table_name database table name.
* @return object
*/
function lp_get_row( $id, $table_name ) {
global $wpdb;
$data = $wpdb->get_row(
$wpdb->prepare(
'SELECT * FROM %i WHERE id = %d',
$wpdb->prefix . $table_name,
$id
)
);
return $data;
}