Example #1
0
 /**
  * Log handler
  *
  * @param Connector $connector Connector responsible for logging the event
  * @param string $message      sprintf-ready error message string
  * @param array  $args         sprintf (and extra) arguments to use
  * @param int $object_id       Target object id
  * @param string $context      Context of the event
  * @param string $action       Action of the event
  * @param int $user_id         User responsible for the event
  *
  * @return mixed True if updated, otherwise false|WP_Error
  */
 public function log($connector, $message, $args, $object_id, $context, $action, $user_id = null)
 {
     if (is_null($user_id)) {
         $user_id = get_current_user_id();
     }
     if (is_null($object_id)) {
         $object_id = 0;
     }
     $wp_cron_tracking = isset($this->plugin->settings->options['advanced_wp_cron_tracking']) ? $this->plugin->settings->options['advanced_wp_cron_tracking'] : false;
     $author = new Author($user_id);
     $agent = $author->get_current_agent();
     // WP Cron tracking requires opt-in and WP Cron to be enabled
     if (!$wp_cron_tracking && 'wp_cron' === $agent) {
         return false;
     }
     $user = new \WP_User($user_id);
     if ($this->is_record_excluded($connector, $context, $action, $user)) {
         return false;
     }
     $user_meta = array('user_email' => (string) (!empty($user->user_email)) ? $user->user_email : '', 'display_name' => (string) $author->get_display_name(), 'user_login' => (string) (!empty($user->user_login)) ? $user->user_login : '', 'user_role_label' => (string) $author->get_role(), 'agent' => (string) $agent);
     if ('wp_cli' === $agent && function_exists('posix_getuid')) {
         $uid = posix_getuid();
         $user_info = posix_getpwuid($uid);
         $user_meta['system_user_id'] = (int) $uid;
         $user_meta['system_user_name'] = (string) $user_info['name'];
     }
     // Prevent any meta with null values from being logged
     $stream_meta = array_filter($args, function ($var) {
         return !is_null($var);
     });
     // Add user meta to Stream meta
     $stream_meta['user_meta'] = $user_meta;
     // All meta must be strings, so we will serialize any array meta values
     array_walk($stream_meta, function (&$v) {
         $v = (string) maybe_serialize($v);
     });
     // Get the current time in milliseconds
     $iso_8601_extended_date = wp_stream_get_iso_8601_extended_date();
     $recordarr = array('object_id' => (int) $object_id, 'site_id' => (int) is_multisite() ? get_current_site()->id : 1, 'blog_id' => (int) apply_filters('wp_stream_blog_id_logged', get_current_blog_id()), 'user_id' => (int) $user_id, 'user_role' => (string) (!empty($user->roles)) ? $user->roles[0] : '', 'created' => (string) $iso_8601_extended_date, 'summary' => (string) vsprintf($message, $args), 'connector' => (string) $connector, 'context' => (string) $context, 'action' => (string) $action, 'ip' => (string) wp_stream_filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP), 'meta' => (array) $stream_meta);
     if (0 === $recordarr['object_id']) {
         unset($recordarr['object_id']);
     }
     $result = $this->plugin->db->insert($recordarr);
     $this->debug_backtrace($recordarr);
     return $result;
 }
Example #2
0
 function column_default($item, $column_name)
 {
     $out = '';
     $record = new Record($item);
     switch ($column_name) {
         case 'date':
             $created = date('Y-m-d H:i:s', strtotime($record->created));
             $date_string = sprintf('<time datetime="%s" class="relative-time record-created">%s</time>', wp_stream_get_iso_8601_extended_date(strtotime($record->created)), get_date_from_gmt($created, 'Y/m/d'));
             $out = $this->column_link($date_string, 'date', get_date_from_gmt($created, 'Y/m/d'));
             $out .= '<br />';
             $out .= get_date_from_gmt($created, 'h:i:s A');
             break;
         case 'summary':
             $out = $record->summary;
             $object_title = $record->get_object_title();
             $view_all_text = $object_title ? sprintf(esc_html__('View all activity for "%s"', 'stream'), esc_attr($object_title)) : esc_html__('View all activity for this object', 'stream');
             if ($record->object_id) {
                 $out .= $this->column_link('<span class="dashicons dashicons-search stream-filter-object-id"></span>', array('object_id' => $record->object_id, 'context' => $record->context), null, esc_attr($view_all_text));
             }
             $out .= $this->get_action_links($record);
             break;
         case 'user_id':
             $user = new Author((int) $record->user_id, (array) maybe_unserialize($record->user_meta));
             $filtered_records_url = add_query_arg(array('page' => $this->plugin->admin->records_page_slug, 'user_id' => absint($user->id)), self_admin_url($this->plugin->admin->admin_parent_page));
             $out = sprintf('<a href="%s">%s <span>%s</span></a>%s%s%s', $filtered_records_url, $user->get_avatar_img(80), $user->get_display_name(), $user->is_deleted() ? sprintf('<br /><small class="deleted">%s</small>', esc_html__('Deleted User', 'stream')) : '', $user->get_role() ? sprintf('<br /><small>%s</small>', $user->get_role()) : '', $user->get_agent() ? sprintf('<br /><small>%s</small>', $user->get_agent_label($user->get_agent())) : '');
             break;
         case 'context':
             $connector_title = $this->get_term_title($record->{'connector'}, 'connector');
             $context_title = $this->get_term_title($record->{'context'}, 'context');
             $out = $this->column_link($connector_title, 'connector', $item->{'connector'});
             $out .= '<br />&#8627;&nbsp;';
             $out .= $this->column_link($context_title, array('connector' => $record->{'connector'}, 'context' => $record->{'context'}));
             break;
         case 'action':
             $out = $this->column_link($this->get_term_title($record->{$column_name}, $column_name), $column_name, $record->{$column_name});
             break;
         case 'blog_id':
             $blog = $record->blog_id && is_multisite() ? get_blog_details($record->blog_id) : $this->plugin->admin->network->get_network_blog();
             $out = $this->column_link($blog->blogname, 'blog_id', $blog->blog_id);
             break;
         case 'ip':
             $out = $this->column_link($record->{$column_name}, 'ip', $record->{$column_name});
             break;
         default:
             /**
              * Registers new Columns to be inserted into the table.  The cell contents of this column is set
              * below with 'wp_stream_inster_column_default-'
              *
              * @return array
              */
             $inserted_columns = apply_filters('wp_stream_register_column_defaults', $new_columns = array());
             if (!empty($inserted_columns) && is_array($inserted_columns)) {
                 foreach ($inserted_columns as $column_title) {
                     /**
                      * If column title inserted via wp_stream_register_column_defaults ($column_title) exists
                      * among columns registered with get_columns ($column_name) and there is an action associated
                      * with this column, do the action
                      *
                      * Also, note that the action name must include the $column_title registered
                      * with wp_stream_register_column_defaults
                      */
                     if ($column_title === $column_name && has_filter("wp_stream_insert_column_default-{$column_title}")) {
                         /**
                          * Allows for the addition of content under a specified column.
                          *
                          * @param object $record  Contents of the row
                          *
                          * @return string
                          */
                         $out = apply_filters("wp_stream_insert_column_default-{$column_title}", $column_name, $record);
                     } else {
                         $out = $column_name;
                     }
                 }
             } else {
                 $out = $column_name;
             }
     }
     echo $out;
     // xss ok
 }
Example #3
0
 public function test_get_role()
 {
     $this->assertEquals('Administrator', $this->author->get_role());
 }