Admin.php
3 years ago
Ajax_Handler.php
2 years ago
Controls.php
2 years ago
Core.php
3 years ago
Elements.php
2 years ago
Enqueue.php
3 years ago
Facebook_Feed.php
3 years ago
Helper.php
2 years ago
Library.php
3 years ago
Login_Registration.php
2 years ago
Shared.php
5 years ago
Template_Query.php
4 years ago
Twitter_Feed.php
2 years ago
Woo_Product_Comparable.php
3 years ago
index.php
3 years ago
Facebook_Feed.php
295 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Essential_Addons_Elementor\Traits; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; |
| 7 | } // Exit if accessed directly |
| 8 | use \Essential_Addons_Elementor\Classes\Helper as HelperClass; |
| 9 | |
| 10 | trait Facebook_Feed { |
| 11 | /** |
| 12 | * Facebook Feed |
| 13 | * |
| 14 | * @param array $settings optional widget's settings |
| 15 | * |
| 16 | * @return false|string|void |
| 17 | * @since 3.4.0 |
| 18 | */ |
| 19 | public function facebook_feed_render_items( $settings = [] ) { |
| 20 | // check if ajax request |
| 21 | if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'facebook_feed_load_more' ) { |
| 22 | $ajax = wp_doing_ajax(); |
| 23 | // check ajax referer |
| 24 | check_ajax_referer( 'essential-addons-elementor', 'security' ); |
| 25 | |
| 26 | // init vars |
| 27 | $page = isset( $_POST['page'] ) ? intval( $_REQUEST['page'], 10 ) : 0; |
| 28 | if ( ! empty( $_POST['post_id'] ) ) { |
| 29 | $post_id = intval( $_POST['post_id'], 10 ); |
| 30 | } else { |
| 31 | $err_msg = __( 'Post ID is missing', 'essential-addons-for-elementor-lite' ); |
| 32 | if ( $ajax ) { |
| 33 | wp_send_json_error( $err_msg ); |
| 34 | } |
| 35 | |
| 36 | return false; |
| 37 | } |
| 38 | if ( ! empty( $_POST['widget_id'] ) ) { |
| 39 | $widget_id = sanitize_text_field( $_POST['widget_id'] ); |
| 40 | } else { |
| 41 | $err_msg = __( 'Widget ID is missing', 'essential-addons-for-elementor-lite' ); |
| 42 | if ( $ajax ) { |
| 43 | wp_send_json_error( $err_msg ); |
| 44 | } |
| 45 | |
| 46 | return false; |
| 47 | } |
| 48 | $settings = HelperClass::eael_get_widget_settings( $post_id, $widget_id ); |
| 49 | |
| 50 | } else { |
| 51 | // init vars |
| 52 | $page = 0; |
| 53 | $settings = ! empty( $settings ) ? $settings : $this->get_settings_for_display(); |
| 54 | } |
| 55 | |
| 56 | $html = ''; |
| 57 | $page_id = $settings['eael_facebook_feed_page_id']; |
| 58 | $token = $settings['eael_facebook_feed_access_token']; |
| 59 | $source = $settings['eael_facebook_feed_data_source']; |
| 60 | $display_comment = isset( $settings['eael_facebook_feed_comments'] ) ? $settings['eael_facebook_feed_comments'] : ''; |
| 61 | |
| 62 | if ( empty( $page_id ) || empty( $token ) ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | $key = 'eael_facebook_feed_' . md5( str_rot13( str_replace( '.', '', $source . $page_id . $token ) ) . $settings['eael_facebook_feed_cache_limit'] ); |
| 67 | $facebook_data = get_transient( $key ); |
| 68 | |
| 69 | if ( $facebook_data == false ) { |
| 70 | $facebook_data = wp_remote_retrieve_body( wp_remote_get( $this->get_url($page_id, $token, $source, $display_comment), [ |
| 71 | 'timeout' => 70, |
| 72 | ] ) ); |
| 73 | $facebook_data = json_decode( $facebook_data, true ); |
| 74 | if ( isset( $facebook_data['data'] ) ) { |
| 75 | set_transient( $key, $facebook_data, ( $settings['eael_facebook_feed_cache_limit'] * MINUTE_IN_SECONDS ) ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if ( ! isset( $facebook_data['data'] ) ) { |
| 80 | return; |
| 81 | } |
| 82 | $facebook_data = $facebook_data['data']; |
| 83 | |
| 84 | switch ( $settings['eael_facebook_feed_sort_by'] ) { |
| 85 | case 'least-recent': |
| 86 | $facebook_data = array_reverse( $facebook_data ); |
| 87 | break; |
| 88 | } |
| 89 | $items = array_splice( $facebook_data, ( $page * $settings['eael_facebook_feed_image_count']['size'] ), $settings['eael_facebook_feed_image_count']['size'] ); |
| 90 | $bg_style = isset( $settings['eael_facebook_feed_image_render_type'] ) && $settings['eael_facebook_feed_image_render_type'] == 'cover' ? "background-size: cover;background-position: center;background-repeat: no-repeat;" : "background-size: 100% 100%;background-repeat: no-repeat;"; |
| 91 | foreach ( $items as $item ) { |
| 92 | $t = 'eael_facebook_feed_message_max_length'; // short it |
| 93 | $limit = isset( $settings[ $t ] ) && isset( $settings[ $t ]['size'] ) ? $settings[ $t ]['size'] : null; |
| 94 | $message = wp_trim_words( ( isset( $item['message'] ) ? $item['message'] : ( isset( $item['story'] ) ? $item['story'] : '' ) ), $limit, '...' ); |
| 95 | $photo = ( isset( $item['full_picture'] ) ? esc_url( $item['full_picture'] ) : '' ); |
| 96 | $likes = ( isset( $item['reactions'] ) ? $item['reactions']['summary']['total_count'] : 0 ); |
| 97 | $comments = ( isset( $item['comments'] ) ? $item['comments']['summary']['total_count'] : 0 ); |
| 98 | |
| 99 | if ( empty( $photo ) ) { |
| 100 | $photo = isset( $item['attachments']['data'][0]['media']['image']['src'] ) ? esc_url( $item['attachments']['data'][0]['media']['image']['src'] ) : $photo; |
| 101 | } |
| 102 | |
| 103 | if ( $settings['eael_facebook_feed_layout'] == 'card' ) { |
| 104 | $item_form_name = ! empty( $item['from']['name'] ) ? $item['from']['name'] : ''; |
| 105 | $current_page_id = ! empty( $item['from']['id'] ) ? $item['from']['id'] : $page_id; |
| 106 | |
| 107 | $html .= '<div class="eael-facebook-feed-item"> |
| 108 | <div class="eael-facebook-feed-item-inner"> |
| 109 | <header class="eael-facebook-feed-item-header clearfix"> |
| 110 | <div class="eael-facebook-feed-item-user clearfix"> |
| 111 | <a href="https://www.facebook.com/' . $current_page_id . '" target="' . ( $settings['eael_facebook_feed_link_target'] == 'yes' ? '_blank' : '_self' ) . '"><img src="https://graph.facebook.com/v4.0/' . $current_page_id . '/picture" alt="' . esc_attr( $item_form_name ) . '" class="eael-facebook-feed-avatar"></a> |
| 112 | <a href="https://www.facebook.com/' . $current_page_id . '" target="' . ( $settings['eael_facebook_feed_link_target'] == 'yes' ? '_blank' : '_self' ) . '"><p class="eael-facebook-feed-username">' . esc_html( $item_form_name ) . '</p></a> |
| 113 | </div>'; |
| 114 | |
| 115 | if ( $settings['eael_facebook_feed_date'] ) { |
| 116 | $html .= '<a href="' . esc_url( $item['permalink_url'] ) . '" target="' . ( $settings['eael_facebook_feed_link_target'] ? '_blank' : '_self' ) . '" class="eael-facebook-feed-post-time"><i class="far fa-clock" aria-hidden="true"></i> ' . date_i18n( get_option('date_format'), strtotime( $item['created_time'] ) ) . '</a>'; |
| 117 | } |
| 118 | $html .= '</header>'; |
| 119 | |
| 120 | if ( $settings['eael_facebook_feed_message'] && ! empty( $message ) ) { |
| 121 | $html .= '<div class="eael-facebook-feed-item-content"> |
| 122 | <p class="eael-facebook-feed-message">' . $this->eael_str_check( $message ) . '</p> |
| 123 | </div>'; |
| 124 | } |
| 125 | |
| 126 | if ( ! empty( $photo ) || isset( $item['attachments']['data'] ) ) { |
| 127 | $html .= '<div class="eael-facebook-feed-preview-wrap">'; |
| 128 | if ( $item['status_type'] == 'shared_story' ) { |
| 129 | |
| 130 | if ( isset( $settings['eael_facebook_feed_is_show_preview_thumbnail'] ) && 'yes' == $settings['eael_facebook_feed_is_show_preview_thumbnail'] ) { |
| 131 | |
| 132 | $html .= '<a href="' . esc_url( $item['permalink_url'] ) . '" target="' . ( $settings['eael_facebook_feed_link_target'] == 'yes' ? '_blank' : '_self' ) . '" class="eael-facebook-feed-preview-img">'; |
| 133 | if ( !empty($item['attachments']['data'][0]['media_type']) && $item['attachments']['data'][0]['media_type'] == 'video' ) { |
| 134 | $html .= '<div class="eael-facebook-feed-img-container" style="background:url(' . esc_url( $photo ) . ');' . esc_attr( $bg_style ) . '"> |
| 135 | <img class="eael-facebook-feed-img" src="' . esc_url( $photo ) . '"></div> |
| 136 | <div class="eael-facebook-feed-preview-overlay"><i class="far fa-play-circle" aria-hidden="true"></i></div>'; |
| 137 | } else { |
| 138 | $html .= '<div class="eael-facebook-feed-img-container" style="background:url(' . esc_url( $photo ) . ');' . esc_attr( $bg_style ) . '"> |
| 139 | <img class="eael-facebook-feed-img" src="' . esc_url( $photo ) . '"></div>'; |
| 140 | } |
| 141 | $html .= '</a>'; |
| 142 | } |
| 143 | |
| 144 | $html .= '<div class="eael-facebook-feed-url-preview">'; |
| 145 | if ( isset( $settings['eael_facebook_feed_is_show_preview_host'] ) && 'yes' == $settings['eael_facebook_feed_is_show_preview_host'] && !empty($item['attachments']['data'][0]['unshimmed_url']) ) { |
| 146 | $html .= '<p class="eael-facebook-feed-url-host">' . parse_url( $item['attachments']['data'][0]['unshimmed_url'] )['host'] . '</p>'; |
| 147 | } |
| 148 | if ( isset( $settings['eael_facebook_feed_is_show_preview_title'] ) && 'yes' == $settings['eael_facebook_feed_is_show_preview_title'] ) { |
| 149 | $html .= '<h2 class="eael-facebook-feed-url-title">' . esc_html( $item['attachments']['data'][0]['title'] ) . '</h2>'; |
| 150 | } |
| 151 | |
| 152 | if ( isset( $settings['eael_facebook_feed_is_show_preview_description'] ) && 'yes' == $settings['eael_facebook_feed_is_show_preview_description'] ) { |
| 153 | $description = isset( $item['attachments']['data'][0]['description'] ) ? $item['attachments']['data'][0]['description'] : ''; |
| 154 | $html .= '<p class="eael-facebook-feed-url-description">' . $description . '</p>'; |
| 155 | } |
| 156 | $html .= '</div>'; |
| 157 | |
| 158 | } else if ( $item['status_type'] == 'added_video' ) { |
| 159 | if ( isset( $settings['eael_facebook_feed_is_show_preview_thumbnail'] ) && 'yes' == $settings['eael_facebook_feed_is_show_preview_thumbnail'] ) { |
| 160 | |
| 161 | $html .= '<a href="' . esc_url( $item['permalink_url'] ) . '" target="' . ( $settings['eael_facebook_feed_link_target'] == 'yes' ? '_blank' : '_self' ) . '" class="eael-facebook-feed-preview-img"> |
| 162 | <div class="eael-facebook-feed-img-container" style="background:url(' . esc_url( $photo ) . '); ' . esc_attr( $bg_style ) . '"> |
| 163 | <img class="eael-facebook-feed-img" src="' . esc_url( $photo ) . '"> |
| 164 | </div> |
| 165 | <div class="eael-facebook-feed-preview-overlay"><i class="far fa-play-circle" aria-hidden="true"></i></div> |
| 166 | </a>'; |
| 167 | } |
| 168 | } else { |
| 169 | if ( isset( $settings['eael_facebook_feed_is_show_preview_thumbnail'] ) && 'yes' == $settings['eael_facebook_feed_is_show_preview_thumbnail'] ) { |
| 170 | |
| 171 | $html .= '<a href="' . esc_url( $item['permalink_url'] ) . '" target="' . ( $settings['eael_facebook_feed_link_target'] == 'yes' ? '_blank' : '_self' ) . '" class="eael-facebook-feed-preview-img"> |
| 172 | <div class="eael-facebook-feed-img-container" style="background:url(' . esc_url( $photo ) . '); ' . esc_attr( $bg_style ) . '"> |
| 173 | <img class="eael-facebook-feed-img" src="' . esc_url( $photo ) . '"> |
| 174 | </div> |
| 175 | </a>'; |
| 176 | |
| 177 | } |
| 178 | } |
| 179 | $html .= '</div>'; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | if ( $settings['eael_facebook_feed_likes'] || $settings['eael_facebook_feed_comments'] ) { |
| 184 | $html .= '<footer class="eael-facebook-feed-item-footer"> |
| 185 | <div class="clearfix">'; |
| 186 | if ( $settings['eael_facebook_feed_likes'] ) { |
| 187 | $html .= '<span class="eael-facebook-feed-post-likes"><i class="far fa-thumbs-up" aria-hidden="true"></i> ' . esc_html( $likes ) . '</span>'; |
| 188 | } |
| 189 | if ( $settings['eael_facebook_feed_comments'] ) { |
| 190 | $html .= '<span class="eael-facebook-feed-post-comments"><i class="far fa-comments" aria-hidden="true"></i> ' . esc_html( $comments ) . '</span>'; |
| 191 | } |
| 192 | $html .= '</div> |
| 193 | </footer>'; |
| 194 | } |
| 195 | $html .= '</div> |
| 196 | </div>'; |
| 197 | } else { |
| 198 | $html .= '<a href="' . esc_url( $item['permalink_url'] ) . '" target="' . ( $settings['eael_facebook_feed_link_target'] ? '_blank' : '_self' ) . '" class="eael-facebook-feed-item"> |
| 199 | <div class="eael-facebook-feed-item-inner"> |
| 200 | <div class="eael-facebook-feed-img-container" style="background:url(' . ( empty( $photo ) ? EAEL_PLUGIN_URL . 'assets/front-end/img/flexia-preview.jpg' : esc_url( $photo ) ) . '); ' . esc_attr( $bg_style ) . '"> |
| 201 | <img class="eael-facebook-feed-img" src="' . ( empty( $photo ) ? EAEL_PLUGIN_URL . 'assets/front-end/img/flexia-preview.jpg' : esc_url( $photo ) ) . '"> |
| 202 | </div>'; |
| 203 | |
| 204 | if ( $settings['eael_facebook_feed_likes'] || $settings['eael_facebook_feed_comments'] ) { |
| 205 | $html .= '<div class="eael-facebook-feed-item-overlay"> |
| 206 | <div class="eael-facebook-feed-item-overlay-inner"> |
| 207 | <div class="eael-facebook-feed-meta">'; |
| 208 | if ( $settings['eael_facebook_feed_likes'] ) { |
| 209 | $html .= '<span class="eael-facebook-feed-post-likes"><i class="far fa-thumbs-up" aria-hidden="true"></i> ' . esc_html( $likes ) . '</span>'; |
| 210 | } |
| 211 | if ( $settings['eael_facebook_feed_comments'] ) { |
| 212 | $html .= '<span class="eael-facebook-feed-post-comments"><i class="far fa-comments" aria-hidden="true"></i> ' . esc_html( $comments ) . '</span>'; |
| 213 | } |
| 214 | $html .= '</div> |
| 215 | </div> |
| 216 | </div>'; |
| 217 | } |
| 218 | $html .= '</div> |
| 219 | </a>'; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'facebook_feed_load_more' ) { |
| 224 | $data = [ |
| 225 | 'num_pages' => ceil( count( $facebook_data ) / $settings['eael_facebook_feed_image_count']['size'] ), |
| 226 | 'html' => $html, |
| 227 | ]; |
| 228 | while ( ob_get_status() ) { |
| 229 | ob_end_clean(); |
| 230 | } |
| 231 | if ( function_exists( 'gzencode' ) ) { |
| 232 | $response = gzencode( wp_json_encode( $data ) ); |
| 233 | header( 'Content-Type: application/json; charset=utf-8' ); |
| 234 | header( 'Content-Encoding: gzip' ); |
| 235 | header( 'Content-Length: ' . strlen( $response ) ); |
| 236 | |
| 237 | printf( '%1$s', $response ); |
| 238 | } else { |
| 239 | wp_send_json( $data ); |
| 240 | } |
| 241 | wp_die(); |
| 242 | |
| 243 | } |
| 244 | |
| 245 | return $html; |
| 246 | } |
| 247 | |
| 248 | public function eael_str_check( $textData = '' ) { |
| 249 | $stringText = ''; |
| 250 | if ( strlen( $textData ) > 5 ) { |
| 251 | $explodeText = explode( ' ', trim( $textData ) ); |
| 252 | for ( $st = 0; $st < count( $explodeText ); $st ++ ) { |
| 253 | $pos = stripos( trim( $explodeText[ $st ] ), '#' ); |
| 254 | $pos1 = stripos( trim( $explodeText[ $st ] ), '@' ); |
| 255 | $poshttp = stripos( trim( $explodeText[ $st ] ), 'http' ); |
| 256 | $poshttps = stripos( trim( $explodeText[ $st ] ), 'https' ); |
| 257 | |
| 258 | if ( $pos !== false ) { |
| 259 | $stringText .= '<a href="https://facebook.com/hashtag/' . str_replace( '#', '', $explodeText[ $st ] ) . '?source=feed_text" target="_blank"> ' . esc_html( $explodeText[ $st ] ) . ' </a>'; |
| 260 | } elseif ( $pos1 !== false ) { |
| 261 | $stringText .= '<a href="https://facebook.com/' . $explodeText[ $st ] . '/" target="_blank"> ' . esc_html( $explodeText[ $st ] ) . ' </a>'; |
| 262 | } elseif ( $poshttp !== false || $poshttps !== false ) { |
| 263 | $stringText .= '<a href="' . esc_url( $explodeText[ $st ] ) . '" target="_blank"> ' . esc_html( $explodeText[ $st ] ) . ' </a>'; |
| 264 | } else { |
| 265 | $stringText .= ' ' . $explodeText[ $st ]; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return $stringText; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * get_url |
| 275 | * Build and return api endpoint based on source type |
| 276 | * |
| 277 | * @param string $page_id string |
| 278 | * @param string $token string |
| 279 | * @param string $source string |
| 280 | * |
| 281 | * @return string |
| 282 | */ |
| 283 | public function get_url( $page_id = '', $token = '', $source = 'posts', $display_comment = '' ) { |
| 284 | $comment_count = $display_comment == 'yes' ? ',comments.summary(total_count)' : ''; |
| 285 | $post_limit = apply_filters( 'eael_facebook_feed_post_limit', 99 ); |
| 286 | $post_url = "https://graph.facebook.com/v8.0/{$page_id}/posts?fields=status_type,created_time,from,message,story,full_picture,permalink_url,attachments.limit(1){type,media_type,title,description,unshimmed_url,media}{$comment_count},reactions.summary(total_count)&limit={$post_limit}&access_token={$token}"; |
| 287 | $feed_url = "https://graph.facebook.com/v8.0/{$page_id}/feed?fields=id,message,full_picture,status_type,created_time,attachments{title,description,type,url,media},from,permalink_url,shares,call_to_action{$comment_count},reactions.summary(total_count),privacy&access_token={$token}&limit={$post_limit}&locale=en_US"; |
| 288 | |
| 289 | if ( 'posts' === $source ) { |
| 290 | return $post_url; |
| 291 | } |
| 292 | return $feed_url; |
| 293 | } |
| 294 | } |
| 295 |