<?php
if (!defined('ABSPATH')) {
die;
} // Cannot access directly.
//**切换暗黑风格
function tap_dark()
{
$is_ripro_dark = !empty($_POST['is_ripro_dark']) ? intval($_POST['is_ripro_dark']) : 0;
$_SESSION['is_ripro_dark'] = $is_ripro_dark;
echo $_SESSION['is_ripro_dark'];
exit();
}
add_action('wp_ajax_tap_dark', 'tap_dark');
add_action('wp_ajax_nopriv_tap_dark', 'tap_dark');
//**切换博客模式
function blog_style()
{
$is_blog_style = ($_POST['is_blog_style'] == '0') ? 1 : 0;
$_SESSION['is_blog_style'] = $is_blog_style;
echo $_SESSION['is_blog_style'];
exit();
}
add_action('wp_ajax_blog_style', 'blog_style');
add_action('wp_ajax_nopriv_blog_style', 'blog_style');
function get_bigger_img()
{
//新版本海报抛弃传统的php后端生产模式,使用js前段绘图,不占用服务器资源 性能出众 参考灵感来自wpcom.cn和b2的html2canvasjs技术
header('Content-type:application/html; Charset=utf-8');
global $current_user;
$post_id = !empty($_POST['id']) ? (int)$_POST['id'] : 0;
$post = get_post($post_id);
if ($current_user->ID>0) {
// 生出带参数的推广文章链接
$afflink = add_query_arg(array('ref' => $current_user->ID), get_the_permalink($post_id));
} else {
$afflink = get_the_permalink($post_id);
}
if (!$post) {
exit('参数错误');
}
$img_u = _get_post_thumbnail_url($post_id);
$img_t = get_template_directory_uri() . '/timthumb.php?src=' . $img_u . '&h=300&w=400&zc=1&a=c&q=80&s=1';
// $imageInfo = getimagesize($img_t);
// $b64 = base64_encode(file_get_contents($img_t));
// switch ($imageInfo[2]) { //判读图片类型
// case 1: $img_type = "gif";
// break;
// case 2: $img_type = "jpg";
// break;
// case 3: $img_type = "png";
// break;
// }
// $img_base64 = 'data:image/' . $img_type . ';base64,' . $b64;
echo '<div id="poster-html" class="poster-html">';
echo '<div class="poster-header">';
echo '<img src="'.$img_t.'">';
echo '<h2 class="poster-title">'.get_the_title($post_id).'</h2>';
echo '</div>';
echo '<div class="poster-body">';
echo '<div class="poster-meta">';
echo '<div class="poster-author">'.get_avatar($post->post_author).get_the_author_meta('display_name', $post->post_author).'</div>';
echo '<div class="poster-data">'.$post->post_date.'</div>';
echo '</div>';
echo '<div class="poster-text">'.wp_trim_words(strip_shortcodes($post->post_content), 120, '...').'</div>';
echo '</div>';
echo '<div class="poster-footer">';
echo '<div class="poster-logo">';
echo '<img src="'._cao('poster_logo').'">';
echo '<p>'._cao('poster_desc').'</p>';
echo '</div>';
echo '<div class="poster-qrcode">';
echo '<img src="'.getQrcode($afflink).'">';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="poster-canvas"></div>';
echo '<a class="poster-down btn" href="" download="'.get_the_title($post_id).'.png"><i class="fa fa-spinner fa-spin"></i> '.esc_html__('海报生成中', 'rizhuti-v2').'</a>';
exit;
}
add_action('wp_ajax_nopriv_get_bigger_img', 'get_bigger_img');
add_action('wp_ajax_get_bigger_img', 'get_bigger_img');
/**
* [ajax_getcat_post 分类文章获取]
* @Author Dadong2g
* @DateTime 2020-03-05T19:56:17+0800
* @return [type] [html str]
*/
function ajax_getcat_post()
{
global $wp_query;
header('Content-type:application/html; Charset=utf-8');
// $paged = !empty($_POST['paged']) ? esc_sql($_POST['paged']) : 1;
$cat = !empty($_POST['cat']) ? (int)$_POST['cat'] : '';
/////////////
$is_cao_site_list_blog = is_cao_site_list_blog();
if ($is_cao_site_list_blog) {
$latest_layout = 'bloglist';
} else {
$latest_layout = _cao('latest_layout', 'grid');
}
/////////////
$args = array(
'cat' => $cat,
'post_status' => 'publish',
);
$data = new WP_Query($args);
if ($data->have_posts()) {
while ($data->have_posts()) : $data->the_post();
get_template_part('parts/template-parts/content', $latest_layout);
endwhile;
} else {
get_template_part('parts/template-parts/content', 'none');
}
wp_reset_postdata();
exit();
}
add_action('wp_ajax_ajax_getcat_post', 'ajax_getcat_post');
add_action('wp_ajax_nopriv_ajax_getcat_post', 'ajax_getcat_post');
/**
* [ajax_search AJAX搜索]
* @Author Dadong2g
* @DateTime 2019-08-21T23:35:34+0800
* @return [type] [JSON Arr]
*/
function ajax_search()
{
global $wp_query;
header('Content-type:application/json; Charset=utf-8');
$text = !empty($_POST['text']) ? esc_sql($_POST['text']) : null;
$args = array('s' => $text,'posts_per_page' => 5);
$array_posts = array();
$data = new WP_Query($args);
while ($data->have_posts()) : $data->the_post();
array_push($array_posts, array("title"=>get_the_title(),"url"=>get_permalink(),"img"=>_get_post_timthumb_src() ));
endwhile;
echo json_encode($array_posts);
exit();
}
add_action('wp_ajax_ajax_search', 'ajax_search');
add_action('wp_ajax_nopriv_ajax_search', 'ajax_search');
function get_mpweixin_qr()
{
header('Content-type:application/json; Charset=utf-8');
$wxConfig = _cao('oauth_mpweixin');
$CaoMpWeixin = new CaoMpWeixin($wxConfig);
echo json_encode($CaoMpWeixin->getLoginQr());
exit;
}
add_action('wp_ajax_get_mpweixin_qr', 'get_mpweixin_qr');
add_action('wp_ajax_nopriv_get_mpweixin_qr', 'get_mpweixin_qr');
function check_mpweixin_qr()
{
header('Content-type:application/json; Charset=utf-8');
$scene_id = !empty($_POST['scene_id']) ? sanitize_text_field(wp_unslash($_POST[ 'scene_id' ])) : null;
global $current_user;
$current_user_id =$current_user->ID;
// 查询数据库
global $wpdb, $mpwx_log_table_name;
$res = $wpdb->get_row($wpdb->prepare("SELECT * FROM $mpwx_log_table_name WHERE scene_id = %s ", esc_sql($scene_id)));
if (($res->create_time+180)<time()) {
echo json_encode(array('status' => 0));
exit;
//180秒内有效
}
// 查询openid
$_prefix = 'mpweixin';
$_openid_meta_key = 'open_' . $_prefix . '_openid';
$user_exist = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key=%s AND meta_value=%s", $_openid_meta_key, $res->openid));
if (!$current_user_id && !empty($res) && $res->scene_id == $scene_id && !empty($res->openid)) {
if (!empty($user_exist)) {
wp_set_auth_cookie($user_exist, true, false);
$user = get_user_by('id', $user_exist);
do_action('wp_login', $user->user_login, $user); // 保证挂载的action执行
}
$status = 1;
} else {
$status = 0;
}
echo json_encode(array('status' => $status));
exit;
}
add_action('wp_ajax_check_mpweixin_qr', 'check_mpweixin_qr');
add_action('wp_ajax_nopriv_check_mpweixin_qr', 'check_mpweixin_qr');
/**
* [user_login 用户登录]
* @Author Dadong2g
* @DateTime 2019-06-02T15:34:38+0800
* @return [type] [description]
*/
function user_login()
{
header('Content-type:application/json; Charset=utf-8');
$username = !empty($_POST['username']) ? esc_sql($_POST['username']) : null;
$password = !empty($_POST['password']) ? esc_sql($_POST['password']) : null;
$rememberme = !empty($_POST['rememberme']) ? esc_sql($_POST['rememberme']) : null;
if (_cao('is_close_wplogin')) {
echo json_encode(array('status' => '0', 'msg' => '仅开放社交账号登录'));
exit;
}
$login_data = array();
$login_data['user_login'] = $username;
$login_data['user_password'] = $password;
$login_data['remember'] = false;
if (isset($rememberme) && $rememberme == '1') {