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

Refactor JS snippet to use WP AJAX #109

Merged
merged 12 commits into from
Apr 13, 2020
Merged

Refactor JS snippet to use WP AJAX #109

merged 12 commits into from
Apr 13, 2020

Conversation

stklcode
Copy link
Contributor

@stklcode stklcode commented Oct 2, 2018

The current JavaScript tracking method issues a GET-request to the home page with two parameters, statify_referrer and statify_target. As a result the usual WP page is processed, due to the parameters without caching (at least with the most comon caching plugins).

As mehtioned in #100 the tracking call might take long time depending on the actual setup (seen times up to 2s). That's because the request is intercepted and completed after Statify has been intialized. And that is typically not the first plugin loaded.

This PR replaces the custom GET tracking method by a POST call to a WP AJAX action. On AJAX calls the overhead is far less than on actual page generation, because many plugins opt-out immediately.

PS: I already tagged some changes in the PHP code with @since 1.7.0, because I don't see this change to appear in any 1.6.x release obviously. Doesn't mean anything however, might be rescheduled to any milestone 😃

The custom GET request intercepts normal page generation and might
trigger other plugins' actions before Statify is loaded. It also
provided an open door for lightweight malicious requests targeting the
statistics.
Using WP AJAX including Nonce verification reduces both problems.
statify.php Outdated
@@ -20,6 +20,7 @@
define( 'STATIFY_FILE', __FILE__ );
define( 'STATIFY_DIR', dirname( __FILE__ ) );
define( 'STATIFY_BASE', plugin_basename( __FILE__ ) );
define( 'STATIFY_VERSION', '1.6.3' );
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be 1.7.0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely (if we don‘t add this to 1.6.x - and I don‘t think so - or postpone this to 1.8 obviously)

js/snippet.js Outdated Show resolved Hide resolved
@patrickrobrecht patrickrobrecht added this to the 1.7.0 milestone Oct 29, 2018
inc/class-statify-frontend.php Outdated Show resolved Hide resolved
/**
* Track the page view via AJAX.
*
* @return void
Copy link
Member

Choose a reason for hiding this comment

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

In my mind there is not helpful. Also the hint on phpdoc - This tag should not be used for constructors or methods defined with a void return type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're using this tag in 15 places... Apparently all introduced by myself (default pattern in my IDE because it's mandatory in most of my styleguides - and btw. the PHPdoc ref marks this as "MAY be omitted").

We shouldn't mix it up, so either remove all or none.
Along with some int(eger) and bool(ean) tags probably little out of scope of this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, plugincollektiv need a style guide, simplest way is the WP codex. In this context it is only a hint and the result of different views, works outside of the pluginkollektiv projects. Feel free to leave that or remove all ;)

@bueltge
Copy link
Member

bueltge commented Apr 4, 2019

Hi @stklcode
great PR, thanks for your time to work again on the plugin. I have only two small hints, not important, but maybe helpful for you to merge this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants