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
settings.php
701 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Settings class |
| 4 | * |
| 5 | * @author X-Team <x-team.com> |
| 6 | * @author Shady Sharaf <shady@x-team.com> |
| 7 | */ |
| 8 | class MainWP_WP_Stream_Settings { |
| 9 | |
| 10 | const KEY = 'mainwp_wp_stream'; |
| 11 | const NETWORK_KEY = 'mainwp_wp_stream_network'; |
| 12 | const DEFAULTS_KEY = 'mainwp_wp_stream_defaults'; |
| 13 | public static $options = array(); |
| 14 | public static $option_key = ''; |
| 15 | public static $fields = array(); |
| 16 | public static function load() { |
| 17 | self::$option_key = self::get_option_key(); |
| 18 | self::$options = self::get_options(); |
| 19 | |
| 20 | // Register settings, and fields |
| 21 | add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); |
| 22 | |
| 23 | // Check if we need to flush rewrites rules |
| 24 | add_action( 'update_option_' . self::KEY, array( __CLASS__, 'updated_option_trigger_flush_rules' ), 10, 2 ); |
| 25 | |
| 26 | // Remove records when records TTL is shortened |
| 27 | add_action( 'update_option_' . self::KEY, array( __CLASS__, 'updated_option_ttl_remove_records' ), 10, 2 ); |
| 28 | |
| 29 | add_filter( 'mainwp_wp_stream_serialized_labels', array( __CLASS__, 'get_settings_translations' ) ); |
| 30 | |
| 31 | // Ajax callback function to search users |
| 32 | add_action( 'wp_ajax_mainwp_stream_get_users', array( __CLASS__, 'get_users' ) ); |
| 33 | |
| 34 | // Ajax callback function to search IPs |
| 35 | add_action( 'wp_ajax_mainwp_stream_get_ips', array( __CLASS__, 'get_ips' ) ); |
| 36 | } |
| 37 | |
| 38 | public static function get_users(){ |
| 39 | if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( MainWP_WP_Stream_Admin::SETTINGS_CAP ) ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | check_ajax_referer( 'stream_get_users', 'nonce' ); |
| 44 | |
| 45 | $response = (object) array( |
| 46 | 'status' => false, |
| 47 | 'message' => esc_html__( 'There was an error in the request', 'mainwp-child-reports' ), |
| 48 | ); |
| 49 | |
| 50 | $search = ( isset( $_POST['find'] )? wp_unslash( trim( $_POST['find'] ) ) : '' ); |
| 51 | $request = (object) array( |
| 52 | 'find' => $search, |
| 53 | ); |
| 54 | |
| 55 | add_filter( 'user_search_columns', array( __CLASS__, 'add_display_name_search_columns' ), 10, 3 ); |
| 56 | |
| 57 | $users = new WP_User_Query( |
| 58 | array( |
| 59 | 'search' => "*{$request->find}*", |
| 60 | 'search_columns' => array( |
| 61 | 'user_login', |
| 62 | 'user_nicename', |
| 63 | 'user_email', |
| 64 | 'user_url', |
| 65 | ), |
| 66 | 'orderby' => 'display_name', |
| 67 | 'number' => MainWP_WP_Stream_Admin::PRELOAD_AUTHORS_MAX, |
| 68 | ) |
| 69 | ); |
| 70 | |
| 71 | remove_filter( 'user_search_columns', array( __CLASS__, 'add_display_name_search_columns' ), 10 ); |
| 72 | |
| 73 | if ( 0 === $users->get_total() ) { |
| 74 | wp_send_json_error( $response ); |
| 75 | } |
| 76 | |
| 77 | $response->status = true; |
| 78 | $response->message = ''; |
| 79 | $response->users = array(); |
| 80 | |
| 81 | require_once MAINWP_WP_STREAM_INC_DIR . 'class-wp-stream-author.php'; |
| 82 | |
| 83 | foreach ( $users->results as $key => $user ) { |
| 84 | $author = new MainWP_WP_Stream_Author( $user->ID ); |
| 85 | |
| 86 | $args = array( |
| 87 | 'id' => $author->ID, |
| 88 | 'text' => $author->display_name, |
| 89 | ); |
| 90 | |
| 91 | $args['tooltip'] = esc_attr( |
| 92 | sprintf( |
| 93 | __( "ID: %d\nUser: %s\nEmail: %s\nRole: %s", 'mainwp-child-reports' ), |
| 94 | $author->id, |
| 95 | $author->user_login, |
| 96 | $author->user_email, |
| 97 | ucwords( $author->get_role() ) |
| 98 | ) |
| 99 | ); |
| 100 | |
| 101 | $args['icon'] = $author->get_avatar_src( 32 ); |
| 102 | |
| 103 | $response->users[] = $args; |
| 104 | } |
| 105 | |
| 106 | if ( empty( $search ) || preg_match( '/wp|cli|system|unknown/i', $search ) ) { |
| 107 | $author = new MainWP_WP_Stream_Author( 0 ); |
| 108 | $response->users[] = array( |
| 109 | 'id' => $author->id, |
| 110 | 'text' => $author->get_display_name(), |
| 111 | 'icon' => $author->get_avatar_src( 32 ), |
| 112 | 'tooltip' => esc_html__( 'Actions performed by the system when a user is not logged in (e.g. auto site upgrader, or invoking WP-CLI without --user)', 'mainwp-child-reports' ), |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | wp_send_json_success( $response ); |
| 117 | } |
| 118 | |
| 119 | public static function get_ips(){ |
| 120 | if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( MainWP_WP_Stream_Admin::SETTINGS_CAP ) ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | check_ajax_referer( 'stream_get_ips', 'nonce' ); |
| 125 | |
| 126 | global $wpdb; |
| 127 | |
| 128 | $results = $wpdb->get_col( |
| 129 | $wpdb->prepare( |
| 130 | " |
| 131 | SELECT distinct(`ip`) |
| 132 | FROM `{$wpdb->mainwp_reports}` |
| 133 | WHERE `ip` LIKE %s |
| 134 | ORDER BY inet_aton(`ip`) ASC |
| 135 | LIMIT %d; |
| 136 | ", |
| 137 | like_escape( $_POST['find'] ) . '%', |
| 138 | $_POST['limit'] |
| 139 | ) |
| 140 | ); |
| 141 | |
| 142 | wp_send_json_success( $results ); |
| 143 | } |
| 144 | |
| 145 | public static function add_display_name_search_columns( $search_columns, $search, $query ){ |
| 146 | $search_columns[] = 'display_name'; |
| 147 | |
| 148 | return $search_columns; |
| 149 | } |
| 150 | |
| 151 | public static function get_option_key() { |
| 152 | $option_key = self::KEY; |
| 153 | |
| 154 | $current_page = mainwp_wp_stream_filter_input( INPUT_GET, 'page' ); |
| 155 | |
| 156 | if ( ! $current_page ) { |
| 157 | $current_page = mainwp_wp_stream_filter_input( INPUT_GET, 'action' ); |
| 158 | } |
| 159 | |
| 160 | if ( 'mainwp_wp_stream_default_settings' === $current_page ) { |
| 161 | $option_key = self::DEFAULTS_KEY; |
| 162 | } |
| 163 | |
| 164 | if ( 'mainwp_wp_stream_network_settings' === $current_page ) { |
| 165 | $option_key = self::NETWORK_KEY; |
| 166 | } |
| 167 | |
| 168 | return apply_filters( 'mainwp_wp_stream_settings_option_key', $option_key ); |
| 169 | } |
| 170 | |
| 171 | public static function get_fields() { |
| 172 | if ( empty( self::$fields ) ) { |
| 173 | if (!class_exists('MainWP_WP_Stream_Admin')) |
| 174 | require_once MAINWP_WP_STREAM_INC_DIR . 'admin.php'; |
| 175 | $branding_text = MainWP_WP_Stream_Admin::get_branding_title(); |
| 176 | $branding_text = !empty($branding_text) ? 'Reset ' . $branding_text . ' Reports Database' : esc_html__( 'Reset MainWP Child Reports Database', 'mainwp-child-reports' ); |
| 177 | $branding_name = !empty($branding_text) ? $branding_text : 'MainWP Child'; |
| 178 | $chk_label = 'Hide ' . $branding_name . ' and ' . $branding_name . ' Reports from reports'; |
| 179 | $chk_desc = 'If selected, the ' . $branding_name . ' plugin and the ' . $branding_name . ' Reports plugin will be left out from reports for this site.'; |
| 180 | $hide_child_plugins = get_option('mainwp_creport_hide_child_plugins', 'yes'); |
| 181 | // to fix can not set default checked checkbox |
| 182 | $checkbox_hide_childs = '<tr><th scope="row"><label for="mainwp_creport_hide_child_plugins">' . $chk_label; |
| 183 | $checkbox_hide_childs .= '</label></th><td><label><input name="mainwp_creport_hide_child_plugins" id="mainwp_creport_hide_child_plugins" value="1" type="checkbox" ' . ($hide_child_plugins == 'yes' ? 'checked' : '') . '> '; |
| 184 | $checkbox_hide_childs .= '</label><p class="description">' . $chk_desc . '.</p></td></tr>'; |
| 185 | |
| 186 | self::$fields = array( |
| 187 | 'general' => array( |
| 188 | 'title' => esc_html__( 'General', 'default' ), |
| 189 | 'fields' => array( |
| 190 | array( |
| 191 | 'name' => 'records_ttl', |
| 192 | 'title' => esc_html__( 'Keep Records for', 'mainwp-child-reports' ), |
| 193 | 'type' => 'number', |
| 194 | 'class' => 'small-text', |
| 195 | 'desc' => esc_html__( 'Maximum number of days to keep activity records. Leave blank to keep records forever.', 'mainwp-child-reports' ), |
| 196 | 'default' => 180, |
| 197 | 'after_field' => esc_html__( 'days', 'mainwp-child-reports' ), |
| 198 | ), |
| 199 | array( |
| 200 | 'name' => 'period_of_time', |
| 201 | 'title' => esc_html__( 'Minimum time between posts/pages update reports', 'mainwp-child-reports' ), |
| 202 | 'type' => 'select', |
| 203 | 'choices' => array( '0' => '0', '30' => '30', '60' => '60', '90' => '90', '120' => '120'), |
| 204 | 'desc' => '', |
| 205 | 'default' => 30, |
| 206 | 'current_value' => array( '30' ), |
| 207 | 'after_field' => esc_html__( 'minutes', 'mainwp-child-reports' ) . $checkbox_hide_childs, // to add checkbox |
| 208 | ), |
| 209 | array( |
| 210 | 'name' => 'delete_all_records', |
| 211 | 'title' => 'Reset ' . $branding_name . ' Reports Database', |
| 212 | 'type' => 'link', |
| 213 | 'href' => add_query_arg( |
| 214 | array( |
| 215 | 'action' => 'mainwp_wp_stream_reset', |
| 216 | 'mainwp_wp_stream_nonce' => wp_create_nonce( 'stream_nonce' ), |
| 217 | ), |
| 218 | admin_url( 'admin-ajax.php' ) |
| 219 | ), |
| 220 | 'desc' => esc_html__( 'Warning: Clicking this will delete all activity records from the database.', 'mainwp-child-reports' ), |
| 221 | 'default' => 0, |
| 222 | ), |
| 223 | ), |
| 224 | ), |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | return self::$fields; |
| 229 | } |
| 230 | |
| 231 | public static function get_options() { |
| 232 | $option_key = self::$option_key; |
| 233 | |
| 234 | $defaults = self::get_defaults( $option_key ); |
| 235 | |
| 236 | if ( self::DEFAULTS_KEY === $option_key ) { |
| 237 | return $defaults; |
| 238 | } |
| 239 | |
| 240 | return apply_filters( |
| 241 | 'mainwp_wp_stream_options', |
| 242 | wp_parse_args( |
| 243 | (array) get_option( $option_key, array() ), |
| 244 | $defaults |
| 245 | ), |
| 246 | $option_key |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | public static function get_defaults() { |
| 251 | $fields = self::get_fields(); |
| 252 | $defaults = array(); |
| 253 | |
| 254 | foreach ( $fields as $section_name => $section ) { |
| 255 | foreach ( $section['fields'] as $field ) { |
| 256 | $defaults[ $section_name.'_'.$field['name'] ] = isset( $field['default'] ) |
| 257 | ? $field['default'] |
| 258 | : null; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return apply_filters( |
| 263 | 'mainwp_wp_stream_option_defaults', |
| 264 | wp_parse_args( |
| 265 | (array) get_site_option( self::DEFAULTS_KEY, array() ), |
| 266 | $defaults |
| 267 | ) |
| 268 | ); |
| 269 | } |
| 270 | |
| 271 | public static function register_settings() { |
| 272 | $sections = self::get_fields(); |
| 273 | |
| 274 | register_setting( self::$option_key, self::$option_key, array( 'MainWP_WP_Stream_Settings', 'sanitize_settings' ) ); |
| 275 | |
| 276 | foreach ( $sections as $section_name => $section ) { |
| 277 | add_settings_section( |
| 278 | $section_name, |
| 279 | null, |
| 280 | '__return_false', |
| 281 | self::$option_key |
| 282 | ); |
| 283 | |
| 284 | foreach ( $section['fields'] as $field_idx => $field ) { |
| 285 | if ( ! isset( $field['type'] ) ) { // No field type associated, skip, no GUI |
| 286 | continue; |
| 287 | } |
| 288 | add_settings_field( |
| 289 | $field['name'], |
| 290 | $field['title'], |
| 291 | ( isset( $field['callback'] ) ? $field['callback'] : array( __CLASS__, 'output_field' ) ), |
| 292 | self::$option_key, |
| 293 | $section_name, |
| 294 | $field + array( |
| 295 | 'section' => $section_name, |
| 296 | 'label_for' => sprintf( '%s_%s_%s', self::$option_key, $section_name, $field['name'] ), // xss ok |
| 297 | ) |
| 298 | ); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | public static function sanitize_settings( $input ) { |
| 304 | if (isset($_POST['mainwp_creport_hide_child_plugins'])) { |
| 305 | update_option('mainwp_creport_hide_child_plugins', 'yes'); |
| 306 | } else { |
| 307 | update_option('mainwp_creport_hide_child_plugins', 'no'); |
| 308 | } |
| 309 | return $input; |
| 310 | } |
| 311 | |
| 312 | public static function updated_option_trigger_flush_rules( $old_value, $new_value ) { |
| 313 | if ( is_array( $new_value ) && is_array( $old_value ) ) { |
| 314 | $new_value = ( array_key_exists( 'general_private_feeds', $new_value ) ) ? $new_value['general_private_feeds'] : 0; |
| 315 | $old_value = ( array_key_exists( 'general_private_feeds', $old_value ) ) ? $old_value['general_private_feeds'] : 0; |
| 316 | |
| 317 | if ( $new_value !== $old_value ) { |
| 318 | delete_option( 'rewrite_rules' ); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | public static function render_field( $field ) { |
| 324 | $output = null; |
| 325 | $type = isset( $field['type'] ) ? $field['type'] : null; |
| 326 | $section = isset( $field['section'] ) ? $field['section'] : null; |
| 327 | $name = isset( $field['name'] ) ? $field['name'] : null; |
| 328 | $class = isset( $field['class'] ) ? $field['class'] : null; |
| 329 | $placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : null; |
| 330 | $description = isset( $field['desc'] ) ? $field['desc'] : null; |
| 331 | $href = isset( $field['href'] ) ? $field['href'] : null; |
| 332 | $after_field = isset( $field['after_field'] ) ? $field['after_field'] : null; |
| 333 | $default = isset( $field['default'] ) ? $field['default'] : null; |
| 334 | $title = isset( $field['title'] ) ? $field['title'] : null; |
| 335 | $nonce = isset( $field['nonce'] ) ? $field['nonce'] : null; |
| 336 | $current_value = self::$options[ $section . '_' . $name ]; |
| 337 | $option_key = self::$option_key; |
| 338 | |
| 339 | |
| 340 | if ( is_callable( $current_value ) ) { |
| 341 | $current_value = call_user_func( $current_value ); |
| 342 | } |
| 343 | |
| 344 | if ( ! $type || ! $section || ! $name ) { |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | if ( 'multi_checkbox' === $type |
| 349 | && ( empty( $field['choices'] ) || ! is_array( $field['choices'] ) ) |
| 350 | ) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | switch ( $type ) { |
| 355 | case 'text': |
| 356 | case 'number': |
| 357 | $output = sprintf( |
| 358 | '<input type="%1$s" name="%2$s[%3$s_%4$s]" id="%2$s_%3$s_%4$s" class="%5$s" placeholder="%6$s" value="%7$s" /> %8$s', |
| 359 | esc_attr( $type ), |
| 360 | esc_attr( $option_key ), |
| 361 | esc_attr( $section ), |
| 362 | esc_attr( $name ), |
| 363 | esc_attr( $class ), |
| 364 | esc_attr( $placeholder ), |
| 365 | esc_attr( $current_value ), |
| 366 | $after_field // xss ok |
| 367 | ); |
| 368 | break; |
| 369 | case 'checkbox': |
| 370 | $output = sprintf( |
| 371 | '<label><input type="checkbox" name="%1$s[%2$s_%3$s]" id="%1$s[%2$s_%3$s]" value="1" %4$s /> %5$s</label>', |
| 372 | esc_attr( $option_key ), |
| 373 | esc_attr( $section ), |
| 374 | esc_attr( $name ), |
| 375 | checked( $current_value, 1, false ), |
| 376 | $after_field // xss ok |
| 377 | ); |
| 378 | break; |
| 379 | case 'multi_checkbox': |
| 380 | $output = sprintf( |
| 381 | '<div id="%1$s[%2$s_%3$s]"><fieldset>', |
| 382 | esc_attr( $option_key ), |
| 383 | esc_attr( $section ), |
| 384 | esc_attr( $name ) |
| 385 | ); |
| 386 | // Fallback if nothing is selected |
| 387 | $output .= sprintf( |
| 388 | '<input type="hidden" name="%1$s[%2$s_%3$s][]" value="__placeholder__" />', |
| 389 | esc_attr( $option_key ), |
| 390 | esc_attr( $section ), |
| 391 | esc_attr( $name ) |
| 392 | ); |
| 393 | $current_value = (array) $current_value; |
| 394 | $choices = $field['choices']; |
| 395 | if ( is_callable( $choices ) ) { |
| 396 | $choices = call_user_func( $choices ); |
| 397 | } |
| 398 | foreach ( $choices as $value => $label ) { |
| 399 | $output .= sprintf( |
| 400 | '<label>%1$s <span>%2$s</span></label><br />', |
| 401 | sprintf( |
| 402 | '<input type="checkbox" name="%1$s[%2$s_%3$s][]" value="%4$s" %5$s />', |
| 403 | esc_attr( $option_key ), |
| 404 | esc_attr( $section ), |
| 405 | esc_attr( $name ), |
| 406 | esc_attr( $value ), |
| 407 | checked( in_array( $value, $current_value ), true, false ) |
| 408 | ), |
| 409 | esc_html( $label ) |
| 410 | ); |
| 411 | } |
| 412 | $output .= '</fieldset></div>'; |
| 413 | break; |
| 414 | case 'select': |
| 415 | $current_value = (array) self::$options[ $section . '_' . $name ]; |
| 416 | $default_value = isset( $default['value'] ) ? $default['value'] : '-1'; |
| 417 | $default_name = isset( $default['name'] ) ? $default['name'] : 'Choose Setting'; |
| 418 | |
| 419 | $output = sprintf( |
| 420 | '<select name="%1$s[%2$s_%3$s]" id="%1$s_%2$s_%3$s">', |
| 421 | esc_attr( $option_key ), |
| 422 | esc_attr( $section ), |
| 423 | esc_attr( $name ) |
| 424 | ); |
| 425 | $output .= sprintf( |
| 426 | '<option value="%1$s" %2$s>%3$s</option>', |
| 427 | esc_attr( $default_value ), |
| 428 | selected( in_array( $default_value, $current_value ), true, false ), |
| 429 | esc_html( $default_name ) |
| 430 | ); |
| 431 | foreach ( $field['choices'] as $value => $label ) { |
| 432 | $output .= sprintf( |
| 433 | '<option value="%1$s" %2$s>%3$s</option>', |
| 434 | esc_attr( $value ), |
| 435 | selected( in_array( $value, $current_value ), true, false ), |
| 436 | esc_html( $label ) |
| 437 | ); |
| 438 | } |
| 439 | $output .= '</select>'; |
| 440 | $output .= $after_field; |
| 441 | break; |
| 442 | case 'file': |
| 443 | $output = sprintf( |
| 444 | '<input type="file" name="%1$s[%2$s_%3$s]" id="%1$s_%2$s_%3$s" class="%4$s">', |
| 445 | esc_attr( $option_key ), |
| 446 | esc_attr( $section ), |
| 447 | esc_attr( $name ), |
| 448 | esc_attr( $class ) |
| 449 | ); |
| 450 | break; |
| 451 | case 'link': |
| 452 | $output = sprintf( |
| 453 | '<a id="%1$s_%2$s_%3$s" class="%4$s" href="%5$s">%6$s</a>', |
| 454 | esc_attr( $option_key ), |
| 455 | esc_attr( $section ), |
| 456 | esc_attr( $name ), |
| 457 | esc_attr( $class ), |
| 458 | esc_attr( $href ), |
| 459 | esc_attr( $title ) |
| 460 | ); |
| 461 | break; |
| 462 | case 'select2' : |
| 463 | if ( ! isset ( $current_value ) ) { |
| 464 | $current_value = array(); |
| 465 | } |
| 466 | |
| 467 | if ( false !== ( $key = array_search( '__placeholder__', $current_value ) ) ) { |
| 468 | unset( $current_value[ $key ] ); |
| 469 | } |
| 470 | |
| 471 | $data_values = array(); |
| 472 | $selected_values = array(); |
| 473 | if ( isset( $field['choices'] ) ) { |
| 474 | $choices = $field['choices']; |
| 475 | if ( is_callable( $choices ) ) { |
| 476 | $param = ( isset( $field['param'] ) ) ? $field['param'] : null; |
| 477 | $choices = call_user_func( $choices, $param ); |
| 478 | } |
| 479 | foreach ( $choices as $key => $value ) { |
| 480 | $data_values[] = array( 'id' => $key, 'text' => $value ); |
| 481 | if ( in_array( $key, $current_value ) ) { |
| 482 | $selected_values[] = array( 'id' => $key, 'text' => $value ); |
| 483 | } |
| 484 | } |
| 485 | $class .= ' with-source'; |
| 486 | } else { |
| 487 | foreach ( $current_value as $value ) { |
| 488 | if ( '__placeholder__' === $value || '' === $value ) { |
| 489 | continue; |
| 490 | } |
| 491 | $selected_values[] = array( 'id' => $value, 'text' => $value ); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | $output = sprintf( |
| 496 | '<div id="%1$s[%2$s_%3$s]">', |
| 497 | esc_attr( $option_key ), |
| 498 | esc_attr( $section ), |
| 499 | esc_attr( $name ) |
| 500 | ); |
| 501 | $output .= sprintf( |
| 502 | '<input type="hidden" data-values=\'%1$s\' data-selected=\'%2$s\' value="%3$s" class="select2-select %4$s" data-select-placeholder="%5$s-%6$s-select-placeholder" %7$s />', |
| 503 | esc_attr( json_encode( $data_values ) ), |
| 504 | esc_attr( json_encode( $selected_values ) ), |
| 505 | esc_attr( implode( ',', $current_value ) ), |
| 506 | $class, |
| 507 | esc_attr( $section ), |
| 508 | esc_attr( $name ), |
| 509 | isset( $nonce ) ? sprintf( ' data-nonce="%s"', esc_attr( wp_create_nonce( $nonce ) ) ) : '' |
| 510 | ); |
| 511 | // to store data with default value if nothing is selected |
| 512 | $output .= sprintf( |
| 513 | '<input type="hidden" name="%1$s[%2$s_%3$s][]" class="%2$s-%3$s-select-placeholder" value="__placeholder__" />', |
| 514 | esc_attr( $option_key ), |
| 515 | esc_attr( $section ), |
| 516 | esc_attr( $name ) |
| 517 | ); |
| 518 | $output .= '</div>'; |
| 519 | break; |
| 520 | case 'select2_user_role': |
| 521 | $current_value = (array)$current_value; |
| 522 | $data_values = array(); |
| 523 | |
| 524 | if ( isset( $field['choices'] ) ) { |
| 525 | $choices = $field['choices']; |
| 526 | if ( is_callable( $choices ) ) { |
| 527 | $param = ( isset( $field['param'] ) ) ? $field['param'] : null; |
| 528 | $choices = call_user_func( $choices, $param ); |
| 529 | } |
| 530 | } else { |
| 531 | $choices = array(); |
| 532 | } |
| 533 | |
| 534 | foreach ( $choices as $key => $role ) { |
| 535 | $args = array( 'id' => $key, 'text' => $role ); |
| 536 | $users = get_users( array( 'role' => $key ) ); |
| 537 | if ( count( $users ) ) { |
| 538 | $args['user_count'] = sprintf( _n( '1 user', '%s users', count( $users ), 'mainwp-child-reports' ), count( $users ) ); |
| 539 | } |
| 540 | $data_values[] = $args; |
| 541 | } |
| 542 | |
| 543 | $selected_values = array(); |
| 544 | foreach ( $current_value as $value ) { |
| 545 | if ( ! is_string( $value ) && ! is_numeric( $value ) ) { |
| 546 | continue; |
| 547 | } |
| 548 | |
| 549 | if ( '__placeholder__' === $value || '' === $value ) { |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | if ( is_numeric( $value ) ) { |
| 554 | $user = new WP_User( $value ); |
| 555 | $selected_values[] = array( 'id' => $user->ID, 'text' => $user->display_name ); |
| 556 | } else { |
| 557 | foreach ( $data_values as $role ) { |
| 558 | if ( $role['id'] !== $value ) { |
| 559 | continue; |
| 560 | } |
| 561 | $selected_values[] = $role; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | $output = sprintf( |
| 567 | '<div id="%1$s[%2$s_%3$s]">', |
| 568 | esc_attr( $option_key ), |
| 569 | esc_attr( $section ), |
| 570 | esc_attr( $name ) |
| 571 | ); |
| 572 | $output .= sprintf( |
| 573 | '<input type="hidden" data-values=\'%1$s\' data-selected=\'%2$s\' value="%3$s" class="select2-select %5$s" data-select-placeholder="%4$s-%5$s-select-placeholder" data-nonce="%6$s" />', |
| 574 | json_encode( $data_values ), |
| 575 | json_encode( $selected_values ), |
| 576 | esc_attr( implode( ',', $current_value ) ), |
| 577 | esc_attr( $section ), |
| 578 | esc_attr( $name ), |
| 579 | esc_attr( wp_create_nonce( 'stream_get_users' ) ) |
| 580 | ); |
| 581 | // to store data with default value if nothing is selected |
| 582 | $output .= sprintf( |
| 583 | '<input type="hidden" name="%1$s[%2$s_%3$s][]" class="%2$s-%3$s-select-placeholder" value="__placeholder__" />', |
| 584 | esc_attr( $option_key ), |
| 585 | esc_attr( $section ), |
| 586 | esc_attr( $name ) |
| 587 | ); |
| 588 | $output .= '</div>'; |
| 589 | break; |
| 590 | } |
| 591 | $output .= ! empty( $description ) ? sprintf( '<p class="description">%s</p>', $description /* xss ok */ ) : null; |
| 592 | |
| 593 | return $output; |
| 594 | } |
| 595 | |
| 596 | public static function output_field( $field ) { |
| 597 | $method = 'output_' . $field['name']; |
| 598 | |
| 599 | if ( method_exists( __CLASS__, $method ) ) { |
| 600 | return call_user_func( array( __CLASS__, $method ), $field ); |
| 601 | } |
| 602 | |
| 603 | $output = self::render_field( $field ); |
| 604 | |
| 605 | echo $output; // xss okay |
| 606 | } |
| 607 | |
| 608 | public static function get_roles() { |
| 609 | $wp_roles = new WP_Roles(); |
| 610 | $roles = array(); |
| 611 | |
| 612 | foreach ( $wp_roles->get_names() as $role => $label ) { |
| 613 | $roles[ $role ] = translate_user_role( $label ); |
| 614 | } |
| 615 | |
| 616 | return $roles; |
| 617 | } |
| 618 | |
| 619 | public static function get_connectors() { |
| 620 | return MainWP_WP_Stream_Connectors::$term_labels['stream_connector']; |
| 621 | } |
| 622 | |
| 623 | public static function get_default_connectors() { |
| 624 | return array_keys( MainWP_WP_Stream_Connectors::$term_labels['stream_connector'] ); |
| 625 | } |
| 626 | |
| 627 | public static function get_terms_labels( $column ) { |
| 628 | $return_labels = array(); |
| 629 | |
| 630 | if ( isset ( MainWP_WP_Stream_Connectors::$term_labels[ 'stream_' . $column ] ) ) { |
| 631 | $return_labels = MainWP_WP_Stream_Connectors::$term_labels[ 'stream_' . $column ]; |
| 632 | ksort( $return_labels ); |
| 633 | } |
| 634 | |
| 635 | return $return_labels; |
| 636 | } |
| 637 | |
| 638 | public static function get_active_connectors() { |
| 639 | $excluded_connectors = self::get_excluded_by_key( 'connectors' ); |
| 640 | $active_connectors = array_diff( array_keys( self::get_terms_labels( 'connector' ) ), $excluded_connectors ); |
| 641 | $active_connectors = wp_list_filter( $active_connectors, array( '__placeholder__' ), 'NOT' ); |
| 642 | |
| 643 | return $active_connectors; |
| 644 | } |
| 645 | |
| 646 | public static function get_excluded_by_key( $column ) { |
| 647 | $option_name = ( 'authors' === $column || 'roles' === $column ) ? 'exclude_authors_and_roles' : 'exclude_' . $column; |
| 648 | |
| 649 | $excluded_values = ( isset( self::$options[ $option_name ] ) ) ? self::$options[ $option_name ] : array(); |
| 650 | |
| 651 | if ( is_callable( $excluded_values ) ) { |
| 652 | $excluded_values = call_user_func( $excluded_values ); |
| 653 | } |
| 654 | |
| 655 | $excluded_values = wp_list_filter( $excluded_values, array( '__placeholder__' ), 'NOT' ); |
| 656 | |
| 657 | if ( 'exclude_authors_and_roles' === $option_name ) { |
| 658 | // Convert numeric strings to integers |
| 659 | array_walk( $excluded_values, |
| 660 | function ( &$value ) { |
| 661 | if ( is_numeric( $value ) ) { |
| 662 | $value = absint( $value ); |
| 663 | } |
| 664 | } |
| 665 | ); |
| 666 | |
| 667 | $filter = ( 'roles' === $column ) ? 'is_string' : 'is_int'; // Author roles are always strings and author ID's are always integers |
| 668 | |
| 669 | $excluded_values = array_values( array_filter( $excluded_values, $filter ) ); // Reset the array keys |
| 670 | } |
| 671 | |
| 672 | return $excluded_values; |
| 673 | } |
| 674 | |
| 675 | public static function get_settings_translations( $labels ) { |
| 676 | if ( ! isset( $labels[ self::KEY ] ) ) { |
| 677 | $labels[ self::KEY ] = array(); |
| 678 | } |
| 679 | |
| 680 | foreach ( self::get_fields() as $section_slug => $section ) { |
| 681 | foreach ( $section['fields'] as $field ) { |
| 682 | $labels[ self::KEY ][ sprintf( '%s_%s', $section_slug, $field['name'] ) ] = $field['title']; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return $labels; |
| 687 | } |
| 688 | |
| 689 | public static function updated_option_ttl_remove_records( $old_value, $new_value ) { |
| 690 | $ttl_before = isset( $old_value['general_records_ttl'] ) ? (int) $old_value['general_records_ttl'] : -1; |
| 691 | $ttl_after = isset( $new_value['general_records_ttl'] ) ? (int) $new_value['general_records_ttl'] : -1; |
| 692 | |
| 693 | if ( $ttl_after < $ttl_before ) { |
| 694 | /** |
| 695 | * Action assists in purging when TTL is shortened |
| 696 | */ |
| 697 | do_action( 'mainwp_wp_stream_auto_purge' ); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 |