# -*- coding: utf-8 -*- from pathlib import Path # save profile rewards p = Path('inc/helpers/aksrt-user-center.php') t = p.read_text(encoding='utf-8') if 'rewards_weixin' not in t: needle = "update_user_meta($user_id, 'aksrt_user_contacts', aksrt_normalize_user_contacts($input['contacts'] ?? []));\n\t\t\treturn true;" insert = """update_user_meta($user_id, 'aksrt_user_contacts', aksrt_normalize_user_contacts($input['contacts'] ?? [])); // Tip QR images (attachment id or URL). Empty clears. if (array_key_exists('rewards_weixin', $input) || array_key_exists('rewards_alipay', $input)) { foreach (['rewards_weixin' => 'aksrt_rewards_weixin', 'rewards_alipay' => 'aksrt_rewards_alipay'] as $field => $meta) { if (!array_key_exists($field, $input)) { continue; } $raw = is_string($input[$field]) ? trim(wp_unslash($input[$field])) : ''; if ($raw === '' || $raw === '0') { delete_user_meta($user_id, $meta); continue; } if (ctype_digit($raw)) { $att = (int) $raw; if ($att > 0 && wp_attachment_is_image($att)) { update_user_meta($user_id, $meta, $att); } continue; } $url = esc_url_raw($raw); if ($url !== '') { update_user_meta($user_id, $meta, $url); } } } return true;""" if needle not in t: raise SystemExit('save needle missing') p.write_text(t.replace(needle, insert, 1), encoding='utf-8') print('save rewards ok') else: print('save rewards already') # tasks deep links + done CTA p = Path('inc/helpers/user-center-renderers/credits-points-tasks.php') t = p.read_text(encoding='utf-8') start = t.find("case 'comment_new':") end = t.find("case 'bbs_posts_new':", start) if start < 0 or end < 0: raise SystemExit('task cases missing') new_block = """case 'comment_new': $posts = get_posts(['numberposts' => 1, 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC']); $href = $home; if (!empty($posts[0]) && $posts[0] instanceof WP_Post) { $link = get_permalink($posts[0]); if (is_string($link) && $link !== '') { $href = $link . '#comments'; } } return [ 'type' => 'link', 'label' => __('去完成', 'aksrt'), 'href' => $href, ]; case 'login': return [ 'type' => 'none', 'label' => __('登录即可领取', 'aksrt'), ]; case 'post_like': case 'post_favorite': case 'comment_like': $posts = get_posts(['numberposts' => 1, 'post_status' => 'publish', 'orderby' => 'comment_count', 'order' => 'DESC']); $href = $home; if (!empty($posts[0]) && $posts[0] instanceof WP_Post) { $link = get_permalink($posts[0]); if (is_string($link) && $link !== '') { $href = $link; } } return [ 'type' => 'link', 'label' => __('去看看', 'aksrt'), 'href' => $href, ]; case 'followed': $center = function_exists('aksrt_get_user_center_url') ? (string) aksrt_get_user_center_url() : $home; return [ 'type' => 'link', 'label' => __('去主页', 'aksrt'), 'href' => $center !== '' ? $center : $home, ]; """ # preserve indent of original case indent = '' line = t[t.rfind('\n', 0, start)+1:start] # new_block assumes tabs matching file t = t[:start] + new_block + t[end:] # CTA block old_cta = """ \"> """ new_cta = """ \"> """ # try without escaped quotes old_cta2 = old_cta.replace('\\"', '"') new_cta2 = new_cta.replace('\\"', '"') if old_cta2 in t: t = t.replace(old_cta2, new_cta2, 1) print('cta updated') else: # fuzzy find i = t.find("data-aksrt-signin-goto") print('signin btn at', i) print(repr(t[i-120:i+220])) # replace using unique start s = t.find("") e = t.find("", s) e = t.find("\n", e) if s > 0 and e > s: t = t[:s] + new_cta2 + t[e+1:] print('cta fuzzy updated') else: print('cta not found') p.write_text(t, encoding='utf-8') print('tasks updated')