* @return mixed */ public static function get( $context_id = false, $column = 'id' ) { global $wpdb; if ( ! $context_id ) { return null; } // Only allow two columns if ( ! in_array( $column, array( 'id', 'affilizz_publication_id' ), true ) ) { $column = 'id'; } if ( ! self::table_exists() ) { return null; } $table_name = esc_sql( $wpdb->prefix . ( \Affilizz\Core::get_publications_database_table() ) ); $column = esc_sql( $column ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Custom plugin table query with allow-listed/escaped identifiers. return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `{$table_name}` WHERE `{$column}` = %s", $context_id ) ) ?: null; } /** * Returns a list of publications recently used by the user. * @author Affilizz * @return array */ public static function recent( $user_id = null, $count = 20 ) { if ( empty( $user_id ) ) { $user_id = ( get_current_user_id() ?? 0 ); } if ( empty( $user_id ) ) { return array(); } if ( ! self::table_exists() ) { return array(); } global $wpdb; $table_name = esc_sql( $wpdb->prefix . ( \Affilizz\Core::get_publications_database_table() ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Custom plugin table name is escaped before interpolation. $publication_contents_query = $wpdb->prepare( "SELECT * FROM `{$table_name}` WHERE user_id = %s ORDER BY `date_modified` DESC LIMIT 0, %d", $user_id, intval( $count ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared above; custom table results are intentionally uncached. $results = $wpdb->get_results( $publication_contents_query ) ?? null; return wp_list_pluck( $results, 'publication_id' ); } /** * Checks if the publication table exists. * @author Affilizz * @since 1.12.0 * @return Boolean Does the table exist ? */ public static function table_exists() { if ( self::$table_exists !== null ) { return self::$table_exists; } global $wpdb; $table_name = esc_sql( $wpdb->base_prefix . ( \Affilizz\Core::get_publications_database_table() ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One-time table existence check cached in a static property. self::$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ) ) == $table_name; return self::$table_exists; } }