$post_data ] ); } /** * Save post. * * @since 1.1.0 */ public function save_post() { check_ajax_referer( 'sellkit', 'nonce' ); $fields = sellkit_post( 'fields' ); $post_id = sellkit_htmlspecialchars( INPUT_POST, 'post_id' ); $post_type = sellkit_htmlspecialchars( INPUT_POST, 'post_type' ); $this->sub_action = sellkit_htmlspecialchars( INPUT_POST, 'sub_action' ); $sanitized_fields = $this->sanitize_fields( $fields ); $this->validate_fields( $sanitized_fields ); $target_post_id = $this->handle_save_post( $sanitized_fields, $post_type, $post_id ); if ( empty( $target_post_id ) ) { wp_send_json_error( [ __( 'something went wrong', 'sellkit' ) ] ); } if ( $post_id ) { wp_send_json_success( [ 'post_id' => $post_id, 'message' => __( 'The update process has been completed.', 'sellkit' ), ] ); } wp_send_json_success( [ 'post_id' => $target_post_id, 'message' => __( 'The creation process has been completed.', 'sellkit' ), ] ); } /** * Update or add new post. * * @param array $data All data. * @param string $post_type The post type. * @param bool $post_id The post Id. * @return bool|integer * * @since 1.1.0 * @SuppressWarnings(PHPMD.NPathComplexity) */ private function handle_save_post( $data, $post_type, $post_id = false ) { $title = sanitize_text_field( $data['sellkit_post_title'] ); $slug = sanitize_text_field( $data['sellkit_post_slug'] ); $post_status = 'publish' === $data['sellkit_post_status'] ? 'publish' : 'draft'; $post_args = [ 'post_title' => $title, 'post_name' => $slug ?: sanitize_title( $title ), 'post_type' => $post_type, 'post_status' => $post_status, ]; if ( ! $post_id ) { $new_post_id = wp_insert_post( $post_args ); } if ( $post_id ) { $post_args = array_merge( $post_args, [ 'ID' => $post_id ] ); $new_post_id = wp_update_post( $post_args ); do_action( "sellkit_posts_after_saving_$post_type", $post_id ); } if ( empty( $new_post_id ) || is_wp_error( $new_post_id ) ) { return false; } foreach ( $data as $name => $value ) { if ( 'sellkit_post_title' === $name || 'sellkit_post_status' === $name ) { continue; } update_post_meta( $new_post_id, $name, $value ); if ( 'sellkit_steps' === $name ) { do_action( 'sellkit_funnels_update_steps', $value, $new_post_id ); } } if ( empty( $data['conditions'] ) ) { delete_post_meta( $new_post_id, 'conditions' ); } do_action( "sellkit_funnels_after_$this->sub_action", $post_id ); if ( empty( $data['sellkit_steps'] ) ) { delete_post_meta( $new_post_id, 'sellkit_steps' ); } return $new_post_id; } /** * Sanitize fields. * * @param array $fields Fields. * * @return array * * @since 1.1.0 */ private function sanitize_fields( $fields ) { $new_fields = []; foreach ( $fields as $key => $field ) { if ( is_array( $fields[ $key ] ) ) { $new_fields[ $key ] = $this->sanitize_fields( $fields[ $key ] ); } $field_value = sanitize_meta( $key, $field, 'post' ); if ( ! is_array( $fields[ $key ] ) ) { $new_fields[ $key ] = $field_value; } } if ( ! empty( $new_fields['conditions'] ) ) { $new_fields['conditions'] = sellkit_condition_row_sanitization( $new_fields['conditions'] ); } if ( ! empty( $new_fields['filters'] ) ) { $new_fields['filters'] = sellkit_filter_row_sanitization( $new_fields['filters'] ); } return $new_fields; } /** * Validate fields. * * @param array $fields Fields. * * @since 1.1.0 */ public function validate_fields( $fields ) { $errors = []; if ( empty( $fields['sellkit_post_title'] ) ) { $errors[] = __( 'Post title should not be empty.', 'sellkit' ); } if ( empty( $errors ) ) { return; } wp_send_json_error( $errors ); } } Sellkit_Admin_Posts::get_instance();