wp_json_api 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 87

WordPress and WooCommerce JSON API Dart package for Flutter #

Official WooSignal package

API features: #

WordPress

  • Register/Sign Up API for Users
  • Login (with email or username)
  • Get Users Info
  • Update Users Info
  • Update Users Password

WooCommerce

  • Get Customers Info (Billing and Shipping)
  • Update Customers details

To use this API you must have the WP JSON API Plugin installed first on your WordPress site, you can download it via the WooSignal website.

Getting Started #

In your flutter project add the dependency:

dependencies:
  ...
  wp_json_api: ^0.1.2

Usage example #

Import wp_json_api.dart

import 'package:wp_json_api/wp_json_api.dart';

Example using Wp JSON API #

import 'package:wp_json_api/wp_json_api.dart';
...

void main() {

WPJsonAPI.instance.initWith(baseUrl: "https://mysite.com");

...

Call a method from the request callback #

WPUserLoginResponse wpUserLoginResponse = await WPJsonAPI.instance
          .api((request) => request.wpLogin(
            email: email,
            password: password
          ));

Available API Requests #

WordPress - Get Nonce

  • Used for returning a valid nonce
WPNonceResponse wpNonceResponse = await WPJsonAPI.instance
          .api((request) => request.wpNonce());

WordPress - Verify Nonce

  • Used for verifying register and login request
WPNonceVerifiedResponse wpNonceVerifiedResponse = await WPJsonAPI.instance
          .api((request) => request.wpNonceVerify(
            nonce: nonce
          ));

WordPress - Login with email

  • Used to login a user
WPUserLoginResponse wpUserLoginResponse = await WPJsonAPI.instance
      .api((request) => request.wpLogin(
          email: email,
          password: password,
          authType: WPAuthType.WpEmail
      ));

WordPress - Login with username

  • Used to login a user
WPUserLoginResponse wpUserLoginResponse = await WPJsonAPI.instance
      .api((request) => request.wpLogin(
          username: username,
          password: password,
          authType: WPAuthType.WpUsername
      ));

WordPress - Register

  • Used to register a user
  • The username parameter is required, ensure that this is unique
WPUserRegisterResponse wpUserRegisterResponse = await WPJsonAPI.instance
      .api((request) => request.wpRegister(
          email: email,
          password: password,
          username: username
      ));

WordPress - Get Users Info

  • Used to get a WordPress users info
  • The first parameter is the userToken which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
WPUserInfoResponse wpUserInfoResponse = await WPJsonAPI.instance
        .api((request) => request.wpGetUserInfo(
            userToken
          ));

WordPress - Update Users Info

  • Used to update a WordPress users info
  • The first parameter is the userToken which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
WPUserInfoUpdatedResponse wpUserInfoUpdatedResponse = await WPJsonAPI.instance
        .api((request) => request.wpUpdateUserInfo(
          userToken,
          firstName: firstName,
          lastName: lastName,
          displayName: displayName
      ));

WordPress - Update users password

  • Used to update a users password
  • The first parameter is the userToken which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
WPUserResetPasswordResponse wpUserResetPasswordResponse = await WPJsonAPI.instance
        .api((request) => request.wpResetPassword(
            userToken,
            password: password
        ));

WooCommerce - Get users info in WooCommerce

  • Used to get WooCommerce info for a given user
  • The first parameter is the userToken which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
WCCustomerInfoResponse wcCustomerInfoResponse = await WPJsonAPI.instance
          .api((request) => request.wcCustomerInfo(
          userToken
      ));

WooCommerce - Update users info in WooCommerce

  • Used to update a users WooCommerce details
  • All the parameter are optional so if you wanted to just update the name, you could just add first_name and last_name
  • The first parameter is the userToken which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
WCCustomerUpdatedResponse wcCustomerUpdatedResponse = await WPJsonAPI.instance
        .api((request) => request.wcUpdateCustomerInfo(
            userToken,
            firstName: firstName,
            lastName: lastName,
            displayName: displayName,
            billingFirstName: billingFirstName,
            billingLastName: billingLastName,
            billingCompany: billingCompany,
            billingAddress1: billingAddress1,
            billingAddress2: billingAddress2,
            billingCity: billingCity,
            billingState: billingState,
            billingPostcode: billingPostcode,
            billingCountry: billingCountry,
            billingEmail: billingEmail,
            billingPhone: billingPhone,
            shippingFirstName: shippingFirstName,
            shippingLastName: shippingLastName,
            shippingCompany: shippingCompany,
            shippingAddress1: shippingAddress1,
            shippingAddress2: shippingAddress2,
            shippingCity: shippingCity,
            shippingState: shippingState,
            shippingPostcode: shippingPostcode,
            shippingCountry: shippingCountry,
            shippingEmail: shippingEmail,
            shippingPhone: shippingPhone
        ));

For help getting started with WooSignal, view our online documentation, which offers a more detailed guide.

Usage #

To use this plugin, add wp_json_api as a dependency in your pubspec.yaml file.

Disclaimer: This plugin is not affiliated with or supported by Automattic, Inc. All logos and trademarks are the property of their respective owners.

[0.1.2] - 2020-05-01 #

  • Readme update

[0.1.1] - 2020-04-30 #

  • Readme update

[0.1.0] - 2020-04-30 #

  • pubspec.yaml desc change, updates to networking class and readme

[0.0.1] - 2020-04-06 #

  • Initial Release

example/main.dart

import 'package:flutter/material.dart';
import 'package:wp_json_api/enums/WPAuthType.dart';
import 'package:wp_json_api/models/responses/WPUserInfoResponse.dart';
import 'package:wp_json_api/models/responses/WPUserLoginResponse.dart';
import 'package:wp_json_api/wp_json_api.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'WooSignal Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'WooSignal Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController _tfEmailController;
  TextEditingController _tfPasswordController;

  @override
  void initState() {
    super.initState();

    // INSTALL THE WP JSON API PLUGIN
    // FIRST ON YOUR WORDPRESS STORE
    // LINK https://woosignal.com/plugins/wordpress/wpapp-json-api

    WPJsonAPI.instance.initWith(baseUrl: "http://mysite.com");

    _tfEmailController = TextEditingController();
    _tfPasswordController = TextEditingController();
  }

  _login() async {
    String email = _tfEmailController.text;
    String password = _tfPasswordController.text;

    // LOGIN
    WPUserLoginResponse wpUserLoginResponse =
        await WPJsonAPI.instance.api((request) {
      return request.wpLogin(
          email: email, password: password, authType: WPAuthType.WpEmail);
    });

    if (wpUserLoginResponse != null) {
      print(wpUserLoginResponse.data.userToken);
      print(wpUserLoginResponse.data.userId);

      // GET USER INFO
      WPUserInfoResponse wpUserInfoResponse =
          await WPJsonAPI.instance.api((request) {
        return request.wpGetUserInfo(wpUserLoginResponse.data.userToken);
      });

      if (wpUserInfoResponse != null) {
        print(wpUserInfoResponse.data.firstName);
        print(wpUserInfoResponse.data.lastName);
      } else {
        print("something went wrong");
      }
    } else {
      print("invalid login details");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              controller: _tfEmailController,
              keyboardType: TextInputType.emailAddress,
            ),
            TextField(
              controller: _tfPasswordController,
              keyboardType: TextInputType.text,
              obscureText: true,
            ),
            MaterialButton(
              child: Text("Login"),
              onPressed: _login,
            )
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  wp_json_api: ^0.1.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:wp_json_api/wp_json_api.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
75
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
87
Learn more about scoring.

We analyzed this package on May 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev
  • Flutter: 1.17.0

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
http ^0.12.0+4 0.12.1
Transitive dependencies
charcode 1.1.3
collection 1.14.12
http_parser 3.1.4
meta 1.1.8
path 1.7.0
pedantic 1.9.0
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test