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