| | 1 | <?php |
| | 2 | /* |
| | 3 | * Create Sponsor Invoice Post type |
| | 4 | */ |
| | 5 | class WCP_Sponsor_Invoice { |
| | 6 | const POST_TYPE = 'wcp_sponsor_invoice'; |
| | 7 | |
| | 8 | public function __construct() { |
| | 9 | add_action( 'init', array( $this, 'register_post_type' ) ); |
| | 10 | add_action( 'init', array( __CLASS__, 'register_post_statuses') ); |
| | 11 | add_action( 'add_meta_boxes', array( $this, 'init_meta_boxes' ) ); |
| | 12 | add_action( 'save_post', array( $this, 'save_invoice' ), 10, 2 ); |
| | 13 | |
| | 14 | } |
| | 15 | |
| | 16 | /** |
| | 17 | * Register the custom post type for sponsor invoices |
| | 18 | * |
| | 19 | * @return object | WP_Error |
| | 20 | */ |
| | 21 | public function register_post_type() { |
| | 22 | $labels = array( |
| | 23 | 'name' => _x( 'Sponsor Invoices', 'general sponsor invoices', 'wordcamporg' ), |
| | 24 | 'singular_name' => _x( 'Sponsor Invoice', 'post type singular name', 'wordcamporg' ), |
| | 25 | 'menu_name' => _x( 'Sponsor Invoices', 'admin menu', 'wordcamporg' ), |
| | 26 | 'name_admin_bar' => _x( 'Sponsor Invoices', 'add new on admin bar', 'wordcamporg' ), |
| | 27 | 'add_new' => _x( 'Add New', 'invoice', 'wordcamporg' ), |
| | 28 | 'add_new_item' => __( 'Add New Sponsor Invoice', 'wordcamporg' ), |
| | 29 | 'new_item' => __( 'New Invoice', 'wordcamporg' ), |
| | 30 | 'edit_item' => __( 'Edit Invoice', 'wordcamporg' ), |
| | 31 | 'view_item' => __( 'View Invoice', 'wordcamporg' ), |
| | 32 | 'all_items' => __( 'All Invoices', 'wordcamporg' ), |
| | 33 | 'search_items' => __( 'Search Invoices', 'wordcamporg' ), |
| | 34 | 'not_found' => __( 'No invoice found.', 'wordcamporg' ), |
| | 35 | 'not_found_in_trash' => __( 'No invoice found in Trash.', 'wordcamporg' ) |
| | 36 | ); |
| | 37 | |
| | 38 | $args = array( |
| | 39 | 'labels' => $labels, |
| | 40 | 'description' => 'WordCamp Payment Sponsor Invoices', |
| | 41 | 'public' => false, |
| | 42 | 'show_ui' => true, |
| | 43 | 'show_in_nav_menus' => true, |
| | 44 | 'menu_position' => 25, |
| | 45 | 'supports' => array( 'title' ), |
| | 46 | 'has_archive' => true, |
| | 47 | ); |
| | 48 | |
| | 49 | return register_post_type( self::POST_TYPE, $args ); |
| | 50 | } |
| | 51 | |
| | 52 | /** |
| | 53 | * Register our custom post statuses |
| | 54 | */ |
| | 55 | public static function register_post_statuses() { |
| | 56 | register_post_status( |
| | 57 | 'Pre-Contact', |
| | 58 | array( |
| | 59 | 'label' => _x( 'Pre-Contact', 'post', 'wordcamporg' ), |
| | 60 | 'label_count' => _nx_noop( 'Pre Contact <span class="count">(%s)</span>', 'Pre Contact <span class="count">(%s)</span>', 'wordcamporg' ), |
| | 61 | 'public' => true, |
| | 62 | 'publicly_queryable' => false, |
| | 63 | ) |
| | 64 | ); |
| | 65 | |
| | 66 | register_post_status( |
| | 67 | 'Negotiation', |
| | 68 | array( |
| | 69 | 'label' => _x( 'Negotiation', 'post', 'wordcamporg' ), |
| | 70 | 'label_count' => _nx_noop( 'Negotiation <span class="count">(%s)</span>', 'Negotiation <span class="count">(%s)</span>', 'wordcamporg' ), |
| | 71 | 'public' => true, |
| | 72 | 'publicly_queryable' => false, |
| | 73 | ) |
| | 74 | ); |
| | 75 | register_post_status( |
| | 76 | 'Invoiced', |
| | 77 | array( |
| | 78 | 'label' => _x( 'Invoiced', 'post', 'wordcamporg' ), |
| | 79 | 'label_count' => _nx_noop( 'Invoiced <span class="count">(%s)</span>', 'Invoiced <span class="count">(%s)</span>', 'wordcamporg' ), |
| | 80 | 'public' => true, |
| | 81 | 'publicly_queryable' => false, |
| | 82 | ) |
| | 83 | ); |
| | 84 | |
| | 85 | register_post_status( |
| | 86 | 'Received Payment', |
| | 87 | array( |
| | 88 | 'label' => _x( 'Received Payment', 'post', 'wordcamporg' ), |
| | 89 | 'label_count' => _nx_noop( 'Received Payment <span class="count">(%s)</span>', 'Received Payment <span class="count">(%s)</span>', 'wordcamporg' ), |
| | 90 | 'public' => true, |
| | 91 | 'publicly_queryable' => false, |
| | 92 | ) |
| | 93 | ); |
| | 94 | register_post_status( |
| | 95 | 'Cancelled', |
| | 96 | array( |
| | 97 | 'label' => _x( 'Cancelled', 'post', 'wordcamporg' ), |
| | 98 | 'label_count' => _nx_noop( 'Cancelled <span class="count">(%s)</span>', 'Cancelled <span class="count">(%s)</span>', 'wordcamporg' ), |
| | 99 | 'public' => true, |
| | 100 | 'publicly_queryable' => false, |
| | 101 | ) |
| | 102 | ); |
| | 103 | |
| | 104 | } |
| | 105 | |
| | 106 | /** |
| | 107 | * Add metaboxes for Sponsor Invoice |
| | 108 | * |
| | 109 | */ |
| | 110 | public function init_meta_boxes() { |
| | 111 | add_meta_box( |
| | 112 | 'wcp_sponsor_invoice', |
| | 113 | __( 'Sponsor Invoice', 'wordcamporg' ), |
| | 114 | array( $this, 'render_sponsor_invoice_metabox' ), |
| | 115 | self::POST_TYPE, |
| | 116 | 'normal', |
| | 117 | 'high' |
| | 118 | ); |
| | 119 | } |
| | 120 | |
| | 121 | /** |
| | 122 | * Gets Sponsorship Types |
| | 123 | * |
| | 124 | * @return array() |
| | 125 | */ |
| | 126 | public static function get_sponsorship_type() { |
| | 127 | $types = array( 'money' => 'Money', |
| | 128 | 'in_kind_donation' => 'In-Kind Donation', |
| | 129 | 'other' => 'Other' |
| | 130 | ); |
| | 131 | return $types; |
| | 132 | } |
| | 133 | |
| | 134 | /** |
| | 135 | * Render Sponsor Invoice Metabox |
| | 136 | * @param WP_Post $post |
| | 137 | * |
| | 138 | */ |
| | 139 | public function render_sponsor_invoice_metabox( $post ) { |
| | 140 | wp_nonce_field( 'sponsor_invoice', 'sponsor_invoice_nonce' ); |
| | 141 | $categories = self::get_sponsorship_type(); |
| | 142 | $assigned_category = get_post_meta( $post->ID, '_campinvoices_sponsorship_type', true ); |
| | 143 | |
| | 144 | require_once( dirname( __DIR__ ) . '/views/payment-request/metabox-sponsor-invoice.php' ); |
| | 145 | } |
| | 146 | /** |
| | 147 | * Render a <input type="text"> field with the given attributes. |
| | 148 | * |
| | 149 | * @param WP_Post $post |
| | 150 | * @param string $label |
| | 151 | * @param string $name |
| | 152 | */ |
| | 153 | protected function render_text_input( $post, $label, $name, $description = '', $variant = 'text', $row_classes = array(), $readonly = false ) { |
| | 154 | $value = $this->get_field_value( $name, $post ); |
| | 155 | array_walk( $row_classes, 'sanitize_html_class' ); |
| | 156 | $row_classes = implode( ' ', $row_classes ); |
| | 157 | |
| | 158 | require( dirname( __DIR__ ) . '/views/payment-request/input-text.php' ); |
| | 159 | } |
| | 160 | |
| | 161 | /** |
| | 162 | * Render a <select> field with the given attributes. |
| | 163 | * |
| | 164 | * @param WP_Post $post |
| | 165 | * @param string $label |
| | 166 | * @param string $name |
| | 167 | */ |
| | 168 | protected function render_select_input( $post, $label, $name ) { |
| | 169 | $selected = get_post_meta( $post->ID, '_campinvoices_' . $name, true ); |
| | 170 | $options = $this->get_field_value( $name, $post ); |
| | 171 | |
| | 172 | require( dirname( __DIR__ ) . '/views/payment-request/input-select.php' ); |
| | 173 | } |
| | 174 | |
| | 175 | /** |
| | 176 | * Render a <textarea> field with the given attributes. |
| | 177 | * |
| | 178 | * @param WP_Post $post |
| | 179 | * @param string $label |
| | 180 | * @param string $name |
| | 181 | * @param string $description |
| | 182 | */ |
| | 183 | protected function render_textarea_input( $post, $label, $name, $description = '' ) { |
| | 184 | $date = get_post_meta( $post->ID, '_campinvoices_' . $name, true ); |
| | 185 | |
| | 186 | require( dirname( __DIR__ ) . '/views/payment-request/input-textarea.php' ); |
| | 187 | } |
| | 188 | |
| | 189 | /** |
| | 190 | * Render a <input type="checkbox"> field with the given attributes. |
| | 191 | * |
| | 192 | * @param WP_Post $post |
| | 193 | * @param string $label |
| | 194 | * @param string $name |
| | 195 | */ |
| | 196 | protected function render_checkbox_input( $post, $label, $name, $description = '' ) { |
| | 197 | $value = $this->get_field_value( $name, $post ); |
| | 198 | |
| | 199 | require( dirname( __DIR__ ) . '/views/payment-request/input-checkbox.php' ); |
| | 200 | } |
| | 201 | |
| | 202 | /** |
| | 203 | * Get the value of a given field. |
| | 204 | * |
| | 205 | * @param string $name |
| | 206 | * @param WP_Post $post |
| | 207 | * |
| | 208 | * @return mixed |
| | 209 | */ |
| | 210 | protected function get_field_value( $name, $post ) { |
| | 211 | $value = get_post_meta( $post->ID, '_campinvoices_' . $name, true ); |
| | 212 | |
| | 213 | return $value; |
| | 214 | } |
| | 215 | |
| | 216 | /** |
| | 217 | * Save the post's data |
| | 218 | * |
| | 219 | * @param int $post_id |
| | 220 | * @param WP_Post $post |
| | 221 | */ |
| | 222 | public function save_invoice( $post_id, $post ) { |
| | 223 | // Verify nonces |
| | 224 | $nonces = array( 'sponsor_invoice_nonce' ); |
| | 225 | |
| | 226 | foreach ( $nonces as $nonce ) { |
| | 227 | if ( ! isset( $_POST[ $nonce ] ) || ! wp_verify_nonce( $_POST[ $nonce ], str_replace( '_nonce', '', $nonce ) ) ) { |
| | 228 | return; |
| | 229 | } |
| | 230 | } |
| | 231 | |
| | 232 | // Sanitize and save the field values |
| | 233 | $this->sanitize_save_normal_fields( $post_id ); |
| | 234 | $this->sanitize_save_misc_fields( $post_id ); |
| | 235 | } |
| | 236 | |
| | 237 | /** |
| | 238 | * Sanitize and save values for all normal fields |
| | 239 | * |
| | 240 | * @param int $post_id |
| | 241 | */ |
| | 242 | protected function sanitize_save_normal_fields( $post_id ) { |
| | 243 | foreach ( $_POST as $key => $unsafe_value ) { |
| | 244 | switch ( $key ) { |
| | 245 | case 'notes': |
| | 246 | $safe_value = wp_kses( $unsafe_value, wp_kses_allowed_html( 'strip' ) ); |
| | 247 | break; |
| | 248 | |
| | 249 | case 'contact_name': |
| | 250 | case 'sponsor_email': |
| | 251 | case 'company_name': |
| | 252 | case 'sponsorship_level': |
| | 253 | case 'coupon_code': |
| | 254 | case 'received_amount': |
| | 255 | case 'sponsorship_level': |
| | 256 | $safe_value = sanitize_text_field( $unsafe_value ); |
| | 257 | break; |
| | 258 | |
| | 259 | default: |
| | 260 | $safe_value = null; |
| | 261 | break; |
| | 262 | } |
| | 263 | |
| | 264 | if ( ! is_null( $safe_value ) ) { |
| | 265 | update_post_meta( $post_id, '_campinvoices_' . $key, $safe_value ); |
| | 266 | } |
| | 267 | } |
| | 268 | } |
| | 269 | |
| | 270 | /** |
| | 271 | * Sanitize and save values for all checkbox fields |
| | 272 | * |
| | 273 | * @param int $post_id |
| | 274 | */ |
| | 275 | protected function sanitize_save_misc_fields( $post_id ) { |
| | 276 | |
| | 277 | // Checkboxes |
| | 278 | $checkbox_fields = array( 'published_blog_post', 'published_logo', 'check_requested', 'check_sent' ); |
| | 279 | foreach( $checkbox_fields as $field ) { |
| | 280 | if ( isset( $_POST[ $field ] ) ) { |
| | 281 | update_post_meta( $post_id, '_campinvoices_' . $field, $_POST[ $field ] ); |
| | 282 | } else { |
| | 283 | delete_post_meta( $post_id, '_campinvoices_' . $field ); |
| | 284 | } |
| | 285 | } |
| | 286 | } |
| | 287 | |
| | 288 | |
| | 289 | } |
| | 290 | |
| | 291 | ?> |