!== $post->post_password ) { $submit_post = false; } // Did this request happen via wp-admin? $from_web = isset( $_SERVER['REQUEST_METHOD'] ) && 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST[$this->ADMIN_PAGE] ); if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && isset( $_POST['wpas_title'] ) ) { if ( empty( $_POST['wpas_title'] ) ) { delete_post_meta( $post_id, $this->POST_MESS ); } else { update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $_POST['wpas_title'] ) ) ); } } // change current user to provide context for get_services() if we're running during cron if ( defined( 'DOING_CRON' ) && DOING_CRON ) { $cron_user = (int) $GLOBALS['user_ID']; wp_set_current_user( $post->post_author ); } /** * In this phase, we mark connections that we want to SKIP. When Publicize is actually triggered, * it will Publicize to everything *except* those marked for skipping. */ foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) { foreach ( $connections as $connection ) { $connection_data = ''; if ( method_exists( $connection, 'get_meta' ) ) $connection_data = $connection->get_meta( 'connection_data' ); elseif ( ! empty( $connection['connection_data'] ) ) $connection_data = $connection['connection_data']; /** This action is documented in modules/publicize/ui.php */ if ( false == apply_filters( 'wpas_submit_post?', $submit_post, $post_id, $service_name, $connection_data ) ) { delete_post_meta( $post_id, $this->PENDING ); continue; } if ( !empty( $connection->unique_id ) ) $unique_id = $connection->unique_id; else if ( !empty( $connection['connection_data']['token_id'] ) ) $unique_id = $connection['connection_data']['token_id']; // This was a wp-admin request, so we need to check the state of checkboxes if ( $from_web ) { // delete stray service-based post meta delete_post_meta( $post_id, $this->POST_SKIP . $service_name ); // We *unchecked* this stream from the admin page, or it's set to readonly, or it's a new addition if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$unique_id] ) ) { // Also make sure that the service-specific input isn't there. // If the user connected to a new service 'in-page' then a hidden field with the service // name is added, so we just assume they wanted to Publicize to that service. if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$service_name] ) ) { // Nothing seems to be checked, so we're going to mark this one to be skipped update_post_meta( $post_id, $this->POST_SKIP . $unique_id, 1 ); continue; } else { // clean up any stray post meta delete_post_meta( $post_id, $this->POST_SKIP . $unique_id ); } } else { // The checkbox for this connection is explicitly checked -- make sure we DON'T skip it delete_post_meta( $post_id, $this->POST_SKIP . $unique_id ); } } /** * Fires right before the post is processed for Publicize. * Users may hook in here and do anything else they need to after meta is written, * and before the post is processed for Publicize. * * @since 2.1.2 * * @param bool $submit_post Should the post be publicized. * @param int $post->ID Post ID. * @param string $service_name Service name. * @param array $connection Array of connection details. */ do_action( 'publicize_save_meta', $submit_post, $post_id, $service_name, $connection ); } } if ( defined( 'DOING_CRON' ) && DOING_CRON ) { wp_set_current_user( $cron_user ); } // Next up will be ::publicize_post() } /** * Alters the "Post Published" message to include information about where the post * was Publicized to. * * Attached to the `post_updated_messages` filter * * @param string[] $messages * @return string[] */ public function update_published_message( $messages ) { global $post_type, $post_type_object, $post; if ( ! $this->post_type_is_publicizeable( $post_type ) ) { return $messages; } // Bail early if the post is private. if ( 'publish' !== $post->post_status ) { return $messages; } $view_post_link_html = ''; $viewable = is_post_type_viewable( $post_type_object ); if ( $viewable ) { $view_text = esc_html__( 'View post' ); // intentionally omitted domain if ( 'jetpack-portfolio' == $post_type ) { $view_text = esc_html__( 'View project', 'jetpack' ); } $view_post_link_html = sprintf( ' %2$s', esc_url( get_permalink( $post ) ), $view_text ); } $services = $this->get_publicizing_services( $post->ID ); if ( empty( $services ) ) { return $messages; } $labels = array(); foreach ( $services as $service_name => $display_names ) { $labels[] = sprintf( /* translators: Service name is %1$s, and account name is %2$s. */ esc_html__( '%1$s (%2$s)', 'jetpack' ), esc_html( $service_name ), esc_html( implode( ', ', $display_names ) ) ); } $messages['post'][6] = sprintf( /* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */ esc_html__( 'Post published and sharing on %1$s.', 'jetpack' ), implode( ', ', $labels ) ) . $view_post_link_html; if ( $post_type == 'post' && class_exists('Jetpack_Subscriptions' ) ) { $subscription = Jetpack_Subscriptions::init(); if ( $subscription->should_email_post_to_subscribers( $post ) ) { $messages['post'][6] = sprintf( /* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */ esc_html__( 'Post published, sending emails to subscribers and sharing post on %1$s.', 'jetpack' ), implode( ', ', $labels ) ) . $view_post_link_html; } } $messages['jetpack-portfolio'][6] = sprintf( /* translators: %1$s is a comma-separated list of services and accounts. Ex. Facebook (@jetpack), Twitter (@jetpack) */ esc_html__( 'Project published and sharing project on %1$s.', 'jetpack' ), implode( ', ', $labels ) ) . $view_post_link_html; return $messages; } /** * Get the Connections the Post was just Publicized to. * * Only reliable just after the Post was published. * * @param int $post_id * @return string[] Array of Service display name => Connection display name */ function get_publicizing_services( $post_id ) { $services = array(); foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) { // services have multiple connections. foreach ( $connections as $connection ) { $unique_id = ''; if ( ! empty( $connection->unique_id ) ) $unique_id = $connection->unique_id; else if ( ! empty( $connection['connection_data']['token_id'] ) ) $unique_id = $connection['connection_data']['token_id']; // Did we skip this connection? if ( get_post_meta( $post_id, $this->POST_SKIP . $unique_id, true ) ) { continue; } $services[ $this->get_service_label( $service_name ) ][] = $this->get_display_name( $service_name, $connection ); } } return $services; } /** * Is the post Publicize-able? * * Only valid prior to Publicizing a Post. * * @param WP_Post $post * @return bool */ function post_is_publicizeable( $post ) { if ( ! $this->post_type_is_publicizeable( $post->post_type ) ) return false; // This is more a precaution. To only publicize posts that are published. (Mostly relevant for Jetpack sites) if ( 'publish' !== $post->post_status ) { return false; } // If it's not flagged as ready, then abort. @see ::flag_post_for_publicize() if ( ! get_post_meta( $post->ID, $this->PENDING, true ) ) return false; return true; } /** * Is a given post type Publicize-able? * * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via * the publicize_post_types array filter. * * @param string $post_type The post type to check. * @return bool True if the post type can be Publicized. */ function post_type_is_publicizeable( $post_type ) { if ( 'post' == $post_type ) return true; return post_type_supports( $post_type, 'publicize' ); } /** * Already-published posts should not be Publicized by default. This filter sets checked to * false if a post has already been published. * * Attached to the `publicize_checkbox_default` filter * * @param bool $checked * @param int $post_id * @param string $service_name 'facebook', 'twitter', etc * @param object|array The Connection object (WordPress.com) or array (Jetpack) * @return bool */ function publicize_checkbox_default( $checked, $post_id, $service_name, $connection ) { if ( 'publish' == get_post_status( $post_id ) ) { return false; } return $checked; } /* * Util */ /** * Converts a Publicize message template string into a sprintf format string * * @param string[] $args * 0 - The Publicize message template: 'Check out my post: %title% @ %url' * ... - The template tags 'title', 'url', etc. * @return string */ protected static function build_sprintf( $args ) { $search = array(); $replace = array(); foreach ( $args as $k => $arg ) { if ( 0 == $k ) { $string = $arg; continue; } $search[] = "%$arg%"; $replace[] = "%$k\$s"; } return str_replace( $search, $replace, $string ); } } function publicize_calypso_url() { $calypso_sharing_url = 'https://wordpress.com/marketing/connections/'; if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) { $site_suffix = Jetpack::build_raw_urls( home_url() ); } elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) { $site_suffix = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() ); } if ( $site_suffix ) { return $calypso_sharing_url . $site_suffix; } else { return $calypso_sharing_url; } }