-
Notifications
You must be signed in to change notification settings - Fork 0
/
dx-share-selection.php
342 lines (274 loc) · 10.5 KB
/
dx-share-selection.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/*
Plugin Name: DX Share Selection
Plugin URI: https://devrix.com/
Plugin Author: nofeairnc
Description: DX Share Selection is a fork of WP Selected Text sharer aiming to share your selected text in social networks. Select a text/code snippet from your post/page and share it to various social media websites.
Version: 1.4
Author: DevriX
Author URI: https://devrix.com/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
define('DXSS_VERSION', '1.4');
if ( ! defined( 'WP_CONTENT_URL' ) ) {
$dxss_pluginpath = get_option( 'siteurl' ) . '/wp-content/plugins/' . plugin_basename( dirname( __FILE__ ) ) . '/';
} else {
$dxss_pluginpath = WP_CONTENT_URL . '/plugins/' . plugin_basename( dirname( __FILE__ ) ) . '/';
}
// Load languages
load_plugin_textdomain( 'dxss', false, basename( dirname( __FILE__ ) ) . '/languages/' );
// Include the files
require_once 'integration.php';
require_once 'dxss-option-helper.php';
// WPSTS Is active check
function dxss_is_active() {
if ( get_option( 'dxss_active' ) == 1 ) {
return 1;
} else {
return 0;
}
}
// WPSTS plugin activate
function dxss_plugin_activate() {
update_option( 'dxss_active', 1 );
}
register_activation_hook( __FILE__, 'dxss_plugin_activate' );
// WPSTS plugin deactivate
function dxss_plugin_deactivate() {
update_option( 'dxss_active', 0 );
}
register_deactivation_hook( __FILE__, 'dxss_plugin_deactivate' );
// Admin Notices
function dxss_admin_notices() {
if ( isset( $_GET['page'] ) && ! dxss_is_active() && $_GET['page'] != 'dx-share-selection' ) {
echo '<div class="updated fade"><p>' . __( '<b>DX Share Selection</b> plugin is intalled. You should immediately adjust <a href="options-general.php?page=dx-share-selection">the settings</a>', 'dxss' ) . '</p></div>';
}
}
add_action( 'admin_notices', 'dxss_admin_notices' );
// Action Links
function dxss_plugin_actions( $links, $file ) {
static $this_plugin;
if ( ! $this_plugin ) {
$this_plugin = plugin_basename( __FILE__ );
}
if ( $file == $this_plugin ) {
$settings_link = '<a href="options-general.php?page=dx-share-selection">' . __( 'Settings', 'dxss' ) . '</a> ' . __( 'Support', 'dxss' ) . '</a>';
$links = array_merge( array( $settings_link ), $links );
}
return $links;
}
add_filter( 'plugin_action_links', 'dxss_plugin_actions', 10, 2 );
// Load the Javascripts
function dxss_admin_js() {
global $dxss_pluginpath;
$admin_js_url = $dxss_pluginpath . 'dxss-admin-js.js';
$color_url = $dxss_pluginpath . '/js/farbtastic/farbtastic.js';
$dxss_js = $dxss_pluginpath . '/dxss/dev/jquery.selected-text-sharer.js';
if ( isset( $_GET['page'] ) && $_GET['page'] == 'dx-share-selection' ) {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'dx-share-selection', $admin_js_url, array( 'jquery' ) );
wp_localize_script( 'dx-share-selection', 'dx_share_selection', array(
'settings_data_default' => DXSS_Option_Helper::get_default_settings_data()
)
);
wp_enqueue_script( 'farbtastic', $color_url, array( 'jquery', 'dx-share-selection' ) );
wp_enqueue_script( 'dxss_js', $dxss_js, array( 'jquery', 'dx-share-selection', 'farbtastic' ) );
}
}
add_action( 'admin_enqueue_scripts', 'dxss_admin_js' );
// Load the admin CSS
function dxss_admin_css() {
global $dxss_pluginpath;
if ( isset( $_GET['page'] ) && $_GET['page'] == 'dx-share-selection' ) {
wp_enqueue_style( 'dsxx-css', $dxss_pluginpath . 'dxss-css.css' );
wp_enqueue_style( 'dxss-admin-css', $dxss_pluginpath . 'dxss-admin-css.css' );
wp_enqueue_style( 'farbtastic-css', $dxss_pluginpath . '/js/farbtastic/farbtastic.css' );
}
}
add_action( 'admin_enqueue_scripts', 'dxss_admin_css' );
// Bitly shorten url
function dxss_shorten_url( $url ) {
// Get the Options
$dxss_settings = DXSS_Option_Helper::fetch_settings_data();
$dxss_bitly = $dxss_settings['bitly'];
$bityly_split = explode( ',', $dxss_bitly );
if ( $bityly_split[0] == '' || $bityly_split[1] == '' ) {
return false;
}
$login = trim( $bityly_split[0] );
$appkey = trim( $bityly_split[1] );
$version = '4';
$bitly = 'https://api-ssl.bitly.com/v' . $version . '/shorten';
$response = wp_remote_post( $bitly, array(
'headers' => array( 'Content-Type' => 'application/json; charset=utf-8', 'Authorization' => 'Bearer ' . $appkey ),
'body' => json_encode( array( 'long_url' => $url ) ),
'method' => 'POST',
'data_format' => 'body',
) );
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
$response_arr = json_decode( $response['body'], true );
if ( ! empty( $response_arr['link'] ) ) {
return $response_arr['link'];
}
}
return false;
}
// One function for getting the url and title of the page
function dxss_get_post_details() {
// Get the global variables
global $post;
// Inside loop
$permalink_inside_loop = get_permalink( $post->ID );
$title_inside_loop = str_replace( '+', '%20', get_the_title( $post->ID ) );
// Outside loop
$permalink_outside_loop = ( ! empty( $_SERVER['HTTPS'] ) ) ? 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] : 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$title_outside_loop = str_replace( '+', '%20', wp_title( '', 0 ) );
// If title is null
if ( $title_outside_loop == '' ) {
$title_outside_loop = str_replace( '+', '%20', get_bloginfo( 'name' ) );
}
if ( in_the_loop() ) {
$details = array(
'permalink' => $permalink_inside_loop,
'title' => $title_inside_loop,
);
} else {
$details = array(
'permalink' => $permalink_outside_loop,
'title' => $title_outside_loop,
);
}
return $details;
}
// Get processed list
function dxss_get_processed_list() {
global $post;
$info = dxss_get_post_details();
$rss = get_bloginfo( 'rss_url' );
$blogname = urlencode( get_bloginfo( 'name' ) . ' ' . get_bloginfo( 'description' ) );
$terms = array(
'{url}',
'{title}',
'{blogname}',
'{rss-url}',
);
$replacable = array(
$info['permalink'],
$info['title'],
$blogname,
$rss,
);
// Get the Options
$dxss_settings = DXSS_Option_Helper::fetch_settings_data();
$dxss_lists = $dxss_settings['lists'];
// Generate short url from Bit.ly only if needed
if ( false !== mb_strpos( $dxss_lists, '{surl}' ) ) {
$terms[] = '{surl}';
$replacable[] = dxss_shorten_url( $info['permalink'] );
}
$listExplode = explode( "\n", $dxss_lists );
$listImplode = implode( '|', $listExplode );
$list = str_replace( "\r", '', $listImplode );
$listFinal = str_replace( $terms, $replacable, $list );
return $listFinal;
}
// Enqueue Scripts to the WordPress
add_action( 'wp_enqueue_scripts', 'dxss_scripts' );
function dxss_scripts() {
global $dxss_pluginpath;
// Get the Options
$dxss_settings = DXSS_Option_Helper::fetch_settings_data();
$dxss_scriptPlace = $dxss_settings['scriptPlace'];
wp_enqueue_style( 'dsxx-css', $dxss_pluginpath . 'dxss-css.css' );
wp_enqueue_script( 'wp-selected-text-searcher', $dxss_pluginpath . 'dxss/dev/jquery.selected-text-sharer.js', array( 'jquery' ), null, $dxss_scriptPlace );
}
// Activate Jquery the Jquery
function dxss_jquery_plugin_activate() {
// Get the Options
$dxss_settings = DXSS_Option_Helper::fetch_settings_data();
$dxss_title = $dxss_settings['title'];
$dxss_lists = $dxss_settings['lists'];
$dxss_borderColor = $dxss_settings['borderColor'];
$dxss_bgColor = $dxss_settings['bgColor'];
$dxss_titleColor = $dxss_settings['titleColor'];
$dxss_hoverColor = $dxss_settings['hoverColor'];
$dxss_textColor = $dxss_settings['textColor'];
$dxss_extraClass = $dxss_settings['extraClass'];
$dxss_element = $dxss_settings['element'];
$dxss_scriptPlace = $dxss_settings['scriptPlace'];
$dxss_truncateChars = $dxss_settings['truncateChars'];
echo "\n" .
"<script type='text/javascript'>
/* <![CDATA[ */
jQuery(document).ready(function(){
if(jQuery('" . $dxss_element . "').length > 0){
jQuery('" . $dxss_element . "').selectedTextSharer({
title : '$dxss_title',
lists : '" . dxss_get_processed_list() . "',
truncateChars : '$dxss_truncateChars',
extraClass : '$dxss_extraClass',
borderColor : '$dxss_borderColor',
background : '$dxss_bgColor',
titleColor : '$dxss_titleColor',
hoverColor : '$dxss_hoverColor',
textColor : '$dxss_textColor'
});
}
});
/* ]]>*/
</script>\n";
}
add_action( 'wp_footer', 'dxss_jquery_plugin_activate' );
## Add the Admin menu
add_action('admin_menu', 'dxss_addpage');
function dxss_addpage() {
add_submenu_page( 'options-general.php', 'DX Share Selection', 'DX Share Selection', 'manage_options', 'dx-share-selection', 'dxss_admin_page' );
}
function dxss_admin_page() {
global $dxss_pluginpath;
$dxss_updated = false;
if ( ! empty( $_POST['dxss_submit'] ) ) {
// Get and store options
$dxss_settings['title'] = $_POST['dxss_title'];
$dxss_settings['lists'] = preg_replace( '/^[ \t]*[\r\n]+/m', '', trim( stripslashes( $_POST['dxss_lists'] ) ) );
$dxss_settings['borderColor'] = $_POST['dxss_borderColor'];
$dxss_settings['bgColor'] = $_POST['dxss_bgColor'];
$dxss_settings['titleColor'] = $_POST['dxss_titleColor'];
$dxss_settings['hoverColor'] = $_POST['dxss_hoverColor'];
$dxss_settings['textColor'] = $_POST['dxss_textColor'];
$dxss_settings['extraClass'] = $_POST['dxss_extraClass'];
$dxss_settings['scriptPlace'] = $_POST['dxss_scriptPlace'];
$dxss_settings['truncateChars'] = $_POST['dxss_truncateChars'];
$dxss_settings['element'] = $_POST['dxss_element'];
$dxss_settings['bitly'] = $_POST['dxss_bitly'];
$dxss_settings['dxss_is_activate'] = 1;
DXSS_Option_Helper::update_settings_data( $dxss_settings );
$dxss_updated = true;
if ( get_option( 'dxss_active' ) == 0 ) {
update_option( 'dxss_active', 1 );
}
}
if ( $dxss_updated == true ) {
echo "<div class='message updated'><p>Updated successfully</p></div>";
}
// Get the Options
$dxss_settings = DXSS_Option_Helper::fetch_settings_data();
$dxss_title = $dxss_settings['title'];
$dxss_lists = $dxss_settings['lists'];
$dxss_borderColor = $dxss_settings['borderColor'];
$dxss_bgColor = $dxss_settings['bgColor'];
$dxss_titleColor = $dxss_settings['titleColor'];
$dxss_hoverColor = $dxss_settings['hoverColor'];
$dxss_textColor = $dxss_settings['textColor'];
$dxss_extraClass = $dxss_settings['extraClass'];
$dxss_element = $dxss_settings['element'];
$dxss_scriptPlace = $dxss_settings['scriptPlace'];
$dxss_truncateChars = $dxss_settings['truncateChars'];
$dxss_bitly = $dxss_settings['bitly'];
/*
Load the admin menu html
* It has php and html mixed up, so a simple readfile() won't work.
*/
require 'dxss.php';
}