Skip to content

Commit

Permalink
Add Analytics Hits, Enhance Error Reporting, Router Refactor (#63)
Browse files Browse the repository at this point in the history
* Button Component: Unify frontend styles

* patch theme preview buttons

* remove isLink prop from Blue Sky buttons

* menu highlighting first pass

get nav highlighting working for all pages but individual product details

* refactor router

* change data left on window object

* Merge branch 'master' into enhance/router-refactor

* progress

* resolve merge conflicts

* updated pot

* more progress

* post-merge conflict resolution

* push working pageview

* ko console logs

* router marketplace enhancements, a11y, pv events

* progress push to clone to clean environment

* working error logging

* imports cleanup

* notice dismiss

* nix test route

* remove unused browser detect dependency

* remove legacy help test

* restore adminUrl

* Fix tests

* Code styling fixes

* Bump version to 2.5.1

* Use the same cache version

* Set PHP version and config

* Override defaults for GitHub

* Don't check the vendor directory when linting YML files

* Try storing error logs on test failure.

* fix redirect issue

* Update GitHub Action to properly store error log

* Update Cypress workflow to properly copy debug.log to artifacts when present

Co-authored-by: Micah Wood <micah@wpscholar.com>
  • Loading branch information
0aveRyan and wpscholar authored Jan 5, 2021
1 parent 6238c77 commit 5e49e5c
Show file tree
Hide file tree
Showing 145 changed files with 29,497 additions and 29,471 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache Composer vendor directory
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
Expand Down Expand Up @@ -92,7 +92,7 @@ jobs:
run: find .

- name: Configure WordPress
run: echo '{"port":8880,"plugins":["${{ steps.workflow.outputs.DIST }}/${{ steps.workflow.outputs.PACKAGE }}"]}' > .wp-env.json
run: echo '{"config":{"WP_DEBUG_DISPLAY":false},"plugins":["${{ steps.workflow.outputs.DIST }}/${{ steps.workflow.outputs.PACKAGE }}"]}' > .wp-env.override.json

- name: Install WordPress
run: npx wp-env start
Expand All @@ -106,3 +106,18 @@ jobs:
with:
name: screenshots
path: ./tests/cypress/screenshots

- name: Check if a debug.log file exists
id: hasErrors
run: npx wp-env run wordpress test ! -f /var/www/html/wp-content/debug.log

- name: Copy debug.log file
if: ${{ steps.hasErrors.outputs.failure }}
run: cat /var/www/html/wp-content/debug.log 1>./debug.log 2>/dev/null

- name: Store debug.log file
if: ${{ steps.hasErrors.outputs.failure }}
uses: actions/upload-artifact@v2
with:
name: logs
path: ./debug.log
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
if: "!! env.GIT_DIFF"

- name: Cache Composer vendor directory
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/upload-artifact-on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: echo "::set-output name=dir::$(npm config get cache)"

- name: Cache node modules
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
Expand All @@ -71,7 +71,7 @@ jobs:
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache Composer vendor directory
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/upload-asset-on-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
run: echo "::set-output name=dir::$(npm config get cache)"

- name: Cache node modules
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
Expand All @@ -72,7 +72,7 @@ jobs:
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache Composer vendor directory
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ cypress.env.json
/.docs/*
!/assets/bluehost.css
!/assets/admin-global.css

# File Types
*.log
*.sql
*.zip
6 changes: 6 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"config": {
"WP_DEBUG": true,
"WP_DEBUG_LOG": true,
"WP_DEBUG_DISPLAY": true
},
"phpVersion": "5.6",
"plugins": [
"."
],
Expand Down
2 changes: 1 addition & 1 deletion assets/images/white-triangle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions bluehost-wordpress-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Bluehost
* Description: This plugin integrates your WordPress site with the Bluehost control panel, including performance, security, and update features.
* Version: 2.5.0
* Version: 2.5.1
* Requires at least: 4.7
* Requires PHP: 5.6
* Author: Bluehost
Expand All @@ -27,7 +27,7 @@
}

// Define constants
define( 'BLUEHOST_PLUGIN_VERSION', '2.5.0' );
define( 'BLUEHOST_PLUGIN_VERSION', '2.5.1' );
define( 'BLUEHOST_PLUGIN_FILE', __FILE__ );
define( 'BLUEHOST_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'BLUEHOST_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
Expand Down
3 changes: 3 additions & 0 deletions inc/Notifications/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class AdminNotices {
* Render admin notices where appropriate.
*/
public static function maybeRenderAdminNotices() {
if ( isset( $_GET['page'] ) && 'bluehost' === \filter_input( INPUT_GET, 'page' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}
$page = str_replace( admin_url(), '', Url::getCurrentUrl() );
$notifications = new NotificationsRepository();
$collection = $notifications->collection();
Expand Down
173 changes: 62 additions & 111 deletions inc/RestApi/AdminErrorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ class AdminErrorController extends \WP_REST_Controller {
protected $endpoint = '/error/track';

/**
* Number of Errors stored in Database.
* Number of versions to retain
*
* @var integer
*/
protected $error_count = 10;
protected $retain_versions = 5;

/**
* Number of Incidents stored per-error.
* Errors per-version to retain
*
* @var integer
*/
protected $error_incident_count = 5;
protected $errors_per_version = 12;

/**
* Undocumented variable
* Unique key for error.
*
* @var [type]
* @var string
*/
protected $key;

Expand Down Expand Up @@ -79,6 +79,13 @@ class AdminErrorController extends \WP_REST_Controller {
*/
protected $updated;

/**
* Conditional outcome (for error-tracing this error reporting)
*
* @var integer
*/
protected $condition = 0;

/**
* Register routes.
*/
Expand All @@ -104,17 +111,21 @@ public function register_routes() {
* @return \WP_REST_Response
*/
public function error_logging( \WP_REST_Request $request ) {
$this->params = $request->get_body_params();
unset( $this->params['browser']['versionNumber'] );
$this->params = $request->get_params();
if ( ! empty( $this->params ) ) {
$code = $this->handle_error_log();
$this->condition = 0.5;
$this->key = base64_encode( $this->params['message'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$response = $this->handle_error_log();
} else {
$this->condition = 0.25;
}

return new \WP_REST_Response(
array(
'success' => 'it-the-success',
'code' => $code,
)
'condition' => $this->condition,
'debug' => $request,
),
false === $response ? 500 : 200
);
}

Expand All @@ -124,110 +135,50 @@ public function error_logging( \WP_REST_Request $request ) {
* @return int
*/
protected function handle_error_log() {
$this->saved = $this->database( 'get' );

if ( ! $this->should_log( $this->saved ) ) {
if ( ! empty( $this->saved['error_max'] || ! $this->saved['error_max'] ) ) {
$this->database(
'update',
array_merge(
$this->saved,
array(
'error_max' => true,
'date' => new \DateTime(),
)
)
);
$plug_ver = BLUEHOST_PLUGIN_VERSION;
$this->saved = $this->database( 'get' );
$this->updated = $this->saved;

if ( isset( $this->saved[ $plug_ver ] ) ) {
if ( count( $this->saved[ $plug_ver ] ) <= $this->errors_per_version ) {
array_unshift( $this->updated[ $plug_ver ], $this->key );
$this->updated[ $plug_ver ] = array_unique( $this->updated[ $plug_ver ] );
$this->condition = 1;
} else {
array_pop( $this->updated[ $plug_ver ] );
array_unshift( $this->updated[ $plug_ver ], $this->key );
$this->updated[ $plug_ver ] = array_unique( $this->updated[ $plug_ver ] );
$this->condition = 2;
}

return 0;
} elseif ( count( $this->saved ) <= $this->retain_versions ) {
$this->updated[ $plug_ver ] = array( $this->key );
$this->condition = 3;
} else {
array_pop( $this->updated );
$this->updated[ $plug_ver ] = array( $this->key );
$this->condition = 4;
}

$this->updated = $this->saved; // make immutable
$this->key = base64_encode( $this->params['message'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$this->browser_key = base64_encode( implode( ',', $this->params['browser'] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode

if ( $this->params['browser']['mobile'] ) {
$this->params['browser']['mobile'] = true === $this->params['browser']['mobile'] ? 'mobile' : 'desktop';
}

if ( ! empty( $this->updated[ $this->key ] ) && ! empty( $this->updated[ $this->key ]['count'] ) ) {
$count = (int) $this->updated[ $this->key ]['count'];
if ( ! isset( $this->updated['error_max'] ) || 'true' !== $this->updated['error_max'] ) {
if ( ! in_array( implode( ',', $this->params['browser'] ), $this->updated[ $this->key ]['browsers'], true ) ) {
$this->updated[ $this->key ]['browsers'][] = implode( ',', $this->params['browser'] );
}
$this->make_log_summary();
$this->limit_log_entries();
$this->updated[ $this->key ]['max'] = 'true';
} elseif ( ( (int) $this->error_count - 1 ) >= $count ) {
$this->updated[ $this->key ]['count'] = (int) $this->updated[ $this->key ]['count'] + 1;
if ( ! in_array( implode( ',', $this->params['browser'] ), $this->updated[ $this->key ]['browsers'], true ) ) {
$this->updated[ $this->key ]['browsers'][] = implode( ',', $this->params['browser'] );
}
$this->make_log_summary();
$this->limit_log_entries();
}
} elseif ( ! isset( $this->updated[ $this->key ] ) ) {
$this->updated[ $this->key ] = array(
'count' => 1,
'message' => $this->params['message'],
'browsers' => array(
implode( ',', $this->params['browser'] ),
),
if ( ! isset( $this->saved[ $plug_ver ][ $this->key ] ) ) {
$request = new \WP_REST_Request( 'POST', '/bluehost/v1/data/events' );
$request->set_body_params(
array(
'action' => 'bwa-error',
'category' => 'AdminError',
'data' => $this->params,
)
);
$this->make_log_summary();
}

if ( $this->updated !== $this->saved ) {
return $this->database( 'update', $this->updated ) ? 1 : 2;
}
$response = \rest_do_request( $request );
if ( ! $response->is_error() ) {
$this->database( 'update', $this->updated );

return 3;
}

/**
* Make Incident Summary from Available Parameters.
*/
protected function make_log_summary() {
$this->updated[ $this->key ]['incidents'][ $this->params['date'] ] = $this->params['browser']['name'] . ' ' . $this->params['browser']['version'] . ' on ' . $this->params['browser']['os'];
}

/**
* Limit most log entries per-error based on $this->error_incident_count
*
* @return void
*/
protected function limit_log_entries() {
$this->updated[ $this->key ]['incidents'] = array_slice(
$this->updated[ $this->key ]['incidents'],
- $this->error_incident_count
);
}

/**
* Evaluate data to determine if it should be logged.
*
* @param array $saved Collection of saved errors.
*
* @return bool
*/
protected function should_log( $saved = array() ) {
$filter = apply_filters( 'bluehost_admin_error_logging_active', true );

if (
true === (
! empty( $saved['error_max'] )
&& is_bool( $saved['error_max'] )
&& true === (bool) $saved['error_max']
) ||
count( $stored ) >= $this->error_store_count ||
( true || 1 ) !== $filter
) {
return false;
return $response;
} else {
return false;
}
} else {
return null;
}

return true;
}

/**
Expand All @@ -248,7 +199,7 @@ protected function database( $type = 'get', $data = array() ) {
case 'read':
case 'get':
default:
return \get_option( $this->option_key );
return \get_option( $this->option_key, array() );
}
}

Expand Down
Loading

0 comments on commit 5e49e5c

Please sign in to comment.