PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 6.2.4
Social Media Auto Poster – Schedule & Publish to Buffer v6.2.4
6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.2 6.1.1 6.1.0 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 All 125 releases
wp-to-buffer / lib / social / includes / class-twitter-api.php

class-twitter-api.php in Social Media Auto Poster – Schedule & Publish to Buffer 6.2.4, at lib/social/includes/class-twitter-api.php

181 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP Zinc Twitter API class
4 *
5 * @package WPZinc\Social
6 * @author WP Zinc
7 */
8
9 namespace WPZinc\Social;
10
11 /**
12 * API class to handle returning a Twitter username for a given Twitter User ID,
13 * checking the site's transient first before falling back to the WP Zinc API.
14 *
15 * @package WPZinc\Social
16 * @author WP Zinc
17 * @version 3.7.3
18 */
19 class Twitter_API extends \WPZinc\Social\API {
20
21 /**
22 * Holds the base class object.
23 *
24 * @since 3.7.3
25 *
26 * @var object
27 */
28 public $base;
29
30 /**
31 * Holds the API endpoint
32 *
33 * @since 3.7.3
34 *
35 * @var string
36 */
37 public $api_endpoint = 'https://www.wpzinc.com/?twitter_api=1';
38
39 /**
40 * Constructor
41 *
42 * @since 3.7.3
43 *
44 * @param object $base Base Plugin Class.
45 */
46 public function __construct( $base ) {
47
48 // Store base class.
49 $this->base = $base;
50
51 }
52
53 /**
54 * Returns the username for the given Twitter User ID.
55 *
56 * @since 3.7.3
57 *
58 * @param int $user_id User ID.
59 * @param int $transient_expiration_time Transient Expiration Time.
60 * @return \WP_Error|string
61 */
62 public function get_username_by_id( $user_id, $transient_expiration_time ) {
63
64 // Return Twitter username from cache, if it exists.
65 $twitter_username = $this->get_cached_username( $user_id );
66 if ( $twitter_username ) {
67 return $twitter_username;
68 }
69
70 // Fetch Twitter Username from API.
71 $twitter_username = $this->post(
72 'users_lookup',
73 array(
74 'input' => $user_id,
75 )
76 );
77
78 // Bail if an error occured.
79 if ( is_wp_error( $twitter_username ) ) {
80 return $twitter_username;
81 }
82
83 // Cache the Twitter Username.
84 $this->add_cached_username( $user_id, $twitter_username, $transient_expiration_time );
85
86 // Finally, return the Twitter Username.
87 return $twitter_username;
88
89 }
90
91 /**
92 * Sends the given User ID and Username mapping to the WP Zinc API, so it can be stored
93 * in the WP Zinc API for future lookups.
94 *
95 * As this is called via AJAX when using WebSockets to fetch a Twitter username
96 * by ID, we don't care about the WP Zinc API's response, because the user cannot
97 * do anything with it.
98 *
99 * @since 5.0.2
100 *
101 * @param int $user_id User ID.
102 * @param string $username Username.
103 * @return \WP_Error|array
104 */
105 public function username_save( $user_id, $username ) {
106
107 return $this->post(
108 'username_save',
109 array(
110 'user_id' => $user_id,
111 'username' => $username,
112 )
113 );
114
115 }
116
117 /**
118 * Returns all Twitter User ID to Username mappings stored in the transient.
119 *
120 * @since 5.0.2
121 *
122 * @return array
123 */
124 private function get_cached_usernames() {
125
126 // Get transient data.
127 $twitter_ids_usernames = get_transient( $this->base->plugin->name . '_twitter_api_usernames' );
128 if ( ! is_array( $twitter_ids_usernames ) ) {
129 return array();
130 }
131
132 return $twitter_ids_usernames;
133
134 }
135
136 /**
137 * Returns the cached Twitter Username stored in the transient for the given Twitter User ID.
138 *
139 * @since 5.0.2
140 *
141 * @param int $user_id User ID.
142 * @return bool|string
143 */
144 private function get_cached_username( $user_id ) {
145
146 // Get transient data.
147 $twitter_ids_usernames = $this->get_cached_usernames();
148
149 // If we have a username for this user ID, return the ID now.
150 if ( array_key_exists( $user_id, $twitter_ids_usernames ) ) {
151 return $twitter_ids_usernames[ $user_id ];
152 }
153
154 return false;
155
156 }
157
158 /**
159 * Adds the given Twitter User ID and Username mapping to the transient.
160 *
161 * @since 5.0.2
162 *
163 * @param int $user_id User ID.
164 * @param string $username Username.
165 * @param int $transient_expiration_time Transient Expiration Time.
166 */
167 private function add_cached_username( $user_id, $username, $transient_expiration_time ) {
168
169 // Get transient.
170 $twitter_ids_usernames = $this->get_cached_usernames();
171
172 // Store the Twitter ID and Username in the transient.
173 $twitter_ids_usernames[ $user_id ] = $username;
174
175 // Update transient.
176 set_transient( $this->base->plugin->name . '_twitter_api_usernames', $twitter_ids_usernames, $transient_expiration_time );
177
178 }
179
180 }
181