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_Feed_Saver.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_Feed_Saver.php
644 lines
1 <?php
2 /**
3 * Instagram Feed Database
4 *
5 * @since 2.0
6 */
7
8 namespace TwitterFeed\Builder;
9 use TwitterFeed\CTF_Settings;
10
11 class CTF_Feed_Saver {
12
13 /**
14 * @var int
15 *
16 * @since 2.0
17 */
18 private $insert_id;
19
20 /**
21 * @var array
22 *
23 * @since 2.0
24 */
25 private $data;
26
27 /**
28 * @var array
29 *
30 * @since 2.0
31 */
32 private $sanitized_and_sorted_data;
33
34 /**
35 * @var array
36 *
37 * @since 2.0
38 */
39 private $feed_db_data;
40
41
42 /**
43 * @var string
44 *
45 * @since 2.0
46 */
47 private $feed_name;
48
49 /**
50 * @var bool
51 *
52 * @since 2.0
53 */
54 private $is_legacy;
55
56 /**
57 * CTF_Feed_Saver constructor.
58 *
59 * @param int $insert_id
60 *
61 * @since 2.0
62 */
63 public function __construct( $insert_id ) {
64 if ( $insert_id === 'legacy' ) {
65 $this->is_legacy = true;
66 $this->insert_id = 0;
67 } else {
68 $this->is_legacy = false;
69 $this->insert_id = $insert_id;
70 }
71 }
72
73 /**
74 * Feed insert ID if it exists
75 *
76 * @return bool|int
77 *
78 * @since 2.0
79 */
80 public function get_feed_id() {
81 if ( $this->is_legacy ) {
82 return 'legacy';
83 }
84 if ( ! empty( $this->insert_id ) ) {
85 return $this->insert_id;
86 } else {
87 return false;
88 }
89 }
90
91 /**
92 * @param array $data
93 *
94 * @since 2.0
95 */
96 public function set_data( $data ) {
97 $this->data = $data;
98 }
99
100 /**
101 * @param string $feed_name
102 *
103 * @since 2.0
104 */
105 public function set_feed_name( $feed_name ) {
106 $this->feed_name = $feed_name;
107 }
108
109 /**
110 * @param array $feed_db_data
111 *
112 * @return array
113 *
114 * @since 2.0
115 */
116 public function get_feed_db_data() {
117 return $this->feed_db_data;
118 }
119
120 /**
121 * Adds a new feed if there is no associated feed
122 * found. Otherwise updates the exiting feed.
123 *
124 * @return false|int
125 *
126 * @since 2.0
127 */
128 public function update_or_insert() {
129 $this->sanitize_and_sort_data();
130
131 if ( $this->exists_in_database() ) {
132 return $this->update();
133 } else {
134 return $this->insert();
135 }
136 }
137
138 /**
139 * Whether or not a feed exists with the
140 * associated insert ID
141 *
142 * @return bool
143 *
144 * @since 2.0
145 */
146 public function exists_in_database() {
147 if ( $this->is_legacy ) {
148 return true;
149 }
150
151 if ( $this->insert_id === false ) {
152 return false;
153 }
154
155 $args = array(
156 'id' => $this->insert_id
157 );
158
159 $results = CTF_Db::feeds_query( $args );
160
161 return isset( $results[0] );
162 }
163
164 /**
165 * Inserts a new feed from sanitized and sorted data.
166 * Some data is saved in the ctf_feeds table and some is
167 * saved in the ctf_feed_settings table.
168 *
169 * @return false|int
170 *
171 * @since 2.0
172 */
173 public function insert() {
174 if ( $this->is_legacy ) {
175 return $this->update();
176 }
177
178 if ( ! isset( $this->sanitized_and_sorted_data ) ) {
179 return false;
180 }
181
182 $settings_array = CTF_Feed_Saver::format_settings( $this->sanitized_and_sorted_data['feed_settings'] );
183
184 $this->sanitized_and_sorted_data['feeds'][] = array(
185 'key' => 'settings',
186 'values' => array( ctf_json_encode( $settings_array ) )
187 );
188
189 if ( ! empty( $this->feed_name ) ) {
190 $this->sanitized_and_sorted_data['feeds'][] = array(
191 'key' => 'feed_name',
192 'values' => array( $this->feed_name )
193 );
194 }
195
196 $this->sanitized_and_sorted_data['feeds'][] = array(
197 'key' => 'status',
198 'values' => array( 'publish' )
199 );
200
201 $insert_id = CTF_Db::feeds_insert( $this->sanitized_and_sorted_data['feeds'] );
202
203 if ( $insert_id ) {
204 $this->insert_id = $insert_id;
205
206 return $insert_id;
207 }
208
209 return false;
210 }
211
212 /**
213 * Updates an existing feed and related settings from
214 * sanitized and sorted data.
215 *
216 * @return false|int
217 *
218 * @since 2.0
219 */
220 public function update() {
221 if ( ! isset( $this->sanitized_and_sorted_data ) ) {
222 return false;
223 }
224
225 $args = array(
226 'id' => $this->insert_id
227 );
228
229 $settings_array = CTF_Feed_Saver::format_settings( $this->sanitized_and_sorted_data['feed_settings'] );
230
231 if ( $this->is_legacy ) {
232 $to_save_json = ctf_json_encode( $settings_array );
233 return update_option( 'ctf_legacy_feed_settings', $to_save_json, false );
234 }
235
236 $this->sanitized_and_sorted_data['feeds'][] = array(
237 'key' => 'settings',
238 'values' => array( ctf_json_encode( $settings_array ) )
239 );
240
241 $this->sanitized_and_sorted_data['feeds'][] = array(
242 'key' => 'feed_name',
243 'values' => [sanitize_text_field($this->feed_name)]
244 );
245
246 $success = CTF_Db::feeds_update( $this->sanitized_and_sorted_data['feeds'], $args );
247
248 return $success;
249 }
250
251 /**
252 * Converts settings that have been sanitized into an associative array
253 * that can be saved as JSON in the database
254 *
255 * @param $raw_settings
256 *
257 * @return array
258 *
259 * @since 2.0
260 */
261 public static function format_settings( $raw_settings ) {
262 $settings_array = array();
263 foreach ( $raw_settings as $single_setting ) {
264 if ( count( $single_setting['values'] ) > 1 ) {
265 $settings_array[ $single_setting['key'] ] = $single_setting['values'];
266
267 } else {
268 $settings_array[ $single_setting['key'] ] = isset( $single_setting['values'][0] ) ? $single_setting['values'][0] : '';
269 }
270 }
271
272 return $settings_array;
273 }
274
275 /**
276 * Gets the Preview Settings
277 * for the Feed Fly Preview
278 *
279 * @return array|bool
280 *
281 * @since 2.0
282 */
283 public function get_feed_preview_settings( $preview_settings ){
284
285 }
286
287 /**
288 * Retrieves and organizes feed setting data for easy use in
289 * the builder
290 *
291 * @return array|bool
292 *
293 * @since 2.0
294 */
295 public function get_feed_settings() {
296 if ( $this->is_legacy ) {
297 $twitter_feed_settings = new CTF_Settings( array(), ctf_get_database_settings(), array() );
298 $twitter_feed_settings->set_feed_type_and_terms();
299 //$twitter_feed_settings->set_transient_name();
300 $return = $twitter_feed_settings->get_settings();
301
302 $this->feed_db_data = array(
303 'id' => 'legacy',
304 'feed_name' => __( 'Legacy Feeds', 'custom-twitter-feeds' ),
305 'feed_title' => __( 'Legacy Feeds', 'custom-twitter-feeds' ),
306 'status' => 'publish',
307 'last_modified' => date( 'Y-m-d H:i:s' ),
308 );
309 } else if ( empty( $this->insert_id ) ) {
310 return false;
311 } else {
312 $args = array(
313 'id' => $this->insert_id,
314 );
315 $settings_db_data = CTF_Db::feeds_query( $args );
316 if ( false === $settings_db_data || sizeof($settings_db_data) == 0) {
317 return false;
318 }
319 $this->feed_db_data = array(
320 'id' => $settings_db_data[0]['id'],
321 'feed_name' => $settings_db_data[0]['feed_name'],
322 'feed_title' => $settings_db_data[0]['feed_title'],
323 'status' => $settings_db_data[0]['status'],
324 'last_modified' => $settings_db_data[0]['last_modified'],
325 );
326
327 $return = json_decode( $settings_db_data[0]['settings'], true );
328 $return['feed_name'] = $settings_db_data[0]['feed_name'];
329 }
330 $return = wp_parse_args( $return, CTF_Feed_Saver::settings_defaults() );
331
332 // Ensure dateformat is always a string for proper select matching in the customizer.
333 if ( isset( $return['dateformat'] ) ) {
334 $return['dateformat'] = (string) $return['dateformat'];
335 }
336
337 return $return;
338 }
339
340
341
342 /**
343 * Retrieves and organizes feed setting data for easy use in
344 * the builder
345 * It will NOT get the settings from the DB, but from the Customizer builder
346 * To be used for updating feed preview on the fly
347 *
348 * @return array|bool
349 *
350 * @since 2.0
351 */
352 public function get_feed_settings_preview( $settings_db_data ) {
353 if ( false === $settings_db_data || sizeof($settings_db_data) == 0) {
354 return false;
355 }
356 $return = $settings_db_data;
357 $return = wp_parse_args( $return, CTF_Feed_Saver::settings_defaults() );
358
359 return $return;
360 }
361
362
363
364 /**
365 * Default settings, $return_array equalling false will return
366 * the settings in the general way that the "CTF_Shortcode" class,
367 * "ctf_get_processed_options" method does
368 *
369 * @param bool $return_array
370 *
371 * @return array
372 *
373 * @since 2.0
374 */
375 public static function settings_defaults( $return_array = true ) {
376 {
377 $translations = get_option( 'ctf_options', array() );
378
379 $final_translations = [];
380 $final_translations['retweetedtext'] = isset( $translations['retweetedtext'] ) ? stripslashes( esc_attr( $translations['retweetedtext'] ) ) : __( 'Retweeted', 'custom-twitter-feeds' );
381 $final_translations['inreplytotext'] = isset( $translations['inreplytotext'] ) ? stripslashes( esc_attr( $translations['inreplytotext'] ) ) : __( 'In Reply To', 'custom-twitter-feeds' );
382 $final_translations['buttontext'] = isset( $translations['buttontext'] ) ? stripslashes( esc_attr( $translations['buttontext'] ) ) : __( 'Load More', 'custom-twitter-feeds' );
383 $final_translations['minutestext'] = isset( $translations['minutestext'] ) ? stripslashes( esc_attr( $translations['minutestext'] ) ) : __( 'Minutes', 'custom-twitter-feeds' );
384 $final_translations['hoursetext'] = isset( $translations['hoursetext'] ) ? stripslashes( esc_attr( $translations['hoursetext'] ) ) : __( 'Hours', 'custom-twitter-feeds' );
385 $final_translations['nowtext'] = isset( $translations['nowtext'] ) ? stripslashes( esc_attr( $translations['nowtext'] ) ) : __( 'Now', 'custom-twitter-feeds' );
386
387
388
389
390 $defaults = array(
391 //V2
392 'customizer' => false,
393
394 'ajax_theme' => false,
395 'have_own_tokens' => '',
396 'use_own_consumer' => '',
397 #'preserve_settings' => '',
398
399 'usertimeline_includereplies' => '',
400 'hometimeline_includereplies' => '',
401 'mentionstimeline_includereplies' => '',
402 'usertimeline_includeretweets' => true,
403 'hometimeline_includeretweets' => true,
404 'mentionstimeline_includeretweets' => '',
405 'tab' => 'configure',
406 'consumer_key' => '',
407 'consumer_secret' => '',
408 'access_token' => '',
409 'access_token_secret' => '',
410 'type' => 'usertimeline',
411 'usertimeline_text' => '',
412 'hashtag_text' => '',
413 //'search_text' => '',
414 //'lists_id' => '',
415 //'lists_owner' => '',
416 'num' => '5',
417 #'cache_time' => '1',
418 #'cache_time_unit' => '3600',
419 //'lists_info' => '{}',
420
421 'includereplies' => false,
422 'includeretweets' => true,
423 'width_mobile_no_fixed' => false,
424
425 'include_replied_to' => true,
426
427 'include_retweeter' => true,
428 'include_author' => true,
429 'include_avatar' => true,
430 'include_author_text' => true, //NEW
431 'include_text' => true,
432 'include_date' => true,
433 'include_actions' => true,
434 'include_linkbox' => true,
435 'include_media' => true,
436 'include_twittercards' => true,
437 'include_logo' => true,
438
439 'include_twitterlink' => true,
440
441
442 'creditctf' => false,
443 'showbutton' => true,
444 'showheader' => true,
445 'persistentcache' => true,
446 'selfreplies' => false,
447 'autores' => true,
448 'disableintents' => false,
449 'customtemplates' => false,
450 'shorturls' => false,
451 'curlcards' => true,
452 'sslonly' => false,
453 'disablelightbox' => false,
454 'masonry' => false,
455 'carousel' => false,
456 'carouselnavarrows' => true,
457 'carouselpag' => false,
458 'carouselautoplay' => false,
459 'autoscroll' => false,
460 'width' => '100',
461 'width_unit' => '%',
462 'height' => '',
463 'height_unit' => '%',
464 'class' => '',
465 'layout' => 'list',
466 'masonrycols' => '3',
467 'masonrytabletcols' => '2', //NEW
468 'masonrymobilecols' => '1',
469 'carouselrows' => '1', //NEW
470 'carouselcols' => '3',
471 'carouseltabletcols' => '2', //NEW
472 'carouselmobilecols' => '1',
473 'carouselloop' => 'rewind',
474 'carouselarrows' => 'onhover',
475 'carouselheight' => 'tallest',
476 'carouseltime' => '5000',
477 'maxmedia' => '4',
478 'imagecols' => 'auto',
479 'autoscrolldistance' => '200',
480 'includewords' => '',
481 'excludewords' => '',
482 'includeanyall' => 'any',
483 'filterandor' => 'and',
484 'excludeanyall' => 'any',
485 'remove_by_id' => '',
486 'custom_css' => '',
487 'custom_js' => '',
488 'request_method' => 'auto',
489 'cron_cache_clear' => 'unset',
490 'multiplier' => '1.25',
491 'font_method' => 'svg',
492 'include_media_placeholder' => true,
493 'showbio' => true,
494 'disablelinks' => false,
495 'linktexttotwitter' => false,
496 'bgcolor' => '#',
497
498 'headertextcolor' => '#', //OLD
499 'headertext' => '', //OLD
500 'headersize' => 'small', //OLD
501
502 'headerstyle' => 'standard', //NEW
503 'headerbgcolor' => '#',
504 'customheadertextcolor' => '#',
505 'customheadertext' => ctf_should_rebrand_to_x() ? __('We are on X', 'custom-twitter-feeds') : __('We are on Twitter', 'custom-twitter-feeds'),
506 'customheadersize' => 'small',
507
508
509 'timezone' => 'default',
510 'dateformat' => '1',
511 'datecustom' => '',
512 'mtime' => '',
513 'htime' => '',
514 'nowtime' => '',
515 'datetextsize' => 'inherit',
516 'datetextweight' => 'inherit',
517 'datetextcolor' => '#',
518 'authortextcolor' => '#',
519 'authortextsize' => 'inherit',
520 'authortextweight' => 'inherit',
521 'logosize' => 'inherit',
522 'logocolor' => '#',
523 'tweettextsize' => 'inherit',
524 'tweettextweight' => 'inherit',
525 'textcolor' => '#',
526 'textlength' => '280',
527 'retweetedtext' => $final_translations['retweetedtext'],
528 'linktextcolor' => '#',
529 'quotedauthorsize' => 'inherit',
530 'quotedauthorweight' => 'inherit',
531 'iconsize' => 'inherit',
532 'iconcolor' => '#',
533 'viewtwitterlink' => true, //NEW
534 'twitterlinktext' => ctf_should_rebrand_to_x() ? 'X' : 'Twitter',
535 'buttoncolor' => '#',
536 'buttonhovercolor' => '#',
537 'buttontextcolor' => '#',
538 'buttontext' => $final_translations['buttontext'],
539 'inreplytotext' => $final_translations['inreplytotext'],
540
541 'feedtemplate' => 'default',// NEW
542 'cardstextsize' => 'inherit', // NEW
543 'cardstextcolor' => '#', // NEW
544
545 'colorpalette' => 'inherit', // NEW
546 'custombgcolor'=> '', // NEW
547 'customaccentcolor'=> '', // NEW
548 'customtextcolor1' => '', // NEW
549 'customtextcolor2' => '', // NEW
550 'customlinkcolor' => '', // NEW
551
552 'tweetpoststyle' => 'regular',
553 'tweetbgcolor' => '#fff',
554 'tweetcorners' => '5',
555 'tweetboxshadow' => true,
556 'tweetsepcolor' => '#ddd', // NEW
557 'tweetsepsize' => '1',
558 'tweetsepline' => true
559 );
560
561 $defaults = CTF_Feed_Saver::filter_defaults( $defaults );
562
563 // some settings are comma separated and not arrays when the feed is created
564 if ( $return_array ) {
565 $settings_with_multiples = array(
566 );
567
568 foreach ( $settings_with_multiples as $multiple_key ) {
569 if ( isset( $defaults[ $multiple_key ] ) ) {
570 $defaults[ $multiple_key ] = explode( ',', $defaults[ $multiple_key ] );
571 }
572 }
573 }
574
575 return $defaults;
576 }
577 }
578
579 /**
580 * Provides backwards compatibility for extensions
581 *
582 * @param array $defaults
583 *
584 * @return array
585 *
586 * @since 2.0
587 */
588 public static function filter_defaults( $defaults ) {
589
590 return $defaults;
591 }
592
593 /**
594 * Saves settings for legacy feeds. Runs on first update automatically.
595 *
596 * @since 2.0
597 */
598 public static function set_legacy_feed_settings() {
599 $to_save = CTF_Post_Set::legacy_to_builder_convert();
600
601 $to_save_json = ctf_json_encode( $to_save );
602
603 update_option( 'ctf_legacy_feed_settings', $to_save_json, false );
604 }
605
606 /**
607 * Used for taking raw post data related to settings
608 * an sanitizing it and sorting it to easily use in
609 * the database tables
610 *
611 * @since 2.0
612 */
613 private function sanitize_and_sort_data() {
614 $data = $this->data;
615
616 $sanitized_and_sorted = array(
617 'feeds' => array(),
618 'feed_settings' => array()
619 );
620
621 foreach ( $data as $key => $value ) {
622 $data_type = CTF_Feed_Saver_Manager::get_data_type( $key );
623 $sanitized_values = array();
624 if ( is_array( $value ) ) {
625 foreach ( $value as $item ) {
626 $type = CTF_Feed_Saver_Manager::is_boolean( $item ) ? 'boolean' : $data_type['sanitization'];
627 $sanitized_values[] = CTF_Feed_Saver_Manager::sanitize( $type , $item );
628 }
629 } else {
630 $type = CTF_Feed_Saver_Manager::is_boolean( $value ) ? 'boolean' : $data_type['sanitization'];
631 $sanitized_values[] = CTF_Feed_Saver_Manager::sanitize( $type, $value );
632 }
633
634 $single_sanitized = array(
635 'key' => $key,
636 'values' => $sanitized_values
637 );
638
639 $sanitized_and_sorted[ $data_type['table'] ][] = $single_sanitized;
640 }
641
642 $this->sanitized_and_sorted_data = $sanitized_and_sorted;
643 }
644 }