PluginProbe ʕ •ᴥ•ʔ
Custom Twitter Feeds – A Tweets Widget or X Feed Widget / 2.7.0
Custom Twitter Feeds – A Tweets Widget or X Feed Widget v2.7.0
2.8.0 2.7.0 2.6.1 2.6.0 1.0 1.0.1 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.2 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.4 1.4.1 1.5 1.5.1 1.6 1.6.1 1.7 1.8 1.8.1 1.8.2 1.8.3 1.8.4 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1 2.1.1 2.1.2 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.5.4 2.5.5 trunk
custom-twitter-feeds / inc / Builder / CTF_Db.php
custom-twitter-feeds / inc / Builder Last commit date
Controls 1 month ago Tabs 1 month ago CTF_Db.php 1 month ago CTF_Feed_Builder.php 1 month ago CTF_Feed_Saver.php 1 month ago CTF_Feed_Saver_Manager.php 1 month ago CTF_Post_Set.php 1 month ago CTF_Theme_CSS.php 1 month ago CTF_Tooltip_Wizard.php 1 month ago SB_Builder_Customizer.php 1 month ago
CTF_Db.php
667 lines
1 <?php
2 /**
3 * Twitter Feed Database
4 *
5 * @since 2.0
6 */
7
8 namespace TwitterFeed\Builder;
9
10 class CTF_Db {
11
12 const RESULTS_PER_PAGE = 20;
13
14 const RESULTS_PER_CRON_UPDATE = 6;
15
16
17 /**
18 * Query the to get feeds list for Elementor
19 *
20 * @return array|bool
21 *
22 * @since 2.0
23 */
24 public static function elementor_feeds_query() {
25 global $wpdb;
26 $feeds_elementor = [];
27 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
28 $feeds_list = $wpdb->get_results( "
29 SELECT id, feed_name FROM $feeds_table_name;
30 "
31 );
32 if ( ! empty( $feeds_list ) ) {
33 foreach($feeds_list as $feed) {
34 $feeds_elementor[$feed->id] = $feed->feed_name;
35 }
36 }
37 return $feeds_elementor;
38 }
39
40 /**
41 * Query feeds list for the modern Elementor widget.
42 *
43 * Returns feed objects with id and feed_name properties,
44 * suitable for localized script data.
45 *
46 * @since 2.5.0
47 *
48 * @return array
49 */
50 public static function elementor_feeds_list() {
51 global $wpdb;
52 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
53 return $wpdb->get_results( "SELECT id, feed_name FROM $feeds_table_name" );
54 }
55
56
57 /**
58 * Count the ctf_feeds table
59 *
60 * @param array $args
61 *
62 * @return array|bool
63 *
64 * @since 2.0
65 */
66 public static function feeds_count() {
67 global $wpdb;
68 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
69 $results = $wpdb->get_results(
70 "SELECT COUNT(*) AS num_entries FROM $feeds_table_name", ARRAY_A
71 );
72 return isset($results[0]['num_entries']) ? (int)$results[0]['num_entries'] : 0;
73 }
74
75
76 /**
77 * Query the ctf_feeds table
78 *
79 * @param array $args
80 *
81 * @return array|bool
82 *
83 * @since 2.0
84 */
85 public static function feeds_query( $args = array() ) {
86 global $wpdb;
87 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
88 $page = 0;
89 if ( isset( $args['page'] ) ) {
90 $page = (int)$args['page'] - 1;
91 unset( $args['page'] );
92 }
93
94 $offset = max( 0, $page * self::RESULTS_PER_PAGE );
95
96 if ( isset( $args['id'] ) ) {
97 $sql = $wpdb->prepare( "
98 SELECT * FROM $feeds_table_name
99 WHERE id = %d;
100 ", $args['id'] );
101 } else {
102 $sql = $wpdb->prepare( "
103 SELECT * FROM $feeds_table_name
104 LIMIT %d
105 OFFSET %d;", self::RESULTS_PER_PAGE, $offset );
106 }
107
108 return $wpdb->get_results( $sql, ARRAY_A );
109 }
110
111 /**
112 * Update feed data in the ctf_feed table
113 *
114 * @param array $to_update
115 * @param array $where_data
116 *
117 * @return false|int
118 *
119 * @since 2.0
120 */
121 public static function feeds_update( $to_update, $where_data ) {
122 global $wpdb;
123 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
124
125 $data = array();
126 $where = array();
127 $format = array();
128 foreach ( $to_update as $single_insert ) {
129 if ( $single_insert['key'] ) {
130 $data[ $single_insert['key'] ] = $single_insert['values'][0];
131 $format[] = '%s';
132 }
133 }
134
135 if ( isset( $where_data['id'] ) ) {
136 $where['id'] = $where_data['id'];
137 $where_format = array( '%d' );
138 } elseif ( isset( $where_data['feed_name'] ) ) {
139 $where['feed_name'] = $where_data['feed_name'];
140 $where_format = array( '%s' );
141 } else {
142 return false;
143 }
144
145 $data['last_modified'] = date( 'Y-m-d H:i:s' );
146 $format[] = '%s';
147
148 $affected = $wpdb->update( $feeds_table_name, $data, $where, $format, $where_format );
149
150 return $affected;
151 }
152
153 /**
154 * New feed data is added to the ctf_feeds table and
155 * the new insert ID is returned
156 *
157 * @param array $to_insert
158 *
159 * @return false|int
160 *
161 * @since 2.0
162 */
163 public static function feeds_insert( $to_insert ) {
164 global $wpdb;
165 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
166
167 $data = array();
168 $format = array();
169 foreach ( $to_insert as $single_insert ) {
170 if ( $single_insert['key'] ) {
171 $data[ $single_insert['key'] ] = $single_insert['values'][0];
172 $format[] = '%s';
173 }
174 }
175
176 $data['last_modified'] = date( 'Y-m-d H:i:s' );
177 $format[] = '%s';
178
179 $data['author'] = get_current_user_id();
180 $format[] = '%d';
181
182 $wpdb->insert( $feeds_table_name, $data, $format );
183 return $wpdb->insert_id;
184 }
185
186 /**
187 * Query the ctf_feeds table
188 * Porcess to define the name of the feed when adding new
189 *
190 * @param array $args
191 *
192 * @return array|bool
193 *
194 * @since 2.0
195 */
196 public static function feeds_query_name( $feedname ) {
197 global $wpdb;
198 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
199 $sql = $wpdb->prepare(
200 "SELECT * FROM $feeds_table_name
201 WHERE feed_name LIKE %s;",
202 $wpdb->esc_like($feedname) . '%'
203 );
204 $count = sizeof($wpdb->get_results( $sql, ARRAY_A ));
205 return ($count == 0) ? $feedname : $feedname .' ('. ($count+1) .')';
206 }
207
208
209
210 /**
211 * Query to Remove Feeds from Database
212 *
213 * @param array $feed_ids_array array of feed IDs to be deleted.
214 *
215 * @since 2.0
216 */
217 public static function delete_feeds_query($feed_ids_array)
218 {
219 global $wpdb;
220 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
221 $feed_caches_table_name = $wpdb->prefix . 'ctf_feed_caches';
222 $sanitized_feed_ids_array = array();
223 foreach ($feed_ids_array as $id) {
224 $sanitized_feed_ids_array[] = absint($id);
225 }
226 $feed_ids_array = implode(',', $sanitized_feed_ids_array);
227 $wpdb->query(
228 "DELETE FROM $feeds_table_name WHERE id IN ($feed_ids_array)"
229 );
230 $wpdb->query(
231 "DELETE FROM $feed_caches_table_name WHERE feed_id IN ($feed_ids_array)"
232 );
233
234 echo ctf_json_encode(CTF_Feed_Builder::get_feed_list());
235 wp_die();
236 }
237
238
239 /**
240 * Query to Duplicate a Single Feed
241 *
242 * @param array $args
243 *
244 * @return array|bool
245 *
246 * @since 2.0
247 */
248 public static function duplicate_feed_query( $feed_id ){
249 global $wpdb;
250 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
251 $wpdb->query(
252 $wpdb->prepare(
253 "INSERT INTO $feeds_table_name (feed_name, settings, author, status)
254 SELECT CONCAT(feed_name, ' (copy)'), settings, author, status
255 FROM $feeds_table_name
256 WHERE id = %d; ", $feed_id
257 )
258 );
259
260
261
262 echo ctf_json_encode(CTF_Feed_Builder::get_feed_list());
263 wp_die();
264 }
265
266
267 /**
268 * Get cache records in the ctf_feed_caches table
269 *
270 * @param array $args
271 *
272 * @return array|object|null
273 */
274 public static function feed_caches_query( $args ) {
275 global $wpdb;
276 $feed_cache_table_name = $wpdb->prefix . 'ctf_feed_caches';
277
278 if ( ! isset( $args['cron_update'] ) ) {
279 $sql = "
280 SELECT * FROM $feed_cache_table_name;";
281 } else {
282 if ( ! isset( $args['additional_batch'] ) ) {
283 $sql = $wpdb->prepare( "
284 SELECT * FROM $feed_cache_table_name
285 WHERE cron_update = 'yes'
286 AND feed_id != 'legacy'
287 ORDER BY last_updated ASC
288 LIMIT %d;", self::RESULTS_PER_CRON_UPDATE );
289 } else {
290 $sql = $wpdb->prepare( "
291 SELECT * FROM $feed_cache_table_name
292 WHERE cron_update = 'yes'
293 AND last_updated < %s
294 AND feed_id != 'legacy'
295 ORDER BY last_updated ASC
296 LIMIT %d;", date( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ), self::RESULTS_PER_CRON_UPDATE );
297 }
298
299 }
300
301 return $wpdb->get_results( $sql, ARRAY_A );
302 }
303
304
305 /**
306 * Creates all database tables used in the new admin area in
307 * the 6.0 update.
308 *
309 * TODO: Add error reporting
310 *
311 * @since 2.0
312 */
313 public static function create_tables() {
314 if ( !function_exists( 'dbDelta' ) ) {
315 require_once ABSPATH . '/wp-admin/includes/upgrade.php';
316 }
317
318 global $wpdb;
319 $max_index_length = 191;
320 $charset_collate = '';
321 if ( method_exists( $wpdb, 'get_charset_collate' ) ) { // get_charset_collate introduced in WP 3.5
322 $charset_collate = $wpdb->get_charset_collate();
323 }
324
325 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
326
327 if ( $wpdb->get_var( "show tables like '$feeds_table_name'" ) != $feeds_table_name ) {
328 $sql = "
329 CREATE TABLE $feeds_table_name (
330 id bigint(20) unsigned NOT NULL auto_increment,
331 feed_name text NOT NULL default '',
332 feed_title text NOT NULL default '',
333 settings longtext NOT NULL default '',
334 author bigint(20) unsigned NOT NULL default '1',
335 status varchar(255) NOT NULL default '',
336 last_modified datetime NOT NULL default '0000-00-00 00:00:00',
337 PRIMARY KEY (id),
338 KEY author (author)
339 ) $charset_collate;
340 ";
341 $wpdb->query( $sql );
342 }
343 $error = $wpdb->last_error;
344 $query = $wpdb->last_query;
345 $had_error = false;
346 if ( $wpdb->get_var( "show tables like '$feeds_table_name'" ) != $feeds_table_name ) {
347 $had_error = true;
348 //$sb_instagram_posts_manager->add_error( 'database_create', '<strong>' . __( 'There was an error when trying to create the database tables used to locate feeds.', 'custom-twitter-feeds' ) .'</strong><br>' . $error . '<br><code>' . $query . '</code>' );
349 }
350
351 if ( ! $had_error ) {
352 //$sb_instagram_posts_manager->remove_error( 'database_create' );
353 }
354
355 $feed_caches_table_name = $wpdb->prefix . 'ctf_feed_caches';
356
357 if ( $wpdb->get_var( "show tables like '$feed_caches_table_name'" ) != $feed_caches_table_name ) {
358 $sql = "
359 CREATE TABLE " . $feed_caches_table_name . " (
360 id bigint(20) unsigned NOT NULL auto_increment,
361 feed_id varchar(255) NOT NULL default '',
362 cache_key varchar(255) NOT NULL default '',
363 cache_value longtext NOT NULL default '',
364 cron_update varchar(20) NOT NULL default 'yes',
365 last_updated datetime NOT NULL default '0000-00-00 00:00:00',
366 PRIMARY KEY (id),
367 KEY feed_id (feed_id)
368 ) $charset_collate;";
369 $wpdb->query( $sql );
370 }
371 $error = $wpdb->last_error;
372 $query = $wpdb->last_query;
373 $had_error = false;
374 if ( $wpdb->get_var( "show tables like '$feed_caches_table_name'" ) != $feed_caches_table_name ) {
375 $had_error = true;
376 //$sb_instagram_posts_manager->add_error( 'database_create', '<strong>' . __( 'There was an error when trying to create the database tables used to locate feeds.', 'custom-twitter-feeds' ) .'</strong><br>' . $error . '<br><code>' . $query . '</code>' );
377 }
378
379 if ( ! $had_error ) {
380 //$sb_instagram_posts_manager->remove_error( 'database_create' );
381 }
382
383
384 if ( ! $had_error ) {
385 //$sb_instagram_posts_manager->remove_error( 'database_create' );
386 }
387 }
388
389
390
391 public static function clear_ctf_feed_caches() {
392 global $wpdb;
393 $feed_caches_table_name = $wpdb->prefix . 'ctf_feed_caches';
394
395 if ( $wpdb->get_var( "show tables like '$feed_caches_table_name'" ) === $feed_caches_table_name ) {
396 $wpdb->query( "DELETE FROM $feed_caches_table_name" );
397 }
398 }
399
400
401 public static function reset_tables() {
402 global $wpdb;
403 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
404
405 $wpdb->query( "DROP TABLE IF EXISTS $feeds_table_name" );
406 $feed_caches_table_name = $wpdb->prefix . 'ctf_feed_caches';
407
408 $wpdb->query( "DROP TABLE IF EXISTS $feed_caches_table_name" );
409
410 }
411
412 public static function reset_db_update() {
413 update_option( 'ctf_db_version', 1.3 );
414 delete_option( 'ctf_legacy_feed_settings' );
415
416 // are there existing feeds to toggle legacy onboarding?
417 $ctf_statuses_option = get_option( 'ctf_statuses', array() );
418
419 if ( isset( $ctf_statuses_option['legacy_onboarding'] ) ) {
420 unset( $ctf_statuses_option['legacy_onboarding'] );
421 }
422 if ( isset( $ctf_statuses_option['support_legacy_shortcode'] ) ) {
423 unset( $ctf_statuses_option['support_legacy_shortcode'] );
424 }
425
426 global $wpdb;
427
428 $table_name = $wpdb->prefix . "usermeta";
429 $wpdb->query( "
430 DELETE
431 FROM $table_name
432 WHERE `meta_key` LIKE ('ctf\_%')
433 " );
434
435
436 $feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
437
438 $results = $wpdb->query( "
439 DELETE
440 FROM $feed_locator_table_name
441 WHERE feed_id LIKE '*%';" );
442
443 update_option( 'ctf_statuses', $ctf_statuses_option );
444 }
445
446 public static function reset_legacy() {
447
448 //Settings
449 delete_option( 'ctf_statuses' );
450 delete_option( 'ctf_options' );
451 delete_option( 'ctf_ver' );
452 delete_option( 'ctf_db_version' );
453
454 // Clear backup caches
455 global $wpdb;
456 $table_name = $wpdb->prefix . "options";
457 $wpdb->query( "
458 DELETE
459 FROM $table_name
460 WHERE `option_name` LIKE ('%!ctf\_%')
461 " );
462 $wpdb->query( "
463 DELETE
464 FROM $table_name
465 WHERE `option_name` LIKE ('%\_transient\_&ctf\_%')
466 " );
467 $wpdb->query( "
468 DELETE
469 FROM $table_name
470 WHERE `option_name` LIKE ('%\_transient\_timeout\_&ctf\_%')
471 " );
472 $wpdb->query( "
473 DELETE
474 FROM $table_name
475 WHERE `option_name` LIKE ('%sb_wlupdated_%')
476 " );
477
478 //image resizing
479 $upload = wp_upload_dir();
480 $posts_table_name = $wpdb->prefix . 'ctf_posts';
481 $feeds_posts_table_name = esc_sql( $wpdb->prefix . 'ctf_feeds_posts' );
482
483 $image_files = glob( trailingslashit( $upload['basedir'] ) . trailingslashit( CTF_UPLOADS_NAME ) . '*' ); // get all file names
484 foreach ( $image_files as $file ) { // iterate files
485 if ( is_file( $file ) ) {
486 unlink( $file );
487 } // delete file
488 }
489
490 //global $wp_filesystem;
491
492 //$wp_filesystem->delete( trailingslashit( $upload['basedir'] ) . trailingslashit( CTF_UPLOADS_NAME ) , true );
493 //Delete tables
494 $wpdb->query( "DROP TABLE IF EXISTS $posts_table_name" );
495 $wpdb->query( "DROP TABLE IF EXISTS $feeds_posts_table_name" );
496 $locator_table_name = $wpdb->prefix . CTF_FEED_LOCATOR;
497 $wpdb->query( "DROP TABLE IF EXISTS $locator_table_name" );
498
499 $table_name = $wpdb->prefix . "options";
500 $wpdb->query( "
501 DELETE
502 FROM $table_name
503 WHERE `option_name` LIKE ('%\_transient\_\$ctf\_%')
504 " );
505 $wpdb->query( "
506 DELETE
507 FROM $table_name
508 WHERE `option_name` LIKE ('%\_transient\_timeout\_\$ctf\_%')
509 " );
510 delete_option( 'ctf_usage_tracking' );
511 delete_option( 'ctf_usage_tracking_config' );
512 delete_option( 'ctf_smash_usage_tracking' );
513 delete_option( 'ctf_smash_usage_tracking_site_token' );
514 delete_option( 'ctf_smash_usage_tracking_schedule' );
515 delete_option( 'ctf_smash_usage_events' );
516 delete_option( 'ctf_smash_usage_active_dates' );
517 delete_option( 'ctf_smash_usage_session_durations' );
518 delete_option( 'ctf_configure' );
519 delete_option( 'ctf_customize' );
520 delete_option( 'ctf_style' );
521 delete_option( 'ctf_options' );
522 delete_option( 'ctf_statuses' );
523 delete_option( 'ctf_local_avatars' );
524 delete_option( 'ctf_db_version' );
525 delete_option( 'ctf_cache_list' );
526 delete_option( 'ctf_twitter_cards' );
527 delete_option( 'ctf_errors' );
528
529 global $wp_roles;
530 $wp_roles->remove_cap( 'administrator', 'manage_twitter_feed_options' );
531 wp_clear_scheduled_hook( 'ctf_feed_update' );
532 wp_clear_scheduled_hook( 'ctf_usage_tracking_cron' );
533 wp_clear_scheduled_hook( \TwitterFeed\UsageTracking\Config::CRON_HOOK );
534 }
535
536 /**
537 * Query the cff_feeds table
538 *
539 * @param array $args
540 *
541 * @return array|bool
542 *
543 * @since 4.0
544 */
545 public static function all_feeds_query()
546 {
547 global $wpdb;
548 $feeds_table_name = $wpdb->prefix . 'ctf_feeds';
549 $sql = "SELECT * FROM $feeds_table_name";
550 return $wpdb->get_results($sql, ARRAY_A);
551 }
552
553 /**
554 * Summary of get_posts_by_ids
555 *
556 * @param mixed $post_ids
557 *
558 * @return array
559 */
560 public static function get_posts_by_ids($post_ids)
561 {
562 global $wpdb;
563 $posts_table = $wpdb->prefix . 'ctf_posts';
564
565 if (empty($post_ids)) {
566 return [];
567 }
568
569 $post_ids = esc_sql($post_ids);
570 $sql_ids = "'" . implode('\', \'', $post_ids) . "'";
571
572 $posts = $wpdb->get_results("SELECT * FROM $posts_table
573 WHERE twitter_id IN ($sql_ids)",
574 ARRAY_A
575 );
576 $results = [];
577
578 foreach ($posts as $post) {
579 $transformed = self::transform_single_post_schema($post);
580 if (!empty($transformed)) {
581 array_push($results, $transformed);
582 }
583 }
584
585 return $results;
586 }
587
588 /**
589 * Summary of transform_single_post_schema
590 *
591 * @param mixed $post
592 *
593 * @return array
594 */
595 public static function transform_single_post_schema($post)
596 {
597 $data = parse_json_data($post['json_data']);
598
599 if (empty($data) || empty($data['id_str'])) {
600 return [];
601 }
602
603 return [
604 'plugin' => [
605 'slug' => 'twitter',
606 ],
607 'feed_post_id' => $data['id_str'],
608 'text' => !empty($data['text']) ? $data['text'] : '',
609 'imageSrc' => '',
610 'updated_time_ago' => !empty($data['created_at']) ? $data['created_at'] : '',
611 'profile' => [
612 'label' => !empty($data['user']['name']) ? $data['user']['name'] : '',
613 'url' => !empty($data['user']['screen_name']) ? 'https://x.com/' . $data['user']['screen_name'] : '',
614 'id' => !empty($data['user']['rest_id']) ? $data['user']['rest_id'] : '',
615 ]
616 ];
617 }
618
619 /**
620 * Summary of get_feed_source_info
621 *
622 * @param mixed $feed_id
623 *
624 * @return array
625 */
626 public static function get_feed_source_info($feed_id)
627 {
628 global $wpdb;
629 $feeds_table = $wpdb->prefix . 'ctf_feeds';
630 $feed = $wpdb->get_row(
631 $wpdb->prepare(
632 "SELECT * FROM $feeds_table WHERE id = %d",
633 $feed_id
634 ),
635 ARRAY_A
636 );
637
638 if (empty($feed['settings'])) {
639 return [];
640 }
641
642 $settings = json_decode($feed['settings'], true);
643 $name = 'Twitter Feed ' . $feed_id;
644 $image = '';
645
646 if ($settings['type'] === 'usertimeline') {
647 $name = !empty($settings['screenname'])
648 ? $settings['screenname']
649 : $settings['usertimeline_text'];
650
651 $image = 'https://unavatar.io/x/' . str_replace("@", "", $name);
652 }
653
654 if ($settings['type'] === 'search') {
655 $name = !empty($settings['feed_term'])
656 ? $settings['feed_term']
657 : $name;
658 }
659
660
661 return [
662 'name' => $name,
663 'picture' => $image
664 ];
665 }
666 }
667