publicize.php
| 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 = 'edit_others_posts'; |
| 44 | |
| 45 | /** |
| 46 | * Sets up the basics of Publicize |
| 47 | */ |
| 48 | function __construct() { |
| 49 | $this->default_message = Publicize_Util::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 = Publicize_Util::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 = Publicize_Util::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 | // Connection test callback |
| 112 | add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Functions to be implemented by the extended class (publicize-wpcom or publicize-jetpack) |
| 117 | */ |
| 118 | abstract function get_connection_id( $connection ); |
| 119 | abstract function connect_url( $service_name ); |
| 120 | abstract function disconnect_url( $service_name, $id ); |
| 121 | abstract function get_connection_meta( $connection ); |
| 122 | abstract function get_services( $filter = 'all' ); |
| 123 | abstract function get_connections( $service, $_blog_id = false, $_user_id = false ); |
| 124 | abstract function get_connection( $service, $id, $_blog_id = false, $_user_id = false ); |
| 125 | abstract function flag_post_for_publicize( $new_status, $old_status, $post ); |
| 126 | abstract function test_connection( $service_name, $connection ); |
| 127 | abstract function disconnect( $service, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ); |
| 128 | |
| 129 | /** |
| 130 | * Shared Functions |
| 131 | */ |
| 132 | |
| 133 | /** |
| 134 | * Returns an external URL to the connection's profile |
| 135 | */ |
| 136 | function get_profile_link( $service_name, $c ) { |
| 137 | $cmeta = $this->get_connection_meta( $c ); |
| 138 | |
| 139 | if ( isset( $cmeta['connection_data']['meta']['link'] ) ) { |
| 140 | if ( 'facebook' == $service_name && 0 === strpos( parse_url( $cmeta['connection_data']['meta']['link'], PHP_URL_PATH ), '/app_scoped_user_id/' ) ) { |
| 141 | // App-scoped Facebook user IDs are not usable profile links |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return $cmeta['connection_data']['meta']['link']; |
| 146 | } elseif ( 'facebook' == $service_name && isset( $cmeta['connection_data']['meta']['facebook_page'] ) ) { |
| 147 | return 'https://facebook.com/' . $cmeta['connection_data']['meta']['facebook_page']; |
| 148 | } elseif ( 'tumblr' == $service_name && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) { |
| 149 | return 'http://' . $cmeta['connection_data']['meta']['tumblr_base_hostname']; |
| 150 | } elseif ( 'twitter' == $service_name ) { |
| 151 | return 'https://twitter.com/' . substr( $cmeta['external_display'], 1 ); // Has a leading '@' |
| 152 | } elseif ( 'google_plus' == $service_name && isset( $cmeta['connection_data']['meta']['google_plus_page'] ) ) { |
| 153 | return 'https://plus.google.com/' . $cmeta['connection_data']['meta']['google_plus_page']; |
| 154 | } elseif ( 'google_plus' == $service_name ) { |
| 155 | return 'https://plus.google.com/' . $cmeta['external_id']; |
| 156 | } else if ( 'linkedin' == $service_name ) { |
| 157 | if ( !isset( $cmeta['connection_data']['meta']['profile_url'] ) ) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | $profile_url_query = parse_url( $cmeta['connection_data']['meta']['profile_url'], PHP_URL_QUERY ); |
| 162 | wp_parse_str( $profile_url_query, $profile_url_query_args ); |
| 163 | if ( isset( $profile_url_query_args['key'] ) ) { |
| 164 | $id = $profile_url_query_args['key']; |
| 165 | } elseif ( isset( $profile_url_query_args['id'] ) ) { |
| 166 | $id = $profile_url_query_args['id']; |
| 167 | } else { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | return esc_url_raw( add_query_arg( 'id', urlencode( $id ), 'http://www.linkedin.com/profile/view' ) ); |
| 172 | } else { |
| 173 | return false; // no fallback. we just won't link it |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns a display name for the connection |
| 179 | */ |
| 180 | function get_display_name( $service_name, $c ) { |
| 181 | $cmeta = $this->get_connection_meta( $c ); |
| 182 | |
| 183 | if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) { |
| 184 | return $cmeta['connection_data']['meta']['display_name']; |
| 185 | } elseif ( $service_name == 'tumblr' && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) { |
| 186 | return $cmeta['connection_data']['meta']['tumblr_base_hostname']; |
| 187 | } elseif ( $service_name == 'twitter' ) { |
| 188 | return $cmeta['external_display']; |
| 189 | } else { |
| 190 | $connection_display = $cmeta['external_display']; |
| 191 | if ( empty( $connection_display ) ) |
| 192 | $connection_display = $cmeta['external_name']; |
| 193 | return $connection_display; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public static function get_service_label( $service_name ) { |
| 198 | switch ( $service_name ) { |
| 199 | case 'linkedin': |
| 200 | return 'LinkedIn'; |
| 201 | break; |
| 202 | case 'google_plus': |
| 203 | return 'Google+'; |
| 204 | break; |
| 205 | case 'twitter': |
| 206 | case 'facebook': |
| 207 | case 'tumblr': |
| 208 | default: |
| 209 | return ucfirst( $service_name ); |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | function show_options_popup( $service_name, $c ) { |
| 215 | $cmeta = $this->get_connection_meta( $c ); |
| 216 | |
| 217 | // always show if no selection has been made for facebook |
| 218 | if ( 'facebook' == $service_name && empty( $cmeta['connection_data']['meta']['facebook_profile'] ) && empty( $cmeta['connection_data']['meta']['facebook_page'] ) ) |
| 219 | return true; |
| 220 | |
| 221 | // always show if no selection has been made for tumblr |
| 222 | if ( 'tumblr' == $service_name && empty ( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) |
| 223 | return true; |
| 224 | |
| 225 | // if we have the specific connection info.. |
| 226 | if ( isset( $_GET['id'] ) ) { |
| 227 | if ( $cmeta['connection_data']['id'] == $_GET['id'] ) |
| 228 | return true; |
| 229 | } else { |
| 230 | // otherwise, just show if this is the completed step / first load |
| 231 | if ( !empty( $_GET['action'] ) && 'completed' == $_GET['action'] && !empty( $_GET['service'] ) && $service_name == $_GET['service'] && ! in_array( $_GET['service'], array( 'facebook', 'tumblr' ) ) ) |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | function user_id() { |
| 239 | global $current_user; |
| 240 | return $current_user->ID; |
| 241 | } |
| 242 | |
| 243 | function blog_id() { |
| 244 | return get_current_blog_id(); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Returns true if a user has a connection to a particular service, false otherwise |
| 249 | */ |
| 250 | function is_enabled( $service, $_blog_id = false, $_user_id = false ) { |
| 251 | if ( !$_blog_id ) |
| 252 | $_blog_id = $this->blog_id(); |
| 253 | |
| 254 | if ( !$_user_id ) |
| 255 | $_user_id = $this->user_id(); |
| 256 | |
| 257 | $connections = $this->get_connections( $service, $_blog_id, $_user_id ); |
| 258 | return ( is_array( $connections ) && count( $connections ) > 0 ? true : false ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Fires when a post is saved, checks conditions and saves state in postmeta so that it |
| 263 | * can be picked up later by @see ::publicize_post() on WordPress.com codebase. |
| 264 | */ |
| 265 | function save_meta( $post_id, $post ) { |
| 266 | $cron_user = null; |
| 267 | $submit_post = true; |
| 268 | |
| 269 | if ( ! $this->post_type_is_publicizeable( $post->post_type ) ) |
| 270 | return; |
| 271 | |
| 272 | // Don't Publicize during certain contexts: |
| 273 | |
| 274 | // - import |
| 275 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 276 | $submit_post = false; |
| 277 | } |
| 278 | |
| 279 | // - on quick edit, autosave, etc but do fire on p2, quickpress, and instapost ajax |
| 280 | if ( |
| 281 | defined( 'DOING_AJAX' ) |
| 282 | && |
| 283 | DOING_AJAX |
| 284 | && |
| 285 | !did_action( 'p2_ajax' ) |
| 286 | && |
| 287 | !did_action( 'wp_ajax_json_quickpress_post' ) |
| 288 | && |
| 289 | !did_action( 'wp_ajax_instapost_publish' ) |
| 290 | && |
| 291 | !did_action( 'wp_ajax_post_reblog' ) |
| 292 | && |
| 293 | !did_action( 'wp_ajax_press-this-save-post' ) |
| 294 | ) { |
| 295 | $submit_post = false; |
| 296 | } |
| 297 | |
| 298 | // - bulk edit |
| 299 | if ( isset( $_GET['bulk_edit'] ) ) { |
| 300 | $submit_post = false; |
| 301 | } |
| 302 | |
| 303 | // - API/XML-RPC Test Posts |
| 304 | if ( |
| 305 | ( |
| 306 | defined( 'XMLRPC_REQUEST' ) |
| 307 | && |
| 308 | XMLRPC_REQUEST |
| 309 | || |
| 310 | defined( 'APP_REQUEST' ) |
| 311 | && |
| 312 | APP_REQUEST |
| 313 | ) |
| 314 | && |
| 315 | 0 === strpos( $post->post_title, 'Temporary Post Used For Theme Detection' ) |
| 316 | ) { |
| 317 | $submit_post = false; |
| 318 | } |
| 319 | |
| 320 | // only work with certain statuses (avoids inherits, auto drafts etc) |
| 321 | if ( !in_array( $post->post_status, array( 'publish', 'draft', 'future' ) ) ) { |
| 322 | $submit_post = false; |
| 323 | } |
| 324 | |
| 325 | // don't publish password protected posts |
| 326 | if ( '' !== $post->post_password ) { |
| 327 | $submit_post = false; |
| 328 | } |
| 329 | |
| 330 | // Did this request happen via wp-admin? |
| 331 | $from_web = isset( $_SERVER['REQUEST_METHOD'] ) |
| 332 | && |
| 333 | 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) |
| 334 | && |
| 335 | isset( $_POST[$this->ADMIN_PAGE] ); |
| 336 | |
| 337 | if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && isset( $_POST['wpas_title'] ) ) { |
| 338 | if ( empty( $_POST['wpas_title'] ) ) { |
| 339 | delete_post_meta( $post_id, $this->POST_MESS ); |
| 340 | } else { |
| 341 | update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $_POST['wpas_title'] ) ) ); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // change current user to provide context for get_services() if we're running during cron |
| 346 | if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 347 | $cron_user = (int) $GLOBALS['user_ID']; |
| 348 | wp_set_current_user( $post->post_author ); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * In this phase, we mark connections that we want to SKIP. When Publicize is actually triggered, |
| 353 | * it will Publicize to everything *except* those marked for skipping. |
| 354 | */ |
| 355 | foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) { |
| 356 | foreach ( $connections as $connection ) { |
| 357 | $connection_data = ''; |
| 358 | if ( method_exists( $connection, 'get_meta' ) ) |
| 359 | $connection_data = $connection->get_meta( 'connection_data' ); |
| 360 | elseif ( ! empty( $connection['connection_data'] ) ) |
| 361 | $connection_data = $connection['connection_data']; |
| 362 | |
| 363 | /** This action is documented in modules/publicize/ui.php */ |
| 364 | if ( false == apply_filters( 'wpas_submit_post?', $submit_post, $post_id, $service_name, $connection_data ) ) { |
| 365 | delete_post_meta( $post_id, $this->PENDING ); |
| 366 | continue; |
| 367 | } |
| 368 | |
| 369 | if ( !empty( $connection->unique_id ) ) |
| 370 | $unique_id = $connection->unique_id; |
| 371 | else if ( !empty( $connection['connection_data']['token_id'] ) ) |
| 372 | $unique_id = $connection['connection_data']['token_id']; |
| 373 | |
| 374 | // This was a wp-admin request, so we need to check the state of checkboxes |
| 375 | if ( $from_web ) { |
| 376 | // delete stray service-based post meta |
| 377 | delete_post_meta( $post_id, $this->POST_SKIP . $service_name ); |
| 378 | |
| 379 | // We *unchecked* this stream from the admin page, or it's set to readonly, or it's a new addition |
| 380 | if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$unique_id] ) ) { |
| 381 | // Also make sure that the service-specific input isn't there. |
| 382 | // If the user connected to a new service 'in-page' then a hidden field with the service |
| 383 | // name is added, so we just assume they wanted to Publicize to that service. |
| 384 | if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$service_name] ) ) { |
| 385 | // Nothing seems to be checked, so we're going to mark this one to be skipped |
| 386 | update_post_meta( $post_id, $this->POST_SKIP . $unique_id, 1 ); |
| 387 | continue; |
| 388 | } else { |
| 389 | // clean up any stray post meta |
| 390 | delete_post_meta( $post_id, $this->POST_SKIP . $unique_id ); |
| 391 | } |
| 392 | } else { |
| 393 | // The checkbox for this connection is explicitly checked -- make sure we DON'T skip it |
| 394 | delete_post_meta( $post_id, $this->POST_SKIP . $unique_id ); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Fires right before the post is processed for Publicize. |
| 400 | * Users may hook in here and do anything else they need to after meta is written, |
| 401 | * and before the post is processed for Publicize. |
| 402 | * |
| 403 | * @since 2.1.2 |
| 404 | * |
| 405 | * @param bool $submit_post Should the post be publicized. |
| 406 | * @param int $post->ID Post ID. |
| 407 | * @param string $service_name Service name. |
| 408 | * @param array $connection Array of connection details. |
| 409 | */ |
| 410 | do_action( 'publicize_save_meta', $submit_post, $post_id, $service_name, $connection ); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 415 | wp_set_current_user( $cron_user ); |
| 416 | } |
| 417 | |
| 418 | // Next up will be ::publicize_post() |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Is a given post type Publicize-able? |
| 423 | * |
| 424 | * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via |
| 425 | * the publicize_post_types array filter. |
| 426 | * |
| 427 | * @param string $post_type The post type to check. |
| 428 | * $return bool True if the post type can be Publicized. |
| 429 | */ |
| 430 | function post_type_is_publicizeable( $post_type ) { |
| 431 | if ( 'post' == $post_type ) |
| 432 | return true; |
| 433 | |
| 434 | return post_type_supports( $post_type, 'publicize' ); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Runs tests on all the connections and returns the results to the caller |
| 439 | */ |
| 440 | function test_publicize_conns() { |
| 441 | $test_results = array(); |
| 442 | |
| 443 | foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) { |
| 444 | foreach ( $connections as $connection ) { |
| 445 | |
| 446 | $id = $this->get_connection_id( $connection ); |
| 447 | |
| 448 | $connection_test_passed = true; |
| 449 | $connection_test_message = __( 'This connection is working correctly.' , 'jetpack' ); |
| 450 | $user_can_refresh = false; |
| 451 | $refresh_text = ''; |
| 452 | $refresh_url = ''; |
| 453 | |
| 454 | $connection_test_result = true; |
| 455 | if ( method_exists( $this, 'test_connection' ) ) { |
| 456 | $connection_test_result = $this->test_connection( $service_name, $connection ); |
| 457 | } |
| 458 | |
| 459 | if ( is_wp_error( $connection_test_result ) ) { |
| 460 | $connection_test_passed = false; |
| 461 | $connection_test_message = $connection_test_result->get_error_message(); |
| 462 | $error_data = $connection_test_result->get_error_data(); |
| 463 | |
| 464 | $user_can_refresh = $error_data['user_can_refresh']; |
| 465 | $refresh_text = $error_data['refresh_text']; |
| 466 | $refresh_url = $error_data['refresh_url']; |
| 467 | } |
| 468 | |
| 469 | $test_results[] = array( |
| 470 | 'connectionID' => $id, |
| 471 | 'serviceName' => $service_name, |
| 472 | 'connectionTestPassed' => $connection_test_passed, |
| 473 | 'connectionTestMessage' => esc_attr( $connection_test_message ), |
| 474 | 'userCanRefresh' => $user_can_refresh, |
| 475 | 'refreshText' => esc_attr( $refresh_text ), |
| 476 | 'refreshURL' => $refresh_url |
| 477 | ); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | wp_send_json_success( $test_results ); |
| 482 | } |
| 483 | } |
| 484 |