-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update support request flow. #6627
Conversation
Plugin builds for 4fe6689 are ready 🛎️!
|
44b3b42
to
363d8dc
Compare
@@ -0,0 +1,27 @@ | |||
Feature: AMP Support Request |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to also test data for a validated URL is in the output as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To test validated URLs we need to mock those. (Like done previouslly ) But it's not the case here.
(Sorry pushed to the wrong branch in #6627 (comment)). There's a few more issues in #6147 to fix (which is why #5939 was re-opened), so this PR shouldn't close the issue just yet. |
This PR also addresses feedbacks from PR #6147. |
@@ -170,8 +173,8 @@ public function enqueue_assets( $hook_suffix ) { | |||
]; | |||
} | |||
|
|||
$this->support_data->set_args( $args ); | |||
$data = $this->support_data->get_data(); | |||
$support_data = $this->injector->make( SupportData::class, [ 'args' => $args ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After taking a closer look, I think instantiating SupportData
via the injector makes things overly complicated. Directly creating a SupportData
object should be simple enough here, and therefore there would be no need for Injector
.
$support_data = $this->injector->make( SupportData::class, [ 'args' => $args ] ); | |
$support_data = new SupportData( $args ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another alternative:
$support_data = $this->injector->make( SupportData::class, [ 'args' => $args ] ); | |
$support_data = $this->injector->make( SupportData::class, compact( 'args' ) ); |
$support_data = $this->injector->make( SupportData::class, [ 'args' => $args ] ); | ||
$data = $support_data->get_data(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, to get the data in one line:
$support_data = $this->injector->make( SupportData::class, [ 'args' => $args ] ); | |
$data = $support_data->get_data(); | |
$data = ( new SupportData( $args ) )->get_data(); |
@@ -106,8 +111,8 @@ public function send_diagnostic( /** @noinspection PhpUnusedParameterInspection | |||
'is_synthetic' => $is_synthetic, | |||
]; | |||
|
|||
$this->support_data->set_args( $args ); | |||
$data = $this->support_data->get_data(); | |||
$support_data = $this->injector->make( SupportData::class, [ 'args' => $args ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same suggestion as above, creating the SupportData
object should be simple enough.
$support_data = $this->injector->make( SupportData::class, [ 'args' => $args ] ); | |
$support_data = new SupportData( $args ); |
@@ -476,11 +450,7 @@ public static function normalize_error( $error_data ) { | |||
|
|||
$error_data['text'] = ( ! empty( $error_data['text'] ) ) ? trim( $error_data['text'] ) : ''; | |||
|
|||
$error_data = wp_json_encode( $error_data ); | |||
$error_data = static::remove_domain( $error_data ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for overloading the remove_domain()
method and not just iterating over the error data and calling remove_domain()
on each value?
$error_data = static::remove_domain( $error_data ); | |
foreach ( $error_data as $key => $value ) { | |
$error_data[ $key ] = self::remove_domain( $value ); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Internally self::remove_domain()
will do the same thing if the provided value is an array. I kept that part in self::remove_domain()
because $error_data
can be multi-dimension array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. Not a fan of overloading remove_domain()
and returning multiple data types however as that increases code complexity.
@@ -101,10 +103,10 @@ public function callback( WP_REST_Request $request ) { | |||
$request_args = $request->get_param( 'args' ); | |||
$request_args = ( ! empty( $request_args ) && is_array( $request_args ) ) ? $request_args : []; | |||
|
|||
$this->support_data->set_args( $request_args ); | |||
$support_response = $this->support_data->send_data(); | |||
$support_data = $this->injector->make( SupportData::class, [ 'args' => $request_args ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same suggestion as above, creating the SupportData
object should be simple enough.
$support_data = $this->injector->make( SupportData::class, [ 'args' => $request_args ] ); | |
$support_data = new SupportData( $args ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally it was $support_data = new SupportData( $args );
. I have used an injector after suggestions from the previous PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yea, but unless I'm missing something there's no benefit in creating the SupportData
via the injector as its not a service or anything. cc @westonruter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another example of using the injector to create a class can be found in PairedRouting
:
Lines 252 to 263 in 002f88e
$structure_class = apply_filters( 'amp_custom_paired_url_structure', null ); | |
if ( ! $structure_class || ! is_subclass_of( $structure_class, PairedUrlStructure::class ) ) { | |
$structure_slug = AMP_Options_Manager::get_option( Option::PAIRED_URL_STRUCTURE ); | |
if ( array_key_exists( $structure_slug, self::PAIRED_URL_STRUCTURES ) ) { | |
$structure_class = self::PAIRED_URL_STRUCTURES[ $structure_slug ]; | |
} else { | |
$structure_class = QueryVarUrlStructure::class; | |
} | |
} | |
$this->paired_url_structure = $this->injector->make( $structure_class ); |
While in practice it makes no difference, in theory the injector is better because this could reference an interface instead of an actual class, and the bindings defined in AmpWpPlugin
could automatically route the class to instantiate. This could make it easier to test with a mock, as I understand.
Also, by using make
the injector is able to re-use instances which are listed among the shared instances in AmpWpPlugin
, which isn't possible otherwise.
So yeah, I understand from dependency injection that classes should generally not be instantiated directly.
cc @schlessera
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation @westonruter. In this scenario we wouldn't be benefiting from using the injector, since the class being instantiated does not require any sort of dependency resolution and so would not make use of the bindings or shared instances provided by AmpWpPlugin
. However, I do understand the benefits of using the injector with regards to testing especially, so after giving it more thought I'm OK with the current implementation.
@@ -89,7 +89,7 @@ export function AMPSupport( props ) { | |||
{ | |||
__html: sprintf( | |||
/* translators: %s is the URL to create a new support topic */ | |||
__( 'In order to best assist you, please submit the following information to our private database. Once you have done so, copy the the resulting support ID and mention it in a <a href="%s" rel="noreferrer" target="_blank">support forum topic</a>. You do not have to submit data to get support, but our team will be able to help you more effectively if you do so.', 'amp' ), | |||
__( 'In order to best assist you, please click the Send Data button below to send the following site information to our private database. Once you have done so, copy the the resulting Support UUID in the blue box that appears and include the ID in a new <a href="%s" rel="noreferrer" target="_blank">support forum topic</a>. You do not have to submit data to get support, but our team will be able to help you more effectively if you do so.', 'amp' ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__( 'In order to best assist you, please click the Send Data button below to send the following site information to our private database. Once you have done so, copy the the resulting Support UUID in the blue box that appears and include the ID in a new <a href="%s" rel="noreferrer" target="_blank">support forum topic</a>. You do not have to submit data to get support, but our team will be able to help you more effectively if you do so.', 'amp' ), | |
__( 'In order to best assist you, please tap the Send Data button below to send the following site information to our private database. Once you have done so, copy the the resulting Support UUID in the blue box that appears and include the ID in a new <a href="%s" rel="noreferrer" target="_blank">support forum topic</a>. You do not have to submit data to get support, but our team will be able to help you more effectively if you do so.', 'amp' ), |
@@ -101,10 +103,10 @@ public function callback( WP_REST_Request $request ) { | |||
$request_args = $request->get_param( 'args' ); | |||
$request_args = ( ! empty( $request_args ) && is_array( $request_args ) ) ? $request_args : []; | |||
|
|||
$this->support_data->set_args( $request_args ); | |||
$support_response = $this->support_data->send_data(); | |||
$support_data = $this->injector->make( SupportData::class, [ 'args' => $request_args ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another example of using the injector to create a class can be found in PairedRouting
:
Lines 252 to 263 in 002f88e
$structure_class = apply_filters( 'amp_custom_paired_url_structure', null ); | |
if ( ! $structure_class || ! is_subclass_of( $structure_class, PairedUrlStructure::class ) ) { | |
$structure_slug = AMP_Options_Manager::get_option( Option::PAIRED_URL_STRUCTURE ); | |
if ( array_key_exists( $structure_slug, self::PAIRED_URL_STRUCTURES ) ) { | |
$structure_class = self::PAIRED_URL_STRUCTURES[ $structure_slug ]; | |
} else { | |
$structure_class = QueryVarUrlStructure::class; | |
} | |
} | |
$this->paired_url_structure = $this->injector->make( $structure_class ); |
While in practice it makes no difference, in theory the injector is better because this could reference an interface instead of an actual class, and the bindings defined in AmpWpPlugin
could automatically route the class to instantiate. This could make it easier to test with a mock, as I understand.
Also, by using make
the injector is able to re-use instances which are listed among the shared instances in AmpWpPlugin
, which isn't possible otherwise.
So yeah, I understand from dependency injection that classes should generally not be instantiated directly.
cc @schlessera
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, passing onto @westonruter for a final review.
@@ -476,11 +450,7 @@ public static function normalize_error( $error_data ) { | |||
|
|||
$error_data['text'] = ( ! empty( $error_data['text'] ) ) ? trim( $error_data['text'] ) : ''; | |||
|
|||
$error_data = wp_json_encode( $error_data ); | |||
$error_data = static::remove_domain( $error_data ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. Not a fan of overloading remove_domain()
and returning multiple data types however as that increases code complexity.
Codecov Report
@@ Coverage Diff @@
## develop #6627 +/- ##
=============================================
+ Coverage 76.63% 77.16% +0.53%
- Complexity 6318 6412 +94
=============================================
Files 248 250 +2
Lines 19812 20077 +265
=============================================
+ Hits 15182 15493 +311
+ Misses 4630 4584 -46
Flags with carried forward coverage won't be shown. Click here to find out more.
|
…rces with deleted themes/plugins
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've addressed some of my PR feedback with commits.
@@ -42,15 +51,16 @@ public function register() { | |||
// Add support link to Admin Bar. | |||
add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 105 ); | |||
|
|||
// Add support link to meta box. | |||
add_filter( 'amp_validated_url_status_actions', [ $this, 'amp_validated_url_status_actions' ], 10, 2 ); | |||
if ( is_admin() ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not strictly required since outside the admin these actions won't do anything, but it doesn't hurt.
@@ -170,8 +173,8 @@ public function enqueue_assets( $hook_suffix ) { | |||
]; | |||
} | |||
|
|||
$this->support_data->set_args( $args ); | |||
$data = $this->support_data->get_data(); | |||
$support_data = $this->injector->make( SupportData::class, [ 'args' => $args ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another alternative:
$support_data = $this->injector->make( SupportData::class, [ 'args' => $args ] ); | |
$support_data = $this->injector->make( SupportData::class, compact( 'args' ) ); |
* @codeCoverageIgnore | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why ignore coverage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, because now it is covered in the feature test.
if ( is_resource( $file ) ) { | ||
while ( ! feof( $file ) ) { | ||
$line = fgets( $file ); | ||
$lines[] = $line; | ||
$line_count = count( $lines ); | ||
if ( $line_count > $no_of_lines ) { | ||
array_shift( $lines ); | ||
} | ||
} | ||
|
||
fclose( $file ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this looks like it will do the job, it is still reading through the entire file. It is using less memory now, which is good, but it could be more optimal by only seeking through the file starting from the end. For example: https://stackoverflow.com/a/15017711/93579
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would still be good to do.
Summary
Amends #6147
Related: #5939
Checklist