Making WordPress.org


Ignore:
Timestamp:
02/04/2026 07:07:00 AM (7 months ago)
Author:
dd32
Message:

Oficial WordPress Events: Use INSERT ON DUPLICATE KEY UPDATE rather than REPLACE to allow for other table columns to be set as needed from elsewhere. This also adds a first-seen 'created_at' column.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php

    r14646 r14653  
    102102         */
    103103        public function prime_events_cache() {
    104                 global $wpdb;
    105 
    106104                $this->log( 'started call #' . did_action( 'owpe_prime_events_cache' ) );
    107105
     
    117115                foreach ( $events as $event ) {
    118116                        $row_values = array(
    119                                 'id'              => null,
    120117                                'type'            => $event->type,
    121118                                'source_id'       => $event->source_id,
     
    134131                                'latitude'        => $event->latitude,
    135132                                'longitude'       => $event->longitude,
     133                                'created_at'      => gmdate( 'Y-m-d H:i:s' ),
    136134                        );
    137135
     
    141139                        }
    142140
    143                         /*
    144                          * Insert the events into the table, without creating duplicates
    145                          *
    146                          * Note: Since replace() is matching against a unique key rather than the primary `id` key, it's
    147                          * expected for each row to be deleted and re-inserted, making the IDs increment each time.
    148                          *
    149                          * See http://stackoverflow.com/a/12205366/450127
    150                          */
    151                         $wpdb->replace( self::EVENTS_TABLE, $row_values );
     141                        $keys_not_to_update = array(
     142                                'created_at',
     143                        );
     144
     145                        $this->insert_on_duplicate_key_update(
     146                                $wpdb->prefix . self::EVENTS_TABLE,
     147                                $row_values,
     148                                array_diff( array_keys( $row_values ), $keys_not_to_update )
     149                        );
    152150                }
    153151
    154152                $this->log( "finished job\n\n" );
     153        }
     154
     155        /**
     156         * INSERT INTO ... ON DUPLICATE KEY UPDATE ... helper
     157         *
     158         * @param string $table       The table to insert into.
     159         * @param array  $data        Associative array of field => value pairs to insert.
     160         * @param array  $update_keys Array of field names to update on duplicate key.
     161         */
     162        protected function insert_on_duplicate_key_update( string $table, array $data, array $update_keys ) {
     163                global $wpdb;
     164
     165                $field_placeholders = [];
     166                $value_placeholders = [];
     167                $duplicate_sets     = [];
     168                $field_args         = [];
     169                $values_args        = [];
     170                $duplicate_args     = [];
     171                foreach ( $data as $field => $value ) {
     172                        $field_placeholders[] = '%i';
     173                        $value_placeholders[] = '%s';
     174
     175                        $field_args[]  = $field;
     176                        $values_args[] = $value;
     177
     178                        if ( $update_keys && in_array( $field, $update_keys, true ) ) {
     179                                $duplicate_sets[] = '%i = VALUES(%i)';
     180                                $duplicate_args[] = $field;
     181                                $duplicate_args[] = $field;
     182                        }
     183                }
     184
     185                $field_placeholders = implode( ', ', $field_placeholders );
     186                $value_placeholders = implode( ', ', $value_placeholders );
     187                $duplicate_sets     = implode( ', ', $duplicate_placehodlers );
     188
     189                return $wpdb->query( $wpdb->prepare(
     190                        "INSERT INTO %i ( {$field_placeholders} ) VALUES ( {$value_placeholders} ) ON DUPLICATE KEY UPDATE {$duplicate_sets}",
     191                        $table,
     192                        ...$field_args,
     193                        ...$values_args,
     194                        ...$duplicate_args
     195                ) );
    155196        }
    156197
Note: See TracChangeset for help on using the changeset viewer.