Skip to content

Commit 95a59f9

Browse files
committed
Added ability to change login errors
1 parent 3800aa0 commit 95a59f9

File tree

5 files changed

+546
-91
lines changed

5 files changed

+546
-91
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
*.bak
3+
*.log

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Changelog
2+
3+
### master
4+
5+
- Fixed: Firing sequence of `common_toolkit_loaded` hook
6+
- Added: `ctk_environment` filter
7+
- Added: Ability to change login errors
8+
- Added: Ability to cache JSON config file
9+
- Added: Ability to disable WordPress core, plugin and/or theme updates
10+
- Added: Ability to modify or disable WordPress heartbeat
11+
- Added: Ability to disable WordPress search
12+
- Added: Ability to define custom environment constant
13+
14+
### 0.8.0 - January 27, 2019
15+
16+
- Initial release

README.md

Lines changed: 172 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,127 @@
55

66
# WordPress Common Toolkit MU Plugin
77

8-
A simple [MU plugin](https://codex.wordpress.org/Must_Use_Plugins) for WordPress that adds functionality that I use on web site projects.
8+
A simple [MU plugin](https://codex.wordpress.org/Must_Use_Plugins) for WordPress that adds functionality that I use on web site projects, including a [configuration registry](#getting-configuration-values).
9+
10+
- [Installation](#installation)
11+
- [Configuration](#configuration)
12+
- [Features](#features)
13+
- [Environment Filter](#environment-filter)
14+
- [Action Hook](#action-hook)
15+
- [Shortcodes](#shortcodes)
916

1017
## Installation
1118

1219
Simply copy the `common-toolkit.php` file to your `wp-content/mu-plugins` directory (create one if it does not exist).
1320

14-
## Requirements
21+
### Requirements
1522

16-
- PHP 7.0 or higher
23+
- **PHP 5.4+** (via JSON config file) and **PHP 7.x** (via array _or_ JSON file)
1724
- WordPress 4.7 or higher
1825

1926
## Configuration
2027

21-
| **Variable** | **Description** | **Type** | **Default** |
22-
|------------------------|--------------------------------------------------------------------------------------------------------------------------|-------------|---------------|
23-
| `environment` | Environment of current instance (ex: 'production', 'development', 'staging') | string | "production" |
24-
| `disable_emojis` | Remove support for emojis | bool | false |
25-
| `admin_bar_color` | Change admin bar color in current environment | string | _null_ |
26-
| `script_attributes` | Enable support for [additional attributes](#add-attributes-to-enqueued-scripts) to script tags via wp_enqueue_script() | bool | flase |
27-
| `shortcodes` | Enable custom [shortcodes](#shortcodes) created by this class | bool | false |
28-
| `disable_xmlrpc` | Disable XML-RPC | bool | false |
29-
| `meta_generator` | Enable or change meta generator tags in page head and RSS feeds | bool/string | true |
30-
| `windows_live_writer` | Enable [Windows Live Writer](https://is.gd/Q6KjEQ) support | bool | true |
31-
| `feed_links` | Include RSS feed links in page head | bool | true |
28+
All variables are optional.
29+
30+
| **Variable** | **Description** | **Type** | **Default** |
31+
|---------------------------|--------------------------------------------------------------------------------------------------------------------------|-------------------|---------------|
32+
| `environment` | Environment of current instance (ex: 'production', 'development', 'staging') | string | "production" |
33+
| `environment_constant` | Constant used to determine environment, environmental variable name for `getenv()`. | string | "WP_ENV" |
34+
| `environment_production` | The label used to match if production environment. | string | "production" |
35+
| `admin_bar_color` | Change admin bar color in current environment | string | _null_ |
36+
| `disable_emojis` | Remove support for emojis | bool | false |
37+
| `disable_search` | Disable WordPress site search | bool | false |
38+
| `disable_updates` | Disable WordPress core, plugin and/or theme updates. Values: 'core', 'plugin', 'theme'; `true` for all | bool/string/array | false |
39+
| `disable_xmlrpc` | Disable XML-RPC | bool | false |
40+
| `feed_links` | Include RSS feed links in page head | bool | true |
41+
| `heartbeat` | Modify or disable the WordPress heartbeat. Set to integer to change, `false` to disable | bool/int | null |
42+
| `hide_login_errors` | Replaces login errors with generic "Login failed" text rather than specific reason | bool/string | null |
43+
| `meta_generator` | Enable or change meta generator tags in page head and RSS feeds | bool/string | false |
44+
| `script_attributes` | Enable support for [additional attributes](#add-attributes-to-enqueued-scripts) to script tags via wp_enqueue_script() | bool | flase |
45+
| `shortcodes` | Enable custom [shortcodes](#shortcodes) created by this class | bool | false |
46+
| `windows_live_writer` | Enable [Windows Live Writer](https://is.gd/Q6KjEQ) support | bool | true |
3247

3348
### Example
3449

35-
Add to your `wp-config.php`:
50+
#### Via Configuration File (PHP 5.6 or higher)
51+
52+
This is the preferred method if you wish to avoid having a complex array in your `wp-config.php`:
53+
54+
```php
55+
// Load configuration from a file in webroot.
56+
define( 'CTK_CONFIG', 'sample-config.json' );
57+
58+
// Load configuration from a file off of the parent directory of webroot
59+
define( 'CTK_CONFIG', '../conf/sample-config.json' );
60+
```
61+
62+
See [sample-config.json](https://github.com/dmhendricks/wordpress-mu-common-toolkit/blob/master/sample-config.json) for example.
63+
64+
#### Via Array (PHP 7 or higher)
65+
66+
Rather than using a JSON file for configuration, you can set `CTK_CONFIG` to an array of valyes in `wp-config.php`:
3667

3768
```php
3869
define( 'CTK_CONFIG', [ 'disable_emojis' => true, 'admin_bar_color' => '#336699', 'script_attributes' => true, 'meta_generator' => 'Atari 2600' ] );
3970
```
4071

72+
### Caching JSON Config File
73+
74+
If your WordPress Instance has caching enabled, you can configure this plugin to cache the contents of your configuration JSON file with a constant in `wp-config.php`:
75+
76+
```php
77+
define( 'CTK_CACHE_EXPIRE', 120 ); // In seconds
78+
```
79+
80+
### Getting Configuration Values
81+
82+
You can use the `ctk_config` filter to retrieve values from the config registry (including custom). Using [sample-config.json](https://github.com/dmhendricks/wordpress-mu-common-toolkit/blob/master/sample-config.json) as an example:
83+
84+
```php
85+
// Get meta generator value
86+
$meta_generator = apply_filter( 'ctk_config', 'common_toolkit/meta_generator' );
87+
88+
// Get single custom variable
89+
$ny_var = apply_filter( 'ctk_config', 'my_custom_variable' );
90+
91+
// Get an array of classic books
92+
$classic_books = apply_filter( 'ctk_config', 'nested_example/books/classics' );
93+
94+
// Get entire config registry as associative array
95+
$config = apply_filter( 'ctk_config', null );
96+
```
97+
98+
You can add any variable you want to make available to your site's themes and plugins.
99+
41100
## Features
42101

43102
### WordPress Environment
44103

104+
You can set your instance environment using the following methods (in order of precedence; defaults to "production" if not set using any of the following methods):
105+
106+
#### 1. Define a Constant in `wp-config.php`
107+
108+
```php
109+
define( 'WP_ENV', 'staging' );
110+
```
111+
112+
If you wish to use a different constant name, you can set the `environment_constant` in the config:
113+
114+
```php
115+
define( 'MY_ENVIRONMENT', 'development' );
116+
define( 'CTK_CONFIG', [ 'environment_constant' => 'MY_ENVIRONMENT' ] );
117+
```
118+
119+
This will also change the name of the environmental variable used to retrieve the environment:
120+
121+
```php
122+
echo getenv( 'MY_ENVIRONMENT' ); // Result: development
123+
// ...or:
124+
echo apply_filters( 'ctk_environment', null ); // Result: development
125+
```
126+
127+
#### 2. Define `environment` Variable in Config
128+
45129
Setting:
46130

47131
```php
@@ -52,6 +136,8 @@ Getting:
52136

53137
```php
54138
echo getenv( 'WP_ENV' ); // Result: staging
139+
// ...or:
140+
echo apply_filters( 'ctk_environment', null ); // Result: staging
55141
```
56142

57143
If not defined, "production" is returned.
@@ -92,6 +178,77 @@ $parse_uri['fragment'] = 'newhash';
92178
$uri = \MU_Plugins\CommonToolkit::build_url( $parse_uri );
93179
```
94180

181+
### Disable WordPress Core, Plugin and/or Theme Updates
182+
183+
You can disable any of the update notifications or specific. It accepts string, boolean or an array of values. Examples:
184+
185+
```php
186+
// Disable all update notifications
187+
define( 'CTK_CONFIG', [ 'disable_updates' => true ] ); // boolean
188+
189+
// Disable WordPress core and theme updates only (not plugins)
190+
define( 'CTK_CONFIG', [ 'disable_updates' => [ 'core', 'theme' ] ] ); // array
191+
192+
// Disable only plugin updates updates
193+
define( 'CTK_CONFIG', [ 'disable_updates' => 'plugin' ] ); // string
194+
```
195+
196+
## Environment Filter
197+
198+
You can alternately retrieve the current environment using the `ctk_environment` filter:
199+
200+
```php
201+
echo apply_filters( 'ctk_environment', null ); // 'production', 'staging', etc
202+
```
203+
204+
You can also pass `is_production` to determine if we're currently in production more. It compares the value of your environment (defined above) with the value of `common_toolkit/environment_production` (which defaults to "production"). In this way, you can set your production label/string value to whatever you like.
205+
206+
Determining if in production mode using **defaults**:
207+
208+
```php
209+
if( apply_filters( 'ctk_environment', 'is_production' ) ) {
210+
// Do something intended only for production
211+
} else {
212+
// Do something else
213+
}
214+
```
215+
216+
As noted above, you can change the string comparison of what is considered production in config. For example, if you wanted to use "live" instead of "production":
217+
218+
```php
219+
define( 'CTK_CONFIG', [ 'environment_production' => 'live' ] );
220+
221+
// Result: true
222+
define( 'WP_ENV', 'live' ); // wp-config.php
223+
var_dump( apply_filters( 'ctk_environment', 'is_production' ) );
224+
225+
// Result: false
226+
define( 'WP_ENV', 'staging' ); // wp-config.php
227+
var_dump( apply_filters( 'ctk_environment', 'is_production' ) );
228+
```
229+
230+
This special filter value is provided solely for convenience. You may, of course, do a manual comparison:
231+
232+
```php
233+
if( getenv( 'WP_ENV' ) == 'production' ) { // Replace variable with value of `environment_constant`, if set
234+
// Do something intended only for production
235+
} else {
236+
// Do something else
237+
}
238+
```
239+
240+
241+
## Action Hook
242+
243+
If you want to perform some logic only if this script is loaded, you can use the `common_toolkit_loaded` action hook (which executes during the [init](https://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request) phase).
244+
245+
```php
246+
add_action( 'common_toolkit_loaded', function() {
247+
// Do something if common toolkit is loaded ...
248+
var_dump( apply_filters( 'ctk_config', null ) );
249+
});
250+
```
251+
95252
## Shortcodes
96253

97254
### `[get_datetime]`
@@ -111,23 +268,4 @@ Current date/time: [get_datetime]
111268

112269
See PHP's [`date()`](https://php.net/date) function for formatting options.
113270

114-
## Future Plans
115-
116-
- Add support for PHP 5.6
117-
- Add support for external config and`.env` files
118-
- Detect [Classic Editor](https://wordpress.org/plugins/classic-editor/)/[ClassicPress](https://www.classicpress.net/)
119-
- Add ability to change favicons by environment
120-
- Custom WP Admin footer
121-
- Add function to test if WP Object Cache is enabled
122-
- Add rewrite for custom image/script URLs/CDN support
123-
- Hide login errors
124-
- Disable update notifications: core, plugins, themes or all; Option to remove for all but admins
125-
- Change excerpt length
126-
- Ability to add class instance to $GLOBALS
127-
- Remove `?ver=` from some/all scripts
128-
- Disable JSON REST API
129-
- Enable TinyMCE lower toolbar
130-
- Completely disable comments
131-
- Inject standard Google Analytics script
132-
133271
[![Analytics](https://ga-beacon.appspot.com/UA-67333102-2/dmhendricks/wordpress-mu-common-toolkit?flat)](https://github.com/igrigorik/ga-beacon/?utm_source=github.com&utm_medium=referral&utm_content=button&utm_campaign=dmhendricks%2Fwordpress-mu-common-toolkit)

0 commit comments

Comments
 (0)