isset( $_POST['form_status'] ) ? sanitize_key( wp_unslash( $_POST['form_status'] ) ) : 'draft', 'deadline' => isset( $_POST['form_deadline'] ) ? sanitize_text_field( wp_unslash( $_POST['form_deadline'] ) ) : '', 'show_results_after_deadline' => ! empty( $_POST['form_show_results_after_deadline'] ), 'links' => aj_form_posted_links(), 'image_id' => isset( $_POST['form_image_id'] ) ? absint( $_POST['form_image_id'] ) : 0, 'show_success_message' => ! empty( $_POST['form_show_success'] ), 'duplicate_check' => isset( $_POST['form_duplicate_check'] ) ? sanitize_key( wp_unslash( $_POST['form_duplicate_check'] ) ) : 'cookie', 'show_total_responses' => ! empty( $_POST['form_show_total'] ), 'show_field_counts' => ! empty( $_POST['form_show_counts'] ), 'submit_mode' => isset( $_POST['form_submit_mode'] ) ? sanitize_key( wp_unslash( $_POST['form_submit_mode'] ) ) : 'button', ); $form_id = aj_form_save_form( $form_id, $title, $type, $description, $fields, $settings ); wp_safe_redirect( admin_url( 'admin.php?page=aj-form-edit&form_id=' . $form_id . '&saved=1' ) ); exit; } add_action( 'admin_init', 'aj_form_maybe_save_form' ); /** * POST 로 들어온 관련 링크 줄들을 배열로. * * 리피터는 label[]/url[] 두 배열로 전송된다. 정제는 * aj_form_sanitize_links() 가 저장 직전에 한 번 더 한다. * * @return array */ function aj_form_posted_links() { // 정제는 저장 직전 aj_form_sanitize_links() 가 한다(라벨은 sanitize_text_field, // URL 은 esc_url_raw). 여기서 걸면 배열 구조가 뭉개진다. // nonce 는 호출자인 aj_form_maybe_save_form() 의 check_admin_referer() 가 검증했다. // phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- 위 주석 참고. $labels = isset( $_POST['form_link_label'] ) ? (array) wp_unslash( $_POST['form_link_label'] ) : array(); $urls = isset( $_POST['form_link_url'] ) ? (array) wp_unslash( $_POST['form_link_url'] ) : array(); // phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $links = array(); foreach ( $urls as $i => $url ) { $links[] = array( 'label' => isset( $labels[ $i ] ) ? (string) $labels[ $i ] : '', 'url' => (string) $url, ); } return $links; } /** * 빌더에 넘길 필드 배열. * * 각 필드에 `_realCounts`(선택지별 실제 응답 수)를 붙여, 관리자가 입력하는 * "표시할 개수"에서 실제 수를 빼 seed 를 역산할 수 있게 한다. 밑줄로 시작하는 * 키는 aj_form_sanitize_fields() 가 버리므로 저장에는 영향이 없다. * * @param int $form_id * @param array $fields * @return array */ function aj_form_fields_for_builder( $form_id, $fields ) { $stats = $form_id ? aj_form_compute_stats( $fields, aj_form_get_submissions( $form_id ) ) : array(); $result = array(); foreach ( array_values( $fields ) as $index => $field ) { $field['_realCounts'] = array(); if ( isset( $stats[ $index ]['counts'] ) ) { $seeds = isset( $field['seeds'] ) && is_array( $field['seeds'] ) ? $field['seeds'] : array(); foreach ( $stats[ $index ]['counts'] as $option => $count ) { // compute_stats 의 counts 에는 seed 가 이미 더해져 있다. $seed = isset( $seeds[ $option ] ) ? (int) $seeds[ $option ] : 0; $field['_realCounts'][ $option ] = max( 0, (int) $count - $seed ); } } $result[] = $field; } return $result; } /** * 폼 생성/수정 화면 렌더링. */ function aj_form_render_edit_page() { // 어떤 폼을 여는지 읽기만 한다. 권한은 바로 위에서 검사했고 상태를 바꾸지 않는다. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- 위 주석 참고. $form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0; $title = ''; $type = 'form'; $description = ''; $defaults = aj_form_default_settings(); $show_success = $defaults['show_success_message']; $duplicate = $defaults['duplicate_check']; $submit_mode = $defaults['submit_mode']; $show_total = $defaults['show_total_responses']; $show_counts = $defaults['show_field_counts']; $after_dl = $defaults['show_results_after_deadline']; $deadline = ''; $links = array(); $view_url = ''; $image_id = 0; // **신규 폼은 초안으로 시작한다.** 기본값 배열의 status 가 publish 인 것은 // status 키가 없던 시절의 폼을 보호하기 위한 것이고, 새로 만드는 폼에까지 // 적용하면 완성 전에 사이트에 노출된다. $status = 'draft'; if ( $form_id ) { $form = aj_form_get_form( $form_id ); if ( $form ) { $title = $form['title']; $type = aj_form_normalize_type( $form['type'] ); $description = isset( $form['description'] ) ? $form['description'] : ''; $show_success = aj_form_setting( $form, 'show_success_message' ); $duplicate = aj_form_normalize_duplicate_mode( aj_form_setting( $form, 'duplicate_check' ) ); $submit_mode = aj_form_normalize_submit_mode( aj_form_setting( $form, 'submit_mode' ) ); $show_total = (bool) aj_form_setting( $form, 'show_total_responses' ); $show_counts = (bool) aj_form_setting( $form, 'show_field_counts' ); $status = aj_form_normalize_status( aj_form_setting( $form, 'status' ) ); $deadline = aj_form_sanitize_deadline( aj_form_setting( $form, 'deadline' ) ); $after_dl = (bool) aj_form_setting( $form, 'show_results_after_deadline' ); $links = aj_form_sanitize_links( aj_form_setting( $form, 'links' ) ); $view_url = aj_form_view_url( $form ); $image_id = aj_form_image_id( $form ); } else { $form_id = 0; // 존재하지 않으면 신규 작성 모드. } } // datetime-local 은 'Y-m-dTH:i' 만 받는다. 초까지 넘기면 브라우저가 버린다. $deadline_input = '' !== $deadline ? str_replace( ' ', 'T', substr( $deadline, 0, 16 ) ) : ''; $field_types = aj_form_field_types(); $form_types = aj_form_types(); $statuses = aj_form_statuses(); ?>