1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-10 02:06:44 +02:00
gdk/functions/Common.php

1459 lines
45 KiB
PHP
Raw Normal View History

2020-01-25 15:50:40 +01:00
<?php
2021-10-06 15:44:41 +02:00
2021-10-10 15:48:53 +02:00
function gdk_log($log, $dump = null)
{
if (isset($dump)) {
$log = var_dump($log);
}
echo "<script>console.info('log:".$log."');</script>";
}
2020-04-07 03:34:36 +02:00
2021-10-10 15:48:53 +02:00
function gdk_rand_color()
{
$arr = [
'red',
'green',
'blue',
'yellow',
];
$rndKey = array_rand($arr);
echo $arr[$rndKey];
2020-04-07 03:34:36 +02:00
}
2021-10-10 15:48:53 +02:00
//获取友情链接ID默认是第一个创建的分类
2021-10-10 15:48:53 +02:00
function gdk_link_id()
{
$arr = get_terms('link_category', 'orderby=id&hide_empty=0');
return $arr[0]->term_id;
}
2020-01-25 15:50:40 +01:00
/**
2020-02-20 11:59:26 +01:00
* 获取摘要
2021-10-10 15:48:53 +02:00
*
* @param mixed $length
* @param null|mixed $post
* @param mixed $echo
* @param mixed $more
2020-01-25 15:50:40 +01:00
*/
2020-03-15 14:29:06 +01:00
function gdk_print_excerpt($length, $post = null, $echo = true, $more = '...')
{
global $post;
$text = $post->post_excerpt;
2020-01-25 15:50:40 +01:00
if ('' == $text) {
2020-03-15 14:29:06 +01:00
$text = get_the_content(); //获取文字
2020-01-25 15:50:40 +01:00
$text = apply_filters('the_content', $text);
2021-10-10 15:48:53 +02:00
$text = strtr($text, [']]>' => ']]>']);
2020-01-25 15:50:40 +01:00
}
$text = strip_shortcodes($text);
$text = strip_tags($text);
2020-03-15 14:29:06 +01:00
$excerpt = wp_trim_words($text, $length, $more);
2020-01-25 15:50:40 +01:00
2021-10-10 15:48:53 +02:00
if (isset($excerpt)) {
2020-03-15 14:29:06 +01:00
$result = apply_filters('the_excerpt', $excerpt);
2020-01-25 15:50:40 +01:00
}
2021-10-10 15:48:53 +02:00
if (true == $echo) {
2020-01-25 15:50:40 +01:00
echo $result;
} else {
return $result;
}
}
function nc_comment_add_at($comment_text, $comment = '')
{
if (!empty($comment) && $comment->comment_parent > 0) {
2021-10-10 15:48:53 +02:00
$comment_text = '<a rel="nofollow" class="comment_at" href="#comment-'.$comment->comment_parent.'">@'.get_comment_author($comment->comment_parent).'</a> '.$comment_text;
2020-01-25 15:50:40 +01:00
}
2021-10-10 15:48:53 +02:00
2020-01-25 15:50:40 +01:00
return $comment_text;
}
2020-04-03 13:11:11 +02:00
function gdk_record_visitors()
2020-03-15 14:29:06 +01:00
{
if (is_singular()) {
global $post;
$post_ID = $post->ID;
if ($post_ID) {
2021-10-10 15:48:53 +02:00
$post_views = (int) get_post_meta($post_ID, 'views', true);
2020-03-15 14:29:06 +01:00
if (!update_post_meta($post_ID, 'views', ($post_views + 1))) {
add_post_meta($post_ID, 'views', 1, true);
}
}
}
2020-01-25 15:50:40 +01:00
}
function nc_post_views($before = '(点击 ', $after = ' 次)', $echo = 1)
{
global $post;
$post_ID = $post->ID;
2021-10-10 15:48:53 +02:00
$views = (int) get_post_meta($post_ID, 'views', true);
2020-01-25 15:50:40 +01:00
if ($echo) {
echo $before, number_format($views), $after;
} else {
return $views;
}
}
/**
* Load a component into a template while supplying data.
*
2021-10-10 15:48:53 +02:00
* @param string $slug the slug name for the generic template
* @param array $params An associated array of data that will be extracted into the templates scope
* @param bool $output whether to output component or return as string
*
2020-01-25 15:50:40 +01:00
* @return string
*/
2021-10-10 15:48:53 +02:00
function nc_get_template_part_with_vars($slug, array $params = [], $output = true)
2020-01-25 15:50:40 +01:00
{
if (!$output) {
ob_start();
}
$template_file = locate_template("{$slug}.php", false, false);
2021-10-10 15:48:53 +02:00
extract(['template_params' => $params], EXTR_SKIP);
2020-03-15 14:29:06 +01:00
require $template_file;
2020-01-25 15:50:40 +01:00
if (!$output) {
return ob_get_clean();
}
}
function nc_ajax_load_comments()
{
global $wp_query;
2021-10-10 15:48:53 +02:00
$type = sanitize_text_field($_POST['type']);
2020-01-25 15:50:40 +01:00
$paged = sanitize_text_field($_POST['paged']);
$q = sanitize_text_field($_POST['query']);
if ($paged < 1 || $paged > $_POST['commentcount']) {
wp_die();
}
2021-10-10 15:48:53 +02:00
if ('page' === $type) {
$wp_query = new WP_Query(['page_id' => $q, 'cpage' => $paged]);
2020-01-25 15:50:40 +01:00
}
2021-10-10 15:48:53 +02:00
if ('post' === $type) {
$wp_query = new WP_Query(['p' => $q, 'cpage' => $paged]);
2020-01-25 15:50:40 +01:00
}
if (have_posts()) {
while (have_posts()) {
the_post();
comments_template();
}
}
wp_reset_postdata();
wp_die();
}
/**
* 获取评论下一页页码
*/
function nc_get_next_page_number()
{
$page_number = get_comment_pages_count();
2021-10-10 15:48:53 +02:00
if ('newest' == get_option('default_comments_page')) {
2020-01-25 15:50:40 +01:00
$next_page = $page_number - 1;
} else {
$next_page = 2;
}
2021-10-10 15:48:53 +02:00
2020-01-25 15:50:40 +01:00
return $next_page;
}
function nc_like_init($key, $direct = false)
{
// $direct === true 时不计 cookie
2021-10-10 15:48:53 +02:00
$id = $_POST['id'];
$action = $_POST['do_action'];
2020-01-25 15:50:40 +01:00
$lh_raters = get_post_meta($id, $key, true);
2021-10-10 15:48:53 +02:00
$domain = ('localhost' != $_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : false;
2020-01-25 15:50:40 +01:00
2021-10-10 15:48:53 +02:00
if ('do' == $action) {
2020-01-25 15:50:40 +01:00
$expire = time() + 99999999;
2021-10-10 15:48:53 +02:00
if (!isset($_COOKIE[$key.'_'.$id]) || $direct) {
gdk_set_cookie($key.'_'.$id, $id, $expire);
2020-01-25 15:50:40 +01:00
if (!$lh_raters || !is_numeric($lh_raters)) {
update_post_meta($id, $key, 1);
} else {
update_post_meta($id, $key, ($lh_raters + 1));
}
}
}
2021-10-10 15:48:53 +02:00
if ('undo' == $action && !$direct) {
2020-01-25 15:50:40 +01:00
$expire = time() - 1;
2021-10-10 15:48:53 +02:00
if (isset($_COOKIE[$key.'_'.$id])) {
gdk_set_cookie($key.'_'.$id, $id, $expire);
2020-01-25 15:50:40 +01:00
update_post_meta($id, $key, ($lh_raters - 1));
}
}
echo get_post_meta($id, $key, true);
2021-10-10 15:48:53 +02:00
exit;
2020-01-25 15:50:40 +01:00
}
function nc_timeago($ptime = null, $post = null)
{
2021-10-10 15:48:53 +02:00
if (null === $post) {
2020-01-25 15:50:40 +01:00
global $post;
}
$ptime = $ptime ?: get_post_time('G', false, $post);
2021-10-10 15:48:53 +02:00
return human_time_diff($ptime, current_time('timestamp')).'前';
2020-01-25 15:50:40 +01:00
}
function nc_get_translated_role_name($user_id)
{
2021-10-10 15:48:53 +02:00
$data = get_userdata($user_id);
2020-01-25 15:50:40 +01:00
$roles = $data->roles;
if (array_key_exists('administrator', $roles)) {
2020-01-25 15:50:40 +01:00
return __('Administrator', 'jimu');
2021-10-10 15:48:53 +02:00
}
if (array_key_exists('editor', $roles)) {
2020-01-25 15:50:40 +01:00
return __('Certified Editor', 'jimu');
2021-10-10 15:48:53 +02:00
}
if (array_key_exists('author', $roles)) {
2020-01-25 15:50:40 +01:00
return __('Special Author', 'jimu');
2021-10-10 15:48:53 +02:00
}
if (array_key_exists('subscriber', $roles)) {
2020-01-25 15:50:40 +01:00
return __('Subscriber', 'jimu');
}
return __('Contributor', 'jimu');
}
2020-03-15 14:29:06 +01:00
function nc_get_meta($key, $single = true)
{
2020-01-25 15:50:40 +01:00
global $post;
2021-10-10 15:48:53 +02:00
2020-01-25 15:50:40 +01:00
return get_post_meta($post->ID, $key, $single);
}
2020-03-15 14:29:06 +01:00
function nc_the_meta($key, $placeholder = '')
{
2020-01-25 15:50:40 +01:00
echo nc_get_meta($key, true) ?: $placeholder;
2020-01-25 18:54:41 +01:00
}
2020-02-02 11:19:41 +01:00
//判定是否是手机
2020-03-15 14:29:06 +01:00
function gdk_is_mobile()
{
2020-01-26 18:57:04 +01:00
$ua = $_SERVER['HTTP_USER_AGENT'];
if (empty($ua)) {
2020-01-25 18:54:41 +01:00
return false;
2021-10-10 15:48:53 +02:00
}
if ((in_string($ua, 'Mobile') && false === strpos($ua, 'iPad')) // many mobile devices (all iPh, etc.)
2020-03-15 14:29:06 +01:00
|| in_string($ua, 'Android') || in_string($ua, 'NetType/') || in_string($ua, 'Kindle') || in_string($ua, 'MQQBrowser') || in_string($ua, 'Opera Mini') || in_string($ua, 'Opera Mobi') || in_string($ua, 'HUAWEI') || in_string($ua, 'TBS/') || in_string($ua, 'Mi') || in_string($ua, 'iPhone')) {
2020-01-25 18:54:41 +01:00
return true;
}
2021-10-10 15:48:53 +02:00
return false;
2020-01-25 18:54:41 +01:00
}
2020-01-26 15:14:37 +01:00
//判断是否是登陆页面
2020-03-15 14:29:06 +01:00
function is_login()
{
return array_key_exists($GLOBALS['pagenow'], ['wp-login.php', 'wp-register.php']);
2020-01-26 15:14:37 +01:00
}
//判断字符串内是否有指定字符串
2020-03-15 14:29:06 +01:00
function in_string($text, $find)
{
2021-10-10 15:48:53 +02:00
if (false !== strpos($text, $find)) {
2020-03-15 14:29:06 +01:00
return true;
}
2021-10-10 15:48:53 +02:00
return false;
2020-01-26 18:57:04 +01:00
}
2020-02-03 19:12:24 +01:00
//判断是否是微信
2020-03-15 14:29:06 +01:00
function gdk_is_weixin()
{
if (in_string($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
2020-02-03 19:12:24 +01:00
return true;
}
2021-10-10 15:48:53 +02:00
return false;
2020-02-03 19:12:24 +01:00
}
2020-02-02 11:19:41 +01:00
//获取浏览器信息
2021-10-10 15:48:53 +02:00
function gdk_getBrowser()
2020-03-15 14:29:06 +01:00
{
2021-10-10 15:48:53 +02:00
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = $u_agent;
2020-03-15 14:29:06 +01:00
$platform = '';
2021-10-10 15:48:53 +02:00
$version = '';
2020-03-15 14:29:06 +01:00
//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'Linux';
} elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'Mac';
} elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'Windows';
}
// Next get the name of the useragent yes seperately and for good reason
if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) {
$bname = 'Internet Explorer';
2021-10-10 15:48:53 +02:00
$ub = 'MSIE';
2020-03-15 14:29:06 +01:00
} elseif (preg_match('/Firefox/i', $u_agent)) {
$bname = 'Mozilla Firefox';
2021-10-10 15:48:53 +02:00
$ub = 'Firefox';
2020-03-15 14:29:06 +01:00
} elseif (preg_match('/Chrome/i', $u_agent)) {
$bname = 'Google Chrome';
2021-10-10 15:48:53 +02:00
$ub = 'Chrome';
2020-03-15 14:29:06 +01:00
} elseif (preg_match('/Safari/i', $u_agent)) {
$bname = 'Apple Safari';
2021-10-10 15:48:53 +02:00
$ub = 'Safari';
2020-03-15 14:29:06 +01:00
} elseif (preg_match('/Opera/i', $u_agent)) {
$bname = 'Opera';
2021-10-10 15:48:53 +02:00
$ub = 'Opera';
2020-03-15 14:29:06 +01:00
} elseif (preg_match('/Netscape/i', $u_agent)) {
$bname = 'Netscape';
2021-10-10 15:48:53 +02:00
$ub = 'Netscape';
2020-03-15 14:29:06 +01:00
}
// finally get the correct version number
2021-10-10 15:48:53 +02:00
$known = ['Version', $ub, 'other'];
$pattern = '#( ?<browser>'.join('|', $known).
2020-03-15 14:29:06 +01:00
')[/ ]+( ?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}
if (isset($matches['browser']) && is_array($matches['browser'])) {
// see how many we have
$i = count($matches['browser']);
2021-10-10 15:48:53 +02:00
if (1 != $i) {
2020-03-15 14:29:06 +01:00
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
2021-10-10 15:48:53 +02:00
if (strripos($u_agent, 'Version') < strripos($u_agent, $ub)) {
2020-03-15 14:29:06 +01:00
$version = $matches['version'][0];
} else {
$version = $matches['version'][1];
}
} else {
$version = $matches['version'][0];
}
} else {
2021-10-10 15:48:53 +02:00
$version = '?';
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
return [
2020-03-15 14:29:06 +01:00
'userAgent' => $u_agent,
2021-10-10 15:48:53 +02:00
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern,
];
2020-01-26 18:57:04 +01:00
}
2020-02-02 11:19:41 +01:00
//获取IP地址
2020-03-15 14:29:06 +01:00
function gdk_get_ip()
{
2021-10-10 15:48:53 +02:00
$proxy_headers = ['CLIENT_IP', 'FORWARDED', 'FORWARDED_FOR', 'FORWARDED_FOR_IP', 'HTTP_CLIENT_IP', 'HTTP_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED_FOR_IP', 'HTTP_PC_REMOTE_ADDR', 'HTTP_PROXY_CONNECTION', 'HTTP_VIA', 'HTTP_X_FORWARDED', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR_IP', 'HTTP_X_IMFORWARDS', 'HTTP_XROXY_CONNECTION', 'VIA', 'X_FORWARDED', 'X_FORWARDED_FOR'];
foreach ($proxy_headers as $proxy_header) {
if (isset($_SERVER[$proxy_header])) {
if (gdk_validate_ip($_SERVER[$proxy_header])) {
return $_SERVER[$proxy_header];
2021-10-10 15:48:53 +02:00
}
if (false !== stristr(',', $_SERVER[$proxy_header])) {
$proxy_header_temp = trim(array_shift(explode(',', $_SERVER[$proxy_header])));
if (($pos_temp = in_string($proxy_header_temp, ':'))) {
$proxy_header_temp = substr($proxy_header_temp, 0, $pos_temp);
}
if (gdk_validate_ip($proxy_header_temp)) {
return $proxy_header_temp;
}
2020-03-15 14:29:06 +01:00
}
}
}
2021-10-10 15:48:53 +02:00
if (gdk_validate_ip($_SERVER['REMOTE_ADDR'])) {
return $_SERVER['REMOTE_ADDR'];
2020-03-15 14:29:06 +01:00
}
2020-01-26 18:57:04 +01:00
}
/**
* Ensures an ip address is both a valid IP and does not fall within
* a private network range.
2021-10-10 15:48:53 +02:00
*
* @param mixed $ip
2020-01-26 18:57:04 +01:00
*/
2020-03-15 14:29:06 +01:00
function gdk_validate_ip($ip)
{
2021-10-10 15:48:53 +02:00
if ('unknown' === strtolower($ip)) {
2020-03-15 14:29:06 +01:00
return false;
}
// generate ipv4 network address
$ip = ip2long($ip);
// if the ip is set and not equivalent to 255.255.255.255
2021-10-10 15:48:53 +02:00
if (false !== $ip && -1 !== $ip) {
2020-03-15 14:29:06 +01:00
// make sure to get unsigned long representation of ip
// due to discrepancies between 32 and 64 bit OSes and
// signed numbers ( ints default to signed in PHP)
$ip = sprintf('%u', $ip);
// do private network range checking
if ($ip >= 0 && $ip <= 50331647) {
return false;
}
if ($ip >= 167772160 && $ip <= 184549375) {
return false;
}
if ($ip >= 2130706432 && $ip <= 2147483647) {
return false;
}
if ($ip >= 2851995648 && $ip <= 2852061183) {
return false;
}
if ($ip >= 2886729728 && $ip <= 2887778303) {
return false;
}
if ($ip >= 3221225984 && $ip <= 3221226239) {
return false;
}
if ($ip >= 3232235520 && $ip <= 3232301055) {
return false;
}
if ($ip >= 4294967040) {
return false;
}
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return true;
2020-01-26 18:57:04 +01:00
}
//Ajax报错方式
2020-03-15 14:29:06 +01:00
function gdk_die($ErrMsg)
{
header('HTTP/1.1 405 Method Not Allowed');
header('Content-Type: text/plain;charset=UTF-8');
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
exit($ErrMsg);
2020-01-27 15:28:34 +01:00
}
2020-02-02 19:46:18 +01:00
//面包屑导航
2020-03-15 14:29:06 +01:00
function gdk_breadcrumbs($delimiter = '»', $hometitle = 'Home')
{
2020-04-03 13:11:11 +02:00
$before = '<span class="current active">';
2020-02-02 19:46:18 +01:00
// 在当前链接前插入
$after = '</span>';
// 在当前链接后插入
2020-03-15 14:29:06 +01:00
if (!is_home() && !is_front_page() || is_paged()) {
2020-04-03 13:11:11 +02:00
echo '<div itemscope itemtype="http://schema.org/WebPage" id="crumbs" class="cm-breadcrumb">';
2020-02-02 19:46:18 +01:00
global $post;
$homeLink = home_url();
2021-10-10 15:48:53 +02:00
echo ' <a itemprop="breadcrumb" href="'.$homeLink.'">'.$hometitle.'</a> '.$delimiter.' ';
2020-03-15 14:29:06 +01:00
if (is_category()) {
2020-02-02 19:46:18 +01:00
// 分类 存档
global $wp_query;
2021-10-10 15:48:53 +02:00
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
2020-02-02 19:46:18 +01:00
$parentCat = get_category($thisCat->parent);
2021-10-10 15:48:53 +02:00
if (0 != $thisCat->parent) {
$cat_code = get_category_parents($parentCat, true, ' '.$delimiter.' ');
2020-03-15 14:29:06 +01:00
echo $cat_code = str_replace('<a', '<a itemprop="breadcrumb"', $cat_code);
2020-02-02 19:46:18 +01:00
}
2021-10-10 15:48:53 +02:00
echo $before.''.single_cat_title('', false).''.$after;
2020-03-15 14:29:06 +01:00
} elseif (is_day()) {
2020-02-02 19:46:18 +01:00
// 天 存档
2021-10-10 15:48:53 +02:00
echo '<a itemprop="breadcrumb" href="'.get_year_link(get_the_time('Y')).'">'.get_the_time('Y').'</a> '.$delimiter.' ';
echo '<a itemprop="breadcrumb" href="'.get_month_link(get_the_time('Y'), get_the_time('m')).'">'.get_the_time('F').'</a> '.$delimiter.' ';
echo $before.get_the_time('d').$after;
2020-03-15 14:29:06 +01:00
} elseif (is_month()) {
2020-02-02 19:46:18 +01:00
// 月 存档
2021-10-10 15:48:53 +02:00
echo '<a itemprop="breadcrumb" href="'.get_year_link(get_the_time('Y')).'">'.get_the_time('Y').'</a> '.$delimiter.' ';
echo $before.get_the_time('F').$after;
2020-03-15 14:29:06 +01:00
} elseif (is_year()) {
2020-02-02 19:46:18 +01:00
// 年 存档
2021-10-10 15:48:53 +02:00
echo $before.get_the_time('Y').$after;
2020-03-15 14:29:06 +01:00
} elseif (is_single() && !is_attachment()) {
2020-02-02 19:46:18 +01:00
// 文章
2021-10-10 15:48:53 +02:00
if ('post' != get_post_type()) {
2020-02-02 19:46:18 +01:00
// 自定义文章类型
$post_type = get_post_type_object(get_post_type());
2021-10-10 15:48:53 +02:00
$slug = $post_type->rewrite;
echo '<a itemprop="breadcrumb" href="'.$homeLink.'/'.$slug['slug'].'/">'.$post_type->labels->singular_name.'</a> '.$delimiter.' ';
echo $before.get_the_title().$after;
2020-02-02 19:46:18 +01:00
} else {
// 文章 post
2021-10-10 15:48:53 +02:00
$cat = get_the_category();
$cat = $cat[0];
$cat_code = get_category_parents($cat, true, ' '.$delimiter.' ');
2020-03-15 14:29:06 +01:00
echo $cat_code = str_replace('<a', '<a itemprop="breadcrumb"', $cat_code);
2021-10-10 15:48:53 +02:00
echo $before.get_the_title().$after;
2020-02-02 19:46:18 +01:00
}
2021-10-10 15:48:53 +02:00
} elseif (!is_single() && !is_page() && 'post' != get_post_type()) {
2020-02-02 19:46:18 +01:00
$post_type = get_post_type_object(get_post_type());
2021-10-10 15:48:53 +02:00
echo $before.$post_type->labels->singular_name.$after;
2020-03-15 14:29:06 +01:00
} elseif (is_attachment()) {
2020-02-02 19:46:18 +01:00
// 附件
$parent = get_post($post->post_parent);
2021-10-10 15:48:53 +02:00
$cat = get_the_category($parent->ID);
$cat = $cat[0];
echo '<a itemprop="breadcrumb" href="'.get_permalink($parent).'">'.$parent->post_title.'</a> '.$delimiter.' ';
echo $before.get_the_title().$after;
2020-03-15 14:29:06 +01:00
} elseif (is_page() && !$post->post_parent) {
2020-02-02 19:46:18 +01:00
// 页面
2021-10-10 15:48:53 +02:00
echo $before.get_the_title().$after;
2020-03-15 14:29:06 +01:00
} elseif (is_page() && $post->post_parent) {
2020-02-02 19:46:18 +01:00
// 父级页面
2021-10-10 15:48:53 +02:00
$parent_id = $post->post_parent;
$breadcrumbs = [];
2020-02-02 19:46:18 +01:00
while ($parent_id) {
2021-10-10 15:48:53 +02:00
$page = get_page($parent_id);
$breadcrumbs[] = '<a itemprop="breadcrumb" href="'.get_permalink($page->ID).'">'.get_the_title($page->ID).'</a>';
$parent_id = $page->post_parent;
2020-02-02 19:46:18 +01:00
}
$breadcrumbs = array_reverse($breadcrumbs);
2020-03-15 14:29:06 +01:00
foreach ($breadcrumbs as $crumb) {
2021-10-10 15:48:53 +02:00
echo $crumb.' '.$delimiter.' ';
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
echo $before.get_the_title().$after;
2020-03-15 14:29:06 +01:00
} elseif (is_search()) {
2020-02-02 19:46:18 +01:00
// 搜索结果
2020-03-15 14:29:06 +01:00
echo $before;
printf(__('Search Results for: %s', 'cmp'), get_search_query());
echo $after;
} elseif (is_tag()) {
2020-02-02 19:46:18 +01:00
//标签 存档
2020-03-15 14:29:06 +01:00
echo $before;
printf(__('Tag Archives: %s', 'cmp'), single_tag_title('', false));
echo $after;
} elseif (is_author()) {
2020-02-02 19:46:18 +01:00
// 作者存档
global $author;
$userdata = get_userdata($author);
2020-03-15 14:29:06 +01:00
echo $before;
printf(__('Author Archives: %s', 'cmp'), $userdata->display_name);
echo $after;
} elseif (is_404()) {
2020-02-02 19:46:18 +01:00
// 404 页面
echo $before;
2020-03-15 14:29:06 +01:00
_e('Not Found', 'cmp');
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
echo $after;
2020-02-02 19:46:18 +01:00
}
2020-03-15 14:29:06 +01:00
if (get_query_var('paged')) {
2020-02-02 19:46:18 +01:00
// 分页
2020-03-15 14:29:06 +01:00
if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author()) {
echo sprintf(__('( Page %s )', 'cmp'), get_query_var('paged'));
}
2020-02-02 19:46:18 +01:00
}
echo '</div>';
}
}
2020-01-27 15:28:34 +01:00
//设置cookie数据
2020-03-15 14:29:06 +01:00
function gdk_set_cookie($key, $value, $expire)
{
$expire = ($expire < time()) ? $expire + time() : $expire;
$secure = ('https' === parse_url(get_option('home'), PHP_URL_SCHEME));
setcookie($key, $value, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure);
if (COOKIEPATH != SITECOOKIEPATH) {
2020-01-27 15:28:34 +01:00
setcookie($key, $value, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure);
}
$_COOKIE[$key] = $value;
}
2021-10-10 15:48:53 +02:00
//判断是否是电话号码,号码返回 true 不是返回false
2020-03-15 14:29:06 +01:00
function gdk_is_mobile_number($number)
{
2021-10-10 15:48:53 +02:00
return (bool) preg_match('/^0{0,1}(1[3,5,8][0-9]|14[5,7]|166|17[0,1,3,6,7,8]|19[8,9])[0-9]{8}$/', $number);
2020-01-27 15:28:34 +01:00
}
//获取纯文本
2020-03-15 14:29:06 +01:00
function gdk_plain_text($text)
{
$text = wp_strip_all_tags($text);
2021-10-10 15:48:53 +02:00
$replace = ['"' => '', '\'' => '', "\r\n" => ' ', "\n" => ' ', ' ' => ' '];
$text = strtr($text, $replace);
2020-03-15 14:29:06 +01:00
return trim($text);
2020-01-27 15:28:34 +01:00
}
// 获取第一段
2020-03-15 14:29:06 +01:00
function gdk_first_p($text)
{
if ($text) {
$text = explode("\n", trim(strip_tags($text)));
$text = trim($text['0']);
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $text;
2020-01-27 15:28:34 +01:00
}
//获取当前页面链接
2020-03-15 14:29:06 +01:00
function gdk_get_current_url()
{
2021-10-10 15:48:53 +02:00
$ssl = (!empty($_SERVER['HTTPS']) && 'on' == $_SERVER['HTTPS']) ? true : false;
$sp = strtolower($_SERVER['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')).(($ssl) ? 's' : '');
$port = $_SERVER['SERVER_PORT'];
$port = ((!$ssl && '80' == $port) || ($ssl && '443' == $port)) ? '' : ':'.$port;
$host = $_SERVER['HTTP_X_FORWARDED_HOST'] ?? $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'];
return $protocol.'://'.$host.$port.$_SERVER['REQUEST_URI'];
2020-01-27 15:28:34 +01:00
}
2020-02-02 11:19:41 +01:00
//发起HTTP请求
2021-10-10 15:48:53 +02:00
function gdk_http_request($url, $args = [], $err_args = [])
{
$args = wp_parse_args($args, [
'timeout' => 3,
'method' => '',
'body' => [],
'sslverify' => false,
'blocking' => true, // 如果不需要立刻知道结果,可以设置为 false
'stream' => false, // 如果是保存远程的文件,这里需要设置为 true
'filename' => null, // 设置保存下来文件的路径和名字
'need_json_decode' => true, //对结果进行解码,一般都需要
'need_json_encode' => false, //对发起参数编码
// 'headers' => array('Accept-Encoding'=>'gzip;'), //使用压缩传输数据
// 'headers' => array('Accept-Encoding'=>''),
// 'compress' => false,
'decompress' => true,
]);
2020-03-15 14:29:06 +01:00
$need_json_decode = $args['need_json_decode'];
$need_json_encode = $args['need_json_encode'];
2021-10-10 15:48:53 +02:00
$method = ($args['method']) ? strtoupper($args['method']) : ($args['body'] ? 'POST' : 'GET');
unset($args['need_json_decode'], $args['need_json_encode'], $args['method']);
if ('GET' == $method) {
2020-03-15 14:29:06 +01:00
$response = wp_remote_get($url, $args);
2021-10-10 15:48:53 +02:00
} elseif ('POST' == $method) {
2020-03-15 14:29:06 +01:00
if ($need_json_encode && is_array($args['body'])) {
$args['body'] = json_encode($args['body']);
}
$response = wp_remote_post($url, $args);
2021-10-10 15:48:53 +02:00
} elseif ('HEAD' == $method) {
2020-03-15 14:29:06 +01:00
if ($need_json_encode && is_array($args['body'])) {
$args['body'] = json_encode($args['body']);
}
$response = wp_remote_head($url, $args);
} else {
if ($need_json_encode && is_array($args['body'])) {
$args['body'] = json_encode($args['body']);
}
$response = wp_remote_request($url, $args);
}
if (is_wp_error($response)) {
2021-10-10 15:48:53 +02:00
trigger_error($url."\n".$response->get_error_code().' : '.$response->get_error_message()."\n".var_export($args['body'], true));
2020-03-15 14:29:06 +01:00
return $response;
}
2021-10-10 15:48:53 +02:00
$headers = $response['headers'];
2020-03-15 14:29:06 +01:00
$response = $response['body'];
if ($need_json_decode || isset($headers['content-type']) && strpos($headers['content-type'], '/json')) {
if ($args['stream']) {
$response = file_get_contents($args['filename']);
}
$response = json_decode($response, true);
if (is_wp_error($response)) {
return $response;
}
}
2021-10-10 15:48:53 +02:00
extract(wp_parse_args($err_args, [
2020-03-15 14:29:06 +01:00
'errcode' => 'errcode',
2021-10-10 15:48:53 +02:00
'errmsg' => 'errmsg',
'detail' => 'detail',
2020-03-15 14:29:06 +01:00
'success' => 0,
2021-10-10 15:48:53 +02:00
]));
2020-03-15 14:29:06 +01:00
if (isset($response[$errcode]) && $response[$errcode] != $success) {
$errcode = $response[$errcode];
2021-10-10 15:48:53 +02:00
$errmsg = isset($response[$errmsg]) ? $response[$errmsg] : '';
2020-03-15 14:29:06 +01:00
if (isset($response[$detail])) {
$detail = $response[$detail];
2021-10-10 15:48:53 +02:00
trigger_error($url."\n".$errcode.' : '.$errmsg."\n".var_export($detail, true)."\n".var_export($args['body'], true));
2020-03-15 14:29:06 +01:00
return new WP_Error($errcode, $errmsg, $detail);
}
2021-10-10 15:48:53 +02:00
trigger_error($url."\n".$errcode.' : '.$errmsg."\n".var_export($args['body'], true));
return new WP_Error($errcode, $errmsg);
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $response;
2020-01-27 15:28:34 +01:00
}
2020-02-02 11:19:41 +01:00
//根据腾讯视频网址或者ID互相转化
2020-03-15 14:29:06 +01:00
function gdk_get_qq_vid($id_or_url)
{
if (filter_var($id_or_url, FILTER_VALIDATE_URL)) {
if (preg_match('#https://v.qq.com/x/page/(.*?).html#i', $id_or_url, $matches)) {
2020-01-27 15:28:34 +01:00
return $matches[1];
2021-10-10 15:48:53 +02:00
}
if (preg_match('#https://v.qq.com/x/cover/.*/(.*?).html#i', $id_or_url, $matches)) {
2020-01-27 15:28:34 +01:00
return $matches[1];
}
2021-10-10 15:48:53 +02:00
return '';
2020-01-27 15:28:34 +01:00
}
2021-10-10 15:48:53 +02:00
return $id_or_url;
2020-01-27 15:28:34 +01:00
}
2020-02-02 11:19:41 +01:00
//根据秒拍id或者网站获取视频直连
2020-03-15 14:29:06 +01:00
function get_video_mp4($id_or_url)
{
if (filter_var($id_or_url, FILTER_VALIDATE_URL)) {
if (preg_match('#http://www.miaopai.com/show/(.*?).htm#i', $id_or_url, $matches)) {
2021-10-10 15:48:53 +02:00
return 'http://gslb.miaopai.com/stream/'.esc_attr($matches[1]).'.mp4';
}
if (preg_match('#https://v.qq.com/x/page/(.*?).html#i', $id_or_url, $matches)) {
2020-01-27 15:28:34 +01:00
return get_qqv_mp4($matches[1]);
2021-10-10 15:48:53 +02:00
}
if (preg_match('#https://v.qq.com/x/cover/.*/(.*?).html#i', $id_or_url, $matches)) {
2020-01-27 15:28:34 +01:00
return get_qqv_mp4($matches[1]);
}
2021-10-10 15:48:53 +02:00
return str_replace(['%3A', '%2F'], [':', '/'], urlencode($id_or_url));
2020-01-27 15:28:34 +01:00
}
2021-10-10 15:48:53 +02:00
return get_qqv_mp4($id_or_url);
2020-01-27 15:28:34 +01:00
}
2020-02-02 11:19:41 +01:00
//获取腾讯视频
2020-03-15 14:29:06 +01:00
function get_qqv_mp4($vid)
{
if (strlen($vid) > 20) {
return new WP_Error('invalid_qqv_vid', '非法的腾讯视频 ID');
}
$mp4 = wp_cache_get($vid, 'qqv_mp4');
2021-10-10 15:48:53 +02:00
if (false === $mp4) {
$response = gdk_http_request('http://vv.video.qq.com/getinfo?otype=json&platform=11001&vid='.$vid, [
'timeout' => 4,
2020-03-15 14:29:06 +01:00
'need_json_decode' => false,
2021-10-10 15:48:53 +02:00
]);
2020-03-15 14:29:06 +01:00
if (is_wp_error($response)) {
return $response;
}
$response = trim(substr($response, strpos($response, '{')), ';');
$response = json_decode($response);
if (is_wp_error($response)) {
return $response;
}
if (empty($response['vl'])) {
return new WP_Error('illegal_qqv', '该腾讯视频不存在或者为收费视频!');
}
2021-10-10 15:48:53 +02:00
$u = $response['vl']['vi'][0];
$p0 = $u['ul']['ui'][0]['url'];
$p1 = $u['fn'];
$p2 = $u['fvkey'];
$mp4 = $p0.$p1.'?vkey='.$p2;
2020-03-15 14:29:06 +01:00
wp_cache_set($vid, $mp4, 'qqv_mp4', HOUR_IN_SECONDS * 6);
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $mp4;
}
//字符串转数组,默认分隔符是:,
2020-03-15 14:29:06 +01:00
function gdk_str2arr($data, $delimiter = ',')
{
// 数组原样返回
if (is_array($data)) {
return $data;
}
// 字符串处理
2021-10-10 15:48:53 +02:00
$string = (string) $data;
2020-03-15 14:29:06 +01:00
if (empty($string)) {
$result = [];
} elseif (preg_match('/^{.*?}$/', $string) || preg_match('/^\[.*?]$/', $string)) {
$result = json_decode($string, true);
} elseif (preg_match('/^a:.*?(})$/', $string)) {
$result = unserialize($string, null);
} elseif (strpos($string, $delimiter) >= 1) {
$result = explode($delimiter, $string);
} else {
$result = [];
}
if (!is_array($result) || count($result) < 1) {
return [];
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $result;
}
2020-02-02 11:19:41 +01:00
//根据连接特征获取网盘连接来源
2020-03-15 14:29:06 +01:00
function gdk_panlinks($links)
{
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return false;
}
if (in_string($links, 'baidu')) {
$linknane = '百度网盘';
2020-03-15 14:29:06 +01:00
} elseif (in_string($links, 'yunpan')) {
$linknane = '360云盘';
2020-03-15 14:29:06 +01:00
} elseif (in_string($links, 'lanzous')) {
$linknane = '蓝奏网盘';
2020-03-15 14:29:06 +01:00
} elseif (in_string($links, '189')) {
$linknane = '天翼云盘';
2020-03-15 14:29:06 +01:00
} elseif (in_string($links, 'mega')) {
$linknane = 'MEGA云盘';
2020-03-15 14:29:06 +01:00
} elseif (in_string($links, 'yadi.sk')) {
$linknane = '毛子云盘';
2020-03-15 14:29:06 +01:00
} elseif (in_string($links, 'cdn')) {
$linknane = 'CDN';
2020-03-15 14:29:06 +01:00
} elseif (in_string($links, 'ctfile')) {
$linknane = '360云盘';
2020-03-15 14:29:06 +01:00
} elseif (in_string($links, 'weiyun')) {
$linknane = '腾讯微云';
2020-03-15 14:29:06 +01:00
} else {
$linknane = '下载';
}
2021-10-10 15:48:53 +02:00
return $linknane;
2020-02-02 11:19:41 +01:00
}
//一个简单可重复使用的邮件模板
2021-10-10 15:48:53 +02:00
function gdk_mail_temp($mail_title, $mail_cotent, $link, $link_title)
2020-03-15 14:29:06 +01:00
{
2021-10-10 15:48:53 +02:00
return '<div style="width:500px;margin:auto">
<h1 style="background:#2695f3;color:#fff;padding:20px 10px;">'.$mail_title.'</h1>
<div style="padding:15px;border-bottom:dashed 1px #ddd;">'.$mail_cotent.'</div>
<a href="'.$link.'" style="display:block;margin:auto;margin-top:40px;padding:10px;width:150px;outline:0;border:1px solid #2695f3;border-radius:25px;color:#2695f3;text-align:center;font-weight:700;font-size:14px;text-decoration:none;" rel="noopener" target="_blank">'.$link_title.'</a>
2020-02-02 11:19:41 +01:00
<br><br>
<div style="color:#cecece;font-size: 12px;">本邮件为系统自动发送,请勿回复。<br>
2021-10-10 15:48:53 +02:00
如果不想被此类邮件打扰,请前往 <a style="color: #cecece;" href="'.home_url().'" rel="noopener" target="_blank">'.get_option('blogname').'</a> 留言说明,由我们来操作处理。
2020-03-26 15:24:00 +01:00
</div></div>';
2020-02-02 11:19:41 +01:00
}
2020-02-02 19:46:18 +01:00
//获取所有站点分类id,带缓存
2020-03-15 14:29:06 +01:00
function gdk_category()
{
2020-02-02 19:46:18 +01:00
$cat_ids = get_transient('gdk_category');
if (false === $cat_ids) {
$categories = get_terms('category', 'hide_empty=0');
2021-10-10 15:48:53 +02:00
$k = [];
2020-02-02 19:46:18 +01:00
foreach ($categories as $categorie) {
$k[] = $categorie->term_id;
}
2021-10-10 15:48:53 +02:00
$cat_ids = implode(',', $k);
2020-03-15 14:29:06 +01:00
set_transient('gdk_category', $cat_ids, 60 * 60 * 24 * 5); //缓存5天
2020-02-02 19:46:18 +01:00
}
2021-10-10 15:48:53 +02:00
$cat_ids = explode(',', $cat_ids);
2020-02-02 19:46:18 +01:00
foreach ($cat_ids as $catid) {
$cat_name = get_cat_name($catid);
2021-10-10 15:48:53 +02:00
$output = '<span>'.$cat_name.'=(<b>'.$catid.'</b>)</span>&nbsp;&nbsp;';
2020-02-02 19:46:18 +01:00
echo $output;
}
2020-02-04 18:14:04 +01:00
}
/*使用字符串转数组分类标签获取信息
2020-03-15 14:29:06 +01:00
*$term 分类还是标签, tag是标签,cat是分类
*$meta 需要获取的具体项目,参数des=描述,参数keyword=关键词,参数img=图片
*$id 分类还是标签的id,为空显示当前分类或者标签数据
*数据来源于分类/标签的图片描述
*/
function gdk_term_meta($term, $meta, $id)
{
2021-10-10 15:48:53 +02:00
if ('cat' == $term) {
2020-03-15 14:29:06 +01:00
$term_meta = gdk_str2arr(category_description($id), '@@');
2021-10-10 15:48:53 +02:00
} elseif ('tag' == $term) {
2020-03-15 14:29:06 +01:00
$term_meta = gdk_str2arr(tag_description($id), '@@');
} else {
return false;
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
switch ($meta) {
case 'des':
$result = $term_meta[0];
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
break;
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
case 'keyword':
$result = $term_meta[1];
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
break;
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
case 'img':
$result = $term_meta[2];
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
break;
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
default:
2020-02-04 18:14:04 +01:00
return false;
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $result;
2020-02-04 18:14:04 +01:00
}
2020-04-03 13:11:11 +02:00
//CDN 缩略图处理样式
function gdk_thumb_color()
{
switch (gdk_option('gdk_cdn_serves')) {
case '1':
2020-04-06 04:24:38 +02:00
case '3':
2020-04-03 13:11:11 +02:00
return '?imageAve';
2021-10-10 15:48:53 +02:00
2020-04-03 13:11:11 +02:00
break;
2021-10-10 15:48:53 +02:00
2020-04-03 13:11:11 +02:00
case '2':
return '!/exformat/hex';
2021-10-10 15:48:53 +02:00
2020-04-03 13:11:11 +02:00
break;
2021-10-10 15:48:53 +02:00
2020-04-03 13:11:11 +02:00
case '4':
return '?x-oss-process=image/average-hue';
2021-10-10 15:48:53 +02:00
2020-04-03 13:11:11 +02:00
break;
2021-10-10 15:48:53 +02:00
2020-04-03 13:11:11 +02:00
case '5':
return '?x-image-process=image/average-hue';
2021-10-10 15:48:53 +02:00
2020-04-03 13:11:11 +02:00
break;
2021-10-10 15:48:53 +02:00
2020-04-03 13:11:11 +02:00
default:
return false;
}
}
//CDN 缩略图处理样式
function gdk_thumb_style($width, $height)
{
switch (gdk_option('gdk_cdn_serves')) {
case '1':
2021-10-10 15:48:53 +02:00
return '?imageView2/1/w/'.$width.'/h/'.$height;
break;
2021-10-10 15:48:53 +02:00
case '2':
2021-10-10 15:48:53 +02:00
return '!/both/'.$width.'x'.$height.'/force/true';
break;
2021-10-10 15:48:53 +02:00
case '3':
2021-10-10 15:48:53 +02:00
return '?imageMogr2/thumbnail/'.$width.'x'.$height.'!';
break;
2021-10-10 15:48:53 +02:00
case '4':
2021-10-10 15:48:53 +02:00
return '?x-oss-process=image/resize,m_fill,h_'.$height.',w_'.$width.',limit_0';
break;
2021-10-10 15:48:53 +02:00
case '5':
2021-10-10 15:48:53 +02:00
return '?x-image-process=image/resize,m_fill,h_'.$height.',w_'.$width.',limit_0';
break;
2021-10-10 15:48:53 +02:00
default:
return false;
}
}
2020-02-04 18:14:04 +01:00
//输出缩略图地址
2021-10-10 15:48:53 +02:00
function gdk_thumbnail_src($id = null)
2020-03-15 14:29:06 +01:00
{
2021-10-10 15:48:53 +02:00
if (isset($id)) {
$post_ID = $id;
$post_content = get_post($id)->post_content;
} else {
global $post;
$post_ID = $post->ID;
$post_content = $post->post_content;
}
$gdk_thumb = get_post_meta($post_ID, 'gdk_thumb');
if (isset($gdk_thumb)) {
2020-03-26 15:24:00 +01:00
//输出自定义域图片地址
2021-10-10 15:48:53 +02:00
$gdk_thumbnail_url = $gdk_thumb[0];
2020-03-26 15:24:00 +01:00
} elseif (has_post_thumbnail()) {
//如果有特色缩略图,则输出缩略图地址
2021-10-10 15:48:53 +02:00
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post_ID), 'full');
$gdk_thumbnail_url = $thumbnail_src[0];
2020-02-04 18:14:04 +01:00
} else {
ob_start();
ob_end_clean();
2021-10-10 15:48:53 +02:00
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $output);
$gdk_thumbnail_url = $output[1][0]; //获取该图片 src
if (empty($gdk_thumbnail_url)) {
2020-03-26 15:24:00 +01:00
//如果日志中没有图片,则显示随机图片
2021-10-10 15:48:53 +02:00
$gdk_thumbnail_url = GDK_BASE_URL.'assets/img/thumb/'.mt_rand(1, 12).'.jpg';
2020-02-04 18:14:04 +01:00
}
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
return $gdk_thumbnail_url;
2020-02-04 18:14:04 +01:00
}
2020-02-13 01:53:12 +01:00
/**
* 获取略缩图输出img标签代码
2021-10-10 15:48:53 +02:00
*
2020-02-13 01:53:12 +01:00
* @param [init] $way 缩略图方案代码1=cdn2=timthumb3=aq_resize
* @param [init] $width 缩略图宽度
* @param [init] $height 缩略图高度
* @param [string] $atrr img标签的属性
2021-10-10 15:48:53 +02:00
*
2020-02-13 01:53:12 +01:00
* @return [string] img标签的图片代码
*/
function gdk_thumb_img($way, $width, $height, $atrr = 'class="thumb_img"')
2020-03-15 14:29:06 +01:00
{
2020-02-13 12:07:45 +01:00
$url = gdk_thumbnail_src();
2021-10-10 15:48:53 +02:00
if (1 === $way) {//cdn
$src = $url.gdk_thumb_style($width, $height);
} elseif (2 === $way) {
$src = GDK_BASE_URL.'public/timthumb.php?src='.$url.'&h='.$height.'&w='.$width.'&q=90&zc=1&ct=1';
} elseif (3 === $way) {
2020-03-15 14:29:06 +01:00
$src = aq_resize($url, $width, $height, true);
if (empty($src)) {
2021-10-10 15:48:53 +02:00
$src = GDK_BASE_URL.'public/timthumb.php?src='.$url.'&h='.$height.'&w='.$width.'&q=90&zc=1&ct=1';
2020-02-13 12:07:45 +01:00
}
2020-03-15 14:29:06 +01:00
} else {
2020-02-13 01:53:12 +01:00
return false;
}
2021-10-10 15:48:53 +02:00
echo '<img '.$atrr.' src="'.$src.'">';
2020-02-13 01:53:12 +01:00
}
2020-02-04 18:14:04 +01:00
//生成订单号编码
2020-03-15 14:29:06 +01:00
function gdk_order_id()
{
date_default_timezone_set('Asia/Shanghai');
2021-10-10 15:48:53 +02:00
return 'E'.date('YmdHis').mt_rand(10000, 99999);
2020-02-04 18:14:04 +01:00
}
//生成随机数
function randomString($length = 11)
{
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
}
2020-03-20 14:22:58 +01:00
//获取云落的远程通知加入缓存12小时一次
function gdk_Remote_Notice($url = 'https: //u.gitcafe.net/api/notice.txt', $hours = 12)
2020-03-15 14:29:06 +01:00
{
$Yunluo_Notice = get_transient('Yunluo_Notice');
if (false === $Yunluo_Notice) {
2020-03-20 14:22:58 +01:00
$response = wp_remote_get($url);
if (is_array($response) && !is_wp_error($response)) {
set_transient('Yunluo_Notice', $response['body'], 60 * 60 * $hours); //缓存12小时
2020-03-15 14:29:06 +01:00
} else {
2020-03-20 14:22:58 +01:00
set_transient('Yunluo_Notice', '有点小尴尬哈啊,服务器菌暂时有点累了呢,先休息一会儿~', 60 * 60 * $hours); //缓存12小时
2020-03-15 14:29:06 +01:00
}
2020-02-04 18:14:04 +01:00
}
2021-10-10 15:48:53 +02:00
2020-02-04 18:14:04 +01:00
return $Yunluo_Notice;
}
//获取页面id并且不可重用
2020-03-15 14:29:06 +01:00
function gdk_page_id($pagephp)
{
2020-02-04 18:14:04 +01:00
global $wpdb;
$pagephp = esc_sql($pagephp);
2021-10-10 15:48:53 +02:00
$pageid = $wpdb->get_row("SELECT `post_id` FROM `{$wpdb->postmeta}` WHERE `meta_value` = 'pages/{$pagephp}.php'", ARRAY_A)['post_id'];
2020-02-04 18:14:04 +01:00
return $pageid;
}
//根据订单描述金币数据d=订单号 u=用户id
2021-10-10 15:48:53 +02:00
function gdk_order_check($d, $u = null)
2020-03-15 14:29:06 +01:00
{
global $wpdb;
2021-10-10 15:48:53 +02:00
$des = " WHERE `description` = '".$d."'";
$userid = '';
if (isset($u) && (null !== $u)) {
$userid = " AND `user_id` = '".$u."'";
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
$result = $wpdb->query('SELECT `point_id` FROM '.GDK_Points_Database::points_get_table('users').$des.$userid." AND `status` = 'accepted' LIMIT 3", ARRAY_A);
2020-03-15 14:29:06 +01:00
return $result; //0=无订单结果1=有订单结果,>1均为异常重复入库数据
2020-02-04 18:14:04 +01:00
}
function gdk_get_Version()
{
echo '<input type="button" class="button button-secondary get_new_version" value="点击检测更新">';
}
2020-02-06 11:51:43 +01:00
/*
* Payjs支付操作函数
* 订单标题
2020-02-06 11:51:43 +01:00
* 订单备注
* $_POST['money'] = 提交的金额,$_POST['way'] = 支付方式,支付宝为alipay,不设置默认微信,
*/
2020-03-15 14:29:06 +01:00
function payjs_action($body, $attach, $money)
{
$config = [
'mchid' => gdk_option('gdk_payjs_id'), // 配置商户号
2021-10-10 15:48:53 +02:00
'key' => gdk_option('gdk_payjs_key'), // 配置通信密钥
2020-03-15 14:29:06 +01:00
];
// 初始化
$payjs = new GDK_Payjs($config);
2021-10-10 15:48:53 +02:00
$data = [
'body' => $body, // 订单标题
'attach' => $attach, // 订单备注
'out_trade_no' => gdk_order_id(), // 订单号
'total_fee' => intval($money) * 100, // 金额,单位:分
'notify_url' => GDK_BASE_URL.'public/notify.php', //异步通知文件
'hide' => '1',
2020-03-15 14:29:06 +01:00
];
2021-10-10 15:48:53 +02:00
$result['money'] = intval($money); //RMB金额
2020-03-15 14:29:06 +01:00
$result['trade_no'] = $data['out_trade_no'];
2021-10-10 15:48:53 +02:00
if ('alipay' == $_POST['way']) {
$data['type'] = 'alipay';
2020-03-15 14:29:06 +01:00
$result['way'] = '支付宝';
} else {
$result['way'] = '微信';
}
if (gdk_is_mobile()) {
2021-10-10 15:48:53 +02:00
$rst = $payjs->cashier($data); //手机使用收银台
2020-03-15 14:29:06 +01:00
$result['img'] = $rst;
} else {
2021-10-10 15:48:53 +02:00
$rst = $payjs->native($data); //电脑使用扫码
2020-03-15 14:29:06 +01:00
$result['img'] = $rst['code_url'];
}
if (in_string($attach, 'PP')) { //如果是付费可见,增加一个参数
$result['mode'] = '1'; //1=付费可见
} else {
$result['mode'] = '0';
}
2020-03-15 14:29:06 +01:00
exit(implode('|', $result)); //以字符串形式返回并停止运行
2020-02-06 11:51:43 +01:00
}
//接受payjs支付结果推送
2020-03-15 14:29:06 +01:00
function payjs_notify()
{
// 配置通信参数
$config = [
'mchid' => gdk_option('gdk_payjs_id'), // 配置商户号
2021-10-10 15:48:53 +02:00
'key' => gdk_option('gdk_payjs_key'), // 配置通信密钥
2020-03-15 14:29:06 +01:00
];
$payjs = new GDK_Payjs($config);
2021-10-10 15:48:53 +02:00
$data = $payjs->notify(); //需要做签名性检查
2020-03-15 14:29:06 +01:00
// 对返回码判断
2021-10-10 15:48:53 +02:00
if (1 == $data['return_code']) {
2020-03-15 14:29:06 +01:00
echo 'success';
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $data;
}
2021-10-10 15:48:53 +02:00
exit($data['return_msg']);
2020-02-06 11:51:43 +01:00
}
2020-02-05 19:44:30 +01:00
//充值按钮
2020-03-15 14:29:06 +01:00
function buy_points()
{
2020-03-26 15:24:00 +01:00
if (is_user_logged_in()) {
//logined
2020-02-05 19:44:30 +01:00
$result = '
2020-02-15 12:37:30 +01:00
<a data-fancybox="pay_fancybox" data-src="#pay_fancybox" href="javascript:;" class="cm-btn primary">点击充值</a>
2020-02-05 19:44:30 +01:00
<form id="pay_fancybox" name="pay_form" style="display: none; width: 100%; max-width: 500px;" class="pure-form">
<h2 class="mb-3">积分充值</h2>
<p>请在下面输入充值金额以及支付工具,微信支付宝都可以,如果下面选项中有支付宝一般建议支付宝</p>
2021-10-10 15:48:53 +02:00
<p class="cm-alert success">本站支付比例为: 1 RMB = '.gdk_option('gdk_rate').'金币</p></blockquote>
2020-02-05 19:44:30 +01:00
<label for="money">支付金额</label>
<input name="money" id="money" min="1" value="2" type="number" required>
<br /><label for="pay_way">支付方式</label>';
2020-03-15 14:29:06 +01:00
if (gdk_option('gdk_payjs_alipay')) {
$result .= '
2020-02-05 19:44:30 +01:00
<label><input name="pay_way" type="radio" value = "alipay" checked/> 支付宝</label> &nbsp;&nbsp;&nbsp;&nbsp;<label><input name="pay_way" type="radio" value = "wechat" /> 微信</label>';
2020-03-15 14:29:06 +01:00
} else {
$result .= '<br /><label><input name="pay_way" type="radio" value = "wechat" checked/> 微信</label>';
}
$result .= '
2020-02-15 12:37:30 +01:00
<p class="mb-0 cm-text-right">
2021-10-10 15:48:53 +02:00
<input data-fancybox-close type="button" id="submit_pay" data-action="pay_points" data-id="'.get_current_user_id().'" class="cm-btn primary" value="提交">
2020-02-05 19:44:30 +01:00
</p>
</form>';
2020-03-26 15:24:00 +01:00
} else {
// no login
2021-10-10 15:48:53 +02:00
$result = '<div class=\'cm-alert error\'>本页面需要您登录才可以操作,请先 '.weixin_login_btn().' 或者<a href="'.esc_url(wp_registration_url()).'">立即注册</a></div>';
2020-02-05 19:44:30 +01:00
}
2021-10-10 15:48:53 +02:00
2020-02-07 16:57:46 +01:00
return $result;
}
2020-02-05 19:44:30 +01:00
//获取bing图
2021-10-10 15:48:53 +02:00
function gdk_get_bing_img()
2020-03-15 14:29:06 +01:00
{
$bing_imgurl = get_transient('Bing_img');
2020-03-15 14:29:06 +01:00
if (false === $bing_imgurl) {
2021-10-10 15:48:53 +02:00
$arr = json_decode(file_get_contents('https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'), true);
$bing_imgurl = 'https://cn.bing.com'.$arr['images'][0]['url'];
2020-03-15 14:29:06 +01:00
set_transient('Bing_img', $bing_imgurl, 60 * 60 * 12);
}
2021-10-10 15:48:53 +02:00
return $bing_imgurl;
}
2020-02-05 19:44:30 +01:00
2020-03-15 14:29:06 +01:00
function login_modal()
{
2020-02-07 16:57:46 +01:00
$result = '<a data-fancybox="login_fancybox" data-src="#login_fancybox" href="javascript:;">登录</a>
<div id="login_fancybox" style="width: 100%; max-width: 500px;overflow:auto;display:none;">';
2021-10-10 15:48:53 +02:00
$result .= wp_login_form([
'echo' => false,
2020-03-15 14:29:06 +01:00
'value_remember' => true,
'value_username' => '请输入用户名...',
2021-10-10 15:48:53 +02:00
]);
2020-02-07 16:57:46 +01:00
$result .= '</div>';
2021-10-10 15:48:53 +02:00
return $result;
2020-02-07 16:57:46 +01:00
}
2020-02-05 19:44:30 +01:00
2021-10-10 15:48:53 +02:00
/*开始微信*
* 生成随机字符
* sk是12位随机字符, key格式是域名@sk
*/
2020-03-15 14:29:06 +01:00
function gdk_weauth_token()
{
2020-02-07 16:57:46 +01:00
$strs = 'QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm';
2021-10-10 15:48:53 +02:00
$sk = substr(str_shuffle($strs), mt_rand(0, strlen($strs) - 11), 12); //12位
set_transient($sk.'-OK', 1, 60); //1分钟缓存 get_transient($sk.'OK') == 1
$key = $_SERVER['HTTP_HOST'].'@'.$sk;
2020-02-07 16:57:46 +01:00
return $key;
2020-03-15 14:29:06 +01:00
}
2020-03-15 14:29:06 +01:00
function gdk_weauth_qr()
{
2021-10-10 15:48:53 +02:00
$qr64 = [];
$qr64['key'] = gdk_weauth_token();
$qr64['qrcode'] = gdk_http_request('https://wa.isdot.net/qrcode?str='.$qr64['key'])['qrcode'];
2020-02-13 01:53:12 +01:00
//$qr64['qrcode'] = gdk_http_request('https://api.goauth.jysafe.cn/qrcode?str='.$qr64['key'])['qrcode'];//预备使用,备胎
2020-02-07 16:57:46 +01:00
return $qr64;
2020-03-15 14:29:06 +01:00
}
2020-02-10 18:40:56 +01:00
2020-02-07 16:57:46 +01:00
/**
2021-10-10 15:48:53 +02:00
* 微信登陆按钮.
2020-02-07 16:57:46 +01:00
*/
2020-03-15 14:29:06 +01:00
function weixin_login_btn()
{
2020-06-29 04:47:57 +02:00
$result = '<a id="weixin_login_btn" href="javascript:;" data-action="gdk_weauth_qr_gen" class="cm-btn primary weixin_login_btn">微信登陆</a>';
2020-03-15 14:29:06 +01:00
if (is_user_logged_in()) {
$user_id = get_current_user_id();
2021-10-10 15:48:53 +02:00
$user = get_user_by('id', $user_id);
2020-03-15 14:29:06 +01:00
if ($user_id > 0) {
if (!empty($user->user_email)) {
$result .= '<script>window.localStorage.setItem(\'ls-bind\',1);</script>';
}
}
2020-03-15 14:29:06 +01:00
//$result .= '<p>您已登陆</p><p>您的ID是:'.$user_id.'</p><p>您的昵称是:'.$user->nickname.'</p>';
}
2021-10-10 15:48:53 +02:00
2020-02-05 19:44:30 +01:00
return $result;
2020-02-07 16:57:46 +01:00
}
2021-10-10 15:48:53 +02:00
// 获取用户ID
2020-03-15 14:29:06 +01:00
function create_user_id($userdata)
{
2021-10-10 15:48:53 +02:00
$nickname = $userdata[0];
$wxavatar = $userdata[6];
$openid = $userdata[7];
$password = wp_generate_password(12, false);
$login_name = 'wx_'.wp_create_nonce($openid);
if (is_user_logged_in()) {
$user_id = get_current_user_id();
update_user_meta($user_id, 'wx_openid', $openid);
update_user_meta($user_id, 'wx_avatar', $wxavatar);
} else {
2021-10-10 15:48:53 +02:00
$weauth_user = get_users(
[
'meta_key ' => 'wx_openid',
'meta_value' => $openid,
]
2020-03-15 14:29:06 +01:00
);
if (is_wp_error($weauth_user) || !count($weauth_user)) {
2021-10-10 15:48:53 +02:00
$user_info = [
'user_login' => $login_name,
2020-03-15 14:29:06 +01:00
'display_name' => $nickname,
2021-10-10 15:48:53 +02:00
'user_pass' => $password,
'nickname' => $nickname,
];
$user_id = wp_insert_user($user_info);
update_user_meta($user_id, 'wx_openid', $openid);
update_user_meta($user_id, 'wx_avatar', $wxavatar);
} else {
$user_id = $weauth_user[0]->ID;
}
}
2021-10-10 15:48:53 +02:00
return $user_id;
}
2020-02-07 16:57:46 +01:00
2020-02-15 12:37:30 +01:00
//生成hover颜色
2020-03-15 14:29:06 +01:00
function editorial_hover_color($hex, $steps)
{
// Steps should be between -255 and 255. Negative = darker, positive = lighter
$steps = max(-255, min(255, $steps));
// Normalize into a six character long hex string
2021-10-10 15:48:53 +02:00
$hex = strtr($hex, '#', '');
if (3 == strlen($hex)) {
$hex = str_repeat(substr($hex, 0, 1), 2).str_repeat(substr($hex, 1, 1), 2).str_repeat(substr($hex, 2, 1), 2);
2020-03-15 14:29:06 +01:00
}
// Split into three parts: R, G and B
$color_parts = str_split($hex, 2);
2021-10-10 15:48:53 +02:00
$return = '#';
2020-03-15 14:29:06 +01:00
foreach ($color_parts as $color) {
$color = hexdec($color);
// Convert to decimal
$color = max(0, min(255, $color + $steps));
// Adjust color
$return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT);
// Make two char hex code
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $return;
2020-02-15 12:37:30 +01:00
}
/**
2021-10-10 15:48:53 +02:00
* Get minified css and removed space.
*
* @param mixed $css
2020-02-15 12:37:30 +01:00
*/
2020-03-15 14:29:06 +01:00
function pwd_minify_css($css)
{
// Normalize whitespace
$css = preg_replace('/\s+/', ' ', $css);
// Remove ; before }
$css = preg_replace('/;(?=\s*})/', '', $css);
// Remove space after , : ; { } */ >
$css = preg_replace('/(,|:|;|\{|}|\*\/|>) /', '$1', $css);
// Remove space before , ; { }
$css = preg_replace('/ (,|;|\{|})/', '$1', $css);
// Strips leading 0 on decimal values (converts 0.5px into .5px)
$css = preg_replace('/(:| )0\.([0-9]+)(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}.${2}${3}', $css);
// Strips units if value is 0 (converts 0px to 0)
$css = preg_replace('/(:| )(\.?)0(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}0', $css);
// Return minified CSS
return trim($css);
2020-02-15 12:37:30 +01:00
}
//生成微信验证码
2020-03-15 14:29:06 +01:00
function wx_captcha()
{
date_default_timezone_set('Asia/Shanghai');
2021-10-10 15:48:53 +02:00
$min = floor(date('i') / 2);
$day = date('d');
$day = ltrim($day, 0);
$home = home_url();
$wx_token = gdk_option('gdk_wxmp_token');
$captcha = sha1($min.$home.$wx_token);
2020-02-15 12:37:30 +01:00
2021-10-10 15:48:53 +02:00
return substr($captcha, $day, 6);
2020-02-16 12:38:04 +01:00
}
2020-03-15 14:29:06 +01:00
function gdk_post_dropdown()
{
2021-10-10 15:48:53 +02:00
// Option list of all post
$gdk_options_posts = [];
2020-02-16 12:38:04 +01:00
$gdk_options_posts_obj = get_posts('posts_per_page=-1');
2020-03-15 14:29:06 +01:00
foreach ($gdk_options_posts_obj as $gdk_posts) {
$gdk_options_posts[$gdk_posts->ID] = $gdk_posts->post_title;
2020-02-16 12:38:04 +01:00
}
2021-10-10 15:48:53 +02:00
2020-02-16 12:38:04 +01:00
return $gdk_options_posts;
}
2020-03-15 14:29:06 +01:00
function gdk_categories_dropdown()
{
2021-10-10 15:48:53 +02:00
// Option list of all categories
$args = [
'type' => 'post',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
2020-03-15 14:29:06 +01:00
'hierarchical' => 1,
2021-10-10 15:48:53 +02:00
'taxonomy' => 'category',
];
$gdk_option_categories = [];
$gdk_category_lists = get_categories($args);
2020-03-15 14:29:06 +01:00
foreach ($gdk_category_lists as $gdk_category) {
2020-02-16 12:38:04 +01:00
$gdk_option_categories[$gdk_category->term_id] = $gdk_category->name;
}
2021-10-10 15:48:53 +02:00
2020-02-16 12:38:04 +01:00
return $gdk_option_categories;
}
2020-03-15 14:29:06 +01:00
function gdk_tag_dropdown()
{
2021-10-10 15:48:53 +02:00
// Option list of all tags
$args = [
'type' => 'post',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
2020-03-15 14:29:06 +01:00
'hierarchical' => 1,
2021-10-10 15:48:53 +02:00
'taxonomy' => 'tag',
];
$gdk_option_tags = [];
$gdk_tag_lists = get_tags($args);
2020-02-16 12:38:04 +01:00
$gdk_option_tags[''] = '选择标签';
2020-03-15 14:29:06 +01:00
foreach ($gdk_tag_lists as $gdk_tag) {
2020-02-16 12:38:04 +01:00
$gdk_option_tags[$gdk_tag->term_id] = $gdk_tag->name;
}
2021-10-10 15:48:53 +02:00
2020-02-16 12:38:04 +01:00
return $gdk_option_tag;
}
2020-02-26 04:31:45 +01:00
//转化图片为base64格式
2021-10-10 15:48:53 +02:00
function gdk_base64img($image_file)
2020-03-15 14:29:06 +01:00
{
2021-10-10 15:48:53 +02:00
$image_info = getimagesize($image_file);
$image_data = file_get_contents($image_file);
return 'data:'.$image_info['mime'].';base64,'.base64_encode($image_data);
}
//生成二维码
2021-10-10 15:48:53 +02:00
function gdk_getQrcode($url)
{
//引入phpqrcode类库
2021-10-10 15:48:53 +02:00
require_once GDK_ROOT_PATH.'/class/qrcode.class.php';
$errorCorrectionLevel = 'L'; //容错级别
2021-10-10 15:48:53 +02:00
$matrixPointSize = 6; //生成图片大小
ob_start();
QRcode::png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
$data = ob_get_contents();
ob_end_clean();
$imageString = base64_encode($data);
2021-10-10 15:48:53 +02:00
header('content-type:application/json; charset=utf-8');
return 'data:image/jpeg;base64,'.$imageString;
}
2020-03-21 16:43:54 +01:00
2021-10-10 15:48:53 +02:00
function gdk_unzip_url($url, $where)
2020-03-21 16:43:54 +01:00
{
2021-10-10 15:48:53 +02:00
$zippath = $where.'/'.(basename($url));
2021-05-04 08:04:38 +02:00
$curl = curl_init();
2021-10-10 15:48:53 +02:00
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 300);
$downfile = fopen($zippath, 'wb');
curl_setopt($curl, CURLOPT_FILE, $downfile);
2021-05-04 08:04:38 +02:00
curl_exec($curl);
curl_close($curl);
2021-10-10 15:48:53 +02:00
require_once ABSPATH.'wp-admin/includes/file.php';
2020-03-21 16:43:54 +01:00
\WP_Filesystem();
\unzip_file($zippath, $where);
usleep(300000);
2021-10-10 15:48:53 +02:00
unlink($zippath);
2020-03-21 16:43:54 +01:00
}
2020-03-21 17:33:39 +01:00
//新窗口打开
2021-10-10 15:48:53 +02:00
function gdk_blank_open()
2020-03-21 17:33:39 +01:00
{
if (!gdk_option('gdk_target_blank')) {
echo 'target="_blank"';
}
2020-03-26 15:24:00 +01:00
}
2020-04-06 04:24:38 +02:00
function gdk_guest_form()
{
?>
2020-04-06 13:13:00 +02:00
<p><textarea id="msg_content" placeholder="请输入留言内容" name="" rows="4" cols="" required></textarea></p>
2021-10-10 15:48:53 +02:00
<p><input type="email" id="msg_mail" placeholder="请输入邮箱" style="margin-right:30px;" required /><input id="msg_submit"
data-action="msg_submit" type="button" value="提交"></p>
2020-04-06 04:24:38 +02:00
<?php
2021-10-10 15:48:53 +02:00
}