Instagram Widget by WPZOOM - Version 1.7.5

Version Description

  • Fixed strings text domain
  • Fixed dismiss url for admin notice
  • Added Text Domain and Domain Path to plugin description
Download this release

Release Info

Developer WPZOOM
Plugin Icon 128x128 Instagram Widget by WPZOOM
Version 1.7.5
Comparing to
See all releases

Code changes from version 1.6.4 to 1.7.5

LICENSE ADDED
@@ -0,0 +1,339 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your
12
+ freedom to share and change it. By contrast, the GNU General Public
13
+ License is intended to guarantee your freedom to share and change free
14
+ software--to make sure the software is free for all its users. This
15
+ General Public License applies to most of the Free Software
16
+ Foundation's software and to any other program whose authors commit to
17
+ using it. (Some other Free Software Foundation software is covered by
18
+ the GNU Lesser General Public License instead.) You can apply it to
19
+ your programs, too.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ this service if you wish), that you receive source code or can get it
25
+ if you want it, that you can change the software or use pieces of it
26
+ in new free programs; and that you know you can do these things.
27
+
28
+ To protect your rights, we need to make restrictions that forbid
29
+ anyone to deny you these rights or to ask you to surrender the rights.
30
+ These restrictions translate to certain responsibilities for you if you
31
+ distribute copies of the software, or if you modify it.
32
+
33
+ For example, if you distribute copies of such a program, whether
34
+ gratis or for a fee, you must give the recipients all the rights that
35
+ you have. You must make sure that they, too, receive or can get the
36
+ source code. And you must show them these terms so they know their
37
+ rights.
38
+
39
+ We protect your rights with two steps: (1) copyright the software, and
40
+ (2) offer you this license which gives you legal permission to copy,
41
+ distribute and/or modify the software.
42
+
43
+ Also, for each author's protection and ours, we want to make certain
44
+ that everyone understands that there is no warranty for this free
45
+ software. If the software is modified by someone else and passed on, we
46
+ want its recipients to know that what they have is not the original, so
47
+ that any problems introduced by others will not reflect on the original
48
+ authors' reputations.
49
+
50
+ Finally, any free program is threatened constantly by software
51
+ patents. We wish to avoid the danger that redistributors of a free
52
+ program will individually obtain patent licenses, in effect making the
53
+ program proprietary. To prevent this, we have made it clear that any
54
+ patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ GNU GENERAL PUBLIC LICENSE
60
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+ 0. This License applies to any program or other work which contains
63
+ a notice placed by the copyright holder saying it may be distributed
64
+ under the terms of this General Public License. The "Program", below,
65
+ refers to any such program or work, and a "work based on the Program"
66
+ means either the Program or any derivative work under copyright law:
67
+ that is to say, a work containing the Program or a portion of it,
68
+ either verbatim or with modifications and/or translated into another
69
+ language. (Hereinafter, translation is included without limitation in
70
+ the term "modification".) Each licensee is addressed as "you".
71
+
72
+ Activities other than copying, distribution and modification are not
73
+ covered by this License; they are outside its scope. The act of
74
+ running the Program is not restricted, and the output from the Program
75
+ is covered only if its contents constitute a work based on the
76
+ Program (independent of having been made by running the Program).
77
+ Whether that is true depends on what the Program does.
78
+
79
+ 1. You may copy and distribute verbatim copies of the Program's
80
+ source code as you receive it, in any medium, provided that you
81
+ conspicuously and appropriately publish on each copy an appropriate
82
+ copyright notice and disclaimer of warranty; keep intact all the
83
+ notices that refer to this License and to the absence of any warranty;
84
+ and give any other recipients of the Program a copy of this License
85
+ along with the Program.
86
+
87
+ You may charge a fee for the physical act of transferring a copy, and
88
+ you may at your option offer warranty protection in exchange for a fee.
89
+
90
+ 2. You may modify your copy or copies of the Program or any portion
91
+ of it, thus forming a work based on the Program, and copy and
92
+ distribute such modifications or work under the terms of Section 1
93
+ above, provided that you also meet all of these conditions:
94
+
95
+ a) You must cause the modified files to carry prominent notices
96
+ stating that you changed the files and the date of any change.
97
+
98
+ b) You must cause any work that you distribute or publish, that in
99
+ whole or in part contains or is derived from the Program or any
100
+ part thereof, to be licensed as a whole at no charge to all third
101
+ parties under the terms of this License.
102
+
103
+ c) If the modified program normally reads commands interactively
104
+ when run, you must cause it, when started running for such
105
+ interactive use in the most ordinary way, to print or display an
106
+ announcement including an appropriate copyright notice and a
107
+ notice that there is no warranty (or else, saying that you provide
108
+ a warranty) and that users may redistribute the program under
109
+ these conditions, and telling the user how to view a copy of this
110
+ License. (Exception: if the Program itself is interactive but
111
+ does not normally print such an announcement, your work based on
112
+ the Program is not required to print an announcement.)
113
+
114
+ These requirements apply to the modified work as a whole. If
115
+ identifiable sections of that work are not derived from the Program,
116
+ and can be reasonably considered independent and separate works in
117
+ themselves, then this License, and its terms, do not apply to those
118
+ sections when you distribute them as separate works. But when you
119
+ distribute the same sections as part of a whole which is a work based
120
+ on the Program, the distribution of the whole must be on the terms of
121
+ this License, whose permissions for other licensees extend to the
122
+ entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+ Thus, it is not the intent of this section to claim rights or contest
125
+ your rights to work written entirely by you; rather, the intent is to
126
+ exercise the right to control the distribution of derivative or
127
+ collective works based on the Program.
128
+
129
+ In addition, mere aggregation of another work not based on the Program
130
+ with the Program (or with a work based on the Program) on a volume of
131
+ a storage or distribution medium does not bring the other work under
132
+ the scope of this License.
133
+
134
+ 3. You may copy and distribute the Program (or a work based on it,
135
+ under Section 2) in object code or executable form under the terms of
136
+ Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+ a) Accompany it with the complete corresponding machine-readable
139
+ source code, which must be distributed under the terms of Sections
140
+ 1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+ b) Accompany it with a written offer, valid for at least three
143
+ years, to give any third party, for a charge no more than your
144
+ cost of physically performing source distribution, a complete
145
+ machine-readable copy of the corresponding source code, to be
146
+ distributed under the terms of Sections 1 and 2 above on a medium
147
+ customarily used for software interchange; or,
148
+
149
+ c) Accompany it with the information you received as to the offer
150
+ to distribute corresponding source code. (This alternative is
151
+ allowed only for noncommercial distribution and only if you
152
+ received the program in object code or executable form with such
153
+ an offer, in accord with Subsection b above.)
154
+
155
+ The source code for a work means the preferred form of the work for
156
+ making modifications to it. For an executable work, complete source
157
+ code means all the source code for all modules it contains, plus any
158
+ associated interface definition files, plus the scripts used to
159
+ control compilation and installation of the executable. However, as a
160
+ special exception, the source code distributed need not include
161
+ anything that is normally distributed (in either source or binary
162
+ form) with the major components (compiler, kernel, and so on) of the
163
+ operating system on which the executable runs, unless that component
164
+ itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering
167
+ access to copy from a designated place, then offering equivalent
168
+ access to copy the source code from the same place counts as
169
+ distribution of the source code, even though third parties are not
170
+ compelled to copy the source along with the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program
173
+ except as expressly provided under this License. Any attempt
174
+ otherwise to copy, modify, sublicense or distribute the Program is
175
+ void, and will automatically terminate your rights under this License.
176
+ However, parties who have received copies, or rights, from you under
177
+ this License will not have their licenses terminated so long as such
178
+ parties remain in full compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not
181
+ signed it. However, nothing else grants you permission to modify or
182
+ distribute the Program or its derivative works. These actions are
183
+ prohibited by law if you do not accept this License. Therefore, by
184
+ modifying or distributing the Program (or any work based on the
185
+ Program), you indicate your acceptance of this License to do so, and
186
+ all its terms and conditions for copying, distributing or modifying
187
+ the Program or works based on it.
188
+
189
+ 6. Each time you redistribute the Program (or any work based on the
190
+ Program), the recipient automatically receives a license from the
191
+ original licensor to copy, distribute or modify the Program subject to
192
+ these terms and conditions. You may not impose any further
193
+ restrictions on the recipients' exercise of the rights granted herein.
194
+ You are not responsible for enforcing compliance by third parties to
195
+ this License.
196
+
197
+ 7. If, as a consequence of a court judgment or allegation of patent
198
+ infringement or for any other reason (not limited to patent issues),
199
+ conditions are imposed on you (whether by court order, agreement or
200
+ otherwise) that contradict the conditions of this License, they do not
201
+ excuse you from the conditions of this License. If you cannot
202
+ distribute so as to satisfy simultaneously your obligations under this
203
+ License and any other pertinent obligations, then as a consequence you
204
+ may not distribute the Program at all. For example, if a patent
205
+ license would not permit royalty-free redistribution of the Program by
206
+ all those who receive copies directly or indirectly through you, then
207
+ the only way you could satisfy both it and this License would be to
208
+ refrain entirely from distribution of the Program.
209
+
210
+ If any portion of this section is held invalid or unenforceable under
211
+ any particular circumstance, the balance of the section is intended to
212
+ apply and the section as a whole is intended to apply in other
213
+ circumstances.
214
+
215
+ It is not the purpose of this section to induce you to infringe any
216
+ patents or other property right claims or to contest validity of any
217
+ such claims; this section has the sole purpose of protecting the
218
+ integrity of the free software distribution system, which is
219
+ implemented by public license practices. Many people have made
220
+ generous contributions to the wide range of software distributed
221
+ through that system in reliance on consistent application of that
222
+ system; it is up to the author/donor to decide if he or she is willing
223
+ to distribute software through any other system and a licensee cannot
224
+ impose that choice.
225
+
226
+ This section is intended to make thoroughly clear what is believed to
227
+ be a consequence of the rest of this License.
228
+
229
+ 8. If the distribution and/or use of the Program is restricted in
230
+ certain countries either by patents or by copyrighted interfaces, the
231
+ original copyright holder who places the Program under this License
232
+ may add an explicit geographical distribution limitation excluding
233
+ those countries, so that distribution is permitted only in or among
234
+ countries not thus excluded. In such case, this License incorporates
235
+ the limitation as if written in the body of this License.
236
+
237
+ 9. The Free Software Foundation may publish revised and/or new versions
238
+ of the General Public License from time to time. Such new versions will
239
+ be similar in spirit to the present version, but may differ in detail to
240
+ address new problems or concerns.
241
+
242
+ Each version is given a distinguishing version number. If the Program
243
+ specifies a version number of this License which applies to it and "any
244
+ later version", you have the option of following the terms and conditions
245
+ either of that version or of any later version published by the Free
246
+ Software Foundation. If the Program does not specify a version number of
247
+ this License, you may choose any version ever published by the Free Software
248
+ Foundation.
249
+
250
+ 10. If you wish to incorporate parts of the Program into other free
251
+ programs whose distribution conditions are different, write to the author
252
+ to ask for permission. For software which is copyrighted by the Free
253
+ Software Foundation, write to the Free Software Foundation; we sometimes
254
+ make exceptions for this. Our decision will be guided by the two goals
255
+ of preserving the free status of all derivatives of our free software and
256
+ of promoting the sharing and reuse of software generally.
257
+
258
+ NO WARRANTY
259
+
260
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+ REPAIR OR CORRECTION.
269
+
270
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+ POSSIBILITY OF SUCH DAMAGES.
279
+
280
+ END OF TERMS AND CONDITIONS
281
+
282
+ How to Apply These Terms to Your New Programs
283
+
284
+ If you develop a new program, and you want it to be of the greatest
285
+ possible use to the public, the best way to achieve this is to make it
286
+ free software which everyone can redistribute and change under these terms.
287
+
288
+ To do so, attach the following notices to the program. It is safest
289
+ to attach them to the start of each source file to most effectively
290
+ convey the exclusion of warranty; and each file should have at least
291
+ the "copyright" line and a pointer to where the full notice is found.
292
+
293
+ <one line to give the program's name and a brief idea of what it does.>
294
+ Copyright (C) <year> <name of author>
295
+
296
+ This program is free software; you can redistribute it and/or modify
297
+ it under the terms of the GNU General Public License as published by
298
+ the Free Software Foundation; either version 2 of the License, or
299
+ (at your option) any later version.
300
+
301
+ This program is distributed in the hope that it will be useful,
302
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
303
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
+ GNU General Public License for more details.
305
+
306
+ You should have received a copy of the GNU General Public License along
307
+ with this program; if not, write to the Free Software Foundation, Inc.,
308
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309
+
310
+ Also add information on how to contact you by electronic and paper mail.
311
+
312
+ If the program is interactive, make it output a short notice like this
313
+ when it starts in an interactive mode:
314
+
315
+ Gnomovision version 69, Copyright (C) year name of author
316
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
+ This is free software, and you are welcome to redistribute it
318
+ under certain conditions; type `show c' for details.
319
+
320
+ The hypothetical commands `show w' and `show c' should show the appropriate
321
+ parts of the General Public License. Of course, the commands you use may
322
+ be called something other than `show w' and `show c'; they could even be
323
+ mouse-clicks or menu items--whatever suits your program.
324
+
325
+ You should also get your employer (if you work as a programmer) or your
326
+ school, if any, to sign a "copyright disclaimer" for the program, if
327
+ necessary. Here is a sample; alter the names:
328
+
329
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
331
+
332
+ <signature of Ty Coon>, 1 April 1989
333
+ Ty Coon, President of Vice
334
+
335
+ This General Public License does not permit incorporating your program into
336
+ proprietary programs. If your program is a subroutine library, you may
337
+ consider it more useful to permit linking proprietary applications with the
338
+ library. If this is what you want to do, use the GNU Lesser General
339
+ Public License instead of this License.
class-wpzoom-instagram-image-uploader.php ADDED
@@ -0,0 +1,394 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Exit if accessed directly.
4
+ */
5
+ if ( ! defined( 'ABSPATH' ) ) {
6
+ exit;
7
+ }
8
+
9
+ /**
10
+ * Class WPZOOM_Instagram_Image_Uploader
11
+ */
12
+ class WPZOOM_Instagram_Image_Uploader {
13
+ public static $ajax_action_name = 'wpzoom_instagram_get_image_async';
14
+ private static $instance;
15
+ private static $media_metakey_name = 'wpzoom_instagram_media_id';
16
+ private static $prefix_name = 'wpzoom-share-buttons';
17
+ private static $post_status_name = 'wpzoom-hidden';
18
+ private static $transient_name = 'zoom_instagram_is_configured';
19
+
20
+ /**
21
+ * WPZOOM_Instagram_Image_Uploader constructor.
22
+ */
23
+ private function __construct() {
24
+ // Private to disabled instantiation.
25
+ }
26
+
27
+ /**
28
+ * @return mixed
29
+ */
30
+ public static function getInstance() {
31
+ if ( null === self::$instance ) {
32
+ self::$instance = new self();
33
+ add_action( 'init', self::$instance, 'custom_post_status' );
34
+ add_action( 'wp_ajax_' . self::$ajax_action_name, array( self::$instance, 'get_image_async' ) );
35
+ add_action( 'wp_ajax_nopriv_' . self::$ajax_action_name, array( self::$instance, 'get_image_async' ) );
36
+
37
+ }
38
+
39
+ return self::$instance;
40
+ }
41
+
42
+ /**
43
+ * Get image.
44
+ *
45
+ * @param $media_size
46
+ * @param $media_url
47
+ * @param $media_id
48
+ *
49
+ * @return bool
50
+ */
51
+ static function get_image( $media_size, $media_url, $media_id ) {
52
+
53
+ $args = array(
54
+ 'post_type' => 'attachment',
55
+ 'posts_per_page' => 1,
56
+ 'post_status' => self::$post_status_name,
57
+ 'meta_query' => array(
58
+ array(
59
+ 'key' => self::$media_metakey_name,
60
+ 'value' => $media_id,
61
+ ),
62
+ ),
63
+ );
64
+ $query = new WP_Query( $args );
65
+
66
+ if ( $query->have_posts() ) {
67
+ $post = array_shift( $query->posts );
68
+ $attachment_id = $post->ID;
69
+
70
+ $image_src = wp_get_attachment_image_src( $attachment_id, self::get_image_size_name( $media_size ) );
71
+
72
+ return ! empty( $image_src ) ? $image_src[0] : $media_url;
73
+
74
+ }
75
+
76
+ return false;
77
+ }
78
+
79
+ /**
80
+ * Get prefixed image size name.
81
+ *
82
+ * @param $size
83
+ *
84
+ * @return string
85
+ */
86
+ public static function get_image_size_name( $size ) {
87
+
88
+ return self::$prefix_name . '-' . $size;
89
+
90
+ }
91
+
92
+ /**
93
+ * Get response api from transient.
94
+ *
95
+ * @return mixed
96
+ */
97
+ function get_api_transient() {
98
+ return json_decode( get_transient( self::$transient_name ) );
99
+ }
100
+
101
+ /**
102
+ * Get transient lifetime from settings.
103
+ *
104
+ * @return float|int
105
+ */
106
+ function get_transient_lifetime() {
107
+
108
+ $options = get_option( 'wpzoom-instagram-widget-settings', wpzoom_instagram_get_default_settings() );
109
+
110
+ $values = array(
111
+ 'minutes' => MINUTE_IN_SECONDS,
112
+ 'hours' => HOUR_IN_SECONDS,
113
+ 'days' => DAY_IN_SECONDS,
114
+ );
115
+ $keys = array_keys( $values );
116
+ $type = in_array( $options['transient-lifetime-type'], $keys ) ? $values[ $options['transient-lifetime-type'] ] : $values['minutes'];
117
+
118
+ return $type * $options['transient-lifetime-value'];
119
+ }
120
+
121
+ /**
122
+ * Get image from ajax.
123
+ */
124
+ function get_image_async() {
125
+
126
+ $sliced = wp_array_slice_assoc( $_POST, array( 'media-id', 'nonce', 'image-resolution', 'image-width', 'regenerate-thumbnails' ) );
127
+ $sliced = array_map( 'sanitize_text_field', $sliced );
128
+
129
+ if ( ! wp_verify_nonce( $sliced['nonce'], self::get_nonce_action( $sliced['media-id'] ) ) ) {
130
+ $error = new WP_Error( '001', __( 'Invalid nonce.', 'wpzoom-instagram-widget' ), __( 'Invalid nonce provided for this action', 'wpzoom-instagram-widget' ) );
131
+
132
+ wp_send_json_error( $error, 500 );
133
+ }
134
+
135
+ $media_url = self::get_media_url_by_id( $sliced['media-id'] );
136
+
137
+ if ( empty( $media_url ) ) {
138
+ $error = new WP_Error( '002', __( 'Invalid media id.', 'wpzoom-instagram-widget' ), __( 'Could not retrieve image url with provided media id', 'wpzoom-instagram-widget' ) );
139
+
140
+ wp_send_json_error( $error, 500 );
141
+ }
142
+
143
+ $args = array(
144
+ 'post_type' => 'attachment',
145
+ 'posts_per_page' => 1,
146
+ 'post_status' => self::$post_status_name,
147
+ 'meta_query' => array(
148
+ array(
149
+ 'key' => self::$media_metakey_name,
150
+ 'value' => $sliced['media-id'],
151
+ ),
152
+ ),
153
+ );
154
+
155
+ $query = new WP_Query( $args );
156
+
157
+ if ( $query->have_posts() ) {
158
+ $post = array_shift( $query->posts );
159
+ $attachment_id = $post->ID;
160
+
161
+ } else {
162
+ $attachment_id = self::upload_image( $media_url, $sliced['media-id'] );
163
+ self::$instance->set_images_to_transient( $attachment_id, $sliced['media-id'] );
164
+ }
165
+
166
+ if ( wp_validate_boolean( $sliced['regenerate-thumbnails'] ) ) {
167
+
168
+ $metadata = $this->regenerate_thumbnails( $attachment_id );
169
+
170
+ if ( ! is_wp_error( $metadata ) && ! empty( $metadata ) ) {
171
+ self::$instance->set_images_to_transient( $attachment_id, $sliced['media-id'] );
172
+ }
173
+ }
174
+
175
+ if ( is_wp_error( $attachment_id ) ) {
176
+ wp_send_json_error( $attachment_id, 500 );
177
+ }
178
+
179
+ $media_size = self::$instance->get_best_size( $sliced['image-width'], $sliced['image-resolution'] );
180
+
181
+ $image_src = wp_get_attachment_image_src( $attachment_id, self::get_image_size_name( $media_size ) );
182
+
183
+ $image_src = ! empty( $image_src ) ? $image_src[0] : $media_url;
184
+
185
+ wp_send_json_success( array( 'image_src' => $image_src ) );
186
+ }
187
+
188
+ /**
189
+ * @param $id
190
+ *
191
+ * @return string
192
+ */
193
+ public static function get_nonce_action( $id ) {
194
+ return self::$ajax_action_name . '_' . $id;
195
+ }
196
+
197
+ public function regenerate_thumbnails( $attachment_id ) {
198
+ $fullsizepath = get_attached_file( $attachment_id );
199
+
200
+ add_filter( 'intermediate_image_sizes_advanced', array( self::$instance, 'set_image_sizes' ), 10 );
201
+
202
+ $metadata = wp_generate_attachment_metadata( $attachment_id, $fullsizepath );
203
+
204
+ remove_filter( 'intermediate_image_sizes_advanced', array( self::$instance, 'set_image_sizes' ), 10 );
205
+ return $metadata;
206
+ }
207
+
208
+ /**
209
+ * @param $media_id
210
+ *
211
+ * @return bool
212
+ */
213
+ public static function get_media_url_by_id( $media_id ) {
214
+
215
+ $transient = self::$instance->get_api_transient();
216
+
217
+ if ( empty( $transient->data ) ) {
218
+ return false;
219
+ }
220
+ $plucked = wp_list_pluck( $transient->data, 'media_url', 'id' );
221
+
222
+ return array_key_exists( $media_id, $plucked ) ? $plucked[ $media_id ] : false;
223
+ }
224
+
225
+ /**
226
+ * @param $media_url
227
+ * @param $media_id
228
+ *
229
+ * @return string|WP_Error
230
+ */
231
+ static function upload_image( $media_url, $media_id ) {
232
+
233
+ require_once ABSPATH . 'wp-admin/includes/media.php';
234
+ require_once ABSPATH . 'wp-admin/includes/file.php';
235
+ require_once ABSPATH . 'wp-admin/includes/image.php';
236
+
237
+ add_filter( 'intermediate_image_sizes_advanced', array( self::$instance, 'set_image_sizes' ), 10 );
238
+ add_filter( 'wp_insert_attachment_data', array( self::$instance, 'insert_post_data' ), 10 );
239
+
240
+ $attachment_id = media_sideload_image( $media_url, null, null, 'id' );
241
+
242
+ remove_filter( 'intermediate_image_sizes_advanced', array( self::$instance, 'set_image_sizes' ), 10 );
243
+ remove_filter( 'wp_insert_attachment_data', array( self::$instance, 'insert_post_data' ), 10 );
244
+
245
+ update_post_meta( $attachment_id, self::$media_metakey_name, $media_id );
246
+
247
+ return $attachment_id;
248
+
249
+ }
250
+
251
+ /**
252
+ * @param $post_data
253
+ *
254
+ * @return mixed
255
+ */
256
+ public function insert_post_data( $post_data ) {
257
+
258
+ $post_data['post_status'] = self::$post_status_name;
259
+
260
+ return $post_data;
261
+ }
262
+
263
+ /**
264
+ * Register custom post status.
265
+ */
266
+ function custom_post_status() {
267
+ register_post_status(
268
+ self::$post_status_name,
269
+ array(
270
+ 'public' => true,
271
+ 'exclude_from_search' => true,
272
+ 'internal' => true,
273
+ )
274
+ );
275
+ }
276
+
277
+ /**
278
+ * Disable the cloning of this class.
279
+ *
280
+ * @return void
281
+ */
282
+ final public function __clone() {
283
+ throw new Exception( 'Feature disabled.' );
284
+ }
285
+
286
+ /**
287
+ * Disable the wakeup of this class.
288
+ *
289
+ * @return void
290
+ */
291
+ final public function __wakeup() {
292
+ throw new Exception( 'Feature disabled.' );
293
+ }
294
+
295
+ /**
296
+ * Set image sizes.
297
+ *
298
+ * @param $sizes
299
+ *
300
+ * @return array
301
+ */
302
+ function set_image_sizes( $sizes ) {
303
+
304
+ return array(
305
+ self::get_image_size_name( 'thumbnail' ) => array(
306
+ 'width' => 150,
307
+ 'height' => 150,
308
+ ),
309
+ self::get_image_size_name( 'low_resolution' ) => array(
310
+ 'width' => 320,
311
+ 'height' => 320,
312
+ ),
313
+ self::get_image_size_name( 'standard_resolution' ) => array(
314
+ 'width' => 640,
315
+ 'height' => 640,
316
+ ),
317
+ );
318
+
319
+ }
320
+
321
+ /**
322
+ * Alter transient data object with the new values.
323
+ *
324
+ * @param $attachment_id
325
+ * @param $media_id
326
+ */
327
+ protected function set_images_to_transient( $attachment_id, $media_id ) {
328
+
329
+ $transient = self::$instance->get_api_transient();
330
+
331
+ if ( ! empty( $transient->data ) ) {
332
+ foreach ( $transient->data as $key => $item ) {
333
+ if ( $item->id === $media_id ) {
334
+ $thumbnail = wp_get_attachment_image_src( $attachment_id, self::get_image_size_name( 'thumbnail' ) );
335
+ $low_resolution = wp_get_attachment_image_src( $attachment_id, self::get_image_size_name( 'low_resolution' ) );
336
+ $standard_resolution = wp_get_attachment_image_src( $attachment_id, self::get_image_size_name( 'standard_resolution' ) );
337
+ $item->images->thumbnail->url = ! empty( $thumbnail ) ? $thumbnail[0] : '';
338
+ $item->images->low_resolution->url = ! empty( $low_resolution ) ? $low_resolution[0] : '';
339
+
340
+ $item->images->standard_resolution->url = ! empty( $standard_resolution ) ? $standard_resolution[0] : '';
341
+
342
+ $transient->data[ $key ] = $item;
343
+ }
344
+ }
345
+
346
+ $this->set_api_transient( $transient );
347
+ }
348
+ }
349
+
350
+ /**
351
+ * Set api transient.
352
+ *
353
+ * @param $data
354
+ *
355
+ * @return bool
356
+ */
357
+ function set_api_transient( $data ) {
358
+ return set_transient( self::$transient_name, wp_json_encode( $data ), self::$instance->get_transient_lifetime() );
359
+ }
360
+
361
+ /**
362
+ * Get best image size.
363
+ *
364
+ * @param $desired_width
365
+ * @param string $image_resolution
366
+ *
367
+ * @return int|string
368
+ */
369
+ protected function get_best_size( $desired_width, $image_resolution = 'default_algorithm' ) {
370
+
371
+ $size = 'thumbnail';
372
+
373
+ $sizes = array(
374
+ 'thumbnail' => 150,
375
+ 'low_resolution' => 320,
376
+ 'standard_resolution' => 640,
377
+ );
378
+
379
+ $diff = PHP_INT_MAX;
380
+
381
+ if ( array_key_exists( $image_resolution, $sizes ) ) {
382
+ return $image_resolution;
383
+ }
384
+
385
+ foreach ( $sizes as $key => $value ) {
386
+ if ( abs( $desired_width - $value ) < $diff ) {
387
+ $size = $key;
388
+ $diff = abs( $desired_width - $value );
389
+ }
390
+ }
391
+
392
+ return $size;
393
+ }
394
+ }
class-wpzoom-instagram-widget-api.php CHANGED
@@ -1,333 +1,350 @@
1
  <?php
 
 
 
 
 
 
2
 
3
  class Wpzoom_Instagram_Widget_API {
4
- /**
5
- * @var Wpzoom_Instagram_Widget_API The reference to *Singleton* instance of this class
6
- */
7
- private static $instance;
8
-
9
- /**
10
- * Instagram Access Token
11
- *
12
- * @var string
13
- */
14
- protected $access_token;
15
-
16
- /**
17
- * Request headers.
18
- *
19
- * @var array
20
- */
21
- public $headers = [];
22
-
23
  /**
24
  * Errors collector.
25
  *
26
  * @var array|WP_Error
27
  */
28
- public $errors = [];
29
-
30
- /**
31
- * Returns the *Singleton* instance of this class.
32
- *
33
- * @return Wpzoom_Instagram_Widget_API The *Singleton* instance.
34
- */
35
- public static function getInstance()
36
- {
37
- if ( null === self::$instance ) {
38
- self::$instance = new self();
39
- }
40
-
41
- return self::$instance;
42
- }
43
-
44
- protected function __construct() {
45
- $options = get_option( 'wpzoom-instagram-widget-settings' , wpzoom_instagram_get_default_settings());
46
 
47
- $this->request_type = !empty($options['request-type']) ? $options['request-type'] : '';
48
- $this->access_token = $options['access-token'];
49
 
50
- if('with-basic-access-token' === $this->request_type){
51
- $this->access_token = $options['basic-access-token'];
52
- }
53
 
 
 
 
 
 
54
 
55
- $this->username = !empty($options['username']) ? $options['username'] : '';
56
- $this->transient_lifetime_type = !empty($options['transient-lifetime-type']) ? $options['transient-lifetime-type'] : 'days';
57
- $this->transient_lifetime_value = !empty($options['transient-lifetime-value']) ? $options['transient-lifetime-value'] : 1;
58
- $this->is_embed_stream = ! empty( $options['is-embed-stream'] ) ? wp_validate_boolean( $options['is-embed-stream'] ) : false;
59
- $this->is_forced_timeout = ! empty($options['is-forced-timeout']) ? wp_validate_boolean( $options['is-forced-timeout'] ) : false;
60
- $this->request_timeout_value = !empty($options['request-timeout-value']) ? $options['request-timeout-value'] : 15;
61
 
62
- if ($this->is_forced_timeout && !empty($this->request_timeout_value)) {
63
- $this->headers['timeout'] = $this->request_timeout_value;
64
- }
65
 
66
- $this->errors = new WP_Error();
67
 
68
- }
69
 
70
  /**
71
- * Get error messages collection.
72
  *
73
- * @return array
74
  */
75
- public function get_errors(){
76
- return [
77
- 'user-info-without-token' => [
78
- 'code' => 'user-info-without-token',
79
- 'message' => __('Empty json user info from Public Feed.', 'wpzoom-instagram-widget')
80
- ],
81
- 'response-data-without-token-from-json-invalid-response' => [
82
- 'code' => 'response-data-without-token-from-json-invalid-response',
83
- 'message' => __('The request from the Public Feed failed. Invalid server response from Public JSON API url.', 'wpzoom-instagram-widget')
84
- ],
85
- 'response-data-without-token-from-json-invalid-json-format' => [
86
- 'code' => 'response-data-without-token-from-json-invalid-json-format',
87
- 'message' => __('The request from the Public Feed failed. Invalid JSON format from Public JSON API url.', 'wpzoom-instagram-widget')
88
- ],
89
- 'response-data-without-token-from-html-invalid-response' => [
90
- 'code' => 'response-data-without-token-from-html-invalid-response',
91
- 'message' => __('The request from the Public Feed failed. Check username.', 'wpzoom-instagram-widget')
92
- ],
93
- 'response-data-without-token-from-html-invalid-json-format' => [
94
- 'code' => 'response-data-without-token-from-html-invalid-json-format',
95
- 'message' => __('The request from the Public Feed failed. Invalid JSON format from parsed html body.', 'wpzoom-instagram-widget')
96
- ],
97
- 'items-without-token-invalid-response' => [
98
- 'code' => 'items-without-token-invalid-response',
99
- 'message' => __('Get items from the Public Feed failed. Invalid response.', 'wpzoom-instagram-widget')
100
- ],
101
- 'items-without-token-invalid-json-structure' => [
102
- 'code' => 'items-without-token-invalid-json-structure',
103
- 'message' => __('Get items from the Public Feed failed. Malformed data structure.', 'wpzoom-instagram-widget')
104
- ],
105
- 'items-with-token-invalid-response' => [
106
- 'code' => 'items-with-token-invalid-response',
107
- 'message' => __('Geting items from the Instagram API Feed failed. Invalid response.', 'wpzoom-instagram-widget')
108
- ],
109
- 'items-with-token-invalid-data-structure' => [
110
- 'code' => 'items-with-token-invalid-data-structure',
111
- 'message' => __('Get items from the Instagram API Feed failed. Malformed data structure.', 'wpzoom-instagram-widget')
112
- ],
113
- 'user-with-token-invalid-response' => [
114
- 'code' => 'user-with-token-invalid-response',
115
- 'message' => __('Get user data from the Instagram API Feed failed. Invalid response.', 'wpzoom-instagram-widget')
116
- ],
117
- 'user-with-token-invalid-data-structure' => [
118
- 'code' => 'user-with-token-invalid-data-structure',
119
- 'message' => __('Get user data from the Instagram API Feed failed. Malformed data structure.', 'wpzoom-instagram-widget')
120
- ],
121
-
122
- ];
123
- }
124
 
125
  /**
126
- * Retrieve error message by key.
127
- *
128
- * @param $key
129
  *
130
- * @return bool|mixed
131
  */
132
- public function get_error( $key ) {
133
- $errors = $this->get_errors();
 
 
 
 
 
 
 
 
 
 
134
 
135
- return array_key_exists( $key, $errors ) ? $errors[ $key ] : false;
136
- }
 
 
 
137
 
138
- function get_transient_lifetime() {
139
 
140
- $values = array( 'minutes' => MINUTE_IN_SECONDS, 'hours' => HOUR_IN_SECONDS, 'days' => DAY_IN_SECONDS );
141
- $keys = array_keys( $values );
142
- $type = in_array( $this->transient_lifetime_type, $keys ) ? $values[ $this->transient_lifetime_type ] : $values['minutes'];
143
 
144
- return $type * $this->transient_lifetime_value;
145
- }
 
 
146
 
147
- function get_user_info_without_token( $user ) {
148
 
149
- $response = $this->get_response_without_token( $user );
 
150
 
151
- if ( is_wp_error( $response ) ) {
152
- $error_data = $this->get_error('user-info-without-token');
153
- $this->errors->add($error_data['code'], $error_data['message']);
154
- return new WP_Error( $error_data['code'], $error_data['message'] );
155
- }
156
 
157
- if (isset($response->entry_data->ProfilePage[0]->graphql->user)) {
158
- $user_info = $response->entry_data->ProfilePage[0]->graphql->user;
159
- } elseif (isset($response->graphql->user)) {
160
- $user_info = $response->graphql->user;
161
- } else {
162
- $error_data = $this->get_error('user-info-without-token');
163
- $this->errors->add($error_data['code'], $error_data['message']);
164
- return new WP_Error( $error_data['code'], $error_data['message'] );
165
- }
166
 
167
- $converted = new stdClass;
 
 
 
 
 
 
168
 
169
- $converted->data = (object) array(
170
- 'bio' => ! empty( $user_info->biography ) ? $user_info->biography : '',
171
- 'counts' => (object) array(
172
- 'followed_by' => ! empty( $user_info->edge_followed_by->count ) ? $user_info->edge_followed_by->count : 0,
173
- 'follows' => ! empty( $user_info->edge_follow->count ) ? $user_info->edge_follow->count : 0,
174
- 'media' => ! empty( $user_info->edge_owner_to_timeline_media->count ) ? $user_info->edge_owner_to_timeline_media->count : 0,
175
- ),
176
- 'full_name' => ! empty( $user_info->full_name ) ? $user_info->full_name : '',
177
- 'id' => ! empty( $user_info->id ) ? $user_info->id : '',
178
- 'is_business' => ! empty( $user_info->is_business_account ) ? $user_info->is_business_account : '',
179
- 'profile_picture' => ! empty( $user_info->profile_pic_url ) ? $user_info->profile_pic_url : '',
180
- 'username' => ! empty( $user_info->username ) ? $user_info->username : '',
181
- 'website' => ! empty( $user_info->external_url ) ? $user_info->external_url : ''
182
- );
183
 
184
- return $converted;
 
185
 
186
- }
 
187
 
188
- function convert_user_info_to_old_structure( $user_info ) {
 
189
 
190
- $converted = new stdClass;
191
 
192
- $converted->data = (object) array(
193
- 'bio' => null,
194
- 'counts' => (object) array(
195
- 'followed_by' => null,
196
- 'follows' => null,
197
- 'media' => null,
198
- ),
199
- 'full_name' => ! empty( $user_info->username ) ? $user_info->username : '',
200
- 'id' => ! empty( $user_info->id ) ? $user_info->id : '',
201
- 'is_business' => null,
202
- 'profile_picture' => null,
203
- 'username' => ! empty( $user_info->username ) ? $user_info->username : '',
204
- 'website' => null
205
- );
206
-
207
- return $converted;
208
 
209
- }
210
 
211
- function get_response_without_token_from_json( $user ) {
212
 
213
- $user = trim( $user );
214
- $url = 'https://instagram.com/' . str_replace( '@', '', $user ).'/?__a=1';
215
 
216
- $request = wp_remote_get( $url, $this->headers );
 
 
217
 
218
- if (is_wp_error($request) || 200 != wp_remote_retrieve_response_code($request)) {
219
- $error_data = $this->get_error('response-data-without-token-from-json-invalid-response');
220
- $this->errors->add($error_data['code'], $error_data['message']);
221
- return new WP_Error($error_data['code'], $error_data['message']);
222
- }
223
 
224
- $result = json_decode( wp_remote_retrieve_body( $request ) );
 
225
 
226
- if ( empty( $result ) ) {
227
- $error_data = $this->get_error('response-data-without-token-from-json-invalid-json-format');
228
- $this->errors->add($error_data['code'], $error_data['message']);
229
- return new WP_Error($error_data['code'], $error_data['message']);
230
- }
231
 
232
- return $result;
233
- }
234
 
 
235
 
236
- function get_response_without_token( $user ) {
 
237
 
238
- $user = trim( $user );
239
- $url = 'https://instagram.com/' . str_replace( '@', '', $user );
240
 
241
- $request = wp_remote_get( $url, $this->headers );
 
 
242
 
243
- if (is_wp_error($request) || 200 != wp_remote_retrieve_response_code($request)) {
 
 
244
 
245
- $error_data = $this->get_error('response-data-without-token-from-html-invalid-response');
246
- $this->errors->add($error_data['code'], $error_data['message']);
 
247
 
248
- $result = $this->get_response_without_token_from_json($user);
249
 
250
- if (is_wp_error($result)) {
251
- return new WP_Error('invalid_response', __('Invalid response from Instagram', 'wpzoom-instagram-widget'));
252
- } else {
253
- return $result;
254
- }
255
- }
256
 
257
- $body = wp_remote_retrieve_body( $request );
 
 
 
 
 
 
 
 
 
 
258
 
259
- $doc = new DOMDocument();
 
 
 
260
 
261
- @$doc->loadHTML( $body );
 
262
 
263
- $script_tags = $doc->getElementsByTagName( 'script' );
 
 
 
 
 
 
 
 
 
 
 
264
 
265
- $json = '';
266
 
267
- foreach ( $script_tags as $script_tag ) {
268
- if ( strpos( $script_tag->nodeValue, 'window._sharedData = ' ) !== false ) {
269
- $json = $script_tag->nodeValue;
270
- break;
271
- }
272
- }
273
 
274
- $json = str_replace( array( 'window._sharedData = ', '};' ), array( '', '}' ), $json );
275
- $result = json_decode( $json );
 
 
 
 
276
 
277
- if ( empty( $result ) ) {
 
278
 
279
- $error_data = $this->get_error('response-data-without-token-from-html-invalid-json-format');
280
- $this->errors->add($error_data['code'], $error_data['message']);
 
 
 
 
 
 
 
281
 
282
- $result = $this->get_response_without_token_from_json($user);
 
283
 
284
- if(is_wp_error($result)){
285
- return new WP_Error( 'empty-json', __( 'Empty json decoded data.', 'wpzoom-instagram-widget' ) );
286
- }
287
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
 
289
- return $result;
290
- }
291
 
292
  function convert_items_to_old_structure( $data ) {
293
- $converted = new stdClass;
294
  $converted->data = array();
295
 
296
  foreach ( $data->data as $key => $item ) {
297
 
298
  $converted->data[] = (object) array(
299
  'id' => $item->id,
 
300
  'user' => (object) array(
301
  'id' => null,
302
  'fullname' => null,
303
  'profile_picture' => null,
304
- 'username' => $item->username
305
  ),
306
  'images' => (object) array(
307
  'thumbnail' => (object) array(
308
- 'url' => $this->convert_to_embed_url( 'thumbnail', $item->permalink ),
309
  'width' => 150,
310
- 'height' => 150
311
  ),
312
  'low_resolution' => (object) array(
313
- 'url' => $this->convert_to_embed_url( 'low_resolution', $item->permalink ),
314
  'width' => 320,
315
- 'height' => 320
316
  ),
317
  'standard_resolution' => (object) array(
318
- 'url' => $this->convert_to_embed_url( 'standard_resolution', $item->permalink ),
319
  'width' => 640,
320
- 'height' => 640
321
  ),
322
  ),
323
- 'type' => ($item->media_type === 'IMAGE' || $item->media_type === 'CAROUSEL_ALBUM') ? 'image' : 'video',
324
  'likes' => null,
325
  'comments' => null,
326
  'created_time' => null,
327
  'link' => $item->permalink,
328
  'caption' => (object) array(
329
- 'text' => isset( $item->caption ) ? $item->caption : ''
330
- )
331
  );
332
 
333
  }
@@ -335,443 +352,395 @@ class Wpzoom_Instagram_Widget_API {
335
  return $converted;
336
  }
337
 
338
- function get_items_without_token( $user ) {
339
-
340
- $result = $this->get_response_without_token( $user );
341
-
342
- if (is_wp_error($result)) {
343
- $error_data = $this->get_error('items-without-token-invalid-response');
344
- $this->errors->add($error_data['code'], $error_data['message']);
345
- return new WP_Error($error_data['code'], $error_data['message']);
346
- }
347
-
348
- if (isset($result->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges)) {
349
- $edges = $result->entry_data->ProfilePage[0]->graphql->user->edge_owner_to_timeline_media->edges;
350
- } elseif (isset($result->graphql->user->edge_owner_to_timeline_media->edges)) {
351
- $edges = $result->graphql->user->edge_owner_to_timeline_media->edges;
352
- } else {
353
- $error_data = $this->get_error('items-without-token-invalid-json-structure');
354
- $this->errors->add($error_data['code'], $error_data['message']);
355
- return new WP_Error($error_data['code'], $error_data['message']);
356
- }
357
-
358
- $converted = new stdClass;
359
- $converted->data = array();
360
- foreach ( $edges as $edge ) {
361
-
362
- $node = $edge->node;
363
-
364
- $converted->data[] = (object) array(
365
- 'user' => (object) array(
366
- 'id' => $node->owner->id,
367
- 'fullname' => '',
368
- 'profile_picture' => '',
369
- 'username' => $node->owner->username
370
- ),
371
- 'images' => (object) array(
372
- 'thumbnail' => (object) array(
373
- 'url' => $node->thumbnail_resources[0]->src,
374
- 'width' => $node->thumbnail_resources[0]->config_width,
375
- 'height' => $node->thumbnail_resources[0]->config_height
376
- ),
377
- 'low_resolution' => (object) array(
378
- 'url' => $node->thumbnail_resources[2]->src,
379
- 'width' => $node->thumbnail_resources[2]->config_width,
380
- 'height' => $node->thumbnail_resources[2]->config_height
381
- ),
382
- 'standard_resolution' => (object) array(
383
- 'url' => $node->thumbnail_resources[4]->src,
384
- 'width' => $node->thumbnail_resources[4]->config_width,
385
- 'height' => $node->thumbnail_resources[4]->config_height
386
- ),
387
- ),
388
- 'type' => empty($node->is_video) ? 'image': 'video',
389
- 'likes' => isset( $node->edge_liked_by ) ? $node->edge_liked_by : 0,
390
- 'comments' => isset( $node->edge_media_to_comment ) ? $node->edge_media_to_comment : 0,
391
- 'created_time' => $node->taken_at_timestamp,
392
- 'link' => sprintf( 'https://www.instagram.com/p/%s/', $node->shortcode ),
393
- 'caption' => (object) array(
394
- 'text' => isset( $node->edge_media_to_caption->edges[0]->node->text ) ? $node->edge_media_to_caption->edges[0]->node->text : ''
395
- )
396
- );
397
- }
398
-
399
- return $converted;
400
- }
401
-
402
- /**
403
- * @param $screen_name string Instagram username
404
- * @param $image_limit int Number of images to retrieve
405
- * @param $image_width int Desired image width to retrieve
406
- *
407
- * @return array|bool Array of tweets or false if method fails
408
- */
409
- public function get_items( $instance ) {
410
-
411
- $sliced = wp_array_slice_assoc( $instance, array(
412
- 'image-limit',
413
- 'image-width',
414
- 'image-resolution',
415
- 'username',
416
- 'disable-video-thumbs'
417
- ) );
418
-
419
- $image_limit = $sliced['image-limit'];
420
- $image_width = $sliced['image-width'];
421
- $image_resolution = ! empty( $sliced['image-resolution'] ) ? $sliced['image-resolution'] : 'default_algorithm';
422
- $injected_username = ! empty( $sliced['username'] ) ? $sliced['username'] : '';
423
- $disable_video_thumbs = ! empty( $sliced['disable-video-thumbs'] );
424
-
425
- $transient = 'zoom_instagram_is_configured';
426
-
427
- $injected_username = trim( $injected_username );
428
-
429
- if ( ! empty( $injected_username ) && 'without-access-token' === $this->request_type ) {
430
- $injected_username = str_replace( '@', '', $injected_username );
431
- $transient = $transient . '_' . $injected_username;
432
- }
433
-
434
- if ( false !== ( $data = get_transient( $transient ) ) && is_object( $data ) && ! empty( $data->data ) ) {
435
-
436
- return $this->processing_response_data( $data, $image_width, $image_resolution, $image_limit, $disable_video_thumbs );
437
- }
438
-
439
- $is_external_username = ! empty( $this->username ) || ! empty( $injected_username );
440
- $external_username = ! empty( $injected_username ) ? $injected_username : $this->username;
441
-
442
-
443
- if ( ! empty( $this->access_token ) ) {
444
 
445
- $request_url = sprintf( 'https://api.instagram.com/v1/users/self/media/recent/?access_token=%s&count=%s', $this->access_token, 30 );
446
 
447
- if ( 'with-basic-access-token' === $this->request_type ) {
448
- $request_url = add_query_arg( [
449
- 'fields' => 'media_url,media_type,caption,username,permalink,thumbnail_url',
450
- 'access_token' => $this->access_token
451
- ],
452
- 'https://graph.instagram.com/me/media' );
453
- }
454
 
455
- $response = wp_remote_get( $request_url, $this->headers );
 
 
 
 
 
 
 
 
 
 
 
 
456
 
457
- if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
458
- set_transient( $transient, false, MINUTE_IN_SECONDS );
 
459
 
460
- $error_data = $this->get_error( 'items-with-token-invalid-response' );
461
- $this->errors->add( $error_data['code'], $error_data['message'] );
462
 
463
- return false;
464
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465
 
466
- $data = json_decode( wp_remote_retrieve_body( $response ) );
 
467
 
468
- if ( 'with-basic-access-token' === $this->request_type ) {
469
- $data = $this->convert_items_to_old_structure( $data );
470
- }
471
- }
472
 
473
- if ( 'without-access-token' === $this->request_type && ! empty( $is_external_username ) ) {
 
474
 
 
475
 
476
- $data = $this->get_items_without_token( $external_username );
477
 
478
- if ( is_wp_error( $data ) ) {
479
- set_transient( $transient, false, MINUTE_IN_SECONDS );
480
 
481
- return false;
482
- }
483
 
484
- }
 
 
 
 
 
485
 
486
- if ( ! empty( $data->data ) ) {
487
- set_transient( $transient, $data, $this->get_transient_lifetime() );
488
- } else {
489
- set_transient( $transient, false, MINUTE_IN_SECONDS );
490
 
491
- $error_data = $this->get_error('items-with-token-invalid-data-structure');
492
- $this->errors->add($error_data['code'], $error_data['message']);
493
 
494
- return false;
495
- }
496
 
497
- return $this->processing_response_data( $data, $image_width, $image_resolution, $image_limit, $disable_video_thumbs );
498
- }
499
 
500
- public function processing_response_data( $data, $image_width, $image_resolution = 'default_algorithm', $image_limit, $disable_video_thumbs = false ) {
501
 
502
- $result = array();
503
- $username = '';
 
 
 
 
504
 
505
- foreach ( $data->data as $key => $item ) {
 
506
 
507
- if ( empty( $username ) ) {
508
- $username = $item->user->username;
509
- }
510
 
511
- if ( $key === $image_limit ) {
512
- break;
513
- }
514
 
515
- if ( ! empty( $disable_video_thumbs ) && isset( $item->type ) && 'video' == $item->type ) {
516
- $image_limit++;
517
- continue;
518
 
519
- }
 
 
 
520
 
521
- $best_size = $this->get_best_size( $image_width, $image_resolution );
522
- $image_url = $this->is_embed_stream ? $this->convert_to_embed_url( $best_size, $item->link ) : $item->images->{$best_size}->url;
523
 
524
- $result[] = array(
525
- 'link' => $item->link,
526
- 'image-url' => $image_url,
527
- 'image-caption' => ! empty( $item->caption->text ) ? esc_attr( $item->caption->text ) : '',
528
- 'likes_count' => ! empty( $item->likes->count ) ? esc_attr( $item->likes->count ) : 0,
529
- 'comments_count' => ! empty( $item->comments->count ) ? esc_attr( $item->comments->count ) : 0
530
- );
531
- }
532
 
533
- $result = array( 'items' => $result, 'username' => $username );
 
534
 
535
- return $result;
536
- }
537
 
538
- public function get_user_info( $injected_username = '' ) {
 
 
539
 
 
 
540
 
541
- $transient = 'zoom_instagram_user_info';
542
 
543
- $injected_username = rtrim( $injected_username );
 
 
544
 
545
- if ( ! empty( $injected_username ) && 'without-access-token' === $this->request_type ) {
546
- $injected_username = str_replace( '@', '', $injected_username );
547
- $transient = $transient . '_' . $injected_username;
548
- }
549
 
550
- if ( false !== ( $data = get_transient( $transient ) ) && is_object( $data ) && ! empty( $data->data ) ) {
 
551
 
552
- return $data;
553
- }
 
 
 
 
554
 
555
- $is_external_username = ! empty( $this->username ) || ! empty( $injected_username );
556
- $external_username = ! empty( $injected_username ) ? $injected_username : $this->username;
557
 
558
- if ( ! empty( $this->access_token ) ) {
559
 
560
- $request_url = sprintf( 'https://api.instagram.com/v1/users/self/?access_token=%s', $this->access_token );
 
 
 
 
 
 
561
 
562
- if ( 'with-basic-access-token' === $this->request_type ) {
563
- $request_url = add_query_arg( [
564
- 'access_token' => $this->access_token,
565
- 'fields' => 'account_type,id,media_count,username'
566
- ],
567
- 'https://graph.instagram.com/me' );
568
- }
569
 
570
- $response = wp_remote_get( $request_url, $this->headers );
571
 
572
- if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
573
- set_transient( $transient, false, MINUTE_IN_SECONDS );
574
 
575
- $error_data = $this->get_error( 'user-with-token-invalid-response' );
576
- $this->errors->add( $error_data['code'], $error_data['message'] );
577
 
578
- return false;
579
- }
 
 
580
 
581
- $data = json_decode( wp_remote_retrieve_body( $response ) );
582
 
583
- if ( 'with-basic-access-token' === $this->request_type ) {
584
- $data = $this->convert_user_info_to_old_structure( $data );
585
- }
586
 
587
- }
 
588
 
589
- if ( 'without-access-token' === $this->request_type && ! empty( $is_external_username ) ) {
590
 
591
- $data = $this->get_user_info_without_token( $external_username );
 
 
 
 
 
 
592
 
593
- if ( is_wp_error( $data ) ) {
594
- set_transient( $transient, false, MINUTE_IN_SECONDS );
595
 
596
- return false;
597
- }
598
 
599
- }
 
600
 
601
- if ( ! empty( $data->data ) ) {
602
- set_transient( $transient, $data, $this->get_transient_lifetime() );
603
- } else {
604
- set_transient( $transient, false, MINUTE_IN_SECONDS );
605
 
606
- $error_data = $this->get_error('user-with-token-invalid-data-structure');
607
- $this->errors->add($error_data['code'], $error_data['message']);
608
 
609
- return false;
610
- }
611
 
612
- return $data;
613
 
614
- }
615
 
616
- /**
617
- * @param $screen_name string Instagram username
618
- *
619
- * @return bool|int Instagram user id or false on error
620
- */
621
- protected function get_user_id( $screen_name ) {
622
- $user_id_option = 'zoom_instagram_uid_' . $screen_name;
623
 
624
- if ( false !== ( $user_id = get_option( $user_id_option ) ) ) {
625
- return $user_id;
626
- }
627
 
628
- $response = wp_remote_get( sprintf( 'https://api.instagram.com/v1/users/search?q=%s&access_token=%s', $screen_name, $this->access_token ), $this->headers );
 
 
 
629
 
630
- if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
631
- return false;
632
- }
633
 
634
- $result = json_decode( wp_remote_retrieve_body( $response ) );
 
635
 
636
- if ( ! isset( $result->data ) ) {
637
- return false;
638
- }
639
 
640
- $user_id = false;
641
 
642
- foreach ( $result->data as $user ) {
643
- if ( $user->username === $screen_name ) {
644
- $user_id = $user->id;
645
 
646
- break;
647
- }
648
- }
649
 
650
- update_option( $user_id_option, $user_id );
651
 
652
- return $user_id;
653
- }
654
 
655
- /**
656
- * @param $desired_width int Desired image width in pixels
657
- *
658
- * @return string Image size for Instagram API
659
- */
660
- protected function get_best_size( $desired_width, $image_resolution = 'default_algorithm' ) {
661
- $size = 'thumbnail';
662
- $sizes = array(
663
- 'thumbnail' => 150,
664
- 'low_resolution' => 306,
665
- 'standard_resolution' => 640
666
- );
667
 
668
- $diff = PHP_INT_MAX;
669
 
670
- if ( array_key_exists( $image_resolution, $sizes ) ) {
671
- return $image_resolution;
672
- }
673
 
674
- foreach ( $sizes as $key => $value ) {
675
- if ( abs( $desired_width - $value ) < $diff ) {
676
- $size = $key;
677
- $diff = abs( $desired_width - $value );
678
- }
679
- }
 
 
 
 
 
 
 
 
680
 
681
- return $size;
682
- }
683
 
684
- protected function convert_to_embed_url( $size, $link ) {
685
- $sizes = array(
686
- 'thumbnail' => 't',
687
- 'low_resolution' => 'm',
688
- 'standard_resolution' => 'l'
689
- );
690
 
691
- $shortcode = trim( basename( $link ) );
692
- $embed_url = sprintf( 'https://instagram.com/p/%1$s/media/?size=%2$s', $shortcode, $sizes[ $size ] );
693
 
694
- return $embed_url;
695
- }
696
 
697
- /**
698
- * Check if given access token is valid for Instagram Api.
699
- */
700
- public static function is_access_token_valid( $access_token, $request_type = '' ) {
701
 
702
- $request_url = sprintf( 'https://api.instagram.com/v1/users/self/?access_token=%s', $access_token );
 
 
703
 
704
- if ( empty( $access_token ) ) {
705
- return false;
706
  }
707
 
708
- if ( 'with-basic-access-token' === $request_type ) {
709
- $request_url = add_query_arg( [
710
- 'fields' => 'username',
711
- 'access_token' => $access_token
 
 
 
712
 
713
- ], 'https://graph.instagram.com/me' );
714
  }
715
 
716
- $response = wp_remote_get( $request_url );
717
 
718
- if ( is_wp_error( $response ) ) {
719
- return $response;
720
- }
 
 
 
 
 
 
 
 
 
 
 
721
 
722
- if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
723
- return false;
724
- }
725
 
726
- return true;
727
  }
728
 
729
  public function is_configured() {
730
- $transient = 'zoom_instagram_is_configured';
 
 
 
 
 
 
 
 
 
731
 
732
- if ( false !== ( $result = get_transient( $transient ) ) ) {
733
- if ( 'yes' === $result ) {
734
- return true;
735
- }
736
 
737
- if ( 'no' === $result ) {
738
- return false;
739
- }
740
 
741
- if ( ! empty( $result ) ) {
742
- return true;
743
- }
744
- }
745
 
746
- if(empty($this->username)){
747
- $condition = $this->is_access_token_valid( $this->access_token, $this->request_type );
748
 
749
- } else{
750
- $condition = true;
751
- }
752
 
 
753
 
754
- if ( true === $condition ) {
755
- set_transient( $transient, 'yes', DAY_IN_SECONDS );
756
 
757
- return true;
758
- }
 
 
759
 
760
- set_transient( $transient, 'no', DAY_IN_SECONDS );
 
 
761
 
762
- return false;
763
- }
 
 
764
 
765
- public static function reset_cache() {
766
- delete_transient( 'zoom_instagram_is_configured' );
767
- delete_transient( 'zoom_instagram_user_info' );
768
- }
 
 
 
 
 
769
 
770
- public function get_access_token() {
771
- return $this->access_token;
772
- }
773
 
774
- public function set_access_token( $access_token ) {
775
- $this->access_token = $access_token;
776
- }
777
- }
1
  <?php
2
+ /**
3
+ * Exit if accessed directly.
4
+ */
5
+ if ( ! defined( 'ABSPATH' ) ) {
6
+ exit;
7
+ }
8
 
9
  class Wpzoom_Instagram_Widget_API {
10
+ /**
11
+ * @var Wpzoom_Instagram_Widget_API The reference to *Singleton* instance of this class
12
+ */
13
+ private static $instance;
14
+ /**
15
+ * Request headers.
16
+ *
17
+ * @var array
18
+ */
19
+ public $headers = array();
 
 
 
 
 
 
 
 
 
20
  /**
21
  * Errors collector.
22
  *
23
  * @var array|WP_Error
24
  */
25
+ public $errors = array();
26
+ /**
27
+ * Instagram Access Token
28
+ *
29
+ * @var string
30
+ */
31
+ protected $access_token;
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ protected function __construct() {
34
+ $options = get_option( 'wpzoom-instagram-widget-settings', wpzoom_instagram_get_default_settings() );
35
 
36
+ $this->request_type = ! empty( $options['request-type'] ) ? $options['request-type'] : '';
37
+ $this->access_token = ! empty( $options['basic-access-token'] ) ? $options['basic-access-token'] : '';
 
38
 
39
+ $this->username = ! empty( $options['username'] ) ? $options['username'] : '';
40
+ $this->transient_lifetime_type = ! empty( $options['transient-lifetime-type'] ) ? $options['transient-lifetime-type'] : 'days';
41
+ $this->transient_lifetime_value = ! empty( $options['transient-lifetime-value'] ) ? $options['transient-lifetime-value'] : 1;
42
+ $this->is_forced_timeout = ! empty( $options['is-forced-timeout'] ) ? wp_validate_boolean( $options['is-forced-timeout'] ) : false;
43
+ $this->request_timeout_value = ! empty( $options['request-timeout-value'] ) ? $options['request-timeout-value'] : 15;
44
 
45
+ if ( $this->is_forced_timeout && ! empty( $this->request_timeout_value ) ) {
46
+ $this->headers['timeout'] = $this->request_timeout_value;
47
+ }
 
 
 
48
 
49
+ $this->image_uploader = WPZOOM_Instagram_Image_Uploader::getInstance();
 
 
50
 
51
+ $this->errors = new WP_Error();
52
 
53
+ }
54
 
55
  /**
56
+ * Returns the *Singleton* instance of this class.
57
  *
58
+ * @return Wpzoom_Instagram_Widget_API The *Singleton* instance.
59
  */
60
+ public static function getInstance() {
61
+ if ( null === self::$instance ) {
62
+ self::$instance = new self();
63
+ }
64
+
65
+ return self::$instance;
66
+ }
67
+
68
+ public static function reset_cache() {
69
+ delete_transient( 'zoom_instagram_is_configured' );
70
+ delete_transient( 'zoom_instagram_user_info' );
71
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  /**
74
+ * @param $screen_name string Instagram username
75
+ * @param $image_limit int Number of images to retrieve
76
+ * @param $image_width int Desired image width to retrieve
77
  *
78
+ * @return array|bool Array of tweets or false if method fails
79
  */
80
+ public function get_items( $instance ) {
81
+
82
+ $sliced = wp_array_slice_assoc(
83
+ $instance,
84
+ array(
85
+ 'image-limit',
86
+ 'image-width',
87
+ 'image-resolution',
88
+ 'username',
89
+ 'disable-video-thumbs',
90
+ )
91
+ );
92
 
93
+ $image_limit = $sliced['image-limit'];
94
+ $image_width = $sliced['image-width'];
95
+ $image_resolution = ! empty( $sliced['image-resolution'] ) ? $sliced['image-resolution'] : 'default_algorithm';
96
+ $injected_username = ! empty( $sliced['username'] ) ? $sliced['username'] : '';
97
+ $disable_video_thumbs = ! empty( $sliced['disable-video-thumbs'] );
98
 
99
+ $transient = 'zoom_instagram_is_configured';
100
 
101
+ $injected_username = trim( $injected_username );
 
 
102
 
103
+ if ( ! empty( $injected_username ) && 'without-access-token' === $this->request_type ) {
104
+ $injected_username = str_replace( '@', '', $injected_username );
105
+ $transient = $transient . '_' . $injected_username;
106
+ }
107
 
108
+ if ( false !== ( $data = json_decode( get_transient( $transient ) ) ) && is_object( $data ) && ! empty( $data->data ) ) {
109
 
110
+ return $this->processing_response_data( $data, $image_width, $image_resolution, $image_limit, $disable_video_thumbs );
111
+ }
112
 
113
+ $is_external_username = ! empty( $this->username ) || ! empty( $injected_username );
114
+ $external_username = ! empty( $injected_username ) ? $injected_username : $this->username;
 
 
 
115
 
116
+ if ( ! empty( $this->access_token ) ) {
 
 
 
 
 
 
 
 
117
 
118
+ $request_url = add_query_arg(
119
+ array(
120
+ 'fields' => 'media_url,media_type,caption,username,permalink,thumbnail_url',
121
+ 'access_token' => $this->access_token,
122
+ ),
123
+ 'https://graph.instagram.com/me/media'
124
+ );
125
 
126
+ $response = wp_remote_get( $request_url, $this->headers );
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
+ if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
129
+ set_transient( $transient, wp_json_encode( false ), MINUTE_IN_SECONDS );
130
 
131
+ $error_data = $this->get_error( 'items-with-token-invalid-response' );
132
+ $this->errors->add( $error_data['code'], $error_data['message'] );
133
 
134
+ return false;
135
+ }
136
 
137
+ $data = json_decode( wp_remote_retrieve_body( $response ) );
138
 
139
+ $data = $this->convert_items_to_old_structure( $data );
140
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141