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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions _inc/lib/core-api/class.jetpack-core-api-module-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ public function update_data( $request ) {
break;

case 'onboarding':
jetpack_require_lib( 'widgets' );
// Break apart and set Jetpack onboarding options.
$result = $this->_process_onboarding( (array) $value );
if ( empty( $result ) ) {
Expand Down Expand Up @@ -1053,6 +1054,13 @@ private function _process_onboarding( $data ) {
}
}

if ( isset( $data['businessAddress'] ) ) {
$handled_business_address = self::handle_business_address( $data['businessAddress'] );
if ( is_wp_error( $handled_business_address ) ) {
$error[] = 'BusinessAddress';
}
}

if ( ! empty( $data['installWooCommerce'] ) ) {
jetpack_require_lib( 'plugins' );
$wc_install_result = Jetpack_Plugins::install_and_activate_plugin( 'woocommerce' );
Expand All @@ -1066,6 +1074,87 @@ private function _process_onboarding( $data ) {
: join( ', ', $error );
}

/**
* Add or update Business Address widget.
*
* @param array $address Array of business address fields.
*
* @return WP_Error|true True if the data was saved correctly.
*/
static function handle_business_address( $address ) {
$first_sidebar = Jetpack_Widgets::get_first_sidebar();

$widgets_module_active = Jetpack::is_module_active( 'widgets' );
if ( ! $widgets_module_active ) {
$widgets_module_active = Jetpack::activate_module( 'widgets', false, false );
}
if ( ! $widgets_module_active ) {
return new WP_Error( 'module_activation_failed', 'Failed to activate the widgets module.', 400 );
}

if ( $first_sidebar ) {
$title = isset( $address['name'] ) ? sanitize_text_field( $address['name'] ) : '';
$street = isset( $address['street'] ) ? sanitize_text_field( $address['street'] ) : '';
$city = isset( $address['city'] ) ? sanitize_text_field( $address['city'] ) : '';
$state = isset( $address['state'] ) ? sanitize_text_field( $address['state'] ) : '';
$zip = isset( $address['zip'] ) ? sanitize_text_field( $address['zip'] ) : '';

$full_address = implode( ' ', array_filter( array( $street, $city, $state, $zip ) ) );

$widget_options = array(
'title' => $title,
'address' => $full_address,
'phone' => '',
'hours' => '',
'showmap' => false,
'email' => ''
);

$widget_updated = '';
if ( ! self::has_business_address_widget( $first_sidebar ) ) {
$widget_updated = Jetpack_Widgets::insert_widget_in_sidebar( 'widget_contact_info', $widget_options, $first_sidebar );
} else {
$widget_updated = Jetpack_Widgets::update_widget_in_sidebar( 'widget_contact_info', $widget_options, $first_sidebar );
}
if ( is_wp_error( $widget_updated ) ) {
return new WP_Error( 'widget_update_failed', 'Widget could not be updated.', 400 );
}

$address_save = array(
'name' => $title,
'street' => $street,
'city' => $city,
'state' => $state,
'zip' => $zip
);
update_option( 'jpo_business_address', $address_save );
return true;
}

// No sidebar to place the widget
return new WP_Error( 'sidebar_not_found', 'No sidebar.', 400 );
}

/**
* Check whether "Contact Info & Map" widget is present in a given sidebar.
*
* @param string $sidebar ID of the sidebar to which the widget will be added.
*
* @return bool Whether the widget is present in a given sidebar.
*/
static function has_business_address_widget( $sidebar ) {
$sidebars_widgets = get_option( 'sidebars_widgets', array() );
if ( ! isset( $sidebars_widgets[ $sidebar ] ) ) {
return false;
}
foreach ( $sidebars_widgets[ $sidebar ] as $widget ) {
if ( strpos( $widget, 'widget_contact_info' ) !== false ) {
return true;
}
}
return false;
}

/**
* Calls WPCOM through authenticated request to create, regenerate or delete the Post by Email address.
* @todo: When all settings are updated to use endpoints, move this to the Post by Email module and replace __process_ajax_proxy_request.
Expand Down
92 changes: 92 additions & 0 deletions _inc/lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,96 @@ public static function get_registered_widget_object( $id_base ) {
public static function validate_id_base( $id_base ) {
return ( false !== self::get_registered_widget_object( $id_base ) );
}

/**
* Insert a new widget in a given sidebar.
*
* @param string $widget_id ID of the widget.
* @param array $widget_options Content of the widget.
* @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.
*/
static function insert_widget_in_sidebar( $widget_id, $widget_options, $sidebar ) {
// Retrieve sidebars, widgets and their instances
$sidebars_widgets = get_option( 'sidebars_widgets', array() );
$widget_instances = get_option( 'widget_' . $widget_id, array() );

// Retrieve the key of the next widget instance
$numeric_keys = array_filter( array_keys( $widget_instances ), 'is_int' );
$next_key = $numeric_keys ? max( $numeric_keys ) + 1 : 2;

// Add this widget to the sidebar
if ( ! isset( $sidebars_widgets[ $sidebar ] ) ) {
$sidebars_widgets[ $sidebar ] = array();
}
$sidebars_widgets[ $sidebar ][] = $widget_id . '-' . $next_key;

// Add the new widget instance
$widget_instances[ $next_key ] = $widget_options;

// Store updated sidebars, widgets and their instances
if (
! ( update_option( 'sidebars_widgets', $sidebars_widgets ) )
|| ( ! ( update_option( 'widget_' . $widget_id, $widget_instances ) ) )
) {
return new WP_Error( 'widget_update_failed', 'Failed to update widget or sidebar.', 400 );
};

return true;
}

/**
* Update the content of an existing widget in a given sidebar.
*
* @param string $widget_id ID of the widget.
* @param array $widget_options New content for the update.
* @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.
*/
static function update_widget_in_sidebar( $widget_id, $widget_options, $sidebar ) {
// Retrieve sidebars, widgets and their instances
$sidebars_widgets = get_option( 'sidebars_widgets', array() );
$widget_instances = get_option( 'widget_' . $widget_id, array() );

// Retrieve index of first widget instance in that sidebar
$widget_key = false;
foreach ( $sidebars_widgets[ $sidebar ] as $widget ) {
if ( strpos( $widget, $widget_id ) !== false ) {
$widget_key = absint( str_replace( $widget_id . '-', '', $widget ) );
break;
}
}

// There is no widget instance
if ( ! $widget_key ) {
return new WP_Error( 'invalid_data', 'No such widget.', 400 );
}

// Update the widget instance with the new data
$widget_instances[ $widget_key ] = array_merge( $widget_instances[ $widget_key ], $widget_options );

// Store updated widget instances and return Error when not successful
if ( ! ( update_option( 'widget_' . $widget_id, $widget_instances ) ) ) {
return new WP_Error( 'widget_update_failed', 'Failed to update widget.', 400 );
};
return true;
}

/**
* Retrieve the first active sidebar.
*
* @return string|WP_Error First active sidebar, error if none exists.
*/
static function get_first_sidebar() {
$active_sidebars = get_option( 'sidebars_widgets', array() );
unset( $active_sidebars[ 'wp_inactive_widgets' ], $active_sidebars[ 'array_version' ] );

if ( empty( $active_sidebars ) ) {
return false;
}
$active_sidebars_keys = array_keys( $active_sidebars );
return array_shift( $active_sidebars_keys );
}
}
1 change: 1 addition & 0 deletions class.jetpack-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ static function get_all_wp_options() {
'jetpack_sso_require_two_step',
'jetpack_sso_remove_login_form',
'jetpack_last_connect_url_check',
'jpo_business_address',
'jpo_site_type',
'jpo_homepage_format',
'jpo_contact_page',
Expand Down