Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync: IDC: Add get_raw_url method to Jetpack_Sync_Functions #5852

Merged
merged 20 commits into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c411790
Sync: IDC: Add get_raw_url method to Jetpack_Sync_Functions
ebinnion Dec 12, 2016
9514416
Sync: IDC: Add SSL support to Jetpack_Sync_Functions::get_raw_url()
ebinnion Dec 12, 2016
b2895f9
Sync: IDC: Allow using raw URL with constant
ebinnion Dec 12, 2016
0bb2aeb
Sync: IDC: Add filters to the home_url and site_url callables
ebinnion Dec 12, 2016
07df0da
Sync: IDC: Add support for domain mapping
ebinnion Dec 12, 2016
aecd3d5
Sync: IDC: 3rd Party: Hook domain mapping plugins to sync filters
ebinnion Dec 13, 2016
6c37dc1
Sync: IDC: Add tests for 3rd party domain mapping support
ebinnion Dec 13, 2016
8d3a7fb
Sync: IDC: Fix broken test by changing constant to static variable
ebinnion Dec 13, 2016
b9a8df6
Sync: IDC: Use Jetpack_Sync_Options to bypass potential caching issues
ebinnion Dec 13, 2016
7214ce7
Sync: IDC: Remove error_log that was causing tests to fail
ebinnion Dec 13, 2016
8be4bea
Sync: IDC: Swap DOMAIN_MAPPING constant check to SUNRISE_LOADED
ebinnion Dec 14, 2016
e54dbeb
Sync: Default to using raw home and siteurl values
ebinnion Jan 30, 2017
f6e22f5
Sync: Stop using removed Jetpack_Sync_Options class
ebinnion Jul 19, 2017
e244dde
Sync: IDC: Get home/siteurl with sync functions method
ebinnion Jul 22, 2017
bfd6b46
IDC: Remove broken test that is no longer applicable
ebinnion Jul 22, 2017
1824c78
Sync: Factor out logic to get raw URL; Do not set scheme
ebinnion Jul 22, 2017
0404b78
Sync: Update broken test after last commit to not modify protocol on …
ebinnion Jul 22, 2017
15f0555
Sync: IDC: Switch IDC methods to conditonally use raw URL as well
ebinnion Jul 24, 2017
c448404
Sync: IDC: Update docblocks to 5.2 for raw URL logic
ebinnion Jul 24, 2017
1e4a204
Sync: IDC: Simplify some logic around getting URLs
ebinnion Jul 24, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 3rd-party/3rd-party.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_once( JETPACK__PLUGIN_DIR . '3rd-party/bitly.php' );
require_once( JETPACK__PLUGIN_DIR . '3rd-party/bbpress.php' );
require_once( JETPACK__PLUGIN_DIR . '3rd-party/woocommerce.php' );
require_once( JETPACK__PLUGIN_DIR . '3rd-party/domain-mapping.php' );

// We can't load this conditionally since polldaddy add the call in class constuctor.
require_once( JETPACK__PLUGIN_DIR . '3rd-party/polldaddy.php' );
Expand Down
113 changes: 113 additions & 0 deletions 3rd-party/domain-mapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/**
* Class Jetpack_3rd_Party_Domain_Mapping
*
* This class contains methods that are used to provide compatibility between Jetpack sync and domain mapping plugins.
*/
class Jetpack_3rd_Party_Domain_Mapping {

/**
* @var Jetpack_3rd_Party_Domain_Mapping
**/
private static $instance = null;

/**
* An array of methods that are used to hook the Jetpack sync filters for home_url and site_url to a mapping plugin.
*
* @var array
*/
static $test_methods = array(
'hook_wordpress_mu_domain_mapping',
'hook_wpmu_dev_domain_mapping'
);

static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new Jetpack_3rd_Party_Domain_Mapping;
}

return self::$instance;
}

private function __construct() {
add_action( 'plugins_loaded', array( $this, 'attempt_to_hook_domain_mapping_plugins' ) );
}

/**
* This function is called on the plugins_loaded action and will loop through the $test_methods
* to try and hook a domain mapping plugin to the Jetpack sync filters for the home_url and site_url callables.
*/
function attempt_to_hook_domain_mapping_plugins() {
if ( ! Jetpack_Constants::is_defined( 'SUNRISE' ) ) {
return;
}

$hooked = false;
$count = count( self::$test_methods );
for ( $i = 0; $i < $count && ! $hooked; $i++ ) {
$hooked = call_user_func( array( $this, self::$test_methods[ $i ] ) );
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool!

/**
* This method will test for a constant and function that are known to be used with Donncha's WordPress MU
* Domain Mapping plugin. If conditions are met, we hook the domain_mapping_siteurl() function to Jetpack sync
* filters for home_url and site_url callables.
*
* @return bool
*/
function hook_wordpress_mu_domain_mapping() {
if ( ! Jetpack_Constants::is_defined( 'SUNRISE_LOADED' ) || ! $this->function_exists( 'domain_mapping_siteurl' ) ) {
return false;
}

add_filter( 'jetpack_sync_home_url', 'domain_mapping_siteurl' );
add_filter( 'jetpack_sync_site_url', 'domain_mapping_siteurl' );

return true;
}

/**
* This method will test for a class and method known to be used in WPMU Dev's domain mapping plugin. If the
* method exists, then we'll hook the swap_to_mapped_url() to our Jetpack sync filters for home_url and site_url.
*
* @return bool
*/
function hook_wpmu_dev_domain_mapping() {
if ( ! $this->class_exists( 'domain_map' ) || ! $this->method_exists( 'domain_map', 'utils' ) ) {
return false;
}

$utils = $this->get_domain_mapping_utils_instance();
add_filter( 'jetpack_sync_home_url', array( $utils, 'swap_to_mapped_url' ) );
add_filter( 'jetpack_sync_site_url', array( $utils, 'swap_to_mapped_url' ) );

return true;
}

/*
* Utility Methods
*
* These methods are very minimal, and in most cases, simply pass on arguments. Why create them you ask?
* So that we can test.
*/

public function method_exists( $class, $method ) {
return method_exists( $class, $method );
}

public function class_exists( $class ) {
return class_exists( $class );
}

public function function_exists( $function ) {
return function_exists( $function );
}

public function get_domain_mapping_utils_instance() {
return domain_map::utils();
}
}

Jetpack_3rd_Party_Domain_Mapping::init();
5 changes: 3 additions & 2 deletions class.jetpack-xmlrpc-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,10 @@ function sync_object( $args ) {
* @return array
*/
function validate_urls_for_idc_mitigation() {
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php';
return array(
'home' => get_home_url(),
'siteurl' => get_site_url(),
'home' => Jetpack_Sync_Functions::home_url(),
'siteurl' => Jetpack_Sync_Functions::site_url(),
);
}

Expand Down
5 changes: 3 additions & 2 deletions class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -5797,9 +5797,10 @@ public static function normalize_url_protocol_agnostic( $url ) {
* @return array Array of the local urls, wpcom urls, and error code
*/
public static function get_sync_error_idc_option( $response = array() ) {
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php';
$local_options = array(
'home' => get_home_url(),
'siteurl' => get_site_url(),
'home' => Jetpack_Sync_Functions::home_url(),
'siteurl' => Jetpack_Sync_Functions::site_url(),
);

$options = array_merge( $local_options, $response );
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<testsuite name="jitm">
<file>tests/php/test_class.jetpack-jitm.php</file>
</testsuite>
<testsuite name="3rd-party">
<directory prefix="test_" suffix=".php">tests/php/3rd-party</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
Expand Down
5 changes: 3 additions & 2 deletions sync/class.jetpack-sync-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,16 @@ static function set_is_importing_true() {
}

static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration ) {
require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php';
Jetpack::load_xml_rpc_client();

$query_args = array(
'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action
'codec' => $codec_name, // send the name of the codec used to encode the data
'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences
'queue' => $queue_id, // sync or full_sync
'home' => get_home_url(), // Send home url option to check for Identity Crisis server-side
'siteurl' => get_site_url(), // Send siteurl option to check for Identity Crisis server-side
'home' => Jetpack_Sync_Functions::home_url(), // Send home url option to check for Identity Crisis server-side
'siteurl' => Jetpack_Sync_Functions::site_url(), // Send siteurl option to check for Identity Crisis server-side
'cd' => sprintf( '%.4f', $checkout_duration), // Time spent retrieving queue items from the DB
'pd' => sprintf( '%.4f', $preprocess_duration), // Time spent converting queue items into data to send
);
Expand Down
68 changes: 60 additions & 8 deletions sync/class.jetpack-sync-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,53 @@ public static function file_system_write_access() {
return false;
}

/**
* Helper function that is used when getting home or siteurl values. Decides
* whether to get the raw or filtered value.
*
* @return string
*/
public static function get_raw_or_filtered_url( $url_type ) {
if (
! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) ||
Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' )
) {
$url = self::get_raw_url( $url_type );
} else {
$url_function = ( 'home' == $url_type )
? 'home_url'
: 'site_url';
$url = self::normalize_www_in_url( $url_type, $url_function );
$url = self::get_protocol_normalized_url( $url_function, $url );
}

return $url;
}

public static function home_url() {
return self::get_protocol_normalized_url(
'home_url',
self::normalize_www_in_url( 'home', 'home_url' )
);
$url = self::get_raw_or_filtered_url( 'home' );

/**
* Allows overriding of the home_url value that is synced back to WordPress.com.
*
* @since 5.2
*
* @param string $home_url
*/
return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) );
}

public static function site_url() {
return self::get_protocol_normalized_url(
'site_url',
self::normalize_www_in_url( 'siteurl', 'site_url' )
);
$url = self::get_raw_or_filtered_url( 'siteurl' );

/**
* Allows overriding of the site_url value that is synced back to WordPress.com.
*
* @since 5.2
*
* @param string $site_url
*/
return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) );
}

public static function main_network_site_url() {
Expand Down Expand Up @@ -184,6 +219,23 @@ public static function get_protocol_normalized_url( $callable, $new_value ) {
return set_url_scheme( $new_value, $forced_scheme );
}

public static function get_raw_url( $option_name ) {
$value = null;
$constant = ( 'home' == $option_name )
? 'WP_HOME'
: 'WP_SITEURL';

if ( Jetpack_Constants::is_defined( $constant ) ) {
$value = Jetpack_Constants::get_constant( $constant );
} else {
// Let's get the option from the database so that we can bypass filters. This will help
// ensure that we get more uniform values.
$value = Jetpack_Options::get_raw_option( $option_name );
}

return $value;
}

public static function normalize_www_in_url( $option, $url_function ) {
$url = wp_parse_url( call_user_func( $url_function ) );
$option_url = wp_parse_url( get_option( $option ) );
Expand Down
4 changes: 2 additions & 2 deletions sync/class.jetpack-sync-module-callables.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function maybe_sync_callables() {
return;
}

$callable_checksums = (array) get_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() );
$callable_checksums = (array) Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this causing some misbehaviours?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From memory, I noticed that the callables weren't syncing as often as I expected them to. Once I made this change, it cleared up.


// only send the callables that have changed
foreach ( $callables as $name => $value ) {
Expand All @@ -166,7 +166,7 @@ public function maybe_sync_callables() {
$callable_checksums[ $name ] = $checksum;
}
}
update_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
}

public function expand_callables( $args ) {
Expand Down
Loading