1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Exit if accessed directly |
4
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
5
|
|
|
exit; |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Determine if we should share this post when it's being published |
10
|
|
|
* |
11
|
|
|
* @param string $new_status The new status of the post |
12
|
|
|
* @param string $old_status The old status of the poast |
13
|
|
|
* @param object $post The Post Object |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
function ppp_share_on_publish( $new_status, $old_status, $post ) { |
18
|
|
|
// don't publish password protected posts |
19
|
|
|
if ( '' !== $post->post_password ) { |
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if ( $new_status == 'publish' && $old_status != 'publish' ) { |
24
|
|
|
global $ppp_options; |
25
|
|
|
|
26
|
|
|
$allowed_post_types = isset( $ppp_options['post_types'] ) ? $ppp_options['post_types'] : array(); |
27
|
|
|
$allowed_post_types = apply_filters( 'ppp_schedule_share_post_types', $allowed_post_types ); |
28
|
|
|
|
29
|
|
|
if ( !isset( $post->post_status ) || !array_key_exists( $post->post_type, $allowed_post_types ) ) { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
do_action( 'ppp_share_on_publish', $new_status, $old_status, $post ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create timestamps and unique identifiers for each cron. |
41
|
|
|
* @param int $post_id |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
function ppp_get_timestamps( $post_id ) { |
45
|
1 |
|
$times = array(); |
46
|
1 |
|
return apply_filters( 'ppp_get_timestamps', $times, $post_id ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Hook for the crons to fire and send shares |
51
|
|
|
* @param id $post_id |
52
|
|
|
* @param string $name |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
function ppp_share_post( $post_id, $name ) { |
56
|
|
|
global $ppp_options, $ppp_social_settings, $ppp_share_settings, $ppp_twitter_oauth; |
57
|
|
|
|
58
|
|
|
if ( ppp_is_dev_or_staging() ) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// If we've already started to share this, don't share it again. |
63
|
|
|
// Compensates for wp-cron's race conditions |
64
|
|
|
if ( get_transient( 'ppp_sharing' . $name ) === 'true' ) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// For 10 seconds, don't allow another share to go for this post |
69
|
|
|
set_transient( 'ppp_sharing' . $name, 'true', 10 ); |
70
|
|
|
|
71
|
|
|
$name_parts = explode( '_', $name ); |
72
|
|
|
$index = $name_parts[1]; |
73
|
|
|
$service = isset( $name_parts[3] ) ? $name_parts[3] : 'tw'; |
74
|
|
|
|
75
|
|
|
// If we're fired on a cron, check for stale cron runs |
76
|
|
|
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
77
|
|
|
switch( $service ) { |
78
|
|
|
case 'tw': |
79
|
|
|
$post_meta = get_post_meta( $post_id, '_ppp_tweets', true ); |
80
|
|
|
break; |
81
|
|
|
|
82
|
|
|
case 'fb': |
83
|
|
|
$post_meta = get_post_meta( $post_id, '_ppp_fb_shares', true ); |
84
|
|
|
break; |
85
|
|
|
|
86
|
|
|
case 'li': |
87
|
|
|
$post_meta = get_post_meta( $post_id, '_ppp_li_shares', true ); |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$share_data = array(); |
92
|
|
|
if ( isset( $post_meta[ $index ] ) ) { |
93
|
|
|
$share_data = $post_meta[ $index ]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$timestamp = ppp_generate_timestamp( $share_data['date'], $share_data['time'] ); |
97
|
|
|
|
98
|
|
|
// If the current time is more than 60 minutes (filterable) overdue, don't fire the share |
99
|
|
|
$share_buffer = apply_filters( 'ppp_share_buffer', HOUR_IN_SECONDS ); |
100
|
|
|
if ( ( current_time( 'timestamp', 1 ) - $timestamp ) > $share_buffer ) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
do_action( 'ppp_share_scheduled_' . $service, $post_id, $index, $name ); |
106
|
|
|
|
107
|
|
|
// Just in case we get caught in a weird state, kill this share immediately after we execute it. |
108
|
|
|
wp_clear_scheduled_hook( 'ppp_share_post_event', array( $post_id, $name ) ); |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the Social Share Tokens from the API |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
function ppp_set_social_tokens() { |
117
|
|
|
if ( ( defined( 'PPP_TW_CONSUMER_KEY' ) && defined( 'PPP_TW_CONSUMER_SECRET' ) ) || |
118
|
|
|
( defined( 'LINKEDIN_KEY' ) && defined( 'LINKEDIN_SECRET' ) ) || |
119
|
|
|
( defined( 'bitly_clientid' ) && defined( 'bitly_secret' ) ) || |
120
|
|
|
( defined( 'PPP_FB_APP_ID' ) && defined( 'PPP_FB_APP_SECRET' ) ) |
121
|
|
|
) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$social_tokens = ppp_has_local_tokens(); |
126
|
|
|
|
127
|
|
|
if ( empty( $social_tokens ) ) { |
128
|
|
|
if ( ! defined( 'PPP_LOCAL_TOKENS' ) ) { |
129
|
|
|
define( 'PPP_LOCAL_TOKENS', false ); |
130
|
|
|
} |
131
|
|
|
$social_tokens = get_transient( 'ppp_social_tokens' ); |
132
|
|
|
|
133
|
|
|
if ( ! $social_tokens ) { |
134
|
|
|
$license = trim( get_option( '_ppp_license_key' ) ); |
135
|
|
|
$url = PPP_STORE_URL . '/?ppp-get-tokens&ppp-license-key=' . $license . '&ver=' . md5( time() . $license ); |
136
|
|
|
$response = wp_remote_get( $url, array( 'timeout' => 15, 'sslverify' => false ) ); |
137
|
|
|
|
138
|
|
|
if ( is_wp_error( $response ) ) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$social_tokens = json_decode( wp_remote_retrieve_body( $response ) ); |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} else { |
147
|
|
|
if ( ! defined ( 'PPP_LOCAL_TOKENS' ) ) { |
148
|
|
|
define( 'PPP_LOCAL_TOKENS', true ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
delete_transient( 'ppp_social_tokens' ); |
152
|
|
|
|
153
|
|
|
if ( isset( $social_tokens->options ) ) { |
154
|
|
|
foreach ( $social_tokens->options as $constant => $value ) { |
155
|
|
|
|
156
|
|
|
$constant = strtoupper( $constant ); |
157
|
|
|
|
158
|
|
|
if ( defined( $constant ) ) { |
159
|
|
|
continue; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
switch( $constant ) { |
163
|
|
|
|
164
|
|
|
case 'NO_AUTO_UPDATE': |
165
|
|
|
// Avoid the call to the API to check for software updates |
166
|
|
|
$value = is_bool( $value ) ? $value : false; |
167
|
|
|
if ( ! defined( 'NO_AUTO_UPDATE' ) ) { |
168
|
|
|
define( 'NO_AUTO_UPDATE', $value ); |
169
|
|
|
} |
170
|
|
|
break; |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ( false === PPP_LOCAL_TOKENS && ! isset( $social_tokens->error ) && ( isset( $social_tokens->twitter ) || isset( $social_tokens->facebook ) || isset( $social_tokens->linkedin ) ) ) { |
178
|
|
|
set_transient( 'ppp_social_tokens', $social_tokens, WEEK_IN_SECONDS ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
do_action( 'ppp_set_social_token_constants', $social_tokens ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Checks if the user has uploaded a social media tokens JSON file |
186
|
|
|
* @return boolean If a file exists, true, else false. |
187
|
|
|
*/ |
188
|
|
|
function ppp_has_local_tokens() { |
189
|
|
|
|
190
|
1 |
|
$token_file = apply_filters( 'ppp_local_social_token_path', ppp_get_upload_path() . '/ppp-social-tokens.json' ); |
191
|
1 |
|
$local_tokens = false; |
192
|
|
|
|
193
|
1 |
|
if ( ! file_exists( $token_file ) ) { |
194
|
1 |
|
return $local_tokens; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$local_tokens = json_decode( file_get_contents( $token_file ) ); |
198
|
|
|
|
199
|
|
|
// Failed to parse as JSON |
200
|
|
|
if ( false === $local_tokens ) { |
201
|
|
|
return $local_tokens; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// No social tokens found in the format we accept or it was empty |
205
|
|
|
if ( empty( $local_tokens ) || ( ! isset( $local_tokens->twitter ) || ! isset( $local_tokens->facebook ) || ! isset( $local_tokens->linkedin ) ) ) { |
206
|
|
|
return false; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return apply_filters( 'ppp_local_tokens', $local_tokens, $token_file ); |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Generate the link for the share |
215
|
|
|
* @param int $post_id The Post ID |
216
|
|
|
* @param string $name The 'Name from the cron' |
217
|
|
|
* @return string The URL to the post, to share |
218
|
|
|
*/ |
219
|
|
|
function ppp_generate_link( $post_id, $name, $scheduled = true ) { |
220
|
1 |
|
global $ppp_share_settings; |
221
|
1 |
|
$share_link = get_permalink( $post_id ); |
222
|
|
|
|
223
|
1 |
|
if ( ppp_link_tracking_enabled() ) { |
224
|
1 |
|
$share_link = ppp_generate_link_tracking( $share_link, $post_id, $name ); |
225
|
1 |
|
} |
226
|
|
|
|
227
|
1 |
|
if ( ppp_is_shortener_enabled() && $scheduled ) { |
228
|
|
|
$shortener_name = $ppp_share_settings['shortener']; |
229
|
|
|
$share_link = apply_filters( 'ppp_apply_shortener-' . $shortener_name, $share_link ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
1 |
|
return apply_filters( 'ppp_share_link', $share_link ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Given a link, determine if link tracking needs to be applied |
238
|
|
|
* @param string $share_link The Link to share |
239
|
|
|
* @param int $post_id The Post ID the link belongs to |
240
|
|
|
* @param string $name The Name string from the cron |
241
|
|
|
* @return string The URL to post, with proper analytics applied if necessary |
242
|
|
|
*/ |
243
|
|
|
function ppp_generate_link_tracking( $share_link, $post_id, $name ) { |
244
|
1 |
|
if ( ppp_link_tracking_enabled() ) { |
245
|
1 |
|
global $ppp_share_settings; |
246
|
1 |
|
$link_tracking_type = $ppp_share_settings['analytics']; |
247
|
|
|
|
248
|
|
|
// Given the setting name, devs can extend this and apply a filter of ppp_analytics-[setting value] |
249
|
|
|
// to apply their own rules for link tracking |
250
|
1 |
|
$share_link = apply_filters( 'ppp_analytics-' . $link_tracking_type, $share_link, $post_id, $name ); |
251
|
1 |
|
} |
252
|
|
|
|
253
|
1 |
|
$share_link = apply_filters( 'ppp_generate_link_tracking', $share_link, $post_id, $name ); |
254
|
|
|
|
255
|
1 |
|
return $share_link; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Determines if the post being shared should has media attached |
260
|
|
|
* @param int $post_id Post ID |
261
|
|
|
* @param string $network The Network being shared to |
262
|
|
|
* @param bool $use_media If this share should use media or not |
263
|
|
|
* @return mixed If a thumbnail is found returns the URL, otherwise returns false |
264
|
|
|
*/ |
265
|
|
|
function ppp_post_has_media( $post_id, $network, $use_media, $attachment_id = false ) { |
266
|
|
|
if ( !$use_media || empty( $post_id ) || empty( $network ) ) { |
267
|
|
|
return false; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$thumb_id = empty( $attachment_id ) ? get_post_thumbnail_id( $post_id ) : $attachment_id; |
271
|
|
|
$thumb_url = wp_get_attachment_image_src( $thumb_id, 'ppp-' . $network . '-share-image', true ); |
272
|
|
|
|
273
|
|
|
if ( isset( $thumb_url[0] ) && ! empty( $thumb_url[0] ) && !strpos( $thumb_url[0], 'wp-includes/images/media/default.png' ) ) { |
274
|
|
|
return $thumb_url[0]; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return false; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Attempt to get an attachment ID from an Image URL |
282
|
|
|
* @since 2.4 |
283
|
|
|
* @param $image_url |
284
|
|
|
* |
285
|
|
|
* @return int The Attachment ID |
286
|
|
|
*/ |
287
|
|
|
function ppp_get_attachment_id_from_image_url( $image_url ) { |
288
|
|
|
global $wpdb; |
289
|
|
|
$attachment_id = 0; |
290
|
|
|
$parts = parse_url( $image_url ); |
291
|
|
|
$path = explode( '/', $parts['path'] ); |
292
|
|
|
$filename = end( $path ); |
293
|
|
|
|
294
|
|
|
// First check a social media share (thumbnail sizes) |
295
|
|
|
$thumb_id = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value LIKE '%s';", '%' . $wpdb->esc_like( $filename ) . '%' ) ); |
296
|
|
|
if ( ! empty( $thumb_id[0] ) ) { |
297
|
|
|
$attachment_id = $thumb_id[0]; |
298
|
|
|
} else { |
299
|
|
|
|
300
|
|
|
// We didn't find one via the thumbnail sizes, try the full image URL. |
301
|
|
|
$thumb_id = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ) ); |
302
|
|
|
if ( ! empty( $thumb_id[0] ) ) { |
303
|
|
|
$attachment_id = $thumb_id[0]; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $attachment_id; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Get the alt-text defined in the WP Media Uploader for an attachment. |
312
|
|
|
* @since 2.4 |
313
|
|
|
* @param $attachment_id |
314
|
|
|
* |
315
|
|
|
* @return string The alt-text |
316
|
|
|
*/ |
317
|
|
|
function ppp_get_attachment_alt_text( $attachment_id ) { |
318
|
|
|
$alt_text = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); |
319
|
|
|
return apply_filters( 'ppp_attachment_alt_text', $alt_text, $attachment_id ); |
320
|
|
|
} |