autoship_oauth2_connect_site (function)

(src/admin.php) Used to create a connected site in QPilot using the current site info, and generated API Keys.

Function

/**
 * Creates a Site in QPilot using the current blog info, and generated API Keys.
 *
 * @param int $user_id  Optional. The WP User's id.
 * @return stdClass The created site object.
 */
function autoship_oauth2_connect_site( $user_id = NULL ) {

  if ( !isset( $user_id ) || !$user_id )
  $user_id = get_current_user_id();

  // Create API keys
  $api_keys = autoship_oauth2_create_wc_api_keys( $user_id );

  $client = new QPilotClient();

  $site = NULL;

  try {

    // Create site
    $site = $client->create_site( $api_keys['consumer_key'], $api_keys['consumer_secret'] );

    update_option( 'autoship_site_id', $site->id );
    do_action( 'autoship_api_site_created', $site );

  } catch ( Exception $e ) {

    // If an error occurs here is's due to issues POSTing to QPilot.
    autoship_notice_handler( 'qpilot_post_denied', $e->getMessage() );
    autoship_log_entry( __( 'Autoship Oauth Exception', 'autoship' ), sprintf( 'An %s Exception Occurred when attempting to update the Site Connection in QPilot. Additional Details: %s', $e->getCode(), $e->getMessage() ) );

  }

  return $site;
}