vendor
7 years ago
admin.php
7 years ago
class-wp-stream-author.php
10 years ago
connector.php
7 years ago
connectors.php
7 years ago
context-query.php
7 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
7 years ago
live-update.php
9 years ago
log.php
10 years ago
network.php
7 years ago
query.php
7 years ago
settings.php
7 years ago
admin.php
596 lines
| 1 | <?php |
| 2 | |
| 3 | class MainWP_WP_Stream_Admin { |
| 4 | |
| 5 | public static $screen_id = array(); |
| 6 | |
| 7 | public static $list_table = null; |
| 8 | |
| 9 | public static $disable_access = false; |
| 10 | public static $brandingTitle = null; |
| 11 | |
| 12 | const ADMIN_BODY_CLASS = 'mainwp_wp_stream_screen'; |
| 13 | const RECORDS_PAGE_SLUG = 'mainwp-reports-page'; |
| 14 | const SETTINGS_PAGE_SLUG = 'mainwp_wp_stream_settings'; |
| 15 | const ADMIN_PARENT_PAGE = 'options-general.php'; |
| 16 | const VIEW_CAP = 'view_stream'; |
| 17 | const SETTINGS_CAP = 'manage_options'; |
| 18 | const PRELOAD_AUTHORS_MAX = 50; |
| 19 | |
| 20 | public static function load() { |
| 21 | // User and role caps |
| 22 | add_filter( 'user_has_cap', array( __CLASS__, '_filter_user_caps' ), 10, 4 ); |
| 23 | add_filter( 'role_has_cap', array( __CLASS__, '_filter_role_caps' ), 10, 3 ); |
| 24 | |
| 25 | self::$disable_access = apply_filters( 'mainwp_wp_stream_disable_admin_access', false ); |
| 26 | |
| 27 | // Register settings page |
| 28 | if (get_option('mainwp_creport_branding_stream_hide') !== "hide") { |
| 29 | add_filter( 'mainwp-child-init-subpages', array( __CLASS__, 'init_subpages' ) ); |
| 30 | } |
| 31 | |
| 32 | // Admin notices |
| 33 | add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); |
| 34 | |
| 35 | // Add admin body class |
| 36 | add_filter( 'admin_body_class', array( __CLASS__, 'admin_body_class' ) ); |
| 37 | |
| 38 | // Load admin scripts and styles |
| 39 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) ); |
| 40 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_menu_css' ) ); |
| 41 | |
| 42 | // Reset MainWP Reports database |
| 43 | add_action( 'wp_ajax_mainwp_wp_stream_reset', array( __CLASS__, 'ajax_reset_reports' ) ); |
| 44 | |
| 45 | // Reset MainWP Reports settings |
| 46 | add_action( 'wp_ajax_mainwp_wp_stream_defaults', array( __CLASS__, 'wp_ajax_defaults' ) ); |
| 47 | |
| 48 | |
| 49 | // Auto purge setup |
| 50 | add_action( 'wp_loaded', array( __CLASS__, 'purge_schedule_setup' ) ); |
| 51 | add_action( 'mainwp_wp_stream_auto_purge', array( __CLASS__, 'purge_scheduled_action' ) ); |
| 52 | |
| 53 | // Admin notices |
| 54 | add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); |
| 55 | |
| 56 | // Ajax authors list |
| 57 | add_action( 'wp_ajax_mainwp_wp_stream_filters', array( __CLASS__, 'ajax_filters' ) ); |
| 58 | |
| 59 | // Ajax author's name by ID |
| 60 | add_action( 'wp_ajax_mainwp_wp_stream_get_filter_value_by_id', array( __CLASS__, 'get_filter_value_by_id' ) ); |
| 61 | |
| 62 | add_filter('updraftplus_save_last_backup', array( __CLASS__, 'hookUpdraftplusSaveLastBackup' )); |
| 63 | // hmbkp_backup_complete |
| 64 | add_action('mainwp_child_reports_log', array( __CLASS__, 'hook_reports_log' ), 10, 1); |
| 65 | } |
| 66 | |
| 67 | public static function get_branding_title() { |
| 68 | if (self::$brandingTitle === null) { |
| 69 | $cancelled_branding = ( get_option( 'mainwp_child_branding_disconnected' ) === 'yes' ) && ! get_option( 'mainwp_branding_preserve_branding' ); |
| 70 | $branding_header = get_option( 'mainwp_branding_plugin_header' ); |
| 71 | if ( ! $cancelled_branding && ( is_array( $branding_header ) && ! empty( $branding_header['name'] ) ) ) { |
| 72 | self::$brandingTitle = stripslashes( $branding_header['name'] ); |
| 73 | } else { |
| 74 | self::$brandingTitle = ''; |
| 75 | } |
| 76 | } |
| 77 | return self::$brandingTitle; |
| 78 | } |
| 79 | |
| 80 | public static function admin_notices() { |
| 81 | $message = mainwp_wp_stream_filter_input( INPUT_GET, 'message' ); |
| 82 | |
| 83 | switch ( $message ) { |
| 84 | case 'child_reports_data_erased': |
| 85 | printf( '<div class="updated"><p>%s</p></div>', __( 'All records have been successfully erased.', 'mainwp-child-reports' ) ); |
| 86 | break; |
| 87 | case 'child_reports_settings_reset': |
| 88 | printf( '<div class="updated"><p>%s</p></div>', __( 'All site settings have been successfully reset.', 'mainwp-child-reports' ) ); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public static function hookUpdraftplusSaveLastBackup($last_backup) { |
| 94 | |
| 95 | if (!is_array($last_backup)) |
| 96 | return $last_backup; |
| 97 | |
| 98 | if (isset($last_backup['backup_time'])) { |
| 99 | if (empty($last_backup['success'])) |
| 100 | return false; |
| 101 | |
| 102 | $date = $last_backup['backup_time']; |
| 103 | $backup = $last_backup['backup_array']; |
| 104 | |
| 105 | $message = ""; |
| 106 | $backup_type = ""; |
| 107 | if (isset($backup['db'])) { |
| 108 | $message .= "database, "; |
| 109 | $backup_type .= "database, "; |
| 110 | } |
| 111 | if (isset($backup['plugins'])) { |
| 112 | $message .= "plugins, "; |
| 113 | $backup_type .= "plugins, "; |
| 114 | } |
| 115 | |
| 116 | if (isset($backup['themes'])) { |
| 117 | $message .= "themes, "; |
| 118 | $backup_type .= "themes, "; |
| 119 | } |
| 120 | |
| 121 | $message = rtrim($message, ', '); |
| 122 | $message = "Updraftplus backup " . $message ." finished"; |
| 123 | |
| 124 | $backup_type = rtrim($backup_type, ', '); |
| 125 | |
| 126 | $size = "N/A"; |
| 127 | if (isset($backup['db-size'])) { |
| 128 | $size = $backup['db-size']; |
| 129 | } else if (isset($backup['themes-size'])) { |
| 130 | $size = $backup['themes-size']; |
| 131 | } |
| 132 | $destination = ""; |
| 133 | do_action("updraftplus_backup", $destination , $message, __('Finished', 'mainwp-child-reports'), $backup_type, $date); |
| 134 | } |
| 135 | return $last_backup; |
| 136 | } |
| 137 | |
| 138 | public static function hook_reports_log($ext_name = '') { |
| 139 | do_action('mainwp_child_log', $ext_name); |
| 140 | } |
| 141 | |
| 142 | static function get_record_meta_data($record, $meta_key) { |
| 143 | |
| 144 | if (empty($record)) |
| 145 | return ""; |
| 146 | $value = ""; |
| 147 | if (isset($record->meta)) { |
| 148 | $meta = $record->meta; |
| 149 | if (isset($meta[$meta_key])) { |
| 150 | $value = $meta[$meta_key]; |
| 151 | $value = current($value); |
| 152 | if ($meta_key == "author_meta") { |
| 153 | $value = unserialize($value); |
| 154 | $value = $value['display_name']; |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | } |
| 159 | return $value; |
| 160 | } |
| 161 | |
| 162 | public static function init_subpages($subPages = array()) { |
| 163 | if ( is_network_admin() && ! is_plugin_active_for_network( MAINWP_WP_STREAM_PLUGIN ) ) { |
| 164 | return $subPages; |
| 165 | } |
| 166 | |
| 167 | $branding_text = MainWP_WP_Stream_Admin::get_branding_title(); |
| 168 | if (empty($branding_text)) { |
| 169 | $branding_text = 'Child Reports'; |
| 170 | } else { |
| 171 | $branding_text = $branding_text . ' Reports'; |
| 172 | } |
| 173 | |
| 174 | $subPages[] = array('title' => $branding_text, 'slug' => 'reports-page' , 'callback' => array( __CLASS__, 'render_reports_page' ) , 'load_callback' => array( __CLASS__, 'register_list_table' )); |
| 175 | $subPages[] = array('title' => $branding_text . ' Settings', 'slug' => 'reports-settings' , 'callback' => array( __CLASS__, 'render_reports_settings' ) ); |
| 176 | return $subPages; |
| 177 | } |
| 178 | |
| 179 | public static function admin_enqueue_scripts( $hook ) { |
| 180 | // wp_register_script( 'select2', MAINWP_WP_STREAM_URL . 'ui/select2/select2.min.js', array( 'jquery' ), '3.4.5', true ); |
| 181 | // wp_register_style( 'select2', MAINWP_WP_STREAM_URL . 'ui/select2/select2.css', array(), '3.4.5' ); |
| 182 | wp_register_script( 'timeago', MAINWP_WP_STREAM_URL . 'ui/timeago/jquery.timeago.js', array(), '1.4.1', true ); |
| 183 | |
| 184 | $locale = strtolower( substr( get_locale(), 0, 2 ) ); |
| 185 | $file_tmpl = 'ui/timeago/locales/jquery.timeago.%s.js'; |
| 186 | |
| 187 | if ( file_exists( MAINWP_WP_STREAM_DIR . sprintf( $file_tmpl, $locale ) ) ) { |
| 188 | wp_register_script( 'timeago-locale', MAINWP_WP_STREAM_URL . sprintf( $file_tmpl, $locale ), array( 'timeago' ), '1' ); |
| 189 | } else { |
| 190 | wp_register_script( 'timeago-locale', MAINWP_WP_STREAM_URL . sprintf( $file_tmpl, 'en' ), array( 'timeago' ), '1' ); |
| 191 | } |
| 192 | |
| 193 | wp_enqueue_style( 'mainwp-wp-stream-admin', MAINWP_WP_STREAM_URL . 'ui/admin.css', array(), MainWP_WP_Stream::VERSION ); |
| 194 | |
| 195 | //$script_screens = array( 'plugins.php', 'user-edit.php', 'user-new.php', 'profile.php' ); |
| 196 | |
| 197 | if ( 'index.php' === $hook ) { |
| 198 | |
| 199 | } elseif ( in_array( $hook, self::$screen_id ) || $hook == 'settings_page_mainwp-reports-page' ) { |
| 200 | wp_register_script( 'child-report-select2', MAINWP_WP_STREAM_URL . 'ui/select2/select2.min.js', array( 'jquery' ), '3.4.5', true ); |
| 201 | wp_register_style( 'child-report-select2', MAINWP_WP_STREAM_URL . 'ui/select2/select2.css', array(), '3.4.5' ); |
| 202 | |
| 203 | wp_enqueue_script( 'child-report-select2' ); |
| 204 | wp_enqueue_style( 'child-report-select2' ); |
| 205 | |
| 206 | wp_enqueue_script( 'timeago' ); |
| 207 | wp_enqueue_script( 'timeago-locale' ); |
| 208 | |
| 209 | wp_enqueue_script( 'mainwp-wp-stream-admin', MAINWP_WP_STREAM_URL . 'ui/admin.js', array( 'jquery', 'child-report-select2', 'heartbeat' ), MainWP_WP_Stream::VERSION ); |
| 210 | wp_localize_script( |
| 211 | 'mainwp-wp-stream-admin', |
| 212 | 'mainwp_wp_stream', |
| 213 | array( |
| 214 | 'i18n' => array( |
| 215 | 'confirm_purge' => __( 'Are you sure you want to delete all MainWP Child Reports activity records from the database? This cannot be undone.', 'mainwp-child-reports' ), |
| 216 | 'confirm_defaults' => __( 'Are you sure you want to reset all site settings to default? This cannot be undone.', 'mainwp-child-reports' ), |
| 217 | 'confirm_uninstall' => __( 'Are you sure you want to uninstall and deactivate MainWP Child Reports? This will delete all MainWP Child Reports tables from the database and cannot be undone.', 'mainwp-child-reports' ), |
| 218 | ), |
| 219 | 'gmt_offset' => get_option( 'gmt_offset' ), |
| 220 | 'current_screen' => $hook, |
| 221 | 'current_page' => isset( $_GET['paged'] ) ? esc_js( $_GET['paged'] ) : '1', |
| 222 | 'current_order' => isset( $_GET['order'] ) ? esc_js( $_GET['order'] ) : 'desc', |
| 223 | 'current_query' => json_encode( $_GET ), |
| 224 | 'current_query_count' => count( $_GET ), |
| 225 | 'filters' => self::$list_table ? self::$list_table->get_filters() : false, |
| 226 | 'locale' => esc_js( $locale ) |
| 227 | ) |
| 228 | ); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
| 233 | public static function admin_body_class( $classes ) { |
| 234 | if ( isset( $_GET['page'] ) && false !== strpos( $_GET['page'], self::RECORDS_PAGE_SLUG ) ) { |
| 235 | $classes .= sprintf( ' %s ', self::ADMIN_BODY_CLASS ); |
| 236 | } |
| 237 | |
| 238 | return $classes; |
| 239 | } |
| 240 | |
| 241 | public static function admin_menu_css() { |
| 242 | wp_register_style( 'jquery-ui', '//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery-ui.css', array(), '1.10.1' ); |
| 243 | wp_register_style( 'mainwp-wp-stream-datepicker', MAINWP_WP_STREAM_URL . 'ui/datepicker.css', array( 'jquery-ui' ), MainWP_WP_Stream::VERSION ); |
| 244 | |
| 245 | // Make sure we're working off a clean version |
| 246 | include( ABSPATH . WPINC . '/version.php' ); |
| 247 | } |
| 248 | |
| 249 | public static function register_update_hook( $file, $callback, $version ) { |
| 250 | if ( ! is_admin() ) { |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | $plugin = plugin_basename( $file ); |
| 255 | |
| 256 | if ( is_plugin_active_for_network( $plugin ) ) { |
| 257 | $current_versions = get_site_option( MainWP_WP_Stream_Install::KEY . '_connectors', array() ); |
| 258 | $network = true; |
| 259 | } elseif ( is_plugin_active( $plugin ) ) { |
| 260 | $current_versions = get_option( MainWP_WP_Stream_Install::KEY . '_connectors', array() ); |
| 261 | $network = false; |
| 262 | } else { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | if ( version_compare( $version, $current_versions[ $plugin ], '>' ) ) { |
| 267 | call_user_func( $callback, $current_versions[ $plugin ], $network ); |
| 268 | $current_versions[ $plugin ] = $version; |
| 269 | } |
| 270 | |
| 271 | if ( $network ) { |
| 272 | update_site_option( MainWP_WP_Stream_Install::KEY . '_registered_connectors', $current_versions ); |
| 273 | } else { |
| 274 | update_option( MainWP_WP_Stream_Install::KEY . '_registered_connectors', $current_versions ); |
| 275 | } |
| 276 | |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | public static function register_list_table() { |
| 281 | require_once MAINWP_WP_STREAM_INC_DIR . 'list-table.php'; |
| 282 | $param = array(); |
| 283 | if (isset(self::$screen_id['main'])) { |
| 284 | $param['screen'] = self::$screen_id['main']; |
| 285 | } |
| 286 | self::$list_table = new MainWP_WP_Stream_List_Table( $param ); |
| 287 | } |
| 288 | |
| 289 | public static function render_reports_page() { |
| 290 | do_action('mainwp-child-pageheader', 'reports-page'); |
| 291 | self::$list_table->prepare_items(); |
| 292 | echo '<div class="mainwp_child_reports_wrap">'; |
| 293 | self::$list_table->display(); |
| 294 | echo '</div>'; |
| 295 | do_action('mainwp-child-pagefooter', 'reports-page'); |
| 296 | } |
| 297 | |
| 298 | public static function render_reports_settings() { |
| 299 | |
| 300 | $option_key = MainWP_WP_Stream_Settings::$option_key; |
| 301 | $form_action = apply_filters( 'mainwp_wp_stream_settings_form_action', admin_url( 'options.php' ) ); |
| 302 | $sections = MainWP_WP_Stream_Settings::get_fields(); |
| 303 | //settings_errors(); |
| 304 | do_action('mainwp-child-pageheader', 'reports-settings') |
| 305 | ?> |
| 306 | <div class="postbox"> |
| 307 | <div class="inside"> |
| 308 | <form method="post" action="<?php echo esc_attr( $form_action ) ?>" enctype="multipart/form-data"> |
| 309 | <?php |
| 310 | $i = 0; |
| 311 | foreach ( $sections as $section => $data ) { |
| 312 | $i++; |
| 313 | settings_fields( $option_key ); |
| 314 | do_settings_sections( $option_key ); |
| 315 | } |
| 316 | submit_button(); |
| 317 | ?> |
| 318 | </form> |
| 319 | </div> |
| 320 | </div> |
| 321 | |
| 322 | <?php |
| 323 | do_action('mainwp-child-pagefooter', 'reports-settings'); |
| 324 | } |
| 325 | |
| 326 | public static function ajax_reset_reports() { |
| 327 | check_ajax_referer( 'stream_nonce', 'mainwp_wp_stream_nonce' ); |
| 328 | |
| 329 | if ( current_user_can( self::SETTINGS_CAP ) ) { |
| 330 | self::erase_stream_records(); |
| 331 | MainWP_WP_Stream_Install::check_to_copy_data(); |
| 332 | wp_redirect( |
| 333 | add_query_arg( |
| 334 | array( |
| 335 | 'page' => 'mainwp-reports-settings', |
| 336 | 'message' => 'child_reports_data_erased' |
| 337 | ), |
| 338 | admin_url( 'options-general.php' ) |
| 339 | ) |
| 340 | ); |
| 341 | exit; |
| 342 | } else { |
| 343 | wp_die( "You don't have sufficient privileges to do this action." ); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | private static function erase_stream_records() { |
| 348 | global $wpdb; |
| 349 | |
| 350 | $where = ''; |
| 351 | if ( is_multisite() && ! is_plugin_active_for_network( MAINWP_WP_STREAM_PLUGIN ) ) { |
| 352 | $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() ); |
| 353 | } |
| 354 | |
| 355 | $wpdb->query( |
| 356 | $wpdb->prepare( |
| 357 | "DELETE `stream`, `context`, `meta` |
| 358 | FROM {$wpdb->mainwp_reports} AS `stream` |
| 359 | LEFT JOIN {$wpdb->mainwp_reportscontext} AS `context` |
| 360 | ON `context`.`record_id` = `stream`.`ID` |
| 361 | LEFT JOIN {$wpdb->mainwp_reportsmeta} AS `meta` |
| 362 | ON `meta`.`record_id` = `stream`.`ID` |
| 363 | WHERE `stream`.`type` = %s |
| 364 | $where;", |
| 365 | 'stream' |
| 366 | ) |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | public static function wp_ajax_defaults() { |
| 371 | check_ajax_referer( 'stream_nonce', 'mainwp_wp_stream_nonce' ); |
| 372 | |
| 373 | if ( ! is_plugin_active_for_network( MAINWP_WP_STREAM_PLUGIN ) ) { |
| 374 | wp_die( "You don't have sufficient privileges to do this action." ); |
| 375 | } |
| 376 | |
| 377 | if ( current_user_can( self::SETTINGS_CAP ) ) { |
| 378 | self::reset_stream_settings(); |
| 379 | wp_redirect( |
| 380 | add_query_arg( |
| 381 | array( |
| 382 | 'page' => is_network_admin() ? 'mainwp_wp_stream_network_settings' : 'mainwp_wp_stream_settings', |
| 383 | 'message' => 'child_reports_settings_reset', |
| 384 | ), |
| 385 | is_plugin_active_for_network( MAINWP_WP_STREAM_PLUGIN ) ? network_admin_url( self::ADMIN_PARENT_PAGE ) : admin_url( self::ADMIN_PARENT_PAGE ) |
| 386 | ) |
| 387 | ); |
| 388 | exit; |
| 389 | } else { |
| 390 | wp_die( "You don't have sufficient privileges to do this action." ); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | private static function reset_stream_settings() { |
| 395 | global $wpdb; |
| 396 | |
| 397 | $blogs = get_sites(); |
| 398 | |
| 399 | if ( $blogs ) { |
| 400 | foreach ( $blogs as $blog ) { |
| 401 | switch_to_blog( $blog['blog_id'] ); |
| 402 | delete_option( MainWP_WP_Stream_Settings::KEY ); |
| 403 | } |
| 404 | restore_current_blog(); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | public static function purge_schedule_setup() { |
| 409 | if ( ! wp_next_scheduled( 'mainwp_wp_stream_auto_purge' ) ) { |
| 410 | wp_schedule_event( time(), 'twicedaily', 'mainwp_wp_stream_auto_purge' ); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | public static function purge_scheduled_action() { |
| 415 | global $wpdb; |
| 416 | |
| 417 | // Don't purge if in Network Admin if Stream isn't network enabled |
| 418 | if ( is_network_admin() && is_multisite() && ! is_plugin_active_for_network( MAINWP_WP_STREAM_PLUGIN ) ) { |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | if ( is_multisite() && is_plugin_active_for_network( MAINWP_WP_STREAM_PLUGIN ) ) { |
| 423 | $options = (array) get_site_option( MainWP_WP_Stream_Settings::NETWORK_KEY, array() ); |
| 424 | } else { |
| 425 | $options = MainWP_WP_Stream_Settings::get_options(); |
| 426 | } |
| 427 | |
| 428 | $days = $options['general_records_ttl']; |
| 429 | |
| 430 | if (empty($days)) |
| 431 | return; |
| 432 | |
| 433 | $date = new DateTime( 'now', $timezone = new DateTimeZone( 'UTC' ) ); |
| 434 | |
| 435 | $date->sub( DateInterval::createFromDateString( "$days days" ) ); |
| 436 | |
| 437 | $where = $wpdb->prepare( ' AND `stream`.`created` < %s', $date->format( 'Y-m-d H:i:s' ) ); |
| 438 | |
| 439 | if ( is_multisite() && ! is_plugin_active_for_network( MAINWP_WP_STREAM_PLUGIN ) ) { |
| 440 | $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() ); |
| 441 | } |
| 442 | |
| 443 | $wpdb->query( |
| 444 | $wpdb->prepare( |
| 445 | "DELETE `stream`, `context`, `meta` |
| 446 | FROM {$wpdb->mainwp_reports} AS `stream` |
| 447 | LEFT JOIN {$wpdb->mainwp_reportscontext} AS `context` |
| 448 | ON `context`.`record_id` = `stream`.`ID` |
| 449 | LEFT JOIN {$wpdb->mainwp_reportsmeta} AS `meta` |
| 450 | ON `meta`.`record_id` = `stream`.`ID` |
| 451 | WHERE `stream`.`type` = %s |
| 452 | $where;", |
| 453 | 'stream' |
| 454 | ) |
| 455 | ); |
| 456 | } |
| 457 | |
| 458 | private static function _role_can_view_stream( $role ) { |
| 459 | if ( in_array( $role, array('administrator')) ) { |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | public static function _filter_user_caps( $allcaps, $caps, $args, $user = null ) { |
| 467 | global $wp_roles; |
| 468 | |
| 469 | if ( ! isset( $wp_roles ) ) { |
| 470 | $wp_roles = new WP_Roles(); |
| 471 | } |
| 472 | |
| 473 | $user = is_a( $user, 'WP_User' ) ? $user : wp_get_current_user(); |
| 474 | |
| 475 | $roles = array_unique( |
| 476 | array_merge( |
| 477 | $user->roles, |
| 478 | array_filter( |
| 479 | array_keys( $user->caps ), |
| 480 | array( $wp_roles, 'is_role' ) |
| 481 | ) |
| 482 | ) |
| 483 | ); |
| 484 | |
| 485 | foreach ( $caps as $cap ) { |
| 486 | if ( self::VIEW_CAP === $cap ) { |
| 487 | foreach ( $roles as $role ) { |
| 488 | if ( self::_role_can_view_stream( $role ) ) { |
| 489 | $allcaps[ $cap ] = true; |
| 490 | break 2; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return $allcaps; |
| 497 | } |
| 498 | |
| 499 | public static function _filter_role_caps( $allcaps, $cap, $role ) { |
| 500 | if ( self::VIEW_CAP === $cap && self::_role_can_view_stream( $role ) ) { |
| 501 | $allcaps[ $cap ] = true; |
| 502 | } |
| 503 | |
| 504 | return $allcaps; |
| 505 | } |
| 506 | |
| 507 | public static function ajax_filters() { |
| 508 | if ( ! defined( 'DOING_AJAX' ) ) { |
| 509 | wp_die( '-1' ); |
| 510 | } |
| 511 | |
| 512 | check_ajax_referer( 'mainwp_creport_filters_user_search_nonce', 'nonce' ); |
| 513 | |
| 514 | switch ( mainwp_wp_stream_filter_input( INPUT_GET, 'filter' ) ) { |
| 515 | case 'author': |
| 516 | $users = array_merge( |
| 517 | array( 0 => (object) array( 'display_name' => 'WP-CLI' ) ), |
| 518 | get_users() |
| 519 | ); |
| 520 | |
| 521 | // `search` arg for get_users() is not enough |
| 522 | $users = array_filter( |
| 523 | $users, |
| 524 | function ( $user ) { |
| 525 | return false !== mb_strpos( mb_strtolower( $user->display_name ), mb_strtolower( mainwp_wp_stream_filter_input( INPUT_GET, 'q' ) ) ); |
| 526 | } |
| 527 | ); |
| 528 | |
| 529 | if ( count( $users ) > self::PRELOAD_AUTHORS_MAX ) { |
| 530 | $users = array_slice( $users, 0, self::PRELOAD_AUTHORS_MAX ); |
| 531 | // @todo $extra is not used |
| 532 | $extra = array( |
| 533 | 'id' => 0, |
| 534 | 'disabled' => true, |
| 535 | 'text' => sprintf( _n( 'One more result...', '%d more results...', $results_count - self::PRELOAD_AUTHORS_MAX, 'mainwp-child-reports' ), $results_count - self::PRELOAD_AUTHORS_MAX ), |
| 536 | ); |
| 537 | } |
| 538 | |
| 539 | // Get gravatar / roles for final result set |
| 540 | $results = self::get_authors_record_meta( $users ); |
| 541 | |
| 542 | break; |
| 543 | } |
| 544 | if ( isset( $results ) ) { |
| 545 | echo json_encode( array_values( $results ) ); |
| 546 | } |
| 547 | die(); |
| 548 | } |
| 549 | |
| 550 | public static function get_filter_value_by_id() { |
| 551 | $filter = mainwp_wp_stream_filter_input( INPUT_POST, 'filter' ); |
| 552 | switch ( $filter ) { |
| 553 | case 'author': |
| 554 | $id = mainwp_wp_stream_filter_input( INPUT_POST, 'id' ); |
| 555 | if ( $id === '0' ) { |
| 556 | $value = 'WP-CLI'; |
| 557 | break; |
| 558 | } |
| 559 | $user = get_userdata( $id ); |
| 560 | if ( ! $user || is_wp_error( $user ) ) { |
| 561 | $value = ''; |
| 562 | } else { |
| 563 | $value = $user->display_name; |
| 564 | } |
| 565 | break; |
| 566 | default: |
| 567 | $value = ''; |
| 568 | break; |
| 569 | } |
| 570 | echo json_encode( $value ); |
| 571 | wp_die(); |
| 572 | } |
| 573 | |
| 574 | public static function get_authors_record_meta( $authors ) { |
| 575 | require_once MAINWP_WP_STREAM_INC_DIR . 'class-wp-stream-author.php'; |
| 576 | |
| 577 | $authors_records = array(); |
| 578 | |
| 579 | foreach ( $authors as $user_id => $args ) { |
| 580 | $author = new MainWP_WP_Stream_Author( $user_id ); |
| 581 | $disabled = isset( $args['disabled'] ) ? $args['disabled'] : null; |
| 582 | |
| 583 | $authors_records[ $user_id ] = array( |
| 584 | 'text' => $author->get_display_name(), |
| 585 | 'id' => $user_id, |
| 586 | 'label' => $author->get_display_name(), |
| 587 | 'icon' => $author->get_avatar_src( 32 ), |
| 588 | 'title' => '', |
| 589 | 'disabled' => $disabled, |
| 590 | ); |
| 591 | } |
| 592 | |
| 593 | return $authors_records; |
| 594 | } |
| 595 | } |
| 596 |