Download the PHP package inpsyde/wp-context without Composer

On this page you can find all versions of the php package inpsyde/wp-context. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package wp-context

WP Context

A single-class utility to check the current request context in WordPress sites.


PHP Quality Assurance

How to use

This is a Composer package, not a plugin, so first it needs to be installed via Composer.

After that, assuming Composer autoload file is loaded, very early in the load process it is possible to instantiate the WpContext like this:

The library does not implement singleton pattern, nor caches the retrieval of the current context, so it might be a good idea to save the created instance somewhere globally accessible in your plugin/theme/package/application.

Having an instance of WpContext, it is possible to check the current context via its is method, or context-specific methods.

For example:

The method WpContext::is() is convenient to check multiple contexts, context-specific methods are probably better to check a single context.

The full list of contexts that can be checked is:

About "core" and "installing" contexts

WpContext::isCore() checks for the constants ABSPATH being defined, which means that it will normally be true when all the check for other contexts is also true, but WpContext::isInstalling() is an exception to that (more on this below). Another possible exception is WP CLI commands that run before WordPress is loaded.

WpContext::isInstalling() is true when the constant WP_INSTALLING is defined and true, that is when WordPress is installing or upgrading.

In this phase, WpContext returns false for all the other contexts (except for WpContext::isWpCli(), which will be true if the installation/update is happening via WP CLI).

For example, if a cron request is started, and WordPress for any reason sets the WP_INSTALLING constant during that request, WpContext::isCron() will be false, just like WpContext::isCore().

The reason for this is that WordPress is likely not behaving as expected during installation.

For example a code like the following:

which might look very fine, could break if WP_INSTALLING is true, considering in that case the options table might not be there at all. Thanks to the fact that WpContext::isCore() returns false when WP_INSTALLING is true the get_option call above is not executed during installation (when it is not safe to call).

About "installing" and "activate" contexts

The previous section states:

WpContext::isInstalling() is true when the constant WP_INSTALLING is defined and true

but there's an exception to that.

When visiting /wp-activate.php the constant WP_INSTALLING is defined and true, however the issues that usually apply in that case (WP not fully reliable) don't apply there. In fact, when in /wp-activate.php, no "installations" happens, and WP is fully loaded.

This is why /wp-activate.php is a sort of "special case" and WP Context can determine that case via WpContext::isWpActivate(). When that returns true, WpContext::isInstalling() will return false, and WpContext::isCore() will return true, even if WP_INSTALLING is defined and true.

Please note that /wp-activate.php is only available for multisite installations and WpContext::isWpActivate() will always return false in single-site installations.

Ok, but why?

WordPress has core functions and constants to determine the context of current request, so why an additional package?

There are multiple reasons:

Testing code that uses WpContext

Considering that WpContext uses constants and functions to determine current WordPress context it could be hard to unit-test code that make use of it, especially when WordPress is not loaded at all.

In tests, it is possible to obtain an instance of WpContext by calling WpContext::new instead of WpContext::determine() and then use WpContext::force() method to set it to the wanted context

When "forcing" a content different from INSTALLING or CLI, the context CORE is also set to true, not being possible to have, for example, an WordPress AJAX request outside of WordPress core.

The only context, besides CORE, that can be associated with other contexts is CLI. However, force method only accepts a single context, so by using it is not possible to "simulate" a request that is, for example, both CLI and CRON.

For this scope, WpContext has a withCli method, that unlike force does not override current context, but only "appends" CLI context.

For example:

Note that $context->force(WpContext::CLI) can still be used to "simulate" requests that are only WP CLI, not even CORE.

License

This repository is a free software, and is released under the terms of the GNU General Public License version 2 or (at your option) any later version. See LICENSE for complete license.

Contributing

All feedback / bug reports / pull requests are welcome.

Before sending a PR make sure that composer qa outputs no errors.

It will run, in turn:


All versions of wp-context with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package inpsyde/wp-context contains the following files

Loading the files please wait ....