Skip to content

Don't call site_url() twice#6130

Merged
eliorivero merged 1 commit intomasterfrom
update/optimize-is_development_mode
Feb 7, 2017
Merged

Don't call site_url() twice#6130
eliorivero merged 1 commit intomasterfrom
update/optimize-is_development_mode

Conversation

@mjangda
Copy link
Copy Markdown
Member

@mjangda mjangda commented Jan 19, 2017

In Jetpack::is_development_mode, we current call the site_url() function twice on the same line. This is a waste since the value is unlikely to change between those two calls.

Changes proposed in this Pull Request:

Assign the value of site_url() to a variable and uses that value to check whether we're on a tld-less domain or not.

Testing instructions:

  • Make sure JETPACK_DEV_DEBUG constant is not set.
  • With Jetpack on a domain like localhost, verify that the value of Jetpack::is_development_mode() is true.
  • With Jetpack on a domain like localhost.dev, verify that the value of Jetpack::is_development_mode() is false.


elseif ( site_url() && false === strpos( site_url(), '.' ) ) {
$development_mode = true;
elseif ( $site_url = site_url() ) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure why we even need to check if site_url() returns something (something would be pretty broken if it didn't), but it's possible it's this way for a reason.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't know—it's original to the function as committed to Jetpack eff10c9

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, doesn't hurt to leave it.

@jeherve jeherve added [Feature] Offline Mode aka Development Mode or Debug Mode [Status] Needs Review This PR is ready for review. [Type] Janitorial labels Jan 19, 2017
@jeherve jeherve added this to the 4.6 milestone Jan 19, 2017
@georgestephanis
Copy link
Copy Markdown
Contributor

Too many comparisons on the same line, like $development_mode = false === strpos( $site_url, '.' ); read as difficult to parse to me when skimming code, and I hesitate to merge them.

I'd much rather see this code block changed to something resembling ...

$site_url = site_url();
if ( ! empty( $site_url ) || ! wp_in( '.', $site_url ) ) {
    $development_mode = true;
}

We ship wp_in() in ~/functions.compat.php in Jetpack already, so it is available for use and possibly easier to understand, but I'd be just as fine manually writing that out as false === strpos() if y'all would rather.

@jeherve jeherve added [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. labels Jan 25, 2017
@georgestephanis georgestephanis modified the milestones: 3/17 - March, 2/17 - February Jan 30, 2017
@mjangda
Copy link
Copy Markdown
Member Author

mjangda commented Jan 31, 2017

Too many comparisons on the same line read as difficult to parse to me when skimming code

Agreed, but this is an inside an elseif clause so we need to trade off between readability or a calling site_url() + the equality check every time the function is called (even when JETPACK_DEV_DEBUG is set).

We ship wp_in() in ~/functions.compat.php in Jetpack already, so it is available for use and possibly easier to understand

If the load order ever changes, that could be bad :)

@jeherve jeherve added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jan 31, 2017
@samhotchkiss samhotchkiss removed this from the 4.7.0 - March 2017 milestone Feb 3, 2017
@dereksmart dereksmart added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Feb 6, 2017
Copy link
Copy Markdown
Contributor

@eliorivero eliorivero left a comment

Choose a reason for hiding this comment

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

Added a comment to fix the indentation that was previously ok.
To further simplify this and avoid the $development_mode assignment, this could be reduced to

return apply_filters( 'jetpack_development_mode',
	( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) || ( false === strpos( (string) site_url(), '.' ) )
		? true
		: false
);

If it feels too complex, maybe it could be rewritten as:

if ( defined( 'JETPACK_DEV_DEBUG' ) ) {
	$development_mode = JETPACK_DEV_DEBUG;
} elseif ( $site_url = site_url() ) {
	$development_mode = false === strpos( $site_url, '.' );
} else {
	$development_mode = false;
}

the idea is to initialize $development_mode only once.

elseif ( site_url() && false === strpos( site_url(), '.' ) ) {
$development_mode = true;
elseif ( $site_url = site_url() ) {
$development_mode = false === strpos( $site_url, '.' );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you fix the indentation here? It was changed from a tab to spaces (and more than needed). The spacing before the elseif could also be removed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep, good catch. Fixed the indents and spacing.

Instead of calling the function twice, which is a waste, assign the
value to a variable and use that value to check whether we're on a
tld-less domain or not.
@mjangda mjangda force-pushed the update/optimize-is_development_mode branch from 6e78b4c to 8b75eb6 Compare February 7, 2017 15:14
@mjangda
Copy link
Copy Markdown
Member Author

mjangda commented Feb 7, 2017

the idea is to initialize $development_mode only once.

I don't think we gain that much from this change.

Copy link
Copy Markdown
Contributor

@eliorivero eliorivero left a comment

Choose a reason for hiding this comment

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

LGTM 🐑

@eliorivero eliorivero merged commit 9f295bc into master Feb 7, 2017
@eliorivero eliorivero deleted the update/optimize-is_development_mode branch February 7, 2017 19:09
@eliorivero eliorivero removed the [Status] Ready to Merge Go ahead, you can push that green button! label Feb 7, 2017
jeherve added a commit that referenced this pull request Feb 8, 2017
jeherve added a commit that referenced this pull request Feb 21, 2017
dereksmart pushed a commit that referenced this pull request Feb 28, 2017
* Changelog: update stable tag and move changelog to changelog.txt

Also remove old releases from readme.txt to keep the changelog tab short.

* Changelog: add #5883

Also update the filter's docblock to match new version.

* Changelog: add #5938

* Changelog: add #6298

* Changelog: add #3405

* Changelog: add #5941

* Changelog: add #6239

* Changelog: add #6281

* Changelog: add #6303

* Changelog: add #6018

* Changelog: add #6300

* Changelog: add #6296

* Changelog: add #6130

* Changelog: add #6292

* Readme: remove extra "on".

* Changelog: add #6307

* Changelog: add #3297

* Changelog: add #6275

* Changelog: add #6321

* Changelog: add #6297

* Readme: update the support forum link anchor.

Anchor changed when WordPress.org forums were updated to bbPress 2

* Readme: update list of a12s, it wasn't up to date anymore!

* Changelog: add #6338

* Changelog: add #6337

* Changelog: add #6335

* Changelog: add #6333

* Testing List: first version of the 4.7 testing list.

* Changelog: add #6332

* Changelog: add #6325

* Changelog: add #6326

* Changelog: add #6339

* Changelog: add #6342

* Changelog: add #6343

* Changelog: add #6346

* Changelog: add #6347

* Changelog: add #6279

* Changelog: add #6306

* Changelog: add #6312

* Changelog: add #6316

* Changelog: add #6171

* Changelog: add #6317

* Changelog: add #6246

* Changelog: add #6263

* Changelog: add #4220

* Changelog: add #5888

* Changelog: add #3406

* Changelog: add #3637

* Changelog: add #6320

* Changelog: add #5992

* Changelog: add #6322

* Changelog: add #6324

* Changelog: add #6352

* Changelog: add #6355

* Changelog: add #6360

* Changelog: add #6362

* Changelog: add #6369, #6382

* Changelog: add #6370

* Changelog: add #6375

* Changelog: add #6383

* Changelog: add #6384

* Changelog: add #6386

* Changelog: add #6395

* Changelog: add #6403

* Changelog: add #6406

* Changelog: add #6418

* Changelog: add #6419

* Changelog: add #6434

* Changelog: add #6446

* Changelog: add #6006

* Changelog: add #6096

* Changelog: add #6399

* Changelog: fix typo.

@see #6331 (comment)

* Changelog: add #6440

* Changelog: add #6443

* Changelog: add #6445

* Changelog: add #6463

* Changelog: add #6468

* Changelog: add #6471

* Changelog: add #6474

* Changelog: add #6480

* Changelog: add #6497

* Changelog: add #6499

* Changelog: add #6514

* Changelog: add #6267

* Changelog: add #5940

* Changelog: add #6492

* Changelog: add #5281

* Changelog: add #6327

* Changelog: add #6451

* Changelog: add #6525

* Changelog: add #6530
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Offline Mode aka Development Mode or Debug Mode [Type] Janitorial

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants