PluginProbe
Stream – Activity Log & Audit Trail / 2.0.2
Stream – Activity Log & Audit Trail v2.0.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-wp-stream-db.php

class-wp-stream-db.php in Stream – Activity Log & Audit Trail 2.0.2, at classes/class-wp-stream-db.php

162 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WP_Stream_DB {
4
5 /**
6 * Meta information returned in the last query
7 *
8 * @var mixed
9 */
10 public $query_meta = false;
11
12 /**
13 * Store records
14 *
15 * @param array $records
16 *
17 * @return mixed True if updated, false|WP_Error if not
18 */
19 public function store( $records ) {
20 // Take only what's ours!
21 $valid_keys = get_class_vars( 'WP_Stream_Record' );
22
23 // Fill in defaults
24 $defaults = array(
25 'type' => 'stream',
26 'site_id' => 1,
27 'blog_id' => 0,
28 'object_id' => 0,
29 'author' => 0,
30 'author_role' => '',
31 'visibility' => 'publish',
32 'ip' => '',
33 );
34
35 foreach ( $records as $key => $record ) {
36 $records[ $key ] = array_intersect_key( $record, $valid_keys );
37 $records[ $key ] = array_filter( $record );
38 $records[ $key ] = wp_parse_args( $record, $defaults );
39 }
40
41 /**
42 * Allows modification of record information just before logging occurs.
43 *
44 * @since 0.0.2
45 *
46 * @param array $records An array of record data.
47 */
48 $records = apply_filters( 'wp_stream_record_array', $records );
49
50 // Allow extensions to handle the saving process
51 if ( empty( $records ) ) {
52 return false;
53 }
54
55 // TODO: Check/Validate *required* fields
56
57 $result = $this->insert( $records );
58
59 if ( $result && ! is_wp_error( $result ) ) {
60 /**
61 * Fires when A Post is inserted
62 *
63 * @param int $record_id Inserted record ID
64 * @param array $recordarr Array of information on this record
65 */
66 do_action( 'wp_stream_records_inserted', $records );
67 }
68
69 return true;
70 }
71
72 /**
73 * Insert a new record
74 *
75 * @internal Used by store()
76 *
77 * @param array $records
78 *
79 * @return object $response The inserted records
80 */
81 private function insert( array $records ) {
82 return WP_Stream::$api->new_records( $records );
83 }
84
85 /**
86 * Query records
87 *
88 * @internal Used by WP_Stream_Query, and is not designed to be called explicitly
89 *
90 * @param array $query Query body.
91 * @param array $fields Returns specified fields only.
92 *
93 * @return array List of records that match query
94 */
95 public function query( $query, $fields ) {
96 $response = WP_Stream::$api->search( $query, $fields );
97
98 if ( empty( $response ) || ! isset( $response->meta ) || ! isset( $response->records ) ) {
99 return false;
100 }
101
102 $this->query_meta = $response->meta;
103
104 $results = (array) $response->records;
105
106 /**
107 * Allows developers to change the final result set of records
108 *
109 * @param array $results
110 * @param array $query
111 * @param array $fields
112 *
113 * @return array Filtered array of record results
114 */
115 return apply_filters( 'wp_stream_query_results', $results, $query, $fields );
116 }
117
118 /**
119 * Get total count of the last query using query() method
120 *
121 * @return integer Total item count
122 */
123 public function get_found_rows() {
124 if ( ! isset( $this->query_meta->total ) ) {
125 return 0;
126 }
127 return $this->query_meta->total;
128 }
129
130 /**
131 * Get meta data for last query using query() method
132 *
133 * @return array Meta data for query
134 */
135 public function get_query_meta() {
136 return $this->query_meta;
137 }
138
139 /**
140 * Returns array of existing values for requested field.
141 * Used to fill search filters with only used items, instead of all items.
142 *
143 * @param string Requested field (i.e., 'context')
144 *
145 * @return array Array of distinct values
146 */
147 public function get_distinct_field_values( $field ) {
148 $query['aggregations']['fields']['terms']['field'] = $field;
149
150 $values = array();
151 $response = WP_Stream::$api->search( $query, array( $field ) );
152
153 if ( isset( $response->meta->aggregations->fields->buckets ) ) {
154 foreach ( $response->meta->aggregations->fields->buckets as $field ) {
155 $values[] = $field->key;
156 }
157 }
158
159 return $values;
160 }
161 }
162