C:\xampp\htdocs\landing\wp-content\updraft\plugins-old\custom-facebook-feed\inc\CFF_Oembed.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/**
 * Class CFF_Oembed
 *
 * Replaces the native WordPress functionality for Facebook oembed
 * to allow authenticated oembeds
 *
 * @since 2.16/3.16
 */

namespace CustomFacebookFeed;

if ( ! 
defined'ABSPATH' ) ) {
    die( 
'-1' );
}

class 
CFF_Oembed
{
    
/**
     * CFF_Oembed constructor.
     *
     * If an account has been connected, hooks are added
     * to change how Facebook links are handled for oembeds
     *
     * @since 2.16/3.16
     */
    
public function __construct() {
        if ( 
CFF_Oembed::can_do_oembed() ) {
            if ( 
CFF_Oembed::can_check_for_old_oembeds() ) {
                
add_action'the_post', array( 'CustomFacebookFeed\CFF_Oembed''check_page_for_old_oembeds' ) );
            }
            
add_filter'oembed_providers', array( 'CustomFacebookFeed\CFF_Oembed''oembed_providers' ), 10);
            
add_filter'oembed_fetch_url', array( 'CustomFacebookFeed\CFF_Oembed''oembed_set_fetch_url' ), 10);
            
add_filter'oembed_result', array( 'CustomFacebookFeed\CFF_Oembed''oembed_result' ), 10);
        }
        if ( 
CFF_Oembed::should_extend_ttl() ) {
            
add_filter'oembed_ttl', array( 'CustomFacebookFeed\CFF_Oembed''oembed_ttl' ), 10);
        }
    }

    
/**
     * Check to make sure there is a saved access token to
     * enable authenticated oembeds
     *
     * @return bool
     *
     * @since 2.16/3.16
     */
    
public static function can_do_oembed() {
        
$oembed_token_settings get_option'cff_oembed_token', array() );

        if ( isset( 
$oembed_token_settings['disabled'] ) && $oembed_token_settings['disabled'] ) {
            return 
false;
        }

        
$access_token CFF_Oembed::last_access_token();
        if ( ! 
$access_token ) {
            return 
false;
        }

        return 
true;
    }

    
/**
     * The "time to live" for Instagram oEmbeds is extended if the access token expires.
     * Even if new oEmbeds will not use the Instagram Feed system due to an expired token
     * the time to live should continue to be extended.
     *
     * @return bool
     *
     * @since 2.16/3.16
     */
    
public static function should_extend_ttl() {
        
$oembed_token_settings get_option'cff_oembed_token', array() );

        if ( isset( 
$oembed_token_settings['disabled'] ) && $oembed_token_settings['disabled'] ) {
            return 
false;
        }

        
$will_expire CFF_Oembed::oembed_access_token_will_expire();
        if ( 
$will_expire ) {
            return 
true;
        }

        return 
false;
    }

    
/**
     * Checking for old oembeds makes permanent changes to posts
     * so we want the user to turn it off and on
     *
     * @return bool
     *
     * @since 2.16/3.16
     */
    
public static function can_check_for_old_oembeds() {
        
/**
         * TODO: if setting is enabled
         */
        
return true;
    }

    
/**
     * Filters the WordPress list of oembed providers to
     * change what url is used for remote requests for the
     * oembed data
     *
     * @param array $providers
     *
     * @return mixed
     *
     * @since 2.16/3.16
     */
    
public static function oembed_providers$providers ) {
        
$oembed_url CFF_Oembed::oembed_url();
        if ( 
$oembed_url ) {
            
$post_embed_providers CFF_Oembed::post_providers();
            foreach ( 
$post_embed_providers as $post_provider ) {
                
$providers$post_provider ] = array( $oembed_url 'oembed_post'true );
            }

            
$video_embed_providers CFF_Oembed::video_providers();
            foreach ( 
$video_embed_providers as $video_provider ) {
                
$providers$video_provider ] = array( $oembed_url 'oembed_video'true );
            }
        }

        return 
$providers;
    }

    
/**
     * Add the access token from a connected account to make an authenticated
     * call to get oembed data from Facebook
     *
     * @param string $provider
     * @param string $url
     * @param array $args
     *
     * @return string
     *
     * @since 2.16/3.16
     */
    
public static function oembed_set_fetch_url$provider$url$args ) {
        
$access_token CFF_Oembed::last_access_token();
        if ( ! 
$access_token ) {
            return 
$provider;
        }

        if ( 
strpos$provider'oembed_post' ) !== false
             
|| strpos$provider'oembed_video' ) !== false ) {

            if ( 
strpos$url'?' ) !== false ) {
                
$exploded explode'?'$url );
                if ( isset( 
$exploded[1] ) ) {
                    
$provider str_replaceurlencode'?' $exploded[1] ), ''$provider );
                }
            }
            
$provider add_query_arg'access_token'$access_token$provider );
        }

        return 
$provider;
    }

    
/**
     * New oembeds are wrapped in a div for easy detection of older oembeds
     * that will need to be updated
     *
     * @param string $html
     * @param string $url
     * @param array $args
     *
     * @return string
     *
     * @since 2.16/3.16
     */
    
public static function oembed_result$html$url$args ) {
        
$post_embed_providers CFF_Oembed::post_providers();
        foreach ( 
$post_embed_providers as $post_provider ) {
            if ( 
preg_match$post_provider$url ) === ) {
                if ( 
strpos$html'class="fb-post"' ) !== false ) {
                    
$html '<div class="cff-embed-wrap cff-post-embed-wrap">' str_replace'class="fb-post"''class="fb-post cff-embed cff-post-embed"'$html ) . '</div>';
                }
            }
        }

        
$video_embed_providers CFF_Oembed::video_providers();
        foreach ( 
$video_embed_providers as $video_provider ) {
            if ( 
preg_match$video_provider$url ) === ) {
                if ( 
strpos$html'class="fb-video"' ) !== false ) {
                    
$html '<div class="cff-embed-wrap cff-video-embed-wrap">' str_replace'class="fb-video"''class="fb-video cff-embed cff-video-embed"'$html ) . '</div>';
                }
            }
        }

        return 
$html;
    }

    
/**
     * Extend the "time to live" for oEmbeds created with access tokens that expire
     *
     * @param $ttl
     * @param $url
     * @param $attr
     * @param $post_ID
     *
     * @return float|int
     *
     * @since 2.16/3.16
     */
    
public static function oembed_ttl$ttl$url$attr$post_ID ) {
        
$providers CFF_Oembed::post_providers();
        foreach ( 
$providers as $provider ) {
            if ( 
preg_match$provider$url ) === ) {
                
$ttl 30 YEAR_IN_SECONDS;
            }
        }

        
$providers CFF_Oembed::video_providers();
        foreach ( 
$providers as $provider ) {
            if ( 
preg_match$provider$url ) === ) {
                
$ttl 30 YEAR_IN_SECONDS;
            }
        }

        return 
$ttl;
    }

    
/**
     * Only one api URL for FB
     *
     * @return bool|string
     *
     * @since 2.16/3.16
     */
    
public static function oembed_url() {
        return 
'https://graph.facebook.com/';
    }

    
/**
     * Any access token will work for oembeds so the access token
     * saved in settings is used
     *
     * @return bool|string
     *
     * @since 2.16/3.16
     */
    
public static function last_access_token() {
        
$oembed_token_settings get_option'cff_oembed_token', array() );
        
$will_expire CFF_Oembed::oembed_access_token_will_expire();
        if ( ! empty( 
$oembed_token_settings['access_token'] )
             && (! 
$will_expire || $will_expire time()) ) {
            return 
str_replace(":"":02Sb981f26534g75h091287a46p5l63"$oembed_token_settings['access_token']);
        } else {
            
$settings_access_token trim(get_option('cff_access_token'));
            if ( ! empty( 
$settings_access_token ) ) {
                return 
str_replace(":"":02Sb981f26534g75h091287a46p5l63"$settings_access_token);
            }

            if ( 
class_exists'SB_Instagram_Oembed' ) ) {
                
$sbi_oembed_token_settings get_option'sbi_oembed_token', array() );
                if ( ! empty( 
$sbi_oembed_token_settings['access_token'] ) ) {
                    return 
$sbi_oembed_token_settings['access_token'];
                }
            }
        }

        return 
false;
    }

    
/**
     * Access tokens created from FB accounts not connected to an
     * FB page expire after 60 days.
     *
     * @return bool|int
     */
    
public static function oembed_access_token_will_expire() {
        
$oembed_token_settings get_option'cff_oembed_token', array() );
        
$will_expire = isset( $oembed_token_settings['expiration_date'] ) && (int)$oembed_token_settings['expiration_date'] > ? (int)$oembed_token_settings['expiration_date'] : false;

        return 
$will_expire;
    }

    
/**
     * Before links in the content are processed, old oembed post meta
     * records are deleted so new oembed data will be retrieved and saved.
     * If this check has been done and no old oembeds are found, a flag
     * is saved as post meta to skip the process.
     *
     * @since 2.16/3.16
     */
    
public static function check_page_for_old_oembeds() {
        if ( 
is_admin() ) {
            return;
        }

        
$post_ID get_the_ID();
        
$done_checking = (int)get_post_meta$post_ID'_cff_oembed_done_checking'true ) === 1;

        if ( ! 
$done_checking ) {

            
$num_found CFF_Oembed::delete_facebook_oembed_caches$post_ID );
            if ( 
$num_found === ) {
                
update_post_meta$post_ID'_cff_oembed_done_checking');
            }
        }
    }

    
/**
     * Loop through post meta data and if it's an oembed and has content
     * that looks like a Facebook oembed, delete it
     *
     * @param $post_ID
     *
     * @return int number of old oembed caches found
     *
     * @since 2.16/3.16
     */
    
public static function delete_facebook_oembed_caches$post_ID ) {
        
$post_metas get_post_meta$post_ID );
        if ( empty( 
$post_metas ) ) {
            return 
0;
        }

        
$total_found 0;
        foreach ( 
$post_metas as $post_meta_key => $post_meta_value ) {
            if ( 
'_oembed_' === substr$post_meta_key0) ) {
                if ( 
strpos$post_meta_value[0], 'class="fb-post"' ) !== false
                     
&& strpos$post_meta_value[0], 'cff-embed-wrap' ) === false ) {
                    
$total_found++;
                    
delete_post_meta$post_ID$post_meta_key );
                    if ( 
'_oembed_time_' !== substr$post_meta_key013 ) ) {
                        
delete_post_meta$post_IDstr_replace'_oembed_''_oembed_time_'$post_meta_key ) );
                    }
                } elseif ( 
strpos$post_meta_value[0], 'class="fb-video"' ) !== false
                           
&& strpos$post_meta_value[0], 'cff-embed-wrap' ) === false ) {
                    
$total_found++;
                    
delete_post_meta$post_ID$post_meta_key );
                    if ( 
'_oembed_time_' !== substr$post_meta_key013 ) ) {
                        
delete_post_meta$post_IDstr_replace'_oembed_''_oembed_time_'$post_meta_key ) );
                    }
                }
            }
        }

        return 
$total_found;
    }

    
/**
     * Current list of regex to identify FB URLs that could become oembeds using
     * the 'oembed_post' endpoint.
     *
     * @return array
     *
     * @since 2.16/3.16
     */
    
public static function post_providers() {
        
$post_embed_providers = array(
            
'#https?://www\.facebook\.com/.*/posts/.*#i',
            
'#https?://www\.facebook\.com/.*/activity/.*#i',
            
'#https?://www\.facebook\.com/.*/photos/.*#i',
            
'#https?://www\.facebook\.com/photo(s/|\.php).*#i',
            
'#https?://www\.facebook\.com/permalink\.php.*#i',
            
'#https?://www\.facebook\.com/media/.*#i',
            
'#https?://www\.facebook\.com/questions/.*#i',
            
'#https?://www\.facebook\.com/notes/.*#i',
        );

        return 
$post_embed_providers;
    }

    
/**
     * Current list of regex to identify FB URLs that could become oembeds using
     * the 'oembed_video' endpoint.
     *
     * @return array
     *
     * @since 2.16/3.16
     */
    
public static function video_providers() {
        
$video_embed_providers = array(
            
'#https?://www\.facebook\.com/.*/videos/.*#i',
            
'#https?://www\.facebook\.com/video\.php.*#i'
        
);

        return 
$video_embed_providers;
    }

    
/**
     * Used for clearing the oembed update check flag for all posts
     *
     * @since 2.16/3.16
     */
    
public static function clear_checks() {
        global 
$wpdb;
        
$table_name esc_sql$wpdb->prefix "postmeta" );
        
$result $wpdb->query("
            DELETE
            FROM 
$table_name
            WHERE meta_key = '_cff_oembed_done_checking';"
);
    }
}

/*
function cffOembedInit() {
    return new CFF_Oembed();
}
cffOembedInit();
*/
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586