Controls
2 weeks ago
Tabs
2 weeks ago
CFF_Builder_Customizer.php
2 weeks ago
CFF_Db.php
2 weeks ago
CFF_Feed_Builder.php
2 weeks ago
CFF_Feed_Saver.php
2 weeks ago
CFF_Feed_Saver_Manager.php
2 weeks ago
CFF_Post_Set.php
2 weeks ago
CFF_Source.php
2 weeks ago
CFF_Theme_CSS.php
2 weeks ago
CFF_Tooltip_Wizard.php
2 weeks ago
index.php
2 weeks ago
CFF_Post_Set.php
389 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Custom Facebook Feed Feed Post Set |
| 5 | * |
| 6 | * @since 4.0 |
| 7 | */ |
| 8 | |
| 9 | namespace CustomFacebookFeed\Builder; |
| 10 | |
| 11 | use CustomFacebookFeed\CFF_Cache_System; |
| 12 | use CustomFacebookFeed\CFF_Parse; |
| 13 | |
| 14 | if (!defined('ABSPATH')) { |
| 15 | exit; // Exit if accessed directly |
| 16 | } |
| 17 | |
| 18 | class CFF_Post_Set |
| 19 | { |
| 20 | /** |
| 21 | * @var int |
| 22 | */ |
| 23 | private $feed_id; |
| 24 | |
| 25 | /** |
| 26 | * @var array |
| 27 | */ |
| 28 | private $feed_settings; |
| 29 | |
| 30 | /** |
| 31 | * @var array |
| 32 | */ |
| 33 | private $converted_settings; |
| 34 | |
| 35 | /** |
| 36 | * @var string |
| 37 | */ |
| 38 | private $transient_name; |
| 39 | |
| 40 | /** |
| 41 | * @var array|object |
| 42 | */ |
| 43 | private $data; |
| 44 | |
| 45 | /** |
| 46 | * @var array|object |
| 47 | */ |
| 48 | private $comments_data; |
| 49 | |
| 50 | public function __construct($feed_id) |
| 51 | { |
| 52 | $this->feed_id = $feed_id; |
| 53 | $this->transient_name = '*' . $feed_id; |
| 54 | |
| 55 | $this->data = array(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @return array|object |
| 60 | * |
| 61 | * @since 4.0 |
| 62 | */ |
| 63 | public function get_data() |
| 64 | { |
| 65 | return $this->data; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return array|object |
| 70 | * |
| 71 | * @since 4.0 |
| 72 | */ |
| 73 | public function get_comments_data() |
| 74 | { |
| 75 | return $this->comments_data; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return array |
| 80 | * |
| 81 | * @since 4.0 |
| 82 | */ |
| 83 | public function get_feed_settings() |
| 84 | { |
| 85 | return $this->feed_settings; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return array |
| 90 | * |
| 91 | * @since 4.0 |
| 92 | */ |
| 93 | public function get_converted_settings() |
| 94 | { |
| 95 | return $this->converted_settings; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sets the settings in builder form as well as converted |
| 100 | * settings for general use in the plugin |
| 101 | * |
| 102 | * @since 4.0 |
| 103 | */ |
| 104 | public function init($customizerBuilder = false, $previewSettings = false) |
| 105 | { |
| 106 | $saver = new CFF_Feed_Saver($this->feed_id); |
| 107 | if ($customizerBuilder && $previewSettings != false) { |
| 108 | if (isset($previewSettings['album'])) { |
| 109 | $previewSettings['album'] = CFF_Source::extract_id($previewSettings['album'], 'album'); |
| 110 | } |
| 111 | if (isset($previewSettings['playlist'])) { |
| 112 | $previewSettings['playlist'] = CFF_Source::extract_id($previewSettings['playlist'], 'playlist'); |
| 113 | } |
| 114 | $this->feed_settings = $previewSettings; |
| 115 | } else { |
| 116 | $this->feed_settings = $saver->get_feed_settings(); |
| 117 | } |
| 118 | |
| 119 | $this->converted_settings = CFF_Post_Set::builder_to_general_settings_convert($this->feed_settings); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Gathers posts from the API until the minimum number of posts |
| 124 | * for the feed are retrieved then stores the results |
| 125 | * |
| 126 | * @since 4.0 |
| 127 | */ |
| 128 | public function fetch() |
| 129 | { |
| 130 | $settings = $this->converted_settings; |
| 131 | $transient = $settings['type'] === 'events' ? $this->feed_id : $this->transient_name; |
| 132 | |
| 133 | $facebook_feed = new \CustomFacebookFeed\CFF_Feed_Pro($this->transient_name, true); |
| 134 | |
| 135 | if ($facebook_feed->need_posts($settings['num']) && $facebook_feed->can_get_more_posts()) { |
| 136 | while ($facebook_feed->need_posts($settings['num']) && $facebook_feed->can_get_more_posts()) { |
| 137 | $facebook_feed->add_remote_posts($settings, $this->feed_id); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $post_data = $facebook_feed->get_post_data(); |
| 142 | $this->data = $post_data; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Gathers comments for posts. |
| 147 | * |
| 148 | * @return array |
| 149 | * |
| 150 | * @since 4.0 |
| 151 | */ |
| 152 | public function fetch_comments() |
| 153 | { |
| 154 | if (empty($this->data)) { |
| 155 | return array(); |
| 156 | } |
| 157 | |
| 158 | $settings = $this->converted_settings; |
| 159 | |
| 160 | $comments = []; |
| 161 | foreach ($this->data as $single_post) { |
| 162 | $id = CFF_Parse::get_post_id($single_post); |
| 163 | $json_object = \CustomFacebookFeed\CFF_Utils::cff_fetchUrl("https://graph.facebook.com/" . $id . "/?fields=comments.limit(5){created_time,from{name,id,picture{url},link},id,message,message_tags,attachment,like_count}&access_token=" . $settings['accesstoken']); |
| 164 | $comments_return = json_decode($json_object); |
| 165 | if (isset($comments_return->comments->data)) { |
| 166 | $comments[ $id ] = $comments_return->comments->data; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | $this->comments_data = $comments; |
| 171 | |
| 172 | return $comments; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Converts raw settings from the cff_feed_settings table into the |
| 177 | * more general way that the "CFF_Shortcode" class, |
| 178 | * "cff_get_processed_options" method does |
| 179 | * |
| 180 | * @param array $builder_settings |
| 181 | * |
| 182 | * @return array |
| 183 | * |
| 184 | * @since 4.0 |
| 185 | */ |
| 186 | public static function builder_to_general_settings_convert($builder_settings) |
| 187 | { |
| 188 | $settings_with_multiples = array( |
| 189 | 'type', |
| 190 | 'include', |
| 191 | 'exclude', |
| 192 | 'filter', |
| 193 | 'exfilter' |
| 194 | ); |
| 195 | |
| 196 | foreach ($settings_with_multiples as $array_setting) { |
| 197 | if (is_array($builder_settings[ $array_setting ])) { |
| 198 | $builder_settings[ $array_setting ] = implode(',', $builder_settings[ $array_setting ]); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (isset($builder_settings['sources']) && is_array($builder_settings['sources'])) { |
| 203 | $access_tokens = array(); |
| 204 | $ids = array(); |
| 205 | $id_access_tokens = array(); |
| 206 | $sources_setting = array(); |
| 207 | foreach ($builder_settings['sources'] as $source) { |
| 208 | $source_array = array(); |
| 209 | if (! is_array($source)) { |
| 210 | $args = array( 'id' => $source ); |
| 211 | if (isset($builder_settings['feedtype']) && $builder_settings['feedtype'] == 'events') { |
| 212 | $args['privilege'] = 'events'; |
| 213 | } |
| 214 | $source_query = \CustomFacebookFeed\Builder\CFF_Db::source_query($args); |
| 215 | |
| 216 | if (isset($source_query[0])) { |
| 217 | $source_array = $source_query[0]; |
| 218 | $sources_setting[] = $source_query[0]; |
| 219 | } |
| 220 | } else { |
| 221 | $source_array = $source; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | if (! empty($source_array)) { |
| 226 | $access_tokens[] = $source_array['access_token']; |
| 227 | $ids[] = $source_array['account_id']; |
| 228 | $id_access_tokens[ $source_array['account_id'] ] = $source_array['access_token']; |
| 229 | $builder_settings['pagetype'] = $source_array['account_type']; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (! empty($sources_setting)) { |
| 234 | $builder_settings['sources'] = $sources_setting; |
| 235 | } |
| 236 | |
| 237 | if (count($builder_settings['sources']) > 1) { |
| 238 | $builder_settings['accesstoken'] = $id_access_tokens; |
| 239 | $builder_settings['id'] = implode(',', $ids); |
| 240 | } else { |
| 241 | $builder_settings['accesstoken'] = implode(',', $access_tokens); |
| 242 | $builder_settings['id'] = implode(',', $ids); |
| 243 | } |
| 244 | } |
| 245 | $builder_settings['multifeedactive'] = \CustomFacebookFeed\CFF_FB_Settings::check_active_extension('multifeed'); |
| 246 | // $builder_settings['daterangeactive'] = \CustomFacebookFeed\CFF_FB_Settings::check_active_extension( 'date_range' ); |
| 247 | $builder_settings['daterangeactive'] = false; // Not sure why plugin things date range is active when not |
| 248 | $builder_settings['featuredpostactive'] = \CustomFacebookFeed\CFF_FB_Settings::check_active_extension('featured_post'); |
| 249 | $builder_settings['albumactive'] = \CustomFacebookFeed\CFF_FB_Settings::check_active_extension('album'); |
| 250 | $builder_settings['carouselactive'] = \CustomFacebookFeed\CFF_FB_Settings::check_active_extension('carousel'); |
| 251 | $builder_settings['reviewsactive'] = \CustomFacebookFeed\CFF_FB_Settings::check_active_extension('reviews'); |
| 252 | |
| 253 | |
| 254 | if ( |
| 255 | empty($builder_settings['pagetoken']) |
| 256 | || (is_array($builder_settings['pagetoken']) && empty($builder_settings['pagetoken'][0]) ) |
| 257 | ) { |
| 258 | $builder_settings['pagetoken'] = $builder_settings['accesstoken']; |
| 259 | } |
| 260 | |
| 261 | if ($builder_settings['feedtype'] === 'reviews') { |
| 262 | $builder_settings['type'] = 'review'; |
| 263 | } |
| 264 | |
| 265 | return $builder_settings; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Convert settings from 3.x for use in the builder in 4.0+ |
| 270 | * |
| 271 | * @param array $atts |
| 272 | * |
| 273 | * @return array |
| 274 | * |
| 275 | * @since 4.0.3 |
| 276 | */ |
| 277 | public static function legacy_to_builder_convert($atts = array()) |
| 278 | { |
| 279 | $options = get_option('cff_style_settings', array()); |
| 280 | $legacy_feed_settings_obj = new \CustomFacebookFeed\CFF_FB_Settings($atts, $options); |
| 281 | |
| 282 | $processed_settings = $legacy_feed_settings_obj->get_settings(); |
| 283 | |
| 284 | $settings_with_multiples = CFF_Post_Set::get_settings_with_multiple(); |
| 285 | |
| 286 | foreach ($settings_with_multiples as $multiple_key) { |
| 287 | if ( |
| 288 | isset($processed_settings[ $multiple_key ]) |
| 289 | && ! is_array($processed_settings[ $multiple_key ]) |
| 290 | ) { |
| 291 | $processed_settings[ $multiple_key ] = explode(',', $processed_settings[ $multiple_key ]); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if ( |
| 296 | $processed_settings['cols'] > 1 |
| 297 | || $processed_settings['colsmobile'] > 1 |
| 298 | ) { |
| 299 | $processed_settings['feedlayout'] = 'masonry'; |
| 300 | } else { |
| 301 | $processed_settings['feedlayout'] = 'list'; |
| 302 | } |
| 303 | $processed_settings['feedtype'] = 'timeline'; |
| 304 | |
| 305 | if (! in_array('likebox', $processed_settings['include'])) { |
| 306 | $processed_settings['showlikebox'] = 'off'; |
| 307 | } |
| 308 | if (! in_array('author', $processed_settings['include'])) { |
| 309 | $processed_settings['showauthornew'] = false; |
| 310 | } else { |
| 311 | $processed_settings['showauthornew'] = true; |
| 312 | } |
| 313 | if (! in_array('link', $processed_settings['include'])) { |
| 314 | $processed_settings['showfacebooklink'] = ''; |
| 315 | $processed_settings['showsharelink'] = ''; |
| 316 | } else { |
| 317 | $processed_settings['showfacebooklink'] = 'on'; |
| 318 | $processed_settings['showsharelink'] = 'on'; |
| 319 | } |
| 320 | $processed_settings['showfacebooklink'] = $processed_settings['showfacebooklink'] === 'on' || $processed_settings['showfacebooklink'] === 'true' ? 'on' : ''; |
| 321 | $processed_settings['showsharelink'] = $processed_settings['showsharelink'] === 'on' || $processed_settings['showsharelink'] === 'true' ? 'on' : ''; |
| 322 | |
| 323 | $processed_settings['textlength'] = get_option('cff_title_length', '400'); |
| 324 | $processed_settings['desclength'] = get_option('cff_body_length', '200'); |
| 325 | |
| 326 | $processed_settings['num'] = isset($processed_settings['num']) ? (string)$processed_settings['num'] : '5'; |
| 327 | |
| 328 | $processed_settings = CFF_Post_Set::filter_builder_settings($processed_settings); |
| 329 | |
| 330 | return $processed_settings; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Settings that can include an array of values |
| 335 | * |
| 336 | * @return array |
| 337 | * |
| 338 | * @since 4.0.3 |
| 339 | */ |
| 340 | public static function get_settings_with_multiple() |
| 341 | { |
| 342 | $settings_with_multiples = array( |
| 343 | 'type', |
| 344 | 'include', |
| 345 | 'exclude', |
| 346 | ); |
| 347 | |
| 348 | if (\CustomFacebookFeed\CFF_Utils::cff_is_pro_version()) { |
| 349 | $settings_with_multiples = \CustomFacebookFeed\Builder\Pro\CFF_Post_Set_Pro::add_pro_settings_with_multiple($settings_with_multiples); |
| 350 | } |
| 351 | |
| 352 | return $settings_with_multiples; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Used for changing the settings used for general front end feeds |
| 357 | * |
| 358 | * @param array $builder_settings |
| 359 | * |
| 360 | * @return array |
| 361 | * |
| 362 | * @since 4.0.3 |
| 363 | */ |
| 364 | public static function filter_general_settings($builder_settings) |
| 365 | { |
| 366 | if (\CustomFacebookFeed\CFF_Utils::cff_is_pro_version()) { |
| 367 | $builder_settings = \CustomFacebookFeed\Builder\Pro\CFF_Post_Set_Pro::add_general_pro_settings($builder_settings); |
| 368 | } |
| 369 | return $builder_settings; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Used for changing the settings for feeds being edited in the customizer |
| 374 | * |
| 375 | * @param array $processed_settings |
| 376 | * |
| 377 | * @return array |
| 378 | * |
| 379 | * @since 4.0.3 |
| 380 | */ |
| 381 | public static function filter_builder_settings($processed_settings) |
| 382 | { |
| 383 | if (\CustomFacebookFeed\CFF_Utils::cff_is_pro_version()) { |
| 384 | $processed_settings = \CustomFacebookFeed\Builder\Pro\CFF_Post_Set_Pro::add_builder_pro_settings($processed_settings); |
| 385 | } |
| 386 | return $processed_settings; |
| 387 | } |
| 388 | } |
| 389 |