Skip to content
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

Merged
merged 14 commits into from
Jan 18, 2018

Conversation

AnnaMag
Copy link
Contributor

@AnnaMag AnnaMag commented Jan 14, 2018

An alternative approach to #8480 and #8426.

Changes proposed in this Pull Request:

  • adds functions required to inject a Business Address widget or update the existing one,
  • saves Business Address data in an option,
  • enables widget module if inactive ( throws an error on failure ).

Testing instructions:

  • Follow the instructions REST API: add functionality for saving Business Address in JPO #8426 tested on a site with a Business Plan ( or temporarily set isBusiness to true in jetpack-onboarding/main.jsx in Calypso).
  • To test that it works for the initially inactive 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 in wp jetpack module list and that the widget has been set correctly.
  • Verify that the jpo_business_address option is set i.e. using wp option get jpo_business_address.

@AnnaMag AnnaMag self-assigned this Jan 14, 2018
@AnnaMag AnnaMag requested a review from a team as a code owner January 14, 2018 23:18
@AnnaMag AnnaMag force-pushed the update/jpo-enable-widgets-no-lib branch 2 times, most recently from a05716e to 6d845d0 Compare January 14, 2018 23:41
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.
@AnnaMag AnnaMag requested review from tyxla and ockham January 14, 2018 23:46
@AnnaMag AnnaMag added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] In Progress labels Jan 14, 2018
Copy link
Member

@tyxla tyxla left a 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 );
Copy link
Member

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() {
Copy link
Member

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 ) {
Copy link
Member

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 ) {
Copy link
Member

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 ) );
Copy link
Member

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
Copy link
Member

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' ] . ' ' .
Copy link
Member

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?

Copy link
Contributor Author

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' ];
Copy link
Member

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 ) ) {
Copy link
Member

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 );
Copy link
Member

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.

@tyxla tyxla added [Status] In Progress and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Jan 15, 2018
@AnnaMag AnnaMag added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Jan 15, 2018
@tyxla tyxla added this to the 5.8 milestone Jan 15, 2018
'email' => ''
);

if ( ! self::has_business_address_widget( $first_sidebar ) ) {
Copy link
Member

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

Copy link
Contributor Author

@AnnaMag AnnaMag Jan 15, 2018

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

…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.
@AnnaMag AnnaMag added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] In Progress labels Jan 15, 2018
@AnnaMag AnnaMag force-pushed the update/jpo-enable-widgets-no-lib branch from 4e16702 to 370809e Compare January 15, 2018 22:54
@AnnaMag
Copy link
Contributor Author

AnnaMag commented Jan 15, 2018

Thanks for reviewing @tyxla. Nice sugestions!

The patch has been adapted 👍 I'd appreciate another look when you get a chance. Thanks!

@AnnaMag AnnaMag force-pushed the update/jpo-enable-widgets-no-lib branch from bcd0652 to 217ee22 Compare January 16, 2018 21:58
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.
@AnnaMag
Copy link
Contributor Author

AnnaMag commented Jan 16, 2018

Feedback incorporated 👍 Thanks @tyxla!

Copy link
Member

@tyxla tyxla left a 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 );
Copy link
Member

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'] )
: '';
Copy link
Member

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 = '';
Copy link
Member

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 );
Copy link
Member

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.

}

/**
* Update the content of an exisitng widget in a given sidebar.
Copy link
Member

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

* @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.
Copy link
Member

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.

* @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.
Copy link
Member

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_key = false;
foreach ( $sidebars_widgets[ $sidebar ] as $widget ) {
if ( strpos( $widget, 'widget_contact_info' ) !== false ) {
$widget_key = absint( str_replace( 'widget_contact_info-', '', $widget ) );
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes!

@tyxla tyxla requested review from zinigor and dereksmart January 17, 2018 08:53
@tyxla
Copy link
Member

tyxla commented Jan 17, 2018

@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.
@AnnaMag
Copy link
Contributor Author

AnnaMag commented Jan 17, 2018

All points addressed 👍 . Thanks for an awesome review @tyxla !

Copy link
Member

@tyxla tyxla left a 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 ❤️

@@ -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.
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♀️

Copy link
Member

@zinigor zinigor left a 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.


// Store updated sidebars, widgets and their instances
if ( ! ( update_option( 'sidebars_widgets', $sidebars_widgets ) )
|| ( ! ( update_option( 'widget_' . $widget_id, $widget_instances ) ) ) ) {
Copy link
Member

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
) {

@AnnaMag
Copy link
Contributor Author

AnnaMag commented Jan 18, 2018

Thanks for the review @zinigor! Styling addressed 👍
Could you have another look, please? Feel free to merge :)

@zinigor zinigor added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Jan 18, 2018
@zinigor
Copy link
Member

zinigor commented Jan 18, 2018

Thanks! Please merge once Travis passes.

@AnnaMag AnnaMag merged commit 4531d28 into master Jan 18, 2018
@AnnaMag AnnaMag deleted the update/jpo-enable-widgets-no-lib branch January 18, 2018 21:18
@tyxla tyxla removed the [Status] Ready to Merge Go ahead, you can push that green button! label Jan 18, 2018
jeherve added a commit that referenced this pull request Jan 25, 2018
zinigor pushed a commit that referenced this pull request Jan 30, 2018
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants