Admin
4 weeks ago
Builder
4 weeks ago
Integrations
4 weeks ago
SmashTwitter
4 weeks ago
UsageTracking
4 weeks ago
V2
4 weeks ago
blocks
4 weeks ago
CTF_Display_Elements.php
4 weeks ago
CTF_Feed.php
4 weeks ago
CTF_Feed_Locator.php
4 weeks ago
CTF_GDPR_Integrations.php
4 weeks ago
CTF_Parse.php
4 weeks ago
CTF_Settings.php
4 weeks ago
CtfAdmin.php
4 weeks ago
CtfCache.php
4 weeks ago
CtfFeed.php
4 weeks ago
CtfOauthConnect.php
4 weeks ago
SB_Twitter_Cron_Updater.php
4 weeks ago
admin-hooks.php
4 weeks ago
ctf-functions.php
4 weeks ago
notices.php
4 weeks ago
widget.php
4 weeks ago
ctf-functions.php
402 lines
| 1 | <?php |
| 2 | |
| 3 | use TwitterFeed\CTF_Feed; |
| 4 | use TwitterFeed\CtfFeed; |
| 5 | use TwitterFeed\CTF_Settings; |
| 6 | use TwitterFeed\CTF_GDPR_Integrations; |
| 7 | use TwitterFeed\Admin\CTF_Global_Settings; |
| 8 | |
| 9 | function ctf_should_rebrand_to_x() { |
| 10 | $ctf_settings = wp_parse_args(get_option('ctf_options'), (new CTF_Global_Settings)->default_settings_options()); |
| 11 | return isset( $ctf_settings['rebranding'] ) && $ctf_settings['rebranding'] === true; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Reduces a user-supplied value to a whitespace-separated list of CSS class names. |
| 16 | * |
| 17 | * Characters that carry meaning in HTML markup are removed rather than encoded, so the |
| 18 | * result is safe to concatenate into a class list. Characters that are legal in modern |
| 19 | * class names -- ':' (variant prefixes), '/', '.', '-' and '_' -- are preserved so |
| 20 | * utility-framework and namespaced class names survive unchanged. |
| 21 | * |
| 22 | * @param mixed $value Raw class list. |
| 23 | * |
| 24 | * @return string Sanitized, whitespace-normalized class list. |
| 25 | * |
| 26 | * @since 2.7.1 |
| 27 | */ |
| 28 | function ctf_sanitize_css_class_list( $value ) { |
| 29 | if ( ! is_scalar( $value ) ) { |
| 30 | return ''; |
| 31 | } |
| 32 | |
| 33 | $value = wp_strip_all_tags( (string) $value ); |
| 34 | |
| 35 | // Control characters become separators rather than being deleted, so that a newline |
| 36 | // or tab between two class names cannot silently merge them into one. |
| 37 | // Bails to an empty class list on malformed UTF-8. |
| 38 | $stripped = preg_replace( '/[^\P{C}]/u', ' ', $value ); |
| 39 | if ( null === $stripped ) { |
| 40 | return ''; |
| 41 | } |
| 42 | |
| 43 | $stripped = preg_replace( '/[<>"\'`=\\\\]/', '', $stripped ); |
| 44 | $stripped = preg_replace( '/\s+/', ' ', (string) $stripped ); |
| 45 | |
| 46 | return trim( (string) $stripped ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * May include support for templates in theme folders in the future |
| 51 | * |
| 52 | * @return string full path to template |
| 53 | * |
| 54 | * @since 2.0 custom templates supported |
| 55 | */ |
| 56 | function ctf_get_feed_template_part( $part, $settings = array() ) { |
| 57 | $options = get_option( 'ctf_options', array() ); |
| 58 | $file = ''; |
| 59 | $settings['customtemplates'] = isset( $options['customtemplates'] ) ? $options['customtemplates'] !== 'false' : false; |
| 60 | |
| 61 | $using_custom_templates_in_theme = apply_filters( 'ctf_use_theme_templates', $settings['customtemplates'] ); |
| 62 | $generic_path = trailingslashit( CTF_PLUGIN_DIR ) . 'templates/'; |
| 63 | |
| 64 | if ( $using_custom_templates_in_theme == true ) { |
| 65 | $custom_header_template = locate_template( 'ctf/header.php', false, false ); |
| 66 | $custom_header_generic_template = locate_template( 'ctf/header-generic.php', false, false ); |
| 67 | $custom_header_text_template = locate_template( 'ctf/header-text.php', false, false ); |
| 68 | $custom_item_template = locate_template( 'ctf/item.php', false, false ); |
| 69 | $custom_footer_template = locate_template( 'ctf/footer.php', false, false ); |
| 70 | $custom_feed_template = locate_template( 'ctf/feed.php', false, false ); |
| 71 | $custom_author_template = locate_template( 'ctf/author.php', false, false ); |
| 72 | $custom_linkbox_template = locate_template( 'ctf/linkbox.php', false, false ); |
| 73 | $custom_actions_template = locate_template( 'ctf/actions.php', false, false ); |
| 74 | } else { |
| 75 | $custom_header_template = false; |
| 76 | $custom_header_generic_template = false; |
| 77 | $custom_item_template = false; |
| 78 | $custom_footer_template = false; |
| 79 | $custom_feed_template = false; |
| 80 | $custom_author_template = false; |
| 81 | $custom_linkbox_template = false; |
| 82 | $custom_actions_template = false; |
| 83 | } |
| 84 | |
| 85 | if ( $part === 'header' ) { |
| 86 | if ( $custom_header_template ) { |
| 87 | $file = $custom_header_template; |
| 88 | } else { |
| 89 | $file = $generic_path . 'header.php'; |
| 90 | } |
| 91 | } elseif ( $part === 'header-generic' ) { |
| 92 | if ( $custom_header_generic_template ) { |
| 93 | $file = $custom_header_generic_template; |
| 94 | } else { |
| 95 | $file = $generic_path . 'header-generic.php'; |
| 96 | } |
| 97 | } if ( $part === 'header-text' ) { |
| 98 | if ( $custom_header_generic_template ) { |
| 99 | $file = $custom_header_text_template; |
| 100 | } else { |
| 101 | $file = $generic_path . 'header-text.php'; |
| 102 | } |
| 103 | } elseif ( $part === 'item' ) { |
| 104 | if ( $custom_item_template ) { |
| 105 | $file = $custom_item_template; |
| 106 | } else { |
| 107 | $file = $generic_path . 'item.php'; |
| 108 | } |
| 109 | } elseif ( $part === 'footer' ) { |
| 110 | if ( $custom_footer_template ) { |
| 111 | $file = $custom_footer_template; |
| 112 | } else { |
| 113 | $file = $generic_path . 'footer.php'; |
| 114 | } |
| 115 | } elseif ( $part === 'feed' ) { |
| 116 | if ( $custom_feed_template ) { |
| 117 | $file = $custom_feed_template; |
| 118 | } else { |
| 119 | $file = $generic_path . 'feed.php'; |
| 120 | } |
| 121 | } elseif ( $part === 'author' ) { |
| 122 | if ( $custom_author_template ) { |
| 123 | $file = $custom_author_template; |
| 124 | } else { |
| 125 | $file = $generic_path . 'author.php'; |
| 126 | } |
| 127 | } elseif ( $part === 'linkbox' ) { |
| 128 | if ( $custom_linkbox_template ) { |
| 129 | $file = $custom_linkbox_template; |
| 130 | } else { |
| 131 | $file = $generic_path . 'linkbox.php'; |
| 132 | } |
| 133 | } elseif ( $part === 'actions' ) { |
| 134 | if ( $custom_actions_template ) { |
| 135 | $file = $custom_actions_template; |
| 136 | } else { |
| 137 | $file = $generic_path . 'actions.php'; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return $file; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Check if it's Customizer |
| 146 | * |
| 147 | * @since 2.0 |
| 148 | */ |
| 149 | function ctf_doing_customizer( $settings ) { |
| 150 | return !empty( $settings['customizer'] ) && $settings['customizer'] == true; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @return int |
| 155 | */ |
| 156 | function ctf_get_utc_offset() { |
| 157 | return get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | function ctf_current_user_can( $cap ) { |
| 162 | if ( current_user_can( 'manage_twitter_feed_options' ) && 'manage_twitter_feed_options' === $cap ) { |
| 163 | $cap = 'manage_twitter_feed_options'; |
| 164 | } elseif ( current_user_can( 'manage_custom_twitter_feeds_options' ) && 'manage_custom_twitter_feeds_options' === $cap ) { |
| 165 | $cap = 'manage_custom_twitter_feeds_options'; |
| 166 | } |
| 167 | |
| 168 | $cap = apply_filters( 'ctf_settings_pages_capability', $cap ); |
| 169 | |
| 170 | return current_user_can( $cap ); |
| 171 | } |
| 172 | |
| 173 | function ctf_get_manage_options_cap() { |
| 174 | $cap = 'manage_options'; |
| 175 | if ( current_user_can( 'manage_twitter_feed_options' ) ) { |
| 176 | $cap = 'manage_twitter_feed_options'; |
| 177 | } elseif ( current_user_can( 'manage_custom_twitter_feeds_options' ) ) { |
| 178 | $cap = 'manage_custom_twitter_feeds_options'; |
| 179 | } |
| 180 | $cap = apply_filters( 'ctf_settings_pages_capability', $cap ); |
| 181 | |
| 182 | return $cap; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | function ctf_clear_cache_sql() { |
| 187 | global $wpdb; |
| 188 | $table_name = $wpdb->prefix . 'options'; |
| 189 | $result = $wpdb->query( |
| 190 | " |
| 191 | DELETE |
| 192 | FROM $table_name |
| 193 | WHERE `option_name` LIKE ('%\_transient\_ctf\_%') |
| 194 | " |
| 195 | ); |
| 196 | $wpdb->query( |
| 197 | " |
| 198 | DELETE |
| 199 | FROM $table_name |
| 200 | WHERE `option_name` LIKE ('%\_transient\_timeout\_ctf\_%') |
| 201 | " |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | function ctf_background_processing() { |
| 208 | |
| 209 | if ( ! isset( $_POST['feed_id'] ) ) { |
| 210 | return; |
| 211 | } |
| 212 | $feed_id = sanitize_text_field( $_POST['feed_id'] ); |
| 213 | |
| 214 | $atts_raw = isset( $_POST['atts'] ) ? json_decode( stripslashes( $_POST['atts'] ), true ) : array(); |
| 215 | if ( is_array( $atts_raw ) ) { |
| 216 | array_map( 'sanitize_text_field', $atts_raw ); |
| 217 | } else { |
| 218 | $atts_raw = array(); |
| 219 | } |
| 220 | $atts = $atts_raw; // now sanitized |
| 221 | |
| 222 | $location = isset( $_POST['location'] ) && in_array( $_POST['location'], array( 'header', 'footer', 'sidebar', 'content' ), true ) ? sanitize_text_field( $_POST['location'] ) : 'unknown'; |
| 223 | $post_id = isset( $_POST['post_id'] ) && $_POST['post_id'] !== 'unknown' ? (int) $_POST['post_id'] : 'unknown'; |
| 224 | $feed_details = array( |
| 225 | 'feed_id' => $feed_id, |
| 226 | 'atts' => $atts, |
| 227 | 'location' => array( |
| 228 | 'post_id' => $post_id, |
| 229 | 'html' => $location, |
| 230 | ), |
| 231 | ); |
| 232 | |
| 233 | ctf_do_background_tasks( $feed_details ); |
| 234 | |
| 235 | |
| 236 | |
| 237 | //$twitter_feed_settings = new CTF_Settings_Pro( $atts ); |
| 238 | //$twitter_feed_settings->set_feed_type_and_terms(); |
| 239 | //$tw_settings = $twitter_feed_settings->get_settings(); |
| 240 | $twitter_feed_object = CtfFeed::init( $atts, '', 0, array(), 0 ); |
| 241 | $tw_settings = $twitter_feed_object->feed_options; |
| 242 | |
| 243 | $twitter_feed = new CTF_Feed( $feed_id ); |
| 244 | $twitter_feed->set_cache( $tw_settings['cache_time'], $tw_settings ); |
| 245 | $posts = array(); |
| 246 | if ( $twitter_feed->regular_cache_exists() ) { |
| 247 | $twitter_feed->set_post_data_from_cache(); |
| 248 | $posts = $twitter_feed->get_post_data(); |
| 249 | } |
| 250 | |
| 251 | $twitter_return['resizing'] = 'success'; |
| 252 | |
| 253 | echo wp_json_encode( $twitter_return ); |
| 254 | |
| 255 | wp_die(); |
| 256 | } |
| 257 | add_action( 'wp_ajax_ctf_background_processing', 'ctf_background_processing' ); |
| 258 | add_action( 'wp_ajax_nopriv_ctf_background_processing', 'ctf_background_processing' ); |
| 259 | |
| 260 | |
| 261 | |
| 262 | /** |
| 263 | * Triggered by a cron event to update feeds |
| 264 | * |
| 265 | * @since 2.0 |
| 266 | */ |
| 267 | function ctf_cron_updater() { |
| 268 | $settings = ctf_get_database_settings(); |
| 269 | if ( ! empty( $settings['ctf_caching_type'] ) && $settings['ctf_caching_type'] === 'page' ) { |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | $cron_updater = new TwitterFeed\SB_Twitter_Cron_Updater(); |
| 274 | $cron_updater->do_feed_updates(); |
| 275 | ctf_do_background_tasks( array() ); |
| 276 | } |
| 277 | add_action( 'ctf_feed_update', 'ctf_cron_updater' ); |
| 278 | |
| 279 | |
| 280 | function ctf_maybe_ajax_theme_html( $twitter_feed, $feed_id ) { |
| 281 | $options = ctf_get_database_settings(); |
| 282 | if ( $options['ajax_theme'] ) { |
| 283 | echo TwitterFeed\CTF_Display_Elements::get_ajax_code( $options ); |
| 284 | } |
| 285 | } |
| 286 | add_action( 'ctf_before_feed_end', 'ctf_maybe_ajax_theme_html', 10, 2 ); |
| 287 | |
| 288 | /** |
| 289 | * Debug report added at the end of the feed when sbi_debug query arg is added to a page |
| 290 | * that has the feed on it. |
| 291 | * |
| 292 | * @param object $twitter_feed |
| 293 | * @param string $feed_id |
| 294 | */ |
| 295 | function ctf_debug_report( $twitter_feed, $feed_id ) { |
| 296 | |
| 297 | if ( ! isset( $_GET['sbi_debug'] ) && ! isset( $_GET['sb_debug'] ) ) { |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | $settings_obj = new CTF_Settings( array(), array() ); |
| 302 | |
| 303 | $settings = $twitter_feed->feed_options; |
| 304 | |
| 305 | $public_settings_keys = CTF_Settings::get_public_db_settings_keys(); |
| 306 | ?> |
| 307 | |
| 308 | <p>Status</p> |
| 309 | <ul> |
| 310 | <li>Time: <?php echo esc_html( date( 'Y-m-d H:i:s', time() ) ); ?></li> |
| 311 | |
| 312 | </ul> |
| 313 | <p>Settings</p> |
| 314 | <ul> |
| 315 | <?php |
| 316 | foreach ( $public_settings_keys as $key ) : |
| 317 | if ( isset( $settings[ $key ] ) ) : |
| 318 | ?> |
| 319 | <li> |
| 320 | <small><?php echo esc_html( $key ); ?>:</small> |
| 321 | <?php |
| 322 | if ( ! is_array( $settings[ $key ] ) ) : |
| 323 | echo esc_html( $settings[ $key ] ); |
| 324 | else : |
| 325 | ?> |
| 326 | <ul> |
| 327 | <?php |
| 328 | foreach ( $settings[ $key ] as $sub_key => $value ) { |
| 329 | echo '<li><small>' . esc_html( $sub_key ) . ':</small> ' . esc_html( $value ) . '</li>'; |
| 330 | } |
| 331 | ?> |
| 332 | </ul> |
| 333 | <?php endif; ?> |
| 334 | </li> |
| 335 | |
| 336 | <?php |
| 337 | endif; |
| 338 | endforeach; |
| 339 | ?> |
| 340 | </ul> |
| 341 | <p>GDPR</p> |
| 342 | <ul> |
| 343 | <?php |
| 344 | $statuses = CTF_GDPR_Integrations::statuses(); |
| 345 | foreach ( $statuses as $status_key => $value ) : |
| 346 | ?> |
| 347 | <li> |
| 348 | <small><?php echo esc_html( $status_key ); ?>:</small> |
| 349 | <?php |
| 350 | if ( $value == 1 ) { |
| 351 | echo 'success'; |
| 352 | } else { |
| 353 | echo 'failed'; } |
| 354 | ?> |
| 355 | </li> |
| 356 | |
| 357 | <?php endforeach; ?> |
| 358 | <li> |
| 359 | <small>Enabled:</small> |
| 360 | <?php echo CTF_GDPR_Integrations::doing_gdpr( $settings ); ?> |
| 361 | </li> |
| 362 | </ul> |
| 363 | <?php |
| 364 | } |
| 365 | add_action( 'ctf_before_feed_end', 'ctf_debug_report', 99, 2 ); |
| 366 | |
| 367 | function ctf_oauth_url( $page ) { |
| 368 | $nonce = wp_create_nonce( 'ctf_con' ); |
| 369 | |
| 370 | $admin_email = get_option( 'admin_email', '' ); |
| 371 | if ( is_user_logged_in() ) { |
| 372 | $current_user = wp_get_current_user(); |
| 373 | $user_email = $current_user->user_email; |
| 374 | } |
| 375 | $user_email = isset( $user_email ) ? $user_email : $admin_email; |
| 376 | $params = array( |
| 377 | 'state' => $page, |
| 378 | 'wordpress_user' => $user_email, |
| 379 | 'con_nonce' => $nonce, |
| 380 | 'vn' => CTF_VERSION |
| 381 | ); |
| 382 | |
| 383 | return add_query_arg( $params, OAUTH_PROCESSOR_URL ); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Summary of parse_json_data |
| 388 | * |
| 389 | * @param mixed $data |
| 390 | * |
| 391 | * @return array |
| 392 | */ |
| 393 | function parse_json_data($data) |
| 394 | { |
| 395 | $json = json_decode($data, true); |
| 396 | if (is_string($json)) { |
| 397 | $json = json_decode($json, true); |
| 398 | } |
| 399 | return is_array($json) ? $json : []; |
| 400 | } |
| 401 | |
| 402 |