|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
abstract class Publicize_Base { |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Services that are currently connected to the given user |
|
7
|
|
|
* through publicize. |
|
8
|
|
|
*/ |
|
9
|
|
|
public $connected_services = array(); |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Services that are supported by publicize. They don't |
|
13
|
|
|
* necessarily need to be connected to the current user. |
|
14
|
|
|
*/ |
|
15
|
|
|
public $services; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* key names for post meta |
|
19
|
|
|
*/ |
|
20
|
|
|
public $ADMIN_PAGE = 'wpas'; |
|
21
|
|
|
public $POST_MESS = '_wpas_mess'; |
|
22
|
|
|
public $POST_SKIP = '_wpas_skip_'; // connection id appended to indicate that a connection should NOT be publicized to |
|
23
|
|
|
public $POST_DONE = '_wpas_done_'; // connection id appended to indicate a connection has already been publicized to |
|
24
|
|
|
public $USER_AUTH = 'wpas_authorize'; |
|
25
|
|
|
public $USER_OPT = 'wpas_'; |
|
26
|
|
|
public $PENDING = '_publicize_pending'; // ready for Publicize to do its thing |
|
27
|
|
|
public $POST_SERVICE_DONE = '_publicize_done_external'; // array of external ids where we've Publicized |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* default pieces of the message used in constructing the |
|
31
|
|
|
* content pushed out to other social networks |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
public $default_prefix = ''; |
|
35
|
|
|
public $default_message = '%title%'; |
|
36
|
|
|
public $default_suffix = ' '; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* What WP capability is require to create/delete global connections? |
|
40
|
|
|
* All users with this cap can un-globalize all other global connections, and globalize any of their own |
|
41
|
|
|
* Globalized connections cannot be unselected by users without this capability when publishing |
|
42
|
|
|
*/ |
|
43
|
|
|
public $GLOBAL_CAP = 'publish_posts'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sets up the basics of Publicize |
|
47
|
|
|
*/ |
|
48
|
|
|
function __construct() { |
|
49
|
|
|
$this->default_message = self::build_sprintf( array( |
|
50
|
|
|
/** |
|
51
|
|
|
* Filter the default Publicize message. |
|
52
|
|
|
* |
|
53
|
|
|
* @module publicize |
|
54
|
|
|
* |
|
55
|
|
|
* @since 2.0.0 |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $this->default_message Publicize's default message. Default is the post title. |
|
58
|
|
|
*/ |
|
59
|
|
|
apply_filters( 'wpas_default_message', $this->default_message ), |
|
60
|
|
|
'title', |
|
61
|
|
|
'url', |
|
62
|
|
|
) ); |
|
63
|
|
|
|
|
64
|
|
|
$this->default_prefix = self::build_sprintf( array( |
|
65
|
|
|
/** |
|
66
|
|
|
* Filter the message prepended to the Publicize custom message. |
|
67
|
|
|
* |
|
68
|
|
|
* @module publicize |
|
69
|
|
|
* |
|
70
|
|
|
* @since 2.0.0 |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $this->default_prefix String prepended to the Publicize custom message. |
|
73
|
|
|
*/ |
|
74
|
|
|
apply_filters( 'wpas_default_prefix', $this->default_prefix ), |
|
75
|
|
|
'url', |
|
76
|
|
|
) ); |
|
77
|
|
|
|
|
78
|
|
|
$this->default_suffix = self::build_sprintf( array( |
|
79
|
|
|
/** |
|
80
|
|
|
* Filter the message appended to the Publicize custom message. |
|
81
|
|
|
* |
|
82
|
|
|
* @module publicize |
|
83
|
|
|
* |
|
84
|
|
|
* @since 2.0.0 |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $this->default_suffix String appended to the Publicize custom message. |
|
87
|
|
|
*/ |
|
88
|
|
|
apply_filters( 'wpas_default_suffix', $this->default_suffix ), |
|
89
|
|
|
'url', |
|
90
|
|
|
) ); |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Filter the capability to change global Publicize connection options. |
|
94
|
|
|
* |
|
95
|
|
|
* All users with this cap can un-globalize all other global connections, and globalize any of their own |
|
96
|
|
|
* Globalized connections cannot be unselected by users without this capability when publishing. |
|
97
|
|
|
* |
|
98
|
|
|
* @module publicize |
|
99
|
|
|
* |
|
100
|
|
|
* @since 2.2.1 |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $this->GLOBAL_CAP default capability in control of global Publicize connection options. Default to edit_others_posts. |
|
103
|
|
|
*/ |
|
104
|
|
|
$this->GLOBAL_CAP = apply_filters( 'jetpack_publicize_global_connections_cap', $this->GLOBAL_CAP ); |
|
105
|
|
|
|
|
106
|
|
|
// stage 1 and 2 of 3-stage Publicize. Flag for Publicize on creation, save meta, |
|
107
|
|
|
// then check meta and publicize based on that. stage 3 implemented on wpcom |
|
108
|
|
|
add_action( 'transition_post_status', array( $this, 'flag_post_for_publicize' ), 10, 3 ); |
|
109
|
|
|
add_action( 'save_post', array( &$this, 'save_meta' ), 20, 2 ); |
|
110
|
|
|
|
|
111
|
|
|
// Default checkbox state for each Connection |
|
112
|
|
|
add_filter( 'publicize_checkbox_default', array( $this, 'publicize_checkbox_default' ), 10, 4 ); |
|
113
|
|
|
|
|
114
|
|
|
// Alter the "Post Publish" admin notice to mention the Connections we Publicized to. |
|
115
|
|
|
add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 20, 1 ); |
|
116
|
|
|
|
|
117
|
|
|
// Connection test callback |
|
118
|
|
|
add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) ); |
|
119
|
|
|
|
|
120
|
|
|
add_action( 'init', array( $this, 'add_post_type_support' ) ); |
|
121
|
|
|
add_action( 'init', array( $this, 'register_post_meta' ), 20 ); |
|
122
|
|
|
add_action( 'jetpack_register_gutenberg_extensions', array( $this, 'register_gutenberg_extension' ) ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/* |
|
126
|
|
|
* Services: Facebook, Twitter, etc. |
|
127
|
|
|
*/ |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get services for the given blog and user. |
|
131
|
|
|
* |
|
132
|
|
|
* Can return all available services or just the ones with an active connection. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $filter |
|
135
|
|
|
* 'all' (default) - Get all services available for connecting |
|
136
|
|
|
* 'connected' - Get all services currently connected |
|
137
|
|
|
* @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
|
138
|
|
|
* @param false|int $_user_id The user ID. Use false (default) for the current user |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
abstract function get_services( $filter = 'all', $_blog_id = false, $_user_id = false ); |
|
142
|
|
|
|
|
143
|
|
|
function can_connect_service( $service_name ) { |
|
144
|
|
|
return 'google_plus' !== $service_name; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Does the given user have a connection to the service on the given blog? |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
151
|
|
|
* @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
|
152
|
|
|
* @param false|int $_user_id The user ID. Use false (default) for the current user |
|
153
|
|
|
* @return bool |
|
154
|
|
|
*/ |
|
155
|
|
|
function is_enabled( $service_name, $_blog_id = false, $_user_id = false ) { |
|
156
|
|
|
if ( !$_blog_id ) |
|
|
|
|
|
|
157
|
|
|
$_blog_id = $this->blog_id(); |
|
158
|
|
|
|
|
159
|
|
|
if ( !$_user_id ) |
|
|
|
|
|
|
160
|
|
|
$_user_id = $this->user_id(); |
|
161
|
|
|
|
|
162
|
|
|
$connections = $this->get_connections( $service_name, $_blog_id, $_user_id ); |
|
163
|
|
|
return ( is_array( $connections ) && count( $connections ) > 0 ? true : false ); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Generates a connection URL. |
|
168
|
|
|
* |
|
169
|
|
|
* This is the URL, which, when visited by the user, starts the authentication |
|
170
|
|
|
* process required to forge a connection. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
|
|
abstract function connect_url( $service_name ); |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Generates a Connection refresh URL. |
|
179
|
|
|
* |
|
180
|
|
|
* This is the URL, which, when visited by the user, re-authenticates their |
|
181
|
|
|
* connection to the service. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
abstract function refresh_url( $service_name ); |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Generates a disconnection URL. |
|
190
|
|
|
* |
|
191
|
|
|
* This is the URL, which, when visited by the user, breaks their connection |
|
192
|
|
|
* with the service. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
195
|
|
|
* @param string $connection_id Connection ID |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
abstract function disconnect_url( $service_name, $connection_id ); |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns a display name for the Service |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
public static function get_service_label( $service_name ) { |
|
207
|
|
|
switch ( $service_name ) { |
|
208
|
|
|
case 'linkedin': |
|
209
|
|
|
return 'LinkedIn'; |
|
210
|
|
|
break; |
|
|
|
|
|
|
211
|
|
|
case 'google_plus': |
|
212
|
|
|
return 'Google+'; |
|
213
|
|
|
break; |
|
|
|
|
|
|
214
|
|
|
case 'twitter': |
|
215
|
|
|
case 'facebook': |
|
216
|
|
|
case 'tumblr': |
|
217
|
|
|
default: |
|
218
|
|
|
return ucfirst( $service_name ); |
|
219
|
|
|
break; |
|
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/* |
|
224
|
|
|
* Connections: For each Service, there can be multiple connections |
|
225
|
|
|
* for a given user. For example, one user could be connected to Twitter |
|
226
|
|
|
* as both @jetpack and as @wordpressdotcom |
|
227
|
|
|
* |
|
228
|
|
|
* For historical reasons, Connections are represented as an object |
|
229
|
|
|
* on WordPress.com and as an array in Jetpack. |
|
230
|
|
|
*/ |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get the active Connections of a Service |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
236
|
|
|
* @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
|
237
|
|
|
* @param false|int $_user_id The user ID. Use false (default) for the current user |
|
238
|
|
|
* @return false|object[]|array[] false if no connections exist |
|
239
|
|
|
*/ |
|
240
|
|
|
abstract function get_connections( $service_name, $_blog_id = false, $_user_id = false ); |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Get a single Connection of a Service |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
246
|
|
|
* @param string $connection_id Connection ID |
|
247
|
|
|
* @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
|
248
|
|
|
* @param false|int $_user_id The user ID. Use false (default) for the current user |
|
249
|
|
|
* @return false|object[]|array[] false if no connections exist |
|
250
|
|
|
*/ |
|
251
|
|
|
abstract function get_connection( $service_name, $connection_id, $_blog_id = false, $_user_id = false ); |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Get the Connection ID. |
|
255
|
|
|
* |
|
256
|
|
|
* Note that this is different than the Connection's uniqueid. |
|
257
|
|
|
* |
|
258
|
|
|
* Via a quirk of history, ID is globally unique and unique_id |
|
259
|
|
|
* is only unique per site. |
|
260
|
|
|
* |
|
261
|
|
|
* @param object|array The Connection object (WordPress.com) or array (Jetpack) |
|
262
|
|
|
* @return string |
|
263
|
|
|
*/ |
|
264
|
|
|
abstract function get_connection_id( $connection ); |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Get the Connection unique_id |
|
268
|
|
|
* |
|
269
|
|
|
* Note that this is different than the Connections ID. |
|
270
|
|
|
* |
|
271
|
|
|
* Via a quirk of history, ID is globally unique and unique_id |
|
272
|
|
|
* is only unique per site. |
|
273
|
|
|
* |
|
274
|
|
|
* @param object|array The Connection object (WordPress.com) or array (Jetpack) |
|
275
|
|
|
* @return string |
|
276
|
|
|
*/ |
|
277
|
|
|
abstract function get_connection_unique_id( $connection ); |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Get the Connection's Meta data |
|
281
|
|
|
* |
|
282
|
|
|
* @param object|array Connection |
|
283
|
|
|
* @return array Connection Meta |
|
284
|
|
|
*/ |
|
285
|
|
|
abstract function get_connection_meta( $connection ); |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Disconnect a Connection |
|
289
|
|
|
* |
|
290
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
291
|
|
|
* @param string $connection_id Connection ID |
|
292
|
|
|
* @param false|int $_blog_id The blog ID. Use false (default) for the current blog |
|
293
|
|
|
* @param false|int $_user_id The user ID. Use false (default) for the current user |
|
294
|
|
|
* @param bool $force_delete Whether to skip permissions checks |
|
295
|
|
|
* @return false|void False on failure. Void on success. |
|
296
|
|
|
*/ |
|
297
|
|
|
abstract function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Globalizes a Connection |
|
301
|
|
|
* |
|
302
|
|
|
* @param string $connection_id Connection ID |
|
303
|
|
|
* @return bool Falsey on failure. Truthy on success. |
|
304
|
|
|
*/ |
|
305
|
|
|
abstract function globalize_connection( $connection_id ); |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Unglobalizes a Connection |
|
309
|
|
|
* |
|
310
|
|
|
* @param string $connection_id Connection ID |
|
311
|
|
|
* @return bool Falsey on failure. Truthy on success. |
|
312
|
|
|
*/ |
|
313
|
|
|
abstract function unglobalize_connection( $connection_id ); |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Returns an external URL to the Connection's profile |
|
317
|
|
|
* |
|
318
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
319
|
|
|
* @param object|array The Connection object (WordPress.com) or array (Jetpack) |
|
320
|
|
|
* @return false|string False on failure. URL on success. |
|
321
|
|
|
*/ |
|
322
|
|
|
function get_profile_link( $service_name, $connection ) { |
|
323
|
|
|
$cmeta = $this->get_connection_meta( $connection ); |
|
324
|
|
|
|
|
325
|
|
|
if ( isset( $cmeta['connection_data']['meta']['link'] ) ) { |
|
326
|
|
|
if ( 'facebook' == $service_name && 0 === strpos( parse_url( $cmeta['connection_data']['meta']['link'], PHP_URL_PATH ), '/app_scoped_user_id/' ) ) { |
|
327
|
|
|
// App-scoped Facebook user IDs are not usable profile links |
|
328
|
|
|
return false; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
return $cmeta['connection_data']['meta']['link']; |
|
332
|
|
View Code Duplication |
} elseif ( 'facebook' == $service_name && isset( $cmeta['connection_data']['meta']['facebook_page'] ) ) { |
|
333
|
|
|
return 'https://facebook.com/' . $cmeta['connection_data']['meta']['facebook_page']; |
|
334
|
|
|
} elseif ( 'tumblr' == $service_name && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) { |
|
335
|
|
|
return 'http://' . $cmeta['connection_data']['meta']['tumblr_base_hostname']; |
|
336
|
|
|
} elseif ( 'twitter' == $service_name ) { |
|
337
|
|
|
return 'https://twitter.com/' . substr( $cmeta['external_display'], 1 ); // Has a leading '@' |
|
338
|
|
View Code Duplication |
} elseif ( 'google_plus' == $service_name && isset( $cmeta['connection_data']['meta']['google_plus_page'] ) ) { |
|
339
|
|
|
return 'https://plus.google.com/' . $cmeta['connection_data']['meta']['google_plus_page']; |
|
340
|
|
|
} elseif ( 'google_plus' == $service_name ) { |
|
341
|
|
|
return 'https://plus.google.com/' . $cmeta['external_id']; |
|
342
|
|
|
} else if ( 'linkedin' == $service_name ) { |
|
343
|
|
|
if ( !isset( $cmeta['connection_data']['meta']['profile_url'] ) ) { |
|
344
|
|
|
return false; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
$profile_url_query = parse_url( $cmeta['connection_data']['meta']['profile_url'], PHP_URL_QUERY ); |
|
348
|
|
|
wp_parse_str( $profile_url_query, $profile_url_query_args ); |
|
|
|
|
|
|
349
|
|
|
if ( isset( $profile_url_query_args['key'] ) ) { |
|
|
|
|
|
|
350
|
|
|
$id = $profile_url_query_args['key']; |
|
351
|
|
|
} elseif ( isset( $profile_url_query_args['id'] ) ) { |
|
|
|
|
|
|
352
|
|
|
$id = $profile_url_query_args['id']; |
|
353
|
|
|
} else { |
|
354
|
|
|
return false; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
return esc_url_raw( add_query_arg( 'id', urlencode( $id ), 'http://www.linkedin.com/profile/view' ) ); |
|
358
|
|
|
} else { |
|
359
|
|
|
return false; // no fallback. we just won't link it |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Returns a display name for the Connection |
|
365
|
|
|
* |
|
366
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
367
|
|
|
* @param object|array The Connection object (WordPress.com) or array (Jetpack) |
|
368
|
|
|
* @return string |
|
369
|
|
|
*/ |
|
370
|
|
|
function get_display_name( $service_name, $connection ) { |
|
371
|
|
|
$cmeta = $this->get_connection_meta( $connection ); |
|
372
|
|
|
|
|
373
|
|
|
if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) { |
|
374
|
|
|
return $cmeta['connection_data']['meta']['display_name']; |
|
375
|
|
View Code Duplication |
} elseif ( $service_name == 'tumblr' && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) { |
|
376
|
|
|
return $cmeta['connection_data']['meta']['tumblr_base_hostname']; |
|
377
|
|
|
} elseif ( $service_name == 'twitter' ) { |
|
378
|
|
|
return $cmeta['external_display']; |
|
379
|
|
|
} else { |
|
380
|
|
|
$connection_display = $cmeta['external_display']; |
|
381
|
|
|
if ( empty( $connection_display ) ) |
|
382
|
|
|
$connection_display = $cmeta['external_name']; |
|
383
|
|
|
return $connection_display; |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Whether the user needs to select additional options after connecting |
|
389
|
|
|
* |
|
390
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
391
|
|
|
* @param object|array The Connection object (WordPress.com) or array (Jetpack) |
|
392
|
|
|
* @return bool |
|
393
|
|
|
*/ |
|
394
|
|
|
function show_options_popup( $service_name, $connection ) { |
|
395
|
|
|
$cmeta = $this->get_connection_meta( $connection ); |
|
396
|
|
|
|
|
397
|
|
|
// always show if no selection has been made for facebook |
|
398
|
|
|
if ( 'facebook' == $service_name && empty( $cmeta['connection_data']['meta']['facebook_profile'] ) && empty( $cmeta['connection_data']['meta']['facebook_page'] ) ) |
|
399
|
|
|
return true; |
|
400
|
|
|
|
|
401
|
|
|
// always show if no selection has been made for tumblr |
|
402
|
|
|
if ( 'tumblr' == $service_name && empty ( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) |
|
403
|
|
|
return true; |
|
404
|
|
|
|
|
405
|
|
|
// if we have the specific connection info.. |
|
406
|
|
|
if ( isset( $_GET['id'] ) ) { |
|
407
|
|
|
if ( $cmeta['connection_data']['id'] == $_GET['id'] ) |
|
408
|
|
|
return true; |
|
409
|
|
|
} else { |
|
410
|
|
|
// otherwise, just show if this is the completed step / first load |
|
411
|
|
|
if ( !empty( $_GET['action'] ) && 'completed' == $_GET['action'] && !empty( $_GET['service'] ) && $service_name == $_GET['service'] && ! in_array( $_GET['service'], array( 'facebook', 'tumblr' ) ) ) |
|
412
|
|
|
return true; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
return false; |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Whether the Connection is "valid" wrt Facebook's requirements. |
|
420
|
|
|
* |
|
421
|
|
|
* Must be connected to a Page (not a Profile). |
|
422
|
|
|
* (Also returns true if we're in the middle of the connection process) |
|
423
|
|
|
* |
|
424
|
|
|
* @param object|array The Connection object (WordPress.com) or array (Jetpack) |
|
425
|
|
|
* @return bool |
|
426
|
|
|
*/ |
|
427
|
|
|
function is_valid_facebook_connection( $connection ) { |
|
428
|
|
|
if ( $this->is_connecting_connection( $connection ) ) { |
|
429
|
|
|
return true; |
|
430
|
|
|
} |
|
431
|
|
|
$connection_meta = $this->get_connection_meta( $connection ); |
|
432
|
|
|
$connection_data = $connection_meta['connection_data']; |
|
433
|
|
|
return isset( $connection_data[ 'meta' ][ 'facebook_page' ] ); |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Whether the Connection currently being connected |
|
438
|
|
|
* |
|
439
|
|
|
* @param object|array The Connection object (WordPress.com) or array (Jetpack) |
|
440
|
|
|
* @return bool |
|
441
|
|
|
*/ |
|
442
|
|
|
function is_connecting_connection( $connection ) { |
|
443
|
|
|
$connection_meta = $this->get_connection_meta( $connection ); |
|
444
|
|
|
$connection_data = $connection_meta['connection_data']; |
|
445
|
|
|
return isset( $connection_data[ 'meta' ]['options_responses'] ); |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
/** |
|
449
|
|
|
* AJAX Handler to run connection tests on all Connections |
|
450
|
|
|
* @return void |
|
451
|
|
|
*/ |
|
452
|
|
|
function test_publicize_conns() { |
|
453
|
|
|
wp_send_json_success( $this->get_publicize_conns_test_results() ); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* Run connection tests on all Connections |
|
458
|
|
|
* |
|
459
|
|
|
* @return array { |
|
460
|
|
|
* Array of connection test results. |
|
461
|
|
|
* |
|
462
|
|
|
* @type string 'connectionID' Connection identifier string that is unique for each connection |
|
463
|
|
|
* @type string 'serviceName' Slug of the connection's service (facebook, twitter, ...) |
|
464
|
|
|
* @type bool 'connectionTestPassed' Whether the connection test was successful |
|
465
|
|
|
* @type string 'connectionTestMessage' Test success or error message |
|
466
|
|
|
* @type bool 'userCanRefresh' Whether the user can re-authenticate their connection to the service |
|
467
|
|
|
* @type string 'refreshText' Message instructing user to re-authenticate their connection to the service |
|
468
|
|
|
* @type string 'refreshURL' URL, which, when visited by the user, re-authenticates their connection to the service. |
|
469
|
|
|
* @type string 'unique_id' ID string representing connection |
|
470
|
|
|
* } |
|
471
|
|
|
*/ |
|
472
|
|
|
function get_publicize_conns_test_results() { |
|
473
|
|
|
$test_results = array(); |
|
474
|
|
|
|
|
475
|
|
|
foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) { |
|
476
|
|
|
foreach ( $connections as $connection ) { |
|
477
|
|
|
|
|
478
|
|
|
$id = $this->get_connection_id( $connection ); |
|
479
|
|
|
|
|
480
|
|
|
$connection_test_passed = true; |
|
481
|
|
|
$connection_test_message = __( 'This connection is working correctly.' , 'jetpack' ); |
|
482
|
|
|
$user_can_refresh = false; |
|
483
|
|
|
$refresh_text = ''; |
|
484
|
|
|
$refresh_url = ''; |
|
485
|
|
|
|
|
486
|
|
|
$connection_test_result = true; |
|
487
|
|
|
if ( method_exists( $this, 'test_connection' ) ) { |
|
488
|
|
|
$connection_test_result = $this->test_connection( $service_name, $connection ); |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
if ( is_wp_error( $connection_test_result ) ) { |
|
492
|
|
|
$connection_test_passed = false; |
|
493
|
|
|
$connection_test_message = $connection_test_result->get_error_message(); |
|
494
|
|
|
$error_data = $connection_test_result->get_error_data(); |
|
495
|
|
|
|
|
496
|
|
|
$user_can_refresh = $error_data['user_can_refresh']; |
|
497
|
|
|
$refresh_text = $error_data['refresh_text']; |
|
498
|
|
|
$refresh_url = $error_data['refresh_url']; |
|
499
|
|
|
} |
|
500
|
|
|
// Mark facebook profiles as deprecated |
|
501
|
|
|
if ( 'facebook' === $service_name ) { |
|
502
|
|
|
if ( ! $this->is_valid_facebook_connection( $connection ) ) { |
|
503
|
|
|
$connection_test_passed = false; |
|
504
|
|
|
$user_can_refresh = false; |
|
505
|
|
|
$connection_test_message = __( 'Please select a Facebook Page to publish updates.', 'jetpack' ); |
|
506
|
|
|
} |
|
507
|
|
|
} |
|
508
|
|
|
|
|
509
|
|
|
$unique_id = null; |
|
510
|
|
View Code Duplication |
if ( ! empty( $connection->unique_id ) ) { |
|
511
|
|
|
$unique_id = $connection->unique_id; |
|
512
|
|
|
} else if ( ! empty( $connection['connection_data']['token_id'] ) ) { |
|
513
|
|
|
$unique_id = $connection['connection_data']['token_id']; |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
|
|
$test_results[] = array( |
|
517
|
|
|
'connectionID' => $id, |
|
518
|
|
|
'serviceName' => $service_name, |
|
519
|
|
|
'connectionTestPassed' => $connection_test_passed, |
|
520
|
|
|
'connectionTestMessage' => esc_attr( $connection_test_message ), |
|
521
|
|
|
'userCanRefresh' => $user_can_refresh, |
|
522
|
|
|
'refreshText' => esc_attr( $refresh_text ), |
|
523
|
|
|
'refreshURL' => $refresh_url, |
|
524
|
|
|
'unique_id' => $unique_id, |
|
525
|
|
|
); |
|
526
|
|
|
} |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
return $test_results; |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
/** |
|
533
|
|
|
* Run the connection test for the Connection |
|
534
|
|
|
* |
|
535
|
|
|
* @param string $service_name 'facebook', 'twitter', etc. |
|
536
|
|
|
* @param object|array The Connection object (WordPress.com) or array (Jetpack) |
|
537
|
|
|
* @return WP_Error|true WP_Error on failure. True on success |
|
538
|
|
|
*/ |
|
539
|
|
|
abstract function test_connection( $service_name, $connection ); |
|
540
|
|
|
|
|
541
|
|
|
/** |
|
542
|
|
|
* Retrieves current list of connections and applies filters. |
|
543
|
|
|
* |
|
544
|
|
|
* Retrieves current available connections and checks if the connections |
|
545
|
|
|
* have already been used to share current post. Finally, the checkbox |
|
546
|
|
|
* form UI fields are calculated. This function exposes connection form |
|
547
|
|
|
* data directly as array so it can be retrieved for static HTML generation |
|
548
|
|
|
* or JSON consumption. |
|
549
|
|
|
* |
|
550
|
|
|
* @since 6.7.0 |
|
551
|
|
|
* |
|
552
|
|
|
* @param integer $selected_post_id Optional. Post ID to query connection status for. |
|
|
|
|
|
|
553
|
|
|
* |
|
554
|
|
|
* @return array { |
|
555
|
|
|
* Array of UI setup data for connection list form. |
|
556
|
|
|
* |
|
557
|
|
|
* @type string 'unique_id' ID string representing connection |
|
558
|
|
|
* @type string 'service_name' Slug of the connection's service (facebook, twitter, ...) |
|
559
|
|
|
* @type string 'service_label' Service Label (Facebook, Twitter, ...) |
|
560
|
|
|
* @type string 'display_name' Connection's human-readable Username: "@jetpack" |
|
561
|
|
|
* @type bool 'enabled' Default value for the connection (e.g., for a checkbox). |
|
562
|
|
|
* @type bool 'done' Has this connection already been publicized to? |
|
563
|
|
|
* @type bool 'toggleable' Is the user allowed to change the value for the connection? |
|
564
|
|
|
* @type bool 'global' Is this connection a global one? |
|
565
|
|
|
* } |
|
566
|
|
|
*/ |
|
567
|
|
|
public function get_filtered_connection_data( $selected_post_id = null ) { |
|
568
|
|
|
$connection_list = array(); |
|
569
|
|
|
|
|
570
|
|
|
$post = get_post( $selected_post_id ); // Defaults to current post if $post_id is null. |
|
571
|
|
|
// Handle case where there is no current post. |
|
572
|
|
|
if ( ! empty( $post ) ) { |
|
573
|
|
|
$post_id = $post->ID; |
|
574
|
|
|
} else { |
|
575
|
|
|
$post_id = null; |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
$services = $this->get_services( 'connected' ); |
|
579
|
|
|
$all_done = $this->post_is_done_sharing( $post_id ); |
|
580
|
|
|
|
|
581
|
|
|
// We don't allow Publicizing to the same external id twice, to prevent spam. |
|
582
|
|
|
$service_id_done = (array) get_post_meta( $post_id, $this->POST_SERVICE_DONE, true ); |
|
583
|
|
|
|
|
584
|
|
|
foreach ( $services as $service_name => $connections ) { |
|
585
|
|
|
foreach ( $connections as $connection ) { |
|
586
|
|
|
$connection_meta = $this->get_connection_meta( $connection ); |
|
587
|
|
|
$connection_data = $connection_meta['connection_data']; |
|
588
|
|
|
|
|
589
|
|
|
$unique_id = $this->get_connection_unique_id( $connection ); |
|
590
|
|
|
|
|
591
|
|
|
|
|
592
|
|
|
// Was this connection (OR, old-format service) already Publicized to? |
|
593
|
|
|
$done = ! empty( $post ) && ( |
|
594
|
|
|
// New flags |
|
595
|
|
|
1 == get_post_meta( $post->ID, $this->POST_DONE . $unique_id, true ) |
|
596
|
|
|
|| |
|
597
|
|
|
// old flags |
|
598
|
|
|
1 == get_post_meta( $post->ID, $this->POST_DONE . $service_name, true ) |
|
599
|
|
|
); |
|
600
|
|
|
|
|
601
|
|
|
/** |
|
602
|
|
|
* Filter whether a post should be publicized to a given service. |
|
603
|
|
|
* |
|
604
|
|
|
* @module publicize |
|
605
|
|
|
* |
|
606
|
|
|
* @since 2.0.0 |
|
607
|
|
|
* |
|
608
|
|
|
* @param bool true Should the post be publicized to a given service? Default to true. |
|
609
|
|
|
* @param int $post_id Post ID. |
|
610
|
|
|
* @param string $service_name Service name. |
|
611
|
|
|
* @param array $connection_data Array of information about all Publicize details for the site. |
|
612
|
|
|
*/ |
|
613
|
|
|
if ( ! apply_filters( 'wpas_submit_post?', true, $post_id, $service_name, $connection_data ) ) { |
|
614
|
|
|
continue; |
|
615
|
|
|
} |
|
616
|
|
|
|
|
617
|
|
|
// Should we be skipping this one? |
|
618
|
|
|
$skip = ( |
|
619
|
|
|
( |
|
620
|
|
|
! empty( $post ) |
|
621
|
|
|
&& |
|
622
|
|
|
in_array( $post->post_status, array( 'publish', 'draft', 'future' ) ) |
|
623
|
|
|
&& |
|
624
|
|
|
( |
|
625
|
|
|
// New flags |
|
626
|
|
|
get_post_meta( $post->ID, $this->POST_SKIP . $unique_id, true ) |
|
627
|
|
|
|| |
|
628
|
|
|
// Old flags |
|
629
|
|
|
get_post_meta( $post->ID, $this->POST_SKIP . $service_name ) |
|
630
|
|
|
) |
|
631
|
|
|
) |
|
632
|
|
|
|| |
|
633
|
|
|
( |
|
634
|
|
|
is_array( $connection ) |
|
635
|
|
|
&& |
|
636
|
|
|
isset( $connection_meta['external_id'] ) && ! empty( $service_id_done[ $service_name ][ $connection_meta['external_id'] ] ) |
|
637
|
|
|
) |
|
638
|
|
|
); |
|
639
|
|
|
|
|
640
|
|
|
// If this one has already been publicized to, don't let it happen again. |
|
641
|
|
|
$toggleable = ! $done && ! $all_done; |
|
642
|
|
|
|
|
643
|
|
|
// Determine the state of the checkbox (on/off) and allow filtering. |
|
644
|
|
|
$enabled = $done || ! $skip; |
|
645
|
|
|
/** |
|
646
|
|
|
* Filter the checkbox state of each Publicize connection appearing in the post editor. |
|
647
|
|
|
* |
|
648
|
|
|
* @module publicize |
|
649
|
|
|
* |
|
650
|
|
|
* @since 2.0.1 |
|
651
|
|
|
* |
|
652
|
|
|
* @param bool $enabled Should the Publicize checkbox be enabled for a given service. |
|
653
|
|
|
* @param int $post_id Post ID. |
|
654
|
|
|
* @param string $service_name Service name. |
|
655
|
|
|
* @param array $connection Array of connection details. |
|
656
|
|
|
*/ |
|
657
|
|
|
$enabled = apply_filters( 'publicize_checkbox_default', $enabled, $post_id, $service_name, $connection ); |
|
658
|
|
|
|
|
659
|
|
|
/** |
|
660
|
|
|
* If this is a global connection and this user doesn't have enough permissions to modify |
|
661
|
|
|
* those connections, don't let them change it. |
|
662
|
|
|
*/ |
|
663
|
|
|
if ( ! $done && ( 0 == $connection_data['user_id'] && ! current_user_can( $this->GLOBAL_CAP ) ) ) { |
|
664
|
|
|
$toggleable = false; |
|
665
|
|
|
|
|
666
|
|
|
/** |
|
667
|
|
|
* Filters the checkboxes for global connections with non-prilvedged users. |
|
668
|
|
|
* |
|
669
|
|
|
* @module publicize |
|
670
|
|
|
* |
|
671
|
|
|
* @since 3.7.0 |
|
672
|
|
|
* |
|
673
|
|
|
* @param bool $enabled Indicates if this connection should be enabled. Default true. |
|
674
|
|
|
* @param int $post_id ID of the current post |
|
675
|
|
|
* @param string $service_name Name of the connection (Facebook, Twitter, etc) |
|
676
|
|
|
* @param array $connection Array of data about the connection. |
|
677
|
|
|
*/ |
|
678
|
|
|
$enabled = apply_filters( 'publicize_checkbox_global_default', $enabled, $post_id, $service_name, $connection ); |
|
679
|
|
|
} |
|
680
|
|
|
|
|
681
|
|
|
// Force the checkbox to be checked if the post was DONE, regardless of what the filter does. |
|
682
|
|
|
if ( $done ) { |
|
683
|
|
|
$enabled = true; |
|
684
|
|
|
} |
|
685
|
|
|
|
|
686
|
|
|
$connection_list[] = array( |
|
687
|
|
|
'unique_id' => $unique_id, |
|
688
|
|
|
'service_name' => $service_name, |
|
689
|
|
|
'service_label' => $this->get_service_label( $service_name ), |
|
690
|
|
|
'display_name' => $this->get_display_name( $service_name, $connection ), |
|
691
|
|
|
|
|
692
|
|
|
'enabled' => $enabled, |
|
693
|
|
|
'done' => $done, |
|
694
|
|
|
'toggleable' => $toggleable, |
|
695
|
|
|
'global' => 0 == $connection_data['user_id'], |
|
696
|
|
|
); |
|
697
|
|
|
} |
|
698
|
|
|
} |
|
699
|
|
|
|
|
700
|
|
|
return $connection_list; |
|
701
|
|
|
} |
|
702
|
|
|
|
|
703
|
|
|
/** |
|
704
|
|
|
* Checks if post has already been shared by Publicize in the past. |
|
705
|
|
|
* |
|
706
|
|
|
* @since 6.7.0 |
|
707
|
|
|
* |
|
708
|
|
|
* @param integer $post_id Optional. Post ID to query connection status for: will use current post if missing. |
|
|
|
|
|
|
709
|
|
|
* |
|
710
|
|
|
* @return bool True if post has already been shared by Publicize, false otherwise. |
|
711
|
|
|
*/ |
|
712
|
|
|
abstract public function post_is_done_sharing( $post_id = null ); |
|
713
|
|
|
|
|
714
|
|
|
/** |
|
715
|
|
|
* Retrieves full list of available Publicize connection services. |
|
716
|
|
|
* |
|
717
|
|
|
* Retrieves current available publicize service connections |
|
718
|
|
|
* with associated labels and URLs. |
|
719
|
|
|
* |
|
720
|
|
|
* @since 6.7.0 |
|
721
|
|
|
* |
|
722
|
|
|
* @return array { |
|
723
|
|
|
* Array of UI service connection data for all services |
|
724
|
|
|
* |
|
725
|
|
|
* @type string 'name' Name of service. |
|
726
|
|
|
* @type string 'label' Display label for service. |
|
727
|
|
|
* @type string 'url' URL for adding connection to service. |
|
728
|
|
|
* } |
|
729
|
|
|
*/ |
|
730
|
|
|
function get_available_service_data() { |
|
731
|
|
|
$available_services = $this->get_services( 'all' ); |
|
732
|
|
|
$available_service_data = array(); |
|
733
|
|
|
|
|
734
|
|
|
foreach ( $available_services as $service_name => $service ) { |
|
735
|
|
|
$available_service_data[] = array( |
|
736
|
|
|
'name' => $service_name, |
|
737
|
|
|
'label' => $this->get_service_label( $service_name ), |
|
738
|
|
|
'url' => $this->connect_url( $service_name ), |
|
739
|
|
|
); |
|
740
|
|
|
} |
|
741
|
|
|
|
|
742
|
|
|
return $available_service_data; |
|
743
|
|
|
} |
|
744
|
|
|
|
|
745
|
|
|
/* |
|
746
|
|
|
* Site Data |
|
747
|
|
|
*/ |
|
748
|
|
|
|
|
749
|
|
|
function user_id() { |
|
750
|
|
|
return get_current_user_id(); |
|
751
|
|
|
} |
|
752
|
|
|
|
|
753
|
|
|
function blog_id() { |
|
754
|
|
|
return get_current_blog_id(); |
|
755
|
|
|
} |
|
756
|
|
|
|
|
757
|
|
|
/* |
|
758
|
|
|
* Posts |
|
759
|
|
|
*/ |
|
760
|
|
|
|
|
761
|
|
|
/** |
|
762
|
|
|
* Checks old and new status to see if the post should be flagged as |
|
763
|
|
|
* ready to Publicize. |
|
764
|
|
|
* |
|
765
|
|
|
* Attached to the `transition_post_status` filter. |
|
766
|
|
|
* |
|
767
|
|
|
* @param string $new_status |
|
768
|
|
|
* @param string $old_status |
|
769
|
|
|
* @param WP_Post $post |
|
770
|
|
|
* @return void |
|
771
|
|
|
*/ |
|
772
|
|
|
abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
|
773
|
|
|
|
|
774
|
|
|
/** |
|
775
|
|
|
* Ensures the Post internal post-type supports `publicize` |
|
776
|
|
|
* |
|
777
|
|
|
* This feature support flag is used by the REST API. |
|
778
|
|
|
*/ |
|
779
|
|
|
function add_post_type_support() { |
|
780
|
|
|
add_post_type_support( 'post', 'publicize' ); |
|
781
|
|
|
} |
|
782
|
|
|
|
|
783
|
|
|
/** |
|
784
|
|
|
* Register the Publicize Gutenberg extension |
|
785
|
|
|
*/ |
|
786
|
|
|
function register_gutenberg_extension() { |
|
787
|
|
|
// TODO: The `gutenberg/available-extensions` endpoint currently doesn't accept a post ID, |
|
788
|
|
|
// so we cannot pass one to `$this->current_user_can_access_publicize_data()`. |
|
789
|
|
|
|
|
790
|
|
|
if ( $this->current_user_can_access_publicize_data() ) { |
|
791
|
|
|
jetpack_register_plugin( 'publicize' ); |
|
792
|
|
|
} else { |
|
793
|
|
|
jetpack_set_extension_unavailability_reason( 'publicize', 'unauthorized' ); |
|
794
|
|
|
|
|
795
|
|
|
} |
|
796
|
|
|
} |
|
797
|
|
|
|
|
798
|
|
|
/** |
|
799
|
|
|
* Can the current user access Publicize Data. |
|
800
|
|
|
* |
|
801
|
|
|
* @param int $post_id. 0 for general access. Post_ID for specific access. |
|
|
|
|
|
|
802
|
|
|
* @return bool |
|
803
|
|
|
*/ |
|
804
|
|
|
function current_user_can_access_publicize_data( $post_id = 0 ) { |
|
805
|
|
|
/** |
|
806
|
|
|
* Filter what user capability is required to use the publicize form on the edit post page. Useful if publish post capability has been removed from role. |
|
807
|
|
|
* |
|
808
|
|
|
* @module publicize |
|
809
|
|
|
* |
|
810
|
|
|
* @since 4.1.0 |
|
811
|
|
|
* |
|
812
|
|
|
* @param string $capability User capability needed to use publicize |
|
813
|
|
|
*/ |
|
814
|
|
|
$capability = apply_filters( 'jetpack_publicize_capability', 'publish_posts' ); |
|
815
|
|
|
|
|
816
|
|
|
if ( 'publish_posts' === $capability && $post_id ) { |
|
817
|
|
|
return current_user_can( 'publish_post', $post_id ); |
|
818
|
|
|
} |
|
819
|
|
|
|
|
820
|
|
|
return current_user_can( $capability ); |
|
821
|
|
|
} |
|
822
|
|
|
|
|
823
|
|
|
/** |
|
824
|
|
|
* Auth callback for the protected ->POST_MESS post_meta |
|
825
|
|
|
* |
|
826
|
|
|
* @param bool $allowed |
|
827
|
|
|
* @param string $meta_key |
|
828
|
|
|
* @param int $object_id Post ID |
|
829
|
|
|
* @return bool |
|
830
|
|
|
*/ |
|
831
|
|
|
function message_meta_auth_callback( $allowed, $meta_key, $object_id ) { |
|
832
|
|
|
return $this->current_user_can_access_publicize_data( $object_id ); |
|
833
|
|
|
} |
|
834
|
|
|
|
|
835
|
|
|
/** |
|
836
|
|
|
* Registers the ->POST_MESS post_meta for use in the REST API. |
|
837
|
|
|
* |
|
838
|
|
|
* Registers for each post type that with `publicize` feature support. |
|
839
|
|
|
*/ |
|
840
|
|
|
function register_post_meta() { |
|
841
|
|
|
$args = array( |
|
842
|
|
|
'type' => 'string', |
|
843
|
|
|
'description' => __( 'The message to use instead of the title when sharing to Publicize Services', 'jetpack' ), |
|
844
|
|
|
'single' => true, |
|
845
|
|
|
'default' => '', |
|
846
|
|
|
'show_in_rest' => array( |
|
847
|
|
|
'name' => 'jetpack_publicize_message' |
|
848
|
|
|
), |
|
849
|
|
|
'auth_callback' => array( $this, 'message_meta_auth_callback' ), |
|
850
|
|
|
); |
|
851
|
|
|
|
|
852
|
|
|
foreach ( get_post_types() as $post_type ) { |
|
853
|
|
|
if ( ! $this->post_type_is_publicizeable( $post_type ) ) { |
|
854
|
|
|
continue; |
|
855
|
|
|
} |
|
856
|
|
|
|
|
857
|
|
|
$args['object_subtype'] = $post_type; |
|
858
|
|
|
|
|
859
|
|
|
register_meta( 'post', $this->POST_MESS, $args ); |
|
860
|
|
|
} |
|
861
|
|
|
} |
|
862
|
|
|
|
|
863
|
|
|
/** |
|
864
|
|
|
* Fires when a post is saved, checks conditions and saves state in postmeta so that it |
|
865
|
|
|
* can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
|
866
|
|
|
* |
|
867
|
|
|
* Attached to the `save_post` action. |
|
868
|
|
|
* |
|
869
|
|
|
* @param int $post_id |
|
870
|
|
|
* @param WP_Post $post |
|
871
|
|
|
* @return void |
|
872
|
|
|
*/ |
|
873
|
|
|
function save_meta( $post_id, $post ) { |
|
874
|
|
|
$cron_user = null; |
|
875
|
|
|
$submit_post = true; |
|
876
|
|
|
|
|
877
|
|
|
if ( ! $this->post_type_is_publicizeable( $post->post_type ) ) |
|
878
|
|
|
return; |
|
879
|
|
|
|
|
880
|
|
|
// Don't Publicize during certain contexts: |
|
881
|
|
|
|
|
882
|
|
|
// - import |
|
883
|
|
|
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
|
884
|
|
|
$submit_post = false; |
|
885
|
|
|
} |
|
886
|
|
|
|
|
887
|
|
|
// - on quick edit, autosave, etc but do fire on p2, quickpress, and instapost ajax |
|
888
|
|
|
if ( |
|
889
|
|
|
defined( 'DOING_AJAX' ) |
|
890
|
|
|
&& |
|
891
|
|
|
DOING_AJAX |
|
892
|
|
|
&& |
|
893
|
|
|
!did_action( 'p2_ajax' ) |
|
894
|
|
|
&& |
|
895
|
|
|
!did_action( 'wp_ajax_json_quickpress_post' ) |
|
896
|
|
|
&& |
|
897
|
|
|
!did_action( 'wp_ajax_instapost_publish' ) |
|
898
|
|
|
&& |
|
899
|
|
|
!did_action( 'wp_ajax_post_reblog' ) |
|
900
|
|
|
&& |
|
901
|
|
|
!did_action( 'wp_ajax_press-this-save-post' ) |
|
902
|
|
|
) { |
|
903
|
|
|
$submit_post = false; |
|
904
|
|
|
} |
|
905
|
|
|
|
|
906
|
|
|
// - bulk edit |
|
907
|
|
|
if ( isset( $_GET['bulk_edit'] ) ) { |
|
908
|
|
|
$submit_post = false; |
|
909
|
|
|
} |
|
910
|
|
|
|
|
911
|
|
|
// - API/XML-RPC Test Posts |
|
912
|
|
|
if ( |
|
913
|
|
|
( |
|
914
|
|
|
defined( 'XMLRPC_REQUEST' ) |
|
915
|
|
|
&& |
|
916
|
|
|
XMLRPC_REQUEST |
|
917
|
|
|
|| |
|
918
|
|
|
defined( 'APP_REQUEST' ) |
|
919
|
|
|
&& |
|
920
|
|
|
APP_REQUEST |
|
921
|
|
|
) |
|
922
|
|
|
&& |
|
923
|
|
|
0 === strpos( $post->post_title, 'Temporary Post Used For Theme Detection' ) |
|
924
|
|
|
) { |
|
925
|
|
|
$submit_post = false; |
|
926
|
|
|
} |
|
927
|
|
|
|
|
928
|
|
|
// only work with certain statuses (avoids inherits, auto drafts etc) |
|
929
|
|
|
if ( !in_array( $post->post_status, array( 'publish', 'draft', 'future' ) ) ) { |
|
930
|
|
|
$submit_post = false; |
|
931
|
|
|
} |
|
932
|
|
|
|
|
933
|
|
|
// don't publish password protected posts |
|
934
|
|
|
if ( '' !== $post->post_password ) { |
|
935
|
|
|
$submit_post = false; |
|
936
|
|
|
} |
|
937
|
|
|
|
|
938
|
|
|
// Did this request happen via wp-admin? |
|
939
|
|
|
$from_web = isset( $_SERVER['REQUEST_METHOD'] ) |
|
940
|
|
|
&& |
|
941
|
|
|
'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) |
|
942
|
|
|
&& |
|
943
|
|
|
isset( $_POST[$this->ADMIN_PAGE] ); |
|
944
|
|
|
|
|
945
|
|
|
if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && isset( $_POST['wpas_title'] ) ) { |
|
946
|
|
|
if ( empty( $_POST['wpas_title'] ) ) { |
|
947
|
|
|
delete_post_meta( $post_id, $this->POST_MESS ); |
|
948
|
|
|
} else { |
|
949
|
|
|
update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $_POST['wpas_title'] ) ) ); |
|
950
|
|
|
} |
|
951
|
|
|
} |
|
952
|
|
|
|
|
953
|
|
|
// change current user to provide context for get_services() if we're running during cron |
|
954
|
|
|
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
|
955
|
|
|
$cron_user = (int) $GLOBALS['user_ID']; |
|
956
|
|
|
wp_set_current_user( $post->post_author ); |
|
957
|
|
|
} |
|
958
|
|
|
|
|
959
|
|
|
/** |
|
960
|
|
|
* In this phase, we mark connections that we want to SKIP. When Publicize is actually triggered, |
|
961
|
|
|
* it will Publicize to everything *except* those marked for skipping. |
|
962
|
|
|
*/ |
|
963
|
|
|
foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) { |
|
964
|
|
|
foreach ( $connections as $connection ) { |
|
965
|
|
|
$connection_data = ''; |
|
966
|
|
|
if ( method_exists( $connection, 'get_meta' ) ) |
|
967
|
|
|
$connection_data = $connection->get_meta( 'connection_data' ); |
|
968
|
|
|
elseif ( ! empty( $connection['connection_data'] ) ) |
|
969
|
|
|
$connection_data = $connection['connection_data']; |
|
970
|
|
|
|
|
971
|
|
|
/** This action is documented in modules/publicize/ui.php */ |
|
972
|
|
|
if ( false == apply_filters( 'wpas_submit_post?', $submit_post, $post_id, $service_name, $connection_data ) ) { |
|
973
|
|
|
delete_post_meta( $post_id, $this->PENDING ); |
|
974
|
|
|
continue; |
|
975
|
|
|
} |
|
976
|
|
|
|
|
977
|
|
View Code Duplication |
if ( !empty( $connection->unique_id ) ) |
|
978
|
|
|
$unique_id = $connection->unique_id; |
|
979
|
|
|
else if ( !empty( $connection['connection_data']['token_id'] ) ) |
|
980
|
|
|
$unique_id = $connection['connection_data']['token_id']; |
|
981
|
|
|
|
|
982
|
|
|
// This was a wp-admin request, so we need to check the state of checkboxes |
|
983
|
|
|
if ( $from_web ) { |
|
984
|
|
|
// delete stray service-based post meta |
|
985
|
|
|
delete_post_meta( $post_id, $this->POST_SKIP . $service_name ); |
|
986
|
|
|
|
|
987
|
|
|
// We *unchecked* this stream from the admin page, or it's set to readonly, or it's a new addition |
|
988
|
|
|
if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$unique_id] ) ) { |
|
989
|
|
|
// Also make sure that the service-specific input isn't there. |
|
990
|
|
|
// If the user connected to a new service 'in-page' then a hidden field with the service |
|
991
|
|
|
// name is added, so we just assume they wanted to Publicize to that service. |
|
992
|
|
|
if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$service_name] ) ) { |
|
993
|
|
|
// Nothing seems to be checked, so we're going to mark this one to be skipped |
|
994
|
|
|
update_post_meta( $post_id, $this->POST_SKIP . $unique_id, 1 ); |
|
|
|
|
|
|
995
|
|
|
continue; |
|
996
|
|
|
} else { |
|
997
|
|
|
// clean up any stray post meta |
|
998
|
|
|
delete_post_meta( $post_id, $this->POST_SKIP . $unique_id ); |
|
999
|
|
|
} |
|
1000
|
|
|
} else { |
|
1001
|
|
|
// The checkbox for this connection is explicitly checked -- make sure we DON'T skip it |
|
1002
|
|
|
delete_post_meta( $post_id, $this->POST_SKIP . $unique_id ); |
|
1003
|
|
|
} |
|
1004
|
|
|
} |
|
1005
|
|
|
|
|
1006
|
|
|
/** |
|
1007
|
|
|
* Fires right before the post is processed for Publicize. |
|
1008
|
|
|
* Users may hook in here and do anything else they need to after meta is written, |
|
1009
|
|
|
* and before the post is processed for Publicize. |
|
1010
|
|
|
* |
|
1011
|
|
|
* @since 2.1.2 |
|
1012
|
|
|
* |
|
1013
|
|
|
* @param bool $submit_post Should the post be publicized. |
|
1014
|
|
|
* @param int $post->ID Post ID. |
|
1015
|
|
|
* @param string $service_name Service name. |
|
1016
|
|
|
* @param array $connection Array of connection details. |
|
1017
|
|
|
*/ |
|
1018
|
|
|
do_action( 'publicize_save_meta', $submit_post, $post_id, $service_name, $connection ); |
|
1019
|
|
|
} |
|
1020
|
|
|
} |
|
1021
|
|
|
|
|
1022
|
|
|
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
|
1023
|
|
|
wp_set_current_user( $cron_user ); |
|
1024
|
|
|
} |
|
1025
|
|
|
|
|
1026
|
|
|
// Next up will be ::publicize_post() |
|
1027
|
|
|
} |
|
1028
|
|
|
|
|
1029
|
|
|
/** |
|
1030
|
|
|
* Alters the "Post Published" message to include information about where the post |
|
1031
|
|
|
* was Publicized to. |
|
1032
|
|
|
* |
|
1033
|
|
|
* Attached to the `post_updated_messages` filter |
|
1034
|
|
|
* |
|
1035
|
|
|
* @param string[] $messages |
|
1036
|
|
|
* @return string[] |
|
1037
|
|
|
*/ |
|
1038
|
|
|
public function update_published_message( $messages ) { |
|
1039
|
|
|
global $post_type, $post_type_object, $post; |
|
1040
|
|
|
if ( ! $this->post_type_is_publicizeable( $post_type ) ) { |
|
1041
|
|
|
return $messages; |
|
1042
|
|
|
} |
|
1043
|
|
|
$view_post_link_html = ''; |
|
1044
|
|
|
$viewable = is_post_type_viewable( $post_type_object ); |
|
1045
|
|
|
if ( $viewable ) { |
|
1046
|
|
|
$view_text = esc_html__( 'View post' ); // intentionally omitted domain |
|
1047
|
|
|
|
|
1048
|
|
|
if ( 'jetpack-portfolio' == $post_type ) { |
|
1049
|
|
|
$view_text = esc_html__( 'View project', 'jetpack' ); |
|
1050
|
|
|
} |
|
1051
|
|
|
|
|
1052
|
|
|
$view_post_link_html = sprintf( ' <a href="%1$s">%2$s</a>', |
|
1053
|
|
|
esc_url( get_permalink( $post ) ), |
|
1054
|
|
|
$view_text |
|
1055
|
|
|
); |
|
1056
|
|
|
} |
|
1057
|
|
|
|
|
1058
|
|
|
$services = $this->get_publicizing_services( $post->ID ); |
|
1059
|
|
|
if ( empty( $services ) ) { |
|
1060
|
|
|
return $messages; |
|
1061
|
|
|
} |
|
1062
|
|
|
|
|
1063
|
|
|
$labels = array(); |
|
1064
|
|
|
foreach ( $services as $service_name => $display_names ) { |
|
1065
|
|
|
$labels[] = sprintf( |
|
1066
|
|
|
/* translators: Service name is %1$s, and account name is %2$s. */ |
|
1067
|
|
|
esc_html__( '%1$s (%2$s)', 'jetpack' ), |
|
1068
|
|
|
esc_html( $service_name ), |
|
1069
|
|
|
esc_html( implode( ', ', $display_names ) ) |
|
1070
|
|
|
); |
|
1071
|
|
|
} |
|
1072
|
|
|
|
|
1073
|
|
|
$messages['post'][6] = sprintf( |
|
1074
|
|
|
/* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */ |
|
1075
|
|
|
esc_html__( 'Post published and sharing on %1$s.', 'jetpack' ), |
|
1076
|
|
|
implode( ', ', $labels ) |
|
1077
|
|
|
) . $view_post_link_html; |
|
1078
|
|
|
|
|
1079
|
|
|
if ( $post_type == 'post' && class_exists('Jetpack_Subscriptions' ) ) { |
|
1080
|
|
|
$subscription = Jetpack_Subscriptions::init(); |
|
1081
|
|
|
if ( $subscription->should_email_post_to_subscribers( $post ) ) { |
|
1082
|
|
|
$messages['post'][6] = sprintf( |
|
1083
|
|
|
/* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */ |
|
1084
|
|
|
esc_html__( 'Post published, sending emails to subscribers and sharing post on %1$s.', 'jetpack' ), |
|
1085
|
|
|
implode( ', ', $labels ) |
|
1086
|
|
|
) . $view_post_link_html; |
|
1087
|
|
|
} |
|
1088
|
|
|
} |
|
1089
|
|
|
|
|
1090
|
|
|
$messages['jetpack-portfolio'][6] = sprintf( |
|
1091
|
|
|
/* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */ |
|
1092
|
|
|
esc_html__( 'Project published and sharing project on %1$s.', 'jetpack' ), |
|
1093
|
|
|
implode( ', ', $labels ) |
|
1094
|
|
|
) . $view_post_link_html; |
|
1095
|
|
|
|
|
1096
|
|
|
return $messages; |
|
1097
|
|
|
} |
|
1098
|
|
|
|
|
1099
|
|
|
/** |
|
1100
|
|
|
* Get the Connections the Post was just Publicized to. |
|
1101
|
|
|
* |
|
1102
|
|
|
* Only reliable just after the Post was published. |
|
1103
|
|
|
* |
|
1104
|
|
|
* @param int $post_id |
|
1105
|
|
|
* @return string[] Array of Service display name => Connection display name |
|
1106
|
|
|
*/ |
|
1107
|
|
|
function get_publicizing_services( $post_id ) { |
|
1108
|
|
|
$services = array(); |
|
1109
|
|
|
|
|
1110
|
|
|
foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) { |
|
1111
|
|
|
// services have multiple connections. |
|
1112
|
|
|
foreach ( $connections as $connection ) { |
|
1113
|
|
|
$unique_id = ''; |
|
1114
|
|
View Code Duplication |
if ( ! empty( $connection->unique_id ) ) |
|
1115
|
|
|
$unique_id = $connection->unique_id; |
|
1116
|
|
|
else if ( ! empty( $connection['connection_data']['token_id'] ) ) |
|
1117
|
|
|
$unique_id = $connection['connection_data']['token_id']; |
|
1118
|
|
|
|
|
1119
|
|
|
// Did we skip this connection? |
|
1120
|
|
|
if ( get_post_meta( $post_id, $this->POST_SKIP . $unique_id, true ) ) { |
|
1121
|
|
|
continue; |
|
1122
|
|
|
} |
|
1123
|
|
|
$services[ $this->get_service_label( $service_name ) ][] = $this->get_display_name( $service_name, $connection ); |
|
1124
|
|
|
} |
|
1125
|
|
|
} |
|
1126
|
|
|
|
|
1127
|
|
|
return $services; |
|
1128
|
|
|
} |
|
1129
|
|
|
|
|
1130
|
|
|
/** |
|
1131
|
|
|
* Is the post Publicize-able? |
|
1132
|
|
|
* |
|
1133
|
|
|
* Only valid prior to Publicizing a Post. |
|
1134
|
|
|
* |
|
1135
|
|
|
* @param WP_Post $post |
|
1136
|
|
|
* @return bool |
|
1137
|
|
|
*/ |
|
1138
|
|
|
function post_is_publicizeable( $post ) { |
|
1139
|
|
|
if ( ! $this->post_type_is_publicizeable( $post->post_type ) ) |
|
1140
|
|
|
return false; |
|
1141
|
|
|
|
|
1142
|
|
|
// This is more a precaution. To only publicize posts that are published. (Mostly relevant for Jetpack sites) |
|
1143
|
|
|
if ( 'publish' !== $post->post_status ) { |
|
1144
|
|
|
return false; |
|
1145
|
|
|
} |
|
1146
|
|
|
|
|
1147
|
|
|
// If it's not flagged as ready, then abort. @see ::flag_post_for_publicize() |
|
1148
|
|
|
if ( ! get_post_meta( $post->ID, $this->PENDING, true ) ) |
|
1149
|
|
|
return false; |
|
1150
|
|
|
|
|
1151
|
|
|
return true; |
|
1152
|
|
|
} |
|
1153
|
|
|
|
|
1154
|
|
|
/** |
|
1155
|
|
|
* Is a given post type Publicize-able? |
|
1156
|
|
|
* |
|
1157
|
|
|
* Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
|
1158
|
|
|
* the publicize_post_types array filter. |
|
1159
|
|
|
* |
|
1160
|
|
|
* @param string $post_type The post type to check. |
|
1161
|
|
|
* @return bool True if the post type can be Publicized. |
|
1162
|
|
|
*/ |
|
1163
|
|
|
function post_type_is_publicizeable( $post_type ) { |
|
1164
|
|
|
if ( 'post' == $post_type ) |
|
1165
|
|
|
return true; |
|
1166
|
|
|
|
|
1167
|
|
|
return post_type_supports( $post_type, 'publicize' ); |
|
1168
|
|
|
} |
|
1169
|
|
|
|
|
1170
|
|
|
/** |
|
1171
|
|
|
* Already-published posts should not be Publicized by default. This filter sets checked to |
|
1172
|
|
|
* false if a post has already been published. |
|
1173
|
|
|
* |
|
1174
|
|
|
* Attached to the `publicize_checkbox_default` filter |
|
1175
|
|
|
* |
|
1176
|
|
|
* @param bool $checked |
|
1177
|
|
|
* @param int $post_id |
|
1178
|
|
|
* @param string $service_name 'facebook', 'twitter', etc |
|
1179
|
|
|
* @param object|array The Connection object (WordPress.com) or array (Jetpack) |
|
1180
|
|
|
* @return bool |
|
1181
|
|
|
*/ |
|
1182
|
|
|
function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { |
|
1183
|
|
|
if ( 'publish' == get_post_status( $post_id ) ) { |
|
1184
|
|
|
return false; |
|
1185
|
|
|
} |
|
1186
|
|
|
|
|
1187
|
|
|
return $checked; |
|
1188
|
|
|
} |
|
1189
|
|
|
|
|
1190
|
|
|
/* |
|
1191
|
|
|
* Util |
|
1192
|
|
|
*/ |
|
1193
|
|
|
|
|
1194
|
|
|
/** |
|
1195
|
|
|
* Converts a Publicize message template string into a sprintf format string |
|
1196
|
|
|
* |
|
1197
|
|
|
* @param string[] $args |
|
1198
|
|
|
* 0 - The Publicize message template: 'Check out my post: %title% @ %url' |
|
1199
|
|
|
* ... - The template tags 'title', 'url', etc. |
|
1200
|
|
|
* @return string |
|
1201
|
|
|
*/ |
|
1202
|
|
|
protected static function build_sprintf( $args ) { |
|
1203
|
|
|
$search = array(); |
|
1204
|
|
|
$replace = array(); |
|
1205
|
|
|
foreach ( $args as $k => $arg ) { |
|
1206
|
|
|
if ( 0 == $k ) { |
|
1207
|
|
|
$string = $arg; |
|
1208
|
|
|
continue; |
|
1209
|
|
|
} |
|
1210
|
|
|
$search[] = "%$arg%"; |
|
1211
|
|
|
$replace[] = "%$k\$s"; |
|
1212
|
|
|
} |
|
1213
|
|
|
return str_replace( $search, $replace, $string ); |
|
|
|
|
|
|
1214
|
|
|
} |
|
1215
|
|
|
} |
|
1216
|
|
|
|
|
1217
|
|
|
function publicize_calypso_url() { |
|
1218
|
|
|
$calypso_sharing_url = 'https://wordpress.com/sharing/'; |
|
1219
|
|
|
if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) { |
|
1220
|
|
|
$site_suffix = Jetpack::build_raw_urls( home_url() ); |
|
1221
|
|
|
} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) { |
|
1222
|
|
|
$site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() ); |
|
1223
|
|
|
} |
|
1224
|
|
|
|
|
1225
|
|
|
if ( $site_suffix ) { |
|
1226
|
|
|
return $calypso_sharing_url . $site_suffix; |
|
|
|
|
|
|
1227
|
|
|
} else { |
|
1228
|
|
|
return $calypso_sharing_url; |
|
1229
|
|
|
} |
|
1230
|
|
|
} |
|
1231
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: