This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dude-facebook-feed.php
148 lines (125 loc) · 4.52 KB
/
dude-facebook-feed.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
<?php
/**
* Plugin Name: Dude Facebook feed
* Plugin URI: https://github.com/digitoimistodude/dude-facebook-feed
* Description: Fetches the latest posts from Facebook profile or page.
* Version: 0.1.2
* Author: Digitoimisto Dude Oy, Timi Wahalahti
* Author URI: https://www.dude.fi
* Requires at least: 4.4.2
* Tested up to: 5.2
*
* Text Domain: dude-facebook-feed
* Domain Path: /languages
*/
if( !defined( 'ABSPATH' ) )
exit();
Class Dude_Facebook_Feed {
private static $_instance = null;
/**
* Construct everything and begin the magic!
*
* @since 0.1.0
* @version 0.1.0
*/
public function __construct() {
// Add actions to make magic happen
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
} // end function __construct
/**
* Prevent cloning
*
* @since 0.1.0
* @version 0.1.0
*/
public function __clone () {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'dude-facebook-feed' ) );
} // end function __clone
/**
* Prevent unserializing instances of this class
*
* @since 0.1.0
* @version 0.1.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'dude-facebook-feed' ) );
} // end function __wakeup
/**
* Ensure that only one instance of this class is loaded and can be loaded
*
* @since 0.1.0
* @version 0.1.0
* @return Main instance
*/
public static function instance() {
if( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
} // end function instance
/**
* Load plugin localisation
*
* @since 0.1.0
* @version 0.1.0
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'dude-facebook-feed', false, dirname( plugin_basename( __FILE__ ) ).'/languages/' );
} // end function load_plugin_textdomain
public function get_posts( $fbid = '' ) {
if( empty( $fbid ) )
return;
$transient_name = apply_filters( 'dude-facebook-feed/posts_transient', 'dude-facebook-'.$fbid, $fbid );
$posts = get_transient( $transient_name );
if( !empty( $posts ) || false != $posts )
return $posts;
$parameters = array(
'access_token' => apply_filters( 'dude-facebook-feed/parameters/access_token', '' ),
'locale' => apply_filters( 'dude-facebook-feed/parameters/locale', 'fi_FI' ),
'since' => apply_filters( 'dude-facebook-feed/parameters/since', date('Y-m-d', strtotime( '-1 year' ) ) ),
'limit' => apply_filters( 'dude-facebook-feed/parameters/limit', '10' ),
'fields' => implode( ',', apply_filters( 'dude-facebook-feed/parameters/fields', array( 'id', 'created_time', 'type', 'message', 'story', 'full_picture', 'link', 'permalink_url', 'status_type' ) ) ),
);
$response = self::_call_api( $fbid, apply_filters( 'dude-facebook-feed/api_call_parameters', $parameters ) );
if( $response === FALSE ) {
set_transient( $transient_name, $response, apply_filters( 'dude-facebook-feed/posts_transient_lifetime', '600' ) / 2 );
return;
}
$response = json_decode( $response['body'], true );
// FB graph API 2.x => 3.3 backwards comp
if ( isset( $val['status_type'] ) && isset( $val['permalink_url'] ) ) {
foreach ( $response as $key => $val ) {
$response['data'][ $key ]['type'] = $val['status_type'];
$response['data'][ $key ]['link'] = $val['permalink_url'];
}
}
$response = apply_filters( 'dude-facebook-feed/posts', $response );
set_transient( $transient_name, $response, apply_filters( 'dude-facebook-feed/posts_transient_lifetime', '600' ) );
return $response;
} // end function get_posts
private function _call_api( $fbid = '', $parameters = array() ) {
if( empty( $fbid ) )
return false;
if( empty( $parameters ) )
return false;
$parameters = http_build_query( $parameters );
$response = wp_remote_get( 'https://graph.facebook.com/'.$fbid.'/feed/?'.$parameters );
if( is_wp_error( $response ) || $response['response']['code'] !== 200 ) {
self::_write_log( 'response status code not 200 OK, fbid: '.$fbid );
return false;
}
return $response;
} // end function _call_api
private function _write_log ( $log ) {
if( true === WP_DEBUG ) {
if( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
} // end _write_log
} // end class Dude_Facebook_Feed
function dude_facebook_feed() {
return new Dude_Facebook_Feed();
} // end function dude_facebook_feed