<?php
/*
Plugin Name: se-frame-activity
Plugin URI: Http://shopeo.cn
Description: Smoant
Author: SHOPEO
Version: 1.0
Author URI: http://www.cnblogs.com/fxmbz
*/
/*****************创建新的文章类型 */
//注册自定义文章类型-activity
/**
* Register a custom post type called "activity".
*
* @see get_post_type_labels() for label keys.
*/
add_action('init', 'my_custom_post_activity');
function my_custom_post_activity()
{
$labels = array(
'name' => _x('Activity', 'post type 名称'),
'singular_name' => _x('Activity', 'post type 单个 item 时的名称,因为英文有复数'),
'add_new' => _x('新建Activity', '添加新内容的链接名称'),
'add_new_item' => __('新建Activity'),
'edit_item' => __('编辑Activity'),
'new_item' => __('新Activity'),
'all_items' => __('所有Activity'),
'view_item' => __('查看Activity'),
'search_items' => __('搜索Activity'),
'not_found' => __('没有找到有关Activity'),
'not_found_in_trash' => __('回收站里面没有相关Activity'),
'parent_item_colon' => '',
'menu_name' => 'Activity'
);
$args = array(
'labels' => $labels,
'description' => '我们网站的Activity信息',
'public' => true,
'menu_position' => 5,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
'has_archive' => true
);
register_post_type('activity', $args);
}
//注册自定义文章类型-添加分类
function my_taxonomies_activity()
{
$labels = array(
'name' => _x('Activity分类', 'taxonomy 名称'),
'singular_name' => _x('Activity分类', 'taxonomy 单数名称'),
'search_items' => __('搜索Activity分类'),
'all_items' => __('所有Activity分类'),
'parent_item' => __('该Activity分类的上级分类'),
'parent_item_colon' => __('该Activity分类的上级分类:'),
'edit_item' => __('编辑Activity分类'),
'update_item' => __('更新Activity分类'),
'add_new_item' => __('添加新的Activity分类'),
'new_item_name' => __('新Activity分类'),
'menu_name' => __('Activity分类'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true
);
register_taxonomy('activity_category', 'activity', $args);
}
add_action('init', 'my_taxonomies_activity', 0);
//注册自定义文章类型-joinactivity
/**
* Register a custom post type called "joinactivity".
*
* @see get_post_type_labels() for label keys.
*/
add_action('init', 'my_custom_post_joinactivity');
function my_custom_post_joinactivity()
{
$labels = array(
'name' => _x('JoinActivity', 'post type 名称'),
'singular_name' => _x('JoinActivity', 'post type 单个 item 时的名称,因为英文有复数'),
'add_new' => _x('新建JoinActivity', '添加新内容的链接名称'),
'add_new_item' => __('新建JoinActivity'),
'edit_item' => __('编辑JoinActivity'),
'new_item' => __('新JoinActivity'),
'all_items' => __('所有JoinActivity'),
'view_item' => __('查看JoinActivity'),
'search_items' => __('搜索JoinActivity'),
'not_found' => __('没有找到有关JoinActivity'),
'not_found_in_trash' => __('回收站里面没有相关JoinActivity'),
'parent_item_colon' => '',
'menu_name' => 'JoinActivity'
);
$args = array(
'labels' => $labels,
'description' => '我们网站的JoinActivity信息',
'public' => true,
'menu_position' => 5,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
'has_archive' => true
);
register_post_type('joinactivity', $args);
}
//注册自定义文章类型-添加分类
function my_taxonomies_joinactivity()
{
$labels = array(
'name' => _x('JoinActivity分类', 'taxonomy 名称'),
'singular_name' => _x('JoinActivity分类', 'taxonomy 单数名称'),
'search_items' => __('搜索JoinActivity分类'),
'all_items' => __('所有JoinActivity分类'),
'parent_item' => __('该JoinActivity分类的上级分类'),
'parent_item_colon' => __('该JoinActivity分类的上级分类:'),
'edit_item' => __('编辑JoinActivity分类'),
'update_item' => __('更新JoinActivity分类'),
'add_new_item' => __('添加新的JoinActivity分类'),
'new_item_name' => __('新JoinActivity分类'),
'menu_name' => __('JoinActivity分类'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true
);
register_taxonomy('joinactivity_category', 'joinactivity', $args);
}
add_action('init', 'my_taxonomies_joinactivity', 0);
/**
* 关联活动
*/
$new_meta_boxes = array(
'activity' => array(
"name" => "_activity",
"std" => "关联活动",
"title" => "关联活动"
)
);
/**
* 添加元数据
*/
function new_meta_boxes()
{
global $post, $new_meta_boxes;
//获取自定义分类文章
$args = array(
'post_type' => 'activity'
);
$postslist = get_posts($args);
foreach ($new_meta_boxes as $mate_box) {
$meta_box_value = get_post_meta($post->ID, $mate_box['name'], true);
if ($mate_box['name'] == '_activity') {
$strs = '<select name="' . $mate_box['name'] . '"><option value="">请选择</option>';
foreach ($postslist as $postess) {
$gr_seleted = "";
if ($postess->ID == (int) $meta_box_value) {
$gr_seleted = "selected";
}
$strs .= '<option value="' . $postess->ID . '" ' . $gr_seleted . '>' . $postess->post_title . '</option>';
}
$strs .= '</select>';
// // 自定义字段标题
echo '<h3>' . $mate_box['title'] . '</h3>';
echo $strs;
}
}
echo '<input type="hidden" name="ludou_metaboxes_nonce" id="ludou_metaboxes_nonce" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
}
/**
* 创建自定义字段模块
*/
function create_meta_box()
{
if (function_exists('add_meta_box')) {
add_meta_box('new_meta_boxes', '自定义模块', 'new_meta_boxes', 'joinactivity', 'normal', 'high');
}
}
// //将函数连接到指定action(动作)
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
//保存文章数据
function save_postdata($post_id)
{
global $new_meta_boxes;
if (!wp_verify_nonce($_POST['ludou_metaboxes_nonce'], plugin_basename(__FILE__)))
return;
if (!current_user_can('edit_posts', $post_id))
return;
foreach ($new_meta_boxes as $meta_box) {
$data = $_POST[$meta_box['name'] . '_value'];
if ($data == "")
delete_post_meta($post_id, $meta_box['name'] . '_value', get_post_meta($post_id, $meta_box['name'] . '_value', true));
else
update_post_meta($post_id, $meta_box['name'] . '_value', $data);
}
}
/**
* 关联活动
*/
$new_meta_boxes1 = array(
'starttimes' => array(
"name" => "starttime",
"std" => "开始时间",
"title" => "开始时间"
),
'endtimes' => array(
"name" => "endtime",
"std" => "结束时间",
"title" => "结束时间"
),
'noices' => array(
"name" => "noice",
"std" => "活动提示",
"title" => "活动提示"
)
);
/**
* 添加元数据
*/
function new_meta_boxes1()
{
global $post, $new_meta_boxes1;
foreach ($new_meta_boxes1 as $mate_box) {
$meta_box_value1 = get_post_meta($post->ID, $mate_box['name'], true);
if($mate_box['name'] == 'starttime'){
// 自定义字段标题
echo '<h3>' . $mate_box['title'] . '</h3>';
// 自定义字段输入框
echo '<input name="' . $mate_box['name'] .'" value="' . $meta_box_value1 . '" type="datetime-local" >';
}else if($mate_box['name'] == 'endtime'){
// 自定义字段标题
echo '<h3>' . $mate_box['title'] . '</h3>';
// 自定义字段输入框
echo '<input name="' . $mate_box['name'] .'" value="' . $meta_box_value1 . '" type="datetime-local" >';
}else if($mate_box['name'] == 'noice'){
// 自定义字段标题
echo '<h3>' . $mate_box['title'] . '</h3>';
// 自定义字段输入框
echo '<input name="' . $mate_box['name'] . '" value="' . $meta_box_value1 . '" type="number" >';
}
}
echo '<input type="hidden" name="ludou_metaboxes_nonce" id="ludou_metaboxes_nonce" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
}
/**
* 创建自定义字段模块
*/
function create_meta_box1()
{
if (function_exists('add_meta_box')) {
add_meta_box('new_meta_boxes', '自定义模块', 'new_meta_boxes1', 'activity', 'normal', 'high');
}
}
// //将函数连接到指定action(动作)
add_action('admin_menu', 'create_meta_box1');
add_action('save_post', 'save_postdata1');
//保存文章数据
function save_postdata1($post_id)
{
global $new_meta_boxes1;
if (!wp_verify_nonce($_POST['ludou_metaboxes_nonce'], plugin_basename(__FILE__)))
return;
if (!current_user_can('edit_posts', $post_id))
return;
foreach ($new_meta_boxes1 as $meta_box) {
$data = $_POST[$meta_box['name']];
if ($data == "")
delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
else
update_post_meta($post_id, $meta_box['name'], $data);
}
}
function load_assets($hook)
{
$ver = '0.0.1';
wp_enqueue_style('vite', plugin_dir_url(__FILE__) . 'dist/assets/index.css', array(), $ver, false);
wp_enqueue_script('vite', plugin_dir_url(__FILE__) . 'dist/assets/wp-vite-main.js', array(), $ver, true);
}
add_action('admin_enqueue_scripts', 'load_assets');
add_action('wp_enqueue_scripts', 'load_assets');
function to_script_module($tag, $handle)
{
if (strpos($tag, 'vite-main.js') !== false) {
$tag = str_replace('<script', '<script type="module"', $tag);
}
return $tag;
}
add_filter('script_loader_tag', 'to_script_module', 10, 2);
/** 会员登录页面短码 */
add_shortcode('MenberPages', 'my_shortcode_menbers');
function my_shortcode_menbers($attr)
{
ob_start();
$UserInfo = wp_get_current_user();
$id = $attr['id'];
//获取该用户活动图片start
$userid = get_current_user_id();
$arg = array(
'post_type' => 'joinactivity',
'post_author' => $userid,
'post_status' => array('publish'),
'meta_query' => array(
array(
'key' => '_activity',
'value' => $id,
)
)
);
$arrList = new WP_Query($arg);
$u = $arrList->posts;
$imageurl = wp_get_attachment_url(get_post_thumbnail_id($u[0]->ID));
//获取该用户活动图片end
$datas = get_post($id);
$title = $datas->post_title;
$text = $datas->post_content;
$starttime = get_post_meta($id,'starttime')[0];
$endtime = get_post_meta($id,'endtime')[0];
$noice = get_post_meta($id,'noice')[0];
$notime = date("y-m-d h:i:s");
$status = '';
$share_link = get_permalink($u[0]->ID);
if(strtotime($starttime) > strtotime($notime)){
$status=1;//还没开始
}else if(strtotime($notime)>strtotime($starttime) && strtotime($notime)< strtotime($endtime)){
$status=2;//开始中
}else if(strtotime($notime) > strtotime($endtime)){
$status=3;//已经结束
}
$url = wp_get_attachment_url(get_post_thumbnail_id($id));
if (is_user_logged_in()) {
if($status == 1){
echo '<h1 style="text-align: center;padding:50px 0;">活动还没开始!敬请期待!</h1>';
}
if($status == 2){
?>
<script id="shopeo-frame-json" type="application/json">
{
"activity_id": "<?php echo $id; ?>",
"title":"<?php echo $title; ?>",
"frame_img": "<?php echo $url; ?>",
"activity_text": "<?php echo $text; ?>",
"user_id":"<?php echo get_current_user_id(); ?>",
"user_email":"<?php echo $UserInfo->user_email; ?>",
"user_name":"<?php echo $UserInfo->user_login; ?>",
"status":<?php echo $status;?>,
"current_img":"<?php echo $imageurl;?>",
"share_link":"<?php echo $share_link;?>",
"noice":"<?php echo $noice;?>"
}
</script>
<div class="shopeo-frame-app" id="shopeo-frame-app"></div>
<?php
}
if($status == 3){
echo '<h1 style="text-align: center;padding:50px 0;">活动已经结束,谢谢参与!</h1>';
}
} else {
?>
<div class="loginOut" style="text-align:center;margin:50px auto;">
<a href="/my-account/" style="padding:15px 40px; border:1px solid #333; background-color: #f1f1f1;">点击登录参与活动</a>
</div>
<?php
}
return ob_get_clean();
}
//获取列表
add_action("wp_ajax_vote_post_lists", "add_votes_list");
add_action("wp_ajax_nopriv_vote_post_lists", "add_votes_list");
//上传图片
add_action("wp_ajax_vote_upload_lists", "add_votes_opload_img");
add_action("wp_ajax_nopriv_vote_upload_lists", "add_votes_opload_img");
function add_votes_list()
{
$userid = get_current_user_id();
$activeid = $_POST['activity_id'];
$arg = array(
'post_type' => 'joinactivity',
'post_author' => $userid,
'post_status' => array('publish'),
'meta_query' => array(
array(
'key' => '_activity',
'value' => $activeid,
)
)
);
$arrList = new WP_Query($arg);
wp_send_json($arrList->posts);
}
function add_votes_opload_img()
{
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
$id = $_POST['activity_id'];
//获取该用户活动图片start
$userid = get_current_user_id();
$datas = get_post($id);
$title = $datas->post_title;
$content = $datas->post_content;
$arg = array(
'post_type' => 'joinactivity',
'author' => $userid,
'post_status' => array('publish'),
'meta_query' => array(
array(
'key' => '_activity',
'value' => $id,
)
)
);
$arrList = new WP_Query($arg);
$p= $arrList->posts;
// print_r("<pre>");
// print_r($p);
if(count($p) == 1){
$url_pre = get_post_thumbnail_id($p[0]->ID);
wp_delete_attachment( $url_pre, true );
delete_post_thumbnail($p[0]->ID);
$attachment_id = media_handle_upload('file', $p[0]->ID);
set_post_thumbnail($p[0]->ID, $attachment_id);
update_post_meta($p[0]->ID, '_activity', $id);
$my_image_meta = array(
'ID' => $attachment_id, // Specify the image (ID) to be updated
'post_title' => $title, // Set image Title to sanitized title
'post_excerpt' => $title, // Set image Caption (Excerpt) to sanitized title
'post_content' => $content, // Set image Description (Content) to sanitized title
);
wp_update_post( $my_image_meta );
$url = wp_get_attachment_url($attachment_id);
$permalink = get_permalink($p[0]->ID);
wp_send_json(array("share_link" => $permalink, "download_link" => $url));
}else if(count($p) == 0){
$my_post = array(
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'joinactivity',
'post_title' => $title,
'post_content' => $content
);
$post_id = wp_insert_post($my_post);
$permalink = get_permalink($post_id);
$attachment_id = media_handle_upload('file', $post_id);
set_post_thumbnail($post_id, $attachment_id);
update_post_meta($post_id, '_activity', $id);
$url = wp_get_attachment_url(get_post_thumbnail_id($post_id));
// $attachment_id = media_handle_upload('file',0);
// $url = wp_get_attachment_url($attachment_id);
//更改图片的属性
$my_image_meta = array(
'ID' => $attachment_id, // Specify the image (ID) to be updated
'post_title' => $title, // Set image Title to sanitized title
'post_excerpt' => $title, // Set image Caption (Excerpt) to sanitized title
'post_content' => $content, // Set image Description (Content) to sanitized title
);
wp_update_post( $my_image_meta );
wp_send_json(array("share_link" => $permalink, "download_link" => $url));
}else{
wp_send_json(array("msg" => '数据出现错误!'));
}
//获取该用户活动图片end
// $datas = get_post($id);
// $title = $datas->post_title;
// $content = $datas->post_content;
// if (isset($_POST['action']) && $_POST['action'] == 'vote_upload_lists') {
// $my_post = array(
// 'post_status' => 'publish',
// 'post_author' => get_current_user_id(),
// 'post_type' => 'joinactivity',
// 'post_title' => $title,
// 'post_content' => $content
// );
// $post_id = wp_insert_post($my_post);
// $permalink = get_permalink($post_id);
// $attachment_id = media_handle_upload('file', $post_id);
// set_post_thumbnail($post_id, $attachment_id);
// update_post_meta($post_id, '_activity', $id);
// $url = wp_get_attachment_url(get_post_thumbnail_id($post_id));
// // $attachment_id = media_handle_upload('file',0);
// // $url = wp_get_attachment_url($attachment_id);
// //更改图片的属性
// $my_image_meta = array(
// 'ID' => $attachment_id, // Specify the image (ID) to be updated
// 'post_title' => $title, // Set image Title to sanitized title
// 'post_excerpt' => $title, // Set image Caption (Excerpt) to sanitized title
// 'post_content' => $content, // Set image Description (Content) to sanitized title
// );
// wp_update_post( $my_image_meta );
// wp_send_json(array("share_link" => $permalink, "download_link" => $url));
// } else {
// }
}
add_filter('manage_joinactivity_posts_columns', 'add_new_joinactivity_columns');
function add_new_joinactivity_columns($book_columns)
{
$new_columns['cb'] = '<input type="checkbox" />'; //这个是前面那个选框,不要丢了
$new_columns['id'] = __('ID');
$new_columns['title'] = '标题';
$new_columns['images'] = __('Images');
$new_columns['author'] = __('Author');
$new_columns['categories'] = __('Categories');
$new_columns['tags'] = __('Tags');
$new_columns['date'] = _x('Date', 'column name');
//直接返回一个新的数组
return $new_columns;
}
add_action('manage_posts_custom_column', 'manage_joinactivity_columns', 10, 2);
function manage_joinactivity_columns($column_name, $id)
{
global $wpdb;
switch ($column_name) {
case 'id':
echo $id;
break;
case 'images':
//$num_images = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = {$id};"));
$num_images = wp_get_attachment_url(get_post_thumbnail_id($id));
echo '<a href="'.$num_images.'"><img src="' . wp_get_attachment_url(get_post_thumbnail_id($id)) . '"></a>';
break;
default:
break;
}
}
add_filter('manage_activity_posts_columns', 'add_new_activity_columns');
function add_new_activity_columns($book_columns)
{
$new_columns['cb'] = '<input type="checkbox" />'; //这个是前面那个选框,不要丢了
$new_columns['id'] = __('ID');
$new_columns['title'] = '标题';
$new_columns['shortCode'] = __('shortCode');
$new_columns['starttime'] = _x('starttime', 'column name');
$new_columns['endtime'] = _x('endtime', 'column name');
$new_columns['statu'] = _x('statu', 'column name');
$new_columns['date'] = _x('Date', 'column name');
//直接返回一个新的数组
return $new_columns;
}
add_action('manage_posts_custom_column', 'manage_activity_columns', 10, 2);
function manage_activity_columns($column_name, $id)
{
global $wpdb;
switch ($column_name) {
case 'shortCode':
$shortCode = "[MenberPages id='$id']";
echo $shortCode;
break;
case 'starttime':
$starttime = get_post_meta($id,'starttime');
echo $starttime[0];
break;
case 'endtime':
$endtime = get_post_meta($id,'endtime');
echo $endtime[0];
break;
case 'noice':
$noice = get_post_meta($id,'noice');
echo $noice[0];
break;
case 'statu':
$starttime = get_post_meta($id,'starttime')[0];
$endtime = get_post_meta($id,'endtime')[0];
$notime = date("y-m-d h:i:s");
$status = '';
if(strtotime($starttime) > strtotime($notime)){
$status="未开始";//还没开始
}else if(strtotime($notime)>strtotime($starttime) && strtotime($notime)< strtotime($endtime)){
$status="进行中";//开始中
}else if(strtotime($notime) > strtotime($endtime)){
$status="已结束";//已经结束
}
echo $status;
break;
default:
break;
}
}
/**
* Filter slugs
* @since 1.1.0
* @return void
*/
add_action('restrict_manage_posts','author_select');
function author_select($post_type){
if(post_type_supports($post_type,'author')){
wp_dropdown_users([
'name' => 'author',
'who' => 'authors',
'show_option_all' => '所有作者',
'hide_if_only_one_author' => true,
'selected' => $_REQUEST['author'] ?? 0
]);
}
}
?>