Dubble在重力表单提交上创建了自定义帖子类型

时间:2018-08-14 10:55:34

标签: php wordpress gravityforms

当用户在我的WordPress网站上提交重力形式时,gravityform插件将连接到pronamic付款页面。用户将为我们的网站捐款。此捐赠需要在WordPress中保存为自定义帖子类型(捐赠)。到目前为止,我已经创建了以下代码:

class ColsensationAddons {

    /**
     * Constructs and initialize
     */

    public function __construct() {

        add_action('gform_ideal_fulfillment', array( $this, 'handle_payment_subscription_fee'), 10, 2 );
        add_action('gform_user_registered', array( $this, 'handle_user_registration'), 10, 4 );
        add_filter('gform_pre_render_2', array( $this, 'handle_prefill_form') );

    }

    /**
     * Function fires after submission of the payment subscription form
     */

    public function handle_payment_subscription_fee( $entry, $feed ) {


        $form_id = $entry['form_id'];
        $is_fulfilled = $entry['is_fulfilled'];
        $amount = $entry['payment_amount']*100;

        $this->log_action( "Form ID ".$form_id );

        switch( $form_id ) {

            case "3":

                // user subscription fee
                $user_id = rgar( $entry, '1');

                update_user_meta( $user_id, "payment_status", "paid" ); 

                $this->log_action( "Handling fulfillment request." );
                $this->log_action( "User ID: ".$user_id );
                $this->log_action( "Fulfillment status: ".$is_fulfilled );
                $this->log_action( "Updated payment_status to paid" );
                $this->log_action( "Amount paid: ".$amount);
                $this->log_action( "-------" );

                break;

            case "4":
                // donation to a user and/or a team 
                $transaction_costs = rgar( $entry, '8.1');
                $user_id = rgar( $entry, '2');
                $team_id = rgar( $entry, '6');
                $name = rgar( $entry, '4');
                $message = rgar( $entry, '5');


                $this->save_donation( $transaction_costs, $amount, $name, $message, $user_id, $team_id );

                $this->log_action( "Handling donation request." );
                $this->log_action( "Transaction costs: ".$transaction_costs );
                $this->log_action( "User ID: ".$user_id );
                $this->log_action( "Amount: ".$amount );
                $this->log_action( "Donation saved");
                $this->log_action( "-------" );

                break;

            case "5":
                // donation to a team
                $transaction_costs = rgar( $entry, '7.1');
                $team_id = rgar( $entry, '2');
                $name = rgar( $entry, '4');
                $message = rgar( $entry, '5');

                $this->save_donation( $transaction_costs, $amount, $name, $message, false, $team_id);

                $this->log_action( "Handling donation request." );
                $this->log_action( "Team ID: ".$user_id );
                $this->log_action( "Amount: ".$amount );
                $this->log_action( "Donation saved");
                $this->log_action( "-------" );

                break;

        }

    }

    /**
     * Save donation
     */

    public function save_donation($transaction_costs, $amount, $name, $message, $user_id = false, $team_id = false) {

        if($transaction_costs){
        $amount = ($amount - 30);
        }

        $format_amount = number_format( ($amount / 100), 2 );   


        $title = "Donation of € ".$format_amount;

        $post_data = array(
            "post_title" => $title,
            "post_type" => "donatie",
            "post_status" => "publish"
        );

        $post_id = wp_insert_post( $post_data );


        $name = strip_tags( $name );
        $message = strip_tags( $message );

        update_post_meta( $post_id, "donation_amount", $amount );
        update_post_meta( $post_id, "donation_user_id", $user_id );
        update_post_meta( $post_id, "donation_team_id", $team_id );
        update_post_meta( $post_id, "donation_name", $name );
        update_post_meta( $post_id, "donation_message", $message );
        if($transaction_costs){
        update_post_meta( $post_id, "donation_transaction_costs", 30 );
        } 

    }

}

此代码可以正常工作,但是有时我会创建一个微不足道的自定义帖子类型。很奇怪,因为此功能只会启动一个。

有人知道如何解决这个愚蠢的捐赠吗?

0 个答案:

没有答案