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_Form_Repository { /** * Table name. * * @var string */ private $table_name; /** * In-memory cache for loaded forms. * Prevents duplicate database queries during a single request. * * @var array */ private static $cache = array(); /** * Constructor */ public function __construct() { global $wpdb; $this->table_name = $wpdb->prefix . 'affcf_forms'; } // 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 form * * @param AFFCF_Form_Model $form Form model. * @return int|WP_Error Form ID or error. */ public function create( AFFCF_Form_Model $form ) { global $wpdb; // Validate. $validation = $form->validate(); if ( true !== $validation ) { return new WP_Error( 'validation_failed', implode( ' ', $validation ) ); } // Check for duplicate shortcode. if ( $this->shortcode_exists( $form->shortcode ) ) { return new WP_Error( 'duplicate_shortcode', __( 'Shortcode already exists.', 'airy-frontend-forms' ) ); } $data = $form->to_array(); $data['created_at'] = current_time( 'mysql' ); $data['updated_at'] = current_time( 'mysql' ); $result = $wpdb->insert( $this->table_name, $data, array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s' ) ); if ( $result ) { $new_id = $wpdb->insert_id; // Populate cache with the newly created form. $form->id = $new_id; self::$cache[ $new_id ] = $form; do_action( 'affcf_form_created', $new_id ); return $new_id; } return new WP_Error( 'db_error', __( 'Failed to create form.', 'airy-frontend-forms' ) ); } /** * Update form * * @param int $id Form ID. * @param AFFCF_Form_Model $form Form model. * @return bool|WP_Error Success or error. */ public function update( $id, AFFCF_Form_Model $form ) { global $wpdb; // Validate. $validation = $form->validate(); if ( true !== $validation ) { return new WP_Error( 'validation_failed', implode( ' ', $validation ) ); } // Check form exists. if ( ! $this->exists( $id ) ) { return new WP_Error( 'not_found', __( 'Form not found.', 'airy-frontend-forms' ) ); } $data = $form->to_array(); $data['updated_at'] = current_time( 'mysql' ); $result = $wpdb->update( $this->table_name, $data, array( 'id' => $id ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s' ), array( '%d' ) ); if ( false !== $result ) { // Clear cache for this form to prevent stale data. if ( isset( self::$cache[ $id ] ) ) { unset( self::$cache[ $id ] ); } do_action( 'affcf_form_updated', $id ); return true; } return new WP_Error( 'db_error', __( 'Failed to update form.', 'airy-frontend-forms' ) ); } /** * Get form by ID * * @param int $id Form ID. * @return AFFCF_Form_Model|null. */ public function get_by_id( $id ) { // Check cache first to avoid duplicate queries. if ( isset( self::$cache[ $id ] ) ) { return self::$cache[ $id ]; } global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is plugin-controlled via constructor $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE id = %d", $id ), ARRAY_A ); $form = $row ? new AFFCF_Form_Model( $row ) : null; // Store in cache for subsequent calls. if ( $form ) { self::$cache[ $id ] = $form; } return $form; } /** * Get form by shortcode * * @param string $shortcode Shortcode. * @return AFFCF_Form_Model|null. */ public function get_by_shortcode( $shortcode ) { global $wpdb; $row = $wpdb->get_row( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is plugin-controlled via constructor $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE shortcode = %s", $shortcode ), ARRAY_A ); return $row ? new AFFCF_Form_Model( $row ) : null; } /** * Get form by slug * * @param string $slug Form slug. * @return AFFCF_Form_Model|null. */ public function get_by_slug( $slug ) { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is plugin-controlled via constructor $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE form_slug = %s", $slug ), ARRAY_A ); return $row ? new AFFCF_Form_Model( $row ) : null; } /** * Get all forms * * @param array $args Query arguments. * @return array Array of AFFCF_Form_Model objects. */ public function get_all( $args = array() ) { global $wpdb; $defaults = array( 'status' => 'all', 'post_type' => '', 'orderby' => 'id', 'order' => 'DESC', 'limit' => 0, 'offset' => 0, ); $args = wp_parse_args( $args, $defaults ); $where = array( '1=1' ); if ( 'all' !== $args['status'] ) { $where[] = $wpdb->prepare( 'status = %s', $args['status'] ); } if ( ! empty( $args['post_type'] ) ) { $where[] = $wpdb->prepare( 'post_type = %s', $args['post_type'] ); } $where_clause = implode( ' AND ', $where ); $order_clause = sprintf( 'ORDER BY %s %s', sanitize_sql_orderby( $args['orderby'] ), 'ASC' === $args['order'] ? 'ASC' : 'DESC' ); $limit_clause = ''; if ( $args['limit'] > 0 ) { $limit_clause = $wpdb->prepare( 'LIMIT %d', $args['limit'] ); if ( $args['offset'] > 0 ) { $limit_clause .= $wpdb->prepare( ' OFFSET %d', $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_clause} {$order_clause} {$limit_clause}", ARRAY_A ); return array_map( function ( $row ) { return new AFFCF_Form_Model( $row ); }, $rows ); } /** * Delete form * * @param int $id Form ID. * @return bool Success. */ public function delete( $id ) { global $wpdb; if ( ! $this->exists( $id ) ) { return false; } do_action( 'affcf_before_delete_form', $id ); $result = $wpdb->delete( $this->table_name, array( 'id' => $id ), array( '%d' ) ); if ( $result ) { // Clear cache for this form. if ( isset( self::$cache[ $id ] ) ) { unset( self::$cache[ $id ] ); } do_action( 'affcf_form_deleted', $id ); return true; } return false; } /** * Check if form exists * * @param int $id Form ID. * @return bool. */ public function exists( $id ) { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is plugin-controlled via constructor $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$this->table_name} WHERE id = %d", $id ) ); return $exists > 0; } /** * Generate next unique shortcode * * @return string Unique shortcode. */ public function generate_next_shortcode() { global $wpdb; $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$this->table_name}" ); $next_number = $count + 1; while ( $this->shortcode_exists( "shortcode_{$next_number}" ) ) { ++$next_number; } return "shortcode_{$next_number}"; } /** * Check if shortcode exists * * @param string $shortcode Shortcode to check. * @return bool. */ public function shortcode_exists( $shortcode ) { global $wpdb; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name is plugin-controlled via constructor $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$this->table_name} WHERE shortcode = %s", $shortcode ) ); return $exists > 0; } /** * Get form count * * @param array $args Query arguments. * @return int Count. */ public function count( $args = array() ) { global $wpdb; $defaults = array( 'status' => 'all', 'post_type' => '', ); $args = wp_parse_args( $args, $defaults ); $where = array( '1=1' ); if ( 'all' !== $args['status'] ) { $where[] = $wpdb->prepare( 'status = %s', $args['status'] ); } if ( ! empty( $args['post_type'] ) ) { $where[] = $wpdb->prepare( 'post_type = %s', $args['post_type'] ); } $where_clause = implode( ' AND ', $where ); return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$this->table_name} WHERE {$where_clause}" ); } /** * Get forms by post type * * @param string $post_type Post type. * @return array Array of AFFCF_Form_Model objects. */ public function get_by_post_type( $post_type ) { return $this->get_all( array( 'post_type' => $post_type ) ); } /** * Toggle form status * * @param int $id Form ID. * @return bool Success. */ public function toggle_status( $id ) { global $wpdb; $current_status = $wpdb->get_var( $wpdb->prepare( "SELECT status FROM {$this->table_name} WHERE id = %d", $id ) ); if ( ! $current_status ) { return false; } $new_status = ( 'active' === $current_status ) ? 'inactive' : 'active'; $result = $wpdb->update( $this->table_name, array( 'status' => $new_status, 'updated_at' => current_time( 'mysql' ), ), array( 'id' => $id ), array( '%s', '%s' ), array( '%d' ) ); if ( false !== $result ) { do_action( 'affcf_form_status_changed', $id, $new_status, $current_status ); return true; } return false; } // phpcs:enable }