class-admin.php
3 months ago
class-author.php
3 months ago
class-cli.php
3 months ago
class-connector.php
3 months ago
class-connectors.php
3 months ago
class-date-interval.php
3 months ago
class-db-driver-wpdb.php
2 months ago
class-db-driver.php
3 months ago
class-db.php
3 months ago
class-export.php
3 months ago
class-exporter.php
3 months ago
class-filter-input.php
3 months ago
class-form-generator.php
3 months ago
class-install.php
3 months ago
class-list-table.php
3 months ago
class-live-update.php
3 months ago
class-log.php
2 months ago
class-mainwp-child-report-helper.php
3 months ago
class-network.php
3 months ago
class-plugin.php
3 months ago
class-preview-list-table.php
3 months ago
class-query.php
3 months ago
class-record.php
3 months ago
class-settings.php
3 months ago
class-uninstall.php
3 months ago
class-db.php
255 lines
| 1 | <?php |
| 2 | /** MainWP Child Reports Database. */ |
| 3 | |
| 4 | namespace WP_MainWP_Stream; |
| 5 | |
| 6 | // Exit if accessed directly. |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Class DB. |
| 13 | * |
| 14 | * @package WP_MainWP_Stream |
| 15 | */ |
| 16 | class DB { |
| 17 | /** |
| 18 | * Hold the Driver class |
| 19 | * |
| 20 | * @var DB_Driver |
| 21 | */ |
| 22 | public $driver; |
| 23 | |
| 24 | /** |
| 25 | * Number of records in last request |
| 26 | * |
| 27 | * @var int |
| 28 | */ |
| 29 | protected $found_records_count = 0; |
| 30 | |
| 31 | /** @var $wpdb wpdb */ |
| 32 | private $wpdb; |
| 33 | |
| 34 | /** |
| 35 | * DB constructor. |
| 36 | * |
| 37 | * Run each time the class is called. |
| 38 | * |
| 39 | * @param DB_Driver $driver Driver we want to use. |
| 40 | */ |
| 41 | public function __construct( $driver ) { |
| 42 | $this->driver = $driver; |
| 43 | |
| 44 | /** @global object $wpdb WordPress Database instance. */ |
| 45 | global $wpdb; |
| 46 | |
| 47 | $this->wpdb = &$wpdb; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Insert a record |
| 52 | * |
| 53 | * @param array $record |
| 54 | * |
| 55 | * @return int |
| 56 | */ |
| 57 | public function insert( $record ) { |
| 58 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Filter allows modification of record information |
| 64 | * |
| 65 | * @param array $record |
| 66 | * |
| 67 | * @return array |
| 68 | */ |
| 69 | $record = apply_filters( 'wp_mainwp_stream_record_array', $record ); |
| 70 | |
| 71 | if ( empty( $record ) ) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | $fields = array( 'object_id', 'site_id', 'blog_id', 'user_id', 'user_role', 'created', 'summary', 'ip', 'connector', 'context', 'action', 'meta' ); |
| 76 | $data = array_intersect_key( $record, array_flip( $fields ) ); |
| 77 | |
| 78 | $record_id = $this->driver->insert_record( $data ); |
| 79 | |
| 80 | if ( ! $record_id ) { |
| 81 | /** |
| 82 | * Fires on a record insertion error |
| 83 | * |
| 84 | * @param array $record |
| 85 | * @param mixed $result |
| 86 | */ |
| 87 | do_action( 'wp_mainwp_stream_record_insert_error', $record, false ); |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Fires after a record has been inserted |
| 94 | * |
| 95 | * @param int $record_id |
| 96 | * @param array $record |
| 97 | */ |
| 98 | do_action( 'wp_mainwp_stream_record_inserted', $record_id, $record ); |
| 99 | |
| 100 | // Invalidate contexts/connectors cache after new record insertion. |
| 101 | wp_cache_delete( 'mainwp_stream_contexts_connectors' ); |
| 102 | |
| 103 | return absint( $record_id ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns array of existing values for requested column. |
| 108 | * Used to fill search filters with only used items, instead of all items. |
| 109 | * |
| 110 | * GROUP BY allows query to find just the first occurrence of each value in the column, |
| 111 | * increasing the efficiency of the query. |
| 112 | * |
| 113 | * @see assemble_records |
| 114 | * @since 1.0.4 |
| 115 | * |
| 116 | * @param string $column |
| 117 | * |
| 118 | * @return array |
| 119 | */ |
| 120 | public function existing_records( $column ) { |
| 121 | // Sanitize column |
| 122 | $allowed_columns = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' ); |
| 123 | if ( ! in_array( $column, $allowed_columns, true ) ) { |
| 124 | return array(); |
| 125 | } |
| 126 | |
| 127 | $rows = $this->driver->get_column_values( $column ); |
| 128 | |
| 129 | if ( is_array( $rows ) && ! empty( $rows ) ) { |
| 130 | $output_array = array(); |
| 131 | |
| 132 | foreach ( $rows as $row ) { |
| 133 | foreach ( $row as $cell => $value ) { |
| 134 | $output_array[ $value ] = $value; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return (array) $output_array; |
| 139 | } |
| 140 | |
| 141 | $column = sprintf( 'stream_%s', $column ); |
| 142 | |
| 143 | $term_labels = wp_mainwp_stream_get_instance()->connectors->term_labels; |
| 144 | return isset( $term_labels[ $column ] ) ? $term_labels[ $column ] : array(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get stream records |
| 149 | * |
| 150 | * @param array Query args |
| 151 | * |
| 152 | * @return array Stream Records |
| 153 | */ |
| 154 | public function get_records( $args ) { |
| 155 | $defaults = array( |
| 156 | // Search param |
| 157 | 'search' => null, |
| 158 | 'search_field' => 'summary', |
| 159 | 'record_after' => null, // Deprecated, use date_after instead |
| 160 | // Date-based filters |
| 161 | 'date' => null, // Ex: 2015-07-01 |
| 162 | 'date_from' => null, // Ex: 2015-07-01 |
| 163 | 'date_to' => null, // Ex: 2015-07-01 |
| 164 | 'date_after' => null, // Ex: 2015-07-01T15:19:21+00:00 |
| 165 | 'date_before' => null, // Ex: 2015-07-01T15:19:21+00:00 |
| 166 | // Record ID filters |
| 167 | 'record' => null, |
| 168 | 'record__in' => array(), |
| 169 | 'record__not_in' => array(), |
| 170 | // Pagination params |
| 171 | 'records_per_page' => get_option( 'posts_per_page', 20 ), |
| 172 | 'paged' => 1, |
| 173 | // Order |
| 174 | 'order' => 'desc', |
| 175 | 'orderby' => 'date', |
| 176 | // Fields selection |
| 177 | 'fields' => array(), |
| 178 | 'created' => null, |
| 179 | ); |
| 180 | |
| 181 | // Additional property fields |
| 182 | $properties = array( |
| 183 | 'user_id' => null, |
| 184 | 'user_role' => null, |
| 185 | 'ip' => null, |
| 186 | 'object_id' => null, |
| 187 | 'site_id' => null, |
| 188 | 'blog_id' => null, |
| 189 | 'connector' => null, |
| 190 | 'context' => null, |
| 191 | 'action' => null, |
| 192 | ); |
| 193 | |
| 194 | /** |
| 195 | * Filter allows additional query properties to be added |
| 196 | * |
| 197 | * @return array Array of query properties |
| 198 | */ |
| 199 | $properties = apply_filters( 'wp_mainwp_stream_query_properties', $properties ); |
| 200 | |
| 201 | // Add property fields to defaults, including their __in/__not_in variations |
| 202 | foreach ( $properties as $property => $default ) { |
| 203 | if ( ! isset( $defaults[ $property ] ) ) { |
| 204 | $defaults[ $property ] = $default; |
| 205 | } |
| 206 | |
| 207 | $defaults[ "{$property}__in" ] = array(); |
| 208 | $defaults[ "{$property}__not_in" ] = array(); |
| 209 | } |
| 210 | |
| 211 | $args = wp_parse_args( $args, $defaults ); |
| 212 | |
| 213 | /** |
| 214 | * Filter allows additional arguments to query $args |
| 215 | * |
| 216 | * @return array Array of query arguments |
| 217 | */ |
| 218 | $args = apply_filters( 'wp_mainwp_stream_query_args', $args ); |
| 219 | |
| 220 | $result = (array) $this->driver->get_records( $args ); |
| 221 | $this->found_records_count = isset( $result['count'] ) ? $result['count'] : 0; |
| 222 | |
| 223 | return empty( $result['items'] ) ? array() : $result['items']; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Helper function, backwards compatibility |
| 228 | * |
| 229 | * @param array $args Query args |
| 230 | * |
| 231 | * @return array Stream Records |
| 232 | */ |
| 233 | public function query( $args ) { |
| 234 | return $this->get_records( $args ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Return the number of records found in last request |
| 239 | * |
| 240 | * return int |
| 241 | */ |
| 242 | public function get_found_records_count() { |
| 243 | return $this->found_records_count; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Public getter to return table names |
| 248 | * |
| 249 | * @return array |
| 250 | */ |
| 251 | public function get_table_names() { |
| 252 | return $this->driver->get_table_names(); |
| 253 | } |
| 254 | } |
| 255 |