Advertisement
diegographics

Untitled

Mar 16th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.03 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class WordpressCore : MonoBehaviour
  6. {
  7.     //****************************************************************************************************************
  8.     //      +=================+==========================+
  9.     //      |    Project:     |  Wordpress C# API v0.1   |
  10.     //      +=================+==========================+
  11.     //      | Autor:          | Valentin Winkelmann      |
  12.     //      +-----------------+--------------------------+
  13.     //      | Special Thanks: | Max Gerhardt             |
  14.     //      +-----------------+--------------------------+
  15.     //      | The Project is  | still under construction |
  16.     //      +-----------------+--------------------------+
  17.     //         2016 (C) https://valentinwinkelmann.com/
  18.     //
  19.     //              +===========================+
  20.     //              |           Todo            |
  21.     //              +===========================+
  22.     //              | Inspector Integration     |
  23.     //              +---------------------------+
  24.     //              | Facebook Login            |
  25.     //              +---------------------------+
  26.     //              | PushNotification for Chat |
  27.     //              +---------------------------+
  28.     //
  29.     //****************************************************************************************************************
  30.  
  31.  
  32.  
  33.     //      +--------------------+
  34.     //      | JSON Serialization |
  35.     //      +--------------------+
  36.  
  37.     [System.Serializable]
  38.  
  39.     public class get_nonce_structure
  40.     {
  41.         public string status;
  42.         public string controller;
  43.         public string method;
  44.         public string nonce;
  45.         public string error;
  46.     }
  47.  
  48.     [System.Serializable]
  49.     public class email_exists_structure
  50.     {
  51.         public string status;
  52.         public string msg;
  53.         public string error;
  54.     }
  55.  
  56.     [System.Serializable]
  57.     public class username_exists_structure
  58.     {
  59.         public string status;
  60.         public string msg;
  61.         public string error;
  62.     }
  63.  
  64.     [System.Serializable]
  65.     public class register_structure
  66.     {
  67.         public string status;
  68.         public string cookie;
  69.         public int user_id;
  70.         public string error;
  71.     }
  72.  
  73.     [System.Serializable]
  74.     public class get_avatar_structure
  75.     {
  76.         public string status;
  77.         public string avatar;
  78.         public string error;
  79.     }
  80.  
  81.     [System.Serializable]
  82.     public class get_userinfo_structure
  83.     {
  84.         public string status;
  85.         public int id;
  86.         public string nicename;
  87.         public string url;
  88.         public string displayname;
  89.         public string firstname;
  90.         public string lastname;
  91.         public string nickname;
  92.         public string avatar;
  93.         public string error;
  94.     }
  95.  
  96.     // TODO: Implement retrieve_password method, dont know what user_login:var is o.O ?!?!
  97.  
  98.    
  99.     public class validate_auth_cookie_structure
  100.     {
  101.         public string status;
  102.         public bool valid;
  103.         public string error;
  104.     }
  105.  
  106.     [System.Serializable]
  107.     public class generate_auth_cookie_structure
  108.     {
  109.         public string status;
  110.         public string error;
  111.         public string cookie;
  112.         public User user;
  113.     }
  114.     [System.Serializable]
  115.     public class User // General user Class, used by many roots
  116.     {
  117.         public int id;
  118.         public string username;
  119.         public string nicename;
  120.         public string email;
  121.         public string url;
  122.         public string registered;
  123.         public string displayname;
  124.         public string firstname;
  125.         public string lastname;
  126.         public string nickname;
  127.         public string description;
  128.         public string capabilities;
  129.         public string avatar;
  130.     }
  131.     [System.Serializable]
  132.     public class get_currentuserinfo_structure
  133.     {
  134.         public string status;
  135.         public string error;
  136.         public User user;
  137.     }
  138.     [System.Serializable]
  139.     public class get_user_meta_structure
  140.     {
  141.         public string status;
  142.         public string error;
  143.         public string nickname;
  144.         public string first_name;
  145.         public string last_name;
  146.         public string description;
  147.         public string rich_editing;
  148.         public string comment_shortcuts;
  149.         public string admin_color;
  150.         public string show_admin_bar_front;
  151.         public string gqVG38Tc_capabilities;
  152.         public string session_tokens;
  153.         public string bp_xprofile_visibility_levels;
  154.     }
  155.     [System.Serializable]
  156.     public class update_user_meta_structure
  157.     {
  158.         public string status;
  159.         public bool updated;
  160.         public string error;
  161.     }
  162.     [System.Serializable]
  163.     public class delete_user_meta_structure
  164.     {
  165.         public string status;
  166.         public bool deleted;
  167.         public string error;
  168.     }
  169.     [System.Serializable]
  170.     public class xprofile_structure
  171.     {
  172.         public string status;
  173.         public string error;
  174.         //you have to fill here all xProfile fields like here:
  175.         //public string Name_of_a_testfield;
  176.     }
  177.  
  178.  
  179.  
  180.     //XProfileUpdate... This something special....
  181.     //For every xProfile field you need here a public class (xProfile_struct_***) and you have to set they in the xprofile_update_structure
  182.     [System.Serializable]
  183.     public class xProfile_struct_Testfeld
  184.     {
  185.         public bool updated;
  186.     }
  187.     //[System.Serializable]
  188.     //........public class xProfile_struct_FooooBaaaaaa
  189.     //  {
  190.     //      public bool updated;
  191.     //  }
  192.     [System.Serializable]
  193.     public class xprofile_update_structure
  194.     {
  195.         public string status;
  196.         public string error;
  197.         public xProfile_struct_Testfeld Testfeld;
  198.         //public xProfile_struct_FooooBaaaaaa FooBaaField
  199.     }
  200.     //Not realy hard to understand, right?
  201.     //End xProfileUpdate
  202.  
  203.  
  204.     //Todo: Implement FBConect.. Mark Zuckerberg sould fuck him self...
  205.     //Note: PI Media is contacted to get closer informations
  206.  
  207.     [System.Serializable]
  208.     public class post_comment_structure
  209.     {
  210.         public string status;
  211.         public string error;
  212.         public int comment_id;
  213.     }
  214.  
  215.  
  216.     //ProfileStructure, Something Special again...
  217.     //Every xProfile group from buddypress had to addet here and every group gets a class x_**GROUPNAME** where you put your grup fields on
  218.     // perhaps we can make this simpler ?
  219.     public class x_Base
  220.     {
  221.         public string Name;
  222.         public string Testfeld;
  223.         public string Auswahl;
  224.     }
  225.  
  226.     public class x_AnotherGroup
  227.     {
  228.         public string Radio;
  229.     }
  230.  
  231.     public class x_ProfileGroups
  232.     {
  233.         public x_Base Base;
  234.         public x_AnotherGroup AnotherGroup;
  235.     }
  236.  
  237.     public class profile_structure
  238.     {
  239.         public string status;
  240.         public string error;
  241.         public x_ProfileGroups profileGroups;
  242.     }
  243.     //ProfileStructure ends here.
  244.  
  245.     //Friendlist
  246.  
  247.  
  248.     public class friendlist_Friend
  249.     {
  250.         public string user_id;
  251.         public string username;
  252.         public string display_name;
  253.         public string avatar;
  254.     }
  255.  
  256.     public class friends_structure
  257.     {
  258.         public string status;
  259.         public string error;
  260.         public List<friendlist_Friend> friends;
  261.         public int count;
  262.     }
  263.  
  264.     //MessageList: threads
  265.     // lists all messages
  266.     public class threads_Message
  267.     {
  268.         public string id;
  269.         public string thread_id;
  270.         public string sender_id;
  271.         public string subject;
  272.         public string message;
  273.         public string date_sent;
  274.     }
  275.  
  276.     public class threads_Recipient
  277.     {
  278.         public string id;
  279.         public string user_id;
  280.         public string thread_id;
  281.         public string unread_count;
  282.         public string sender_only;
  283.         public string is_deleted;
  284.     }
  285.  
  286.     public class threads_Thread
  287.     {
  288.         public int thread_id;
  289.         public List<threads_Message> messages;
  290.         public List<threads_Recipient> recipients;
  291.         public List<string> sender_ids;
  292.     }
  293.  
  294.     public class threads_structure
  295.     {
  296.         public string status;
  297.         public string error;
  298.         public List<threads_Thread> threads;
  299.         public int total_pages;
  300.         public int current_page;
  301.         public int total_threads;
  302.     }
  303.     //MessageList-End
  304.  
  305.     //Message: thread    ; be carefull, this isn't threads.
  306.     //List only one message thread between two or more users
  307.  
  308.     public class thread_ThreadMessage
  309.     {
  310.         public int message_id;
  311.         public int sender_id;
  312.         public string time_since;
  313.         public string date_sent;
  314.         public string message;
  315.     }
  316.  
  317.     public class thread_ThreadUser
  318.     {
  319.         public int user_id;
  320.         public string avatar;
  321.     }
  322.  
  323.     public class thread_structure
  324.     {
  325.         public string status;
  326.         public string error;
  327.         public string thread_subject;
  328.         public List<thread_ThreadMessage> thread_messages;
  329.         public List<thread_ThreadUser> thread_users;
  330.     }
  331.  
  332.     //Message: thread-End
  333.  
  334.     //new_message
  335.    
  336.     public class new_message_structure
  337.     {
  338.         public string status;
  339.         public string error;
  340.         public int sender_id;
  341.         public List<string> recipient_id;
  342.         public int message_thread_id;
  343.     }
  344.     //new_message -End
  345.  
  346.  
  347.     //          +====================================+
  348.     //          |  Wordpress API Class Decleration   |
  349.     //          +====================================+
  350.     //          | !!! DO NOT TOUCH ANYTHING HERE !!! |
  351.     //          +------------------------------------+
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.     public class wordpress_api : MonoBehaviour
  359.     {
  360.  
  361.         //Privates
  362.         private string url;
  363.         private bool premium;
  364.         private string api_key;
  365.  
  366.  
  367.         public delegate void ResponseHandler(string responseContent);
  368.         IEnumerator AskServerSomething(string request_url, ResponseHandler onSuccess, ResponseHandler onFail)
  369.         {
  370.             //Debug.Log("AskServerSomething wird ausgeführt");
  371.             WWW www = new WWW(url + request_url);
  372.             //Debug.Log("AskServerSomething wurde ausgeführt");
  373.             yield return www;
  374.             //Debug.Log("Yield return fertig");
  375.             if (www.error == null)
  376.             {
  377.                
  378.                 onSuccess(www.text); //Hier soll der string an den erfolgsevent gegeben werden
  379.             }
  380.             else
  381.             {
  382.                
  383.                 onFail(www.text); //Hier soll der string an den fail event gegeben werden
  384.             }
  385.         }
  386.  
  387.  
  388.         public enum Controller
  389.         {
  390.             Core,
  391.             Posts,
  392.             Respond,
  393.             Widgets,
  394.             UserPlus
  395.         }
  396.         public enum Method
  397.         {
  398.             __construct,
  399.             email_exists,
  400.             username_exists,
  401.             register,
  402.             get_avatar,
  403.             get_userinfo,
  404.             retrieve_password,
  405.             validate_auth_cookie,
  406.             generate_auth_cookie,
  407.             get_currentuserinfo,
  408.             get_user_meta,
  409.             update_user_meta,
  410.             delete_user_meta,
  411.             xprofile,
  412.             xprofile_update,
  413.             fb_connect,
  414.             post_comment,
  415.             profile,
  416.             friends,
  417.             threads,
  418.             thread,
  419.             new_message,
  420.             mark_thread_read,
  421.             mark_thread_unread,
  422.             messages_count,
  423.             avatar_upload,
  424.             delete_account,
  425.             get_sidebar,
  426.             submit_comment,
  427.             create_post,
  428.             update_post,
  429.             delete_post,
  430.             info,
  431.             get_recent_posts,
  432.             get_posts,
  433.             get_post,
  434.             get_page,
  435.             get_date_posts,
  436.             get_category_posts,
  437.             get_tag_posts,
  438.             get_author_posts,
  439.             get_search_results,
  440.             get_date_index,
  441.             get_category_index,
  442.             get_tag_index,
  443.             get_author_index,
  444.             get_page_index,
  445.             get_nonce
  446.         }
  447.  
  448.         //  +---------------+
  449.         //  | API Functions |
  450.         //  +---------------+
  451.  
  452.         /// <summary>
  453.         /// Initializes the Wordpress API
  454.         /// you get the API Server side plugin from:
  455.         /// https://de.wordpress.org/plugins/json-api-user/
  456.         /// </summary>
  457.         /// <param name="set_url">example: https://localhost/api/ the "/" at the end of the Url is important! https is is strongly recommended!</param>
  458.         /// <param name="set_api_key">You get this API key here: WordpressDashboard->Settings->UserPlus</param>
  459.         /// <param name="set_premium">if you use the Premium version of the Plugin set this to true</param>
  460.         public void Initialize(string set_url, string set_api_key, bool set_premium)
  461.         {
  462.             url = set_url;
  463.             premium = set_premium;
  464.             api_key = set_api_key;
  465.            
  466.         }
  467.         // GetNonce
  468.         /// <summary>
  469.         /// GetNonce generates a Nonce(Number used once) this nonce is needed by some functions.
  470.         /// </summary>
  471.         /// <param name="SetController">The controler: WordpressCore.wordpress_api.Controller.***</param>
  472.         /// <param name="SetMethod">The Method: WordpressCore.wordpress_api.Method.***</param>
  473.         public void GetNonce(Controller SetController, Method SetMethod, ResponseHandler onSucces, ResponseHandler onFail)
  474.         {
  475.             //Debug.Log("Ask server for nonce");
  476.             StartCoroutine(AskServerSomething("get_nonce/?controller=" + SetController.ToString() + "&method=" + SetMethod.ToString(), onSucces, onFail));
  477.         }
  478.         /// <summary>
  479.         /// generate_auth_cookie generates a authentification cookie for a wordpress user.
  480.         /// </summary>
  481.         /// <param name="username">The Username for the login</param>
  482.         /// <param name="password">The Password for the login</param>
  483.         /// <param name="nonce">The Generated nonce for (UserPlus/User) and generate_auth_cookie</param>
  484.         /// <param name="onSucces">The event that you want to call when the login was succesfully | needs a string content parameter </param>
  485.         /// <param name="onFail">The event that you want to call when the login was unsuccesfully | needs a string content parameter</param>
  486.         public void generate_auth_cookie(string username, string password, string nonce, ResponseHandler onSucces, ResponseHandler onFail)
  487.         {
  488.             StartCoroutine(AskServerSomething("userplus/generate_auth_cookie/?key=" + api_key + "&nonce=" + nonce + "&username=" + username + "&password=" + password, onSucces, onFail));
  489.         }
  490.  
  491.  
  492.         /*
  493.         private void GetNonce_Succes(string content)
  494.         {
  495.             //Debug.Log(content);
  496.             var myObject = JsonUtility.FromJson<get_nonce_structure>(content);
  497.             //Debug.Log("Status: " + myObject.status + " Nonce: " + myObject.nonce);
  498.         }
  499.         private void GetNonce_Fail(string content)
  500.         {
  501.             Debug.Log("Something goes wrong");
  502.         }
  503.         // GetNonce - End
  504.         */
  505.         //TEST IF MAKES COMPILE ERRORS; DELETE THIS SECTION
  506.  
  507.         /// <summary>
  508.         /// The Nonce_Object contains the json parsed nonce data
  509.         /// </summary>
  510.         public class Nonce_Object
  511.         {
  512.  
  513.             private string Nonce_string;
  514.             private string Status_string;
  515.             private string Error_string;
  516.             /// <summary>
  517.             /// Parse_Json converts the json string in this classes.
  518.             /// </summary>
  519.             /// <param name="content">the main json string you get from api.</param>
  520.             public void parse_json(string content)
  521.             {
  522.                 var myObject = JsonUtility.FromJson<get_nonce_structure>(content);
  523.                 Nonce_string = myObject.nonce;
  524.                 Status_string = myObject.status;
  525.                 Error_string = myObject.error;
  526.             }
  527.             public string nonce()
  528.             {
  529.                 return Nonce_string;
  530.             }
  531.             public string status()
  532.             {
  533.                 return Status_string;
  534.             }
  535.             public string error()
  536.             {
  537.                 return Error_string;
  538.             }
  539.         }
  540.         /// <summary>
  541.         /// The generate_auth_cookie_Object contains the json parsed user date.
  542.         /// </summary>
  543.         public class generate_auth_cookie_Object
  544.         {
  545.             private int id_int;
  546.             private string username_string;
  547.             private string nicename_string;
  548.             private string email_string;
  549.             private string url_string;
  550.             private string registered_string;
  551.             private string displayname_string;
  552.             private string firstname_string;
  553.             private string lastname_string;
  554.             private string nickname_string;
  555.             private string description_string;
  556.             private string capabilities_string;
  557.             private string avatar_string;
  558.             private string status_string;
  559.             private string error_string;
  560.             private string cookie_string;
  561.  
  562.             public void parse_json(string content)
  563.             {
  564.                 var myObject = JsonUtility.FromJson<generate_auth_cookie_structure>(content);
  565.                 id_int = myObject.user.id;
  566.                 username_string = myObject.user.username;
  567.                 nicename_string = myObject.user.nicename;
  568.                 email_string = myObject.user.email;
  569.                 url_string = myObject.user.url;
  570.                 registered_string = myObject.user.registered;
  571.                 displayname_string = myObject.user.displayname;
  572.                 firstname_string = myObject.user.firstname;
  573.                 lastname_string = myObject.user.lastname;
  574.                 nickname_string = myObject.user.nicename;
  575.                 description_string = myObject.user.description;
  576.                 capabilities_string = myObject.user.capabilities;
  577.                 avatar_string = myObject.user.avatar;
  578.                 status_string = myObject.status;
  579.                 cookie_string = myObject.cookie;
  580.                 error_string = myObject.error;
  581.             }
  582.             public int id()
  583.             {
  584.                 return id_int;
  585.             }
  586.             public string username()
  587.             {
  588.                 return username_string;
  589.             }
  590.             public string nicename()
  591.             {
  592.                 return nicename_string;
  593.             }
  594.             public string email()
  595.             {
  596.                 return email_string;
  597.             }
  598.             public string url()
  599.             {
  600.                 return url_string;
  601.             }
  602.             public string registered()
  603.             {
  604.                 return registered_string;
  605.             }
  606.             public string displayname()
  607.             {
  608.                 return displayname_string;
  609.             }
  610.             public string firstname()
  611.             {
  612.                 return firstname_string;
  613.             }
  614.             public string lastname()
  615.             {
  616.                 return lastname_string;
  617.             }
  618.             public string nickname()
  619.             {
  620.                 return nickname_string;
  621.             }
  622.             public string description()
  623.             {
  624.                 return description_string;
  625.             }
  626.             public string capabilities()
  627.             {
  628.                 return capabilities_string;
  629.             }
  630.             public string avatar()
  631.             {
  632.                 return avatar_string;
  633.             }
  634.             public string status()
  635.             {
  636.                 return status_string;
  637.             }
  638.             public string cookie()
  639.             {
  640.                 return cookie_string;
  641.             }
  642.             public string error()
  643.             {
  644.                 return error_string;
  645.             }
  646.         }
  647.        
  648.  
  649.  
  650.         //TEST ENDS HERE
  651.         // email_exists
  652.     }
  653. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement