| 1 |
<?php |
| 2 |
/** |
| 3 |
* Facebook related functions |
| 4 |
* |
| 5 |
* @package Gianism |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Returns Facebook ID |
| 11 |
* |
| 12 |
* @since 3.0.0 |
| 13 |
* @package Gianism |
| 14 |
* |
| 15 |
* @param int $user_id |
| 16 |
* |
| 17 |
* @return mixed |
| 18 |
*/ |
| 19 |
function gianism_get_facebook_id( $user_id ) { |
| 20 |
/** @var \Gianism\Service\Facebook $facebook */ |
| 21 |
$facebook = \Gianism\Service\Facebook::get_instance(); |
| 22 |
|
| 23 |
return get_user_meta( $user_id, $facebook->umeta_id, true ); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Returns url to get Publish stream permission |
| 29 |
* |
| 30 |
* This function itself has no effect without action hook. |
| 31 |
* See example below: |
| 32 |
* |
| 33 |
* <pre> |
| 34 |
* // In some template, display button. |
| 35 |
* get_facebook_publish_permission_link(get_permalink(), 'my_favorite_action', array('post_id' => get_the_ID())); |
| 36 |
* |
| 37 |
* // Then, hook action in functions.php in your theme. |
| 38 |
* add_action('my_favorite_action', 'my_publish_action'); |
| 39 |
* |
| 40 |
* function my_publish_action($facebook, $args){ |
| 41 |
* $post = get_post($args['post_id']); |
| 42 |
* try{ |
| 43 |
* $facebook->api("/me/feed", "POST", array( |
| 44 |
* "message" => "I read this article!", |
| 45 |
* "link" => get_permalink($post->ID), |
| 46 |
* "name" => get_the_title($post->ID), |
| 47 |
* "description" => strip_tags($post->post_content), |
| 48 |
* "action" => json_encode(array( |
| 49 |
* "name" => get_bloginfo('name'), |
| 50 |
* "link" => home_url('/'))) |
| 51 |
* )); |
| 52 |
* }catch(FacebookApiException $e){ |
| 53 |
* // Error |
| 54 |
* wp_die($e->getMessage()); |
| 55 |
* } |
| 56 |
* } |
| 57 |
* </pre> |
| 58 |
* |
| 59 |
* @since 3.0.0 |
| 60 |
* @package Gianism |
| 61 |
* |
| 62 |
* @param string $redirect_url URL where user will be redirect after authentication |
| 63 |
* @param string $action Action name which will be fired after authentication |
| 64 |
* @param array $args Array which will be passed to action hook |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
function gianism_get_facebook_publish_permission_link( $redirect_url = null, $action = '', $args = [] ) { |
| 69 |
/** @var \Gianism\Service\Facebook $facebook */ |
| 70 |
$facebook = \Gianism\Service\Facebook::get_instance(); |
| 71 |
|
| 72 |
return $facebook->get_publish_permission_link( $redirect_url, $action, $args ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get admin facebook id |
| 77 |
* |
| 78 |
* This will be user id or facebook page id. |
| 79 |
* |
| 80 |
* <pre> |
| 81 |
* // Example |
| 82 |
* $fb = gianism_fb_admin(); |
| 83 |
* if( !is_wp_error($fb) ){ |
| 84 |
* $feed = $fb->api(gianism_fb_admin_id().'/feed'); |
| 85 |
* foreach( $feed['data'] as $status ){ |
| 86 |
* // Do stuff. |
| 87 |
* } |
| 88 |
* } |
| 89 |
* </pre> |
| 90 |
* |
| 91 |
* @package Gianism |
| 92 |
* @since 3.0.0 |
| 93 |
* @return int|string |
| 94 |
*/ |
| 95 |
function gianism_fb_admin_id() { |
| 96 |
/** @var \Gianism\Service\Facebook $facebook */ |
| 97 |
$facebook = \Gianism\Service\Facebook::get_instance(); |
| 98 |
|
| 99 |
return $facebook->admin_id; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get facebook API client for admin |
| 104 |
* |
| 105 |
* <pre> |
| 106 |
* $fb = gianism_fb_admin(); |
| 107 |
* if( !is_wp_error($fb) ){ |
| 108 |
* // Get feed. |
| 109 |
* $feeds = $fb->api('/me/feed'); |
| 110 |
* // Post feed. |
| 111 |
* $fb->api('/me/feed', 'POST', array( |
| 112 |
* 'message' => 'Hola!', |
| 113 |
* )); |
| 114 |
* } |
| 115 |
* </pre> |
| 116 |
* |
| 117 |
* @package Gianism |
| 118 |
* @since 3.0.0 |
| 119 |
* @return \Facebook\Facebook|WP_Error |
| 120 |
*/ |
| 121 |
function gianism_fb_admin() { |
| 122 |
/** @var \Gianism\Service\Facebook $facebook */ |
| 123 |
$facebook = \Gianism\Service\Facebook::get_instance(); |
| 124 |
|
| 125 |
return $facebook->admin; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get currently set page API |
| 130 |
* |
| 131 |
* @package Gianism |
| 132 |
* @since 3.0.6 |
| 133 |
* @return \Facebook\Facebook|WP_Error |
| 134 |
*/ |
| 135 |
function gianism_fb_page_api() { |
| 136 |
return \Gianism\Service\Facebook::get_instance()->get_current_page_api(); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get instant article status |
| 141 |
* |
| 142 |
* @package Gianism |
| 143 |
* @since 3.0.8 |
| 144 |
* @param string|int $url_or_post_id If post id is set, convert it to permalink. |
| 145 |
* @param bool $development If need development content, set to true. |
| 146 |
* @return WP_Error|array |
| 147 |
*/ |
| 148 |
function gianism_fb_instant_article_status( $url_or_post_id, $development = false ) { |
| 149 |
if ( is_numeric( $url_or_post_id ) ) { |
| 150 |
$url = get_permalink( $url_or_post_id ); |
| 151 |
} else { |
| 152 |
$url = $url_or_post_id; |
| 153 |
} |
| 154 |
try { |
| 155 |
$api = gianism_fb_page_api(); |
| 156 |
if ( is_wp_error( $api ) ) { |
| 157 |
return $api; |
| 158 |
} |
| 159 |
// Get article ID. |
| 160 |
$args = [ |
| 161 |
'access_token' => $api->getDefaultAccessToken()->getValue(), |
| 162 |
'id' => $url, |
| 163 |
'fields' => $development ? 'development_instant_article' : 'instant_article', |
| 164 |
]; |
| 165 |
$response = $api->get( add_query_arg( $args, '/' ) )->getGraphNode()->getField( 'instant_article' ); |
| 166 |
return [ |
| 167 |
'url' => $url, |
| 168 |
'id' => $response->getField( 'id' ), |
| 169 |
'html_source' => $response->getField( 'html_source' ), |
| 170 |
]; |
| 171 |
} catch ( Exception $e ) { |
| 172 |
return new WP_Error( $e->getCode(), $e->getMessage() ); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Update instant article |
| 178 |
* |
| 179 |
* You don't have to determine create and update |
| 180 |
* because fb treats them same way. |
| 181 |
* |
| 182 |
* @package Gianism |
| 183 |
* @since 3.0.8 |
| 184 |
* @param string $html HTML content |
| 185 |
* @param bool $publish If this is live or not. |
| 186 |
* @param bool $is_develop If develop, set to true. |
| 187 |
* @return string|WP_Error Import Status ID if success. See {https://developers.facebook.com/docs/instant-articles/api/} |
| 188 |
*/ |
| 189 |
function gianism_fb_update_instant_article( $html, $publish = true, $is_develop = false ) { |
| 190 |
try { |
| 191 |
$api = gianism_fb_page_api(); |
| 192 |
if ( is_wp_error( $api ) ) { |
| 193 |
return $api; |
| 194 |
} |
| 195 |
$args = [ |
| 196 |
'access_token' => $api->getDefaultAccessToken()->getValue(), |
| 197 |
'html_source' => $html, |
| 198 |
'published' => ! $is_develop && $publish, |
| 199 |
'development_mode' => $is_develop, |
| 200 |
]; |
| 201 |
$response = $api->post( 'me/instant_articles', $args ); |
| 202 |
return $response->getGraphNode()->getField( 'id' ); |
| 203 |
} catch ( Exception $e ) { |
| 204 |
return new WP_Error( $e->getCode(), $e->getMessage() ); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Delete post from Instant article |
| 210 |
* |
| 211 |
* @package Gianism |
| 212 |
* @since 3.0.8 |
| 213 |
* @param string|int $url_or_id URL or post ID. |
| 214 |
* @param bool $development Default false. |
| 215 |
* @return bool|WP_Error |
| 216 |
*/ |
| 217 |
function gianism_fb_delete_instant_article( $url_or_id, $development = false ) { |
| 218 |
try { |
| 219 |
$api = gianism_fb_page_api(); |
| 220 |
if ( is_wp_error( $api ) ) { |
| 221 |
return $api; |
| 222 |
} |
| 223 |
$status = gianism_fb_instant_article_status( $url_or_id, $development ); |
| 224 |
if ( is_wp_error( $status ) ) { |
| 225 |
return $status; |
| 226 |
} |
| 227 |
$result = $api->delete( "/{$status['id']}", [ |
| 228 |
'access_token' => $api->getDefaultAccessToken()->getValue(), |
| 229 |
] ); |
| 230 |
return $result->getGraphNode()->getField( 'success' ); |
| 231 |
} catch ( Exception $e ) { |
| 232 |
return new WP_Error( $e->getCode(), $e->getMessage() ); |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|