File: /var/www/doco/keytest/wp-content/themes/ormedia/functions.php
<?php
add_theme_support('post-thumbnails'); // 缩略图支持
/**
* 注册post类型,用于获取缩略图信息
* @return [type] [description]
*/
function yt_setup_post()
{
$args_block = array(
'label' => __('大廈', 'ormedia'),
'show_ui' => true,
'show_in_menu' => true,
'supports' => array('title', 'author'),
);
$args_entity = array(
'label' => __('個人或公司', 'ormedia'),
'show_ui' => true,
'show_in_menu' => true,
'supports' => array('title', 'editor', 'author'),
);
$args_contract = array(
'label' => __('物業合約', 'ormedia'),
'show_ui' => true,
'show_in_menu' => true,
'supports' => array('title', 'author'),
);
$args_car_park = array(
'labels' => array(
'name' => __('Car Park', 'ormedia'),
'singular_name' => __('Car Park', 'ormedia')
),
'public' => true,
'menu_icon' => 'dashicons-images-alt2',
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'rewrite' => array('slug' => 'orm_car_park'),
'supports' => array('title'),
'capabilities' => array(
'edit_post' => 'edit_car_park',
'edit_posts' => 'edit_car_park',
'edit_others_posts' => 'edit_other_car_park',
'publish_posts' => 'publish_car_park',
'read_post' => 'read_car_park',
'read_private_posts' => 'read_private_car_park',
'delete_post' => 'delete_car_park',
'delete_posts' => 'delete_car_park',
'delete_others_posts' => 'delete_other_car_park',
),
);
register_post_type('orm_block', $args_block);
register_post_type('orm_entity', $args_entity);
register_post_type('orm_contract', $args_contract);
register_post_type('orm_car_park', $args_car_park);
}
add_action('init', 'yt_setup_post');
function log_data($data)
{
$file = fopen(get_template_directory() . "/tmp/log.log", "w");
if (!empty($data)) {
fwrite($file, serialize($data));
} else {
fwrite($file, "empty");
}
fclose($file);
}
function get_property_meta($property_id, $key)
{ //查询metavalue 从keylab_meta表中,当条件为property_id 和metakey
global $wpdb;
$res = $wpdb->get_var($wpdb->prepare("select meta_value from keylab_meta where property_id = %d and meta_key = %s", $property_id, $key));
return $res;
}
function delete_property_meta($property_id)
{ //从keylab_meta表中,删除$property_id所指的表
global $wpdb;
$status = $wpdb->delete("keylab_meta", array("property_id" => $property_id));
return $status; //返回true/false
}
function update_property_meta($property_id, $key, $val)
{
global $wpdb;
//如果metavalue不完全等于0并且metavalue为空
if (get_property_meta($property_id, $key) !== "0" && empty(get_property_meta($property_id, $key))) {
$res = $wpdb->insert( //插入一条数据。
'keylab_meta',
array(
'property_id' => $property_id,
'meta_key' => $key,
'meta_value' => empty($val) ? '0' : $val
),
array('%d', '%s', '%s')
);
} else {//metavalue有值
$res = $wpdb->update(//更新一条数据.
'keylab_meta',
array('meta_value' => empty($val) ? '0' : $val),
array('property_id' => $property_id, 'meta_key' => $key),
array('%s'),
array('%d', '%s')
);
}
return $res !== false;
}
function insert_calendar($value_arr)
{
global $wpdb;
return $wpdb->insert("keylab_calendar", $value_arr);
}
function update_calendar($value_arr, $where_arr)
{
global $wpdb;
return $wpdb->update("keylab_calendar", $value_arr, $where_arr);
}
function delete_calendar($where_arr)
{
global $wpdb;
return $wpdb->delete("keylab_calendar", $where_arr);
}
function computed_period_day($first_report_date, $period_value, $period_unit)
{
switch ($period_unit) {
case '天':
$first_report_date = strtotime("$first_report_date+" . $period_value . "day");
break;
case '週':
$first_report_date = strtotime("$first_report_date+" . $period_value . "week");
break;
case '月':
$first_report_date = strtotime("$first_report_date+" . $period_value . "month");
break;
case '年':
$first_report_date = strtotime("$first_report_date+" . $period_value . "year");
break;
default:
$first_report_date = strtotime($first_report_date);
break;
}
return date('Y-m-d', $first_report_date);
}
function get_role_name($role)
{
global $wp_roles;
if (!isset($wp_roles))
$wp_roles = new WP_Roles();
return $wp_roles->roles[$role]['name'];
}
function get_table_name($table_name)
{
global $wpdb;
$sql_statment = "select table_comment from information_schema.tables where table_name = '$table_name' && table_schema = 'keylab'";
return $wpdb->get_var($sql_statment);
}
function get_col_name($table_data, $table_name)
{
global $wpdb;
$result_arr = [];
$sql_statment = "select COLUMN_NAME,column_comment from INFORMATION_SCHEMA.Columns where table_name='$table_name' and table_schema='keylab'";
$col_arr = $wpdb->get_results($sql_statment);
foreach ($table_data as $key => $value) {
foreach ($col_arr as $value2) {
if ($value2->COLUMN_NAME == $key) {
if ($value2->column_comment != '') {
$result_arr[$value2->column_comment] = $value;
} else {
$result_arr[$value2->COLUMN_NAME] = $value;
}
break;
}
}
}
return $result_arr;
}
function keylab_data_log_by_user2($status, $old_data, $new_data, $sql, $table_name,$relate_id)
{
global $wpdb;
$allheader = getallheaders();
//如果log_admin_id或者log_user_id为空,则不操作。
if (empty($allheader['log_admin_id']) || empty($allheader['log_user_id'])) {
} else {
if ($status) { //标致,true/false
$data = [
'admin_id' => $allheader['log_admin_id'],
'user_id' => $allheader['log_user_id'],
//获取到当前操作系统的用户
'user_name' => get_user_by('ID', $allheader['log_user_id'])->user_login,
'time' => date("Y-m-d H:i:s", strtotime("+8 hour")),
'sql_panel' => $sql,
//表名
'table_name' => $table_name,
'log_relate_id' => intval($relate_id)
];
//如果旧数据完全不等于空并且新数据等于false
if ($old_data !== false && $new_data == false) {
$data['old_data'] = json_encode(get_col_name($old_data, $table_name));
$data['action'] = 'delete';
$data['remark'] = '';
if (!empty($allheader['log_property_id'])) {
$data['remark'] = $data['remark'] . '物業編號:' . $allheader['log_property_id'] . '; ';
}
$data['remark'] = $data['remark'] . '用戶 ' . get_user_by('ID', $allheader['log_user_id'])->user_login . ' 刪除了' . get_table_name($table_name) . ',編號為' . $relate_id;
}
if ($old_data !== false && $new_data !== false) {
$data['old_data'] = json_encode(get_col_name($old_data, $table_name));
$data['new_data'] = json_encode(get_col_name($new_data, $table_name));
$data['action'] = 'update';
$data['remark'] = '';
if (!empty($allheader['log_property_id'])) {
$data['remark'] = $data['remark'] . '物業編號:' . $allheader['log_property_id'] . '; ';
}
$data['remark'] = $data['remark'] . '用戶 ' . get_user_by('ID', $allheader['log_user_id'])->user_login . ' 更新了' . get_table_name($table_name) . ',編號為' . $relate_id;
}
if ($old_data == false && $new_data !== false) {
$data['new_data'] = json_encode(get_col_name($new_data, $table_name));
$data['action'] = 'create';
$data['remark'] = '';
if (!empty($allheader['log_property_id'])) {
$data['remark'] = $data['remark'] . '物業編號:' . $allheader['log_property_id'] . '; ';
}
$data['remark'] = $data['remark'] . '用戶 ' . get_user_by('ID', $allheader['log_user_id'])->user_login . ' 新增了' . get_table_name($table_name) . ',編號為' . $relate_id;
}
$wpdb->insert("keylab_log", $data);
}
}
return $data;
}
function keylab_data_log_by_user($status, $old_data, $new_data, $sql)
{
global $wpdb;
$allheader = getallheaders();
if ($status) {
$data = [
'admin_id' => $allheader['log_admin_id'],
'user_id' => $allheader['log_user_id'],
'time' => date("Y-m-d H:i:s", strtotime("+8 hour")),
'sql_panel' => $sql
];
if ($old_data !== false && $new_data == false) {
$data['old_data'] = json_encode($old_data);
$data['action'] = 'delete';
}
if ($old_data !== false && $new_data !== false) {
$data['old_data'] = json_encode($old_data);
$data['new_data'] = json_encode($new_data);
$data['action'] = 'update';
}
if ($old_data == false && $new_data !== false) {
$data['new_data'] = json_encode($new_data);
$data['action'] = 'create';
}
$wpdb->insert("keylab_log", $data);
} else {
$wpdb->insert("keylab_log", [
'admin_id' => $allheader['log_admin_id'],
'user_id' => $allheader['log_user_id'],
'action' => 'error',
'time' => date("Y-m-d H:i:s", strtotime("+8 hour")),
'sql_panel' => $sql
]);
}
return $data;
}
function keylab_login_log($log_user_id, $admin_id)
{
global $wpdb;
$wpdb->insert("keylab_log", [
'admin_id' => $admin_id,
'user_id' => $log_user_id,
'user_name' => get_user_by('ID', $log_user_id)->user_login,
'action' => 'login',
'remark' => '用戶登錄',
'time' => date("Y-m-d H:i:s", strtotime("+8 hour"))
]);
}
function keylab_else_log($status, $remark)//keylab日志。
{
global $wpdb;
$allheader = getallheaders();
if (empty($allheader['log_admin_id']) || empty($allheader['log_user_id'])) {
} else {
$wpdb->insert("keylab_log", [
'admin_id' => $allheader['log_admin_id'],
'user_id' => $allheader['log_user_id'],
'user_name' => get_user_by('ID', $allheader['log_user_id'])->user_login,
'action' => 'login',
'remark' => $remark,
'time' => date("Y-m-d H:i:s", strtotime("+8 hour"))
]);
}
}
function update_file_in_use($file)
{
foreach ($file as $key => $value) {
update_post_meta($value['uid'], 'in_use', 1);
}
}
function delete_file_in_use($file)
{
foreach ($file as $key => $value) {
if (!empty(wp_delete_attachment($value['uid']))) {
delete_post_meta($value['uid'], 'in_use');
} else {
return false;
}
}
}
function find_diff_file($new_file, $old_file, &$a_delete)
{
foreach ($new_file as $key => $value) {
update_post_meta($value['uid'], 'in_use', 1);
}
foreach ($old_file as $key1 => $value1) {
array_push($a_delete, $value1['uid']);
foreach ($new_file as $key2 => $value2) {
if ($value1['uid'] == $value2['uid']) {
array_pop($a_delete);
}
}
}
}
function send_mailjet_email($args)
{
require_once "/var/www/html/wp-content/plugins/mailjet-for-wordpress/vendor/autoload.php";
$body = [
'FromEmail' => "keylab@keylab.cc",
'FromName' => "keylab manage",
'Subject' => $args['subject'],
'Html-part' => $args['html'],
'Recipients' => [],
];
if (is_array($args['to'])) {
if (!count($args['to'])) {
return true;
}
foreach ($args['to'] as $t) {
$to = ['Email' => $t];
array_push($body['Recipients'], $to);
}
}
$mailjet = new Mailjet\Client("fdc0ca324e6e4bc2ecdeb4d78744aebc", "e1dc650b7700a6f110373e6cb433f29a");
$mailjet->setConnectionTimeout(120);
$response = $mailjet->post(Mailjet\Resources::$Email, ['body' => $body]);
$response->success() && var_dump($response->getData());
return true;
}
function send_email_by_mailjet($args)
{
require_once "/var/www/html/wp-content/plugins/mailjet-for-wordpress/vendor/autoload.php";
$body = [
'FromEmail' => "keylab@keylab.cc",
'FromName' => "keylab manage",
'Subject' => $args['subject'],
'Html-part' => $args['html'],
'Recipients' => [],
];
if (is_array($args['to'])) {
if (!count($args['to'])) {
return true;
}
foreach ($args['to'] as $t) {
$to = ['Email' => $t];
array_push($body['Recipients'], $to);
}
}
//插件格式。
$mailjet = new Mailjet\Client("fdc0ca324e6e4bc2ecdeb4d78744aebc", "e1dc650b7700a6f110373e6cb433f29a");
$mailjet->setConnectionTimeout(120);
$response = $mailjet->post(Mailjet\Resources::$Email, ['body' => $body]);
return $response->success();
}
function send_app_fcm($tokens, $title, $content)
{
$url = "https://fcm.googleapis.com/fcm/send";
$fields = array(
'to' => ($tokens == "all") ? "/topics/all" : $tokens,
'priority' => 'high',
'forceStart' => '1',
'notification' => array(
'title' => $title,
'body' => $content
)
);
$fields = json_encode($fields);
$headers = array(
'Authorization: key=AAAARfV3Nf4:APA91bGHgZ4RBeLy5jqN5ETIB_9zhSBFhenEXzM8JeKSvcU_v847gy4wA0btf5EqMpJanR3UahL8rTMepxx_tZfcmm0e2kbEtLTZ9I77Zz_5DCG_slIuuzy2HMpkRoHIQdhXJjXP_-sN',
'Content-Type: application/json',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function get_real_property_id($user_id)
{
$real_property_id = get_user_meta($user_id, 'real_property_id', true);
if (empty($real_property_id)) {
return [];
} else {
return explode(',', $real_property_id);
}
}
function get_admin_property_id($user_id)
{
global $wpdb;
$sql_statment = "select property_id from keylab_property where admin_wp_id = '$user_id'";
return $wpdb->get_results($sql_statment);
}
function get_users_meta_null($users_id, $key)
{
global $wpdb;
$res = $wpdb->get_results($wpdb->prepare("select meta_value from keylab_users_meta where users_id = %d and meta_key = %s", $users_id, $key));
// echo $wpdb->last_query;
if (count($res) == 0) {
return null;
} else {
return $res[0]->meta_value;
}
}
function get_users_meta($users_id, $key)
{
global $wpdb;
$res = $wpdb->get_results($wpdb->prepare("select meta_value from keylab_users_meta where users_id = %d and meta_key = %s", $users_id, $key));
// echo $wpdb->last_query;
if (count($res) == 0) {
return '';
} else {
return $res[0]->meta_value;
}
}
function delete_users_meta($users_id)
{
global $wpdb;
$status = $wpdb->delete("keylab_users_meta", array("users_id" => $users_id));
return $status;
}
function update_users_meta($users_id, $key, $val)
{
global $wpdb;
if (get_users_meta_null($users_id, $key) === null) {
$res = $wpdb->insert(
'keylab_users_meta',
array(
'users_id' => $users_id,
'meta_key' => $key,
'meta_value' => $val
),
array('%d', '%s', '%s')
);
} else {
$res = $wpdb->update(
'keylab_users_meta',
array('meta_value' => $val),
array('users_id' => $users_id, 'meta_key' => $key),
array('%s'),
array('%d', '%s')
);
}
return $res !== false;
}
function get_positions($uid)
{
global $wpdb;
return $wpdb->get_results($wpdb->prepare("select * from keylab_position where uid = %d or uid = 0 order by uid asc", $uid));
}