/** * Callback after creating the meta box * @param (object) the post object * @return (void) */ function han_dwa_qa_details_meta_box_callback($post) { global $hanDwaQaDetailsActionId; global $hanDwaQaDetailsNonceId; // Set a nonce wp_nonce_field($hanDwaQaDetailsActionId, $hanDwaQaDetailsNonceId); // Prepare the date $quesitonAmount = han_dwa_qa_get_qa_question_amount($post->ID); // Output include_once 'qa_details_html.php'; }
/** * Set the post meta * @return (void) */ private function set_post_meta() { global $wpdb; $this->name = get_post_meta($this->id, 'han_dwa_qa_question_name', true); $this->email = get_post_meta($this->id, 'han_dwa_qa_question_email', true); $tableName = $wpdb->prefix . HAN_DWA_QA_QUESTION_RELATION_TABLE_NAME; $this->qaId = $wpdb->get_var("SELECT `qa_id` FROM `{$tableName}` WHERE `question_id` = '{$this->id}'"); $this->questions = array(); $this->references = array(); for ($i = 1; $i <= han_dwa_qa_get_qa_question_amount($this->qaId); $i++) { $this->questions[] = get_post_meta($this->id, 'han_dwa_qa_question_content_' . $i, true); $this->references[] = get_post_meta($this->id, 'han_dwa_qa_question_reference_' . $i, true); } self::set_votes_count(); }
/** * Add a new question and chain it to a Q&A * @param (array) an array with the question fields * @param (int) the Q&A post ID * @return (bool) true on success, false on error */ function han_dwa_qa_add_question($questionData = null, $qaId = null) { $questionCount = han_dwa_qa_get_qa_question_amount($qaId); if ($qaId && (bool) get_post_status(intval($qaId)) && han_dwa_qa_validate_question_data($questionData, $questionCount) && han_dwa_qa_is_email_unique_for_qa(intval($qaId), sanitize_email($questionData['email']))) { $questionId = wp_insert_post(array('post_title' => sprintf(__('Question from %s', 'han-dwa-qa'), sanitize_text_field($questionData['name'])), 'post_status' => 'publish', 'post_type' => HAN_DWA_QA_QUESTION_CPT)); if ($questionId > 0) { global $wpdb; add_post_meta($questionId, 'han_dwa_qa_question_name', sanitize_text_field($questionData['name'])); add_post_meta($questionId, 'han_dwa_qa_question_email', sanitize_email($questionData['email'])); for ($i = 1; $i <= $questionCount; $i++) { add_post_meta($questionId, 'han_dwa_qa_question_content_' . $i, $questionData['question_' . $i]); add_post_meta($questionId, 'han_dwa_qa_question_reference_' . $i, $questionData['reference_' . $i]); } return (bool) $wpdb->insert($wpdb->prefix . HAN_DWA_QA_QUESTION_RELATION_TABLE_NAME, array('qa_id' => intval($qaId), 'question_id' => intval($questionId))); } } return false; }
<p> <label for="name"><?php _e('Name', 'han-dwa-qa'); ?> <span class="required">*</span></label> <input id="name" name="name" type="text" value="" aria-required="true" required="required" /> </p> <p> <label for="email"><?php echo _e('Email', 'han-dwa-qa'); ?> <span class="required">*</span></label> <input id="email" name="email" type="email" value="" aria-required="true" required="required" /> </p> <?php for ($i = 1; $i <= han_dwa_qa_get_qa_question_amount(get_the_ID()); $i++) { ?> <p> <label for="question-<?php echo $i; ?> "><?php echo sprintf(__('Your question or discussion item %s', 'han-dwa-qa'), $i); ?> <span class="required">*</span></label> <textarea id="question-<?php echo $i; ?> " name="question_<?php echo $i; ?>