set-credentials.js
217 lines
| 1 | /** |
| 2 | * Credentials |
| 3 | * |
| 4 | * @package ShareThisFollowButtons |
| 5 | */ |
| 6 | |
| 7 | /* exported Credentials */ |
| 8 | var Credentials = ( function( $, wp ) { |
| 9 | 'use strict'; |
| 10 | |
| 11 | return { |
| 12 | /** |
| 13 | * Holds data. |
| 14 | */ |
| 15 | data: {}, |
| 16 | |
| 17 | /** |
| 18 | * Boot plugin. |
| 19 | * |
| 20 | * @param data |
| 21 | */ |
| 22 | boot: function( data ) { |
| 23 | this.data = data; |
| 24 | |
| 25 | $( document ).ready( |
| 26 | function() { |
| 27 | this.init(); |
| 28 | }.bind( this ) |
| 29 | ); |
| 30 | }, |
| 31 | |
| 32 | /** |
| 33 | * Initialize plugin. |
| 34 | */ |
| 35 | init: function() { |
| 36 | const self = this; |
| 37 | |
| 38 | // Set default WP config. |
| 39 | wp.ajax.post( |
| 40 | 'set_follow_default_settings', |
| 41 | { |
| 42 | type: 'both', |
| 43 | nonce: self.data.nonce |
| 44 | } |
| 45 | ).always( |
| 46 | function( link ) { |
| 47 | self.registerAccount( self.data.email, Math.random() + '1_stsb_PW!' ); |
| 48 | }.bind( self ) |
| 49 | ); |
| 50 | }, |
| 51 | |
| 52 | /** |
| 53 | * Send hash data to credential setting. |
| 54 | * |
| 55 | * @param secret |
| 56 | * @param propertyid |
| 57 | * @param token |
| 58 | * @param type |
| 59 | */ |
| 60 | |
| 61 | setCredentials: function( secret, propertyid, token ) { |
| 62 | var propSecret = propertyid + '-' + secret; |
| 63 | const self = this; |
| 64 | |
| 65 | // If hash exists send it to credential setting. |
| 66 | wp.ajax.post( |
| 67 | 'set_follow_credentials', |
| 68 | { |
| 69 | data: propSecret, |
| 70 | token: token, |
| 71 | nonce: this.data.nonce |
| 72 | } |
| 73 | ).always( |
| 74 | function( link ) { |
| 75 | // Set default product configs. |
| 76 | const config = { |
| 77 | action: 'Follow us:', |
| 78 | color: 'social', |
| 79 | enabled: false, |
| 80 | action_enable: true, |
| 81 | alignment: 'center', |
| 82 | networks: ['facebook', 'twitter', 'instagram', 'pinterest'], |
| 83 | size: 32, |
| 84 | spacing: 8, |
| 85 | radius: 4, |
| 86 | padding: 8, |
| 87 | action_pos: 'top', |
| 88 | profiles: [], |
| 89 | fade_in: false |
| 90 | }; |
| 91 | |
| 92 | let theData = { |
| 93 | 'token' : token, |
| 94 | 'id': propertyid, |
| 95 | 'product': 'inline-follow-buttons', |
| 96 | 'config': config |
| 97 | }; |
| 98 | |
| 99 | theData = JSON.stringify( theData ); |
| 100 | |
| 101 | // Send new button status value. |
| 102 | $.ajax( |
| 103 | { |
| 104 | url: 'https://platform-api.sharethis.com/v1.0/property/product', |
| 105 | method: 'POST', |
| 106 | async: false, |
| 107 | contentType: 'application/json; charset=utf-8', |
| 108 | data: theData, |
| 109 | success: function () { |
| 110 | wp.ajax.post( |
| 111 | 'set_follow_button_config', |
| 112 | { |
| 113 | button: 'inline-follow', |
| 114 | config: config, |
| 115 | first: true, |
| 116 | nonce: self.data.nonce |
| 117 | } |
| 118 | ) |
| 119 | } |
| 120 | } |
| 121 | ); |
| 122 | }.bind( this ) |
| 123 | ); |
| 124 | }, |
| 125 | |
| 126 | /** |
| 127 | * Register new account. |
| 128 | * |
| 129 | * @param email |
| 130 | * @param pw |
| 131 | */ |
| 132 | registerAccount: function( email, pw ) { |
| 133 | var result = null, |
| 134 | self = this, |
| 135 | url = this.data.url, |
| 136 | randomNumber = Math.floor( |
| 137 | ( |
| 138 | Math.random() * 10000000000000000 |
| 139 | ) + 1 |
| 140 | ), |
| 141 | randomNumber2 = Math.floor( |
| 142 | ( |
| 143 | Math.random() * 10000000000000000 |
| 144 | ) + 1 |
| 145 | ), |
| 146 | theData = JSON.stringify( |
| 147 | { |
| 148 | email: randomNumber + '@' + randomNumber2 + '.com', |
| 149 | password: pw, |
| 150 | custom: { |
| 151 | onboarding_product: 'inline-follow-buttons', |
| 152 | onboarding_domain: url, |
| 153 | is_wordpress: true, |
| 154 | wordpress_email: email, |
| 155 | } |
| 156 | } |
| 157 | ); |
| 158 | |
| 159 | $.ajax( |
| 160 | { |
| 161 | url: 'https://sso.sharethis.com/register', |
| 162 | method: 'POST', |
| 163 | async: false, |
| 164 | contentType: 'application/json; charset=utf-8', |
| 165 | data: theData, |
| 166 | success: function( results ) { |
| 167 | result = results; |
| 168 | |
| 169 | // Create property. |
| 170 | self.createProperty( result, url, '' ); |
| 171 | }, |
| 172 | } |
| 173 | ); |
| 174 | }, |
| 175 | |
| 176 | /** |
| 177 | * Create property for new account. |
| 178 | * |
| 179 | * @param accountInfo |
| 180 | * @param url |
| 181 | */ |
| 182 | createProperty: function( accountInfo, url, type ) { |
| 183 | var result = null, |
| 184 | self = this, |
| 185 | token = accountInfo.token, |
| 186 | theData; |
| 187 | |
| 188 | if ( 'string' === typeof accountInfo ) { |
| 189 | token = accountInfo; |
| 190 | } |
| 191 | |
| 192 | theData = JSON.stringify( |
| 193 | { |
| 194 | token: token, |
| 195 | product: 'inline-follow-buttons', |
| 196 | domain: url, |
| 197 | is_wordpress: true |
| 198 | } |
| 199 | ); |
| 200 | |
| 201 | $.ajax( |
| 202 | { |
| 203 | url: 'https://platform-api.sharethis.com/v1.0/property', |
| 204 | method: 'POST', |
| 205 | async: false, |
| 206 | contentType: 'application/json; charset=utf-8', |
| 207 | data: theData, |
| 208 | success: function( results ) { |
| 209 | result = results; |
| 210 | self.setCredentials( result.secret, result._id, token, type ); |
| 211 | } |
| 212 | } |
| 213 | ); |
| 214 | } |
| 215 | }; |
| 216 | } )( window.jQuery, window.wp ); |
| 217 |