table_name which is set from * $wpdb->prefix in the constructor. This is plugin-controlled, not user input, * so interpolation is safe. PHPCS warnings are suppressed where needed. * * @package Airy_Frontend_Forms */ class AFFCF_Submission_Repository { /** * Table name. * * @var string */ private $table_name; /** * Constructor */ public function __construct() { global $wpdb; $this->table_name = $wpdb->prefix . 'affcf_submissions'; } // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching // All SQL queries below use $this->table_name which is plugin-controlled via constructor above. // Table name interpolation is safe as it's derived from $wpdb->prefix, not user input. /** * Create submission * * @param AFFCF_Submission_Model $submission Submission model. * @return int|WP_Error Submission ID or error. */ public function create( AFFCF_Submission_Model $submission ) { global $wpdb; // Validate. $validation = $submission->validate(); if ( true !== $validation ) { return new WP_Error( 'validation_failed', implode( ' ', $validation ) ); } $data = $submission->to_array(); $data['submission_date'] = current_time( 'mysql' ); $result = $wpdb->insert( $this->table_name, $data, array( '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%f' ) ); if ( $result ) { do_action( 'affcf_submission_created', $wpdb->insert_id ); return $wpdb->insert_id; } return new WP_Error( 'db_error', __( 'Failed to create submission.', 'airy-frontend-forms' ) ); } /** * Get submission by ID * * @param int $id Submission ID. * @return AFFCF_Submission_Model|null. */ public function get_by_id( $id ) { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table name is plugin-controlled via constructor $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE id = %d", $id ), ARRAY_A ); return $row ? new AFFCF_Submission_Model( $row ) : null; } /** * Get submissions by user * * @param int $user_id User ID. * @param array $args Query arguments. * @return array Array of AFFCF_Submission_Model objects. */ public function get_by_user( $user_id, $args = array() ) { global $wpdb; $defaults = array( 'form_id' => 0, 'limit' => 20, 'offset' => 0, ); $args = wp_parse_args( $args, $defaults ); $where = $wpdb->prepare( 'user_id = %d', $user_id ); if ( $args['form_id'] > 0 ) { $where .= $wpdb->prepare( ' AND form_id = %d', $args['form_id'] ); } $limit_clause = ''; if ( $args['limit'] > 0 ) { $limit_clause = $wpdb->prepare( 'LIMIT %d OFFSET %d', $args['limit'], $args['offset'] ); } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table name is plugin-controlled via constructor $rows = $wpdb->get_results( "SELECT * FROM {$this->table_name} WHERE {$where} ORDER BY submission_date DESC {$limit_clause}", ARRAY_A ); return array_map( function ( $row ) { return new AFFCF_Submission_Model( $row ); }, $rows ); } /** * Get submissions by form * * @param int $form_id Form ID. * @param array $args Query arguments. * @return array Array of AFFCF_Submission_Model objects. */ public function get_by_form( $form_id, $args = array() ) { global $wpdb; $defaults = array( 'limit' => 20, 'offset' => 0, ); $args = wp_parse_args( $args, $defaults ); $limit_clause = ''; if ( $args['limit'] > 0 ) { $limit_clause = $wpdb->prepare( 'LIMIT %d OFFSET %d', $args['limit'], $args['offset'] ); } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table name is plugin-controlled via constructor $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE form_id = %d ORDER BY submission_date DESC {$limit_clause}", $form_id ), ARRAY_A ); return array_map( function ( $row ) { return new AFFCF_Submission_Model( $row ); }, $rows ); } /** * Count submissions * * @param array $args Query arguments. * @return int Count. */ public function count( $args = array() ) { global $wpdb; $defaults = array( 'user_id' => 0, 'form_id' => 0, 'submission_type' => '', ); $args = wp_parse_args( $args, $defaults ); $where = array( '1=1' ); if ( $args['user_id'] > 0 ) { $where[] = $wpdb->prepare( 'user_id = %d', $args['user_id'] ); } if ( $args['form_id'] > 0 ) { $where[] = $wpdb->prepare( 'form_id = %d', $args['form_id'] ); } if ( ! empty( $args['submission_type'] ) ) { $where[] = $wpdb->prepare( 'submission_type = %s', $args['submission_type'] ); } $where_clause = implode( ' AND ', $where ); return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$this->table_name} WHERE {$where_clause}" ); } /** * Delete submission * * @param int $id Submission ID. * @return bool Success. */ public function delete( $id ) { global $wpdb; return $wpdb->delete( $this->table_name, array( 'id' => $id ), array( '%d' ) ) !== false; } // phpcs:enable }