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_Draft_Repository { /** * Table name. * * @var string */ private $table_name; /** * Constructor */ public function __construct() { global $wpdb; $this->table_name = $wpdb->prefix . 'affcf_drafts'; } // 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. /** * Save or update draft * * @param AFFCF_Draft_Model $draft Draft model. * @return int|WP_Error Draft ID or error. */ public function save( AFFCF_Draft_Model $draft ) { global $wpdb; // Validate. $validation = $draft->validate(); if ( true !== $validation ) { return new WP_Error( 'validation_failed', implode( ' ', $validation ) ); } // Check if draft exists. $existing = $this->get_by_user_and_form( $draft->user_id, $draft->form_id, $draft->post_id ); $data = $draft->to_array(); if ( $existing ) { // Update existing draft. $data['last_saved'] = current_time( 'mysql' ); $result = $wpdb->update( $this->table_name, $data, array( 'user_id' => $draft->user_id, 'form_id' => $draft->form_id, 'post_id' => $draft->post_id ? $draft->post_id : null, ), array( '%d', '%d', '%d', '%s', '%s', '%s' ), array( '%d', '%d', '%d' ) ); return false !== $result ? $existing->id : new WP_Error( 'db_error', __( 'Failed to update draft.', 'airy-frontend-forms' ) ); } else { // Create new draft. $data['created_at'] = current_time( 'mysql' ); $data['last_saved'] = current_time( 'mysql' ); $result = $wpdb->insert( $this->table_name, $data, array( '%d', '%d', '%d', '%s', '%s', '%s', '%s' ) ); return $result ? $wpdb->insert_id : new WP_Error( 'db_error', __( 'Failed to create draft.', 'airy-frontend-forms' ) ); } } /** * Get draft by user and form * * @param int $user_id User ID. * @param int $form_id Form ID. * @param int $post_id Post ID (optional). * @return AFFCF_Draft_Model|null. */ public function get_by_user_and_form( $user_id, $form_id, $post_id = null ) { global $wpdb; if ( $post_id ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is plugin-controlled via constructor // 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 user_id = %d AND form_id = %d AND post_id = %d", $user_id, $form_id, $post_id ), ARRAY_A ); } else { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is plugin-controlled via constructor // 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 user_id = %d AND form_id = %d AND post_id IS NULL", $user_id, $form_id ), ARRAY_A ); } return $row ? new AFFCF_Draft_Model( $row ) : null; } /** * Get all drafts by user * * @param int $user_id User ID. * @return array Array of AFFCF_Draft_Model objects. */ public function get_by_user( $user_id ) { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is plugin-controlled via constructor $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE user_id = %d ORDER BY last_saved DESC", $user_id ), ARRAY_A ); return array_map( function ( $row ) { return new AFFCF_Draft_Model( $row ); }, $rows ); } /** * Delete draft * * @param int $id Draft ID. * @return bool Success. */ public function delete( $id ) { global $wpdb; return $wpdb->delete( $this->table_name, array( 'id' => $id ), array( '%d' ) ) !== false; } /** * Delete draft by user and form * * @param int $user_id User ID. * @param int $form_id Form ID. * @param int $post_id Post ID (optional). * @return bool Success. */ public function delete_by_user_and_form( $user_id, $form_id, $post_id = null ) { global $wpdb; $where = array( 'user_id' => $user_id, 'form_id' => $form_id, ); if ( $post_id ) { $where['post_id'] = $post_id; } else { $where['post_id'] = null; } return $wpdb->delete( $this->table_name, $where, array( '%d', '%d', '%d' ) ) !== false; } /** * Clean old drafts * * @param int $days Days to keep. * @return int Number of drafts deleted. */ public function clean_old_drafts( $days = 30 ) { global $wpdb; $date = gmdate( 'Y-m-d H:i:s', strtotime( "-{$days} days" ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is plugin-controlled via constructor return $wpdb->query( $wpdb->prepare( "DELETE FROM {$this->table_name} WHERE last_saved < %s", $date ) ); } /** * Get draft count by user * * @param int $user_id User ID. * @return int Count. */ public function count_by_user( $user_id ) { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is plugin-controlled via constructor return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$this->table_name} WHERE user_id = %d", $user_id ) ); } // phpcs:enable }