/** * Get a chunk of records formatted for Stream API ingestion * * @param int $limit The number of rows to query * * @return mixed An array of record arrays, or FALSE if no records were found */ private static function get_records($limit = null) { $limit = is_int($limit) ? $limit : self::$limit; global $wpdb; $records = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT s.*, sc.connector, sc.context, sc.action\n\t\t\t\tFROM {$wpdb->base_prefix}stream AS s, {$wpdb->base_prefix}stream_context AS sc\n\t\t\t\tWHERE s.site_id = %d\n\t\t\t\t\tAND s.blog_id = %d\n\t\t\t\t\tAND s.type = 'stream'\n\t\t\t\t\tAND sc.record_id = s.ID\n\t\t\t\tORDER BY s.created DESC\n\t\t\t\tLIMIT %d\n\t\t\t\t", self::$site_id, self::$blog_id, $limit), ARRAY_A); if (empty($records)) { return false; } self::$_records = array(); foreach ($records as $record => $data) { $stream_meta = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM {$wpdb->base_prefix}stream_meta WHERE record_id = %d", $records[$record]['ID']), ARRAY_A); $stream_meta_output = array(); $author_meta_output = array(); foreach ($stream_meta as $key => $meta) { if ('author_meta' === $meta['meta_key'] && !empty($meta['meta_value'])) { $author_meta_output = maybe_unserialize($meta['meta_value']); unset($stream_meta[$key]); continue; } // Unserialize meta first so we can then check for malformed serialized strings $stream_meta_output[$meta['meta_key']] = maybe_unserialize($meta['meta_value']); // If any serialized data is still lingering in the meta value that means it's malformed and should be removed if (is_string($stream_meta_output[$meta['meta_key']]) && 1 === preg_match('/(a|O) ?\\x3a ?[0-9]+ ?\\x3a ?\\x7b/', $stream_meta_output[$meta['meta_key']])) { unset($stream_meta_output[$meta['meta_key']]); continue; } // All meta must be strings, so serialize any array meta values again $stream_meta_output[$meta['meta_key']] = (string) maybe_serialize($stream_meta_output[$meta['meta_key']]); } // All author meta must be strings array_walk($author_meta_output, function (&$v) { $v = (string) $v; }); $records[$record]['stream_meta'] = $stream_meta_output; $records[$record]['author_meta'] = $author_meta_output; self::$_records[] = $records[$record]; $records[$record]['created'] = wp_stream_get_iso_8601_extended_date(strtotime($records[$record]['created'])); unset($records[$record]['ID']); unset($records[$record]['parent']); // Ensure required fields always exist $records[$record]['site_id'] = !empty($records[$record]['site_id']) ? $records[$record]['site_id'] : 1; $records[$record]['blog_id'] = !empty($records[$record]['blog_id']) ? $records[$record]['blog_id'] : 1; $records[$record]['object_id'] = !empty($records[$record]['object_id']) ? $records[$record]['object_id'] : 0; $records[$record]['author'] = !empty($records[$record]['author']) ? $records[$record]['author'] : 0; $records[$record]['author_role'] = !empty($records[$record]['author_role']) ? $records[$record]['author_role'] : ''; $records[$record]['ip'] = !empty($records[$record]['ip']) ? $records[$record]['ip'] : ''; } return $records; }