From de6c60ea0956b8609d94d815872b8bf175402e0e Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 24 Apr 2014 02:21:40 -0500 Subject: [PATCH 01/10] Refactor author usage into WP_Stream_Author class; use System user always --- includes/admin.php | 34 ++---- includes/class-wp-stream-author.php | 166 ++++++++++++++++++++++++++++ includes/dashboard.php | 81 ++++---------- includes/list-table.php | 88 ++++----------- includes/settings.php | 36 +++--- 5 files changed, 232 insertions(+), 173 deletions(-) create mode 100644 includes/class-wp-stream-author.php diff --git a/includes/admin.php b/includes/admin.php index 7e2dabc0a..36ad62e07 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -787,39 +787,19 @@ public static function get_filter_value_by_id() { } public static function get_authors_record_meta( $authors ) { + require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php'; + $authors_records = array(); foreach ( $authors as $user_id => $author ) { - $icon = ''; $title = ''; - if ( 0 === $user_id ) { - $name = 'WP-CLI'; - $icon = WP_STREAM_URL . 'ui/stream-icons/wp-cli.png'; - $title = 'WP-CLI Operation'; - } else { - $user = is_a( $author, 'WP_User' ) ? $author : $author['label']; // @todo hacky. Stop using WP_User as label - $name = $user->display_name; - - if ( preg_match( '# src=[\'" ]([^\'" ]*)#', get_avatar( $user->user_email, 32 ), $gravatar_src_match ) ) { - list( $gravatar_src, $gravatar_url ) = $gravatar_src_match; - $icon = $gravatar_url; - } - - $title = sprintf( - __( "ID: %d\nUser: %s\nEmail: %s\nRole: %s", 'stream' ), - $user->ID, - $user->user_login, - $user->user_email, - implode( ', ', array_map( 'ucwords', $user->roles ) ) - ); - } - + $author_obj = new WP_Stream_Author( $user_id ); $authors_records[ $user_id ] = array( - 'text' => $name, + 'text' => $author_obj->get_display_name(), 'id' => $user_id, - 'label' => $name, - 'icon' => $icon, + 'label' => $author_obj->get_display_name(), + 'icon' => $author_obj->get_avatar_src( 32 ), 'title' => $title, - 'disabled' => ( is_array( $author ) && isset( $author['disabled'] ) ) ? $author['disabled'] : null, + 'disabled' => ( is_array( $author ) && isset( $author['disabled'] ) ) ? $author['disabled'] : null, // @todo hacky ); } diff --git a/includes/class-wp-stream-author.php b/includes/class-wp-stream-author.php new file mode 100644 index 000000000..e2a199e45 --- /dev/null +++ b/includes/class-wp-stream-author.php @@ -0,0 +1,166 @@ +id = $user_id; + $this->meta = $author_meta; + if ( $this->id ) { + $this->user_obj = new WP_User( $this->id ); + } + } + + /** + * @param string $name + * @throws Exception + * @return string|mixed + */ + function __get( $name ) { + if ( 'display_name' === $name ) { + return $this->get_display_name(); + } elseif ( 'avatar_img' === $name ) { + return $this->get_avatar_img(); + } elseif ( 'avatar_src' === $name ) { + return $this->get_avatar_src(); + } elseif ( 'role' === $name ) { + return $this->get_role(); + } elseif ( ! empty( $this->user_obj ) && 0 !== $this->user_obj->ID ) { + return $this->user_obj->$name; + } else { + throw new Exception( "Unrecognized magic '$name'" ); + } + } + + /** + * @return string + */ + function get_display_name() { + if ( 0 === $this->id ) { + return __( 'System', 'stream' ); + } else { + if ( $this->is_deleted() ) { + if ( ! empty( $this->meta['display_name'] ) ) { + return $this->meta['display_name']; + } elseif ( ! empty( $this->meta['user_login'] ) ) { + return $this->meta['user_login']; + } else { + return __( 'N/A', 'stream' ); + } + } + else if ( ! empty( $this->user_obj->display_name ) ) { + return $this->user_obj->display_name; + } else { + return $this->user_obj->user_login; + } + } + } + + /** + * @param int $size + * @return string + */ + function get_avatar_img( $size = 80 ) { + if ( 0 === $this->id ) { + $url = WP_STREAM_URL . 'ui/stream-icons/wp-cli.png'; + $avatar = sprintf( '%1$s', esc_attr( $this->get_display_name() ), esc_url( $url ), esc_attr( $size ) ); + } else { + if ( $this->is_deleted() ) { + $email = $this->meta['user_email']; + $avatar = get_avatar( $email, $size ); + } else { + $avatar = get_avatar( $this->id, $size ); + } + } + return $avatar; + } + + /** + * @param int $size + * @return string + */ + function get_avatar_src( $size = 80 ) { + $img = $this->get_avatar_img( $size ); + assert( preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) ); + $src = html_entity_decode( $matches[2] ); + return $src; + } + + /** + * Tries to find a label for the record's author_role. + * + * If the author_role exists, use the label associated with it + * Otherwise, if there is a user role label stored as Stream meta then use that + * Otherwise, if the user exists, use the label associated with their current role + * Otherwise, use the role slug as the label + * + * @return string|null + */ + function get_role() { + global $wp_roles; + if ( ! empty( $this->meta['author_role'] ) && isset( $wp_roles->role_names[ $this->meta['author_role'] ] ) ) { + $author_role = $wp_roles->role_names[ $this->meta['author_role'] ]; + } elseif ( ! empty( $this->meta['user_role_label'] ) ) { + $author_role = $this->meta['user_role_label']; + } elseif ( isset( $this->user_obj->roles[0] ) && isset( $wp_roles->role_names[ $this->user_obj->roles[0] ] ) ) { + $author_role = $wp_roles->role_names[ $this->user_obj->roles[0] ]; + } else { + $author_role = null; + } + return $author_role; + } + + /** + * @return string + */ + function get_records_page_url() { + $url = add_query_arg( + array( + 'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG, + 'author' => absint( $this->id ), + ), + self_admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE ) + ); + return $url; + } + + /** + * @return bool + */ + function is_deleted() { + return ( 0 !== $this->id && 0 === $this->user_obj->ID ); + } + + /** + * @return bool + */ + function is_wp_cli() { + return ! empty( $this->meta['is_wp_cli'] ); + } + + /** + * @return string + */ + function __toString() { + return $this->get_display_name(); + } + +} \ No newline at end of file diff --git a/includes/dashboard.php b/includes/dashboard.php index 31adb5f76..14880ea5c 100644 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -206,73 +206,36 @@ public static function stream_activity_options() { * @return string Contents of new row */ public static function widget_row( $item, $i = null ) { - $records_link = add_query_arg( - array( 'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG ), - admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE ) - ); + require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php'; + $author_meta = wp_stream_get_meta( $item->ID, 'author_meta', true ); - $is_wp_cli = ! empty( $author_meta['is_wp_cli'] ); - - if ( 0 === (int)$item->author ) { - $author = new WP_User( 0 ); - $author_name = $is_wp_cli ? 'WP-CLI' : __( 'N/A', 'stream' ); - } else { - $author = get_userdata( $item->author ); - $author_name = $author->display_name; - } - $author_link = add_query_arg( - array( 'author' => isset( $author->ID ) ? absint( $author->ID ) : 0 ), - $records_link + $author = new WP_Stream_Author( (int) $item->author, $author_meta ); + + $time_author = sprintf( + _x( + '%1$s ago by %3$s', + '1: Time, 2: User profile URL, 3: User display name', + 'stream' + ), + human_time_diff( strtotime( $item->created ) ), + esc_url( $author->get_records_page_url() ), + esc_html( $author->get_display_name() ) ); - if ( $author ) { - $time_author = sprintf( - _x( - '%1$s ago by %3$s', - '1: Time, 2: User profile URL, 3: User display name', - 'stream' - ), - human_time_diff( strtotime( $item->created ) ), - esc_url( $author_link ), - esc_html( $author_name ) - ); - } else { - $time_author = sprintf( - __( '%s ago', 'stream' ), - human_time_diff( strtotime( $item->created ) ) - ); - } - - $class = ''; - - if ( isset( $i ) ) { - if ( $i % 2 ) { - $class = 'alternate'; - } + if ( $author->is_wp_cli() ) { + $time_author .= __( ' (via WP-CLI)', 'stream' ); } - if ( 0 === $author->ID ) { - if ( $is_wp_cli ) { - $avatar_url = WP_STREAM_URL . 'ui/stream-icons/wp-cli.png'; - $author_avatar = sprintf( '%s', esc_attr( $author_name ), esc_url( $avatar_url ) ); - } else { - $author_avatar = get_avatar( 'system@wp-stream.com', 72, get_option( 'avatar_default' ) ?: 'mystery', $author_name ); - $author_avatar = preg_replace( "/src='(.+?)'/", "src='\$1&forcedefault=1'", $author_avatar ); - } - } else { - $author_avatar = get_avatar( $author->ID, 72 ); - } + $class = ( isset( $i ) && $i % 2 ) ? 'alternate' : ''; ob_start() ?>
  • - -
    - - - -
    - - + +
    summary ) ?>
  • author ); - $author_meta = wp_stream_get_meta( $item->ID, 'author_meta', true ); - $is_wp_cli = ! empty( $author_meta['is_wp_cli'] ); - $author_role = null; - - global $wp_roles; - $author_ID = isset( $item->author ) ? $item->author : 0; - $user_deleted = false; - - /** - * Tries to find a label for the record's author_role. - * - * If the author_role exists, use the label associated with it - * Otherwise, if there is a user role label stored as Stream meta then use that - * Otherwise, if the user exists, use the label associated with their current role - * Otherwise, use the role slug as the label - */ - if ( ! empty( $item->author_role ) && isset( $wp_roles->role_names[ $item->author_role ] ) ) { - $author_role = $wp_roles->role_names[ $item->author_role ]; - } elseif ( ! empty( $author_meta['user_role_label'] ) ) { - $author_role = $author_meta['user_role_label']; - } elseif ( isset( $user->roles[0] ) && isset( $wp_roles->role_names[ $user->roles[0] ] ) ) { - $author_role = $wp_roles->role_names[ $user->roles[0] ]; - } else { - $author_role = $item->author_role; - } + require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php'; - if ( $user ) { - $author_name = isset( $user->display_name ) ? $user->display_name : $user->user_login; - $author_avatar = get_avatar( $author_ID, 80 ); - } elseif ( $is_wp_cli ) { - $author_name = 'WP-CLI'; - $avatar_url = WP_STREAM_URL . 'ui/stream-icons/wp-cli.png'; - $author_avatar = sprintf( '%s', esc_attr( $author_name ), esc_url( $avatar_url ) ); - } elseif ( ! $is_wp_cli ) { - $author_name = __( 'N/A', 'stream' ); - $author_avatar = get_avatar( 'system@wp-stream.com', 80, get_option( 'avatar_default' ) ?: 'mystery', $author_name ); - $author_avatar = preg_replace( "/src='(.+?)'/", "src='\$1&forcedefault=1'", $author_avatar ); - } else { - $user_deleted = true; - $author_name = ! empty( $author_meta['display_name'] ) ? $author_meta['display_name'] : $author_meta['user_login']; - $author_avatar = get_avatar( $author_meta['user_email'], 80 ); - } + $author_meta = wp_stream_get_meta( $item->ID, 'author_meta', true ); + $author = new WP_Stream_Author( (int) $item->author, $author_meta ); $out = sprintf( '%s %s%s%s%s', - add_query_arg( - array( - 'page' => WP_Stream_Admin::RECORDS_PAGE_SLUG, - 'author' => absint( $author_ID ), - ), - self_admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE ) - ), - $author_avatar, - $author_name, - $user_deleted ? sprintf( '
    %s', esc_html__( 'Deleted User', 'stream' ) ) : '', - $author_role ? sprintf( '
    %s', $author_role ) : '', - $user && $is_wp_cli ? sprintf( '
    %s', __( 'via WP-CLI', 'stream' ) ) : '' + $author->get_records_page_url(), + $author->get_avatar_img( 80 ), + $author->get_display_name(), + $author->is_deleted() ? sprintf( '
    %s', esc_html__( 'Deleted User', 'stream' ) ) : '', + $author->get_role() ? sprintf( '
    %s', $author->get_role() ) : '', + $author->is_wp_cli() ? sprintf( '
    %s', __( 'via WP-CLI', 'stream' ) ) : '' ); break; @@ -444,6 +399,7 @@ function assemble_records( $column, $table = '' ) { // @todo eliminate special condition for authors, especially using a WP_User object as the value; should use string or stringifiable object if ( 'author' === $column ) { + require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php'; $all_records = array(); // Short circuit and return empty array if we have more than 10 users, to use Ajax instead @@ -453,19 +409,23 @@ function assemble_records( $column, $table = '' ) { return array(); } - $wp_cli_user = new WP_User( 0 ); - $authors = get_users(); - $authors[] = $wp_cli_user; + $authors = array_map( + function ( $user_id ) { + return new WP_Stream_Author( $user_id ); + }, + get_users( array( 'fields' => 'ID' ) ) + ); + $authors[] = new WP_Stream_Author( 0, array( 'is_wp_cli' => true ) ); if ( $hide_disabled_column_filter ) { $excluded_records = WP_Stream_Settings::get_excluded_by_key( $setting_key ); } foreach ( $authors as $author ) { - if ( $hide_disabled_column_filter && in_array( $author->ID, $excluded_records ) ) { + if ( $hide_disabled_column_filter && in_array( $author->id, $excluded_records ) ) { continue; } - $all_records[ $author->ID ] = $author; + $all_records[ $author->id ] = $author; } } else { $prefixed_column = sprintf( 'stream_%s', $column ); @@ -499,14 +459,8 @@ function assemble_records( $column, $table = '' ) { } $sort = function ( $a, $b ) use ( $column ) { - // @todo we should ideally not have this author condition; the label should be an object with toString() - if ( 'author' === $column ) { - $label_a = ( 0 === $a['label']->ID ) ? 'WP-CLI' : $a['label']->display_name; - $label_b = ( 0 === $b['label']->ID ) ? 'WP-CLI' : $b['label']->display_name; - } else { - $label_a = $a['label']; - $label_b = $b['label']; - } + $label_a = (string) $a['label']; + $label_b = (string) $b['label']; if ( $label_a === $label_b ) { return 0; } diff --git a/includes/settings.php b/includes/settings.php index 58d88b04d..3a68f2c94 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -120,42 +120,38 @@ public static function get_users(){ $response->message = ''; $response->users = array(); - foreach ( $users->results as $key => $user ) { - $gravatar_url = null; + require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php'; - if ( preg_match( '# src=[\'" ]([^\'" ]*)#', get_avatar( $user->ID, 16 ), $gravatar_src_match ) ) { - $gravatar_url = $gravatar_src_match[1]; - } + foreach ( $users->results as $key => $user ) { + $author = new WP_Stream_Author( $user->ID ); $args = array( - 'id' => $user->ID, - 'text' => $user->display_name, + 'id' => $author->ID, + 'text' => $author->display_name, ); - $user_roles = array_map( 'ucwords', $user->roles ); $args['tooltip'] = esc_attr( sprintf( __( "ID: %d\nUser: %s\nEmail: %s\nRole: %s", 'stream' ), - $user->ID, - $user->user_login, - $user->user_email, - implode( ', ', $user_roles ) + $author->id, + $author->user_login, + $author->user_email, + ucwords( $author->get_role() ) ) ); - if ( null !== $gravatar_url ) { - $args['icon'] = $gravatar_url; - } + $args['icon'] = $author->get_avatar_src( 32 ); $response->users[] = $args; } - if ( empty( $search ) || preg_match( '/wp|cli/i', $search ) ) { + if ( empty( $search ) || preg_match( '/wp|cli|system/i', $search ) ) { + $author = new WP_Stream_Author( 0 ); $response->users[] = array( - 'id' => 0, - 'text' => 'WP-CLI', - 'icon' => WP_STREAM_URL . 'ui/stream-icons/wp-cli.png', - 'tooltip' => '', + 'id' => $author->id, + 'text' => $author->get_display_name(), + 'icon' => $author->get_avatar_src( 32 ), + 'tooltip' => __( 'Actions performed by the system when a user is not logged in (e.g. auto site upgrader, or invoking WP-CLI without --user)', 'stream' ), ); } From b1ec0070247d0eecbad4e64376e71908aefbfada Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 24 Apr 2014 02:30:43 -0500 Subject: [PATCH 02/10] Fix assignment alignment issue for PHPCS --- includes/class-wp-stream-author.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wp-stream-author.php b/includes/class-wp-stream-author.php index e2a199e45..cbc983f86 100644 --- a/includes/class-wp-stream-author.php +++ b/includes/class-wp-stream-author.php @@ -22,7 +22,7 @@ class WP_Stream_Author { * @param array $author_meta */ function __construct( $user_id, $author_meta = array() ) { - $this->id = $user_id; + $this->id = $user_id; $this->meta = $author_meta; if ( $this->id ) { $this->user_obj = new WP_User( $this->id ); From 70920fdedcf5f5e8188282070201f253becb7821 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 24 Apr 2014 02:41:50 -0500 Subject: [PATCH 03/10] Use Unknown user instead of System user --- includes/class-wp-stream-author.php | 2 +- includes/settings.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wp-stream-author.php b/includes/class-wp-stream-author.php index cbc983f86..16bd9da9d 100644 --- a/includes/class-wp-stream-author.php +++ b/includes/class-wp-stream-author.php @@ -55,7 +55,7 @@ function __get( $name ) { */ function get_display_name() { if ( 0 === $this->id ) { - return __( 'System', 'stream' ); + return __( 'Unknown', 'stream' ); } else { if ( $this->is_deleted() ) { if ( ! empty( $this->meta['display_name'] ) ) { diff --git a/includes/settings.php b/includes/settings.php index 3a68f2c94..31428a3bd 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -145,7 +145,7 @@ public static function get_users(){ $response->users[] = $args; } - if ( empty( $search ) || preg_match( '/wp|cli|system/i', $search ) ) { + if ( empty( $search ) || preg_match( '/wp|cli|system|unknown/i', $search ) ) { $author = new WP_Stream_Author( 0 ); $response->users[] = array( 'id' => $author->id, From bb85d70572be97b1ef69177116333662c4ee31a8 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 24 Apr 2014 02:48:25 -0500 Subject: [PATCH 04/10] Fix coding style to use elseif --- includes/class-wp-stream-author.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/class-wp-stream-author.php b/includes/class-wp-stream-author.php index 16bd9da9d..e363b7f34 100644 --- a/includes/class-wp-stream-author.php +++ b/includes/class-wp-stream-author.php @@ -65,8 +65,7 @@ function get_display_name() { } else { return __( 'N/A', 'stream' ); } - } - else if ( ! empty( $this->user_obj->display_name ) ) { + } elseif ( ! empty( $this->user_obj->display_name ) ) { return $this->user_obj->display_name; } else { return $this->user_obj->user_login; From ad852ebfa5ba3b41939a7955a53b59393bb01759 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 24 Apr 2014 10:49:55 -0500 Subject: [PATCH 05/10] Use N/A instead of Unknown --- includes/class-wp-stream-author.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wp-stream-author.php b/includes/class-wp-stream-author.php index e363b7f34..27b1be45e 100644 --- a/includes/class-wp-stream-author.php +++ b/includes/class-wp-stream-author.php @@ -55,7 +55,7 @@ function __get( $name ) { */ function get_display_name() { if ( 0 === $this->id ) { - return __( 'Unknown', 'stream' ); + return __( 'N/A', 'stream' ); } else { if ( $this->is_deleted() ) { if ( ! empty( $this->meta['display_name'] ) ) { From 6799e805aea2167419c3ff1142287989d3ffe439 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 24 Apr 2014 11:53:39 -0500 Subject: [PATCH 06/10] Remove unused variable --- includes/admin.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/admin.php b/includes/admin.php index 36ad62e07..1a582db4f 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -791,14 +791,13 @@ public static function get_authors_record_meta( $authors ) { $authors_records = array(); foreach ( $authors as $user_id => $author ) { - $title = ''; $author_obj = new WP_Stream_Author( $user_id ); $authors_records[ $user_id ] = array( 'text' => $author_obj->get_display_name(), 'id' => $user_id, 'label' => $author_obj->get_display_name(), 'icon' => $author_obj->get_avatar_src( 32 ), - 'title' => $title, + 'title' => '', 'disabled' => ( is_array( $author ) && isset( $author['disabled'] ) ) ? $author['disabled'] : null, // @todo hacky ); } From df48f2fc4158983bfe72a99e2f6c7fc799b3aad4 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 24 Apr 2014 14:49:38 -0500 Subject: [PATCH 07/10] Introduce author agent concept and logging WP Cron See #431 --- includes/class-wp-stream-author.php | 48 ++++++++++++++++++++++++++++- includes/dashboard.php | 4 +-- includes/list-table.php | 2 +- includes/log.php | 18 ++++++----- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/includes/class-wp-stream-author.php b/includes/class-wp-stream-author.php index 27b1be45e..62aede0ae 100644 --- a/includes/class-wp-stream-author.php +++ b/includes/class-wp-stream-author.php @@ -43,6 +43,8 @@ function __get( $name ) { return $this->get_avatar_src(); } elseif ( 'role' === $name ) { return $this->get_role(); + } elseif ( 'agent' === $name ) { + return $this->get_agent(); } elseif ( ! empty( $this->user_obj ) && 0 !== $this->user_obj->ID ) { return $this->user_obj->$name; } else { @@ -73,6 +75,19 @@ function get_display_name() { } } + /** + * @return string|null + */ + function get_agent() { + $agent = null; + if ( ! empty( $this->meta['agent'] ) ) { + $agent = $this->meta['agent']; + } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) { + $agent = 'wp_cli'; // legacy + } + return $agent; + } + /** * @param int $size * @return string @@ -152,7 +167,7 @@ function is_deleted() { * @return bool */ function is_wp_cli() { - return ! empty( $this->meta['is_wp_cli'] ); + return ( 'wp_cli' === $this->get_agent() ); } /** @@ -162,4 +177,35 @@ function __toString() { return $this->get_display_name(); } + /** + * Look at the environment to detect if an agent is being used + * + * @return null|string + */ + static function get_current_agent() { + $agent = null; + if ( defined( 'WP_CLI' ) ) { + $agent = 'wp_cli'; + } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { + $agent = 'wp_cron'; + } + $agent = apply_filters( 'wp_stream_current_agent', $agent ); + return $agent; + } + + /** + * @param string $agent + * @return string + */ + static function get_agent_label( $agent ) { + if ( 'wp_cli' === $agent ) { + $label = __( 'via WP-CLI', 'stream' ); + } elseif ( 'wp_cron' === $agent ) { + $label = __( 'during WP Cron', 'stream' ); + } else { + $label = null; + } + $label = apply_filters( 'wp_stream_agent_label', $label, $agent ); + return $label; + } } \ No newline at end of file diff --git a/includes/dashboard.php b/includes/dashboard.php index 14880ea5c..f59fd0971 100644 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -222,8 +222,8 @@ public static function widget_row( $item, $i = null ) { esc_html( $author->get_display_name() ) ); - if ( $author->is_wp_cli() ) { - $time_author .= __( ' (via WP-CLI)', 'stream' ); + if ( $author->get_agent() ) { + $time_author .= sprintf( ' %s', WP_Stream_Author::get_agent_label( $author->get_agent() ) ); } $class = ( isset( $i ) && $i % 2 ) ? 'alternate' : ''; diff --git a/includes/list-table.php b/includes/list-table.php index 65ad6968b..b4f7a9328 100644 --- a/includes/list-table.php +++ b/includes/list-table.php @@ -217,7 +217,7 @@ function column_default( $item, $column_name ) { $author->get_display_name(), $author->is_deleted() ? sprintf( '
    %s', esc_html__( 'Deleted User', 'stream' ) ) : '', $author->get_role() ? sprintf( '
    %s', $author->get_role() ) : '', - $author->is_wp_cli() ? sprintf( '
    %s', __( 'via WP-CLI', 'stream' ) ) : '' + $author->get_agent() ? sprintf( '
    %s', WP_Stream_Author::get_agent_label( $author->get_agent() ) ) : '' ); break; diff --git a/includes/log.php b/includes/log.php index c4db37830..fb2bc2626 100644 --- a/includes/log.php +++ b/includes/log.php @@ -66,21 +66,23 @@ public function log( $connector, $message, $args, $object_id, $contexts, $user_i if ( is_null( $user_id ) ) { $user_id = get_current_user_id(); } + require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php'; $user = new WP_User( $user_id ); $roles = get_option( $wpdb->get_blog_prefix() . 'user_roles' ); if ( ! isset( $args['author_meta'] ) ) { - $args['author_meta'] = maybe_serialize( - array( - 'user_email' => $user->user_email, - 'display_name' => ( defined( 'WP_CLI' ) && empty( $user->display_name ) ) ? 'WP-CLI' : $user->display_name, - 'user_login' => $user->user_login, - 'user_role_label' => ! empty( $user->roles ) ? $roles[ $user->roles[0] ]['name'] : null, - 'is_wp_cli' => defined( 'WP_CLI' ), - ) + $args['author_meta'] = array( + 'user_email' => $user->user_email, + 'display_name' => ( defined( 'WP_CLI' ) && empty( $user->display_name ) ) ? 'WP-CLI' : $user->display_name, // @todo + 'user_login' => $user->user_login, + 'user_role_label' => ! empty( $user->roles ) ? $roles[ $user->roles[0] ]['name'] : null, + 'agent' => WP_Stream_Author::get_current_agent(), ); } + if ( isset( $args['author_meta'] ) ) { + $args['author_meta'] = maybe_serialize( $args['author_meta'] ); + } // Remove meta with null values from being logged $meta = array_filter( From d0647bc5e4a7c25d6cb3d380ae7f58d66951b632 Mon Sep 17 00:00:00 2001 From: Luke Carbis Date: Mon, 28 Apr 2014 18:29:46 +0000 Subject: [PATCH 08/10] Change some variable names --- includes/admin.php | 21 ++++++++++++--------- includes/class-wp-stream-author.php | 22 +++++++++++----------- includes/list-table.php | 2 +- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/includes/admin.php b/includes/admin.php index 1a582db4f..0d7358a94 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -754,8 +754,9 @@ function ( $user ) { break; } - - echo json_encode( array_values( $results ) ); + if ( isset( $results ) ) { + echo json_encode( array_values( $results ) ); + } die(); } @@ -790,19 +791,21 @@ public static function get_authors_record_meta( $authors ) { require_once WP_STREAM_INC_DIR . 'class-wp-stream-author.php'; $authors_records = array(); - foreach ( $authors as $user_id => $author ) { - $author_obj = new WP_Stream_Author( $user_id ); + + foreach ( $authors as $user_id => $args ) { + $author = new WP_Stream_Author( $user_id ); + $disabled = isset( $args['disabled'] ) ? $args['disabled'] : null; + $authors_records[ $user_id ] = array( - 'text' => $author_obj->get_display_name(), + 'text' => $author->get_display_name(), 'id' => $user_id, - 'label' => $author_obj->get_display_name(), - 'icon' => $author_obj->get_avatar_src( 32 ), + 'label' => $author->get_display_name(), + 'icon' => $author->get_avatar_src( 32 ), 'title' => '', - 'disabled' => ( is_array( $author ) && isset( $author['disabled'] ) ) ? $author['disabled'] : null, // @todo hacky + 'disabled' => $disabled, ); } return $authors_records; } - } diff --git a/includes/class-wp-stream-author.php b/includes/class-wp-stream-author.php index 62aede0ae..b82545258 100644 --- a/includes/class-wp-stream-author.php +++ b/includes/class-wp-stream-author.php @@ -15,7 +15,7 @@ class WP_Stream_Author { /** * @var WP_User */ - protected $user_obj; + protected $user; /** * @param int $user_id @@ -25,7 +25,7 @@ function __construct( $user_id, $author_meta = array() ) { $this->id = $user_id; $this->meta = $author_meta; if ( $this->id ) { - $this->user_obj = new WP_User( $this->id ); + $this->user = new WP_User( $this->id ); } } @@ -45,8 +45,8 @@ function __get( $name ) { return $this->get_role(); } elseif ( 'agent' === $name ) { return $this->get_agent(); - } elseif ( ! empty( $this->user_obj ) && 0 !== $this->user_obj->ID ) { - return $this->user_obj->$name; + } elseif ( ! empty( $this->user ) && 0 !== $this->user->ID ) { + return $this->user->$name; } else { throw new Exception( "Unrecognized magic '$name'" ); } @@ -67,10 +67,10 @@ function get_display_name() { } else { return __( 'N/A', 'stream' ); } - } elseif ( ! empty( $this->user_obj->display_name ) ) { - return $this->user_obj->display_name; + } elseif ( ! empty( $this->user->display_name ) ) { + return $this->user->display_name; } else { - return $this->user_obj->user_login; + return $this->user->user_login; } } } @@ -134,8 +134,8 @@ function get_role() { $author_role = $wp_roles->role_names[ $this->meta['author_role'] ]; } elseif ( ! empty( $this->meta['user_role_label'] ) ) { $author_role = $this->meta['user_role_label']; - } elseif ( isset( $this->user_obj->roles[0] ) && isset( $wp_roles->role_names[ $this->user_obj->roles[0] ] ) ) { - $author_role = $wp_roles->role_names[ $this->user_obj->roles[0] ]; + } elseif ( isset( $this->user->roles[0] ) && isset( $wp_roles->role_names[ $this->user->roles[0] ] ) ) { + $author_role = $wp_roles->role_names[ $this->user->roles[0] ]; } else { $author_role = null; } @@ -160,7 +160,7 @@ function get_records_page_url() { * @return bool */ function is_deleted() { - return ( 0 !== $this->id && 0 === $this->user_obj->ID ); + return ( 0 !== $this->id && 0 === $this->user->ID ); } /** @@ -208,4 +208,4 @@ static function get_agent_label( $agent ) { $label = apply_filters( 'wp_stream_agent_label', $label, $agent ); return $label; } -} \ No newline at end of file +} diff --git a/includes/list-table.php b/includes/list-table.php index b4f7a9328..e9e1d6a0d 100644 --- a/includes/list-table.php +++ b/includes/list-table.php @@ -425,7 +425,7 @@ function ( $user_id ) { if ( $hide_disabled_column_filter && in_array( $author->id, $excluded_records ) ) { continue; } - $all_records[ $author->id ] = $author; + $all_records[ $author->id ] = $author->get_display_name(); } } else { $prefixed_column = sprintf( 'stream_%s', $column ); From 40508d8492e4ea003120fb5620d0be8126d7cdf5 Mon Sep 17 00:00:00 2001 From: Frankie Jarrett Date: Thu, 1 May 2014 20:21:33 -0500 Subject: [PATCH 09/10] Code formatting --- includes/class-wp-stream-author.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/includes/class-wp-stream-author.php b/includes/class-wp-stream-author.php index b82545258..dc20cb085 100644 --- a/includes/class-wp-stream-author.php +++ b/includes/class-wp-stream-author.php @@ -57,7 +57,7 @@ function __get( $name ) { */ function get_display_name() { if ( 0 === $this->id ) { - return __( 'N/A', 'stream' ); + return esc_html__( 'N/A', 'stream' ); } else { if ( $this->is_deleted() ) { if ( ! empty( $this->meta['display_name'] ) ) { @@ -65,7 +65,7 @@ function get_display_name() { } elseif ( ! empty( $this->meta['user_login'] ) ) { return $this->meta['user_login']; } else { - return __( 'N/A', 'stream' ); + return esc_html__( 'N/A', 'stream' ); } } elseif ( ! empty( $this->user->display_name ) ) { return $this->user->display_name; @@ -80,11 +80,13 @@ function get_display_name() { */ function get_agent() { $agent = null; + if ( ! empty( $this->meta['agent'] ) ) { $agent = $this->meta['agent']; } elseif ( ! empty( $this->meta['is_wp_cli'] ) ) { $agent = 'wp_cli'; // legacy } + return $agent; } @@ -104,6 +106,7 @@ function get_avatar_img( $size = 80 ) { $avatar = get_avatar( $this->id, $size ); } } + return $avatar; } @@ -115,6 +118,7 @@ function get_avatar_src( $size = 80 ) { $img = $this->get_avatar_img( $size ); assert( preg_match( '/src=([\'"])(.*?)\1/', $img, $matches ) ); $src = html_entity_decode( $matches[2] ); + return $src; } @@ -130,6 +134,7 @@ function get_avatar_src( $size = 80 ) { */ function get_role() { global $wp_roles; + if ( ! empty( $this->meta['author_role'] ) && isset( $wp_roles->role_names[ $this->meta['author_role'] ] ) ) { $author_role = $wp_roles->role_names[ $this->meta['author_role'] ]; } elseif ( ! empty( $this->meta['user_role_label'] ) ) { @@ -139,6 +144,7 @@ function get_role() { } else { $author_role = null; } + return $author_role; } @@ -153,6 +159,7 @@ function get_records_page_url() { ), self_admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE ) ); + return $url; } @@ -184,12 +191,15 @@ function __toString() { */ static function get_current_agent() { $agent = null; + if ( defined( 'WP_CLI' ) ) { $agent = 'wp_cli'; } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { $agent = 'wp_cron'; } + $agent = apply_filters( 'wp_stream_current_agent', $agent ); + return $agent; } @@ -199,13 +209,16 @@ static function get_current_agent() { */ static function get_agent_label( $agent ) { if ( 'wp_cli' === $agent ) { - $label = __( 'via WP-CLI', 'stream' ); + $label = esc_html__( 'via WP-CLI', 'stream' ); } elseif ( 'wp_cron' === $agent ) { - $label = __( 'during WP Cron', 'stream' ); + $label = esc_html__( 'during WP Cron', 'stream' ); } else { $label = null; } + $label = apply_filters( 'wp_stream_agent_label', $label, $agent ); + return $label; } + } From e13aae955d6c35779d36110ec415d49a78f54e30 Mon Sep 17 00:00:00 2001 From: Frankie Jarrett Date: Mon, 5 May 2014 20:51:47 -0500 Subject: [PATCH 10/10] Use esc_html__ in settings strings, add missing textdomain --- includes/settings.php | 52 +++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/includes/settings.php b/includes/settings.php index 4ebded58d..d0b753951 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -87,7 +87,7 @@ public static function get_users(){ $response = (object) array( 'status' => false, - 'message' => __( 'There was an error in the request', 'stream' ), + 'message' => esc_html__( 'There was an error in the request', 'stream' ), ); $search = ( isset( $_POST['find'] )? wp_unslash( trim( $_POST['find'] ) ) : '' ); @@ -151,7 +151,7 @@ public static function get_users(){ 'id' => $author->id, 'text' => $author->get_display_name(), 'icon' => $author->get_avatar_src( 32 ), - 'tooltip' => __( 'Actions performed by the system when a user is not logged in (e.g. auto site upgrader, or invoking WP-CLI without --user)', 'stream' ), + '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)', 'stream' ), ); } @@ -239,19 +239,19 @@ public static function get_fields() { if ( empty( self::$fields ) ) { $fields = array( 'general' => array( - 'title' => __( 'General', 'stream' ), + 'title' => esc_html__( 'General', 'stream' ), 'fields' => array( array( 'name' => 'role_access', - 'title' => __( 'Role Access', 'stream' ), + 'title' => esc_html__( 'Role Access', 'stream' ), 'type' => 'multi_checkbox', - 'desc' => __( 'Users from the selected roles above will have permission to view Stream Records. However, only site Administrators can access Stream Settings.', 'stream' ), + 'desc' => esc_html__( 'Users from the selected roles above will have permission to view Stream Records. However, only site Administrators can access Stream Settings.', 'stream' ), 'choices' => self::get_roles(), 'default' => array( 'administrator' ), ), array( 'name' => 'private_feeds', - 'title' => __( 'Private Feeds', 'stream' ), + 'title' => esc_html__( 'Private Feeds', 'stream' ), 'type' => 'checkbox', 'desc' => sprintf( __( 'Users from the selected roles above will be given a private key found in their %suser profile%s to access feeds of Stream Records securely. Please %sflush rewrite rules%s on your site after changing this setting.', 'stream' ), @@ -268,21 +268,21 @@ public static function get_fields() { ), '' ), - 'after_field' => __( 'Enabled' ), + 'after_field' => esc_html__( 'Enabled', 'stream' ), 'default' => 0, ), array( 'name' => 'records_ttl', - 'title' => __( 'Keep Records for', 'stream' ), + 'title' => esc_html__( 'Keep Records for', 'stream' ), 'type' => 'number', 'class' => 'small-text', - 'desc' => __( 'Maximum number of days to keep activity records. Leave blank to keep records forever.', 'stream' ), + 'desc' => esc_html__( 'Maximum number of days to keep activity records. Leave blank to keep records forever.', 'stream' ), 'default' => 90, - 'after_field' => __( 'days', 'stream' ), + 'after_field' => esc_html__( 'days', 'stream' ), ), array( 'name' => 'delete_all_records', - 'title' => __( 'Reset Stream Database', 'stream' ), + 'title' => esc_html__( 'Reset Stream Database', 'stream' ), 'type' => 'link', 'href' => add_query_arg( array( @@ -291,66 +291,66 @@ public static function get_fields() { ), admin_url( 'admin-ajax.php' ) ), - 'desc' => __( 'Warning: Clicking this will delete all activity records from the database.', 'stream' ), + 'desc' => esc_html__( 'Warning: Clicking this will delete all activity records from the database.', 'stream' ), 'default' => 0, ), ), ), 'exclude' => array( - 'title' => __( 'Exclude', 'stream' ), + 'title' => esc_html__( 'Exclude', 'stream' ), 'fields' => array( array( 'name' => 'authors_and_roles', - 'title' => __( 'Authors & Roles', 'stream' ), + 'title' => esc_html__( 'Authors & Roles', 'stream' ), 'type' => 'select2_user_role', - 'desc' => __( 'No activity will be logged for these authors and/or roles.', 'stream' ), + 'desc' => esc_html__( 'No activity will be logged for these authors and/or roles.', 'stream' ), 'choices' => self::get_roles(), 'default' => array(), ), array( 'name' => 'connectors', - 'title' => __( 'Connectors', 'stream' ), + 'title' => esc_html__( 'Connectors', 'stream' ), 'type' => 'select2', - 'desc' => __( 'No activity will be logged for these connectors.', 'stream' ), + 'desc' => esc_html__( 'No activity will be logged for these connectors.', 'stream' ), 'choices' => array( __CLASS__, 'get_terms_labels' ), 'param' => 'connector', 'default' => array(), ), array( 'name' => 'contexts', - 'title' => __( 'Contexts', 'stream' ), + 'title' => esc_html__( 'Contexts', 'stream' ), 'type' => 'select2', - 'desc' => __( 'No activity will be logged for these contexts.', 'stream' ), + 'desc' => esc_html__( 'No activity will be logged for these contexts.', 'stream' ), 'choices' => array( __CLASS__, 'get_terms_labels' ), 'param' => 'context', 'default' => array(), ), array( 'name' => 'actions', - 'title' => __( 'Actions', 'stream' ), + 'title' => esc_html__( 'Actions', 'stream' ), 'type' => 'select2', - 'desc' => __( 'No activity will be logged for these actions.', 'stream' ), + 'desc' => esc_html__( 'No activity will be logged for these actions.', 'stream' ), 'choices' => array( __CLASS__, 'get_terms_labels' ), 'param' => 'action', 'default' => array(), ), array( 'name' => 'ip_addresses', - 'title' => __( 'IP Addresses', 'stream' ), + 'title' => esc_html__( 'IP Addresses', 'stream' ), 'type' => 'select2', - 'desc' => __( 'No activity will be logged for these IP addresses.', 'stream' ), + 'desc' => esc_html__( 'No activity will be logged for these IP addresses.', 'stream' ), 'class' => 'ip-addresses', 'default' => array(), 'nonce' => 'stream_get_ips', ), array( 'name' => 'hide_previous_records', - 'title' => __( 'Visibility', 'stream' ), + 'title' => esc_html__( 'Visibility', 'stream' ), 'type' => 'checkbox', 'desc' => sprintf( - __( 'When checked, all past records that match the excluded rules above will be hidden from view.', 'stream' ) + esc_html__( 'When checked, all past records that match the excluded rules above will be hidden from view.', 'stream' ) ), - 'after_field' => __( 'Hide Previous Records', 'stream' ), + 'after_field' => esc_html__( 'Hide Previous Records', 'stream' ), 'default' => 0, ), ),