Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class WordpressCore : MonoBehaviour
- {
- //****************************************************************************************************************
- // +=================+==========================+
- // | Project: | Wordpress C# API v0.1 |
- // +=================+==========================+
- // | Autor: | Valentin Winkelmann |
- // +-----------------+--------------------------+
- // | Special Thanks: | Max Gerhardt |
- // +-----------------+--------------------------+
- // | The Project is | still under construction |
- // +-----------------+--------------------------+
- // 2016 (C) https://valentinwinkelmann.com/
- //
- // +===========================+
- // | Todo |
- // +===========================+
- // | Inspector Integration |
- // +---------------------------+
- // | Facebook Login |
- // +---------------------------+
- // | PushNotification for Chat |
- // +---------------------------+
- //
- //****************************************************************************************************************
- // +--------------------+
- // | JSON Serialization |
- // +--------------------+
- [System.Serializable]
- public class get_nonce_structure
- {
- public string status;
- public string controller;
- public string method;
- public string nonce;
- public string error;
- }
- [System.Serializable]
- public class email_exists_structure
- {
- public string status;
- public string msg;
- public string error;
- }
- [System.Serializable]
- public class username_exists_structure
- {
- public string status;
- public string msg;
- public string error;
- }
- [System.Serializable]
- public class register_structure
- {
- public string status;
- public string cookie;
- public int user_id;
- public string error;
- }
- [System.Serializable]
- public class get_avatar_structure
- {
- public string status;
- public string avatar;
- public string error;
- }
- [System.Serializable]
- public class get_userinfo_structure
- {
- public string status;
- public int id;
- public string nicename;
- public string url;
- public string displayname;
- public string firstname;
- public string lastname;
- public string nickname;
- public string avatar;
- public string error;
- }
- // TODO: Implement retrieve_password method, dont know what user_login:var is o.O ?!?!
- public class validate_auth_cookie_structure
- {
- public string status;
- public bool valid;
- public string error;
- }
- [System.Serializable]
- public class generate_auth_cookie_structure
- {
- public string status;
- public string error;
- public string cookie;
- public User user;
- }
- [System.Serializable]
- public class User // General user Class, used by many roots
- {
- public int id;
- public string username;
- public string nicename;
- public string email;
- public string url;
- public string registered;
- public string displayname;
- public string firstname;
- public string lastname;
- public string nickname;
- public string description;
- public string capabilities;
- public string avatar;
- }
- [System.Serializable]
- public class get_currentuserinfo_structure
- {
- public string status;
- public string error;
- public User user;
- }
- [System.Serializable]
- public class get_user_meta_structure
- {
- public string status;
- public string error;
- public string nickname;
- public string first_name;
- public string last_name;
- public string description;
- public string rich_editing;
- public string comment_shortcuts;
- public string admin_color;
- public string show_admin_bar_front;
- public string gqVG38Tc_capabilities;
- public string session_tokens;
- public string bp_xprofile_visibility_levels;
- }
- [System.Serializable]
- public class update_user_meta_structure
- {
- public string status;
- public bool updated;
- public string error;
- }
- [System.Serializable]
- public class delete_user_meta_structure
- {
- public string status;
- public bool deleted;
- public string error;
- }
- [System.Serializable]
- public class xprofile_structure
- {
- public string status;
- public string error;
- //you have to fill here all xProfile fields like here:
- //public string Name_of_a_testfield;
- }
- //XProfileUpdate... This something special....
- //For every xProfile field you need here a public class (xProfile_struct_***) and you have to set they in the xprofile_update_structure
- [System.Serializable]
- public class xProfile_struct_Testfeld
- {
- public bool updated;
- }
- //[System.Serializable]
- //........public class xProfile_struct_FooooBaaaaaa
- // {
- // public bool updated;
- // }
- [System.Serializable]
- public class xprofile_update_structure
- {
- public string status;
- public string error;
- public xProfile_struct_Testfeld Testfeld;
- //public xProfile_struct_FooooBaaaaaa FooBaaField
- }
- //Not realy hard to understand, right?
- //End xProfileUpdate
- //Todo: Implement FBConect.. Mark Zuckerberg sould fuck him self...
- //Note: PI Media is contacted to get closer informations
- [System.Serializable]
- public class post_comment_structure
- {
- public string status;
- public string error;
- public int comment_id;
- }
- //ProfileStructure, Something Special again...
- //Every xProfile group from buddypress had to addet here and every group gets a class x_**GROUPNAME** where you put your grup fields on
- // perhaps we can make this simpler ?
- public class x_Base
- {
- public string Name;
- public string Testfeld;
- public string Auswahl;
- }
- public class x_AnotherGroup
- {
- public string Radio;
- }
- public class x_ProfileGroups
- {
- public x_Base Base;
- public x_AnotherGroup AnotherGroup;
- }
- public class profile_structure
- {
- public string status;
- public string error;
- public x_ProfileGroups profileGroups;
- }
- //ProfileStructure ends here.
- //Friendlist
- public class friendlist_Friend
- {
- public string user_id;
- public string username;
- public string display_name;
- public string avatar;
- }
- public class friends_structure
- {
- public string status;
- public string error;
- public List<friendlist_Friend> friends;
- public int count;
- }
- //MessageList: threads
- // lists all messages
- public class threads_Message
- {
- public string id;
- public string thread_id;
- public string sender_id;
- public string subject;
- public string message;
- public string date_sent;
- }
- public class threads_Recipient
- {
- public string id;
- public string user_id;
- public string thread_id;
- public string unread_count;
- public string sender_only;
- public string is_deleted;
- }
- public class threads_Thread
- {
- public int thread_id;
- public List<threads_Message> messages;
- public List<threads_Recipient> recipients;
- public List<string> sender_ids;
- }
- public class threads_structure
- {
- public string status;
- public string error;
- public List<threads_Thread> threads;
- public int total_pages;
- public int current_page;
- public int total_threads;
- }
- //MessageList-End
- //Message: thread ; be carefull, this isn't threads.
- //List only one message thread between two or more users
- public class thread_ThreadMessage
- {
- public int message_id;
- public int sender_id;
- public string time_since;
- public string date_sent;
- public string message;
- }
- public class thread_ThreadUser
- {
- public int user_id;
- public string avatar;
- }
- public class thread_structure
- {
- public string status;
- public string error;
- public string thread_subject;
- public List<thread_ThreadMessage> thread_messages;
- public List<thread_ThreadUser> thread_users;
- }
- //Message: thread-End
- //new_message
- public class new_message_structure
- {
- public string status;
- public string error;
- public int sender_id;
- public List<string> recipient_id;
- public int message_thread_id;
- }
- //new_message -End
- // +====================================+
- // | Wordpress API Class Decleration |
- // +====================================+
- // | !!! DO NOT TOUCH ANYTHING HERE !!! |
- // +------------------------------------+
- public class wordpress_api : MonoBehaviour
- {
- //Privates
- private string url;
- private bool premium;
- private string api_key;
- public delegate void ResponseHandler(string responseContent);
- IEnumerator AskServerSomething(string request_url, ResponseHandler onSuccess, ResponseHandler onFail)
- {
- //Debug.Log("AskServerSomething wird ausgeführt");
- WWW www = new WWW(url + request_url);
- //Debug.Log("AskServerSomething wurde ausgeführt");
- yield return www;
- //Debug.Log("Yield return fertig");
- if (www.error == null)
- {
- onSuccess(www.text); //Hier soll der string an den erfolgsevent gegeben werden
- }
- else
- {
- onFail(www.text); //Hier soll der string an den fail event gegeben werden
- }
- }
- public enum Controller
- {
- Core,
- Posts,
- Respond,
- Widgets,
- UserPlus
- }
- public enum Method
- {
- __construct,
- email_exists,
- username_exists,
- register,
- get_avatar,
- get_userinfo,
- retrieve_password,
- validate_auth_cookie,
- generate_auth_cookie,
- get_currentuserinfo,
- get_user_meta,
- update_user_meta,
- delete_user_meta,
- xprofile,
- xprofile_update,
- fb_connect,
- post_comment,
- profile,
- friends,
- threads,
- thread,
- new_message,
- mark_thread_read,
- mark_thread_unread,
- messages_count,
- avatar_upload,
- delete_account,
- get_sidebar,
- submit_comment,
- create_post,
- update_post,
- delete_post,
- info,
- get_recent_posts,
- get_posts,
- get_post,
- get_page,
- get_date_posts,
- get_category_posts,
- get_tag_posts,
- get_author_posts,
- get_search_results,
- get_date_index,
- get_category_index,
- get_tag_index,
- get_author_index,
- get_page_index,
- get_nonce
- }
- // +---------------+
- // | API Functions |
- // +---------------+
- /// <summary>
- /// Initializes the Wordpress API
- /// you get the API Server side plugin from:
- /// https://de.wordpress.org/plugins/json-api-user/
- /// </summary>
- /// <param name="set_url">example: https://localhost/api/ the "/" at the end of the Url is important! https is is strongly recommended!</param>
- /// <param name="set_api_key">You get this API key here: WordpressDashboard->Settings->UserPlus</param>
- /// <param name="set_premium">if you use the Premium version of the Plugin set this to true</param>
- public void Initialize(string set_url, string set_api_key, bool set_premium)
- {
- url = set_url;
- premium = set_premium;
- api_key = set_api_key;
- }
- // GetNonce
- /// <summary>
- /// GetNonce generates a Nonce(Number used once) this nonce is needed by some functions.
- /// </summary>
- /// <param name="SetController">The controler: WordpressCore.wordpress_api.Controller.***</param>
- /// <param name="SetMethod">The Method: WordpressCore.wordpress_api.Method.***</param>
- public void GetNonce(Controller SetController, Method SetMethod, ResponseHandler onSucces, ResponseHandler onFail)
- {
- //Debug.Log("Ask server for nonce");
- StartCoroutine(AskServerSomething("get_nonce/?controller=" + SetController.ToString() + "&method=" + SetMethod.ToString(), onSucces, onFail));
- }
- /// <summary>
- /// generate_auth_cookie generates a authentification cookie for a wordpress user.
- /// </summary>
- /// <param name="username">The Username for the login</param>
- /// <param name="password">The Password for the login</param>
- /// <param name="nonce">The Generated nonce for (UserPlus/User) and generate_auth_cookie</param>
- /// <param name="onSucces">The event that you want to call when the login was succesfully | needs a string content parameter </param>
- /// <param name="onFail">The event that you want to call when the login was unsuccesfully | needs a string content parameter</param>
- public void generate_auth_cookie(string username, string password, string nonce, ResponseHandler onSucces, ResponseHandler onFail)
- {
- StartCoroutine(AskServerSomething("userplus/generate_auth_cookie/?key=" + api_key + "&nonce=" + nonce + "&username=" + username + "&password=" + password, onSucces, onFail));
- }
- /*
- private void GetNonce_Succes(string content)
- {
- //Debug.Log(content);
- var myObject = JsonUtility.FromJson<get_nonce_structure>(content);
- //Debug.Log("Status: " + myObject.status + " Nonce: " + myObject.nonce);
- }
- private void GetNonce_Fail(string content)
- {
- Debug.Log("Something goes wrong");
- }
- // GetNonce - End
- */
- //TEST IF MAKES COMPILE ERRORS; DELETE THIS SECTION
- /// <summary>
- /// The Nonce_Object contains the json parsed nonce data
- /// </summary>
- public class Nonce_Object
- {
- private string Nonce_string;
- private string Status_string;
- private string Error_string;
- /// <summary>
- /// Parse_Json converts the json string in this classes.
- /// </summary>
- /// <param name="content">the main json string you get from api.</param>
- public void parse_json(string content)
- {
- var myObject = JsonUtility.FromJson<get_nonce_structure>(content);
- Nonce_string = myObject.nonce;
- Status_string = myObject.status;
- Error_string = myObject.error;
- }
- public string nonce()
- {
- return Nonce_string;
- }
- public string status()
- {
- return Status_string;
- }
- public string error()
- {
- return Error_string;
- }
- }
- /// <summary>
- /// The generate_auth_cookie_Object contains the json parsed user date.
- /// </summary>
- public class generate_auth_cookie_Object
- {
- private int id_int;
- private string username_string;
- private string nicename_string;
- private string email_string;
- private string url_string;
- private string registered_string;
- private string displayname_string;
- private string firstname_string;
- private string lastname_string;
- private string nickname_string;
- private string description_string;
- private string capabilities_string;
- private string avatar_string;
- private string status_string;
- private string error_string;
- private string cookie_string;
- public void parse_json(string content)
- {
- var myObject = JsonUtility.FromJson<generate_auth_cookie_structure>(content);
- id_int = myObject.user.id;
- username_string = myObject.user.username;
- nicename_string = myObject.user.nicename;
- email_string = myObject.user.email;
- url_string = myObject.user.url;
- registered_string = myObject.user.registered;
- displayname_string = myObject.user.displayname;
- firstname_string = myObject.user.firstname;
- lastname_string = myObject.user.lastname;
- nickname_string = myObject.user.nicename;
- description_string = myObject.user.description;
- capabilities_string = myObject.user.capabilities;
- avatar_string = myObject.user.avatar;
- status_string = myObject.status;
- cookie_string = myObject.cookie;
- error_string = myObject.error;
- }
- public int id()
- {
- return id_int;
- }
- public string username()
- {
- return username_string;
- }
- public string nicename()
- {
- return nicename_string;
- }
- public string email()
- {
- return email_string;
- }
- public string url()
- {
- return url_string;
- }
- public string registered()
- {
- return registered_string;
- }
- public string displayname()
- {
- return displayname_string;
- }
- public string firstname()
- {
- return firstname_string;
- }
- public string lastname()
- {
- return lastname_string;
- }
- public string nickname()
- {
- return nickname_string;
- }
- public string description()
- {
- return description_string;
- }
- public string capabilities()
- {
- return capabilities_string;
- }
- public string avatar()
- {
- return avatar_string;
- }
- public string status()
- {
- return status_string;
- }
- public string cookie()
- {
- return cookie_string;
- }
- public string error()
- {
- return error_string;
- }
- }
- //TEST ENDS HERE
- // email_exists
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement