Controls
4 weeks ago
Tabs
4 weeks ago
CFF_Builder_Customizer.php
4 weeks ago
CFF_Db.php
4 weeks ago
CFF_Feed_Builder.php
4 weeks ago
CFF_Feed_Saver.php
4 weeks ago
CFF_Feed_Saver_Manager.php
4 weeks ago
CFF_Post_Set.php
4 weeks ago
CFF_Source.php
4 weeks ago
CFF_Theme_CSS.php
4 weeks ago
CFF_Tooltip_Wizard.php
4 weeks ago
index.php
4 weeks ago
CFF_Feed_Saver_Manager.php
758 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Custom Facebook Feed Feed Saver Manager |
| 5 | * |
| 6 | * @since 4.0 |
| 7 | */ |
| 8 | |
| 9 | namespace CustomFacebookFeed\Builder; |
| 10 | |
| 11 | if (!defined('ABSPATH')) { |
| 12 | exit; // Exit if accessed directly |
| 13 | } |
| 14 | |
| 15 | class CFF_Feed_Saver_Manager |
| 16 | { |
| 17 | /** |
| 18 | * AJAX hooks for various feed data related functionality |
| 19 | * |
| 20 | * @since 4.0 |
| 21 | */ |
| 22 | public static function hooks() |
| 23 | { |
| 24 | add_action('wp_ajax_cff_feed_saver_manager_builder_update', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'builder_update' )); |
| 25 | add_action('wp_ajax_cff_feed_saver_manager_get_feed_settings', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'get_feed_settings' )); |
| 26 | add_action('wp_ajax_cff_feed_saver_manager_get_feed_list_page', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'get_feed_list_page' )); |
| 27 | add_action('wp_ajax_cff_feed_saver_manager_get_locations_page', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'get_locations_page' )); |
| 28 | add_action('wp_ajax_cff_feed_saver_manager_delete_feeds', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'delete_feed' )); |
| 29 | add_action('wp_ajax_cff_feed_saver_manager_duplicate_feed', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'duplicate_feed' )); |
| 30 | add_action('wp_ajax_cff_feed_saver_manager_clear_single_feed_cache', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'clear_single_feed_cache' )); |
| 31 | add_action('wp_ajax_cff_feed_saver_manager_importer', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'importer' )); |
| 32 | add_action('wp_ajax_cff_feed_saver_manager_fly_preview', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'feed_customizer_fly_preview' )); |
| 33 | add_action('wp_ajax_cff_feed_saver_manager_retrieve_comments', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'retrieve_comments' )); |
| 34 | add_action('wp_ajax_cff_feed_saver_manager_delete_source', array( 'CustomFacebookFeed\Builder\CFF_Feed_Saver_Manager', 'delete_source' )); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Used in an AJAX call to update settings for a particular feed. |
| 39 | * Can also be used to create a new feed if no feed_id sent in |
| 40 | * $_POST data. |
| 41 | * |
| 42 | * @since 4.0 |
| 43 | */ |
| 44 | public static function builder_update() |
| 45 | { |
| 46 | check_ajax_referer('cff-admin', 'nonce'); |
| 47 | |
| 48 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 49 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 50 | if (! current_user_can($cap)) { |
| 51 | wp_send_json_error(); // This auto-dies. |
| 52 | } |
| 53 | |
| 54 | |
| 55 | $settings_data = $_POST; |
| 56 | |
| 57 | $feed_id = false; |
| 58 | if (! empty($settings_data['feed_id'])) { |
| 59 | $feed_id = sanitize_text_field($settings_data['feed_id']); |
| 60 | unset($settings_data['feed_id']); |
| 61 | } elseif (isset($settings_data['feed_id'])) { |
| 62 | unset($settings_data['feed_id']); |
| 63 | } |
| 64 | unset($settings_data['action']); |
| 65 | |
| 66 | if (! isset($settings_data['feed_name'])) { |
| 67 | $settings_data['feed_name'] = ''; |
| 68 | } |
| 69 | |
| 70 | // Check if New |
| 71 | if (isset($settings_data['new_insert']) && $settings_data['new_insert'] == 'true' && isset($settings_data['sourcename'])) { |
| 72 | $settings_data['feed_name'] = CFF_Db::feeds_query_name($settings_data['sourcename']); |
| 73 | |
| 74 | $default_grid = [ |
| 75 | 'albums', |
| 76 | 'videos', |
| 77 | 'photos', |
| 78 | 'singlealbum' |
| 79 | ]; |
| 80 | $feed_type = $settings_data['feedtype']; |
| 81 | if (in_array($feed_type, $default_grid)) { |
| 82 | $settings_data['feedlayout'] = 'grid'; |
| 83 | } |
| 84 | } |
| 85 | unset($settings_data['new_insert']); |
| 86 | unset($settings_data['sourcename']); |
| 87 | $feed_name = ''; |
| 88 | if (isset($settings_data['update_feed']) && $settings_data['update_feed'] == 'true') { |
| 89 | $settings_data['settings']['sources'] = $_POST['sources']; |
| 90 | $feed_name = sanitize_text_field(wp_unslash($settings_data['feed_name'])); |
| 91 | $settings_data = $settings_data['settings']; |
| 92 | } |
| 93 | if (isset($settings_data['album'])) { |
| 94 | $settings_data['album'] = CFF_Source::extract_id($settings_data['album'], 'album'); |
| 95 | } |
| 96 | if (isset($settings_data['playlist'])) { |
| 97 | $settings_data['playlist'] = CFF_Source::extract_id($settings_data['playlist'], 'playlist'); |
| 98 | } |
| 99 | |
| 100 | unset($settings_data['accesstoken']); |
| 101 | $feed_saver = new CFF_Feed_Saver($feed_id); |
| 102 | $feed_saver->set_feed_name($feed_name); |
| 103 | $feed_saver->set_data($settings_data); |
| 104 | $return = array( |
| 105 | 'success' => false, |
| 106 | 'feed_id' => false |
| 107 | ); |
| 108 | |
| 109 | if ($feed_saver->update_or_insert()) { |
| 110 | $return = array( |
| 111 | 'success' => true, |
| 112 | 'feed_id' => $feed_saver->get_feed_id() |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | $feed_cache = new \CustomFacebookFeed\CFF_Cache($feed_id); |
| 117 | $feed_cache->clear('all'); |
| 118 | $feed_cache->clear('posts'); |
| 119 | |
| 120 | if ( |
| 121 | isset($_POST['include_post_set']) && |
| 122 | ! empty($_POST['include_post_set']) |
| 123 | ) { |
| 124 | $post_set = new CFF_Post_Set($feed_saver->get_feed_id()); |
| 125 | |
| 126 | $post_set->init(); |
| 127 | $post_set->fetch(); |
| 128 | |
| 129 | $return['posts'] = $post_set->get_data(); |
| 130 | } |
| 131 | |
| 132 | if ( |
| 133 | isset($_POST['include_header']) && |
| 134 | ! empty($_POST['include_header']) |
| 135 | ) { |
| 136 | if (! isset($post_set)) { |
| 137 | $post_set = new CFF_Post_Set($feed_saver->get_feed_id()); |
| 138 | |
| 139 | $post_set->init(); |
| 140 | } |
| 141 | |
| 142 | $header_details = array(); |
| 143 | $settings = $post_set->get_feed_settings(); |
| 144 | |
| 145 | if (isset($settings['sources'][0])) { |
| 146 | $args = array( |
| 147 | 'id' => $settings['sources'][0] |
| 148 | ); |
| 149 | $results = CFF_Db::source_query($args); |
| 150 | |
| 151 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data($results[0]['account_id'], $results[0]['account_type'] === 'group', $results[0]['access_token'], 0, false, ''); |
| 152 | } |
| 153 | |
| 154 | $return['header'] = $header_details; |
| 155 | } |
| 156 | |
| 157 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode($return); |
| 158 | wp_die(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Retrieve comments AJAX call |
| 163 | * |
| 164 | * @since 4.0 |
| 165 | */ |
| 166 | public static function retrieve_comments() |
| 167 | { |
| 168 | check_ajax_referer('cff-admin', 'nonce'); |
| 169 | |
| 170 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 171 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 172 | if (! current_user_can($cap)) { |
| 173 | wp_send_json_error(); // This auto-dies. |
| 174 | } |
| 175 | |
| 176 | if (empty($_POST['feed_id'])) { |
| 177 | echo '{}'; |
| 178 | wp_die(); |
| 179 | } |
| 180 | |
| 181 | $return = []; |
| 182 | |
| 183 | $feed_id = $_POST['feed_id']; |
| 184 | $feed_saver = new CFF_Feed_Saver($feed_id); |
| 185 | $settings = $feed_saver->get_feed_settings(); |
| 186 | if ($settings != false) { |
| 187 | $post_set = new CFF_Post_Set($feed_id); |
| 188 | $post_set->init(); |
| 189 | $post_set->fetch(); |
| 190 | |
| 191 | $return = $post_set->fetch_comments(); |
| 192 | } |
| 193 | |
| 194 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode($return); |
| 195 | wp_die(); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Used in an AJAX call to delete feeds from the Database |
| 201 | * $_POST data. |
| 202 | * |
| 203 | * @since 4.0 |
| 204 | */ |
| 205 | public static function delete_feed() |
| 206 | { |
| 207 | check_ajax_referer('cff-admin', 'nonce'); |
| 208 | |
| 209 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 210 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 211 | if (! current_user_can($cap)) { |
| 212 | wp_send_json_error(); // This auto-dies. |
| 213 | } |
| 214 | |
| 215 | if (! empty($_POST['feeds_ids']) && is_array($_POST['feeds_ids'])) { |
| 216 | CFF_Db::delete_feeds_query($_POST['feeds_ids']); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** |
| 222 | * Used in an AJAX call to delete Soureces from the Database |
| 223 | * $_POST data. |
| 224 | * |
| 225 | * @since 4.0 |
| 226 | */ |
| 227 | public static function delete_source() |
| 228 | { |
| 229 | check_ajax_referer('cff_admin_nonce', 'nonce'); |
| 230 | |
| 231 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 232 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 233 | if (! current_user_can($cap)) { |
| 234 | wp_send_json_error(); // This auto-dies. |
| 235 | } |
| 236 | |
| 237 | if (! empty($_POST['source_id'])) { |
| 238 | CFF_Db::delete_source_query($_POST['source_id']); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Used in an AJAX call to delete a feed cache from the Database |
| 244 | * $_POST data. |
| 245 | * |
| 246 | * @since 4.0 |
| 247 | */ |
| 248 | public static function clear_single_feed_cache() |
| 249 | { |
| 250 | check_ajax_referer('cff-admin', 'nonce'); |
| 251 | |
| 252 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 253 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 254 | if (! current_user_can($cap)) { |
| 255 | wp_send_json_error(); // This auto-dies. |
| 256 | } |
| 257 | |
| 258 | $feed_id = sanitize_text_field($_POST['feedID']); |
| 259 | |
| 260 | if ($feed_id === 'legacy') { |
| 261 | \CustomFacebookFeed\CFF_Cache::clear_legacy(); |
| 262 | \CustomFacebookFeed\CFF_Cache::clear_page_caches(); |
| 263 | } else { |
| 264 | if (\CustomFacebookFeed\CFF_FB_Settings::check_active_extension('multifeed')) { |
| 265 | \CustomFacebookFeed\CFF_Cache::clear_legacy(); |
| 266 | \CustomFacebookFeed\CFF_Cache::clear_page_caches(); |
| 267 | } |
| 268 | |
| 269 | $feed_cache = new \CustomFacebookFeed\CFF_Cache($feed_id); |
| 270 | |
| 271 | $feed_cache->clear('all'); |
| 272 | $feed_cache->clear('posts'); |
| 273 | } |
| 274 | |
| 275 | CFF_Feed_Saver_Manager::feed_customizer_fly_preview(); |
| 276 | wp_die(); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Used in an AJAX call to duplicate a Feed |
| 281 | * $_POST data. |
| 282 | * |
| 283 | * @since 4.0 |
| 284 | */ |
| 285 | public static function duplicate_feed() |
| 286 | { |
| 287 | check_ajax_referer('cff-admin', 'nonce'); |
| 288 | |
| 289 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 290 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 291 | if (! current_user_can($cap)) { |
| 292 | wp_send_json_error(); // This auto-dies. |
| 293 | } |
| 294 | |
| 295 | if (! empty($_POST['feed_id'])) { |
| 296 | CFF_Db::duplicate_feed_query($_POST['feed_id']); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /** |
| 302 | * Import a feed from JSON data |
| 303 | * |
| 304 | * @since 4.0 |
| 305 | */ |
| 306 | public static function importer() |
| 307 | { |
| 308 | check_ajax_referer('cff-admin', 'nonce'); |
| 309 | |
| 310 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 311 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 312 | if (! current_user_can($cap)) { |
| 313 | wp_send_json_error(); // This auto-dies. |
| 314 | } |
| 315 | |
| 316 | if (! empty($_POST['feed_json']) && strpos($_POST['feed_json'], '{') === 0) { |
| 317 | echo json_encode(CFF_Feed_Saver_Manager::import_feed(stripslashes($_POST['feed_json']))); |
| 318 | } else { |
| 319 | echo json_encode(array( 'success' => false, 'message' => __('Invalid JSON. Must have brackets "{}"', 'custom-facebook-feed') )); |
| 320 | } |
| 321 | wp_die(); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | /** |
| 326 | * Used To check if it's customizer Screens |
| 327 | * Returns Feed info or false! |
| 328 | * |
| 329 | * @param bool $include_comments |
| 330 | * |
| 331 | * @return array|bool |
| 332 | * |
| 333 | * @since 4.0 |
| 334 | */ |
| 335 | public static function maybe_feed_customizer_data($include_comments = false) |
| 336 | { |
| 337 | |
| 338 | |
| 339 | if (isset($_GET['feed_id'])) { |
| 340 | $feed_id = $_GET['feed_id']; |
| 341 | $feed_saver = new CFF_Feed_Saver($feed_id); |
| 342 | $settings = $feed_saver->get_feed_settings(); |
| 343 | $feed_db_data = $feed_saver->get_feed_db_data(); |
| 344 | |
| 345 | if ($settings != false) { |
| 346 | $return = array( |
| 347 | 'feed_info' => $feed_db_data, |
| 348 | 'settings' => $settings, |
| 349 | 'posts' => array() |
| 350 | ); |
| 351 | $post_set = new CFF_Post_Set($feed_id); |
| 352 | $post_set->init(); |
| 353 | $post_set->fetch(); |
| 354 | $return['posts'] = $post_set->get_data(); |
| 355 | |
| 356 | if (! isset($post_set)) { |
| 357 | $post_set = new CFF_Post_Set($feed_saver->get_feed_id()); |
| 358 | $post_set->init(); |
| 359 | } |
| 360 | |
| 361 | $header_details = array(); |
| 362 | $settings = $post_set->get_feed_settings(); |
| 363 | |
| 364 | if (isset($settings['sources'][0])) { |
| 365 | $results = $settings['sources']; |
| 366 | |
| 367 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data($results[0]['account_id'], $results[0]['account_type'] === 'group', $results[0]['access_token'], 0, false, ''); |
| 368 | } |
| 369 | $return['header'] = $header_details; |
| 370 | |
| 371 | if ( |
| 372 | ! empty($return['posts']) |
| 373 | && $include_comments |
| 374 | ) { |
| 375 | $post_set->fetch_comments(); |
| 376 | $return['comments'] = $post_set->get_comments_data(); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | return $return; |
| 381 | } |
| 382 | } |
| 383 | return false; |
| 384 | } |
| 385 | /** |
| 386 | * Used to retrieve Feed Posts for preview screen |
| 387 | * Returns Feed info or false! |
| 388 | * |
| 389 | * @since 4.0 |
| 390 | */ |
| 391 | public static function feed_customizer_fly_preview() |
| 392 | { |
| 393 | check_ajax_referer('cff-admin', 'nonce'); |
| 394 | |
| 395 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 396 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 397 | if (! current_user_can($cap)) { |
| 398 | wp_send_json_error(); // This auto-dies. |
| 399 | } |
| 400 | |
| 401 | if (isset($_POST['feedID']) && isset($_POST['previewSettings'])) { |
| 402 | $feed_id = filter_var($_POST['feedID'], FILTER_VALIDATE_INT); |
| 403 | |
| 404 | $return = array( |
| 405 | 'posts' => array() |
| 406 | ); |
| 407 | $post_set = new CFF_Post_Set($feed_id); |
| 408 | $post_set->init(true, $_POST['previewSettings']); |
| 409 | $post_set->fetch(); |
| 410 | |
| 411 | $return['posts'] = $post_set->get_data(); |
| 412 | |
| 413 | $header_details = array(); |
| 414 | $settings = $post_set->get_feed_settings(); |
| 415 | |
| 416 | if (isset($settings['sources'][0])) { |
| 417 | $args = array( |
| 418 | 'id' => $settings['sources'][0]['account_id'] |
| 419 | ); |
| 420 | $results = CFF_Db::source_query($args); |
| 421 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data($results[0]['account_id'], $results[0]['account_type'] === 'group', $results[0]['access_token'], 0, false, ''); |
| 422 | } |
| 423 | $return['header'] = $header_details; |
| 424 | |
| 425 | echo json_encode($return); |
| 426 | } |
| 427 | die(); |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Used in AJAX call to return settings for an existing feed. |
| 433 | * |
| 434 | * @since 4.0 |
| 435 | */ |
| 436 | public static function get_feed_settings() |
| 437 | { |
| 438 | check_ajax_referer('cff-admin', 'nonce'); |
| 439 | |
| 440 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 441 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 442 | if (! current_user_can($cap)) { |
| 443 | wp_send_json_error(); // This auto-dies. |
| 444 | } |
| 445 | |
| 446 | $feed_id = ! empty($_POST['feed_id']) ? $_POST['feed_id'] : false; |
| 447 | |
| 448 | if (! $feed_id) { |
| 449 | wp_die('no feed id'); |
| 450 | } |
| 451 | |
| 452 | $feed_saver = new CFF_Feed_Saver($feed_id); |
| 453 | $settings = $feed_saver->get_feed_settings(); |
| 454 | |
| 455 | $return = array( |
| 456 | 'settings' => $settings, |
| 457 | 'posts' => array() |
| 458 | ); |
| 459 | |
| 460 | if ( |
| 461 | isset($_POST['include_post_set']) && |
| 462 | ! empty($_POST['include_post_set']) |
| 463 | ) { |
| 464 | $post_set = new CFF_Post_Set($feed_id); |
| 465 | |
| 466 | $post_set->init(); |
| 467 | $post_set->fetch(); |
| 468 | |
| 469 | $return['posts'] = $post_set->get_data(); |
| 470 | } |
| 471 | |
| 472 | if ( |
| 473 | isset($_POST['include_header']) && |
| 474 | ! empty($_POST['include_header']) |
| 475 | ) { |
| 476 | if (! isset($post_set)) { |
| 477 | $post_set = new CFF_Post_Set($feed_saver->get_feed_id()); |
| 478 | |
| 479 | $post_set->init(); |
| 480 | } |
| 481 | |
| 482 | $header_details = array(); |
| 483 | $settings = $post_set->get_feed_settings(); |
| 484 | |
| 485 | if (isset($settings['sources'][0])) { |
| 486 | $args = array( |
| 487 | 'id' => $settings['sources'][0] |
| 488 | ); |
| 489 | $results = CFF_Db::source_query($args); |
| 490 | |
| 491 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data($results[0]['account_id'], $results[0]['account_type'] === 'group', $results[0]['access_token'], 0, false, ''); |
| 492 | } |
| 493 | |
| 494 | $return['header'] = $header_details; |
| 495 | } |
| 496 | |
| 497 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode($return); |
| 498 | wp_die(); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Get a list of feeds with a limit and offset like a page |
| 503 | * |
| 504 | * @since 4.0 |
| 505 | */ |
| 506 | public static function get_feed_list_page() |
| 507 | { |
| 508 | check_ajax_referer('cff-admin', 'nonce'); |
| 509 | |
| 510 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 511 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 512 | if (! current_user_can($cap)) { |
| 513 | wp_send_json_error(); // This auto-dies. |
| 514 | } |
| 515 | |
| 516 | $args = array( 'page' => (int)$_POST['page'] ); |
| 517 | $feeds_data = CFF_Feed_Builder::get_feed_list($args); |
| 518 | |
| 519 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode($feeds_data); |
| 520 | |
| 521 | wp_die(); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Get a list of locations with a limit and offset like a page |
| 526 | * |
| 527 | * @since 4.0 |
| 528 | */ |
| 529 | public static function get_locations_page() |
| 530 | { |
| 531 | check_ajax_referer('cff-admin', 'nonce'); |
| 532 | |
| 533 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 534 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 535 | if (! current_user_can($cap)) { |
| 536 | wp_send_json_error(); // This auto-dies. |
| 537 | } |
| 538 | |
| 539 | $args = array( 'page' => (int)$_POST['page'] ); |
| 540 | |
| 541 | if (! empty($_POST['is_legacy'])) { |
| 542 | $args['feed_id'] = sanitize_text_field($_POST['feed_id']); |
| 543 | } else { |
| 544 | $args['feed_id'] = '*' . (int)$_POST['feed_id']; |
| 545 | } |
| 546 | $feeds_data = \CustomFacebookFeed\CFF_Feed_Locator::facebook_feed_locator_query($args); |
| 547 | |
| 548 | if (count($feeds_data) < CFF_Db::RESULTS_PER_PAGE) { |
| 549 | $args['html_location'] = array( 'footer', 'sidebar', 'header' ); |
| 550 | $args['group_by'] = 'html_location'; |
| 551 | $args['page'] = 1; |
| 552 | $non_content_data = \CustomFacebookFeed\CFF_Feed_Locator::facebook_feed_locator_query($args); |
| 553 | |
| 554 | $feeds_data = array_merge($feeds_data, $non_content_data); |
| 555 | } |
| 556 | |
| 557 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode($feeds_data); |
| 558 | |
| 559 | wp_die(); |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Return a single JSON string for importing a feed |
| 564 | * |
| 565 | * @param int $feed_id |
| 566 | * |
| 567 | * @return string |
| 568 | * |
| 569 | * @since 4.0 |
| 570 | */ |
| 571 | public static function get_export_json($feed_id) |
| 572 | { |
| 573 | $feed_saver = new CFF_Feed_Saver($feed_id); |
| 574 | $settings = $feed_saver->get_feed_settings(); |
| 575 | |
| 576 | return \CustomFacebookFeed\CFF_Utils::cff_json_encode($settings); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * All export strings for all feeds on the first 'page' |
| 581 | * |
| 582 | * @return array |
| 583 | * |
| 584 | * @since 4.0 |
| 585 | */ |
| 586 | public static function get_all_export_json() |
| 587 | { |
| 588 | $args = array( 'page' => 1 ); |
| 589 | |
| 590 | $feeds_data = CFF_Db::feeds_query($args); |
| 591 | |
| 592 | $return = array(); |
| 593 | foreach ($feeds_data as $single_feed) { |
| 594 | $return[ $single_feed['id'] ] = CFF_Feed_Saver_Manager::get_export_json($single_feed['id']); |
| 595 | } |
| 596 | |
| 597 | return $return; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Use a JSON string to import a feed with settings and sources. The return |
| 602 | * is whether or not the import was successful |
| 603 | * |
| 604 | * @param string $json |
| 605 | * |
| 606 | * @return array |
| 607 | * |
| 608 | * @since 4.0 |
| 609 | */ |
| 610 | public static function import_feed($json) |
| 611 | { |
| 612 | $settings_data = json_decode($json, true); |
| 613 | |
| 614 | $return = array( |
| 615 | 'success' => false, |
| 616 | 'message' => '' |
| 617 | ); |
| 618 | |
| 619 | if (empty($settings_data['sources'])) { |
| 620 | $return['message'] = __('No feed source is included. Cannot upload feed.', 'custom-facebook-feed'); |
| 621 | return $return; |
| 622 | } |
| 623 | |
| 624 | $sources = $settings_data['sources']; |
| 625 | |
| 626 | unset($settings_data['sources']); |
| 627 | |
| 628 | $settings_source = array(); |
| 629 | foreach ($sources as $source) { |
| 630 | if (isset($source['account_id'])) { |
| 631 | $settings_source[] = $source['account_id']; |
| 632 | $source_data = array( |
| 633 | 'access_token' => sanitize_text_field($source['access_token']), |
| 634 | 'id' => sanitize_text_field($source['account_id']), |
| 635 | 'type' => sanitize_text_field($source['account_type']), |
| 636 | 'privilege' => isset($source['privilege']) ? sanitize_text_field($source['privilege']) : '', |
| 637 | ); |
| 638 | |
| 639 | if (! empty($source['name'])) { |
| 640 | $source_data['name'] = sanitize_text_field($source['name']); |
| 641 | } |
| 642 | |
| 643 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data($source_data['id'], $source_data['type'] === 'group', $source_data['access_token'], 0, false, ''); |
| 644 | |
| 645 | if (isset($header_details->shortcode_options)) { |
| 646 | unset($header_details->shortcode_options); |
| 647 | } |
| 648 | |
| 649 | if (isset($header_details->name)) { |
| 650 | $source_data['name'] = $header_details->name; |
| 651 | } |
| 652 | $source_data['info'] = $header_details; |
| 653 | |
| 654 | // don't update or insert the access token if there is an API error |
| 655 | if (! isset($header_details->error)) { |
| 656 | CFF_Source::update_or_insert($source_data); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | $settings_data['sources'] = $settings_source; |
| 661 | $feed_saver = new CFF_Feed_Saver(false); |
| 662 | $feed_saver->set_data($settings_data); |
| 663 | |
| 664 | if ($feed_saver->update_or_insert()) { |
| 665 | $return = array( |
| 666 | 'success' => true, |
| 667 | 'feed_id' => $feed_saver->get_feed_id() |
| 668 | ); |
| 669 | |
| 670 | return $return; |
| 671 | } else { |
| 672 | $return['message'] = __('Could not import feed. Please try again', 'custom-facebook-feed'); |
| 673 | } |
| 674 | return $return; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Determines what table and sanitization should be used |
| 679 | * when handling feed setting data. |
| 680 | * |
| 681 | * TODO: Add settings that need something other than sanitize_text_field |
| 682 | * |
| 683 | * @param string $key |
| 684 | * |
| 685 | * @return array |
| 686 | * |
| 687 | * @since 4.0 |
| 688 | */ |
| 689 | public static function get_data_type($key) |
| 690 | { |
| 691 | switch ($key) { |
| 692 | case 'sources': |
| 693 | $return = array( |
| 694 | 'table' => 'feed_settings', |
| 695 | 'sanitization' => 'sanitize_text_field' |
| 696 | ); |
| 697 | break; |
| 698 | case 'feed_title': |
| 699 | $return = array( |
| 700 | 'table' => 'feeds', |
| 701 | 'sanitization' => 'sanitize_text_field' |
| 702 | ); |
| 703 | break; |
| 704 | case 'feed_name': |
| 705 | $return = array( |
| 706 | 'table' => 'feeds', |
| 707 | 'sanitization' => 'sanitize_text_field' |
| 708 | ); |
| 709 | break; |
| 710 | case 'status': |
| 711 | $return = array( |
| 712 | 'table' => 'feeds', |
| 713 | 'sanitization' => 'sanitize_text_field' |
| 714 | ); |
| 715 | break; |
| 716 | case 'author': |
| 717 | $return = array( |
| 718 | 'table' => 'feeds', |
| 719 | 'sanitization' => 'int' |
| 720 | ); |
| 721 | break; |
| 722 | default: |
| 723 | $return = array( |
| 724 | 'table' => 'feed_settings', |
| 725 | 'sanitization' => 'sanitize_text_field' |
| 726 | ); |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | return $return; |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Uses the appropriate sanitization function and returns the result |
| 735 | * for a value |
| 736 | * |
| 737 | * @param string $type |
| 738 | * @param int|string $value |
| 739 | * |
| 740 | * @return int|string |
| 741 | * |
| 742 | * @since 4.0 |
| 743 | */ |
| 744 | public static function sanitize($type, $value) |
| 745 | { |
| 746 | switch ($type) { |
| 747 | case 'int': |
| 748 | $return = intval($value); |
| 749 | break; |
| 750 | default: |
| 751 | $return = sanitize_text_field(wp_unslash($value)); |
| 752 | break; |
| 753 | } |
| 754 | |
| 755 | return $return; |
| 756 | } |
| 757 | } |
| 758 |