-
Notifications
You must be signed in to change notification settings - Fork 0
/
relative-sharer.php
304 lines (253 loc) · 8.38 KB
/
relative-sharer.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* Relative Sharer
*
* @package RelativeMarketing\RelativeSharer
* @author Daniel Gregory
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: Relative Sharer
* Plugin URI: TODO: Add plugin uri
* Description: Adds a sharing par to posts and pages and registers a widget to output links to your social networks.
* Version: 1.0.0
* Author: Relative Marketing
* Author URI: https://relativemarketing.co.uk
* Text Domain: relative-sharer
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
namespace RelativeMarketing\Sharer;
if ( ! defined( 'ABSPATH' ) ) die();
/**
* Constants for easy access to the plugin dir and url
* throughout the plugin.
*/
if ( ! defined( 'RELATIVE_SHARER_DIR' ) )
define( 'RELATIVE_SHARER_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
if ( ! defined( 'RELATIVE_SHARER_URL' ) )
define( 'RELATIVE_SHARER_URL', plugin_dir_url( __FILE__ ) );
/**
* Define option names once to make it easy to update, should we ever need to.
*/
if ( ! defined( 'RELATIVE_SHARER_SOCIAL_NETWORKS' ) )
define( 'RELATIVE_SHARER_SOCIAL_NETWORKS', 'relative_sharer_registered_social_networks' );
if ( ! defined( 'RELATIVE_SHARER_ACTIVE_SOCIAL_NETWORKS' ) )
define( 'RELATIVE_SHARER_ACTIVE_SOCIAL_NETWORKS', 'relative_sharer_active_social_networks' );
if ( ! defined( 'RELATIVE_SHARER_ACTIVE_SOCIAL_NETWORKS_PROFILE' ) )
define( 'RELATIVE_SHARER_ACTIVE_SOCIAL_NETWORKS_PROFILE', RELATIVE_SHARER_ACTIVE_SOCIAL_NETWORKS . '_profile' );
if ( ! defined( 'RELATIVE_SHARER_ACTIVE_SOCIAL_NETWORKS_SHARE' ) )
define( 'RELATIVE_SHARER_ACTIVE_SOCIAL_NETWORKS_SHARE', RELATIVE_SHARER_ACTIVE_SOCIAL_NETWORKS . '_share' );
if ( ! defined( 'RELATIVE_SHARER_AVAILABLE_CONTEXTS' ) ) :
/**
* Not particularly happy with the constant value here, an array value would be better
* but support for array values is only in php 7 and as much as I wish all sites would
* update to 7 it's not always going to be possible (yet).
*
* As a solution define available contexts as a string seperated by a pipe | character
* then use the get_all_available_contexts() helper instead of the constant directly.
*/
define( 'RELATIVE_SHARER_AVAILABLE_CONTEXTS', 'profile|share' );
endif;
/**
* Adds all required scripts and styles to the site
*/
include RELATIVE_SHARER_DIR . 'inc/scripts-styles/loader.php';
/**
* Ensures consistency when adding new social networks
*/
include RELATIVE_SHARER_DIR . 'inc/class-social-network.php';
/**
* Functions to make life easier
*/
include RELATIVE_SHARER_DIR . 'inc/helpers.php';
/**
* Everything to do with creating, reading, updating and deleting plugin data
*/
include RELATIVE_SHARER_DIR . 'inc/social-network-data-management.php';
/**
* Responsible for rendering output in respective context
*/
include RELATIVE_SHARER_DIR . 'inc/rendering/profile.php';
include RELATIVE_SHARER_DIR . 'inc/rendering/share.php';
/**
* Adds a widget to make it easy to add social network links to sidebars and widget areas
*/
include RELATIVE_SHARER_DIR . 'inc/widgets/class-profile-widget.php';
/**
* Test
*/
include RELATIVE_SHARER_DIR . 'tests.php';
// TODO: Move this to it's own file
add_filter( 'the_content', __NAMESPACE__ . '\\output_share_buttons' );
function output_share_buttons( $content ) {
if ( !is_single() ) {
return $content;
}
return get_page_sharer() . $content;
}
function register_default_social_networks() {
/**
* Use some default social networks when activating the plugin for the first time.
*/
// TODO: set final defaults for social network registration on activation
$default = array(
'facebook' => array(
'id' => 'facebook',
'nice_name' => 'Facebook',
'share_link_format' => 'https://www.facebook.com/sharer/sharer.php?u=%2$s',
'social_network_link' => '#',
'icon_type' => 'fa',
'icon' => 'someurl',
'fa_icon' => 'facebook',
),
'twitter' => array(
'id' => 'twitter',
'nice_name' => 'Twitter',
'share_link_format' => 'https://twitter.com/intent/tweet?original_referer=%2$s&url=%2$s',
'social_network_link' => '#',
'icon_type' => 'fa',
'icon' => '',
'fa_icon' => 'twitter',
),
'linkedin' => array(
'id' => 'linkedin',
'nice_name' => 'LinkedIn',
'share_link_format' => 'https://www.linkedin.com/shareArticle?mini=true&url=%2$s&title=%1$s',
'social_network_link' => '#',
'icon_type' => 'fa',
'icon' => '',
'fa_icon' => 'linkedin-in',
),
'instagram' => array(
'id' => 'instagram',
'nice_name' => 'Instagram',
'share_link_format' => '',
'social_network_link' => '',
'icon_type' => 'fa',
'icon' => '',
'fa_icon' => 'instagram',
)
);
foreach ($default as $sn) {
register_and_activate_social_network(
new SocialNetwork(
$sn['id'],
$sn['nice_name'],
$sn['share_link_format'],
$sn['social_network_link'],
$sn['icon'],
$sn['fa_icon']
)
);
}
}
register_activation_hook( __FILE__, __NAMESPACE__ . '\\activate_sharer' );
function activate_sharer() {
// If some options already exist do not do anything
if ( get_option( RELATIVE_SHARER_SOCIAL_NETWORKS ) ) {
return;
}
register_default_social_networks();
}
/**
* Create an options page for this plugin
*/
function create_options_page() {
add_menu_page(
__( 'Relative Sharer', 'relative-sharer' ),
__( 'Relative Sharer', 'relative-sharer' ),
'manage_options',
'relative-sharer',
__NAMESPACE__ . '\\output_options_page',
'dashicons-screenoptions'
);
}
add_action('admin_menu', __NAMESPACE__ . '\\create_options_page' );
function output_options_page() {
echo '<div id="relative-sharer-root"></div>';
}
add_action('rest_api_init', __NAMESPACE__ . '\\rest_data');
function rest_data() {
register_rest_route(
'relative-sharer/v1', 'get-sharing-data',
array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\\get_all_data',
)
);
register_rest_route(
'relative-sharer/v1', 'update-social-network',
array(
'methods' => 'POST',
'callback' => __NAMESPACE__ . '\\endpoint_update_social_network',
// TODO: validate args
// 'args' => ['id' => array( 'validate_callback' => '__return_true' ), 'data' => array( 'validate_callback' => '__return_true' )]
)
);
register_rest_route(
'relative-sharer/v1', 'set-network-visibility',
array(
'methods' => 'POST',
'callback' => __NAMESPACE__ . '\\endpoint_set_network_visibility',
// TODO: validate args
// 'args' => ['id' => array( 'validate_callback' => '__return_true' ), 'data' => array( 'validate_callback' => '__return_true' )]
)
);
register_rest_route(
'relative-sharer/v1', 'update-icon-type',
array(
'methods' => 'POST',
'callback' => __NAMESPACE__ . '\\endpoint_update_icon_type',
// TODO: validate args
// 'args' => ['id' => array( 'validate_callback' => '__return_true' ), 'data' => array( 'validate_callback' => '__return_true' )]
)
);
}
function get_all_data() {
if( ! current_user_can( 'manage_options' )) return;
return array(
'socialNetworks' => get_registered_social_networks_not_assoc(),
'activeShareLinks' => get_active_social_networks('share'),
'activeProfileLinks' => get_active_social_networks('profile')
);
}
/**
* For information about POST requests and getting arguments see:
*
* https://developer.wordpress.org/reference/classes/wp_rest_request/
* https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
*/
function endpoint_update_social_network( \WP_REST_Request $req ) {
if (! current_user_can('manage_options') ) {
return "";
}
$params = $req->get_params();
$data = json_decode($params['data'], true);
return update_social_network( $params['id'], $data );
}
function endpoint_set_network_visibility( \WP_REST_Request $req ) {
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
$params = $req->get_params();
$id = $params['id'];
$activeInContext = $params['activeInContext'];
/**
* Update the active states of the network
*/
$share_action = set_social_network_active_state( $id, 'share', $activeInContext['share'] );
$profile_action = set_social_network_active_state( $id, 'profile', $activeInContext['profile'] );
// Above returns false if an error occurred
if($share_action && $profile_action) {
return true;
}
return false;
}
function endpoint_update_icon_type( \WP_REST_Request $req ) {
if (! current_user_can('manage_options') ) {
return;
}
$params = $req->get_params();
return update_social_network_value( $params['id'], 'icon_type', $params['type']);
}