| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) or die(); |
| 4 |
|
| 5 |
/** @var \Gianism\UI\SettingScreen $this */ |
| 6 |
/** @var \Gianism\Service\Facebook $facebook */ |
| 7 |
$facebook = $this->service->get( 'facebook' ); |
| 8 |
|
| 9 |
?> |
| 10 |
<h3><i class="lsf lsf-facebook"></i> <?php $this->e( 'Token setting' ) ?></h3> |
| 11 |
<?php if ( is_wp_error( ( $error = $facebook->admin ) ) ) : ?> |
| 12 |
<p class="danger"> |
| 13 |
<?php if ( $error->get_error_code() == 410 ) : ?> |
| 14 |
<?php $this->e( 'Token is outdated. Click link and get new one.' ); ?> |
| 15 |
<?php else : ?> |
| 16 |
<?php $this->e( 'Token is not set. Please click link below and get one.' ); ?> |
| 17 |
<?php endif; ?> |
| 18 |
</p> |
| 19 |
<?php else : ?> |
| 20 |
<p class="notice"> |
| 21 |
<?php printf( |
| 22 |
$this->_( 'O.K. You have permission. If you want to renew token, please click link below. Token will be valid for %d days.' ), |
| 23 |
( 60 - floor( ( current_time( 'timestamp' ) - get_option( 'gianism_facebook_admin_refreshed', 0 ) ) / 60 / 60 / 24 ) ) |
| 24 |
); ?> |
| 25 |
</p> |
| 26 |
<?php endif; ?> |
| 27 |
<a class="button" href="<?php echo esc_url( $facebook->get_admin_connect_link() ) ?>"><?php $this->e( 'Get Token' ) ?> |
| 28 |
<small><?php $this->e( 'Read Only' ) ?></small> |
| 29 |
</a> |
| 30 |
<a class="button" |
| 31 |
href="<?php echo esc_url( $facebook->get_admin_connect_link( true ) ) ?>"><?php $this->e( 'Get Token' ) ?> |
| 32 |
<small><?php $this->e( 'Publish' ) ?></small> |
| 33 |
</a> |
| 34 |
|
| 35 |
<h3><i class="lsf lsf-friend"></i> <?php $this->e( 'Account to use' ) ?></h3> |
| 36 |
<?php if ( is_wp_error( $facebook->admin ) ) : ?> |
| 37 |
<p class="description"><?php $this->e( 'You need access token to manage accounts. Click links above and you can get Facebook API token.' ) ?></p> |
| 38 |
<?php elseif ( ! ( $me = $facebook->admin_account ) ) : ?> |
| 39 |
<p class="danger"><?php $this->e( 'Failed to get your account. Please renew token.' ) ?></p> |
| 40 |
<?php else : ?> |
| 41 |
<p class="description"><?php $this->e( 'Please select account to use.' ) ?></p> |
| 42 |
<form method="post" action="<?php echo $this->setting_url( 'fb-api' ) ?>"> |
| 43 |
<?php wp_nonce_field( 'gianism_fb_account' ) ?> |
| 44 |
<table class="form-table"> |
| 45 |
<tr> |
| 46 |
<th><?php $this->e( 'You' ) ?></th> |
| 47 |
<td> |
| 48 |
<label><input type="radio" name="fb_account_id" |
| 49 |
value="me"<?php checked( 'me' == $facebook->admin_id ) ?> /> <?php echo esc_html( $me['name'] ) ?> |
| 50 |
</label> |
| 51 |
</td> |
| 52 |
</tr> |
| 53 |
<tr> |
| 54 |
<th><?php $this->e( 'Your Page' ) ?></th> |
| 55 |
<td> |
| 56 |
<?php |
| 57 |
$out = []; |
| 58 |
foreach ( $facebook->admin_pages as $account ) { |
| 59 |
$out[] = sprintf( |
| 60 |
'<label><input type="radio" name="fb_account_id" value="%s"%s /> %s</label>', |
| 61 |
$account['id'], |
| 62 |
checked( $account['id'] == $facebook->admin_id, true, false ), |
| 63 |
esc_html( $account['name'] ) |
| 64 |
); |
| 65 |
} |
| 66 |
if ( $out ) : |
| 67 |
echo implode( '<br />', $out ); |
| 68 |
else : |
| 69 |
?> |
| 70 |
<p class="description"><?php $this->e( 'You have no Facebook page.' ) ?></p> |
| 71 |
<?php endif; ?> |
| 72 |
</td> |
| 73 |
</tr> |
| 74 |
</table> |
| 75 |
<?php submit_button() ?> |
| 76 |
</form> |
| 77 |
<?php endif; ?> |
| 78 |
|
| 79 |
<h3><i class="lsf lsf-help"></i> <?php $this->e( 'How to use' ) ?></h3> |
| 80 |
<p> |
| 81 |
<?php printf( |
| 82 |
$this->_( 'You can get token-ready <a href="%s">Facebook PHP SDK\'s API client</a>. For example, get latest news feed and show it on footer.' ), |
| 83 |
'https://developers.facebook.com/docs/reference/php' |
| 84 |
) |
| 85 |
?> |
| 86 |
</p> |
| 87 |
<pre class="brush: php"> |
| 88 |
<?php |
| 89 |
echo <<<'PHP' |
| 90 |
// Add action hook. |
| 91 |
add_action('wp_footer', function(){ |
| 92 |
$status = my_fb_status(); |
| 93 |
echo '<div class="footer-fb-status">'.$status.'</div>'; |
| 94 |
}); |
| 95 |
|
| 96 |
/** |
| 97 |
* This function will return facebook's latest status |
| 98 |
* |
| 99 |
* @return string |
| 100 |
*/ |
| 101 |
function my_fb_status(){ |
| 102 |
// Use transient, because network I/O is slow. |
| 103 |
$status = get_transient('my_fb_status'); |
| 104 |
if( false === $status ){ |
| 105 |
// If settings are done, |
| 106 |
// You can get token-ready client with this function. |
| 107 |
$fb = gianism_fb_admin(); |
| 108 |
if( is_wp_error(\$fb) ){ |
| 109 |
// Oops, |
| 110 |
return 'Sorry!'; |
| 111 |
} |
| 112 |
// Let's get Page setting. |
| 113 |
$page_id = gianism_fb_admin_id(); |
| 114 |
// api method is SDK's method for Graph API |
| 115 |
$feeds = $fb->api("{$page_id}/feed"); |
| 116 |
if ( ! $feeds['data'] ) { |
| 117 |
return 'Sorry, no updates.'; |
| 118 |
} |
| 119 |
$status = $feeds['data'][0]['message']; |
| 120 |
// Save it for 0.5h. |
| 121 |
set_transient( 'my_fb_status', $status, 60 * 60 * 0.5 ); |
| 122 |
} |
| 123 |
return $status |
| 124 |
} |
| 125 |
PHP; |
| 126 |
?> |
| 127 |
</pre> |
| 128 |
<p><?php $this->e( 'You should know about Facebook Graph API. Useful resources are below.' ) ?></p> |
| 129 |
<ol> |
| 130 |
<li><a href="https://developers.facebook.com/docs/graph-api">Graph API Reference</a></li> |
| 131 |
<li><a href="https://developers.facebook.com/tools/explorer/">Graph API Explorer</a></li> |
| 132 |
<li><a href="https://developers.facebook.com/docs/reference/php/3.2.3">PHP SDK's documentation</a></li> |
| 133 |
</ol> |
| 134 |
<p> |
| 135 |
<?php printf( |
| 136 |
$this->_( 'For more detailed information, please visit our <a target="_blank" href="%s">support site</a>.' ), |
| 137 |
gianism_utm_link( 'https://gianism.info/', [ |
| 138 |
'utm_medium' => 'fb-api', |
| 139 |
] ) |
| 140 |
); ?> |
| 141 |
</p> |
| 142 |
|