-
Notifications
You must be signed in to change notification settings - Fork 800
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
REST API: add functionality for saving Business Address widget in JPO (alternative) #8523
Conversation
a05716e
to
6d845d0
Compare
This commit adds the functions required to inject a Business Address widget or update an exisiting one in the remote site during the JPO flow. Alternative approach to using Jetpack_Widgets.
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.
Looks awesome for a first version! I think this should be the easier and quicker way to go.
Left some feedback on things we need to iterate on from the JPO plugin version in order to adapt it to fit the Jetpack codebase.
@@ -1053,6 +1053,10 @@ private function _process_onboarding( $data ) { | |||
} | |||
} | |||
|
|||
if ( isset( $data['businessAddress'] ) ) { | |||
self::add_business_address( $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.
We still need to handle the errors in the same way that we handle them for the rest of the onboarding settings (adding them to the error
array). So you might want to update add_business_address()
to return either an array of errors, a WP_Error
object, or true
on success; and if there are any errors, merge them with $errors
.
@@ -1066,6 +1070,126 @@ private function _process_onboarding( $data ) { | |||
: join( ', ', $error ); | |||
} | |||
|
|||
static function get_first_sidebar() { |
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.
How about just moving this one the widgets lib and using it statically from there?
* | ||
* @param string $sidebar ID of the sidebar to which the widget will be added. | ||
*/ | ||
static function insert_widget_in_sidebar( $widget_id, $widget_options, $sidebar ) { |
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.
How about just moving this one the widgets lib and using it statically from there?
* | ||
* @param string $sidebar ID of the sidebar to which the widget will be added. | ||
*/ | ||
static function update_widget_in_sidebar( $widget_id, $widget_options, $sidebar ) { |
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.
How about just moving this one the widgets lib and using it statically from there?
} else { | ||
self::update_widget_in_sidebar( 'widget_contact_info', $widget_options, $first_sidebar ); | ||
} | ||
wp_send_json_success( array( 'updated' => true ) ); |
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.
We're not supposed to call wp_send_json_success
here, because it'll end the request in an unexpected way. Jetpack_Core_API_Data::update_data()
already handles this by either calling a rest_ensure_response()
on success, or returning a WP_Error
object on failure, so we only need to pass on the errors, if any, and success messages, if any (following the pattern that _process_onboarding
already implements).
$sidebars_widgets[ $sidebar ] = array(); | ||
} | ||
$sidebars_widgets[ $sidebar ][] = $widget_id . '-' . $next_key; | ||
// Add the new widget instance |
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.
Nit: Adding a newline before each of those //
comments, will make the methods you introduce more readable.
if ( $first_sidebar ) { | ||
$title = $data['businessAddress'][ 'name' ]; | ||
$address = | ||
$data[ 'businessAddress' ][ 'city' ] . ' ' . |
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.
Indentation appears a little off here. Do we want to make this one a little smarter, by grabbing all values, then filtering out any empty ones, and then building the string?
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.
Where are you prettify? 😆
$first_sidebar = self::get_first_sidebar(); | ||
|
||
if ( $first_sidebar ) { | ||
$title = $data['businessAddress'][ 'name' ]; |
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.
Code style: we don't wrap specific array keys with leading/trailing spaces, so instances like [ 'name' ]
need to become like this: ['name']
.
Contrary to this, variable keys need to have trailing/leading spaces, like this: [ $key ]
.
'email' => '' | ||
); | ||
|
||
if ( ! self::has_business_address_widget( $first_sidebar ) ) { |
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.
Do we also want in this PR to store the business address in an option to make it available for the REST API? Should be a quick one.
if ( ! self::has_business_address_widget( $first_sidebar ) ) { | ||
self::insert_widget_in_sidebar( 'widget_contact_info', $widget_options, $first_sidebar ); | ||
} else { | ||
self::update_widget_in_sidebar( 'widget_contact_info', $widget_options, $first_sidebar ); |
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.
Let's make sure to capture errors from those methods and pass them on to $errors
.
'email' => '' | ||
); | ||
|
||
if ( ! self::has_business_address_widget( $first_sidebar ) ) { |
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.
We need to make sure the widgets module is active, you can borrow the code from here:
https://github.com/Automattic/jetpack/pull/8481/files#diff-2ccec9069213650359171727de9a2471R1048
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.
We already have that in the pipeline, I believe #8481. Will need to be adapted though as it served the other approach.
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.
How do you feel about handling these in the same PR? It's only those 4 lines.
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.
Yes, we can 👍 closing it then.
bfa5b5d
to
23dd7c7
Compare
…usiness Ad. widget style fixes: separating comments, fixing spacing, adding f-ns definitions. pass only relevant part of data to the function handling business address. error handle: specify default values to handle case where no errors are thrown.
4e16702
to
370809e
Compare
Thanks for reviewing @tyxla. Nice sugestions! The patch has been adapted 👍 I'd appreciate another look when you get a chance. Thanks! |
Add to known options.
bcd0652
to
217ee22
Compare
This funcionality is generic and not directly related to the logic of inserting a business address widget.
…aved option. Done to avoid passing empty fields to the widget. This commit also sanitizes the option storing widget content.
Feedback incorporated 👍 Thanks @tyxla! |
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 works like a charm, awesome work 👍 ❤️
Left several comments, but all of them are super minor styling things, nothing blocking.
$widgets_module_active = Jetpack::activate_module( 'widgets', false, false ); | ||
} | ||
if ( ! $widgets_module_active ) { | ||
return new WP_Error( 'module_activation_failed', 'Failed to activate module.', 400 ); |
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.
Maybe worth clarifying which module we failed to activate: Failed to activate the widgets module.
if ( $first_sidebar ) { | ||
$title = isset( $address['name'] ) ? sanitize_text_field( $address['name'] ) : ''; | ||
$street = isset( $address['street'] ) ? sanitize_text_field( $address['street'] ) | ||
: ''; |
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.
Let's keep this on the same line.
); | ||
|
||
$widget_inserted = ''; | ||
$widget_updated = ''; |
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 do we need 2 separate variables for handling insert and update if we return the same error message?
} | ||
|
||
// No sidebar to place the widget | ||
return new WP_Error( 'sidebar_failed', 'No sidebar.', 400 ); |
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'd rephrase sidebar_failed
to sidebar_not_found
.
_inc/lib/widgets.php
Outdated
} | ||
|
||
/** | ||
* Update the content of an exisitng widget in a given sidebar. |
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.
We've got a typo here: exisitng
_inc/lib/widgets.php
Outdated
* @param string $sidebar ID of the sidebar to which the widget will be added. | ||
* | ||
* @return WP_Error|true True when data has been updated correctly, | ||
* error otherwise. |
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.
Let's keep this on the same line.
_inc/lib/widgets.php
Outdated
* @param string $sidebar ID of the sidebar to which the widget will be added. | ||
* | ||
* @return WP_Error|true True when data has been saved correctly, | ||
* error otherwise. |
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.
Let's keep this on the same line.
_inc/lib/widgets.php
Outdated
$widget_key = false; | ||
foreach ( $sidebars_widgets[ $sidebar ] as $widget ) { | ||
if ( strpos( $widget, 'widget_contact_info' ) !== false ) { | ||
$widget_key = absint( str_replace( 'widget_contact_info-', '', $widget ) ); |
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.
Should we use $widget_id
instead of 'widget_contact_info'
here?
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.
yes!
@dereksmart @zinigor can we please get a review of this one? Thank you! |
Fixes typo, removes redundant var declaration, error message or description re-write, removing redundant lines.
All points addressed 👍 . Thanks for an awesome review @tyxla ! |
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.
LGTM 👍 Great work Anna ❤️
_inc/lib/widgets.php
Outdated
@@ -715,14 +714,13 @@ static function insert_widget_in_sidebar( $widget_id, $widget_options, $sidebar | |||
} | |||
|
|||
/** | |||
* Update the content of an exisitng widget in a given sidebar. | |||
* Update the content of an exisiting widget in a given sidebar. |
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.
Should be existing
instead of exisiting
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.
🤦♀️
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.
Tested: widget successfully added with the correct address, code LGTM, but I have one small nitpick.
_inc/lib/widgets.php
Outdated
|
||
// Store updated sidebars, widgets and their instances | ||
if ( ! ( update_option( 'sidebars_widgets', $sidebars_widgets ) ) | ||
|| ( ! ( update_option( 'widget_' . $widget_id, $widget_instances ) ) ) ) { |
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.
Minor nitpick: can we please have the if
indented this way for readability?
if (
condition
|| condition2
) {
Thanks for the review @zinigor! Styling addressed 👍 |
Thanks! Please merge once Travis passes. |
* Changelog 5.8: create base for changelog. * Update 5.8 release post link * fix 5.8 release date * Updates to plugin description * Changelog: add #8499 * Changelog: add #8506 * Changelog: add #8509 * Changelog: add #8516 * Changelog: add #8517 * Changelog: add #8523 * Changelog: add #8547 * Changelog: add #8496 * Changelog: add #8584 * Changelog: add #8595 * Changelog: add #8445 * Changelog: add #8431 * Changelog: add #8284 * Changelog: add #8270 * Changelog: add #8124 * Changelog: add #8581 * Changelog: add #8463 * Changelog: add #8568 (#8646) * Updates to testing list and changelog * Changelog: add #8443 * Changelog: add #8459 * Changelog: add #8469 * Changelog: add #8464 * Changelog: add #8478 and #8479 * Changelog: add #8483 * Changelog: add #8488 * Changelog: add #8513 * Changelog: add #8555 * Changelog: add #8565 * Changelog: add #8601 * Changelog: add #8612 * Changelog: add first pass at Search items. * Changelog: add more info to help test Search. * Changelog: add #8144 * Changelog: add #8313 * Changelog: add #8419 * Changelog: add #8465 * Changelog: add #8515 * Changelog: add #8587 * Changelog: add #8591 * Changelog: add #8659 * Changelog: add #8661 * Changelog: add #8671 * Changelog: add 5.7.1 to archived changelog too. * Reverted changes to readme, removed entry about backups.
An alternative approach to #8480 and #8426.
Changes proposed in this Pull Request:
Testing instructions:
isBusiness
totrue
in jetpack-onboarding/main.jsx in Calypso).widgets
module:use
wp jetpack module deactivate
to deactivate the module and follow the previous instructions. After, verify that the module is listed as active inwp jetpack module list
and that the widget has been set correctly.jpo_business_address
option is set i.e. usingwp option get jpo_business_address
.