PluginProbe
Moloni / 3.0.51
Moloni v3.0.51
5.0.10 5.0.09 5.0.08 5.0.07 5.0.06 trunk 0.4.0.00 0.4.8.1 3.0.10 3.0.11 3.0.35 3.0.36 3.0.37 3.0.38 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.0.46 3.0.47 3.0.48 3.0.49 3.0.50 All 98 releases
moloni / src / Model.php

Model.php in Moloni 3.0.51, at src/Model.php

160 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Moloni;
4
5 class Model
6 {
7 /**
8 * Return the row of moloni_api table with all the session details
9 * @return array|false
10 * @global $wpdb
11 */
12 public static function getTokensRow()
13 {
14 global $wpdb;
15 return $wpdb->get_row("SELECT * FROM moloni_api ORDER BY id DESC", ARRAY_A);
16 }
17
18 /**
19 * Clear moloni_api and set new access and refresh token
20 * @param string $accessToken
21 * @param string $refreshToken
22 * @return array|false
23 * @global $wpdb
24 */
25 public static function setTokens($accessToken, $refreshToken)
26 {
27 global $wpdb;
28 $wpdb->query("TRUNCATE moloni_api");
29 $wpdb->insert('moloni_api', ['main_token' => $accessToken, 'refresh_token' => $refreshToken]);
30 return self::getTokensRow();
31 }
32
33 /**
34 * Check if a setting exists on database and update it or create it
35 * @param string $option
36 * @param string $value
37 * @return int
38 * @global $wpdb
39 */
40 public static function setOption($option, $value)
41 {
42 global $wpdb;
43 $setting = $wpdb->get_row($wpdb->prepare("SELECT * FROM moloni_api_config WHERE config = %s", $option), ARRAY_A);
44
45 if (!empty($setting)) {
46 $wpdb->update('moloni_api_config', ['selected' => $value], ['config' => $option]);
47 } else {
48 $wpdb->insert('moloni_api_config', ['selected' => $value, 'config' => $option]);
49 }
50
51 return $wpdb->insert_id;
52 }
53
54 /**
55 * Checks if tokens need to be refreshed and refreshes them
56 * If it fails, log user out
57 * @param int $retryNumber
58 * @return array|false
59 * @global $wpdb
60 */
61 public static function refreshTokens($retryNumber = 0)
62 {
63 global $wpdb;
64 $tokensRow = self::getTokensRow();
65
66 $expire = false;
67 if (!isset($tokensRow['expiretime'])) {
68 $wpdb->query("ALTER TABLE moloni_api ADD expiretime varchar(250)");
69 } else {
70 $expire = $tokensRow['expiretime'];
71 }
72
73 if (!$expire || $expire < time()) {
74 $results = Curl::refresh($tokensRow['refresh_token']);
75
76 if (!isset($results['access_token'])) {
77 if ($retryNumber > 3) {
78 Log::write('A resetar as tokens depois de ' . $retryNumber . ' tentativas.');
79 $wpdb->query("TRUNCATE moloni_api");
80 return false;
81 } else {
82 $retryNumber++;
83 return self::refreshTokens($retryNumber);
84 }
85 }
86
87 $wpdb->update(
88 "moloni_api", [
89 "main_token" => $results['access_token'],
90 "refresh_token" => $results['refresh_token'],
91 "expiretime" => time() + 3000
92 ], ["id" => $tokensRow['id']]
93 );
94
95 return self::getTokensRow();
96 }
97
98 return $tokensRow;
99 }
100
101 /**
102 * Define constants from database
103 */
104 public static function defineValues()
105 {
106 $tokensRow = self::getTokensRow();
107 define("MOLONI_SESSION_ID", $tokensRow['id']);
108 define("MOLONI_ACCESS_TOKEN", $tokensRow['main_token']);
109
110 if (!empty($tokensRow['company_id'])) {
111 define("MOLONI_COMPANY_ID", $tokensRow['company_id']);
112 }
113 }
114
115 /**
116 * Define company selected settings
117 */
118 public static function defineConfigs()
119 {
120 global $wpdb;
121 $results = $wpdb->get_results("SELECT * FROM moloni_api_config ORDER BY id DESC", ARRAY_A);
122 foreach ($results as $result) {
123 $setting = strtoupper($result['config']);
124 if (!defined($setting)) {
125 define($setting, $result['selected']);
126 }
127 }
128 }
129
130 /**
131 * Get all available custom fields
132 * @return array
133 */
134 public static function getCustomFields()
135 {
136 global $wpdb;
137
138 $results = $wpdb->get_results(
139 "SELECT DISTINCT meta_key FROM " . $wpdb->prefix . "postmeta ORDER BY `" . $wpdb->prefix . "postmeta`.`meta_key` ASC",
140 ARRAY_A
141 );
142
143 $customFields = [];
144 if ($results && is_array($results)) {
145 foreach ($results as $result) {
146 $customFields[] = $result;
147 }
148 }
149 return $customFields;
150 }
151
152 public static function resetTokens()
153 {
154 global $wpdb;
155 $wpdb->query("TRUNCATE moloni_api");
156 return self::getTokensRow();
157 }
158
159 }
160