diff --git a/projects/plugins/wpcomsh/src/wpcomsh.php b/projects/plugins/wpcomsh/src/wpcomsh.php new file mode 100644 index 0000000000000..c3710e95bafaa --- /dev/null +++ b/projects/plugins/wpcomsh/src/wpcomsh.php @@ -0,0 +1,634 @@ +,
,  and 
+	// use 
in support docs where linkifying + // breaks shortcodes, etc. + $_split = preg_split( '/(<[^<>]+>)/i', $content, -1, PREG_SPLIT_DELIM_CAPTURE ); + $end = ''; + $out = ''; + $combine = ''; + $split = array(); + + // Defines a set of rules for the wpcomsh_make_content_clickable() function to ignore matching html elements. + $make_clickable_rules = array( + array( + 'match' => array( ' '', + ), + array( + 'match' => array( '
' ),
+			'end'   => '
', + ), + array( + 'match' => array( '' ), + 'end' => '', + ), + array( + 'match' => array( '', + ), + array( + 'match' => array( '', + ), + array( + 'match' => array( '', + ), + array( + 'match' => array( '
' === $chunk[ strlen( $chunk ) - 1 ] ) ) { + $out .= $chunk; + continue; + } + + // three strpos() are faster than one preg_match() here. If we need to check for more protocols, preg_match() would probably be better. + if ( strpos( $chunk, 'http://' ) !== false || strpos( $chunk, 'https://' ) !== false || strpos( $chunk, 'www.' ) !== false ) { + // looks like there is a plain-text url. + $out .= make_clickable( $chunk ); + } else { + $out .= $chunk; + } + } + + return $out; +} +add_filter( 'the_content', 'wpcomsh_make_content_clickable', 120 ); +add_filter( 'the_excerpt', 'wpcomsh_make_content_clickable', 120 ); + +/** + * Hide scan threats from transients + * + * @param mixed $response The response. + * + * @return mixed + */ +function wpcomsh_hide_scan_threats_from_transients( $response ) { + if ( ! empty( $response->threats ) ) { + $response->threats = array(); + } + return $response; +} +add_filter( 'transient_jetpack_scan_state', 'wpcomsh_hide_scan_threats_from_transients' ); + +/** + * Hide scan threats from api + * + * @param mixed $response The reponse. + * + * @return mixed + */ +function wpcom_hide_scan_threats_from_api( $response ) { + if ( + ! ( $response instanceof WP_REST_Response ) + || $response->get_matched_route() !== '/jetpack/v4/scan' + ) { + return $response; + } + $response_data = $response->get_data(); + if ( empty( $response_data['data'] ) || ! is_string( $response_data['data'] ) ) { + return $response; + } + + $json_body = json_decode( $response_data['data'], true ); + if ( null === $json_body || empty( $json_body['threats'] ) ) { + return $response; + } + + $json_body['threats'] = array(); + $response_data['data'] = wp_json_encode( $json_body ); + $response->set_data( $response_data ); + + return $response; +} +add_filter( 'rest_post_dispatch', 'wpcom_hide_scan_threats_from_api' ); + +/** + * Collect RUM performance data + * https://atomicp2.wordpress.com/2020/07/20/wordpress-com-on-atomic-request-rum/ + */ +function wpcomsh_footer_rum_js() { + $service = 'atomic'; + $allow_iframe = ''; + if ( 'admin_footer' === current_action() ) { + $service = 'atomic-wpadmin'; + + if ( method_exists( 'Jetpack_WPCOM_Block_Editor', 'init' ) ) { + $block_editor = Jetpack_WPCOM_Block_Editor::init(); + if ( $block_editor->is_iframed_block_editor() ) { + $service = 'atomic-gutenframe'; + $allow_iframe = 'data-allow-iframe="true"'; + } + } + } + + printf( + '' . "\n", //phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + esc_attr( $service ), + wp_kses_post( $allow_iframe ), + esc_url( 'https://s0.wp.com/wp-content/js/bilmur.min.js?m=' . gmdate( 'YW' ) ) + ); +} +add_action( 'wp_footer', 'wpcomsh_footer_rum_js' ); +add_action( 'admin_footer', 'wpcomsh_footer_rum_js' ); + +add_filter( 'amp_dev_tools_user_default_enabled', '__return_false' ); + +/** + * Tracks helper. Filters Jetpack TOS option if class exists. + * + * @param mixed $event The event. + * @param mixed $event_properties The event property. + * + * @return void + */ +function wpcomsh_record_tracks_event( $event, $event_properties ) { + if ( class_exists( '\Automattic\Jetpack\Tracking' ) ) { + // User has to agree to ToS for tracking. Thing is, on initial Simple -> Atomic we never set the ToS option. + // And since they agreed to WP.com ToS, we can track but in a roundabout way. :). + add_filter( 'jetpack_options', 'wpcomsh_jetpack_filter_tos_for_tracking', 10, 2 ); + + $jetpack_tracks = new \Automattic\Jetpack\Tracking( 'atomic' ); + $jetpack_tracks->tracks_record_event( + wp_get_current_user(), + $event, + $event_properties + ); + + remove_filter( 'jetpack_options', 'wpcomsh_jetpack_filter_tos_for_tracking', 10 ); + } +} + +/** + * Helper for filtering tos_agreed for tracking purposes. + * Explicit function so it can be removed afterwards. + * + * @param mixed $value The value. + * @param mixed $name Name. + * + * @return mixed + */ +function wpcomsh_jetpack_filter_tos_for_tracking( $value, $name ) { + if ( 'tos_agreed' === $name ) { + return true; + } + + return $value; +} + +/** + * Avoid proxied v2 banner + * + * @return void + */ +function wpcomsh_avoid_proxied_v2_banner() { + $priority = has_action( 'wp_footer', 'atomic_proxy_bar' ); + if ( false !== $priority ) { + remove_action( 'wp_footer', 'atomic_proxy_bar', $priority ); + } + + $priority = has_action( 'admin_footer', 'atomic_proxy_bar' ); + if ( false !== $priority ) { + remove_action( 'admin_footer', 'atomic_proxy_bar', $priority ); + } +} + +// We don't want to show a "PROXIED V2" banner for legacy widget previews +// which are normally embedded within another page. +if ( + defined( 'AT_PROXIED_REQUEST' ) && AT_PROXIED_REQUEST && + isset( $_GET['legacy-widget-preview'] ) && //phpcs:ignore WordPress.Security.NonceVerification + 0 === strncmp( $_SERVER['REQUEST_URI'], '/wp-admin/widgets.php?', strlen( '/wp-admin/widgets.php?' ) ) ) { //phpcs:ignore WordPress.Security.ValidatedSanitizedInput + add_action( 'plugins_loaded', 'wpcomsh_avoid_proxied_v2_banner' ); +} + +// Temporary feature flag for the new Reading Settings page. +add_filter( 'calypso_use_modernized_reading_settings', '__return_true' ); + +/** + * Temporary feature flags for the new Newsletter and podcasting Settings pages, + * its removal should be preceded by a removal of the filter's usage in Jetpack: https://github.com/Automattic/jetpack/pull/32146 + */ +add_filter( 'calypso_use_newsletter_settings', '__return_true' ); +add_filter( 'calypso_use_podcasting_settings', '__return_true' );