-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-gfl-settings.php
272 lines (232 loc) · 9.35 KB
/
class-gfl-settings.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
<?php
/**
* Settings Page for Get Facebook Likes
*
* @author Tan Nguyen <tan@binaty.org>
*/
class GFL_Settings
{
/**
* Constructor only to define hooks
*
* @return void
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
/**
* Create admin menu under Settings
*
* @return void
*/
public function admin_menu()
{
add_options_page(
__( 'Get Facebook Likes', 'get-facebook-likes' ),
__( 'Get Facebook Likes', 'get-facebook-likes' ),
'manage_options',
'get-facebook-likes',
array( $this, 'admin_page' )
);
}
/**
* All plugin settings saved in this method
*
* @return Redirect
*/
public function admin_init()
{
register_setting( 'get_facebook_likes', 'get_facebook_likes_settings' );
if ( ! isset( $_POST['_page_now'] ) || $_POST['_page_now'] != 'get-facebook-likes' )
return;
$settings = array();
$defaults = gfl_default_settings();
foreach ( $defaults as $key => $value )
{
$settings[$key] = $value;
if ( isset( $_POST[$key] ) )
$settings[$key] = $_POST[$key];
}
// Sanitize
$settings['mode'] = isset( $_POST['mode'] ) ? trim( $_POST['mode'] ) : 'basic';
$settings['app_id'] = isset( $_POST['app_id'] ) ? trim( $_POST['app_id'] ) : '';
$settings['auto_add'] = isset( $_POST['auto_add'] ) ? true : false;
$settings['sdk_locale'] = trim( $_POST['sdk_locale'] );
$settings = apply_filters( 'gfl_settings_before_update', $settings );
update_option( 'get_facebook_likes', $settings );
// Redirect with success message
$_POST['_wp_http_referer'] = add_query_arg( 'success', 'true', $_POST['_wp_http_referer'] );
wp_redirect( $_POST['_wp_http_referer'] );
exit;
}
/**
* Render Settings Page Content
*
* @return void
*/
public function admin_page()
{
?>
<script type="text/javascript">
jQuery( function($) {
$('[name="mode"], #auto_add').change(function (){
var modeSelected = $('[name="mode"]:checked').val();
var autoAdd = $('#auto_add').is(':checked');
if ( modeSelected === 'advanced' )
{
$('#auto_add_section, #app_id_section').show();
if (autoAdd) {
$('#app_id_section, #sdk_locale_section').show();
$('#setup-guide').hide();
}
else {
$('#app_id_section, #sdk_locale_section').hide();
$('#setup-guide').show();
}
}
else
{
$('#setup-guide, #auto_add_section, #app_id_section, #sdk_locale_section').hide();
}
});
$('[name="mode"], #auto_add').trigger('change');
});
</script>
<style type="text/css">
#setup-guide{
margin: 30px 0;
padding: 0 30px 30px 30px;
border: 1px solid #eee;
}
#setup-guide hr{
border: none;
border-top: 1px solid #eee;
}
#setup-guide pre{
background: #ddd;
padding: 15px 0;
}
</style>
<div class="wrap">
<h2><?php _e( 'Get Facebook Likes', 'get-facebook-likes' ); ?></h2>
<?php
// Display success message when settings saved
if ( isset( $_GET['success'] ) ) : ?>
<div id="message" class="updated notice is-dismissible">
<p><?php _e( 'Settings <strong>saved</strong>.', 'get-facebook-likes' ); ?></p>
<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
</div>
<?php endif; ?>
<form action="options.php" method="post" id="poststuff">
<?php settings_fields( 'get_facebook_likes' ); ?>
<div id="post-body" class="metabox-holder columns-2">
<div id="postbox-container-2" class="postbox-container">
<div class="meta-box-sortables">
<?php do_action( 'gfl_before_general_settings' ); ?>
<div class="postbox">
<div class="handlediv" title="Click to toggle"> <br></div>
<h3 class="hndle ui-sortable-handle"><?php _e( 'General Settings', 'get-facebook-likes' ); ?></h3>
<div class="inside">
<table class="form-table">
<tr valign="top">
<th><?php _e( 'Mode', 'get-facebook-likes' ); ?></th>
<td>
<div>
<label>
<input type="radio" name="mode" value="basic" <?php if ( gfl_setting( 'mode' ) == 'basic' ) echo 'checked'; ?>> <?php _e( 'Basic', 'get-facebook-likes' ); ?>
</label>
<p class="description">
<?php
_e( 'Update Likes, Shares and Comments count each time the page load. <br>
<strong>Pros:</strong> Works immediately, no need to setup app, api key and custom js <br>
<strong>Cons:</strong> Less accurate, a little bit slower page load', 'get-facebook-likes' );
?>
</p>
</div>
<br>
<section>
<label>
<input type="radio" name="mode" value="advanced" <?php if ( gfl_setting( 'mode' ) == 'advanced' ) echo 'checked'; ?>> <?php _e( 'Advanced', 'get-facebook-likes' ); ?> <code><?php _e( 'Recommended', 'get-facebook-likes' ); ?></code>
</label>
<p class="description">
<?php
_e( 'Update Likes, Shares and Comments count when user hit these buttons. <br>
<strong>Pros:</strong> More accurate, faster page load. <br>
<strong>Cons:</strong> Requires you setup JS SDK.
', 'get-facebook-likes' );
?>
</p>
</section>
<br>
<section id="auto_add_section">
<label for="auto_add">
<input type="checkbox" id="auto_add" value="1" name="auto_add" <?php if ( gfl_setting( 'auto_add' ) === true ) echo 'checked'; ?>>
<?php _e( 'Auto add JS SDK to <code>wp_head</code>', 'get-facebook-likes' ); ?>
</label>
<p class="description">
<?php
_e( "This will add Facebook JS SDK between <code>head</code> tags so you don't have to edit theme files.<br>
Please make sure that no other FB JS SDK installed.<br>
Uncheck this checkbox in case you already have FB JS SDK installed or you want to setup by yourself.", 'get-facebook-likes' );
?>
</p>
</section>
<br>
<section id="app_id_section">
<label for="app_id"><code><?php _e( 'App ID', 'get-facebook-likes' ); ?></code> <?php _e( 'Optional', 'get-facebook-likes' ); ?></label><br>
<input type="text" id="app_id" name="app_id" value="<?php echo gfl_setting( 'app_id' ); ?>">
<p class="description"><?php _e( 'Your Facebook App ID', 'get-facebook-likes' ); ?> <?php _e( 'Recommended', 'get-facebook-likes' ); ?></p>
</section>
<section id="sdk_locale_section">
<label for="sdk_locale"><code><?php _e( 'SDK Locale', 'get-facebook-likes' ); ?></code> <?php _e( 'Optional', 'get-facebook-likes' ); ?></label><br>
<input type="text" id="sdk_locale" name="sdk_locale" value="<?php echo gfl_setting('sdk_locale' ); ?>">
<p class="description">
<?php _e( 'In case you\'re using Facebook SDK in your language', 'get-facebook-likes' ); ?>
</p>
</section>
<section id="setup-guide">
<h3><?php _e( 'Setup Guide', 'get-facebook-likes' ); ?></h3>
<p class="description"><?php _e( "If you've already checked <code>Auto add JS SDK to wp_head</code>. Please ignore this guide.", 'get-facebook-likes' ); ?></p>
<hr>
<h4><?php _e ( 'Step 1', 'get-facebook-likes' ); ?></h4>
<p>
<?php _e ( 'If you\'ve already setup Facebook JS SDK, ignore this step. Otherwise, follow <a href="https://developers.facebook.com/docs/javascript/quickstart/v2.5">FB Quick Start Guide</a>', 'get-facebook-likes' ); ?>
</p>
<hr>
<h4><?php _e( 'Step 2', 'get-facebook-likes' ); ?></h4>
<p>
<?php _e( 'Add <code>GFL_Main.init();</code> after <code>FB.init();</code> in <code>window.fbAsyncInit</code> method, like so', 'get-facebook-likes' ); ?>
<pre>
window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
xfbml : true,
version : 'v2.5'
});
// <?php _e( 'Add this line', 'get-facebook-likes' ); ?>
GFL_Main.init();
};
</pre>
<?php _e( "That's all", 'get-facebook-likes' ); ?>
</p>
</section>
</td>
</tr>
</table>
</div><!--.inside-->
</div><!--.postbox-->
<?php do_action( 'gfl_after_general_settings' ); ?>
</div><!--.metaboxes-->
</div><!--.postbox-container2-->
</div><!--#post-body-->
<br class="clear">
<input type="hidden" name="_page_now" value="get-facebook-likes">
<?php submit_button(); ?>
</form>
</div>
<?php
}
}
new GFL_Settings;