PluginProbe
Gianism / trunk
Gianism vtrunk
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / templates / fb-api.php

fb-api.php in Gianism trunk, at templates/fb-api.php

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