/* ==================================================================== * 【前台投稿扣积分】子比 Zibll(子主题 functions.php 注入·完整版) * -------------------------------------------------------------------- * 规则: * 1) 没积分的登录用户进不去投稿页,直接提示「缺 X 积分」; * 2) 点击「发布投稿」前再校验一次,不足则弹「缺 X 积分」并阻止提交; * 3) 投稿提交成功(待审/发布)立即扣 X 积分,写子比积分流水; * 4) 管理员驳回(待审→草稿)或删除(→回收站)自动退还 X 积分。 * 依赖子比原生积分函数:zibpay_get_user_points() / zibpay_update_user_points() * 注意:请停用并删除旧插件「zib-submit-point」,否则会重复扣分! * ==================================================================== */ if ( ! defined( 'XZP_SUBMIT_COST' ) ) { define( 'XZP_SUBMIT_COST', 3 ); // ← 投稿固定消耗积分,改这里即可 } /* ---------- 积分读写封装 ---------- */ function xzp_points( $uid = 0 ) { if ( function_exists( 'zibpay_get_user_points' ) ) { return (int) zibpay_get_user_points( (int) $uid ); } return 0; } function xzp_points_change( $uid, $value, $type, $desc ) { if ( ! function_exists( 'zibpay_update_user_points' ) ) { return false; } zibpay_update_user_points( (int) $uid, array( 'order_num' => '', // 投稿无关联订单号 'value' => (int) $value, // 正数加、负数扣 'type' => $type, 'desc' => $desc, ) ); return true; } /* ---------- 1) 没积分禁止进入投稿页(服务端硬拦截) ---------- */ function xzp_block_posts_page( $template ) { // 未登录交给子比引导登录,管理员不拦 if ( ! is_user_logged_in() || current_user_can( 'manage_options' ) ) { return $template; } // 只拦子比前台投稿页(模板文件为 pages/newposts.php,父/子主题通用) if ( false === strpos( (string) $template, 'newposts.php' ) ) { return $template; } $points = xzp_points( get_current_user_id() ); if ( $points >= XZP_SUBMIT_COST ) { return $template; // 积分够:放行 } // 积分不足:拦截,不让投稿表单渲染 nocache_headers(); wp_die( '

积分不足,无法投稿

' . '

本次投稿需要 ' . XZP_SUBMIT_COST . ' 积分,你当前积分:' . $points . ',还差 ' . ( XZP_SUBMIT_COST - $points ) . ' 积分,无法进入投稿页面。

' . '

返回首页

', '积分不足' ); return $template; } add_filter( 'template_include', 'xzp_block_posts_page', 20 ); /* ---------- 2) 投稿提交即扣分 ---------- */ function xzp_on_submit_charge( $post_id, $post, $update ) { // 仅处理子比前台投稿「发布/提交审核」(action=posts_save) if ( ! isset( $_POST['action'] ) || 'posts_save' !== $_POST['action'] ) { return; } if ( ! is_object( $post ) || 'post' !== $post->post_type ) { return; } if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { return; } if ( ! in_array( $post->post_status, array( 'pending', 'publish' ), true ) ) { return; } if ( get_post_meta( $post_id, '_xzp_charged', true ) ) { return; // 已扣过,幂等 } $uid = get_current_user_id(); if ( ! $uid || user_can( $uid, 'manage_options' ) ) { return; // 未登录或管理员不扣 } xzp_points_change( $uid, - XZP_SUBMIT_COST, '投稿扣费', '发布投稿《' . get_the_title( $post_id ) . '》' ); update_post_meta( $post_id, '_xzp_charged', current_time( 'mysql' ) ); } add_action( 'save_post', 'xzp_on_submit_charge', 20, 3 ); /* ---------- 3) 驳回/删除退分 ---------- */ function xzp_on_reject_refund( $new_status, $old_status, $post ) { if ( ! is_object( $post ) || 'post' !== $post->post_type ) { return; } // 仅“待审→驳回/删除”或“已发布→删除”退分 $refund = false; if ( 'pending' === $old_status && in_array( $new_status, array( 'draft', 'trash' ), true ) ) { $refund = true; // 待审被驳回(退回草稿)或删除 } elseif ( 'publish' === $old_status && 'trash' === $new_status ) { $refund = true; // 已发布被删除 } if ( ! $refund ) { return; } if ( ! get_post_meta( $post->ID, '_xzp_charged', true ) ) { return; // 没扣过,不用退 } if ( get_post_meta( $post->ID, '_xzp_refunded', true ) ) { return; // 已退过,幂等 } $uid = (int) $post->post_author; if ( ! $uid ) { return; } xzp_points_change( $uid, XZP_SUBMIT_COST, '投稿退回', '投稿《' . get_the_title( $post->ID ) . '》被驳回/删除,退还积分' ); update_post_meta( $post->ID, '_xzp_refunded', current_time( 'mysql' ) ); } add_action( 'transition_post_status', 'xzp_on_reject_refund', 20, 3 ); /* ---------- 4) 提交前 AJAX 积分校验 ---------- */ function xzp_ajax_check() { check_ajax_referer( 'xzp_check', 'nonce' ); $uid = get_current_user_id(); $points = $uid ? xzp_points( $uid ) : 0; $enough = ( $uid && $points >= XZP_SUBMIT_COST ); wp_send_json_success( array( 'enough' => $enough, 'cost' => XZP_SUBMIT_COST, 'points' => $points, ) ); } add_action( 'wp_ajax_xzp_check', 'xzp_ajax_check' ); /* ---------- 5) 前台投稿页:提示 + 点击拦截 ---------- */ function xzp_frontend_js() { if ( is_admin() || ! is_user_logged_in() ) { return; } $uid = get_current_user_id(); $data = array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'xzp_check' ), 'cost' => XZP_SUBMIT_COST, 'points' => xzp_points( $uid ), ); echo ''; echo <<<'XZPJS' XZPJS; } add_action( 'wp_footer', 'xzp_frontend_js', 99 ); douyadouya-首码项目网 - 网上创业赚钱首码项目发布推广平台 - 813首码网
douyadouya-首码项目网 - 网上创业赚钱首码项目发布推广平台 - 813首码网
douyadouya的头像-首码项目网 - 网上创业赚钱首码项目发布推广平台 - 813首码网
徽章-人气佳作-首码项目网 - 网上创业赚钱首码项目发布推广平台 - 813首码网1枚徽章湖北省武汉市
这家伙很懒,什么都没有写...