LLMS_Meta_Box_Order_Submit

LLMS_Meta_Box_Order_Submit class


Source Source

File: includes/admin/post-types/meta-boxes/class.llms.meta.box.order.submit.php

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class LLMS_Meta_Box_Order_Submit extends LLMS_Admin_Metabox {
 
    /**
     * Configure the metabox settings
     *
     * @return   void
     * @since    3.0.0
     * @version  3.0.0
     */
    public function configure() {
 
        $this->id       = 'lifterlms-order-submit';
        $this->title    = __( 'Order Information', 'lifterlms' );
        $this->screens  = array(
            'llms_order',
        );
        $this->context  = 'side';
        $this->priority = 'high';
 
    }
 
    /**
     * Retrieve json to be used by the llms-editable date fields
     *
     * @param    int $time  timestamp
     * @return   string
     * @since    3.10.0
     * @version  3.19.0
     */
    public function get_editable_date_json( $time ) {
 
        return json_encode(
            array(
                'date'   => date_i18n( 'Y-m-d', $time ),
                'hour'   => date_i18n( 'H', $time ),
                'minute' => date_i18n( 'i', $time ),
            )
        );
 
    }
 
    /**
     * Not used because our metabox doesn't use the standard fields api
     *
     * @since  3.0.0
     *
     * @return array
     */
    public function get_fields() {
        return array();
    }
 
    /**
     * Function to field WP::output() method call
     * Passes output instruction to parent
     *
     * @since 3.0.0
     * @since 3.19.0 Unknown.
     *
     * @return string|null
     */
    public function output() {
 
        $order = new LLMS_Order( $this->post );
 
        if ( $order->is_legacy() ) {
            return _e( 'The status of a Legacy order cannot be changed.', 'lifterlms' );
        }
 
        include LLMS_PLUGIN_DIR . 'includes/admin/views/metaboxes/view-order-submit.php';
 
        wp_nonce_field( 'lifterlms_save_data', 'lifterlms_meta_nonce' );
 
    }
 
    /**
     * Save action, update order status
     *
     * @since 3.0.0
     * @since 3.19.0 Unknown.
     * @since 3.35.0 Verify nonces and sanitize `$_POST` data.
     * @since 3.36.0 Date fields require array when sanitized.
     * @since 5.9.0 Stop using deprecated `FILTER_SANITIZE_STRING`.
     * @since 7.0.0 Do not save recurring payments related dates if order's gateway do not support recurring payments modification.
     *
     * @param int $post_id  WP Post ID of the Order
     * @return null
     */
    public function save( $post_id ) {
 
        if ( ! llms_verify_nonce( 'lifterlms_meta_nonce', 'lifterlms_save_data' ) ) {
            return;
        }
 
        $order = llms_get_post( $post_id );
 
        if ( isset( $_POST['_llms_order_status'] ) ) {
 
            $new_status = llms_filter_input_sanitize_string( INPUT_POST, '_llms_order_status' );
            $old_status = $order->get( 'status' );
 
            if ( $old_status !== $new_status ) {
 
                // Update the status.
                $order->set( 'status', $new_status );
 
            }
        }
 
        $editable_dates = array(
            '_llms_date_access_expires',
        );
 
        // Save recurring payments related dates if order's gateway supports recurring payments modification.
        if ( $order->supports_modify_recurring_payments() ) {
            /**
             * Order is important -- if both trial and next payment are updated
             * they should be saved in that order since next payment date
             * is automatically recalculated by trial end date update.
             */
            array_push( $editable_dates, '_llms_date_trial_end', '_llms_date_next_payment' );
        }
 
        foreach ( $editable_dates as $id => $key ) {
 
            if ( isset( $_POST[ $key ] ) ) {
 
                // The array of date, hour, minute that was submitted.
                $dates = llms_filter_input_sanitize_string( INPUT_POST, $key, array( FILTER_REQUIRE_ARRAY ) );
 
                // Format the array of data as a datetime string.
                $new_date = $dates['date'] . ' ' . sprintf( '%02d', $dates['hour'] ) . ':' . sprintf( '%02d', $dates['minute'] );
 
                // Get the existing saved date without seconds (in the same format as $new_date).
                $saved_date = date_i18n( 'Y-m-d H:i', strtotime( get_post_meta( $post_id, $key, true ) ) );
 
                // If the dates are not equal, update the date.
                if ( $new_date !== $saved_date ) {
                    $order->set_date( str_replace( '_llms_date_', '', $key ), $new_date . ':00' );
                }
            }
        }
 
    }
 
}


Top ↑

Methods Methods

  • configure — Configure the metabox settings
  • get_editable_date_json — Retrieve json to be used by the llms-editable date fields
  • get_fields — Not used because our metabox doesn't use the standard fields api
  • output — Function to field WP::output() method call Passes output instruction to parent
  • save — Save action, update order status

Top ↑

Changelog Changelog

Changelog
Version Description
3.36.0 Date fields require array when sanitized.
3.35.0 Verify nonces and sanitize $_POST data.
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.