vendor
10 years ago
admin.php
9 years ago
class-wp-stream-author.php
10 years ago
connector.php
9 years ago
connectors.php
9 years ago
context-query.php
10 years ago
dashboard.php
10 years ago
date-interval.php
10 years ago
db.php
9 years ago
filter-input.php
10 years ago
functions.php
10 years ago
install.php
9 years ago
list-table.php
9 years ago
live-update.php
9 years ago
log.php
10 years ago
network.php
10 years ago
query.php
9 years ago
settings.php
9 years ago
db.php
192 lines
| 1 | <?php |
| 2 | |
| 3 | class MainWP_WP_Stream_DB { |
| 4 | |
| 5 | public static $instance; |
| 6 | |
| 7 | public static $table; |
| 8 | |
| 9 | public static $table_meta; |
| 10 | |
| 11 | public static $table_context; |
| 12 | |
| 13 | public function __construct() { |
| 14 | global $wpdb; |
| 15 | |
| 16 | $prefix = apply_filters( 'mainwp_wp_stream_db_tables_prefix', $wpdb->base_prefix ); |
| 17 | |
| 18 | self::$table = $prefix . 'mainwp_stream'; |
| 19 | self::$table_meta = $prefix . 'mainwp_stream_meta'; |
| 20 | self::$table_context = $prefix . 'mainwp_stream_context'; |
| 21 | |
| 22 | $wpdb->mainwp_reports = self::$table; |
| 23 | $wpdb->mainwp_reportsmeta = self::$table_meta; |
| 24 | $wpdb->mainwp_reportscontext = self::$table_context; |
| 25 | |
| 26 | // Hack for get_metadata |
| 27 | $wpdb->recordmeta = self::$table_meta; |
| 28 | } |
| 29 | |
| 30 | public static function get_instance() { |
| 31 | if ( ! self::$instance ) { |
| 32 | $class = __CLASS__; |
| 33 | self::$instance = new $class; |
| 34 | } |
| 35 | |
| 36 | return self::$instance; |
| 37 | } |
| 38 | |
| 39 | public function get_table_names() { |
| 40 | return array( |
| 41 | self::$table, |
| 42 | self::$table_meta, |
| 43 | self::$table_context, |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | public function insert( $recordarr ) { |
| 48 | global $wpdb; |
| 49 | |
| 50 | $recordarr = apply_filters( 'mainwp_wp_stream_record_array', $recordarr ); |
| 51 | |
| 52 | // Allow extensions to handle the saving process |
| 53 | if ( empty( $recordarr ) ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $fields = array( 'object_id', 'site_id', 'blog_id', 'author', 'author_role', 'created', 'summary', 'parent', 'visibility', 'ip' ); |
| 58 | $data = array_intersect_key( $recordarr, array_flip( $fields ) ); |
| 59 | $data = array_filter( $data ); |
| 60 | |
| 61 | // TODO: Check/Validate *required* fields |
| 62 | |
| 63 | $result = $wpdb->insert( |
| 64 | self::$table, |
| 65 | $data |
| 66 | ); |
| 67 | |
| 68 | if ( 1 === $result ) { |
| 69 | $record_id = $wpdb->insert_id; |
| 70 | } else { |
| 71 | do_action( 'mainwp_wp_stream_post_insert_error', $recordarr ); |
| 72 | return $result; |
| 73 | } |
| 74 | |
| 75 | self::$instance->prev_record = $record_id; |
| 76 | |
| 77 | $connector = $recordarr['connector']; |
| 78 | |
| 79 | foreach ( (array) $recordarr['contexts'] as $context => $action ) { |
| 80 | $this->insert_context( $record_id, $connector, $context, $action ); |
| 81 | } |
| 82 | |
| 83 | foreach ( $recordarr['meta'] as $key => $vals ) { |
| 84 | // If associative array, serialize it, otherwise loop on its members |
| 85 | if ( is_array( $vals ) && 0 !== key( $vals ) ) { |
| 86 | $vals = array( $vals ); |
| 87 | } |
| 88 | foreach ( (array) $vals as $val ) { |
| 89 | $val = maybe_serialize( $val ); |
| 90 | if (empty($val)) |
| 91 | continue; |
| 92 | $this->insert_meta( $record_id, $key, $val ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | do_action( 'mainwp_wp_stream_post_inserted', $record_id, $recordarr ); |
| 97 | |
| 98 | return $record_id; |
| 99 | } |
| 100 | |
| 101 | public function insert_context( $record_id, $connector, $context, $action ) { |
| 102 | global $wpdb; |
| 103 | |
| 104 | $result = $wpdb->insert( |
| 105 | self::$table_context, |
| 106 | array( |
| 107 | 'record_id' => $record_id, |
| 108 | 'connector' => $connector, |
| 109 | 'context' => $context, |
| 110 | 'action' => $action, |
| 111 | ) |
| 112 | ); |
| 113 | |
| 114 | return $result; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | public function get_report( $args = array() ) { |
| 119 | if (!is_array($args)) |
| 120 | return false; |
| 121 | global $wpdb; |
| 122 | $where = ""; |
| 123 | $left_join = ""; |
| 124 | if (isset($args['context'])) { |
| 125 | $left_join = " LEFT JOIN " . self::$table_context . " AS `context` ON `stream`.`ID` = `context`.`record_id` " ; |
| 126 | } |
| 127 | |
| 128 | foreach ($args as $key => $value) { |
| 129 | if ($key == 'context') { |
| 130 | $where .= $wpdb->prepare( ' `context`.`context` = %s AND ', $value); |
| 131 | } else |
| 132 | $where .= $wpdb->prepare( ' `stream`.`' . $key . '` = %s AND ', $value); |
| 133 | } |
| 134 | |
| 135 | $where = rtrim($where, "AND "); |
| 136 | |
| 137 | if (!empty($where)) { |
| 138 | $where .= " AND blog_id = " . apply_filters( 'blog_id_logged', is_network_admin() ? 0 : get_current_blog_id() ); |
| 139 | $result = $wpdb->get_row( 'SELECT `stream`.* FROM ' . self::$table . ' AS `stream` ' . $left_join . ' WHERE ' . $where ); |
| 140 | return $result; |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | public function delete_report( $args = array() ) { |
| 146 | if (!is_array($args)) |
| 147 | return false; |
| 148 | |
| 149 | global $wpdb; |
| 150 | $sql = ""; |
| 151 | |
| 152 | foreach ($args as $key => $value) { |
| 153 | $sql .= $key ." = " . $value . " AND "; |
| 154 | } |
| 155 | |
| 156 | $sql = rtrim($sql, "AND "); |
| 157 | |
| 158 | if ( ! empty( $sql ) ) { |
| 159 | $sql .= " AND blog_id = " . apply_filters( 'blog_id_logged', is_network_admin() ? 0 : get_current_blog_id() ); |
| 160 | |
| 161 | $sql = $wpdb->prepare( 'SELECT ID FROM ' . self::$table . ' WHERE %s ', $sql ); |
| 162 | $record_id = $wpdb->get_var( $sql ); |
| 163 | if ($record_id) { |
| 164 | $sql = $wpdb->prepare( 'DELETE FROM ' . self::$table . ' WHERE %s ', $sql ); |
| 165 | $wpdb->query( $sql ); |
| 166 | $sql = $wpdb->prepare( 'DELETE FROM ' . self::$table_context . ' WHERE record_id = %d ', $record_id ); |
| 167 | $wpdb->query( $sql ); |
| 168 | $sql = $wpdb->prepare( 'DELETE FROM ' . self::$table_meta . ' WHERE record_id = %d ', $record_id ); |
| 169 | $wpdb->query( $sql ); |
| 170 | return true; |
| 171 | } |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | public function insert_meta( $record_id, $key, $val ) { |
| 177 | global $wpdb; |
| 178 | |
| 179 | $result = $wpdb->insert( |
| 180 | self::$table_meta, |
| 181 | array( |
| 182 | 'record_id' => $record_id, |
| 183 | 'meta_key' => $key, |
| 184 | 'meta_value' => $val, |
| 185 | ) |
| 186 | ); |
| 187 | |
| 188 | return $result; |
| 189 | } |
| 190 | |
| 191 | } |
| 192 |