Skip to content

Commit 850407c

Browse files
authored
Merge pull request #290 from plausible/lower_timeout
Improved: lowered timeout and connect_timeout to 10 and 5 seconds.
2 parents 6a29e39 + cd80d27 commit 850407c

File tree

2 files changed

+32
-42
lines changed

2 files changed

+32
-42
lines changed

README.md

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
[![Github CI](https://github.com/plausible/wordpress/actions/workflows/push.yml/badge.svg)](https://github.com/plausible/wordpress/actions/workflows/push.yml) ![WordPress version](https://img.shields.io/wordpress/plugin/v/plausible-analytics.svg) ![WordPress Rating](https://img.shields.io/wordpress/plugin/r/plausible-analytics.svg) ![WordPress Downloads](https://img.shields.io/wordpress/plugin/dt/plausible-analytics.svg)
66

77
Welcome to the Plausible Analytics WordPress Plugin GitHub repository. This is the code source and the center of active
8-
development. Here you can
9-
browse the source, look at open issues, and contribute to the project.
8+
development. Here you can browse the source, look at open issues, and contribute to the project.
109

1110
## Getting Started
1211

1312
If you're looking to contribute or actively develop on Plausible Analytics then skip ahead to
14-
the [Local Development](https://github.com/plausible/wordpress/#local-development) section below. The following is if
15-
you're looking to actively use
16-
the plugin on your WordPress site.
13+
the [Local Development](https://github.com/plausible/wordpress/#local-development) section below. The following is if you're looking to actively use the plugin on your WordPress site.
1714

1815
### Minimum Requirements
1916

@@ -24,20 +21,16 @@ the plugin on your WordPress site.
2421
### Automatic installation
2522

2623
Automatic installation is the easiest option as WordPress handles the file transfers itself, and you don't need to leave
27-
your web browser. To do an
28-
automatic installation of Plausible Analytics, log in to your WordPress dashboard, navigate to the Plugins menu and
29-
click "Add New".
24+
your web browser. To do an automatic installation of Plausible Analytics, log in to your WordPress dashboard, navigate to the Plugins menu, and
25+
click "Add New."
3026

31-
In the search field type "Plausible Analytics" and click Search Plugins. Once you have found the plugin you can view
32-
details about it such as the
33-
point release, rating and description. Most importantly of course, you can install it by simply clicking "Install Now".
27+
In the search field, type "Plausible Analytics" and click Search Plugins. Once you have found the plugin you can view
28+
details about it such as the point release, rating, and description. Most importantly, of course, you can install it by simply clicking "Install Now."
3429

3530
### Manual installation
3631

3732
The manual installation method involves downloading our plugin and uploading it to your server via your favorite FTP
38-
application. The
39-
WordPress codex
40-
contains [instructions on how to do this](https://codex.wordpress.org/Managing_Plugins#Manual_Plugin_Installation).
33+
application. The WordPress codex contains [instructions on how to do this](https://codex.wordpress.org/Managing_Plugins#Manual_Plugin_Installation).
4134

4235
### Support
4336

@@ -46,7 +39,7 @@ use the following channels:
4639

4740
* [WP.org Support Forums](https://wordpress.org/support/plugin/plausible-analytics) - for all users
4841

49-
## Available Actions, Filters and Toggles
42+
## Available Actions, Filters, and Toggles
5043

5144
### Filters
5245

@@ -58,8 +51,10 @@ use the following channels:
5851
enabled.
5952
- `plausible_analytics_pageview_properties`: Allows you to add custom pageview properties when the Pageview Properties
6053
option is enabled under
61-
Enhanced Measurements. For examples, read
54+
Enhanced Measurements. For example, read
6255
the [documentation on Pageview Properties](https://plausible.io/docs/custom-props/for-pageviews).
56+
- `plausible_analytics_api_timeout`: Allows you to modify the timeout for the total duration of Plausible API requests.
57+
- `plausible_analytics_api_connect_timeout`: Allows you to modify the connection timeout for Plausible API requests.
6358

6459
### Actions
6560

@@ -70,17 +65,13 @@ use the following channels:
7065
### Toggles
7166

7267
Using constants, you can modify the behavior of the plugin. `wp-config.php` is the best place to define constants. If
73-
you're using a custom plugin,
74-
make sure its code is loaded before this plugin.
68+
you're using a custom plugin, make sure its code is loaded before this plugin.
7569

7670
- `PLAUSIBLE_SELF_HOSTED_DOMAIN`: Especially useful for Multisite instances using the self-hosted version of Plausible,
77-
this constant allows you to
78-
specify the Self-Hosted Domain for all subsites at once. **IMPORTANT**: this constant takes precedence over the
79-
plugin's setting. So, if this
80-
constant is defined, changing the setting won't have any effect.
81-
- `plausible_proxy`: Appending this `GET`-parameter will force enable the proxy on the page you\'re calling it. This
82-
will allow you to test your proxy
83-
in the frontend, before enabling the option.
71+
this constant allows you to specify the Self-Hosted Domain for all subsites at once. **IMPORTANT**: this constant takes precedence over the
72+
plugin's setting. So, if this constant is defined, changing the setting won't have any effect.
73+
- `plausible_proxy`: Appending this `GET`-parameter will force-enable the proxy on the page you\'re calling it. This
74+
will allow you to test your proxy in the frontend before enabling the option.
8475

8576
## Local Development
8677

@@ -100,10 +91,8 @@ That's it. You're now ready to start development.
10091
Plausible Analytics relies on several npm commands to get you started:
10192

10293
* `npm run watch` - Live reloads JS and SASS files. Typically, you'll run this command before you start development.
103-
It's necessary to build the
104-
JS/CSS
105-
however if you're working strictly within PHP it may not be necessary to run.
106-
* `npm run dev` - Runs a one time build for development. No production files are created.
94+
It's necessary to build the JS/CSS, however, if you're working strictly within PHP it may not be necessary to run.
95+
* `npm run dev` - Runs a one-time build for development. No production files are created.
10796
* `npm run production` - Builds the minified production files for release.
10897

10998
### Development Notes
@@ -127,10 +116,8 @@ Plausible Analytics relies on several npm commands to get you started:
127116
128117
### Regenerating the OpenAPI PHP Client
129118
130-
This plugin uses a OpenAPI PHP Client which is autogenerated by the OpenAPI generator to reduce contract violations,
131-
etc. to a minimum. But, since
132-
this is a WordPress plugin, some manual modifications need to be done to make sure it doesn't conflict with other
133-
plugins:
119+
This plugin uses an OpenAPI PHP Client which is autogenerated by the OpenAPI generator to reduce contract violations, etc. to a minimum. But since this is a WordPress plugin, some manual modifications
120+
need to be done to make sure it doesn't conflict with other plugins:
134121
135122
> [!IMPORTANT]
136123
> You need to have Java and [OpenAPI Generator](https://openapi-generator.tech/docs/installation/) installed
@@ -139,9 +126,9 @@ plugins:
139126
- (Re)generate the PHP client using the following command (trigger it from the Plugin's root dir as output will be saved
140127
to `src/Client`):
141128
`openapi-generator-cli generate -i https://plausible.io/api/plugins/spec/openapi -g php -o src/Client --additional-properties=identifierNamingConvention=snake_case,invokerPackage="Plausible\\Analytics\\WP\\Client" --global-property=apis,models,supportingFiles,modelDocs=false,modelTests=false,apiDocs=false,apiTests=false`
142-
- (When regenerating the PHP client this step can be skipped) Navigate to the `src/Client` director and install Composer
129+
- (When regenerating the PHP client, this step can be skipped) Navigate to the `src/Client` director and install Composer
143130
dependencies: `composer install --no-dev`
144-
- (When regenerating the PHP client this step can be skipped) Run `mozart compose` from the `src/Client` directory (Make
131+
- (When regenerating the PHP client, this step can be skipped) Run `mozart compose` from the `src/Client` directory (Make
145132
sure Mozart is installed
146133
globally)
147134
- In the `src/Client/lib` directory, replace all occurrences of ` GuzzleHttp` (mind the space) with

src/Client.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Plausible\Analytics\WP\Client\Model\UnprocessableEntityError;
2222

2323
/**
24-
* This class acts as middleware between our OpenAPI generated API client and our WP plugin, and takes care of setting
24+
* This class acts as middleware between our OpenAPI generated API client and our WP plugin and takes care of setting
2525
* the required configuration, so we can use the Client in a unified manner.
2626
*/
2727
class Client {
@@ -33,13 +33,16 @@ class Client {
3333
/**
3434
* Setup basic authorization, basic_auth.
3535
*
36-
* @param string $token Allows to specify the token, e.g. when it's not stored in the DB yet.
36+
* @param string $token Allows specifying the token, e.g., when it's not stored in the DB yet.
3737
*/
3838
public function __construct( $token = '' ) {
39-
$config = Configuration::getDefaultConfiguration()->setUsername( 'WordPress' )->setPassword(
40-
$token
41-
)->setHost( Helpers::get_hosted_domain_url() );
42-
$this->api_instance = new DefaultApi( new GuzzleClient(), $config );
39+
$config = Configuration::getDefaultConfiguration()
40+
->setUsername( 'WordPress' )
41+
->setPassword( $token )
42+
->setHost( Helpers::get_hosted_domain_url() );
43+
$timeout = (float) apply_filters( 'plausible_analytics_api_timeout', 10.0 );
44+
$connect_timeout = (float) apply_filters( 'plausible_analytics_api_connect_timeout', 5.0 );
45+
$this->api_instance = new DefaultApi( new GuzzleClient( [ 'timeout' => $timeout, 'connect_timeout' => $connect_timeout ] ), $config );
4346
}
4447

4548
/**
@@ -239,7 +242,7 @@ public function update_tracker_script_configuration( $tracker_script_config_upda
239242

240243
/**
241244
* @param Exception $e
242-
* @param string $error_message The human-readable part of the error message, requires a %s at the end!
245+
* @param string $error_message The human-readable part of the error message, requires a %s at the end!
243246
*
244247
* @return void
245248
*

0 commit comments

Comments
 (0)