class-admin.php
2 years ago
class-author.php
5 years ago
class-cli.php
5 years ago
class-connector.php
2 years ago
class-connectors.php
3 years ago
class-date-interval.php
5 years ago
class-db-driver-wpdb.php
2 years ago
class-db-driver.php
5 years ago
class-db.php
3 years ago
class-export.php
5 years ago
class-exporter.php
5 years ago
class-filter-input.php
3 years ago
class-form-generator.php
5 years ago
class-install.php
3 years ago
class-list-table.php
4 years ago
class-live-update.php
3 years ago
class-log.php
4 years ago
class-mainwp-child-report-helper.php
5 years ago
class-network.php
3 years ago
class-plugin.php
3 years ago
class-preview-list-table.php
5 years ago
class-query.php
4 years ago
class-record.php
5 years ago
class-settings.php
2 years ago
class-uninstall.php
2 years ago
class-admin.php
1029 lines
| 1 | <?php |
| 2 | /** MainWP Child Reports Admin. */ |
| 3 | |
| 4 | namespace WP_MainWP_Stream; |
| 5 | |
| 6 | use DateTime; |
| 7 | use DateTimeZone; |
| 8 | use DateInterval; |
| 9 | use \WP_CLI; |
| 10 | use \WP_Roles; |
| 11 | |
| 12 | /** |
| 13 | * Class Admin. |
| 14 | * |
| 15 | * @package WP_MainWP_Stream |
| 16 | */ |
| 17 | class Admin { |
| 18 | |
| 19 | /** @var Plugin Hold Plugin class. */ |
| 20 | public $plugin; |
| 21 | |
| 22 | /** @var Network Holds Network class. */ |
| 23 | public $network; |
| 24 | |
| 25 | /** @var Live_Update Holds Live Update class. */ |
| 26 | public $live_update; |
| 27 | |
| 28 | /** @var Export Holds Export class. */ |
| 29 | public $export; |
| 30 | |
| 31 | /** @var string Menu page screen id. */ |
| 32 | public $screen_id = array(); |
| 33 | |
| 34 | /** @var List_Table List table object. */ |
| 35 | public $list_table = null; |
| 36 | |
| 37 | /** @var bool Option to disable access to Stream. */ |
| 38 | public $disable_access = false; |
| 39 | |
| 40 | /** @var string Class applied to the body of the admin screen. */ |
| 41 | public $admin_body_class = 'wp_mainwp_stream_screen'; |
| 42 | |
| 43 | /** @var string Slug of the records page. */ |
| 44 | public $records_page_slug = 'mainwp-reports-page'; |
| 45 | |
| 46 | /** @var string Slug of the settings page. */ |
| 47 | public $settings_page_slug = 'mainwp-reports-settings'; |
| 48 | |
| 49 | /** @var string Parent page of the records and settings pages. */ |
| 50 | public $admin_parent_page = 'options-general.php'; |
| 51 | |
| 52 | /** @var string Capability name for viewing records. */ |
| 53 | public $view_cap = 'view_stream'; |
| 54 | |
| 55 | /** @var string Capability name for viewing settings. */ |
| 56 | public $settings_cap = 'manage_options'; |
| 57 | |
| 58 | /** @var int Total amount of authors to pre-load. */ |
| 59 | public $preload_users_max = 50; |
| 60 | |
| 61 | /** @var array Admin notices, collected and displayed on proper action. */ |
| 62 | public $notices = array(); |
| 63 | |
| 64 | /** |
| 65 | * Admin constructor. |
| 66 | * |
| 67 | * Run each time the class is called. |
| 68 | * |
| 69 | * @param Plugin $plugin The main Plugin class. |
| 70 | */ |
| 71 | public function __construct( $plugin ) { |
| 72 | $this->plugin = $plugin; |
| 73 | |
| 74 | add_action( 'init', array( $this, 'init' ) ); |
| 75 | |
| 76 | // Ensure function used in various methods is pre-loaded. |
| 77 | if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 78 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 79 | } |
| 80 | |
| 81 | // User and role caps. |
| 82 | // add_filter( 'user_has_cap', array( $this, 'filter_user_caps' ), 10, 4 ); |
| 83 | // add_filter( 'role_has_cap', array( $this, 'filter_role_caps' ), 10, 3 ); |
| 84 | |
| 85 | if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && ! is_network_admin() ) { |
| 86 | $options = (array) get_site_option( 'wp_mainwp_stream_network', array() ); |
| 87 | $option = isset( $options['general_site_access'] ) ? absint( $options['general_site_access'] ) : 1; |
| 88 | |
| 89 | $this->disable_access = ( $option ) ? false : true; |
| 90 | } |
| 91 | |
| 92 | // Register settings page. |
| 93 | if ( ! $this->disable_access ) { |
| 94 | // add_action( 'admin_menu', array( $this, 'register_menu' ) ); |
| 95 | } |
| 96 | |
| 97 | // Admin notices. |
| 98 | add_action( 'admin_notices', array( $this, 'prepare_admin_notices' ) ); |
| 99 | add_action( 'shutdown', array( $this, 'admin_notices' ) ); |
| 100 | |
| 101 | // Add admin body class. |
| 102 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
| 103 | |
| 104 | // Load admin scripts and styles. |
| 105 | add_action( |
| 106 | 'admin_enqueue_scripts', |
| 107 | array( |
| 108 | $this, |
| 109 | 'admin_enqueue_scripts', |
| 110 | ) |
| 111 | ); |
| 112 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) ); |
| 113 | |
| 114 | // Reset Streams database. |
| 115 | add_action( |
| 116 | 'wp_ajax_wp_mainwp_stream_reset', |
| 117 | array( |
| 118 | $this, |
| 119 | 'wp_ajax_reset', |
| 120 | ) |
| 121 | ); |
| 122 | |
| 123 | |
| 124 | // Uninstall Streams and Deactivate plugin. |
| 125 | $uninstall = $this->plugin->db->driver->purge_storage( $this->plugin ); |
| 126 | |
| 127 | // Auto purge setup. |
| 128 | add_action( 'wp_loaded', array( $this, 'purge_schedule_setup' ) ); |
| 129 | add_action( |
| 130 | 'wp_mainwp_stream_auto_purge', |
| 131 | array( |
| 132 | $this, |
| 133 | 'purge_scheduled_action', |
| 134 | ) |
| 135 | ); |
| 136 | |
| 137 | // Ajax users list. |
| 138 | add_action( |
| 139 | 'wp_ajax_wp_mainwp_stream_filters', |
| 140 | array( |
| 141 | $this, |
| 142 | 'ajax_filters', |
| 143 | ) |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Load admin classes. |
| 149 | * |
| 150 | * @action init |
| 151 | * |
| 152 | * @uses \WP_MainWP_Stream\Export |
| 153 | * @uses \WP_MainWP_Stream\Live_Update |
| 154 | * @uses \WP_MainWP_Stream\Network |
| 155 | */ |
| 156 | public function init() { |
| 157 | $this->network = new Network( $this->plugin ); |
| 158 | $this->live_update = new Live_Update( $this->plugin ); |
| 159 | $this->export = new Export( $this->plugin ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Output specific updates passed as URL parameters. |
| 164 | * |
| 165 | * @action admin_notices |
| 166 | * |
| 167 | * @return void |
| 168 | */ |
| 169 | public function prepare_admin_notices() { |
| 170 | $message = wp_mainwp_stream_filter_input( INPUT_GET, 'message' ); |
| 171 | |
| 172 | switch ( $message ) { |
| 173 | case 'settings_reset': |
| 174 | $this->notice( esc_html__( 'All site settings have been successfully reset.', 'mainwp-child-reports' ) ); |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Handle notice messages according to the appropriate context (WP-CLI or the WP Admin). |
| 181 | * |
| 182 | * @param string $message Message to output. |
| 183 | * @param bool $is_error If the message is error_level (true) or warning (false). |
| 184 | */ |
| 185 | public function notice( $message, $is_error = true ) { |
| 186 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 187 | $message = strip_tags( $message ); |
| 188 | |
| 189 | if ( $is_error ) { |
| 190 | WP_CLI::warning( $message ); |
| 191 | } else { |
| 192 | WP_CLI::success( $message ); |
| 193 | } |
| 194 | } else { |
| 195 | // Trigger admin notices late, so that any notices which occur during page load are displayed. |
| 196 | add_action( 'shutdown', array( $this, 'admin_notices' ) ); |
| 197 | |
| 198 | $notice = compact( 'message', 'is_error' ); |
| 199 | |
| 200 | if ( ! in_array( $notice, $this->notices, true ) ) { |
| 201 | $this->notices[] = $notice; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Show an error or other message in the WP Admin. |
| 208 | * |
| 209 | * @action shutdown |
| 210 | */ |
| 211 | public function admin_notices() { |
| 212 | |
| 213 | /** |
| 214 | * WordPress core white list of html tags and attributes |
| 215 | * that WordPress allows in posts used for sanitizing post_content. |
| 216 | * |
| 217 | * @global object $allowedposttags |
| 218 | */ |
| 219 | global $allowedposttags; |
| 220 | |
| 221 | $custom = array( |
| 222 | 'progress' => array( |
| 223 | 'class' => true, |
| 224 | 'id' => true, |
| 225 | 'max' => true, |
| 226 | 'style' => true, |
| 227 | 'value' => true, |
| 228 | ), |
| 229 | ); |
| 230 | |
| 231 | $allowed_html = array_merge( $allowedposttags, $custom ); |
| 232 | |
| 233 | ksort( $allowed_html ); |
| 234 | |
| 235 | foreach ( $this->notices as $notice ) { |
| 236 | $class_name = empty( $notice['is_error'] ) ? 'updated' : 'error'; |
| 237 | $html_message = sprintf( '<div class="%s">%s</div>', esc_attr( $class_name ), wpautop( $notice['message'] ) ); |
| 238 | |
| 239 | echo wp_kses( $html_message, $allowed_html ); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Register menu page. |
| 245 | * |
| 246 | * @action admin_menu |
| 247 | * |
| 248 | * @return void |
| 249 | * @deprecated Disabled. |
| 250 | */ |
| 251 | public function register_menu() { |
| 252 | |
| 253 | /** |
| 254 | * Filter the main admin menu title. |
| 255 | * |
| 256 | * @return string Return main menu title. |
| 257 | */ |
| 258 | $main_menu_title = apply_filters( 'wp_mainwp_stream_admin_menu_title', esc_html__( 'MainWP Child Reports', 'mainwp-child-reports' ) ); |
| 259 | |
| 260 | /** |
| 261 | * Filter the main admin menu position. |
| 262 | * |
| 263 | * Note: Using longtail decimal string to reduce the chance of position conflicts, see Codex. |
| 264 | * |
| 265 | * @return string Return menue position. |
| 266 | */ |
| 267 | $main_menu_position = apply_filters( 'wp_mainwp_stream_menu_position', '2.999999' ); |
| 268 | |
| 269 | /** |
| 270 | * Filter the main admin page title. |
| 271 | * |
| 272 | * @return string Return Main page title. |
| 273 | */ |
| 274 | $main_page_title = apply_filters( 'wp_mainwp_stream_admin_page_title', esc_html__( 'Reports Records', 'mainwp-child-reports' ) ); |
| 275 | |
| 276 | $this->screen_id['main'] = add_menu_page( |
| 277 | $main_page_title, |
| 278 | $main_menu_title, |
| 279 | $this->view_cap, |
| 280 | $this->records_page_slug, |
| 281 | array( $this, 'render_list_table' ), |
| 282 | 'div', |
| 283 | $main_menu_position |
| 284 | ); |
| 285 | |
| 286 | /** |
| 287 | * Fires before submenu items are added to the Stream menu |
| 288 | * allowing plugins to add menu items before Settings. |
| 289 | * |
| 290 | * @return void |
| 291 | */ |
| 292 | do_action( 'wp_mainwp_stream_admin_menu' ); |
| 293 | |
| 294 | /** |
| 295 | * Filter the Settings admin page title. |
| 296 | * |
| 297 | * @return string Return settings page title. |
| 298 | */ |
| 299 | $settings_page_title = apply_filters( 'wp_mainwp_stream_settings_form_title', esc_html__( 'Reports Settings', 'mainwp-child-reports' ) ); |
| 300 | |
| 301 | $this->screen_id['settings'] = add_submenu_page( |
| 302 | $this->records_page_slug, |
| 303 | $settings_page_title, |
| 304 | esc_html__( 'Settings', 'mainwp-child-reports' ), |
| 305 | $this->settings_cap, |
| 306 | $this->settings_page_slug, |
| 307 | array( $this, 'render_settings_page' ) |
| 308 | ); |
| 309 | |
| 310 | if ( isset( $this->screen_id['main'] ) ) { |
| 311 | |
| 312 | /** |
| 313 | * Fires just before the Stream list table is registered. |
| 314 | * |
| 315 | * @return void |
| 316 | */ |
| 317 | do_action( 'wp_mainwp_stream_admin_menu_screens' ); |
| 318 | |
| 319 | // Register the list table early, so it associates the column headers with 'Screen settings'. |
| 320 | add_action( |
| 321 | 'load-' . $this->screen_id['main'], |
| 322 | array( |
| 323 | $this, |
| 324 | 'register_list_table', |
| 325 | ) |
| 326 | ); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Enqueue scripts/styles for admin screen. |
| 332 | * |
| 333 | * @action admin_enqueue_scripts |
| 334 | * |
| 335 | * @param string $hook Page hook. |
| 336 | * |
| 337 | * @return void |
| 338 | */ |
| 339 | public function admin_enqueue_scripts( $hook ) { |
| 340 | |
| 341 | wp_register_script( 'wp-mainwp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/js/select2.full.min.js', array( 'jquery' ), '3.5.2', true ); |
| 342 | wp_register_style( 'wp-mainwp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/css/select2.min.css', array(), '3.5.2' ); |
| 343 | wp_register_script( 'wp-mainwp-stream-timeago', $this->plugin->locations['url'] . 'ui/lib/timeago/jquery.timeago.js', array(), '1.4.1', true ); |
| 344 | |
| 345 | $locale = strtolower( substr( get_locale(), 0, 2 ) ); |
| 346 | $file_tmpl = 'ui/lib/timeago/locales/jquery.timeago.%s.js'; |
| 347 | |
| 348 | if ( file_exists( $this->plugin->locations['dir'] . sprintf( $file_tmpl, $locale ) ) ) { |
| 349 | wp_register_script( 'wp-mainwp-stream-timeago-locale', $this->plugin->locations['url'] . sprintf( $file_tmpl, $locale ), array( 'wp-mainwp-stream-timeago' ), '1' ); |
| 350 | } else { |
| 351 | wp_register_script( 'wp-mainwp-stream-timeago-locale', $this->plugin->locations['url'] . sprintf( $file_tmpl, 'en' ), array( 'wp-mainwp-stream-timeago' ), '1' ); |
| 352 | } |
| 353 | |
| 354 | wp_enqueue_style( 'wp-mainwp-stream-admin', $this->plugin->locations['url'] . 'ui/css/admin.css', array(), $this->plugin->get_version() ); |
| 355 | |
| 356 | $script_screens = array( 'plugins.php' ); |
| 357 | |
| 358 | // if ( in_array( $hook, $this->screen_id, true ) || in_array( $hook, $script_screens, true ) ) { |
| 359 | if ( $hook == 'settings_page_mainwp-reports-page' || $hook == 'settings_page_mainwp-reports-settings' || in_array( $hook, $script_screens, true ) ) { |
| 360 | wp_enqueue_script( 'wp-mainwp-stream-select2' ); |
| 361 | wp_enqueue_style( 'wp-mainwp-stream-select2' ); |
| 362 | |
| 363 | wp_enqueue_script( 'wp-mainwp-stream-timeago' ); |
| 364 | wp_enqueue_script( 'wp-mainwp-stream-timeago-locale' ); |
| 365 | |
| 366 | wp_enqueue_script( |
| 367 | 'wp-mainwp-stream-admin', |
| 368 | $this->plugin->locations['url'] . 'ui/js/admin.js', |
| 369 | array( |
| 370 | 'jquery', |
| 371 | 'wp-mainwp-stream-select2', |
| 372 | ), |
| 373 | $this->plugin->get_version() |
| 374 | ); |
| 375 | wp_enqueue_script( |
| 376 | 'wp-mainwp-stream-admin-exclude', |
| 377 | $this->plugin->locations['url'] . 'ui/js/exclude.js', |
| 378 | array( |
| 379 | 'jquery', |
| 380 | 'wp-mainwp-stream-select2', |
| 381 | ), |
| 382 | $this->plugin->get_version() |
| 383 | ); |
| 384 | wp_enqueue_script( |
| 385 | 'wp-mainwp-stream-live-updates', |
| 386 | $this->plugin->locations['url'] . 'ui/js/live-updates.js', |
| 387 | array( |
| 388 | 'jquery', |
| 389 | 'heartbeat', |
| 390 | ), |
| 391 | $this->plugin->get_version() |
| 392 | ); |
| 393 | |
| 394 | wp_localize_script( |
| 395 | 'wp-mainwp-stream-admin', |
| 396 | 'wp_mainwp_stream', |
| 397 | array( |
| 398 | 'i18n' => array( |
| 399 | 'confirm_purge' => esc_html__( 'Are you sure you want to delete all Reports activity records from the database? This cannot be undone.', 'mainwp-child-reports' ), |
| 400 | 'confirm_defaults' => esc_html__( 'Are you sure you want to reset all site settings to default? This cannot be undone.', 'mainwp-child-reports' ), |
| 401 | 'confirm_uninstall' => esc_html__( 'Are you sure you want to uninstall and deactivate MainWP Child Reports? This will delete all Reports tables from the database and cannot be undone.', 'mainwp-child-reports' ), |
| 402 | ), |
| 403 | 'locale' => esc_js( $locale ), |
| 404 | 'gmt_offset' => get_option( 'gmt_offset' ), |
| 405 | ) |
| 406 | ); |
| 407 | |
| 408 | wp_localize_script( |
| 409 | 'wp-mainwp-stream-live-updates', |
| 410 | 'wp_mainwp_stream_live_updates', |
| 411 | array( |
| 412 | 'current_screen' => $hook, |
| 413 | 'current_page' => isset( $_GET['paged'] ) ? esc_js( $_GET['paged'] ) : '1', // WPCS: CSRF ok. |
| 414 | // input var okay, CSRF okay |
| 415 | 'current_order' => isset( $_GET['order'] ) ? esc_js( $_GET['order'] ) : 'desc', // WPCS: CSRF ok. |
| 416 | // input var okay, CSRF okay |
| 417 | 'current_query' => wp_mainwp_stream_json_encode( $_GET ), // WPCS: CSRF ok. |
| 418 | // input var okay, CSRF okay |
| 419 | 'current_query_count' => count( $_GET ), // WPCS: CSRF ok. |
| 420 | // input var okay, CSRF okay |
| 421 | ) |
| 422 | ); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * The maximum number of items that can be updated in bulk without receiving a warning. |
| 427 | * |
| 428 | * Stream watches for bulk actions performed in the WordPress Admin (such as updating |
| 429 | * many posts at once) and warns the user before proceeding if the number of items they |
| 430 | * are attempting to update exceeds this threshold value. Since Stream will try to save |
| 431 | * a log for each item, it will take longer than usual to complete the operation. |
| 432 | * |
| 433 | * The default threshold is 100 items. |
| 434 | * |
| 435 | * @return int |
| 436 | */ |
| 437 | $bulk_actions_threshold = apply_filters( 'wp_mainwp_stream_bulk_actions_threshold', 100 ); |
| 438 | |
| 439 | wp_enqueue_script( 'wp-mainwp-stream-global', $this->plugin->locations['url'] . 'ui/js/global.js', array( 'jquery' ), $this->plugin->get_version() ); |
| 440 | wp_localize_script( |
| 441 | 'wp-mainwp-stream-global', |
| 442 | 'wp_mainwp_stream_global', |
| 443 | array( |
| 444 | 'bulk_actions' => array( |
| 445 | 'i18n' => array( |
| 446 | // translators: Placeholder refers to a number of items (e.g. "1,742") |
| 447 | 'confirm_action' => sprintf( esc_html__( 'Are you sure you want to perform bulk actions on over %s items? This process could take a while to complete.', 'mainwp-child-reports' ), number_format( absint( $bulk_actions_threshold ) ) ), |
| 448 | ), |
| 449 | 'threshold' => absint( $bulk_actions_threshold ), |
| 450 | ), |
| 451 | 'plugins_screen_url' => self_admin_url( 'plugins.php#stream' ), |
| 452 | ) |
| 453 | ); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Check whether or not the current admin screen belongs to Stream. |
| 458 | * |
| 459 | * @return bool TRUE|FALSE. |
| 460 | */ |
| 461 | public function is_stream_screen() { |
| 462 | if ( ! isset( $_GET['page'] ) ) { |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | $page = wp_mainwp_stream_filter_input( INPUT_GET, 'page' ); |
| 467 | |
| 468 | if ( empty( $page ) ) { |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | if ( is_admin() && false !== strpos( $page, $this->records_page_slug ) ) { |
| 473 | return true; |
| 474 | } |
| 475 | |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Add a specific body class to all Stream admin screens. |
| 481 | * |
| 482 | * @param string $classes CSS classes to output to body. |
| 483 | * |
| 484 | * @filter admin_body_class |
| 485 | * |
| 486 | * @return string Return body class. |
| 487 | */ |
| 488 | public function admin_body_class( $classes ) { |
| 489 | $stream_classes = array(); |
| 490 | |
| 491 | if ( $this->is_stream_screen() ) { |
| 492 | $stream_classes[] = $this->admin_body_class; |
| 493 | |
| 494 | if ( isset( $_GET['page'] ) ) { // CSRF okay |
| 495 | $stream_classes[] = sanitize_key( $_GET['page'] ); // input var okay, CSRF okay. |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Filter the Stream admin body classes. |
| 501 | * |
| 502 | * @return array Stream array. |
| 503 | */ |
| 504 | $stream_classes = apply_filters( 'wp_mainwp_stream_admin_body_classes', $stream_classes ); |
| 505 | $stream_classes = implode( ' ', array_map( 'trim', $stream_classes ) ); |
| 506 | |
| 507 | return sprintf( '%s %s ', $classes, $stream_classes ); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Add menu styles for various WP Admin skins. |
| 512 | * |
| 513 | * @uses \wp_add_inline_style() |
| 514 | * |
| 515 | * @action admin_enqueue_scripts |
| 516 | */ |
| 517 | public function admin_menu_css() { |
| 518 | wp_register_style( 'wp-mainwp-stream-datepicker', $this->plugin->locations['url'] . 'ui/css/datepicker.css', array(), $this->plugin->get_version() ); |
| 519 | wp_register_style( 'wp-mainwp-stream-icons', $this->plugin->locations['url'] . 'ui/stream-icons/style.css', array(), $this->plugin->get_version() ); |
| 520 | |
| 521 | // Make sure we're working off a clean version |
| 522 | if ( ! file_exists( ABSPATH . WPINC . '/version.php' ) ) { |
| 523 | return; |
| 524 | } |
| 525 | include ABSPATH . WPINC . '/version.php'; |
| 526 | |
| 527 | if ( ! isset( $wp_version ) ) { |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | $body_class = $this->admin_body_class; |
| 532 | $records_page = $this->records_page_slug; |
| 533 | $stream_url = $this->plugin->locations['url']; |
| 534 | |
| 535 | if ( version_compare( $wp_version, '3.8-alpha', '>=' ) ) { |
| 536 | wp_enqueue_style( 'wp-mainwp-stream-icons' ); |
| 537 | |
| 538 | $css = " |
| 539 | #settings_page_{$records_page} .wp-menu-image:before { |
| 540 | font-family: 'WP Stream' !important; |
| 541 | content: '\\73' !important; |
| 542 | } |
| 543 | #settings_page_{$records_page} .wp-menu-image { |
| 544 | background-repeat: no-repeat; |
| 545 | } |
| 546 | #menu-posts-feedback .wp-menu-image:before { |
| 547 | font-family: dashicons !important; |
| 548 | content: '\\f175'; |
| 549 | } |
| 550 | #adminmenu #menu-posts-feedback div.wp-menu-image { |
| 551 | background: none !important; |
| 552 | background-repeat: no-repeat; |
| 553 | } |
| 554 | body.{$body_class} #wpbody-content .wrap h1:nth-child(1):before { |
| 555 | font-family: 'WP Stream' !important; |
| 556 | content: '\\73'; |
| 557 | padding: 0 8px 0 0; |
| 558 | } |
| 559 | "; |
| 560 | } else { |
| 561 | $css = " |
| 562 | #settings_page_{$records_page} .wp-menu-image { |
| 563 | background: url( {$stream_url}ui/stream-icons/menuicon-sprite.png ) 0 90% no-repeat; |
| 564 | } |
| 565 | /* Retina Stream Menu Icon */ |
| 566 | @media only screen and (-moz-min-device-pixel-ratio: 1.5), |
| 567 | only screen and (-o-min-device-pixel-ratio: 3/2), |
| 568 | only screen and (-webkit-min-device-pixel-ratio: 1.5), |
| 569 | only screen and (min-device-pixel-ratio: 1.5) { |
| 570 | #settings_page_{$records_page} .wp-menu-image { |
| 571 | background: url( {$stream_url}ui/stream-icons/menuicon-sprite-2x.png ) 0 90% no-repeat; |
| 572 | background-size:30px 64px; |
| 573 | } |
| 574 | } |
| 575 | #settings_page_{$records_page}.current .wp-menu-image, |
| 576 | #settings_page_{$records_page}.wp-has-current-submenu .wp-menu-image, |
| 577 | #settings_page_{$records_page}:hover .wp-menu-image { |
| 578 | background-position: top left; |
| 579 | } |
| 580 | "; |
| 581 | } |
| 582 | |
| 583 | \wp_add_inline_style( 'wp-admin', $css ); |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Handle the reset AJAX request to reset logs. |
| 588 | * |
| 589 | * @return bool TRUE|FASLE. |
| 590 | */ |
| 591 | public function wp_ajax_reset() { |
| 592 | check_ajax_referer( 'stream_nonce_reset', 'wp_mainwp_stream_nonce_reset' ); |
| 593 | |
| 594 | if ( ! current_user_can( $this->settings_cap ) ) { |
| 595 | wp_die( |
| 596 | esc_html__( "You don't have sufficient privileges to do this action.", 'mainwp-child-reports' ) |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | $this->erase_stream_records(); |
| 601 | |
| 602 | do_action( 'wp_mainwp_child_reposts_recreate_tables_if_not_exist' ); |
| 603 | |
| 604 | if ( defined( 'WP_MAINWP_STREAM_TESTS' ) && WP_MAINWP_STREAM_TESTS ) { |
| 605 | return true; |
| 606 | } |
| 607 | |
| 608 | wp_redirect( |
| 609 | add_query_arg( |
| 610 | array( |
| 611 | 'page' => is_network_admin() ? $this->network->network_settings_page_slug : $this->settings_page_slug, |
| 612 | 'message' => 'data_erased', |
| 613 | ), |
| 614 | self_admin_url( $this->admin_parent_page ) |
| 615 | ) |
| 616 | ); |
| 617 | |
| 618 | exit; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | /** |
| 623 | * Erase stream records. |
| 624 | */ |
| 625 | private function erase_stream_records() { |
| 626 | |
| 627 | /** @global object $wpdb WordPress database object. */ |
| 628 | global $wpdb; |
| 629 | |
| 630 | $where = ''; |
| 631 | |
| 632 | if ( is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) { |
| 633 | $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() ); |
| 634 | } |
| 635 | |
| 636 | $wpdb->query( |
| 637 | "DELETE `stream`, `meta` |
| 638 | FROM {$wpdb->mainwp_stream} AS `stream` |
| 639 | LEFT JOIN {$wpdb->mainwp_streammeta} AS `meta` |
| 640 | ON `meta`.`record_id` = `stream`.`ID` |
| 641 | WHERE 1=1 {$where};" // @codingStandardsIgnoreLine $where already prepared |
| 642 | ); |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Purge Schedule Setup. |
| 647 | */ |
| 648 | public function purge_schedule_setup() { |
| 649 | if ( ! wp_next_scheduled( 'wp_mainwp_stream_auto_purge' ) ) { |
| 650 | wp_schedule_event( time(), 'twicedaily', 'wp_mainwp_stream_auto_purge' ); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Purge Scheduled action. |
| 656 | * |
| 657 | * @throws \Exception |
| 658 | */ |
| 659 | public function purge_scheduled_action() { |
| 660 | |
| 661 | /** @global object $wpdb WordPress database object. */ |
| 662 | global $wpdb; |
| 663 | |
| 664 | // Don't purge when in Network Admin unless Stream is network activated. |
| 665 | if ( |
| 666 | is_multisite() |
| 667 | && |
| 668 | is_network_admin() |
| 669 | && |
| 670 | ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) |
| 671 | ) { |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) { |
| 676 | $options = (array) get_site_option( 'wp_mainwp_stream_network', array() ); |
| 677 | } else { |
| 678 | $options = (array) get_option( 'wp_mainwp_stream', array() ); |
| 679 | } |
| 680 | |
| 681 | if ( ! empty( $options['general_keep_records_indefinitely'] ) ) { |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | if ( ! isset( $options['general_records_ttl'] ) ) { |
| 686 | $days = 100; |
| 687 | } else { |
| 688 | $days = $options['general_records_ttl']; |
| 689 | } |
| 690 | |
| 691 | if ( empty( $days ) ) { |
| 692 | $days = 1; |
| 693 | } |
| 694 | |
| 695 | $timezone = new DateTimeZone( 'UTC' ); |
| 696 | $date = new DateTime( 'now', $timezone ); |
| 697 | |
| 698 | $date->sub( DateInterval::createFromDateString( "$days days" ) ); |
| 699 | |
| 700 | // $where = $wpdb->prepare( ' AND `stream`.`created` < %s', $date->format( 'Y-m-d H:i:s' ) ); |
| 701 | $where = ' AND `stream`.`created` < STR_TO_DATE(' . $wpdb->prepare( '%s', $date->format( 'Y-m-d H:i:s' ) ) . ", '%Y-%m-%d %H:%i:%s') "; |
| 702 | |
| 703 | // Multisite but NOT network activated, only purge the current blog |
| 704 | if ( is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) { |
| 705 | $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() ); |
| 706 | } |
| 707 | |
| 708 | $wpdb->query( |
| 709 | "DELETE `stream`, `meta` |
| 710 | FROM {$wpdb->mainwp_stream} AS `stream` |
| 711 | LEFT JOIN {$wpdb->mainwp_streammeta} AS `meta` |
| 712 | ON `meta`.`record_id` = `stream`.`ID` |
| 713 | WHERE 1=1 {$where};" // @codingStandardsIgnoreLine $where already prepared |
| 714 | ); |
| 715 | } |
| 716 | |
| 717 | |
| 718 | |
| 719 | /** |
| 720 | * Render main page. |
| 721 | * |
| 722 | * @deprecated Disabled. |
| 723 | */ |
| 724 | public function render_list_table() { |
| 725 | $this->list_table->prepare_items(); |
| 726 | ?> |
| 727 | <div class="wrap"> |
| 728 | <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
| 729 | <?php $this->list_table->display(); ?> |
| 730 | </div> |
| 731 | <?php |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Render settings page. |
| 736 | * |
| 737 | * @deprecated Disabled. |
| 738 | */ |
| 739 | public function render_settings_page() { |
| 740 | $option_key = $this->plugin->settings->option_key; |
| 741 | $form_action = apply_filters( 'wp_mainwp_stream_settings_form_action', admin_url( 'options.php' ) ); |
| 742 | |
| 743 | $page_description = apply_filters( 'wp_mainwp_stream_settings_form_description', '' ); |
| 744 | |
| 745 | $sections = $this->plugin->settings->get_fields(); |
| 746 | $active_tab = wp_mainwp_stream_filter_input( INPUT_GET, 'tab' ); |
| 747 | wp_enqueue_script( 'wp-mainwp-stream-settings', $this->plugin->locations['url'] . 'ui/js/settings.js', array( 'jquery' ), $this->plugin->get_version(), true ); |
| 748 | ?> |
| 749 | <div class="wrap"> |
| 750 | <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
| 751 | |
| 752 | <?php if ( ! empty( $page_description ) ) : ?> |
| 753 | <p><?php echo esc_html( $page_description ); ?></p> |
| 754 | <?php endif; ?> |
| 755 | |
| 756 | <?php settings_errors(); ?> |
| 757 | |
| 758 | <?php if ( count( $sections ) > 1 ) : ?> |
| 759 | <h2 class="nav-tab-wrapper"> |
| 760 | <?php $i = 0; ?> |
| 761 | <?php foreach ( $sections as $section => $data ) : ?> |
| 762 | <?php $i++; ?> |
| 763 | <?php $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section ); ?> |
| 764 | <a href="<?php echo esc_url( add_query_arg( 'tab', $section ) ); ?>" class="nav-tab <?php echo $is_active ? esc_attr( ' nav-tab-active' ) : ''; ?>"> |
| 765 | <?php echo esc_html( $data['title'] ); ?> |
| 766 | </a> |
| 767 | <?php endforeach; ?> |
| 768 | </h2> |
| 769 | <?php endif; ?> |
| 770 | |
| 771 | <div class="nav-tab-content" id="tab-content-settings"> |
| 772 | <form method="post" action="<?php echo esc_attr( $form_action ); ?>" enctype="multipart/form-data"> |
| 773 | <div class="settings-sections"> |
| 774 | <?php |
| 775 | $i = 0; |
| 776 | foreach ( $sections as $section => $data ) { |
| 777 | $i++; |
| 778 | |
| 779 | $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section ); |
| 780 | |
| 781 | if ( $is_active ) { |
| 782 | settings_fields( $option_key ); |
| 783 | do_settings_sections( $option_key ); |
| 784 | } |
| 785 | } |
| 786 | ?> |
| 787 | </div> |
| 788 | <?php submit_button(); ?> |
| 789 | </form> |
| 790 | </div> |
| 791 | </div> |
| 792 | <?php |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Instantiate the list table. |
| 797 | * |
| 798 | * @uses \WP_MainWP_Stream\List_Table |
| 799 | */ |
| 800 | public function register_list_table() { |
| 801 | $this->list_table = new List_Table( |
| 802 | $this->plugin, |
| 803 | array( |
| 804 | 'screen' => $this->screen_id['main'], |
| 805 | ) |
| 806 | ); |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Check if a particular role has access |
| 811 | * |
| 812 | * @param string $role User role. |
| 813 | * |
| 814 | * @return bool TRUE|FALSE |
| 815 | * @deprecated Disabled. |
| 816 | */ |
| 817 | private function role_can_view( $role ) { |
| 818 | if ( in_array( $role, $this->plugin->settings->options['general_role_access'], true ) ) { |
| 819 | return true; |
| 820 | } |
| 821 | |
| 822 | return false; |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * Filter user caps to dynamically grant our view cap based on allowed roles. |
| 827 | * |
| 828 | * @param array $allcaps All capabilities. |
| 829 | * @param array $caps Capabilities. |
| 830 | * @param array $args Arguments. |
| 831 | * @param string $user User. |
| 832 | * |
| 833 | * @filter user_has_cap |
| 834 | * |
| 835 | * @return array All capabilities. |
| 836 | * |
| 837 | * @deprecated Disabled. |
| 838 | */ |
| 839 | public function filter_user_caps( $allcaps, $caps, $args, $user = null ) { |
| 840 | |
| 841 | /** @global object $wp_roles Core class used to implement a user roles API. */ |
| 842 | global $wp_roles; |
| 843 | |
| 844 | $_wp_roles = isset( $wp_roles ) ? $wp_roles : new WP_Roles(); |
| 845 | |
| 846 | $user = is_a( $user, 'WP_User' ) ? $user : wp_get_current_user(); |
| 847 | |
| 848 | // @see |
| 849 | // https://github.com/WordPress/WordPress/blob/c67c9565f1495255807069fdb39dac914046b1a0/wp-includes/capabilities.php#L758 |
| 850 | $roles = array_unique( |
| 851 | array_merge( |
| 852 | $user->roles, |
| 853 | array_filter( |
| 854 | array_keys( $user->caps ), |
| 855 | array( $_wp_roles, 'is_role' ) |
| 856 | ) |
| 857 | ) |
| 858 | ); |
| 859 | |
| 860 | $stream_view_caps = array( $this->view_cap ); |
| 861 | |
| 862 | foreach ( $caps as $cap ) { |
| 863 | if ( in_array( $cap, $stream_view_caps, true ) ) { |
| 864 | foreach ( $roles as $role ) { |
| 865 | if ( $this->role_can_view( $role ) ) { |
| 866 | $allcaps[ $cap ] = true; |
| 867 | |
| 868 | break 2; |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | return $allcaps; |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * Filter role caps to dynamically grant our view cap based on allowed roles. |
| 879 | * |
| 880 | * @filter role_has_cap |
| 881 | * |
| 882 | * @param array $allcaps All capabilities. |
| 883 | * @param array $cap Capabilities. |
| 884 | * @param string $role User role. |
| 885 | * |
| 886 | * @return array All capabilities. |
| 887 | * @deprecated Disabled. |
| 888 | */ |
| 889 | public function filter_role_caps( $allcaps, $cap, $role ) { |
| 890 | $stream_view_caps = array( $this->view_cap ); |
| 891 | |
| 892 | if ( in_array( $cap, $stream_view_caps, true ) && $this->role_can_view( $role ) ) { |
| 893 | $allcaps[ $cap ] = true; |
| 894 | } |
| 895 | |
| 896 | return $allcaps; |
| 897 | } |
| 898 | |
| 899 | /** |
| 900 | * Ajax stream filters. |
| 901 | * |
| 902 | * @action wp_ajax_wp_mainwp_stream_filters |
| 903 | */ |
| 904 | public function ajax_filters() { |
| 905 | if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( $this->plugin->admin->settings_cap ) ) { |
| 906 | wp_die( '-1' ); |
| 907 | } |
| 908 | |
| 909 | check_ajax_referer( 'mainwp_stream_filters_user_search_nonce', 'nonce' ); |
| 910 | |
| 911 | switch ( wp_mainwp_stream_filter_input( INPUT_GET, 'filter' ) ) { |
| 912 | case 'user_id': |
| 913 | $users = array_merge( |
| 914 | array( |
| 915 | 0 => (object) array( |
| 916 | 'display_name' => 'WP-CLI', |
| 917 | ), |
| 918 | ), |
| 919 | get_users() |
| 920 | ); |
| 921 | |
| 922 | $search = wp_mainwp_stream_filter_input( INPUT_GET, 'q' ); |
| 923 | if ( $search ) { |
| 924 | // `search` arg for get_users() is not enough |
| 925 | $users = array_filter( |
| 926 | $users, |
| 927 | function ( $user ) use ( $search ) { |
| 928 | return false !== \mb_strpos( \mb_strtolower( $user->display_name ), \mb_strtolower( $search ) ); |
| 929 | } |
| 930 | ); |
| 931 | } |
| 932 | |
| 933 | if ( count( $users ) > $this->preload_users_max ) { |
| 934 | $users = array_slice( $users, 0, $this->preload_users_max ); |
| 935 | } |
| 936 | |
| 937 | // Get gravatar / roles for final result set |
| 938 | $results = $this->get_users_record_meta( $users ); |
| 939 | |
| 940 | break; |
| 941 | } |
| 942 | |
| 943 | if ( isset( $results ) ) { |
| 944 | echo wp_mainwp_stream_json_encode( $results ); // xss ok |
| 945 | } |
| 946 | |
| 947 | die(); |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Get users record meta. |
| 952 | * |
| 953 | * @param array $authors Authors array. |
| 954 | * |
| 955 | * @return array Return author records. |
| 956 | * |
| 957 | * @uses \WP_MainWP_Stream\Author |
| 958 | */ |
| 959 | public function get_users_record_meta( $authors ) { |
| 960 | $authors_records = array(); |
| 961 | |
| 962 | foreach ( $authors as $user_id => $args ) { |
| 963 | $author = new Author( $args->ID ); |
| 964 | |
| 965 | $authors_records[ $user_id ] = array( |
| 966 | 'text' => $author->get_display_name(), |
| 967 | 'id' => $author->id, |
| 968 | 'label' => $author->get_display_name(), |
| 969 | 'icon' => $author->get_avatar_src( 32 ), |
| 970 | 'title' => '', |
| 971 | ); |
| 972 | } |
| 973 | |
| 974 | return $authors_records; |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * Get user meta in a way that is also safe for VIP. |
| 979 | * |
| 980 | * @param int $user_id User ID. |
| 981 | * @param string $meta_key Meta Key. |
| 982 | * @param bool $single Single record check. (optional). |
| 983 | * |
| 984 | * @return array|bool Return user meta array or FALSE on failure. |
| 985 | */ |
| 986 | public function get_user_meta( $user_id, $meta_key, $single = true ) { |
| 987 | if ( wp_mainwp_stream_is_vip() && function_exists( 'get_user_attribute' ) ) { |
| 988 | return get_user_attribute( $user_id, $meta_key ); |
| 989 | } |
| 990 | |
| 991 | return get_user_meta( $user_id, $meta_key, $single ); |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * Update user meta in a way that is also safe for VIP. |
| 996 | * |
| 997 | * @param int $user_id User ID. |
| 998 | * @param string $meta_key Meta Key. |
| 999 | * @param mixed $meta_value Meta value. |
| 1000 | * @param mixed $prev_value Previouse Meta value. (optional). |
| 1001 | * |
| 1002 | * @return int|bool Return ID of record updated or FALSE on failure. |
| 1003 | */ |
| 1004 | public function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 1005 | if ( wp_mainwp_stream_is_vip() && function_exists( 'update_user_attribute' ) ) { |
| 1006 | return update_user_attribute( $user_id, $meta_key, $meta_value ); |
| 1007 | } |
| 1008 | |
| 1009 | return update_user_meta( $user_id, $meta_key, $meta_value, $prev_value ); |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * Delete user meta in a way that is also safe for VIP. |
| 1014 | * |
| 1015 | * @param int $user_id User ID. |
| 1016 | * @param string $meta_key Meta Key. |
| 1017 | * @param mixed $meta_value Meta value (optional). |
| 1018 | * |
| 1019 | * @return bool TRUE|FALSE. |
| 1020 | */ |
| 1021 | public function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) { |
| 1022 | if ( wp_mainwp_stream_is_vip() && function_exists( 'delete_user_attribute' ) ) { |
| 1023 | return delete_user_attribute( $user_id, $meta_key, $meta_value ); |
| 1024 | } |
| 1025 | |
| 1026 | return delete_user_meta( $user_id, $meta_key, $meta_value ); |
| 1027 | } |
| 1028 | } |
| 1029 |