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