Changeset 14653
- Timestamp:
- 02/04/2026 07:07:00 AM (2 months ago)
- 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 102 102 */ 103 103 public function prime_events_cache() { 104 global $wpdb;105 106 104 $this->log( 'started call #' . did_action( 'owpe_prime_events_cache' ) ); 107 105 … … 117 115 foreach ( $events as $event ) { 118 116 $row_values = array( 119 'id' => null,120 117 'type' => $event->type, 121 118 'source_id' => $event->source_id, … … 134 131 'latitude' => $event->latitude, 135 132 'longitude' => $event->longitude, 133 'created_at' => gmdate( 'Y-m-d H:i:s' ), 136 134 ); 137 135 … … 141 139 } 142 140 143 /*144 * Insert the events into the table, without creating duplicates145 *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/450127150 */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 ); 152 150 } 153 151 154 152 $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 ) ); 155 196 } 156 197
Note: See TracChangeset
for help on using the changeset viewer.