REST API: add functionality for saving Business Address in JPO#8426
REST API: add functionality for saving Business Address in JPO#8426
Conversation
There was a problem hiding this comment.
Since there are only two keys, the lines from sidebar option fetch up to the loop could be written as
$active_sidebars = get_option( 'sidebars_widgets', array() );
unset( $active_sidebars[ 'wp_inactive_widgets' ], $active_sidebars[ 'array_version' ] );The unset won't fail if the array item is not set.
It could also be written with an uglier one liner that ends up being longer :D
$active_sidebars = array_diff_key( get_option( 'sidebars_widgets', array() ), array_flip( array( 'wp_inactive_widgets', 'array_version' ) ) );There was a problem hiding this comment.
I settled for the first option to aid readability 👍 Thank a lot!
There was a problem hiding this comment.
Using Jetpack_Widgets does not allow wp_inactive_widgets, so there is in fact a single key :)
c4715a9 to
a462a65
Compare
d969051 to
f1dbd45
Compare
f1dbd45 to
511caeb
Compare
Implement saving widget using activate and update `Jetpack_Widgets` functions to inject the widget onto the page and update its content.
code cosmetics as we only have 2 keys here, no changes in functionality.
`set_widget_settings` updates options without checking whether they exist, so this patch adds such a check `update_widget` unnecessarily adds widget to a sidebar patch adds helper function to look for a widget in a given sidebar by its `id_base`.
511caeb to
2006537
Compare
| if ( empty( $active_sidebars ) ) { | ||
| return false; | ||
| } | ||
| return array_shift( array_keys( $active_sidebars ) ); |
There was a problem hiding this comment.
This will throw a notice, because array_shift also updates the original referenced array (and there is no reference here):
PHP Notice: Only variables should be passed by reference in .../jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php on line 1093
|
|
||
| if( $first_sidebar ) { | ||
| $title = wp_unslash( $data[ 'businessAddress' ][ 'name' ] ); | ||
| $address = wp_unslash( |
There was a problem hiding this comment.
Order seems a little weird to me - city, state, street, zip. In the original JPO plugin it was street, city, state, zip - https://github.com/Automattic/jetpack-onboarding/blob/master/class.jetpack-onboarding-end-points.php#L708.
| $first_sidebar = self::get_first_sidebar(); | ||
|
|
||
| if( $first_sidebar ) { | ||
| $title = wp_unslash( $data[ 'businessAddress' ][ 'name' ] ); |
There was a problem hiding this comment.
Do we want to store $data[ 'businessAddress' ] in a variable to make the code a little shorter by accessing all those fields with less code?
| $first_sidebar = self::get_first_sidebar(); | ||
|
|
||
| if( $first_sidebar ) { | ||
| $title = wp_unslash( $data[ 'businessAddress' ][ 'name' ] ); |
There was a problem hiding this comment.
Currently, we don't validate the presence of name, street or any of the others before using it; so we might end up using undeclared variables.
|
|
||
| if( $first_sidebar ) { | ||
| $title = wp_unslash( $data[ 'businessAddress' ][ 'name' ] ); | ||
| $address = wp_unslash( |
There was a problem hiding this comment.
Do we need those wp_unslash? Currently they might remove any backslashes that we have in the address.
| return array_shift( array_keys( $active_sidebars ) ); | ||
| } | ||
|
|
||
| public function has_business_info_widget( $sidebar ) { |
There was a problem hiding this comment.
Can we make this a method in Jetpack_Widgets that accepts a widget base ID (widget_contact_info in our case)? Could be useful, and would be the right place to keep this code IMHO.
Also, can we use something from Jetpack_Widgets to make this one simpler? get_widget_by_id_base() looks like a good candidate.
There was a problem hiding this comment.
@tyxla, I see the point, and at the same time thinking that it is pretty much a JPO-specific method. Are there other contexts in could be reused if defined in a Jetpack module? 🤔
There was a problem hiding this comment.
No usages right now AFAIK, but it doesn't cost anything (other than just adding a parameter for the widget ID base) to do it, and in case we decide to use it in the future, it will be easier and will not require refactor PRs like this one #8464.
| } | ||
|
|
||
| public function update_widget( $id_base, $sidebar, $position, $widget_options ) { | ||
|
|
There was a problem hiding this comment.
Unnecessary whitespace in the beginning of a block.
| return false; | ||
| } | ||
|
|
||
| public function update_widget( $id_base, $sidebar, $position, $widget_options ) { |
There was a problem hiding this comment.
Should this method be in _inc/lib/widgets.php instead? Guess we should figure another name for it? update_widget_by_id_base maybe?
There was a problem hiding this comment.
I put it in a separate method when at the beginning of getting it up and running the code was much longer. Looking at it now, I reckon it is not needed in fact. Slightly re-written it becomes a two-liner and can be used in-place. Do you agree?
There was a problem hiding this comment.
Not sure what exactly you mean - could you please elaborate?
There was a problem hiding this comment.
I removed that function and included: https://github.com/Automattic/jetpack/pull/8426/files#diff-2ccec9069213650359171727de9a2471R1073.
|
|
||
| $widget = Jetpack_Widgets::get_widget_by_id_base( $id_base ); | ||
| $widget_id = $widget['id']; | ||
| Jetpack_Widgets::update_widget($widget_id, $sidebar, $position, $widget_options ); |
There was a problem hiding this comment.
Two consecutive spaces after $widget_id,.
| return $found; | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
Left some feedback in the other PR (#8480) about this one.
|
Thanks for the reviews @tyxla 🙏 I see that a lot of the comments are concerned with what to keep in the endpoint and what to move to module's methods. I kept a lot in the former, not being sure how much of that extends beyond JPO itself. I'll have another 👀 after we merge the previous related PRs. |
| $position = '0'; | ||
| $id_base = 'widget_contact_info'; | ||
|
|
||
| $has_ba_widget = self::has_business_info_widget( $first_sidebar ); |
There was a problem hiding this comment.
@AnnaMag one thing to consider here: for the GET endpoint, we'll need to be able to:
- Know whether business address has been set or not
- Retrieve all those business address fields, in order to be able to return them in the endpoint.
Right now, with your PR, for 1, we can use self::has_business_info_widget( $first_sidebar ), but for 2. we'll need something else, for example storing $data[ 'businessAddress' ] in an option. If you decide to do that, make sure to also add the option to the list of known options, in order it to be cleaned up after uninstalling JP (as done here: https://github.com/Automattic/jetpack/pull/8495/files#diff-cb166c81692a76d1a92d53cb5e80d67cR537).
There was a problem hiding this comment.
As agreed on Slack let's handle it in the follow-up. As always, 💯 point.
Previous commit in the patch removed it assuming that the sidebar already contains the widget to be updated. This will not be the case in general, e.g. when placing a widget in another sidebar. This commit brings it back conditional on the check that the widget is absent from the sidebar. In addition: - it adds a helper function and leaves a couple of comments for clarity. - small code refactor to avoid declaring unnecessary variables - change error code when no sidebar is present - updates error message when no widget is present in a sidebar.'
8b4f0b6 to
aa6ccf5
Compare
_inc/lib/widgets.php
Outdated
There was a problem hiding this comment.
There's a missing parens here
There was a problem hiding this comment.
Was fixed in #8480 and now also ported here. Thanks for reviewing!
3367d09 to
3cd5b3a
Compare
Adding a check of validity of widget`s ID base for new widgets.
The logic in the function grew smaller with time and can be used in-place in a clear way.
| // Add/update business address widget | ||
| if ( isset( $data['businessAddress'] ) ) { | ||
| //grab the first sidebar and proceed if it is present | ||
| $first_sidebar = Jetpack_Widgets::get_first_sidebar(); |
There was a problem hiding this comment.
This gives me
PHP Deprecated: Non-static method Jetpack_Widgets::get_first_sidebar() should not be called statically in jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php on line 1049
Since get_first_sidebar() doesn't seem to refer to the instance ( $this), I think we can actually make it static.
|
I can't seem to get this to work 🙁 How I'm testing: Using the Jetpack Beta Plugin, and selecting 'Bleeding Edge' -- which at this point was 'last updated 4 hours ago', so it doesn't include #8501. Thus, I'm manually applying #8501 and this PR's diff. The error I'm getting is the 'Unexpected Error' notice, and checking
The server debug log contains (a PHP Depracted and PHP Notice aside), the following PHP Warning:
|
|
Tried this now with simply checking out the |
|
Closing in favor of #8523. |
Depends on #8480.
Save Business Address in a sidebar widget during the JPO flow.
Changes proposed in this Pull Request:
Testing instructions:
https://YourJetpackSandbox.com/wp-admin/admin.php?page=jetpack&action=onboard&calypso_env=development, whereYourJetpackSandbox.comis the domain of your JP sandboxAdd a business addressstepNext stephttps://YourJetpackSandbox.com/wp-admin/widgets.phpand/or your site and verify you see the newly created BA widget in the list of widgets and a post sidebarAdd a business addressand modify the BA inputNote: enabling widget module when not done so is a separate PR