-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathasync-share.php
290 lines (246 loc) · 7.32 KB
/
async-share.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Plugin Name: Async Social Sharing
* Plugin URI: http://www.rachelbaker.me
* Description: Simple social sharing plugin that loads the third-party scripts asynchronously to improve site performance. Plugin provides options to load the following sharing widgets: Twitter, Facebook, Google+, Linkedin and Hacker News.
* Version: 1.8.1
* Author: Rachel Baker
* Author URI: http://www.rachelbaker.me
* Author Email: [email protected]
* License:
*
* Copyright 2014 Rachel Baker ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Constants
*/
define( 'ASYNC_SOCIAL_SHARING_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'ASYNC_SOCIAL_SHARING_PLUGIN_URL', plugins_url( '', __FILE__ ) );
/**
* Allow auto-updates of plugin for WordPress 3.7+.
*/
if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
add_filter( 'auto_update_plugin', '__return_true' );
}
class Async_Social_Sharing {
private static $instance;
/**
* Initializes Async_Social_Sharing Class
*
* @return Object new Async_Social_Sharing Class();
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
self::$instance->setup_actions();
self::$instance->setup_filters();
}
return self::$instance;
}
/**
* Dummy constructor, nothing to see here.
*/
protected function __construct() {}
/**
* Register the action hooks for Async Social Sharing.
*/
private function setup_actions() {
add_action( 'init', array( $this, 'register_styles' ) );
add_action( 'admin_init', array( $this, 'register_options' ) );
add_action( 'admin_menu', array( $this, 'add_options_page' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
}
/**
* Register the filter hooks for Async Social Sharing.
*/
private function setup_filters() {
add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 10, 2 );
add_filter( 'the_content', array( $this, 'display_check' ) );
}
/**
* Register the settings for Async Social Sharing.
* Options are sanitized with the validate_options callback method.
*
* @return unknown
*/
public function register_options() {
return register_setting(
'async_share_plugin_options',
'async_share_options',
array( $this, 'validate_options' )
);
}
/**
* Returns the options for Async Social Sharing.
*
* @return mixed|void
*/
public function get_options() {
return get_option( 'async_share_options' );
}
/**
* Callback sanitization for the Async Social Sharing option values.
*
* @param $input
*
* @return mixed
*/
public static function validate_options( $input ) {
sanitize_text_field( $input );
return $input;
}
/**
* Adds Async Social Sharing sub-menu page to the options main menu.
*
* @return bool|string
*/
public function add_options_page() {
return add_options_page(
'Async Social Sharing Options',
'Async Sharing Options',
'manage_options',
__FILE__,
array( $this, 'display_admin_view' )
);
}
/**
* Renders the plugin settings screen.
*
* @return string
*/
public function display_admin_view() {
ob_start();
include ASYNC_SOCIAL_SHARING_PLUGIN_PATH . 'views/admin-view.php';
return ob_get_flush();
}
/**
* Adds settings link to plugin screen for Async Social Sharing.
*
* @param $links
* @param $file
*
* @return array
*/
public function add_settings_link( $links, $file ) {
$plugin_file_name = plugin_basename( __FILE__ );
if ( $file == $plugin_file_name ) {
$links[] = '<a href="' . get_admin_url() . 'options-general.php?page=' . $plugin_file_name . '">' . __( 'Settings' ) . '</a>';
}
return $links;
}
/**
* Registers included CSS for front-end display of social widgets
*
* Initialized by init hook
*/
public function register_styles() {
$cache_buster = filemtime( ASYNC_SOCIAL_SHARING_PLUGIN_PATH . 'assets/css/async-share.css' );
wp_register_style( 'async_css', ASYNC_SOCIAL_SHARING_PLUGIN_URL . '/assets/css/async-share.css', false, $cache_buster, 'all' );
}
/**
* Loads JavaScript and optionally CSS for front-end display of social widgets
*
* Initialized by wp_enqueue_scripts hook
*/
public function load_scripts() {
$options = $this->get_options();
$cache_buster = filemtime( ASYNC_SOCIAL_SHARING_PLUGIN_PATH . 'assets/js/async-share.js' );
wp_enqueue_script( 'async_js', ASYNC_SOCIAL_SHARING_PLUGIN_URL . '/assets/js/async-share.js', array( 'jquery' ), $cache_buster, true );
if ( isset( $options['fb_appid'] ) ) {
wp_localize_script( 'async_js', 'Async_Social_Sharing', array( 'appid' => $options['fb_appid'] ) );
}
if ( ! isset( $options['disable_css'] ) ) {
wp_enqueue_style( 'async_css' );
}
}
/**
* Renders the social sharing widgets.
*
* @return string
*/
public function display_output_view() {
$options = $this->get_options();
if ( ! empty( $options ) ) {
ob_start();
include( ASYNC_SOCIAL_SHARING_PLUGIN_PATH . 'views/output-view.php' );
return ob_get_clean();
}
return;
}
/**
* Determines where the social sharing widgets should be displayed.
*/
public function display_check( $content ) {
$async_share_output = $this->display_output_view();
$options = $this->get_options();
$above_content = $async_share_output . $content;
$below_content = $content . $async_share_output;
// return early instead of hooking into feed content.
if ( is_feed() ) {
return $content;
}
if ( is_page() ) {
if ( is_array( $options['types'] ) && in_array( 'page', $options['types'] ) ) {
if ( isset( $options['position'] ) && 'above' == $options['position'] ) {
return $above_content;
} else {
return $below_content;
}
}
return $content;
}
if ( is_home() || is_paged() ) {
if ( is_array( $options ) && isset( $options['paged'] ) ) {
if ( isset( $options['position'] ) && 'above' == $options['position'] ) {
return $above_content;
} else {
return $below_content;
}
}
return $content;
}
if ( is_single() ) {
$cpt = get_post_type();
if ( is_array( $options['types'] ) && in_array( $cpt, $options['types'] ) ) {
if ( isset( $options['position'] ) && 'above' == $options['position'] ) {
return $above_content;
} else {
return $below_content;
}
}
return $content;
}
return $content;
}
} // end of Async_Social_Sharing class
/**
* Initializes the Async_Social_Sharing class.
*
* @return Object
*/
function async_social() {
return Async_Social_Sharing::get_instance();
}
async_social();
/**
* Outputs async social sharing for manual display.
* Can be used as a template tag within theme files.
*/
function async_social_display() {
$instance = async_social();
echo $instance->display_output_view();
}