array( 'name' => __( 'Inquiries', 'acs-contact-form' ), 'singular_name' => __( 'Inquiries', 'acs-contact-form' ), ), 'public' => false, 'publicly_queryable' => false, 'exclude_from_search' => true, 'show_ui' => false, 'show_in_menu' => false, 'show_in_nav_menus' => false, 'show_in_rest' => false, 'query_var' => false, 'rewrite' => false, 'has_archive' => false, 'hierarchical' => false, 'capability_type' => 'post', 'map_meta_cap' => true, 'supports' => array( 'title' ), ) ); } add_action( 'init', 'acscf_register_entry_post_type' ); /** * Capability required to read and manage received submissions. * * @return string */ function acscf_manage_capability() { /** * Filters the capability required to manage received submissions. * * @param string $capability Capability name. */ return apply_filters( 'acscf_manage_capability', 'manage_options' ); } /** * Status list. * * @return array */ function acscf_get_statuses() { return array( 'new' => __( 'Untouched', 'acs-contact-form' ), 'in_progress' => __( 'In progress', 'acs-contact-form' ), 'done' => __( 'Done', 'acs-contact-form' ), ); } /** * Category list. * * @return array */ function acscf_get_categories() { return array( 'estimate' => __( 'Estimate', 'acs-contact-form' ), 'consult' => __( 'Consultation', 'acs-contact-form' ), 'recruit' => __( 'Recruiting', 'acs-contact-form' ), 'sales' => __( 'Sales', 'acs-contact-form' ), 'spam' => __( 'Spam', 'acs-contact-form' ), ); } /** * Human label for a status slug. * * @param string $status Status slug. * @return string */ function acscf_status_label( $status ) { $statuses = acscf_get_statuses(); return isset( $statuses[ $status ] ) ? $statuses[ $status ] : $statuses['new']; } /** * Human label for a category slug. * * @param string $category Category slug. * @return string */ function acscf_category_label( $category ) { $categories = acscf_get_categories(); return isset( $categories[ $category ] ) ? $categories[ $category ] : $categories['consult']; } /** * Store one received submission. * * @param array $args { * Submission data. * * @type int $form_id Form post ID. * @type string $form_name Form title. * @type string $name Sender name. * @type string $email Sender email. * @type string $subject Subject. * @type string $message Message body. * @type array $fields List of array( label, value ). * @type array $attachments List of array( name, file ). * @type string $category Category slug. * @type string $referer Source URL. * @type string $ip Remote address. * } * @return int Entry post ID, or 0 on failure. */ function acscf_store_entry( $args ) { $defaults = array( 'form_id' => 0, 'form_name' => '', 'name' => '', 'email' => '', 'subject' => '', 'message' => '', 'fields' => array(), 'attachments' => array(), 'category' => 'consult', 'referer' => '', 'ip' => '', ); $args = wp_parse_args( $args, $defaults ); $title = $args['subject']; if ( '' === trim( (string) $title ) ) { $title = wp_trim_words( (string) $args['message'], 12, '…' ); } if ( '' === trim( (string) $title ) ) { $title = __( '(no subject)', 'acs-contact-form' ); } $entry_id = wp_insert_post( array( 'post_type' => 'acscf_entry', 'post_status' => 'private', 'post_title' => sanitize_text_field( $title ), ), true ); if ( is_wp_error( $entry_id ) || ! $entry_id ) { return 0; } $clean_fields = array(); foreach ( (array) $args['fields'] as $field ) { if ( ! is_array( $field ) || ! isset( $field['label'] ) ) { continue; } $clean_fields[] = array( 'label' => sanitize_text_field( $field['label'] ), 'value' => isset( $field['value'] ) ? sanitize_textarea_field( $field['value'] ) : '', ); } $clean_attachments = array(); foreach ( (array) $args['attachments'] as $attachment ) { if ( empty( $attachment['file'] ) ) { continue; } $clean_attachments[] = array( 'name' => sanitize_file_name( isset( $attachment['name'] ) ? $attachment['name'] : basename( $attachment['file'] ) ), 'file' => $attachment['file'], 'size' => isset( $attachment['size'] ) ? (int) $attachment['size'] : 0, ); } update_post_meta( $entry_id, '_acscf_form_id', (int) $args['form_id'] ); update_post_meta( $entry_id, '_acscf_form_name', sanitize_text_field( $args['form_name'] ) ); update_post_meta( $entry_id, '_acscf_name', sanitize_text_field( $args['name'] ) ); update_post_meta( $entry_id, '_acscf_email', sanitize_email( $args['email'] ) ); update_post_meta( $entry_id, '_acscf_subject', sanitize_text_field( $args['subject'] ) ); update_post_meta( $entry_id, '_acscf_message', sanitize_textarea_field( $args['message'] ) ); update_post_meta( $entry_id, '_acscf_fields', $clean_fields ); update_post_meta( $entry_id, '_acscf_attachments', $clean_attachments ); update_post_meta( $entry_id, '_acscf_category', acscf_sanitize_category( $args['category'] ) ); update_post_meta( $entry_id, '_acscf_status', 'new' ); update_post_meta( $entry_id, '_acscf_referer', esc_url_raw( $args['referer'] ) ); update_post_meta( $entry_id, '_acscf_ip', sanitize_text_field( $args['ip'] ) ); /** * Fires after a submission has been stored. * * @param int $entry_id Entry post ID. * @param array $args Submission data. */ do_action( 'acscf_entry_stored', $entry_id, $args ); return (int) $entry_id; } /** * Sanitize a status slug. * * @param string $status Raw status. * @return string */ function acscf_sanitize_status( $status ) { $status = sanitize_key( $status ); $statuses = acscf_get_statuses(); return isset( $statuses[ $status ] ) ? $status : 'new'; } /** * Sanitize a category slug. * * @param string $category Raw category. * @return string */ function acscf_sanitize_category( $category ) { $category = sanitize_key( $category ); $categories = acscf_get_categories(); return isset( $categories[ $category ] ) ? $category : 'consult'; } /** * Read one entry into a plain array. * * @param int $entry_id Entry post ID. * @return array|null */ function acscf_get_entry( $entry_id ) { $entry_id = (int) $entry_id; $post = get_post( $entry_id ); if ( ! $post || 'acscf_entry' !== $post->post_type ) { return null; } $fields = get_post_meta( $entry_id, '_acscf_fields', true ); $files = get_post_meta( $entry_id, '_acscf_attachments', true ); $notes = get_post_meta( $entry_id, '_acscf_notes', true ); return array( 'id' => $entry_id, 'title' => $post->post_title, 'created' => $post->post_date, 'created_gmt' => $post->post_date_gmt, 'form_id' => (int) get_post_meta( $entry_id, '_acscf_form_id', true ), 'form_name' => (string) get_post_meta( $entry_id, '_acscf_form_name', true ), 'name' => (string) get_post_meta( $entry_id, '_acscf_name', true ), 'email' => (string) get_post_meta( $entry_id, '_acscf_email', true ), 'subject' => (string) get_post_meta( $entry_id, '_acscf_subject', true ), 'message' => (string) get_post_meta( $entry_id, '_acscf_message', true ), 'fields' => is_array( $fields ) ? $fields : array(), 'attachments' => is_array( $files ) ? $files : array(), 'notes' => is_array( $notes ) ? $notes : array(), 'category' => acscf_sanitize_category( get_post_meta( $entry_id, '_acscf_category', true ) ), 'status' => acscf_sanitize_status( get_post_meta( $entry_id, '_acscf_status', true ) ), 'referer' => (string) get_post_meta( $entry_id, '_acscf_referer', true ), 'ip' => (string) get_post_meta( $entry_id, '_acscf_ip', true ), 'replied_at' => (string) get_post_meta( $entry_id, '_acscf_replied_at', true ), 'draft' => (string) get_post_meta( $entry_id, '_acscf_draft', true ), ); } /** * Update the status of an entry. * * @param int $entry_id Entry post ID. * @param string $status New status. * @return bool */ function acscf_set_entry_status( $entry_id, $status ) { $entry_id = (int) $entry_id; $post = get_post( $entry_id ); if ( ! $post || 'acscf_entry' !== $post->post_type ) { return false; } $status = acscf_sanitize_status( $status ); update_post_meta( $entry_id, '_acscf_status', $status ); if ( 'done' === $status && ! get_post_meta( $entry_id, '_acscf_replied_at', true ) ) { update_post_meta( $entry_id, '_acscf_replied_at', current_time( 'mysql' ) ); } return true; } /** * Update the category of an entry. * * @param int $entry_id Entry post ID. * @param string $category New category. * @return bool */ function acscf_set_entry_category( $entry_id, $category ) { $entry_id = (int) $entry_id; $post = get_post( $entry_id ); if ( ! $post || 'acscf_entry' !== $post->post_type ) { return false; } update_post_meta( $entry_id, '_acscf_category', acscf_sanitize_category( $category ) ); return true; } /** * Append a note to the reply history of an entry. * * @param int $entry_id Entry post ID. * @param string $note Note text. * @param int $user_id Author user ID. * @return bool */ function acscf_add_entry_note( $entry_id, $note, $user_id = 0 ) { $entry_id = (int) $entry_id; $note = sanitize_textarea_field( $note ); if ( 'acscf_entry' !== get_post_type( $entry_id ) || '' === trim( $note ) ) { return false; } $notes = get_post_meta( $entry_id, '_acscf_notes', true ); if ( ! is_array( $notes ) ) { $notes = array(); } $notes[] = array( 'time' => current_time( 'mysql' ), 'user' => (int) $user_id, 'text' => $note, ); update_post_meta( $entry_id, '_acscf_notes', $notes ); return true; } /** * Elapsed time since submission, as a human readable string. * * @param string $mysql_date_gmt Post date in GMT. * @return string */ function acscf_elapsed_label( $mysql_date_gmt ) { $timestamp = strtotime( $mysql_date_gmt . ' +0000' ); if ( ! $timestamp ) { return ''; } $diff = time() - $timestamp; if ( $diff < 0 ) { $diff = 0; } /* translators: %s: human readable time difference, e.g. "3 days". */ return sprintf( __( '%s ago', 'acs-contact-form' ), human_time_diff( $timestamp, time() ) ); } /** * Delete the stored files of an entry when the entry itself is deleted. * * @param int $post_id Post ID. * @return void */ function acscf_delete_entry_files( $post_id ) { $post = get_post( $post_id ); if ( ! $post || 'acscf_entry' !== $post->post_type ) { return; } $attachments = get_post_meta( $post_id, '_acscf_attachments', true ); if ( ! is_array( $attachments ) ) { return; } foreach ( $attachments as $attachment ) { if ( ! empty( $attachment['file'] ) ) { acscf_delete_stored_file( $attachment['file'] ); } } } add_action( 'before_delete_post', 'acscf_delete_entry_files' ); /** * URL of the inbox list screen. * * @param array $args Extra query arguments. * @return string */ function acscf_entries_admin_url( $args = array() ) { $url = admin_url( 'edit.php?post_type=acscf_form&page=acscf-entries' ); if ( ! empty( $args ) ) { $url = add_query_arg( $args, $url ); } return $url; } /** * URL of one submission. * * @param int $entry_id Entry post ID. * @return string */ function acscf_entry_admin_url( $entry_id ) { return acscf_entries_admin_url( array( 'entry' => (int) $entry_id ) ); }