return object Meta object with possibly unserialized value. */ public function unserialize_meta( $meta ) { $meta->meta_value = maybe_unserialize( $meta->meta_value ); return $meta; } /** * Retrieve a set of objects by their IDs. * * @access public * * @param string $object_type Object type. * @param array $ids Object IDs. * @return array Array of objects. */ public function get_objects_by_id( $object_type, $ids ) { if ( empty( $ids ) || empty( $object_type ) ) { return array(); } $objects = array(); foreach ( (array) $ids as $id ) { $object = $this->get_object_by_id( $object_type, $id ); // Only add object if we have the object. if ( $object ) { $objects[ $id ] = $object; } } return $objects; } /** * Gets a list of minimum and maximum object ids for each batch based on the given batch size. * * @access public * * @param int $batch_size The batch size for objects. * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. * * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. */ public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { if ( ! $this->table() ) { return false; } $results = array(); $table = $this->table(); $current_max = 0; $current_min = 1; $id_field = $this->id_field(); $replicastore = new Replicastore(); $total = $replicastore->get_min_max_object_id( $id_field, $table, $where_sql, false ); while ( $total->max > $current_max ) { $where = $where_sql ? $where_sql . " AND $id_field > $current_max" : "$id_field > $current_max"; $result = $replicastore->get_min_max_object_id( $id_field, $table, $where, $batch_size ); if ( empty( $result->min ) && empty( $result->max ) ) { // Our query produced no min and max. We can assume the min from the previous query, // and the total max we found in the initial query. $current_max = (int) $total->max; $result = (object) array( 'min' => $current_min, 'max' => $current_max, ); } else { $current_min = (int) $result->min; $current_max = (int) $result->max; } $results[] = $result; } return $results; } /** * Return Total number of objects. * * @param array $config Full Sync config. * * @return int total */ public function total( $config ) { global $wpdb; $table = $this->table(); $where = $this->get_where_sql( $config ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching return (int) $wpdb->get_var( "SELECT COUNT(*) FROM $table WHERE $where" ); } /** * Retrieve the WHERE SQL clause based on the module config. * * @access public * * @param array $config Full sync configuration for this sync module. * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. */ public function get_where_sql( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable return '1=1'; } /** * Filters objects and metadata based on maximum size constraints. * It always allows the first object with its metadata, even if they exceed the limit. * * @access public * * @param string $type The type of objects to filter (e.g., 'post' or 'comment'). * @param array $objects The array of objects to filter (e.g., posts or comments). * @param array $metadata The array of metadata to filter. * @param int $max_meta_size Maximum size for individual objects. * @param int $max_total_size Maximum combined size for objects and metadata. * @return array An array containing the filtered object IDs, filtered objects, and filtered metadata. */ public function filter_objects_and_metadata_by_size( $type, $objects, $metadata, $max_meta_size, $max_total_size ) { $filtered_objects = array(); $filtered_metadata = array(); $filtered_object_ids = array(); $current_size = 0; foreach ( $objects as $object ) { $object_size = strlen( (string) maybe_serialize( $object ) ); $current_metadata = array(); $metadata_size = 0; $id_field = $this->id_field(); $object_id = (int) ( is_object( $object ) ? $object->{$id_field} : $object[ $id_field ] ); foreach ( $metadata as $key => $metadata_item ) { if ( (int) $metadata_item->{$type . '_id'} === $object_id ) { $metadata_item_size = strlen( (string) maybe_serialize( $metadata_item ) ); if ( $metadata_item_size >= $max_meta_size ) { $metadata_item->meta_value = ''; // Trim metadata if too large. $metadata_item_size = strlen( (string) maybe_serialize( $metadata_item ) ); } $current_metadata[] = $metadata_item; $metadata_size += $metadata_item_size; if ( ! empty( $filtered_object_ids ) && ( $current_size + $object_size + $metadata_size ) > $max_total_size ) { break 2; // Exit both loops. } unset( $metadata[ $key ] ); } } // Always allow the first object with metadata. if ( empty( $filtered_object_ids ) || ( $current_size + $object_size + $metadata_size ) <= $max_total_size ) { $filtered_object_ids[] = strval( is_object( $object ) ? $object->{$id_field} : $object[ $id_field ] ); $filtered_objects[] = $object; $filtered_metadata = array_merge( $filtered_metadata, $current_metadata ); $current_size += $object_size + $metadata_size; } else { break; } } return array( $filtered_object_ids, $filtered_objects, $filtered_metadata, ); } }