convert to plugins+theme

This commit is contained in:
OldTaoge
2026-07-21 09:55:39 +08:00
parent 803841cd12
commit aea6095c0a
57 changed files with 2742 additions and 1619 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,295 @@
<?php
/**
* 招新文章标记:ACF “招新文章标记” 字段组、is_recruitment_article 判定、
* 编辑页 meta box、以及“加入我们”页顶部招新新闻 feed。
* 抽离自主题 functions.php(lift-and-shift,逻辑未改)。
*/
if (!defined('ABSPATH')) {
exit;
}
function itstudio_register_acf_fields() {
if (!function_exists('acf_add_local_field_group')) {
return;
}
acf_add_local_field_group(array(
'key' => 'group_itstudio_recruitment_article',
'title' => '招新文章标记',
'fields' => array(
array(
'key' => 'field_itstudio_is_recruitment_article',
'label' => '是否为招新文章',
'name' => 'itstudio_is_recruitment_article',
'type' => 'true_false',
'instructions' => '勾选后,该文章会在“加入我们”页面顶部新闻条中显示(按发布时间排序)。',
'required' => 0,
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '是',
'ui_off_text' => '否',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'announcement',
),
),
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'news',
),
),
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
'position' => 'side',
'style' => 'default',
'active' => true,
'show_in_rest' => 1,
));
}
add_action('acf/init', 'itstudio_register_acf_fields');
function itstudio_get_recruitment_article_meta_key() {
return 'itstudio_is_recruitment_article';
}
function itstudio_normalize_recruitment_flag($value) {
if (is_bool($value)) {
return $value;
}
if (is_numeric($value)) {
return ((int) $value) > 0;
}
$value = strtolower(trim((string) $value));
return in_array($value, array('1', 'true', 'yes', 'on', 'y'), true);
}
function itstudio_is_recruitment_article($post_id) {
$post_id = (int) $post_id;
if ($post_id <= 0) {
return false;
}
$meta_key = itstudio_get_recruitment_article_meta_key();
$meta_keys = array(
$meta_key,
'_itstudio_is_recruitment_article',
'itstudio_join_article',
'join_recruitment_article',
'is_recruitment_article',
);
foreach ($meta_keys as $key) {
$raw = get_post_meta($post_id, $key, true);
if ($raw !== '' && $raw !== null && itstudio_normalize_recruitment_flag($raw)) {
return true;
}
}
if (function_exists('get_field')) {
$acf_keys = array(
'itstudio_is_recruitment_article',
'is_recruitment_article',
'itstudio_join_article',
);
foreach ($acf_keys as $acf_key) {
$raw = get_field($acf_key, $post_id, false);
if ($raw !== '' && $raw !== null && itstudio_normalize_recruitment_flag($raw)) {
return true;
}
}
}
return false;
}
function itstudio_add_recruitment_meta_boxes() {
$screens = array('announcement', 'news', 'post');
foreach ($screens as $screen) {
add_meta_box(
'itstudio_recruitment_flag',
__('招新文章', 'itstudio'),
'itstudio_render_recruitment_meta_box',
$screen,
'side',
'high'
);
}
}
add_action('add_meta_boxes', 'itstudio_add_recruitment_meta_boxes');
function itstudio_render_recruitment_meta_box($post) {
$meta_key = itstudio_get_recruitment_article_meta_key();
$checked = itstudio_is_recruitment_article((int) $post->ID);
wp_nonce_field('itstudio_save_recruitment_meta', 'itstudio_recruitment_meta_nonce');
?>
<p>
<label for="itstudio_is_recruitment_article">
<input
type="checkbox"
id="itstudio_is_recruitment_article"
name="itstudio_is_recruitment_article"
value="1"
<?php checked($checked); ?>
>
<?php esc_html_e('在加入我们页面显示为招新新闻', 'itstudio'); ?>
</label>
</p>
<p style="color:#666;line-height:1.5;margin:0;">
<?php esc_html_e('仅显示当年且已发布的招新文章,最多展示 5 篇。', 'itstudio'); ?>
</p>
<input type="hidden" name="itstudio_recruitment_meta_key" value="<?php echo esc_attr($meta_key); ?>">
<?php
}
function itstudio_save_recruitment_meta_box($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) {
return;
}
if (!isset($_POST['itstudio_recruitment_meta_nonce'])) {
return;
}
$nonce = sanitize_text_field(wp_unslash($_POST['itstudio_recruitment_meta_nonce']));
if (!wp_verify_nonce($nonce, 'itstudio_save_recruitment_meta')) {
return;
}
$post_type = get_post_type($post_id);
if (!in_array($post_type, array('announcement', 'news', 'post'), true)) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
$meta_key = itstudio_get_recruitment_article_meta_key();
$checked = isset($_POST['itstudio_is_recruitment_article'])
? sanitize_text_field(wp_unslash($_POST['itstudio_is_recruitment_article']))
: '';
if (itstudio_normalize_recruitment_flag($checked)) {
update_post_meta($post_id, $meta_key, '1');
} else {
delete_post_meta($post_id, $meta_key);
}
}
add_action('save_post', 'itstudio_save_recruitment_meta_box');
function itstudio_join_get_recruitment_feed_items($join_runtime = array(), $limit = 5) {
$limit = max(1, min(10, (int) $limit));
$join_runtime = is_array($join_runtime) ? $join_runtime : array();
$now = new DateTimeImmutable('now', wp_timezone());
$now_year = (int) $now->format('Y');
$display_year = isset($join_runtime['recruitment_year']) && is_numeric($join_runtime['recruitment_year'])
? (int) $join_runtime['recruitment_year']
: $now_year;
$season_start_ts = null;
$season_end_ts = null;
$stages = isset($join_runtime['stages']) && is_array($join_runtime['stages']) ? $join_runtime['stages'] : array();
foreach ($stages as $stage) {
if (!is_array($stage)) {
continue;
}
$start_ts = isset($stage['start_ts']) && is_numeric($stage['start_ts']) ? (int) $stage['start_ts'] : null;
$end_ts = isset($stage['end_ts']) && is_numeric($stage['end_ts']) ? (int) $stage['end_ts'] : null;
if ($start_ts !== null && ($season_start_ts === null || $start_ts < $season_start_ts)) {
$season_start_ts = $start_ts;
}
if ($end_ts !== null && ($season_end_ts === null || $end_ts > $season_end_ts)) {
$season_end_ts = $end_ts;
}
}
$now_ts = (int) (isset($join_runtime['now_ts']) && is_numeric($join_runtime['now_ts']) ? $join_runtime['now_ts'] : ((int) $now->format('U') * 1000));
if ($season_start_ts !== null && $now_ts < $season_start_ts && $display_year > $now_year) {
// 下年招新未开始:继续展示本年招新资讯
$display_year = $now_year;
}
$query = new WP_Query(array(
'post_type' => array('announcement', 'news', 'post'),
'post_status' => 'publish',
'posts_per_page' => 80,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'date_query' => array(
array(
'year' => $display_year,
),
),
));
$items = array();
if ($query->have_posts()) {
foreach ($query->posts as $post) {
if (count($items) >= $limit) {
break;
}
$post_id = (int) $post->ID;
if (!itstudio_is_recruitment_article($post_id)) {
continue;
}
$title = trim((string) get_the_title($post_id));
$url = (string) get_permalink($post_id);
if ($title === '' || $url === '') {
continue;
}
$excerpt = function_exists('itstudio_get_post_excerpt_chars')
? itstudio_get_post_excerpt_chars($post_id, 72)
: wp_html_excerpt(trim(wp_strip_all_tags((string) get_post_field('post_excerpt', $post_id))), 72, '...');
if ($excerpt === '') {
$excerpt = '...';
}
$author_id = (int) get_post_field('post_author', $post_id);
$author = trim((string) get_the_author_meta('display_name', $author_id));
if ($author === '') {
$author = 'Unknown';
}
$items[] = array(
'id' => $post_id,
'title' => $title,
'excerpt' => $excerpt,
'url' => $url,
'date' => get_the_date('Y-m-d H:i', $post_id),
'date_iso' => get_the_date('c', $post_id),
'author' => $author,
'type' => get_post_type($post_id),
);
}
}
wp_reset_postdata();
return array(
'display_year' => $display_year,
'items' => $items,
);
}
@@ -0,0 +1,261 @@
<?php
/**
* 招新“学期”数据层:把原本单一的 itstudio_join_settings 配置扩展为可存档的多学期集合。
*
* 存储:
* option itstudio_join_terms —— array,键为学期 id,值为 { id, academic_year, season, label, created, settings }
* option itstudio_join_active_term_id —— string,当前生效学期的 id
* option itstudio_join_terms_migrated —— 迁移完成标记
*
* 兼容:旧的 itstudio_join_settings 选项保留,作为迁移来源与兜底。
* 迁移铁律沿用:各字段/选项标识不变,老数据不丢。
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* 可选的季节及其中文名。
*/
function itstudio_join_get_seasons() {
return array(
'fall' => '秋季',
'spring' => '春季',
);
}
/**
* 读取全部学期(id => term 的关联数组)。
*/
function itstudio_join_get_terms() {
$terms = get_option('itstudio_join_terms', array());
return is_array($terms) ? $terms : array();
}
/**
* 当前生效学期 id。
*/
function itstudio_join_get_active_term_id() {
$id = (string) get_option('itstudio_join_active_term_id', '');
if ($id !== '') {
$terms = itstudio_join_get_terms();
if (isset($terms[$id])) {
return $id;
}
}
// 生效指针失效时,回退到第一个存在的学期。
$terms = itstudio_join_get_terms();
if (!empty($terms)) {
return (string) array_key_first($terms);
}
return '';
}
/**
* 取单个学期结构(不存在返回 null)。
*/
function itstudio_join_get_term($term_id) {
$term_id = (string) $term_id;
$terms = itstudio_join_get_terms();
return isset($terms[$term_id]) ? $terms[$term_id] : null;
}
/**
* 学期的展示名:优先自定义 label,否则“学年 + 季节招新”。
*/
function itstudio_join_term_label($term) {
if (!is_array($term)) {
return '';
}
$custom = trim((string) ($term['label'] ?? ''));
if ($custom !== '') {
return $custom;
}
$seasons = itstudio_join_get_seasons();
$season_cn = $seasons[$term['season'] ?? ''] ?? '';
$year = trim((string) ($term['academic_year'] ?? ''));
$label = trim($year . ' ' . $season_cn . '招新');
return $label !== '招新' ? $label : '未命名招新期';
}
/**
* 由学年 + 季节生成稳定且唯一的学期 id(冲突时追加 -2、-3…)。
*/
function itstudio_join_make_term_id($academic_year, $season, array $existing = array()) {
$base = sanitize_title($academic_year . '-' . $season);
if ($base === '') {
$base = 'term';
}
$id = $base;
$suffix = 2;
while (isset($existing[$id])) {
$id = $base . '-' . $suffix;
$suffix++;
}
return $id;
}
/**
* 新建学期。$settings 省略时用默认空配置。返回新学期 id。
*/
function itstudio_join_create_term($academic_year, $season, $label = '', $settings = null) {
$terms = itstudio_join_get_terms();
$season = array_key_exists($season, itstudio_join_get_seasons()) ? $season : 'fall';
$academic_year = itstudio_join_sanitize_shortcode_value($academic_year);
$label = itstudio_join_sanitize_shortcode_value($label);
$id = itstudio_join_make_term_id($academic_year, $season, $terms);
$terms[$id] = array(
'id' => $id,
'academic_year' => $academic_year,
'season' => $season,
'label' => $label,
'created' => current_time('mysql'),
'settings' => is_array($settings)
? itstudio_join_sanitize_settings($settings)
: itstudio_join_get_default_settings(),
);
update_option('itstudio_join_terms', $terms);
// 若尚无“显式”生效学期(读原始选项,不走会兜底的 getter),新建的即为生效。
$active_raw = (string) get_option('itstudio_join_active_term_id', '');
if ($active_raw === '' || !isset($terms[$active_raw])) {
itstudio_join_set_active_term($id);
}
return $id;
}
/**
* 更新某学期的元信息(学年/季节/自定义名)。
*/
function itstudio_join_update_term_meta($term_id, $academic_year, $season, $label = '') {
$terms = itstudio_join_get_terms();
$term_id = (string) $term_id;
if (!isset($terms[$term_id])) {
return false;
}
$terms[$term_id]['academic_year'] = itstudio_join_sanitize_shortcode_value($academic_year);
$terms[$term_id]['season'] = array_key_exists($season, itstudio_join_get_seasons()) ? $season : 'fall';
$terms[$term_id]['label'] = itstudio_join_sanitize_shortcode_value($label);
update_option('itstudio_join_terms', $terms);
return true;
}
/**
* 把一份配置写入指定学期。
*/
function itstudio_join_save_term_settings($term_id, $raw_settings) {
$terms = itstudio_join_get_terms();
$term_id = (string) $term_id;
if (!isset($terms[$term_id])) {
return false;
}
$terms[$term_id]['settings'] = itstudio_join_sanitize_settings($raw_settings);
update_option('itstudio_join_terms', $terms);
return true;
}
/**
* 设为当前生效学期。
*/
function itstudio_join_set_active_term($term_id) {
$term_id = (string) $term_id;
$terms = itstudio_join_get_terms();
if (!isset($terms[$term_id])) {
return false;
}
update_option('itstudio_join_active_term_id', $term_id);
return true;
}
/**
* 删除学期。不允许删“当前生效”或最后一个。
*/
function itstudio_join_delete_term($term_id) {
$term_id = (string) $term_id;
$terms = itstudio_join_get_terms();
if (!isset($terms[$term_id])) {
return false;
}
if (count($terms) <= 1) {
return false;
}
if (itstudio_join_get_active_term_id() === $term_id) {
return false;
}
unset($terms[$term_id]);
update_option('itstudio_join_terms', $terms);
return true;
}
/**
* 由旧的单一配置推断学年与季节(报名开始月份 >= 7 视为秋季)。
*/
function itstudio_join_guess_term_from_settings($settings) {
$settings = is_array($settings) ? $settings : array();
$anchor = '';
foreach (array('registration_start', 'notice_start_date', 'first_interview_date') as $key) {
if (!empty($settings[$key])) {
$anchor = (string) $settings[$key];
break;
}
}
if ($anchor === '' || !preg_match('/(\d{4})-(\d{2})/', $anchor, $m)) {
return array('academic_year' => '', 'season' => 'fall');
}
$year = (int) $m[1];
$month = (int) $m[2];
if ($month >= 7) {
return array('academic_year' => $year . '-' . ($year + 1), 'season' => 'fall');
}
return array('academic_year' => ($year - 1) . '-' . $year, 'season' => 'spring');
}
/**
* 一次性迁移:把旧的单一 itstudio_join_settings 归入首个学期并设为生效。
* 前后台每个请求都会触发,但有标记,已迁移则立即返回。
*/
function itstudio_join_maybe_migrate_terms() {
if (get_option('itstudio_join_terms_migrated')) {
return;
}
$terms = itstudio_join_get_terms();
if (empty($terms)) {
$legacy = get_option('itstudio_join_settings', array());
$legacy = is_array($legacy) ? $legacy : array();
if (!empty($legacy)) {
$guess = itstudio_join_guess_term_from_settings($legacy);
$label = $guess['academic_year'] === '' ? '初始招新期' : '';
itstudio_join_create_term($guess['academic_year'], $guess['season'], $label, $legacy);
} else {
// 全新安装:建一个空的当前学期占位。
$now_year = (int) current_time('Y');
itstudio_join_create_term($now_year . '-' . ($now_year + 1), 'fall', '', null);
}
}
update_option('itstudio_join_terms_migrated', 1);
}
add_action('init', 'itstudio_join_maybe_migrate_terms', 1);
@@ -0,0 +1,36 @@
<?php
/**
* Plugin Name: ITStudio 招新系统
* Plugin URI: https://git.itouc.cn/ITStudio_OUC/ITStudioMainSite
* Description: ITStudio “加入我们”招新系统:招新文章标记、招新阶段时间状态机、录取进度查询(AJAX)、CSV/XLSX 结果解析、Formidable Forms 对接、以及后台“招新设置”页面。
* Version: 1.0.0
* Author: ITStudio OUC
* Text Domain: itstudio-recruitment
* Requires Plugins: itstudio-content
*
* 说明:本插件从 ITStudioMainSite 主题的 functions.php 抽离而来。
* 依赖 itstudio-content(新闻/公告文章类型),并在运行时软依赖 ACF 与 Formidable Forms。
* 迁移铁律:选项名 itstudio_join_settings、meta/ACF 字段 itstudio_is_recruitment_article、
* AJAX action itstudio_join_progress_lookup 必须与主题历史保持一字不差,否则老数据/前端脚本失联。
*/
if (!defined('ABSPATH')) {
exit;
}
define('ITSTUDIO_RECRUITMENT_VERSION', '1.0.0');
define('ITSTUDIO_RECRUITMENT_PATH', plugin_dir_path(__FILE__));
require_once ITSTUDIO_RECRUITMENT_PATH . 'includes/recruitment-articles.php';
require_once ITSTUDIO_RECRUITMENT_PATH . 'includes/join-system.php';
require_once ITSTUDIO_RECRUITMENT_PATH . 'includes/terms.php';
function itstudio_recruitment_activate() {
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'itstudio_recruitment_activate');
function itstudio_recruitment_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook(__FILE__, 'itstudio_recruitment_deactivate');